Metrics API¶
Metrics for measuring multi-agent system health.
SoftMetrics¶
Core metrics computed from soft probabilistic labels.
swarm.metrics.soft_metrics.SoftMetrics
¶
Computes soft (probabilistic) metrics for interaction quality.
Key metrics: - Toxicity rate: E[1-p | accepted] - Conditional loss: E[π_a | accepted] - E[π_a] - Spread: Quality filtering effectiveness - Quality gap: E[p | accepted] - E[p | rejected]
Source code in swarm/metrics/soft_metrics.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | |
__init__(payoff_engine=None)
¶
Initialize metrics calculator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payoff_engine
|
Optional[SoftPayoffEngine]
|
Engine for payoff calculations (default: SoftPayoffEngine()) |
None
|
Source code in swarm/metrics/soft_metrics.py
average_quality(interactions, accepted_only=False)
¶
Compute average quality E[p].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
accepted_only
|
bool
|
If True, only consider accepted interactions |
False
|
Returns:
| Type | Description |
|---|---|
float
|
Average p value |
Source code in swarm/metrics/soft_metrics.py
brier_score(interactions)
¶
Compute Brier score: E[(p - v)^2] where v = (ground_truth + 1) / 2.
The Brier score is a proper scoring rule for probabilistic predictions. - 0 is perfect prediction - 0.25 is equivalent to always predicting p=0.5
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions with ground_truth set |
required |
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Brier score in [0, 1], or None if no ground truth available |
Source code in swarm/metrics/soft_metrics.py
calibration_curve(interactions, bins=10)
¶
Compute calibration curve data.
For each bin of predicted probabilities, compute the fraction of actually positive outcomes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions with ground_truth set |
required |
bins
|
int
|
Number of probability bins |
10
|
Returns:
| Type | Description |
|---|---|
List[Tuple[float, float, int]]
|
List of (mean_predicted, fraction_positive, count) per bin. |
List[Tuple[float, float, int]]
|
Returns empty list if no ground truth available. |
Source code in swarm/metrics/soft_metrics.py
calibration_error(interactions)
¶
Compute calibration error: E[p] - empirical_positive_rate.
Requires ground_truth to be set on interactions. A well-calibrated model has calibration error near 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions with ground_truth set |
required |
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Calibration error, or None if no ground truth available |
Source code in swarm/metrics/soft_metrics.py
coefficient_of_variation(interactions)
¶
Compute coefficient of variation (CV = std/mean) for key metrics.
CV is a standardized measure of dispersion. Higher CV indicates more variability relative to the mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with CV for p, π_a, and π_b |
Source code in swarm/metrics/soft_metrics.py
conditional_loss_counterparty(interactions)
¶
Compute conditional loss for counterparty: E[π_b | accepted] - E[π_b]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
float
|
Conditional loss (negative = adverse selection) |
Source code in swarm/metrics/soft_metrics.py
conditional_loss_initiator(interactions)
¶
Compute conditional loss for initiator: E[π_a | accepted] - E[π_a]
Negative values indicate adverse selection (accepted interactions are worse than average for the initiator).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
float
|
Conditional loss (negative = adverse selection) |
Source code in swarm/metrics/soft_metrics.py
discrimination_auc(interactions)
¶
Compute Area Under ROC Curve (AUC) for discrimination.
AUC measures the model's ability to rank positive cases higher than negative cases. - AUC = 0.5: random guessing - AUC = 1.0: perfect discrimination
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions with ground_truth set |
required |
Returns:
| Type | Description |
|---|---|
Optional[float]
|
AUC value in [0, 1], or None if insufficient data |
Source code in swarm/metrics/soft_metrics.py
expected_calibration_error(interactions, bins=10)
¶
Compute Expected Calibration Error (ECE).
ECE is the weighted average of |E[p|bin] - accuracy(bin)| across bins. A perfectly calibrated model has ECE = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions with ground_truth set |
required |
bins
|
int
|
Number of probability bins |
10
|
Returns:
| Type | Description |
|---|---|
Optional[float]
|
ECE value, or None if no ground truth available |
Source code in swarm/metrics/soft_metrics.py
flag_uncertain(interactions, band=0.2)
¶
Flag interactions with uncertain labels (p near 0.5).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
band
|
float
|
Width of uncertainty band around 0.5 |
0.2
|
Returns:
| Type | Description |
|---|---|
List[SoftInteraction]
|
List of uncertain interactions |
Source code in swarm/metrics/soft_metrics.py
log_loss(interactions, eps=1e-15)
¶
Compute log loss (cross-entropy): -E[vlog(p) + (1-v)log(1-p)].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions with ground_truth set |
required |
eps
|
float
|
Small value to avoid log(0) |
1e-15
|
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Log loss (lower is better), or None if no ground truth available |
Source code in swarm/metrics/soft_metrics.py
participation_by_quality(interactions, threshold=0.5)
¶
Compute acceptance rates for high/low quality interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
threshold
|
float
|
Quality threshold (default 0.5) |
0.5
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with acceptance rates: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Source code in swarm/metrics/soft_metrics.py
payoff_variance_counterparty(interactions)
¶
Compute variance of counterparty payoffs: Var[π_b].
Measures risk/dispersion in counterparty outcomes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
float
|
Variance of counterparty payoffs |
Source code in swarm/metrics/soft_metrics.py
payoff_variance_initiator(interactions)
¶
Compute variance of initiator payoffs: Var[π_a].
Measures risk/dispersion in initiator outcomes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
float
|
Variance of initiator payoffs |
Source code in swarm/metrics/soft_metrics.py
quality_distribution(interactions, bins=10)
¶
Compute quality distribution histogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
bins
|
int
|
Number of bins |
10
|
Returns:
| Type | Description |
|---|---|
List[Tuple[float, float, int]]
|
List of (bin_start, bin_end, count) tuples |
Source code in swarm/metrics/soft_metrics.py
quality_gap(interactions)
¶
Compute quality gap: E[p | accepted] - E[p | rejected]
Negative quality gap indicates adverse selection (accepted interactions have lower quality than rejected ones).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
float
|
Quality gap (negative = adverse selection) |
Source code in swarm/metrics/soft_metrics.py
quality_std(interactions, accepted_only=False)
¶
Compute standard deviation of quality: Std[p].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
accepted_only
|
bool
|
If True, only consider accepted interactions |
False
|
Returns:
| Type | Description |
|---|---|
float
|
Standard deviation of p |
Source code in swarm/metrics/soft_metrics.py
quality_variance(interactions, accepted_only=False)
¶
Compute variance of quality: Var[p].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
accepted_only
|
bool
|
If True, only consider accepted interactions |
False
|
Returns:
| Type | Description |
|---|---|
float
|
Variance of p |
Source code in swarm/metrics/soft_metrics.py
spread(interactions)
¶
Compute spread: (s_plus + s_minus) * (E[p] - E[p | accepted])
Positive spread indicates the market is filtering out high-quality interactions (adverse selection in the quality sense).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
float
|
Spread value |
Source code in swarm/metrics/soft_metrics.py
toxicity_rate(interactions)
¶
Compute toxicity rate: E[1-p | accepted]
This measures the expected fraction of harmful outcomes among accepted interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
float
|
Toxicity rate in [0, 1], or 0.0 if no accepted interactions |
Source code in swarm/metrics/soft_metrics.py
toxicity_rate_all(interactions)
¶
Compute unconditional toxicity rate: E[1-p]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
float
|
Toxicity rate in [0, 1] |
Source code in swarm/metrics/soft_metrics.py
uncertain_fraction(interactions, band=0.2)
¶
Compute fraction of interactions with uncertain labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
band
|
float
|
Width of uncertainty band around 0.5 |
0.2
|
Returns:
| Type | Description |
|---|---|
float
|
Fraction in [0, 1] |
Source code in swarm/metrics/soft_metrics.py
welfare_metrics(interactions)
¶
Compute aggregate welfare metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interactions
|
List[SoftInteraction]
|
List of interactions |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with welfare metrics |
Source code in swarm/metrics/soft_metrics.py
Usage¶
from swarm.metrics.soft_metrics import SoftMetrics
metrics = SoftMetrics()
# Compute individual metrics
toxicity = metrics.toxicity_rate(interactions)
quality_gap = metrics.quality_gap(interactions)
conditional_loss = metrics.conditional_loss(interactions, payoff_engine)
MetricsReporter¶
Dual reporting of soft and hard metrics.
from swarm.metrics.reporters import MetricsReporter
reporter = MetricsReporter(threshold=0.5)
# Generate report
report = reporter.format_report(interactions, verbose=True)
print(report)
# Get structured data
data = reporter.compute_all(interactions)
print(data['soft']['toxicity_rate'])
print(data['hard']['true_positive_rate'])
Report Format¶
=== SWARM Metrics Report ===
Interactions: 100 (70 accepted, 30 rejected)
Soft Metrics:
Toxicity Rate: 0.287
Quality Gap: 0.142
Conditional Loss: -0.051
Hard Metrics (threshold=0.5):
Accept Rate: 0.700
True Positive: 0.821
False Positive: 0.179
Incoherence Metrics¶
Measure decision variance across replays.
from swarm.metrics.incoherence import IncoherenceMetrics, DecisionRecord
incoherence = IncoherenceMetrics()
# Record decisions across replays
for replay in replays:
record = DecisionRecord(
decision_id=decision_id,
replay_id=replay.id,
decision=replay.decision,
outcome=replay.outcome,
)
incoherence.record(record)
# Compute incoherence index
I = incoherence.compute_index()
print(f"Incoherence Index: {I:.3f}")
Incoherence Components¶
| Component | Formula | Meaning |
|---|---|---|
| D | Var[decision] | Decision variance |
| E | E[error] | Expected error |
| I | D / E | Incoherence index |
Collusion Metrics¶
Detect coordinated behavior.
from swarm.metrics.collusion import CollusionMetrics
collusion = CollusionMetrics()
# Analyze pair-level patterns
pair_scores = collusion.pair_analysis(interactions)
# Analyze group-level patterns
group_scores = collusion.group_analysis(interactions, group_size=3)
# Get suspicious pairs
suspicious = collusion.get_suspicious_pairs(threshold=0.8)
Security Metrics¶
Track security-related signals.
from swarm.metrics.security import SecurityMetrics
security = SecurityMetrics()
# Compute security scores
attack_rate = security.attack_detection_rate(interactions)
evasion_rate = security.governance_evasion_rate(interactions, governance)
damage = security.total_externality(interactions)
Capability Metrics¶
Track emergent capabilities.
from swarm.metrics.capabilities import CapabilityMetrics
capabilities = CapabilityMetrics()
# Compute capability scores
task_completion = capabilities.task_completion_rate(interactions)
collaboration_success = capabilities.collaboration_success_rate(interactions)
composite_capability = capabilities.composite_task_capability(interactions)
Custom Metrics¶
Create custom metrics:
from swarm.metrics.base import BaseMetric
class CustomMetric(BaseMetric):
def compute(self, interactions: list) -> float:
# Custom computation
accepted = [i for i in interactions if i.accepted]
return sum(i.p for i in accepted) / len(accepted) if accepted else 0.0
# Use in reporter
reporter = MetricsReporter(
extra_metrics={'custom': CustomMetric()}
)
Aggregation¶
Aggregate metrics across epochs or runs.