API Reference¶
Inference¶
Runs simulation-based inference and returns the posterior.
This function provides a simple interface to run sbi. Inference is run for a single round and hence the returned posterior \(p(\theta|x)\) can be sampled and evaluated for any \(x\) (i.e. it is amortized).
The scope of this function is limited to the most essential features of sbi. For more flexibility (e.g. multi-round inference, different density estimators) please use the flexible interface described here: https://www.mackelab.org/sbi/tutorial/02_flexible_interface/
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulator |
Callable
|
A function that takes parameters \(\theta\) and maps them to
simulations, or observations, |
required |
prior |
Distribution
|
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. Any
object with |
required |
method |
str
|
What inference method to use. Either of SNPE, SNLE or SNRE. |
required |
num_simulations |
int
|
Number of simulation calls. More simulations means a longer runtime, but a better posterior estimate. |
required |
num_workers |
int
|
Number of parallel workers to use for simulations. |
1
|
Returns: Posterior over parameters conditional on observations (amortized).
Source code in sbi/inference/base.py
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 |
|
Prepare simulator and prior for usage in sbi.
NOTE: This is a wrapper around process_prior
and process_simulator
which can be
used in isolation as well.
Attempts to meet the following requirements by reshaping and type-casting:
- the simulator function receives as input and returns a Tensor.
- the simulator can simulate batches of parameters and return batches of data.
- the prior does not produce batches and samples and evaluates to Tensor.
- the output shape is a
torch.Size((1,N))
(i.e, has a leading batch dimension 1).
If this is not possible, a suitable exception will be raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulator |
Callable
|
Simulator as provided by the user. |
required |
prior |
Prior as provided by the user. |
required |
Returns:
Type | Description |
---|---|
Tuple[Callable, Distribution]
|
Tuple (simulator, prior) checked and matching the requirements of sbi. |
Source code in sbi/utils/user_input_checks.py
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 |
|
Returns (\(\theta, x\)) pairs obtained from sampling the proposal and simulating.
This function performs two steps:
- Sample parameters \(\theta\) from the
proposal
. - Simulate these parameters to obtain \(x\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulator |
Callable
|
A function that takes parameters \(\theta\) and maps them to
simulations, or observations, |
required |
proposal |
Any
|
Probability distribution that the parameters \(\theta\) are sampled from. |
required |
num_simulations |
int
|
Number of simulations that are run. |
required |
num_workers |
int
|
Number of parallel workers to use for simulations. |
1
|
simulation_batch_size |
int
|
Number of parameter sets that the simulator maps to data x at once. If None, we simulate all parameter sets at the same time. If >= 1, the simulator has to process data of shape (simulation_batch_size, parameter_dimension). |
1
|
seed |
Optional[int]
|
Seed for reproducibility. |
None
|
show_progress_bar |
bool
|
Whether to show a progress bar for simulating. This will not affect whether there will be a progressbar while drawing samples from the proposal. |
True
|
Returns: Sampled parameters \(\theta\) and simulation-outputs \(x\).
Source code in sbi/inference/base.py
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 |
|
Bases: PosteriorEstimator
Source code in sbi/inference/snpe/snpe_a.py
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 |
|
__init__(prior=None, density_estimator='mdn_snpe_a', num_components=10, device='cpu', logging_level='WARNING', summary_writer=None, show_progress_bars=True)
¶
SNPE-A [1].
[1] Fast epsilon-free Inference of Simulation Models with Bayesian Conditional Density Estimation, Papamakarios et al., NeurIPS 2016, https://arxiv.org/abs/1605.06376.
This class implements SNPE-A. SNPE-A trains across multiple rounds with a maximum-likelihood-loss. This will make training converge to the proposal posterior instead of the true posterior. To correct for this, SNPE-A applies a post-hoc correction after training. This correction has to be performed analytically. Thus, SNPE-A is limited to Gaussian distributions for all but the last round. In the last round, SNPE-A can use a Mixture of Gaussians.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior |
Optional[Distribution]
|
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. Any
object with |
None
|
density_estimator |
Union[str, Callable]
|
If it is a string (only “mdn_snpe_a” is valid), use a
pre-configured mixture of densities network. Alternatively, a function
that builds a custom neural network can be provided. The function will
be called with the first batch of simulations (theta, x), which can
thus be used for shape inference and potentially for z-scoring. It
needs to return a PyTorch |
'mdn_snpe_a'
|
num_components |
int
|
Number of components of the mixture of Gaussians in the
last round. This overrides the |
10
|
device |
str
|
Training device, e.g., “cpu”, “cuda” or “cuda:{0, 1, …}”. |
'cpu'
|
logging_level |
Union[int, str]
|
Minimum severity of messages to log. One of the strings INFO, WARNING, DEBUG, ERROR and CRITICAL. |
'WARNING'
|
summary_writer |
Optional[TensorboardSummaryWriter]
|
A tensorboard |
None
|
show_progress_bars |
bool
|
Whether to show a progressbar during training. |
True
|
Source code in sbi/inference/snpe/snpe_a.py
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 |
|
build_posterior(density_estimator=None, prior=None)
¶
Build posterior from the neural density estimator.
This method first corrects the estimated density with correct_for_proposal
and then returns a DirectPosterior
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
density_estimator |
Optional[TorchModule]
|
The density estimator that the posterior is based on.
If |
None
|
prior |
Optional[Distribution]
|
Prior distribution. |
None
|
Returns:
Type | Description |
---|---|
DirectPosterior
|
Posterior \(p(\theta|x)\) with |
Source code in sbi/inference/snpe/snpe_a.py
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 |
|
correct_for_proposal(density_estimator=None)
¶
Build mixture of Gaussians that approximates the posterior.
Returns a SNPE_A_MDN
object, which applies the posthoc-correction required in
SNPE-A.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
density_estimator |
Optional[TorchModule]
|
The density estimator that the posterior is based on.
If |
None
|
Returns:
Type | Description |
---|---|
SNPE_A_MDN
|
Posterior \(p(\theta|x)\) with |
Source code in sbi/inference/snpe/snpe_a.py
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 |
|
train(final_round=False, training_batch_size=50, learning_rate=0.0005, validation_fraction=0.1, stop_after_epochs=20, max_num_epochs=2 ** 31 - 1, clip_max_norm=5.0, calibration_kernel=None, resume_training=False, retrain_from_scratch=False, show_train_summary=False, dataloader_kwargs=None, component_perturbation=0.005)
¶
Return density estimator that approximates the proposal posterior.
[1] Fast epsilon-free Inference of Simulation Models with Bayesian Conditional Density Estimation, Papamakarios et al., NeurIPS 2016, https://arxiv.org/abs/1605.06376.
Training is performed with maximum likelihood on samples from the latest round, which leads the algorithm to converge to the proposal posterior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
final_round |
bool
|
Whether we are in the last round of training or not. For all but the last round, Algorithm 1 from [1] is executed. In last the round, Algorithm 2 from [1] is executed once. |
False
|
training_batch_size |
int
|
Training batch size. |
50
|
learning_rate |
float
|
Learning rate for Adam optimizer. |
0.0005
|
validation_fraction |
float
|
The fraction of data to use for validation. |
0.1
|
stop_after_epochs |
int
|
The number of epochs to wait for improvement on the validation set before terminating training. |
20
|
max_num_epochs |
int
|
Maximum number of epochs to run. If reached, we stop
training even when the validation loss is still decreasing. Otherwise,
we train until validation loss increases (see also |
2 ** 31 - 1
|
clip_max_norm |
Optional[float]
|
Value at which to clip the total gradient norm in order to prevent exploding gradients. Use None for no clipping. |
5.0
|
calibration_kernel |
Optional[Callable]
|
A function to calibrate the loss with respect to the
simulations |
None
|
resume_training |
bool
|
Can be used in case training time is limited, e.g. on a
cluster. If |
False
|
force_first_round_loss |
If |
required | |
retrain_from_scratch |
bool
|
Whether to retrain the conditional density estimator for the posterior from scratch each round. Not supported for SNPE-A. |
False
|
show_train_summary |
bool
|
Whether to print the number of epochs and validation loss and leakage after the training. |
False
|
dataloader_kwargs |
Optional[Dict]
|
Additional or updated kwargs to be passed to the training and validation dataloaders (like, e.g., a collate_fn) |
None
|
component_perturbation |
float
|
The standard deviation applied to all weights and biases when, in the last round, the Mixture of Gaussians is build from a single Gaussian. This value can be problem-specific and also depends on the number of mixture components. |
0.005
|
Returns:
Type | Description |
---|---|
Module
|
Density estimator that approximates the distribution \(p(\theta|x)\). |
Source code in sbi/inference/snpe/snpe_a.py
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 |
|
Bases: PosteriorEstimator
Source code in sbi/inference/snpe/snpe_c.py
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 |
|
__init__(prior=None, density_estimator='maf', device='cpu', logging_level='WARNING', summary_writer=None, show_progress_bars=True)
¶
SNPE-C / APT [1].
[1] Automatic Posterior Transformation for Likelihood-free Inference, Greenberg et al., ICML 2019, https://arxiv.org/abs/1905.07488.
This class implements two loss variants of SNPE-C: the non-atomic and the atomic version. The atomic loss of SNPE-C can be used for any density estimator, i.e. also for normalizing flows. However, it suffers from leakage issues. On the other hand, the non-atomic loss can only be used only if the proposal distribution is a mixture of Gaussians, the density estimator is a mixture of Gaussians, and the prior is either Gaussian or Uniform. It does not suffer from leakage issues. At the beginning of each round, we print whether the non-atomic or the atomic version is used.
In this codebase, we will automatically switch to the non-atomic loss if the
following criteria are fulfilled:
- proposal is a DirectPosterior
with density_estimator mdn
, as built
with utils.sbi.posterior_nn()
.
- the density estimator is a mdn
, as built with
utils.sbi.posterior_nn()
.
- isinstance(prior, MultivariateNormal)
(from torch.distributions
) or
isinstance(prior, sbi.utils.BoxUniform)
Note that custom implementations of any of these densities (or estimators) will not trigger the non-atomic loss, and the algorithm will fall back onto using the atomic loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior |
Optional[Distribution]
|
A probability distribution that expresses prior knowledge about the parameters, e.g. which ranges are meaningful for them. |
None
|
density_estimator |
Union[str, Callable]
|
If it is a string, use a pre-configured network of the
provided type (one of nsf, maf, mdn, made). Alternatively, a function
that builds a custom neural network can be provided. The function will
be called with the first batch of simulations (theta, x), which can
thus be used for shape inference and potentially for z-scoring. It
needs to return a PyTorch |
'maf'
|
device |
str
|
Training device, e.g., “cpu”, “cuda” or “cuda:{0, 1, …}”. |
'cpu'
|
logging_level |
Union[int, str]
|
Minimum severity of messages to log. One of the strings INFO, WARNING, DEBUG, ERROR and CRITICAL. |
'WARNING'
|
summary_writer |
Optional[TensorboardSummaryWriter]
|
A tensorboard |
None
|
show_progress_bars |
bool
|
Whether to show a progressbar during training. |
True
|
Source code in sbi/inference/snpe/snpe_c.py
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 |
|
train(num_atoms=10, training_batch_size=50, learning_rate=0.0005, validation_fraction=0.1, stop_after_epochs=20, max_num_epochs=2 ** 31 - 1, clip_max_norm=5.0, calibration_kernel=None, resume_training=False, force_first_round_loss=False, discard_prior_samples=False, use_combined_loss=False, retrain_from_scratch=False, show_train_summary=False, dataloader_kwargs=None)
¶
Return density estimator that approximates the distribution \(p(\theta|x)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_atoms |
int
|
Number of atoms to use for classification. |
10
|
training_batch_size |
int
|
Training batch size. |
50
|
learning_rate |
float
|
Learning rate for Adam optimizer. |
0.0005
|
validation_fraction |
float
|
The fraction of data to use for validation. |
0.1
|
stop_after_epochs |
int
|
The number of epochs to wait for improvement on the validation set before terminating training. |
20
|
max_num_epochs |
int
|
Maximum number of epochs to run. If reached, we stop
training even when the validation loss is still decreasing. Otherwise,
we train until validation loss increases (see also |
2 ** 31 - 1
|
clip_max_norm |
Optional[float]
|
Value at which to clip the total gradient norm in order to prevent exploding gradients. Use None for no clipping. |
5.0
|
calibration_kernel |
Optional[Callable]
|
A function to calibrate the loss with respect to the
simulations |
None
|
resume_training |
bool
|
Can be used in case training time is limited, e.g. on a
cluster. If |
False
|
force_first_round_loss |
bool
|
If |
False
|
discard_prior_samples |
bool
|
Whether to discard samples simulated in round 1, i.e. from the prior. Training may be sped up by ignoring such less targeted samples. |
False
|
use_combined_loss |
bool
|
Whether to train the neural net also on prior samples using maximum likelihood in addition to training it on all samples using atomic loss. The extra MLE loss helps prevent density leaking with bounded priors. |
False
|
retrain_from_scratch |
bool
|
Whether to retrain the conditional density estimator for the posterior from scratch each round. |
False
|
show_train_summary |
bool
|
Whether to print the number of epochs and validation loss and leakage after the training. |
False
|
dataloader_kwargs |
Optional[Dict]
|
Additional or updated kwargs to be passed to the training and validation dataloaders (like, e.g., a collate_fn) |
None
|
Returns:
Type | Description |
---|---|
Module
|
Density estimator that approximates the distribution \(p(\theta|x)\). |
Source code in sbi/inference/snpe/snpe_c.py
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 |
|
Bases: LikelihoodEstimator
Source code in sbi/inference/snle/snle_a.py
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 |
|
__init__(prior=None, density_estimator='maf', device='cpu', logging_level='WARNING', summary_writer=None, show_progress_bars=True)
¶
Sequential Neural Likelihood [1].
[1] Sequential Neural Likelihood: Fast Likelihood-free Inference with Autoregressive Flows_, Papamakarios et al., AISTATS 2019, https://arxiv.org/abs/1805.07226
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior |
Optional[Distribution]
|
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. If |
None
|
density_estimator |
Union[str, Callable]
|
If it is a string, use a pre-configured network of the
provided type (one of nsf, maf, mdn, made). Alternatively, a function
that builds a custom neural network can be provided. The function will
be called with the first batch of simulations (theta, x), which can
thus be used for shape inference and potentially for z-scoring. It
needs to return a PyTorch |
'maf'
|
device |
str
|
Training device, e.g., “cpu”, “cuda” or “cuda:{0, 1, …}”. |
'cpu'
|
logging_level |
Union[int, str]
|
Minimum severity of messages to log. One of the strings INFO, WARNING, DEBUG, ERROR and CRITICAL. |
'WARNING'
|
summary_writer |
Optional[TensorboardSummaryWriter]
|
A tensorboard |
None
|
show_progress_bars |
bool
|
Whether to show a progressbar during simulation and sampling. |
True
|
Source code in sbi/inference/snle/snle_a.py
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 |
|
Bases: RatioEstimator
Source code in sbi/inference/snre/snre_a.py
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 |
|
__init__(prior=None, classifier='resnet', device='cpu', logging_level='warning', summary_writer=None, show_progress_bars=True)
¶
AALR[1], here known as SNRE_A.
[1] Likelihood-free MCMC with Amortized Approximate Likelihood Ratios, Hermans et al., ICML 2020, https://arxiv.org/abs/1903.04057
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior |
Optional[Distribution]
|
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. If |
None
|
classifier |
Union[str, Callable]
|
Classifier trained to approximate likelihood ratios. If it is
a string, use a pre-configured network of the provided type (one of
linear, mlp, resnet). Alternatively, a function that builds a custom
neural network can be provided. The function will be called with the
first batch of simulations (theta, x), which can thus be used for shape
inference and potentially for z-scoring. It needs to return a PyTorch
|
'resnet'
|
device |
str
|
Training device, e.g., “cpu”, “cuda” or “cuda:{0, 1, …}”. |
'cpu'
|
logging_level |
Union[int, str]
|
Minimum severity of messages to log. One of the strings INFO, WARNING, DEBUG, ERROR and CRITICAL. |
'warning'
|
summary_writer |
Optional[TensorboardSummaryWriter]
|
A tensorboard |
None
|
show_progress_bars |
bool
|
Whether to show a progressbar during simulation and sampling. |
True
|
Source code in sbi/inference/snre/snre_a.py
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 |
|
train(training_batch_size=50, learning_rate=0.0005, validation_fraction=0.1, stop_after_epochs=20, max_num_epochs=2 ** 31 - 1, clip_max_norm=5.0, resume_training=False, discard_prior_samples=False, retrain_from_scratch=False, show_train_summary=False, dataloader_kwargs=None, loss_kwargs={})
¶
Return classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
training_batch_size |
int
|
Training batch size. |
50
|
learning_rate |
float
|
Learning rate for Adam optimizer. |
0.0005
|
validation_fraction |
float
|
The fraction of data to use for validation. |
0.1
|
stop_after_epochs |
int
|
The number of epochs to wait for improvement on the validation set before terminating training. |
20
|
max_num_epochs |
int
|
Maximum number of epochs to run. If reached, we stop
training even when the validation loss is still decreasing. Otherwise,
we train until validation loss increases (see also |
2 ** 31 - 1
|
clip_max_norm |
Optional[float]
|
Value at which to clip the total gradient norm in order to prevent exploding gradients. Use None for no clipping. |
5.0
|
resume_training |
bool
|
Can be used in case training time is limited, e.g. on a
cluster. If |
False
|
discard_prior_samples |
bool
|
Whether to discard samples simulated in round 1, i.e. from the prior. Training may be sped up by ignoring such less targeted samples. |
False
|
retrain_from_scratch |
bool
|
Whether to retrain the conditional density estimator for the posterior from scratch each round. |
False
|
show_train_summary |
bool
|
Whether to print the number of epochs and validation loss and leakage after the training. |
False
|
dataloader_kwargs |
Optional[Dict]
|
Additional or updated kwargs to be passed to the training and validation dataloaders (like, e.g., a collate_fn) |
None
|
loss_kwargs |
Dict[str, Any]
|
Additional or updated kwargs to be passed to the self._loss fn. |
{}
|
Returns:
Type | Description |
---|---|
Module
|
Classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\). |
Source code in sbi/inference/snre/snre_a.py
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 |
|
Bases: RatioEstimator
Source code in sbi/inference/snre/snre_b.py
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 |
|
__init__(prior=None, classifier='resnet', device='cpu', logging_level='warning', summary_writer=None, show_progress_bars=True)
¶
SRE[1], here known as SNRE_B.
[1] On Contrastive Learning for Likelihood-free Inference, Durkan et al., ICML 2020, https://arxiv.org/pdf/2002.03712
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior |
Optional[Distribution]
|
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. If |
None
|
classifier |
Union[str, Callable]
|
Classifier trained to approximate likelihood ratios. If it is
a string, use a pre-configured network of the provided type (one of
linear, mlp, resnet). Alternatively, a function that builds a custom
neural network can be provided. The function will be called with the
first batch of simulations (theta, x), which can thus be used for shape
inference and potentially for z-scoring. It needs to return a PyTorch
|
'resnet'
|
device |
str
|
Training device, e.g., “cpu”, “cuda” or “cuda:{0, 1, …}”. |
'cpu'
|
logging_level |
Union[int, str]
|
Minimum severity of messages to log. One of the strings INFO, WARNING, DEBUG, ERROR and CRITICAL. |
'warning'
|
summary_writer |
Optional[TensorboardSummaryWriter]
|
A tensorboard |
None
|
show_progress_bars |
bool
|
Whether to show a progressbar during simulation and sampling. |
True
|
Source code in sbi/inference/snre/snre_b.py
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 |
|
train(num_atoms=10, training_batch_size=50, learning_rate=0.0005, validation_fraction=0.1, stop_after_epochs=20, max_num_epochs=2 ** 31 - 1, clip_max_norm=5.0, resume_training=False, discard_prior_samples=False, retrain_from_scratch=False, show_train_summary=False, dataloader_kwargs=None)
¶
Return classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_atoms |
int
|
Number of atoms to use for classification. |
10
|
training_batch_size |
int
|
Training batch size. |
50
|
learning_rate |
float
|
Learning rate for Adam optimizer. |
0.0005
|
validation_fraction |
float
|
The fraction of data to use for validation. |
0.1
|
stop_after_epochs |
int
|
The number of epochs to wait for improvement on the validation set before terminating training. |
20
|
max_num_epochs |
int
|
Maximum number of epochs to run. If reached, we stop
training even when the validation loss is still decreasing. Otherwise,
we train until validation loss increases (see also |
2 ** 31 - 1
|
clip_max_norm |
Optional[float]
|
Value at which to clip the total gradient norm in order to prevent exploding gradients. Use None for no clipping. |
5.0
|
resume_training |
bool
|
Can be used in case training time is limited, e.g. on a
cluster. If |
False
|
discard_prior_samples |
bool
|
Whether to discard samples simulated in round 1, i.e. from the prior. Training may be sped up by ignoring such less targeted samples. |
False
|
retrain_from_scratch |
bool
|
Whether to retrain the conditional density estimator for the posterior from scratch each round. |
False
|
show_train_summary |
bool
|
Whether to print the number of epochs and validation loss and leakage after the training. |
False
|
dataloader_kwargs |
Optional[Dict]
|
Additional or updated kwargs to be passed to the training and validation dataloaders (like, e.g., a collate_fn) |
None
|
Returns:
Type | Description |
---|---|
Module
|
Classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\). |
Source code in sbi/inference/snre/snre_b.py
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 |
|
Bases: RatioEstimator
Source code in sbi/inference/snre/snre_c.py
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 |
|
__init__(prior=None, classifier='resnet', device='cpu', logging_level='warning', summary_writer=None, show_progress_bars=True)
¶
NRE-C[1] is a generalization of the non-sequential (amortized) versions of
SNRE_A and SNRE_B. We call the algorithm SNRE_C within sbi
.
NRE-C:
(1) like SNRE_B, features a “multiclass” loss function where several marginally
drawn parameter-data pairs are contrasted against a jointly drawn pair.
(2) like AALR/NRE_A, i.e., the non-sequential version of SNRE_A, it encourages
the approximate ratio \(p(\theta,x)/p(\theta)p(x)\), accessed through
.potential()
within sbi
, to be exact at optimum. This addresses the
issue that SNRE_B estimates this ratio only up to an arbitrary function
(normalizing constant) of the data \(x\).
Just like for all ratio estimation algorithms, the sequential version of SNRE_C will be estimated only up to a function (normalizing constant) of the data \(x\) in rounds after the first.
[1] Contrastive Neural Ratio Estimation, Benajmin Kurt Miller, et. al., NeurIPS 2022, https://arxiv.org/abs/2210.06170
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior |
Optional[Distribution]
|
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. If |
None
|
classifier |
Union[str, Callable]
|
Classifier trained to approximate likelihood ratios. If it is
a string, use a pre-configured network of the provided type (one of
linear, mlp, resnet). Alternatively, a function that builds a custom
neural network can be provided. The function will be called with the
first batch of simulations (theta, x), which can thus be used for shape
inference and potentially for z-scoring. It needs to return a PyTorch
|
'resnet'
|
device |
str
|
Training device, e.g., “cpu”, “cuda” or “cuda:{0, 1, …}”. |
'cpu'
|
logging_level |
Union[int, str]
|
Minimum severity of messages to log. One of the strings INFO, WARNING, DEBUG, ERROR and CRITICAL. |
'warning'
|
summary_writer |
Optional[TensorboardSummaryWriter]
|
A tensorboard |
None
|
show_progress_bars |
bool
|
Whether to show a progressbar during simulation and sampling. |
True
|
Source code in sbi/inference/snre/snre_c.py
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 |
|
train(num_classes=5, gamma=1.0, training_batch_size=50, learning_rate=0.0005, validation_fraction=0.1, stop_after_epochs=20, max_num_epochs=2 ** 31 - 1, clip_max_norm=5.0, resume_training=False, discard_prior_samples=False, retrain_from_scratch=False, show_train_summary=False, dataloader_kwargs=None)
¶
Return classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_classes |
int
|
Number of theta to classify against, corresponds to \(K\) in
Contrastive Neural Ratio Estimation. Minimum value is 1. Similar to
|
5
|
gamma |
float
|
Determines the relative weight of the sum of all \(K\) dependently drawn classes against the marginally drawn one. Specifically, \(p(y=k) :=p_K\), \(p(y=0) := p_0\), \(p_0 = 1 - K p_K\), and finally \(\gamma := K p_K / p_0\). |
1.0
|
training_batch_size |
int
|
Training batch size. |
50
|
learning_rate |
float
|
Learning rate for Adam optimizer. |
0.0005
|
validation_fraction |
float
|
The fraction of data to use for validation. |
0.1
|
stop_after_epochs |
int
|
The number of epochs to wait for improvement on the validation set before terminating training. |
20
|
max_num_epochs |
int
|
Maximum number of epochs to run. If reached, we stop
training even when the validation loss is still decreasing. Otherwise,
we train until validation loss increases (see also |
2 ** 31 - 1
|
clip_max_norm |
Optional[float]
|
Value at which to clip the total gradient norm in order to prevent exploding gradients. Use None for no clipping. |
5.0
|
exclude_invalid_x |
Whether to exclude simulation outputs |
required | |
resume_training |
bool
|
Can be used in case training time is limited, e.g. on a
cluster. If |
False
|
discard_prior_samples |
bool
|
Whether to discard samples simulated in round 1, i.e. from the prior. Training may be sped up by ignoring such less targeted samples. |
False
|
retrain_from_scratch |
bool
|
Whether to retrain the conditional density estimator for the posterior from scratch each round. |
False
|
show_train_summary |
bool
|
Whether to print the number of epochs and validation loss and leakage after the training. |
False
|
dataloader_kwargs |
Optional[Dict]
|
Additional or updated kwargs to be passed to the training and validation dataloaders (like, e.g., a collate_fn) |
None
|
Returns:
Type | Description |
---|---|
Module
|
Classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\). |
Source code in sbi/inference/snre/snre_c.py
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 |
|
Bases: SNRE_A
Source code in sbi/inference/snre/bnre.py
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 |
|
__init__(prior=None, classifier='resnet', device='cpu', logging_level='warning', summary_writer=None, show_progress_bars=True)
¶
Balanced neural ratio estimation (BNRE)[1]. BNRE is a variation of NRE aiming to produce more conservative posterior approximations
[1] Delaunoy, A., Hermans, J., Rozet, F., Wehenkel, A., & Louppe, G.. Towards Reliable Simulation-Based Inference with Balanced Neural Ratio Estimation. NeurIPS 2022. https://arxiv.org/abs/2208.13624
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior |
Optional[Distribution]
|
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. If |
None
|
classifier |
Union[str, Callable]
|
Classifier trained to approximate likelihood ratios. If it is
a string, use a pre-configured network of the provided type (one of
linear, mlp, resnet). Alternatively, a function that builds a custom
neural network can be provided. The function will be called with the
first batch of simulations \((\theta, x)\), which can thus be used for
shape inference and potentially for z-scoring. It needs to return a
PyTorch |
'resnet'
|
device |
str
|
Training device, e.g., “cpu”, “cuda” or “cuda:{0, 1, …}”. |
'cpu'
|
logging_level |
Union[int, str]
|
Minimum severity of messages to log. One of the strings INFO, WARNING, DEBUG, ERROR and CRITICAL. |
'warning'
|
summary_writer |
Optional[TensorboardSummaryWriter]
|
A tensorboard |
None
|
show_progress_bars |
bool
|
Whether to show a progressbar during simulation and sampling. |
True
|
Source code in sbi/inference/snre/bnre.py
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 |
|
train(regularization_strength=100.0, training_batch_size=50, learning_rate=0.0005, validation_fraction=0.1, stop_after_epochs=20, max_num_epochs=2 ** 31 - 1, clip_max_norm=5.0, resume_training=False, discard_prior_samples=False, retrain_from_scratch=False, show_train_summary=False, dataloader_kwargs=None)
¶
Return classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\). Args:
regularization_strength: The multiplicative coefficient applied to the
balancing regularizer ($\lambda$).
training_batch_size: Training batch size.
learning_rate: Learning rate for Adam optimizer.
validation_fraction: The fraction of data to use for validation.
stop_after_epochs: The number of epochs to wait for improvement on the
validation set before terminating training.
max_num_epochs: Maximum number of epochs to run. If reached, we stop
training even when the validation loss is still decreasing. Otherwise,
we train until validation loss increases (see also `stop_after_epochs`).
clip_max_norm: Value at which to clip the total gradient norm in order to
prevent exploding gradients. Use None for no clipping.
exclude_invalid_x: Whether to exclude simulation outputs `x=NaN` or `x=±∞`
during training. Expect errors, silent or explicit, when `False`.
resume_training: Can be used in case training time is limited, e.g. on a
cluster. If `True`, the split between train and validation set, the
optimizer, the number of epochs, and the best validation log-prob will
be restored from the last time `.train()` was called.
discard_prior_samples: Whether to discard samples simulated in round 1, i.e.
from the prior. Training may be sped up by ignoring such less targeted
samples.
retrain_from_scratch: Whether to retrain the conditional density
estimator for the posterior from scratch each round.
show_train_summary: Whether to print the number of epochs and validation
loss and leakage after the training.
dataloader_kwargs: Additional or updated kwargs to be passed to the training
and validation dataloaders (like, e.g., a collate_fn)
Returns: Classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\).
Source code in sbi/inference/snre/bnre.py
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 |
|
Bases: ABCBASE
Source code in sbi/inference/abc/mcabc.py
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 |
|
__call__(x_o, num_simulations, eps=None, quantile=None, lra=False, sass=False, sass_fraction=0.25, sass_expansion_degree=1, kde=False, kde_kwargs={}, return_summary=False)
¶
Run MCABC and return accepted parameters or KDE object fitted on them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_o |
Union[Tensor, ndarray]
|
Observed data. |
required |
num_simulations |
int
|
Number of simulations to run. |
required |
eps |
Optional[float]
|
Acceptance threshold \(\epsilon\) for distance between observed and simulated data. |
None
|
quantile |
Optional[float]
|
Upper quantile of smallest distances for which the corresponding
parameters are returned, e.g, q=0.01 will return the top 1%. Exactly
one of quantile or |
None
|
lra |
bool
|
Whether to run linear regression adjustment as in Beaumont et al. 2002 |
False
|
sass |
bool
|
Whether to determine semi-automatic summary statistics as in Fearnhead & Prangle 2012. |
False
|
sass_fraction |
float
|
Fraction of simulation budget used for the initial sass run. |
0.25
|
sass_expansion_degree |
int
|
Degree of the polynomial feature expansion for the sass regression, default 1 - no expansion. |
1
|
kde |
bool
|
Whether to run KDE on the accepted parameters to return a KDE object from which one can sample. |
False
|
kde_kwargs |
Dict[str, Any]
|
kwargs for performing KDE: ‘bandwidth=’; either a float, or a string naming a bandwidth heuristics, e.g., ‘cv’ (cross validation), ‘silvermann’ or ‘scott’, default ‘cv’. ‘transform’: transform applied to the parameters before doing KDE. ‘sample_weights’: weights associated with samples. See ‘get_kde’ for more details |
{}
|
return_summary |
bool
|
Whether to return the distances and data corresponding to the accepted parameters. |
False
|
Returns:
Name | Type | Description |
---|---|---|
theta |
if kde False
|
accepted parameters |
kde |
if kde True
|
KDE object based on accepted parameters from which one can .sample() and .log_prob(). |
summary |
if summary True
|
dictionary containing the accepted paramters (if kde True), distances and simulated data x. |
Source code in sbi/inference/abc/mcabc.py
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 |
|
__init__(simulator, prior, distance='l2', num_workers=1, simulation_batch_size=1, show_progress_bars=True)
¶
Monte-Carlo Approximate Bayesian Computation (Rejection ABC) [1].
[1] Pritchard, J. K., Seielstad, M. T., Perez-Lezaun, A., & Feldman, M. W. (1999). Population growth of human Y chromosomes: a study of Y chromosome microsatellites. Molecular biology and evolution, 16(12), 1791-1798.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulator |
Callable
|
A function that takes parameters \(\theta\) and maps them to
simulations, or observations, |
required |
prior |
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. Any
object with |
required | |
distance |
Union[str, Callable]
|
Distance function to compare observed and simulated data. Can be
a custom function or one of |
'l2'
|
num_workers |
int
|
Number of parallel workers to use for simulations. |
1
|
simulation_batch_size |
int
|
Number of parameter sets that the simulator maps to data x at once. If None, we simulate all parameter sets at the same time. If >= 1, the simulator has to process data of shape (simulation_batch_size, parameter_dimension). |
1
|
show_progress_bars |
bool
|
Whether to show a progressbar during simulation and sampling. |
True
|
Source code in sbi/inference/abc/mcabc.py
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 |
|
Bases: ABCBASE
Source code in sbi/inference/abc/smcabc.py
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 712 713 |
|
__call__(x_o, num_particles, num_initial_pop, num_simulations, epsilon_decay, distance_based_decay=False, ess_min=None, kernel_variance_scale=1.0, use_last_pop_samples=True, return_summary=False, kde=False, kde_kwargs={}, kde_sample_weights=False, lra=False, lra_with_weights=False, sass=False, sass_fraction=0.25, sass_expansion_degree=1)
¶
Run SMCABC and return accepted parameters or KDE object fitted on them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_o |
Union[Tensor, ndarray]
|
Observed data. |
required |
num_particles |
int
|
Number of particles in each population. |
required |
num_initial_pop |
int
|
Number of simulations used for initial population. |
required |
num_simulations |
int
|
Total number of possible simulations. |
required |
epsilon_decay |
float
|
Factor with which the acceptance threshold \(\epsilon\) decays. |
required |
distance_based_decay |
bool
|
Whether the \(\epsilon\) decay is constant over populations or calculated from the previous populations distribution of distances. |
False
|
ess_min |
Optional[float]
|
Threshold of effective sampling size for resampling weights. Not used when None (default). |
None
|
kernel_variance_scale |
float
|
Factor for scaling the perturbation kernel variance. |
1.0
|
use_last_pop_samples |
bool
|
Whether to fill up the current population with samples from the previous population when the budget is used up. If False, the current population is discarded and the previous population is returned. |
True
|
lra |
bool
|
Whether to run linear regression adjustment as in Beaumont et al. 2002 |
False
|
lra_with_weights |
bool
|
Whether to run lra as weighted linear regression with SMC weights |
False
|
sass |
bool
|
Whether to determine semi-automatic summary statistics as in Fearnhead & Prangle 2012. |
False
|
sass_fraction |
float
|
Fraction of simulation budget used for the initial sass run. |
0.25
|
sass_expansion_degree |
int
|
Degree of the polynomial feature expansion for the sass regression, default 1 - no expansion. |
1
|
kde |
bool
|
Whether to run KDE on the accepted parameters to return a KDE object from which one can sample. |
False
|
kde_kwargs |
Dict[str, Any]
|
kwargs for performing KDE: ‘bandwidth=’; either a float, or a string naming a bandwidth heuristics, e.g., ‘cv’ (cross validation), ‘silvermann’ or ‘scott’, default ‘cv’. ‘transform’: transform applied to the parameters before doing KDE. ‘sample_weights’: weights associated with samples. See ‘get_kde’ for more details |
{}
|
kde_sample_weights |
bool
|
Whether perform weighted KDE with SMC weights or on raw particles. |
False
|
return_summary |
bool
|
Whether to return a dictionary with all accepted particles, weights, etc. at the end. |
False
|
Returns:
Name | Type | Description |
---|---|---|
theta |
if kde False
|
accepted parameters of the last population. |
kde |
if kde True
|
KDE object fitted on accepted parameters, from which one can .sample() and .log_prob(). |
summary |
if return_summary True
|
dictionary containing the accepted paramters (if kde True), distances and simulated data x of all populations. |
Source code in sbi/inference/abc/smcabc.py
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 |
|
__init__(simulator, prior, distance='l2', num_workers=1, simulation_batch_size=1, show_progress_bars=True, kernel='gaussian', algorithm_variant='C')
¶
Sequential Monte Carlo Approximate Bayesian Computation.
We distinguish between three different SMC methods here
- A: Toni et al. 2010 (Phd Thesis)
- B: Sisson et al. 2007 (with correction from 2009)
- C: Beaumont et al. 2009
In Toni et al. 2010 we find an overview of the differences on page 34: - B: same as A except for resampling of weights if the effective sampling size is too small. - C: same as A except for calculation of the covariance of the perturbation kernel: the kernel covariance is a scaled version of the covariance of the previous population.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulator |
Callable
|
A function that takes parameters \(\theta\) and maps them to
simulations, or observations, |
required |
prior |
Distribution
|
A probability distribution that expresses prior knowledge about the
parameters, e.g. which ranges are meaningful for them. Any
object with |
required |
distance |
Union[str, Callable]
|
Distance function to compare observed and simulated data. Can be
a custom function or one of |
'l2'
|
num_workers |
int
|
Number of parallel workers to use for simulations. |
1
|
simulation_batch_size |
int
|
Number of parameter sets that the simulator maps to data x at once. If None, we simulate all parameter sets at the same time. If >= 1, the simulator has to process data of shape (simulation_batch_size, parameter_dimension). |
1
|
show_progress_bars |
bool
|
Whether to show a progressbar during simulation and sampling. |
True
|
kernel |
Optional[str]
|
Perturbation kernel. |
'gaussian'
|
algorithm_variant |
str
|
Indicating the choice of algorithm variant, A, B, or C. |
'C'
|
Source code in sbi/inference/abc/smcabc.py
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 |
|
get_new_kernel(thetas)
¶
Return new kernel distribution for a given set of paramters.
Source code in sbi/inference/abc/smcabc.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
|
get_particle_ranges(particles, weights, samples_per_dim=100)
¶
Return range of particles in each parameter dimension.
Source code in sbi/inference/abc/smcabc.py
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
|
resample_if_ess_too_small(particles, log_weights, ess_min, pop_idx)
¶
Return resampled particles and uniform weights if effectice sampling size is too small.
Source code in sbi/inference/abc/smcabc.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
|
run_lra_update_weights(particles, xs, observation, log_weights, lra_with_weights)
¶
Return particles and weights adjusted with LRA.
Runs (weighted) linear regression from xs onto particles to adjust the particles.
Updates the SMC weights according to the new particles.
Source code in sbi/inference/abc/smcabc.py
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 |
|
run_sass_set_xo(num_particles, num_pilot_simulations, x_o, lra=False, sass_expansion_degree=1)
¶
Return transform for semi-automatic summary statistics.
Runs an single round of rejection abc with fixed budget and accepts num_particles simulations to run the regression for sass.
Sets self.x_o once the x_shape can be derived from simulations.
Source code in sbi/inference/abc/smcabc.py
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 |
|
sample_from_population_with_weights(particles, weights, num_samples=1)
staticmethod
¶
Return samples from particles sampled with weights.
Source code in sbi/inference/abc/smcabc.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
Posteriors¶
Bases: NeuralPosterior
Posterior \(p(\theta|x_o)\) with log_prob()
and sample()
methods, only
applicable to SNPE.
SNPE trains a neural network to directly approximate the posterior distribution.
However, for bounded priors, the neural network can have leakage: it puts non-zero
mass in regions where the prior is zero. The DirectPosterior
class wraps the
trained network to deal with these cases.
Specifically, this class offers the following functionality:
- correct the calculation of the log probability such that it compensates for the
leakage.
- reject samples that lie outside of the prior bounds.
This class can not be used in combination with SNLE or SNRE.
Source code in sbi/inference/posteriors/direct_posterior.py
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 |
|
__init__(posterior_estimator, prior, max_sampling_batch_size=10000, device=None, x_shape=None, enable_transform=True)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior |
Distribution
|
Prior distribution with |
required |
posterior_estimator |
Flow
|
The trained neural posterior. |
required |
max_sampling_batch_size |
int
|
Batchsize of samples being drawn from the proposal at every iteration. |
10000
|
device |
Optional[str]
|
Training device, e.g., “cpu”, “cuda” or “cuda:0”. If None,
|
None
|
x_shape |
Optional[Size]
|
Shape of a single simulator output. If passed, it is used to check the shape of the observed data and give a descriptive error. |
None
|
enable_transform |
bool
|
Whether to transform parameters to unconstrained space
during MAP optimization. When False, an identity transform will be
returned for |
True
|
Source code in sbi/inference/posteriors/direct_posterior.py
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 |
|
leakage_correction(x, num_rejection_samples=10000, force_update=False, show_progress_bars=False, rejection_sampling_batch_size=10000)
¶
Return leakage correction factor for a leaky posterior density estimate.
The factor is estimated from the acceptance probability during rejection sampling from the posterior.
This is to avoid re-estimating the acceptance probability from scratch
whenever log_prob
is called and norm_posterior=True
. Here, it
is estimated only once for self.default_x
and saved for later. We
re-evaluate only whenever a new x
is passed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_rejection_samples |
int
|
Number of samples used to estimate correction factor. |
10000
|
show_progress_bars |
bool
|
Whether to show a progress bar during sampling. |
False
|
rejection_sampling_batch_size |
int
|
Batch size for rejection sampling. |
10000
|
Returns:
Type | Description |
---|---|
Tensor
|
Saved or newly-estimated correction factor (as a scalar |
Source code in sbi/inference/posteriors/direct_posterior.py
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 |
|
log_prob(theta, x=None, norm_posterior=True, track_gradients=False, leakage_correction_params=None)
¶
Returns the log-probability of the posterior \(p(\theta|x)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
theta |
Tensor
|
Parameters \(\theta\). |
required |
norm_posterior |
bool
|
Whether to enforce a normalized posterior density.
Renormalization of the posterior is useful when some
probability falls out or leaks out of the prescribed prior support.
The normalizing factor is calculated via rejection sampling, so if you
need speedier but unnormalized log posterior estimates set here
|
True
|
track_gradients |
bool
|
Whether the returned tensor supports tracking gradients. This can be helpful for e.g. sensitivity analysis, but increases memory consumption. |
False
|
leakage_correction_params |
Optional[dict]
|
A |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Tensor
|
support of the prior, -∞ (corresponding to 0 probability) outside. |
Source code in sbi/inference/posteriors/direct_posterior.py
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 |
|
map(x=None, num_iter=1000, num_to_optimize=100, learning_rate=0.01, init_method='posterior', num_init_samples=1000, save_best_every=10, show_progress_bars=False, force_update=False)
¶
Returns the maximum-a-posteriori estimate (MAP).
The method can be interrupted (Ctrl-C) when the user sees that the
log-probability converges. The best estimate will be saved in self._map
and
can be accessed with self.map()
. The MAP is obtained by running gradient
ascent from a given number of starting positions (samples from the posterior
with the highest log-probability). After the optimization is done, we select the
parameter set that has the highest log-probability after the optimization.
Warning: The default values used by this function are not well-tested. They might require hand-tuning for the problem at hand.
For developers: if the prior is a BoxUniform
, we carry out the optimization
in unbounded space and transform the result back into bounded space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Optional[Tensor]
|
Deprecated - use |
None
|
num_iter |
int
|
Number of optimization steps that the algorithm takes to find the MAP. |
1000
|
learning_rate |
float
|
Learning rate of the optimizer. |
0.01
|
init_method |
Union[str, Tensor]
|
How to select the starting parameters for the optimization. If
it is a string, it can be either [ |
'posterior'
|
num_init_samples |
int
|
Draw this number of samples from the posterior and evaluate the log-probability of all of them. |
1000
|
num_to_optimize |
int
|
From the drawn |
100
|
save_best_every |
int
|
The best log-probability is computed, saved in the
|
10
|
show_progress_bars |
bool
|
Whether to show a progressbar during sampling from the posterior. |
False
|
force_update |
bool
|
Whether to re-calculate the MAP when x is unchanged and have a cached value. |
False
|
log_prob_kwargs |
Will be empty for SNLE and SNRE. Will contain {‘norm_posterior’: True} for SNPE. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The MAP estimate. |
Source code in sbi/inference/posteriors/direct_posterior.py
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 |
|
sample(sample_shape=torch.Size(), x=None, max_sampling_batch_size=10000, sample_with=None, show_progress_bars=True)
¶
Return samples from posterior distribution \(p(\theta|x)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample_shape |
Shape
|
Desired shape of samples that are drawn from posterior. If
sample_shape is multidimensional we simply draw |
Size()
|
sample_with |
Optional[str]
|
This argument only exists to keep backward-compatibility with
|
None
|
show_progress_bars |
bool
|
Whether to show sampling progress monitor. |
True
|
Source code in sbi/inference/posteriors/direct_posterior.py
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 |
|
Bases: NeuralPosterior
Provides importance sampling to sample from the posterior.
SNLE or SNRE train neural networks to approximate the likelihood(-ratios).
ImportanceSamplingPosterior
allows to estimate the posterior log-probability by
estimating the normlalization constant with importance sampling. It also allows to
perform importance sampling (with .sample()
) and to draw approximate samples with
sampling-importance-resampling (SIR) (with .sir_sample()
)
Source code in sbi/inference/posteriors/importance_posterior.py
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 |
|
__init__(potential_fn, proposal, theta_transform=None, method='sir', oversampling_factor=32, max_sampling_batch_size=10000, device=None, x_shape=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
potential_fn |
Callable
|
The potential function from which to draw samples. |
required |
proposal |
Any
|
The proposal distribution. |
required |
theta_transform |
Optional[TorchTransform]
|
Transformation that is applied to parameters. Is not used
during but only when calling |
None
|
method |
str
|
Either of [ |
'sir'
|
oversampling_factor |
int
|
Number of proposed samples from which only one is selected based on its importance weight. |
32
|
max_sampling_batch_size |
int
|
The batch size of samples being drawn from the proposal at every iteration. |
10000
|
device |
Optional[str]
|
Device on which to sample, e.g., “cpu”, “cuda” or “cuda:0”. If
None, |
None
|
x_shape |
Optional[Size]
|
Shape of a single simulator output. If passed, it is used to check the shape of the observed data and give a descriptive error. |
None
|
Source code in sbi/inference/posteriors/importance_posterior.py
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 |
|
estimate_normalization_constant(x, num_samples=10000, force_update=False)
¶
Returns the normalization constant via importance sampling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_samples |
int
|
Number of importance samples used for the estimate. |
10000
|
force_update |
bool
|
Whether to re-calculate the normlization constant when x is unchanged and have a cached value. |
False
|
Source code in sbi/inference/posteriors/importance_posterior.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 |
|
log_prob(theta, x=None, track_gradients=False, normalization_constant_params=None)
¶
Returns the log-probability of theta under the posterior.
The normalization constant is estimated with importance sampling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
theta |
Tensor
|
Parameters \(\theta\). |
required |
track_gradients |
bool
|
Whether the returned tensor supports tracking gradients. This can be helpful for e.g. sensitivity analysis, but increases memory consumption. |
False
|
normalization_constant_params |
Optional[dict]
|
Parameters passed on to
|
None
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Source code in sbi/inference/posteriors/importance_posterior.py
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 |
|
map(x=None, num_iter=1000, num_to_optimize=100, learning_rate=0.01, init_method='proposal', num_init_samples=1000, save_best_every=10, show_progress_bars=False, force_update=False)
¶
Returns the maximum-a-posteriori estimate (MAP).
The method can be interrupted (Ctrl-C) when the user sees that the
log-probability converges. The best estimate will be saved in self._map
and
can be accessed with self.map()
. The MAP is obtained by running gradient
ascent from a given number of starting positions (samples from the posterior
with the highest log-probability). After the optimization is done, we select the
parameter set that has the highest log-probability after the optimization.
Warning: The default values used by this function are not well-tested. They might require hand-tuning for the problem at hand.
For developers: if the prior is a BoxUniform
, we carry out the optimization
in unbounded space and transform the result back into bounded space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Optional[Tensor]
|
Deprecated - use |
None
|
num_iter |
int
|
Number of optimization steps that the algorithm takes to find the MAP. |
1000
|
learning_rate |
float
|
Learning rate of the optimizer. |
0.01
|
init_method |
Union[str, Tensor]
|
How to select the starting parameters for the optimization. If
it is a string, it can be either [ |
'proposal'
|
num_init_samples |
int
|
Draw this number of samples from the posterior and evaluate the log-probability of all of them. |
1000
|
num_to_optimize |
int
|
From the drawn |
100
|
save_best_every |
int
|
The best log-probability is computed, saved in the
|
10
|
show_progress_bars |
bool
|
Whether to show a progressbar during sampling from the posterior. |
False
|
force_update |
bool
|
Whether to re-calculate the MAP when x is unchanged and have a cached value. |
False
|
log_prob_kwargs |
Will be empty for SNLE and SNRE. Will contain {‘norm_posterior’: True} for SNPE. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The MAP estimate. |
Source code in sbi/inference/posteriors/importance_posterior.py
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 |
|
sample(sample_shape=torch.Size(), x=None, oversampling_factor=32, max_sampling_batch_size=10000, sample_with=None)
¶
Return samples from the approximate posterior distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample_shape |
Shape
|
description |
Size()
|
x |
Optional[Tensor]
|
description |
None
|
Source code in sbi/inference/posteriors/importance_posterior.py
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 |
|
Bases: NeuralPosterior
Provides MCMC to sample from the posterior.
SNLE or SNRE train neural networks to approximate the likelihood(-ratios).
MCMCPosterior
allows to sample from the posterior with MCMC.
Source code in sbi/inference/posteriors/mcmc_posterior.py
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 |
|
mcmc_method: str
property
writable
¶
Returns MCMC method.
posterior_sampler
property
¶
Returns sampler created by sample
.
__init__(potential_fn, proposal, theta_transform=None, method='slice_np', thin=10, warmup_steps=10, num_chains=1, init_strategy='resample', init_strategy_parameters={}, init_strategy_num_candidates=None, num_workers=1, device=None, x_shape=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
potential_fn |
Callable
|
The potential function from which to draw samples. |
required |
proposal |
Any
|
Proposal distribution that is used to initialize the MCMC chain. |
required |
theta_transform |
Optional[TorchTransform]
|
Transformation that will be applied during sampling. Allows to perform MCMC in unconstrained space. |
None
|
method |
str
|
Method used for MCMC sampling, one of |
'slice_np'
|
thin |
int
|
The thinning factor for the chain. |
10
|
warmup_steps |
int
|
The initial number of samples to discard. |
10
|
num_chains |
int
|
The number of chains. |
1
|
init_strategy |
str
|
The initialisation strategy for chains; |
'resample'
|
init_strategy_parameters |
Dict[str, Any]
|
Dictionary of keyword arguments passed to the
init strategy, e.g., for |
{}
|
init_strategy_num_candidates |
Optional[int]
|
Number of candidates to to find init
locations in |
None
|
num_workers |
int
|
number of cpu cores used to parallelize mcmc |
1
|
device |
Optional[str]
|
Training device, e.g., “cpu”, “cuda” or “cuda:0”. If None,
|
None
|
x_shape |
Optional[Size]
|
Shape of a single simulator output. If passed, it is used to check the shape of the observed data and give a descriptive error. |
None
|
Source code in sbi/inference/posteriors/mcmc_posterior.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 |
|
get_arviz_inference_data()
¶
Returns arviz InferenceData object constructed most recent samples.
Note: the InferenceData is constructed using the posterior samples generated in
most recent call to .sample(...)
.
For Pyro HMC and NUTS kernels InferenceData will contain diagnostics, for Pyro Slice or sbi slice sampling samples, only the samples are added.
Returns:
Name | Type | Description |
---|---|---|
inference_data |
InferenceData
|
Arviz InferenceData object. |
Source code in sbi/inference/posteriors/mcmc_posterior.py
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 |
|
log_prob(theta, x=None, track_gradients=False)
¶
Returns the log-probability of theta under the posterior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
theta |
Tensor
|
Parameters \(\theta\). |
required |
track_gradients |
bool
|
Whether the returned tensor supports tracking gradients. This can be helpful for e.g. sensitivity analysis, but increases memory consumption. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Source code in sbi/inference/posteriors/mcmc_posterior.py
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 |
|
map(x=None, num_iter=1000, num_to_optimize=100, learning_rate=0.01, init_method='proposal', num_init_samples=1000, save_best_every=10, show_progress_bars=False, force_update=False)
¶
Returns the maximum-a-posteriori estimate (MAP).
The method can be interrupted (Ctrl-C) when the user sees that the
log-probability converges. The best estimate will be saved in self._map
and
can be accessed with self.map()
. The MAP is obtained by running gradient
ascent from a given number of starting positions (samples from the posterior
with the highest log-probability). After the optimization is done, we select the
parameter set that has the highest log-probability after the optimization.
Warning: The default values used by this function are not well-tested. They might require hand-tuning for the problem at hand.
For developers: if the prior is a BoxUniform
, we carry out the optimization
in unbounded space and transform the result back into bounded space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Optional[Tensor]
|
Deprecated - use |
None
|
num_iter |
int
|
Number of optimization steps that the algorithm takes to find the MAP. |
1000
|
learning_rate |
float
|
Learning rate of the optimizer. |
0.01
|
init_method |
Union[str, Tensor]
|
How to select the starting parameters for the optimization. If
it is a string, it can be either [ |
'proposal'
|
num_init_samples |
int
|
Draw this number of samples from the posterior and evaluate the log-probability of all of them. |
1000
|
num_to_optimize |
int
|
From the drawn |
100
|
save_best_every |
int
|
The best log-probability is computed, saved in the
|
10
|
show_progress_bars |
bool
|
Whether to show a progressbar during sampling from the posterior. |
False
|
force_update |
bool
|
Whether to re-calculate the MAP when x is unchanged and have a cached value. |
False
|
log_prob_kwargs |
Will be empty for SNLE and SNRE. Will contain {‘norm_posterior’: True} for SNPE. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The MAP estimate. |
Source code in sbi/inference/posteriors/mcmc_posterior.py
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 |
|
sample(sample_shape=torch.Size(), x=None, method=None, thin=None, warmup_steps=None, num_chains=None, init_strategy=None, init_strategy_parameters=None, init_strategy_num_candidates=None, mcmc_parameters={}, mcmc_method=None, sample_with=None, num_workers=None, show_progress_bars=True)
¶
Return samples from posterior distribution \(p(\theta|x)\) with MCMC.
Check the __init__()
method for a description of all arguments as well as
their default values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample_shape |
Shape
|
Desired shape of samples that are drawn from posterior. If
sample_shape is multidimensional we simply draw |
Size()
|
mcmc_parameters |
Dict
|
Dictionary that is passed only to support the API of
|
{}
|
mcmc_method |
Optional[str]
|
This argument only exists to keep backward-compatibility with
|
None
|
sample_with |
Optional[str]
|
This argument only exists to keep backward-compatibility with
|
None
|
show_progress_bars |
bool
|
Whether to show sampling progress monitor. |
True
|
Returns:
Type | Description |
---|---|
Tensor
|
Samples from posterior. |
Source code in sbi/inference/posteriors/mcmc_posterior.py
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 |
|
set_mcmc_method(method)
¶
Sets sampling method to for MCMC and returns NeuralPosterior
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method |
str
|
Method to use. |
required |
Returns:
Type | Description |
---|---|
NeuralPosterior
|
|
Source code in sbi/inference/posteriors/mcmc_posterior.py
146 147 148 149 150 151 152 153 154 155 156 |
|
Bases: NeuralPosterior
Provides rejection sampling to sample from the posterior.
SNLE or SNRE train neural networks to approximate the likelihood(-ratios).
RejectionPosterior
allows to sample from the posterior with rejection sampling.
Source code in sbi/inference/posteriors/rejection_posterior.py
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 |
|
__init__(potential_fn, proposal, theta_transform=None, max_sampling_batch_size=10000, num_samples_to_find_max=10000, num_iter_to_find_max=100, m=1.2, device=None, x_shape=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
potential_fn |
Callable
|
The potential function from which to draw samples. |
required |
proposal |
Any
|
The proposal distribution. |
required |
theta_transform |
Optional[TorchTransform]
|
Transformation that is applied to parameters. Is not used
during but only when calling |
None
|
max_sampling_batch_size |
int
|
The batchsize of samples being drawn from the proposal at every iteration. |
10000
|
num_samples_to_find_max |
int
|
The number of samples that are used to find the
maximum of the |
10000
|
num_iter_to_find_max |
int
|
The number of gradient ascent iterations to find the
maximum of the |
100
|
m |
float
|
Multiplier to the |
1.2
|
device |
Optional[str]
|
Training device, e.g., “cpu”, “cuda” or “cuda:0”. If None,
|
None
|
x_shape |
Optional[Size]
|
Shape of a single simulator output. If passed, it is used to check the shape of the observed data and give a descriptive error. |
None
|
Source code in sbi/inference/posteriors/rejection_posterior.py
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 |
|
log_prob(theta, x=None, track_gradients=False)
¶
Returns the log-probability of theta under the posterior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
theta |
Tensor
|
Parameters \(\theta\). |
required |
track_gradients |
bool
|
Whether the returned tensor supports tracking gradients. This can be helpful for e.g. sensitivity analysis, but increases memory consumption. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Source code in sbi/inference/posteriors/rejection_posterior.py
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 |
|
map(x=None, num_iter=1000, num_to_optimize=100, learning_rate=0.01, init_method='proposal', num_init_samples=1000, save_best_every=10, show_progress_bars=False, force_update=False)
¶
Returns the maximum-a-posteriori estimate (MAP).
The method can be interrupted (Ctrl-C) when the user sees that the
log-probability converges. The best estimate will be saved in self._map
and
can be accessed with self.map()
. The MAP is obtained by running gradient
ascent from a given number of starting positions (samples from the posterior
with the highest log-probability). After the optimization is done, we select the
parameter set that has the highest log-probability after the optimization.
Warning: The default values used by this function are not well-tested. They might require hand-tuning for the problem at hand.
For developers: if the prior is a BoxUniform
, we carry out the optimization
in unbounded space and transform the result back into bounded space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Optional[Tensor]
|
Deprecated - use |
None
|
num_iter |
int
|
Number of optimization steps that the algorithm takes to find the MAP. |
1000
|
learning_rate |
float
|
Learning rate of the optimizer. |
0.01
|
init_method |
Union[str, Tensor]
|
How to select the starting parameters for the optimization. If
it is a string, it can be either [ |
'proposal'
|
num_init_samples |
int
|
Draw this number of samples from the posterior and evaluate the log-probability of all of them. |
1000
|
num_to_optimize |
int
|
From the drawn |
100
|
save_best_every |
int
|
The best log-probability is computed, saved in the
|
10
|
show_progress_bars |
bool
|
Whether to show a progressbar during sampling from the posterior. |
False
|
force_update |
bool
|
Whether to re-calculate the MAP when x is unchanged and have a cached value. |
False
|
log_prob_kwargs |
Will be empty for SNLE and SNRE. Will contain {‘norm_posterior’: True} for SNPE. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The MAP estimate. |
Source code in sbi/inference/posteriors/rejection_posterior.py
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 |
|
sample(sample_shape=torch.Size(), x=None, max_sampling_batch_size=None, num_samples_to_find_max=None, num_iter_to_find_max=None, m=None, sample_with=None, show_progress_bars=True)
¶
Return samples from posterior \(p(\theta|x)\) via rejection sampling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample_shape |
Shape
|
Desired shape of samples that are drawn from posterior. If
sample_shape is multidimensional we simply draw |
Size()
|
sample_with |
Optional[str]
|
This argument only exists to keep backward-compatibility with
|
None
|
show_progress_bars |
bool
|
Whether to show sampling progress monitor. |
True
|
Returns:
Type | Description |
---|---|
Samples from posterior. |
Source code in sbi/inference/posteriors/rejection_posterior.py
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 |
|
Bases: NeuralPosterior
Provides VI (Variational Inference) to sample from the posterior.
SNLE or SNRE train neural networks to approximate the likelihood(-ratios).
VIPosterior
allows to learn a tractable variational posterior \(q(\theta)\) which
approximates the true posterior \(p(\theta|x_o)\). After this second training stage,
we can produce approximate posterior samples, by just sampling from q with no
additional cost. For additional information see [1] and [2].
References:
[1] Variational methods for simulation-based inference, Manuel Glöckler, Michael
Deistler, Jakob Macke, 2022, https://openreview.net/forum?id=kZ0UYdhqkNY
[2] Sequential Neural Posterior and Likelihood Approximation, Samuel Wiqvist, Jes
Frellsen, Umberto Picchini, 2021, https://arxiv.org/abs/2102.06522
Source code in sbi/inference/posteriors/vi_posterior.py
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 |
|
q: Distribution
property
writable
¶
Returns the variational posterior.
vi_method: str
property
writable
¶
Variational inference method e.g. one of [rKL, fKL, IW, alpha].
__init__(potential_fn, prior=None, q='maf', theta_transform=None, vi_method='rKL', device='cpu', x_shape=None, parameters=[], modules=[])
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
potential_fn |
Callable
|
The potential function from which to draw samples. |
required |
prior |
Optional[TorchDistribution]
|
This is the prior distribution. Note that this is only
used to check/construct the variational distribution or within some
quality metrics. Please make sure that this matches with the prior
within the potential_fn. If |
None
|
q |
Union[str, PyroTransformedDistribution, VIPosterior, Callable]
|
Variational distribution, either string, |
'maf'
|
theta_transform |
Optional[TorchTransform]
|
Maps form prior support to unconstrained space. The inverse is used here to ensure that the posterior support is equal to that of the prior. |
None
|
vi_method |
str
|
This specifies the variational methods which are used to fit q to
the posterior. We currently support [rKL, fKL, IW, alpha]. Note that
some of the divergences are |
'rKL'
|
device |
str
|
Training device, e.g., |
'cpu'
|
x_shape |
Optional[Size]
|
Shape of a single simulator output. If passed, it is used to check the shape of the observed data and give a descriptive error. |
None
|
parameters |
Iterable
|
List of parameters of the variational posterior. This is only
required for user-defined q i.e. if q does not have a |
[]
|
modules |
Iterable
|
List of modules of the variational posterior. This is only
required for user-defined q i.e. if q does not have a |
[]
|
Source code in sbi/inference/posteriors/vi_posterior.py
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 |
|
evaluate(quality_control_metric='psis', N=int(50000.0))
¶
This function will evaluate the quality of the variational posterior
distribution. We currently support two different metrics of type psis
, which
checks the quality based on the tails of importance weights (there should not be
much with a large one), or prop
which checks the proportionality between q
and potential_fn.
NOTE: In our experience prop
is sensitive to distinguish good
from ok
whereas psis
is more sensitive in distinguishing very bad
from ok
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
quality_control_metric |
str
|
The metric of choice, we currently support [psis, prop, prop_prior]. |
'psis'
|
N |
int
|
Number of samples which is used to evaluate the metric. |
int(50000.0)
|
Source code in sbi/inference/posteriors/vi_posterior.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|
log_prob(theta, x=None, track_gradients=False)
¶
Returns the log-probability of theta under the variational posterior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
theta |
Tensor
|
Parameters |
required |
track_gradients |
bool
|
Whether the returned tensor supports tracking gradients. This can be helpful for e.g. sensitivity analysis but increases memory consumption. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Source code in sbi/inference/posteriors/vi_posterior.py
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 |
|
map(x=None, num_iter=1000, num_to_optimize=100, learning_rate=0.01, init_method='proposal', num_init_samples=10000, save_best_every=10, show_progress_bars=False, force_update=False)
¶
Returns the maximum-a-posteriori estimate (MAP).
The method can be interrupted (Ctrl-C) when the user sees that the
log-probability converges. The best estimate will be saved in self._map
and
can be accessed with self.map()
. The MAP is obtained by running gradient
ascent from a given number of starting positions (samples from the posterior
with the highest log-probability). After the optimization is done, we select the
parameter set that has the highest log-probability after the optimization.
Warning: The default values used by this function are not well-tested. They might require hand-tuning for the problem at hand.
For developers: if the prior is a BoxUniform
, we carry out the optimization
in unbounded space and transform the result back into bounded space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Optional[TorchTensor]
|
Deprecated - use |
None
|
num_iter |
int
|
Number of optimization steps that the algorithm takes to find the MAP. |
1000
|
learning_rate |
float
|
Learning rate of the optimizer. |
0.01
|
init_method |
Union[str, TorchTensor]
|
How to select the starting parameters for the optimization. If
it is a string, it can be either [ |
'proposal'
|
num_init_samples |
int
|
Draw this number of samples from the posterior and evaluate the log-probability of all of them. |
10000
|
num_to_optimize |
int
|
From the drawn |
100
|
save_best_every |
int
|
The best log-probability is computed, saved in the
|
10
|
show_progress_bars |
bool
|
Whether to show a progressbar during sampling from the posterior. |
False
|
force_update |
bool
|
Whether to re-calculate the MAP when x is unchanged and have a cached value. |
False
|
log_prob_kwargs |
Will be empty for SNLE and SNRE. Will contain {‘norm_posterior’: True} for SNPE. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The MAP estimate. |
Source code in sbi/inference/posteriors/vi_posterior.py
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 |
|
sample(sample_shape=torch.Size(), x=None, **kwargs)
¶
Samples from the variational posterior distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample_shape |
Shape
|
Shape of samples |
Size()
|
Returns:
Type | Description |
---|---|
Tensor
|
Samples from posterior. |
Source code in sbi/inference/posteriors/vi_posterior.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
set_q(q, parameters=[], modules=[])
¶
Defines the variational family.
You can specify over which parameters/modules we optimize. This is required for
custom distributions which e.g. do not inherit nn.Modules or has the function
parameters
or modules
to give direct access to trainable parameters.
Further, you can pass a function, which constructs a variational distribution
if called.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q |
Union[str, PyroTransformedDistribution, VIPosterior, Callable]
|
Variational distribution, either string, distribution, or a VIPosterior
object. This specifies a parametric class of distribution over which
the best possible posterior approximation is searched. For string input,
we currently support [nsf, scf, maf, mcf, gaussian, gaussian_diag]. Of
course, you can also specify your own variational family by passing a
|
required |
parameters |
Iterable
|
List of parameters associated with the distribution object. |
[]
|
modules |
Iterable
|
List of modules associated with the distribution object. |
[]
|
Source code in sbi/inference/posteriors/vi_posterior.py
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 |
|
set_vi_method(method)
¶
Sets variational inference method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method |
str
|
One of [rKL, fKL, IW, alpha]. |
required |
Returns:
Type | Description |
---|---|
VIPosterior
|
|
Source code in sbi/inference/posteriors/vi_posterior.py
261 262 263 264 265 266 267 268 269 270 271 272 |
|
train(x=None, n_particles=256, learning_rate=0.001, gamma=0.999, max_num_iters=2000, min_num_iters=10, clip_value=10.0, warm_up_rounds=100, retrain_from_scratch=False, reset_optimizer=False, show_progress_bar=True, check_for_convergence=True, quality_control=True, quality_control_metric='psis', **kwargs)
¶
This method trains the variational posterior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Optional[TorchTensor]
|
The observation. |
None
|
n_particles |
int
|
Number of samples to approximate expectations within the variational bounds. The larger the more accurate are gradient estimates, but the computational cost per iteration increases. |
256
|
learning_rate |
float
|
Learning rate of the optimizer. |
0.001
|
gamma |
float
|
Learning rate decay per iteration. We use an exponential decay scheduler. |
0.999
|
max_num_iters |
int
|
Maximum number of iterations. |
2000
|
min_num_iters |
int
|
Minimum number of iterations. |
10
|
clip_value |
float
|
Gradient clipping value, decreasing may help if you see invalid values. |
10.0
|
warm_up_rounds |
int
|
Initialize the posterior as the prior. |
100
|
retrain_from_scratch |
bool
|
Retrain the variational distributions from scratch. |
False
|
reset_optimizer |
bool
|
Reset the divergence optimizer |
False
|
show_progress_bar |
bool
|
If any progress report should be displayed. |
True
|
quality_control |
bool
|
If False quality control is skipped. |
True
|
quality_control_metric |
str
|
Which metric to use for evaluating the quality. |
'psis'
|
kwargs |
Hyperparameters check corresponding |
{}
|
Returns:
VIPosterior: VIPosterior
(can be used to chain calls).
Source code in sbi/inference/posteriors/vi_posterior.py
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 |
|
Models¶
Returns a function that builds a density estimator for learning the posterior.
This function will usually be used for SNPE. The returned function is to be passed to the inference class when using the flexible interface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
str
|
The type of density estimator that will be created. One of [ |
required |
z_score_theta |
Optional[str]
|
Whether to z-score parameters \(\theta\) before passing them into
the network, can take one of the following:
- |
'independent'
|
z_score_x |
Optional[str]
|
Whether to z-score simulation outputs \(x\) before passing them into the network, same options as z_score_theta. |
'independent'
|
hidden_features |
int
|
Number of hidden features. |
50
|
num_transforms |
int
|
Number of transforms when a flow is used. Only relevant if
density estimator is a normalizing flow (i.e. currently either a |
5
|
num_bins |
int
|
Number of bins used for the splines in |
10
|
embedding_net |
Module
|
Optional embedding network for simulation outputs \(x\). This embedding net allows to learn features from potentially high-dimensional simulation outputs. |
Identity()
|
num_components |
int
|
Number of mixture components for a mixture of Gaussians. Ignored if density estimator is not an mdn. |
10
|
kwargs |
additional custom arguments passed to downstream build functions. |
{}
|
Source code in sbi/utils/get_nn_models.py
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 |
|
Returns a function that builds a density estimator for learning the likelihood.
This function will usually be used for SNLE. The returned function is to be passed to the inference class when using the flexible interface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
str
|
The type of density estimator that will be created. One of [ |
required |
z_score_theta |
Optional[str]
|
Whether to z-score parameters \(\theta\) before passing them into
the network, can take one of the following:
- |
'independent'
|
z_score_x |
Optional[str]
|
Whether to z-score simulation outputs \(x\) before passing them into the network, same options as z_score_theta. |
'independent'
|
hidden_features |
int
|
Number of hidden features. |
50
|
num_transforms |
int
|
Number of transforms when a flow is used. Only relevant if
density estimator is a normalizing flow (i.e. currently either a |
5
|
num_bins |
int
|
Number of bins used for the splines in |
10
|
embedding_net |
Module
|
Optional embedding network for parameters \(\theta\). |
Identity()
|
num_components |
int
|
Number of mixture components for a mixture of Gaussians. Ignored if density estimator is not an mdn. |
10
|
kwargs |
additional custom arguments passed to downstream build functions. |
{}
|
Source code in sbi/utils/get_nn_models.py
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 |
|
Returns a function that builds a classifier for learning density ratios.
This function will usually be used for SNRE. The returned function is to be passed to the inference class when using the flexible interface.
Note that in the view of the SNRE classifier we build below, x=theta and y=x.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
str
|
The type of classifier that will be created. One of [ |
required |
z_score_theta |
Optional[str]
|
Whether to z-score parameters \(\theta\) before passing them into
the network, can take one of the following:
- |
'independent'
|
z_score_x |
Optional[str]
|
Whether to z-score simulation outputs \(x\) before passing them into the network, same options as z_score_theta. |
'independent'
|
hidden_features |
int
|
Number of hidden features. |
50
|
embedding_net_theta |
Module
|
Optional embedding network for parameters \(\theta\). |
Identity()
|
embedding_net_x |
Module
|
Optional embedding network for simulation outputs \(x\). This embedding net allows to learn features from potentially high-dimensional simulation outputs. |
Identity()
|
kwargs |
additional custom arguments passed to downstream build functions. |
{}
|
Source code in sbi/utils/get_nn_models.py
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 |
|
Potentials¶
Returns the potential for posterior-based methods.
It also returns a transformation that can be used to transform the potential into unconstrained space.
The potential is the same as the log-probability of the posterior_estimator
, but
it is set to \(-\inf\) outside of the prior bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
posterior_estimator |
Module
|
The neural network modelling the posterior. |
required |
prior |
Distribution
|
The prior distribution. |
required |
x_o |
Optional[Tensor]
|
The observed data at which to evaluate the posterior. |
required |
enable_transform |
bool
|
Whether to transform parameters to unconstrained space.
When False, an identity transform will be returned for |
True
|
Returns:
Type | Description |
---|---|
Callable
|
The potential function and a transformation that maps |
TorchTransform
|
to unconstrained space. |
Source code in sbi/inference/potentials/posterior_based_potential.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 |
|
Returns potential \(\log(p(x_o|\theta)p(\theta))\) for likelihood-based methods.
It also returns a transformation that can be used to transform the potential into unconstrained space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
likelihood_estimator |
Module
|
The neural network modelling the likelihood. |
required |
prior |
Distribution
|
The prior distribution. |
required |
x_o |
Optional[Tensor]
|
The observed data at which to evaluate the likelihood. |
required |
enable_transform |
bool
|
Whether to transform parameters to unconstrained space.
When False, an identity transform will be returned for |
True
|
Returns:
Type | Description |
---|---|
Callable
|
The potential function \(p(x_o|\theta)p(\theta)\) and a transformation that maps |
TorchTransform
|
to unconstrained space. |
Source code in sbi/inference/potentials/likelihood_based_potential.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 |
|
Returns the potential for ratio-based methods.
It also returns a transformation that can be used to transform the potential into unconstrained space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ratio_estimator |
Module
|
The neural network modelling likelihood-to-evidence ratio. |
required |
prior |
Distribution
|
The prior distribution. |
required |
x_o |
Optional[Tensor]
|
The observed data at which to evaluate the likelihood-to-evidence ratio. |
required |
enable_transform |
bool
|
Whether to transform parameters to unconstrained space.
When False, an identity transform will be returned for |
True
|
Returns:
Type | Description |
---|---|
Callable
|
The potential function and a transformation that maps |
TorchTransform
|
to unconstrained space. |
Source code in sbi/inference/potentials/ratio_based_potential.py
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 |
|
Analysis¶
Plot samples in a 2D grid showing marginals and pairwise marginals.
Each of the diagonal plots can be interpreted as a 1D-marginal of the distribution that the samples were drawn from. Each upper-diagonal plot can be interpreted as a 2D-marginal of the distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
samples |
Union[List[ndarray], List[Tensor], ndarray, Tensor]
|
Samples used to build the histogram. |
required |
points |
Optional[Union[List[ndarray], List[Tensor], ndarray, Tensor]]
|
List of additional points to scatter. |
None
|
limits |
Optional[Union[List, Tensor]]
|
Array containing the plot xlim for each parameter dimension. If None, just use the min and max of the passed samples |
None
|
subset |
Optional[List[int]]
|
List containing the dimensions to plot. E.g. subset=[1,3] will plot plot only the 1st and 3rd dimension but will discard the 0th and 2nd (and, if they exist, the 4th, 5th and so on). |
None
|
offdiag |
Optional[Union[List[str], str]]
|
Plotting style for upper diagonal, {hist, scatter, contour, cond, None}. |
'hist'
|
upper |
Optional[str]
|
deprecated, use offdiag instead. |
None
|
diag |
Optional[Union[List[str], str]]
|
Plotting style for diagonal, {hist, cond, None}. |
'hist'
|
figsize |
Tuple
|
Size of the entire figure. |
(10, 10)
|
labels |
Optional[List[str]]
|
List of strings specifying the names of the parameters. |
None
|
ticks |
Union[List, Tensor]
|
Position of the ticks. |
[]
|
fig |
matplotlib figure to plot on. |
None
|
|
axes |
matplotlib axes corresponding to fig. |
None
|
|
**kwargs |
Additional arguments to adjust the plot, e.g., |
{}
|
Returns: figure and axis of posterior distribution plot
Source code in sbi/analysis/plot.py
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 |
|
Plot samples in a row showing 1D marginals of selected dimensions.
Each of the plots can be interpreted as a 1D-marginal of the distribution that the samples were drawn from.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
samples |
Union[List[ndarray], List[Tensor], ndarray, Tensor]
|
Samples used to build the histogram. |
required |
points |
Optional[Union[List[ndarray], List[Tensor], ndarray, Tensor]]
|
List of additional points to scatter. |
None
|
limits |
Optional[Union[List, Tensor]]
|
Array containing the plot xlim for each parameter dimension. If None, just use the min and max of the passed samples |
None
|
subset |
Optional[List[int]]
|
List containing the dimensions to plot. E.g. subset=[1,3] will plot plot only the 1st and 3rd dimension but will discard the 0th and 2nd (and, if they exist, the 4th, 5th and so on). |
None
|
diag |
Optional[str]
|
Plotting style for 1D marginals, {hist, kde cond, None}. |
'hist'
|
figsize |
Tuple
|
Size of the entire figure. |
(10, 10)
|
labels |
Optional[List[str]]
|
List of strings specifying the names of the parameters. |
None
|
ticks |
Union[List, Tensor]
|
Position of the ticks. |
[]
|
points_colors |
Colors of the |
required | |
fig |
matplotlib figure to plot on. |
None
|
|
axes |
matplotlib axes corresponding to fig. |
None
|
|
**kwargs |
Additional arguments to adjust the plot, e.g., |
{}
|
Returns: figure and axis of posterior distribution plot
Source code in sbi/analysis/plot.py
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 |
|
Plot conditional distribution given all other parameters.
The conditionals can be interpreted as slices through the density
at a location
given by condition
.
For example:
Say we have a 3D density with parameters \(\theta_0\), \(\theta_1\), \(\theta_2\) and
a condition \(c\) passed by the user in the condition
argument.
For the plot of \(\theta_0\) on the diagonal, this will plot the conditional
\(p(\theta_0 | \theta_1=c[1], \theta_2=c[2])\). For the upper
diagonal of \(\theta_1\) and \(\theta_2\), it will plot
\(p(\theta_1, \theta_2 | \theta_0=c[0])\). All other diagonals and upper-diagonals
are built in the corresponding way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
density |
Any
|
Probability density with a |
required |
condition |
Tensor
|
Condition that all but the one/two regarded parameters are fixed to. The condition should be of shape (1, dim_theta), i.e. it could e.g. be a sample from the posterior distribution. |
required |
limits |
Union[List, Tensor]
|
Limits in between which each parameter will be evaluated. |
required |
points |
Optional[Union[List[ndarray], List[Tensor], ndarray, Tensor]]
|
Additional points to scatter. |
None
|
subset |
Optional[List[int]]
|
List containing the dimensions to plot. E.g. subset=[1,3] will plot plot only the 1st and 3rd dimension but will discard the 0th and 2nd (and, if they exist, the 4th, 5th and so on) |
None
|
resolution |
int
|
Resolution of the grid at which we evaluate the |
50
|
figsize |
Tuple
|
Size of the entire figure. |
(10, 10)
|
labels |
Optional[List[str]]
|
List of strings specifying the names of the parameters. |
None
|
ticks |
Union[List, Tensor]
|
Position of the ticks. |
[]
|
points_colors |
Colors of the |
required | |
fig |
matplotlib figure to plot on. |
None
|
|
axes |
matplotlib axes corresponding to fig. |
None
|
|
**kwargs |
Additional arguments to adjust the plot, e.g., |
{}
|
Returns: figure and axis of posterior distribution plot
Source code in sbi/analysis/plot.py
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 |
|
Returns the conditional correlation matrix of a distribution.
To compute the conditional distribution, we condition all but two parameters to
values from condition
, and then compute the Pearson correlation
coefficient \(\rho\) between the remaining two parameters under the distribution
density
. We do so for any pair of parameters specified in subset
, thus
creating a matrix containing conditional correlations between any pair of
parameters.
If condition
is a batch of conditions, this function computes the conditional
correlation matrix for each one of them and returns the mean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
density |
Any
|
Probability density function with |
required |
limits |
Tensor
|
Limits within which to evaluate the |
required |
condition |
Tensor
|
Values to condition the |
required |
subset |
Optional[List[int]]
|
Evaluate the conditional distribution only on a subset of dimensions.
If |
None
|
resolution |
int
|
Number of grid points on which the conditional distribution is evaluated. A higher value increases the accuracy of the estimated correlation but also increases the computational cost. |
50
|
Returns: Average conditional correlation matrix of shape either (num_dim, num_dim)
or (len(subset), len(subset))
if subset
was specified.
Source code in sbi/analysis/conditional_density.py
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 |
|