Calibrator API¶
The Calibrator class provides a unified interface for both standard and probabilistic calibration.
Methods¶
run()- Runs standard single-solution calibration using theoptimization_configfrom the problemrun_probabilistic()- Runs probabilistic calibration using theprobabilistic_configfrom the problem
Calibrator ¶
A Facade for running parameter calibration from a defined CalibrationProblem.
Source code in commol/api/calibrator.py
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 | |
Attributes¶
Functions¶
__init__ ¶
__init__(simulation: Simulation, problem: CalibrationProblem)
Initializes the calibration from a Simulation and CalibrationProblem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulation
|
Simulation
|
A fully initialized Simulation object with the model to calibrate. |
required |
problem
|
CalibrationProblem
|
A fully constructed and validated calibration problem definition. |
required |
Source code in commol/api/calibrator.py
run ¶
run() -> CalibrationResult
Runs the calibration optimization.
Returns:
| Type | Description |
|---|---|
CalibrationResult
|
Object containing the optimized parameter values, final loss, convergence status, and other optimization statistics. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Rust extension is not available. |
ValueError
|
If calibration problem setup is invalid. |
RuntimeError
|
If optimization fails. |
Source code in commol/api/calibrator.py
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 | |
run_with_history ¶
Runs the calibration optimization and returns evaluation history.
Returns all objective function evaluations that occurred during optimization, not just the final best result. This is useful for probabilistic calibration where we want to explore the parameter space.
Returns:
| Type | Description |
|---|---|
CalibrationResultWithHistory
|
Object containing optimized parameters, final loss, and all evaluations. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Rust extension is not available. |
ValueError
|
If calibration problem setup is invalid. |
RuntimeError
|
If optimization fails. |
Source code in commol/api/calibrator.py
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 | |
run_probabilistic ¶
Run probabilistic calibration.
Returns:
| Type | Description |
|---|---|
ProbabilisticCalibrationResult
|
Object containing the ensemble of parameter sets, statistics, predictions with confidence intervals, and coverage metrics. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If probabilistic_config is not set in the CalibrationProblem. |
RuntimeError
|
If calibration or ensemble selection fails. |
Source code in commol/api/calibrator.py
options: show_root_heading: true show_source: true heading_level: 2 show_docstring_attributes: false
Related Classes¶
CalibrationProblem¶
CalibrationProblem ¶
Bases: BaseModel
Defines a complete calibration problem.
This class encapsulates all the information needed to calibrate model parameters against observed data. It provides validation of the calibration setup but delegates the actual optimization to the Rust backend.
Attributes:
| Name | Type | Description |
|---|---|---|
observed_data |
list[ObservedDataPoint]
|
List of observed data points to fit against |
parameters |
list[CalibrationParameter]
|
List of parameters to calibrate with their bounds |
constraints |
list[CalibrationConstraint]
|
List of constraints on calibration parameters (optional, default: empty list) |
loss_function |
str
|
Loss function to use for measuring fit quality (default: "sse") |
optimization_config |
OptimizationConfig
|
Configuration for the optimization algorithm |
probabilistic_config |
ProbabilisticCalibrationConfig | None
|
Optional configuration for probabilistic calibration (default: None). When provided, enables ensemble-based parameter estimation with uncertainty quantification instead of single-point optimization. |
seed |
int | None
|
Random seed for reproducibility across all stochastic processes (default: None, uses system entropy). Controls randomness in: - Optimization algorithms (e.g., Particle Swarm initialization) - Probabilistic calibration runs - Clustering algorithms - Ensemble selection When set, all random operations become deterministic and reproducible. |
Source code in commol/context/calibration.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 | |
Functions¶
validate_unique_parameter_ids ¶
Ensure parameter IDs are unique.
Source code in commol/context/calibration.py
options: show_root_heading: true show_source: false heading_level: 3 show_docstring_attributes: true
CalibrationResult¶
CalibrationResult ¶
Bases: BaseModel
Result of a calibration run.
This is a simple data class that holds the results returned from the Rust calibration function.
Attributes:
| Name | Type | Description |
|---|---|---|
best_parameters |
dict[str, float]
|
Dictionary mapping parameter IDs to their calibrated values |
final_loss |
float
|
Final loss value achieved |
iterations |
int
|
Number of iterations performed |
converged |
bool
|
Whether the optimization converged |
termination_reason |
str
|
Explanation of why optimization terminated |
Source code in commol/context/calibration.py
Functions¶
__str__ ¶
String representation of calibration result.
Source code in commol/context/calibration.py
options: show_root_heading: true show_source: false heading_level: 3 show_docstring_attributes: true
CalibrationParameter¶
CalibrationParameter ¶
Bases: BaseModel
Defines a parameter or initial condition to be calibrated with its bounds.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Identifier (parameter ID for parameters, bin ID for initial conditions) |
parameter_type |
str
|
Type of value being calibrated |
min_bound |
float
|
Minimum allowed value for this parameter |
max_bound |
float
|
Maximum allowed value for this parameter |
initial_guess |
float | None
|
Optional starting value for optimization (if None, midpoint is used) |
Source code in commol/context/calibration.py
Functions¶
validate_bounds ¶
Validate that max_bound > min_bound and initial_guess is within bounds.
Source code in commol/context/calibration.py
options: show_root_heading: true show_source: false heading_level: 3 show_docstring_attributes: true
ObservedDataPoint¶
ObservedDataPoint ¶
Bases: BaseModel
Represents a single observed data point for calibration.
Attributes:
| Name | Type | Description |
|---|---|---|
step |
int
|
Time step of the observation |
compartment |
str
|
Name of the compartment being observed |
value |
float
|
Observed value |
weight |
float
|
Weight for this observation in the loss function (default: 1.0) |
scale_id |
str | None
|
Optional scale parameter ID to apply to model output before comparison |
Source code in commol/context/calibration.py
options: show_root_heading: true show_source: false heading_level: 3 show_docstring_attributes: true
OptimizationConfig¶
Type Alias
OptimizationConfig is a type alias for NelderMeadConfig | ParticleSwarmConfig.
NelderMeadConfig¶
NelderMeadConfig ¶
Bases: BaseModel
Configuration for the Nelder-Mead optimization algorithm.
The Nelder-Mead method is a simplex-based derivative-free optimization algorithm, suitable for problems where gradients are not available.
Attributes:
| Name | Type | Description |
|---|---|---|
max_iterations |
int
|
Maximum number of iterations (default: 1000) |
sd_tolerance |
float
|
Convergence tolerance for standard deviation (default: 1e-6) |
simplex_perturbation |
float
|
Multiplier for creating initial simplex vertices by perturbing each parameter dimension. A value of 1.1 means 10% perturbation. (default: 1.1) |
alpha |
float | None
|
Reflection coefficient (default: None, uses argmin's default) |
gamma |
float | None
|
Expansion coefficient (default: None, uses argmin's default) |
rho |
float | None
|
Contraction coefficient (default: None, uses argmin's default) |
sigma |
float | None
|
Shrink coefficient (default: None, uses argmin's default) |
verbose |
bool
|
Enable verbose output during optimization (default: False) |
header_interval |
int
|
Number of iterations between table header repeats in verbose output (default: 100) |
Source code in commol/context/calibration.py
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 | |
options: show_root_heading: true show_source: false heading_level: 3 show_docstring_attributes: true
ParticleSwarmConfig¶
ParticleSwarmConfig ¶
Bases: BaseModel
Configuration for the Particle Swarm Optimization algorithm.
Attributes:
| Name | Type | Description |
|---|---|---|
num_particles |
int
|
Number of particles in the swarm (default: 20) |
max_iterations |
int
|
Maximum number of iterations (default: 1000) |
verbose |
bool
|
Enable verbose output (default: False) |
initialization |
str
|
Particle initialization strategy (default: "uniform") |
Methods:
| Name | Description |
|---|---|
inertia |
Set inertia weight strategy ("constant" or "chaotic") |
acceleration |
Set acceleration coefficients ("constant" or "time_varying") |
mutation |
Enable mutation to escape local optima |
velocity |
Configure velocity control |
Source code in commol/context/calibration.py
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 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 | |
Attributes¶
inertia_config
property
¶
Get the inertia configuration (for internal use).
acceleration_config
property
¶
Get the acceleration configuration (for internal use).
mutation_config
property
¶
Get the mutation configuration (for internal use).
velocity_config
property
¶
Get the velocity configuration (for internal use).
Functions¶
inertia ¶
Set inertia weight strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str
|
Inertia strategy type: "constant" or "chaotic" |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
factor |
float
|
Fixed inertia weight (canonical PSO: 1/(2*ln(2)) ≈ 0.721). Only used when type="constant". |
w_min |
float
|
Minimum inertia weight. Only used when type="chaotic". |
w_max |
float
|
Maximum inertia weight (must be > w_min). Only used when type="chaotic". |
Returns:
| Type | Description |
|---|---|
Self
|
The config instance for method chaining |
Source code in commol/context/calibration.py
acceleration ¶
Set acceleration coefficient strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str
|
Acceleration strategy type: "constant" or "time_varying" |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
cognitive |
float
|
Cognitive coefficient (c1) - attraction to personal best. Only used when type="constant". |
social |
float
|
Social coefficient (c2) - attraction to swarm best. Only used when type="constant". |
c1_initial |
float
|
Initial cognitive factor (typically high, e.g., 2.5). Only used when type="time_varying". |
c1_final |
float
|
Final cognitive factor (typically low, e.g., 0.5). Only used when type="time_varying". |
c2_initial |
float
|
Initial social factor (typically low, e.g., 0.5). Only used when type="time_varying". |
c2_final |
float
|
Final social factor (typically high, e.g., 2.5). Only used when type="time_varying". |
Returns:
| Type | Description |
|---|---|
Self
|
The config instance for method chaining |
Source code in commol/context/calibration.py
mutation ¶
Enable mutation to help escape local optima.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
str
|
Mutation distribution: "gaussian" or "cauchy". Cauchy has heavier tails for larger jumps. |
required |
scale
|
float
|
Standard deviation (gaussian) or scale parameter (cauchy) |
required |
probability
|
float
|
Mutation probability per iteration (0.0 to 1.0) |
required |
application
|
str
|
Which particles to mutate: "global_best", "all_particles", or "below_average" |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The config instance for method chaining |
Source code in commol/context/calibration.py
velocity ¶
Configure velocity control.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clamp_factor
|
float
|
Velocity clamping as fraction of search space (0.0-1.0). Typically 0.1-0.2. Prevents explosive velocities. |
None
|
mutation_threshold
|
float
|
Reinitialize velocities below this threshold. Typically 0.001-0.01. Prevents stagnation. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
The config instance for method chaining |
Source code in commol/context/calibration.py
options: show_root_heading: true show_source: false heading_level: 3 show_docstring_attributes: true
Enumerations¶
LossFunction¶
LossFunction ¶
Bases: StrEnum
Available loss functions for calibration.
Values
SSE Sum of Squared Errors RMSE Root Mean Squared Error MAE Mean Absolute Error WEIGHTED_SSE Weighted Sum of Squared Errors
Source code in commol/context/constants.py
options: show_root_heading: true show_source: false heading_level: 3 members: true
OptimizationAlgorithm¶
OptimizationAlgorithm ¶
Bases: StrEnum
Available optimization algorithms.
Values
NELDER_MEAD Nelder-Mead simplex algorithm PARTICLE_SWARM Particle Swarm Optimization
Source code in commol/context/constants.py
options: show_root_heading: true show_source: false heading_level: 3 members: true