Twinner¶
The MAP optimiser behind ReplayBG.twin() (multi-start Powell, polish of the
n_polish best distinct starts, optional parallelism). Used directly, Twinner
defaults to parallelize=True (unlike ReplayBG.twin(), which defaults to
False), exposes polish=False to skip the polish, and twin() returns
{'fun', 'x'} (the best negative log-posterior and parameter vector).
A class that estimates model parameters by maximizing a posterior objective.
The twinner performs MAP (maximum a posteriori) estimation: it minimizes the
negative log-posterior (-(log_prior + log_likelihood)) over the unknown
parameters using a multi-start Powell optimisation, optionally parallelised
across CPUs with multiprocessing.
...
Attributes:
| Name | Type | Description |
|---|---|---|
parallelize |
bool
|
A boolean that specifies whether to run the multi-start optimisation in parallel. |
n_jobs |
int
|
The number of parallel jobs to use ( |
n_starts |
int
|
The number of multi-start optimisations. |
verbose |
bool
|
A boolean that specifies the verbosity of the twinner. |
polish |
bool
|
A boolean that specifies whether the best multi-start results are refined with a tight-tolerance polish. |
n_polish |
int
|
The number of best distinct multi-start results that are polished. |
log_history |
bool
|
A boolean that specifies whether the optimisation history is recorded. |
history |
dict
|
The recorded optimisation history (only present when |
Methods:
| Name | Description |
|---|---|
twin |
Runs the multi-start MAP estimation and returns the best parameters. |
Source code in src/py_replay_bg/twinner/twinner.py
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 | |
parallelize = parallelize
instance-attribute
¶
n_jobs = -1 if n_jobs is None else n_jobs
instance-attribute
¶
n_starts = n_starts
instance-attribute
¶
verbose = verbose
instance-attribute
¶
polish = polish
instance-attribute
¶
n_polish = n_polish
instance-attribute
¶
log_history = log_history
instance-attribute
¶
history = dict()
instance-attribute
¶
__init__(parallelize=True, n_jobs=None, n_starts=64, log_history=False, verbose=True, polish=True, n_polish=8)
¶
Constructs all the necessary attributes for the Twinner object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parallelize
|
bool
|
A boolean that specifies whether to run the multi-start optimisation in parallel using multiprocessing. |
: True
|
n_jobs
|
int or None
|
The number of parallel jobs to use. If |
: None
|
n_starts
|
int
|
An integer that specifies the number of multi-start optimisations. |
: 64
|
log_history
|
bool
|
A boolean that specifies whether to record the optimisation history. |
: False
|
verbose
|
bool
|
A boolean that specifies the verbosity of the twinner. |
: True
|
polish
|
bool
|
A boolean that specifies whether to run a deterministic tight-tolerance
refinement of the |
: True
|
n_polish
|
int
|
The number of best multi-start results (with distinct objectives)
that are polished; the best polished result is kept. The raw
default-tolerance ranking is a poor guide to which start polishes
into the best optimum, so polishing several starts is far more
reliable than polishing only the best one. The polishes run in
parallel when |
: 8
|
Source code in src/py_replay_bg/twinner/twinner.py
twin(model, rbg_data, unknown_parameters_prior, correlations=None)
¶
Runs the multi-start MAP estimation for a model.
Builds n_starts initial guesses by sampling the parameter priors,
runs a Powell optimisation of the negative log-posterior (bounds enforced
through the prior) from each start (sequentially or in parallel),
polishes the n_polish best starts with distinct objectives (when
polish is on, sequentially or in parallel), and selects the result
with the lowest objective. Integer
parameters are rounded and all parameters are clipped to their bounds
before being returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
A model instance being fit, implementing |
required |
rbg_data
|
object
|
A data object used to compute the likelihood. |
required |
unknown_parameters_prior
|
dict
|
A dictionary describing the priors and bounds for each parameter to
estimate (keys |
required |
correlations
|
dict or None
|
Optional pairwise prior correlations specified as
|
: None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary with keys |
Source code in src/py_replay_bg/twinner/twinner.py
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 | |
_polish(best, model, rbg_data, unknown_parameters_prior, correlation_structure=None, max_rounds=5, tol=1e-08, rel_improve=1e-07, evals_per_dim=1000, max_budget=8000)
¶
Refines the best multi-start result with a tight-tolerance Powell loop.
Powell resets its search-direction set on each fresh call, so restarting it from the incumbent with tight tolerances lets it keep descending a narrow, ill-conditioned valley that a single default-tolerance run stops short of. The loop repeats until a round fails to improve the objective by a meaningful relative amount, so it stops promptly once the optimum is reached. The starting point is fixed and no randomness is involved, so the result is deterministic.
Cost is bounded on purpose: each restart's function-evaluation budget is
min(evals_per_dim * n_params, max_budget) and the number of restarts is
capped, with an early stop once a round stops improving. The absolute
max_budget cap keeps a single restart affordable on high-dimensional
models (e.g. the 25-parameter extended model, where an uncapped tight
Powell runs for many minutes), while the max_rounds restarts let the
cheaper low-dimensional models still converge fully. The bounds depend only
on fixed problem sizes, so the result stays deterministic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
best
|
OptimizeResult
|
The best multi-start result; its |
required |
model
|
object
|
The model instance being fit (mutated in place by each evaluation). |
required |
rbg_data
|
object
|
The data object used to compute the likelihood. |
required |
unknown_parameters_prior
|
dict
|
The priors/bounds spec (fixes parameter order and integer handling). |
required |
correlation_structure
|
dict or None
|
The precomputed Gaussian-copula structure, or |
: None
|
max_rounds
|
int
|
Maximum number of tight-tolerance Powell restarts (the early stop often triggers before this). |
: 5
|
tol
|
float
|
The |
: 1e-8
|
rel_improve
|
float
|
Minimum relative objective improvement |
: 1e-7
|
evals_per_dim
|
int
|
Per-restart function-evaluation budget per parameter (before the cap). |
: 1000
|
max_budget
|
int
|
Absolute cap on a single restart's function-evaluation budget; bounds the per-restart cost on high-dimensional models. |
: 8000
|
Returns:
| Type | Description |
|---|---|
OptimizeResult
|
|
Source code in src/py_replay_bg/twinner/twinner.py
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 | |
_log_prior(model, unknown_parameters_prior, correlation_structure=None)
¶
Computes the log prior probability of the model parameters.
The base term is the sum of marginal log-densities. When a Gaussian-copula
correlation_structure is supplied, a copula correction term is added so
that the named parameters are jointly correlated while keeping their
marginals unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
A model instance containing the current parameter values. |
required |
unknown_parameters_prior
|
dict
|
A dictionary describing the priors for each parameter. Each prior
must provide a |
required |
correlation_structure
|
dict or None
|
Precomputed Gaussian-copula structure (see
:func: |
: None
|
Returns:
| Type | Description |
|---|---|
float
|
The log prior probability, or |
Source code in src/py_replay_bg/twinner/twinner.py
_log_likelihood(model, rbg_data)
¶
Computes the log likelihood of the observed data under the model.
The model is simulated forward using the input sequence in rbg_data.
The predicted output is then compared against the observed glucose
measurements using a Gaussian error model with a constant coefficient of
variation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
A model instance to simulate. |
required |
rbg_data
|
object
|
A data object containing inputs, output timestamps, and observed glucose values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The log likelihood value for the simulated trajectory. |
Source code in src/py_replay_bg/twinner/twinner.py
_neg_log_posterior(theta, model, rbg_data, unknown_parameters_prior, correlation_structure=None)
¶
Returns the negative log posterior for optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
ndarray
|
A parameter vector in the natural (constrained) parameter space,
ordered to match |
required |
model
|
object
|
A model instance being fit. |
required |
rbg_data
|
object
|
A data object used to compute the likelihood. |
required |
unknown_parameters_prior
|
dict
|
A dictionary describing priors and bounds for the parameters. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The negative log posterior value. |
Source code in src/py_replay_bg/twinner/twinner.py
_log_posterior(theta, model, rbg_data, unknown_parameters_prior, correlation_structure=None)
¶
Compute log-prior, log-likelihood, and log-posterior in one model pass.
The parameter vector is used directly in the natural (constrained)
parameter space: each entry is written into the model's typed parameter
dict (integer parameters are rounded) before model.reset. Every reset
also re-seeds the cold-start glucose and the t=0 input row from
rbg_data (see Model.reset), since reset clears them each call.
Bounds are enforced by the prior, which returns -inf outside
[min, max].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
ndarray
|
A parameter vector in the natural (constrained) parameter space,
ordered to match |
required |
model
|
object
|
A model instance being updated with candidate parameters. |
required |
rbg_data
|
object
|
A data object used to compute the likelihood. |
required |
unknown_parameters_prior
|
dict
|
A dictionary describing priors and bounds for the parameters. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
The triple |