API Reference¶
Inference¶
sbi.inference.base.infer(simulator, prior, method, num_simulations, num_workers=1)
¶
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
|
Source code in /home/michael/Documents/sbi/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 |
|
sbi.utils.user_input_checks.prepare_for_sbi(simulator, prior)
¶
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 /home/michael/Documents/sbi/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 |
|
sbi.inference.base.simulate_for_sbi(simulator, proposal, num_simulations, num_workers=1, simulation_batch_size=1, show_progress_bar=True)
¶
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
|
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
|
Source code in /home/michael/Documents/sbi/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 |
|
sbi.inference.snpe.snpe_a.SNPE_A
¶
Bases: PosteriorEstimator
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
---|---|
nn.Module
|
Density estimator that approximates the distribution \(p(\theta|x)\). |
Source code in /home/michael/Documents/sbi/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 |
|
sbi.inference.snpe.snpe_c.SNPE_C
¶
Bases: PosteriorEstimator
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
---|---|
nn.Module
|
Density estimator that approximates the distribution \(p(\theta|x)\). |
Source code in /home/michael/Documents/sbi/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 |
|
sbi.inference.snle.snle_a.SNLE_A
¶
Bases: LikelihoodEstimator
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
|
sbi.inference.snre.snre_a.SNRE_A
¶
Bases: RatioEstimator
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
---|---|
nn.Module
|
Classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\). |
Source code in /home/michael/Documents/sbi/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 |
|
sbi.inference.snre.snre_b.SNRE_B
¶
Bases: RatioEstimator
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
---|---|
nn.Module
|
Classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\). |
Source code in /home/michael/Documents/sbi/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 |
|
sbi.inference.snre.snre_c.SNRE_C
¶
Bases: RatioEstimator
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
---|---|
nn.Module
|
Classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\). |
Source code in /home/michael/Documents/sbi/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 |
|
sbi.inference.snre.bnre.BNRE
¶
Bases: SNRE_A
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
regularization_strength |
float
|
The multiplicative coefficient applied to the balancing regularizer (\(\lambda\)). |
100.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 |
---|---|
nn.Module
|
Classifier that approximates the ratio \(p(\theta,x)/p(\theta)p(x)\). |
Source code in /home/michael/Documents/sbi/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 |
|
sbi.inference.abc.mcabc.MCABC
¶
Bases: ABCBASE
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
|
sbi.inference.abc.smcabc.SMCABC
¶
Bases: ABCBASE
Source code in /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/sbi/inference/abc/smcabc.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
Posteriors¶
sbi.inference.posteriors.direct_posterior.DirectPosterior
¶
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 /home/michael/Documents/sbi/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 |
flows.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[torch.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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
torch.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 /home/michael/Documents/sbi/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 |
|
sbi.inference.posteriors.importance_posterior.ImportanceSamplingPosterior
¶
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 /home/michael/Documents/sbi/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[torch.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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 /home/michael/Documents/sbi/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 |
torch.Size()
|
x |
Optional[Tensor]
|
description |
None
|
Source code in /home/michael/Documents/sbi/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 |
|
sbi.inference.posteriors.mcmc_posterior.MCMCPosterior
¶
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 /home/michael/Documents/sbi/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 701 702 |
|