Simulation results
A simulation result work with the following logic :
- each iteration (step) is a step_result instance.
- series result aggregates all the before and
SimulationStepResult
dataclass
¶
Result of a simulation step.
The comparison matrix is stored as a symmetric float32 2D array so that
any distance metric (binary, Jaccard, etc.) can be used.
Source code in src/vote_simulation/models/results/step_result.py
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 | |
dist_matrix
property
¶
Read-only 2D matrix of pairwise distances between rules.
distance_matrix_frame
property
¶
Distance matrix as a labeled DataFrame for display and analysis.
mean_distance
property
¶
Mean of all off-diagonal pairwise distances (O(1) numpy ops).
metrics_by_rule
property
¶
Mapping from rule code to its :class:WinnerMetrics for this step.
Only rules registered via :meth:add_method_result_with_metrics will
appear here. Rules loaded from disk (without metrics) return an empty
dict for their key.
metrics_frame
property
¶
Metrics for all rules in this step as a tidy DataFrame.
Returns a DataFrame indexed by rule with one column per metric
field (see :data:~vote_simulation.models.rules.winner_metrics.METRIC_FIELDS).
Rules without metrics are omitted.
most_distant_rules
property
¶
Pair of rules with the maximum distance.
Returns:
| Type | Description |
|---|---|
str
|
|
str
|
than two rules are present. |
rule_codes
property
¶
Ordered rule codes matching the matrix axes.
__post_init__()
¶
Normalize any pre-populated data and build the matrix once.
Source code in src/vote_simulation/models/results/step_result.py
__str__()
¶
String representation with a readable matrix block.
Source code in src/vote_simulation/models/results/step_result.py
add_method_result(rule_code, winners)
¶
Add or update winners for one voting method in this step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule_code
|
str
|
str - Code of the voting method (e.g., "STV", "IRV", "Borda") |
required |
winners
|
list[str]
|
list[str] - List of winner labels for the given method. Can be multiple in case of ties. |
required |
Source code in src/vote_simulation/models/results/step_result.py
add_method_result_with_metrics(rule_code, winners, metrics)
¶
Add winners and pre-computed :class:WinnerMetrics for one rule.
This is the enriched variant of :meth:add_method_result used by the
simulation engine so that winner-quality metrics can be aggregated
across iterations with no extra recomputation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule_code
|
str
|
Voting rule code (e.g. |
required |
winners
|
list[str]
|
List of co-winner labels. |
required |
metrics
|
WinnerMetrics
|
Pre-computed :class: |
required |
Source code in src/vote_simulation/models/results/step_result.py
compute_distance_matrix()
¶
Rebuild the full distance matrix from winners and return it.
Source code in src/vote_simulation/models/results/step_result.py
delete_file(file_path)
staticmethod
¶
Delete a saved step result file.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/vote_simulation/models/results/step_result.py
format_distance_matrix()
¶
Return a printable matrix with row and column labels.
load_from_file(file_path)
¶
Load the step result from a parquet file.
Reads configuration metadata from the parquet schema when available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the parquet file containing the step result. |
required |
Source code in src/vote_simulation/models/results/step_result.py
plot_distance_matrix(ax=None, save_path=None, *, annotate=True, show=True)
¶
Plot the distance matrix as a heatmap.
When save_path is given the plot is written to disk. If save_path
is a directory, the filename is derived automatically from the
attached :attr:config.
Source code in src/vote_simulation/models/results/step_result.py
save_to_file(file_path)
¶
Save the step result to a parquet file.
Configuration metadata is stored via pyarrow schema metadata so that the payload columns remain compact ("Rule" + "Winner" only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the output parquet file. |
required |
Source code in src/vote_simulation/models/results/step_result.py
Data models for simulation outputs across multiple iterations.
SimulationSeriesResult
dataclass
¶
Aggregation of simulation steps.
Maintains a running float64 sum of per-step distance matrices so
that the mean can be computed with a single division at any time, regardless
of how many iterations have been added.
The aggregated :attr:config is automatically updated on each
:meth:add_step call and reflects the union of all per-step configs.
Source code in src/vote_simulation/models/results/series_result.py
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 | |
config
property
writable
¶
Aggregated configuration across all added steps.
mean_distance
property
¶
Scalar mean of all off-diagonal cells in the mean distance matrix.
Value in [0, 100].
mean_distance_matrix
property
¶
Mean pairwise distance matrix over all accumulated steps.
Returns a float32 array of shape (n_rules, n_rules).
Values are in [0, 100]: 0 means every step agreed, 100 means they never did.
mean_distance_matrix_frame
property
¶
Mean distance matrix as a labeled DataFrame.
metrics_summary_frame
property
¶
Per-rule winner-metric statistics aggregated across all iterations.
Returns a :class:~pandas.DataFrame indexed by rule with two
columns per metric field — one for the mean and one for the standard
deviation across all accumulated steps:
<field>_mean, <field>_std for each field in
:data:~vote_simulation.models.rules.winner_metrics.METRIC_FIELDS.
Rules for which no metrics were recorded (e.g. loaded from a parquet file without metrics) are omitted from the frame.
An empty DataFrame is returned when no metrics have been accumulated.
most_distant_rules
property
¶
Pair of rules with the maximum mean distance.
Returns:
| Type | Description |
|---|---|
str
|
|
str
|
than two rules are present. |
step_count
property
¶
Number of recorded steps (equals the iteration count).
add_rules_to_steps(new_rule_codes)
¶
Apply additional rules to all existing steps and update the series.
Does not re-run existing rules, only computes distances for new rules. Rebuilds the accumulated distance matrix with all rules (old + new).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_rule_codes
|
list[str]
|
List of additional rule codes to apply to each step. |
required |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in src/vote_simulation/models/results/series_result.py
add_step(step_result)
¶
Add one step result to the series and accumulate its distance matrix.
Source code in src/vote_simulation/models/results/series_result.py
delete_file(file_path)
staticmethod
¶
Delete a saved series result file.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/vote_simulation/models/results/series_result.py
load_from_file(file_path)
¶
Load the series result from a parquet file and rebuild the accumulator.
Reads per-step config from row columns and aggregated config from schema metadata. Backwards-compatible with files lacking config columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the parquet file containing the series result. |
required |
Source code in src/vote_simulation/models/results/series_result.py
map_rules_2d()
¶
Project rules into 2D using Multi-Dimensional Scaling (MDS).
Uses the mean distance matrix as a precomputed dissimilarity matrix so that pairwise distances in the 2D plane approximate the original rule-to-rule distances.
Returns:
| Type | Description |
|---|---|
MdsProjection
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no steps have been added yet. |
Source code in src/vote_simulation/models/results/series_result.py
map_rules_3d()
¶
Project rules into 3D using Multi-Dimensional Scaling (MDS).
Uses the mean distance matrix as a precomputed dissimilarity matrix so that pairwise distances in the 3D space approximate the original rule-to-rule distances.
Returns:
| Type | Description |
|---|---|
MdsProjection
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no steps have been added yet. |
Source code in src/vote_simulation/models/results/series_result.py
plot_mean_distance_matrix(ax=None, folder_save_path=None, *, annotate=True, show=True)
¶
Plot the mean distance matrix as a heatmap.
Cell values show the percentage of iterations where two rules disagreed. When multi‑config (several models / voter counts / candidate counts), the title mentions all of them.
Source code in src/vote_simulation/models/results/series_result.py
plot_rules_2d(ax=None, *, show=True, save_path=None)
¶
Plot rules as labeled points in a 2D MDS projection.
Distances between points approximate mean pairwise rule distances. The normalized MDS stress is shown on the plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Any | None
|
Optional matplotlib Axes to draw on. A new figure is created when None. |
None
|
show
|
bool
|
Whether to call |
True
|
save_path
|
str | None
|
Optional path (file or directory) to save the plot. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The matplotlib Axes used for plotting. |
Source code in src/vote_simulation/models/results/series_result.py
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 | |
plot_rules_3d(ax=None, *, show=True, save_path=None)
¶
Plot rules as labeled points in a 3D MDS projection.
Distances between points approximate mean pairwise rule distances. The normalized MDS stress is shown on the plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Any | None
|
Optional matplotlib Axes to draw on. A new figure is created when None. |
None
|
show
|
bool
|
Whether to call |
True
|
save_path
|
str | None
|
Optional path (file or directory) to save the plot. |
None
|
Source code in src/vote_simulation/models/results/series_result.py
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 | |
save_to_file(file_path)
¶
Save the series result to a parquet file.
Per-step config is stored in columns GenModel, NVoters,
NCandidates so that each row is self-describing. The aggregated
series config is stored in schema metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the output parquet file. |
required |