Skip to content

Commit 457a0aa

Browse files
Merge remote-tracking branch 'origin/main' into merge_flow_builders_to_current_main
2 parents cc97264 + ebb8c6f commit 457a0aa

33 files changed

+623
-261
lines changed

docs/advanced_tutorials/12_iid_data_and_permutation_invariant_embeddings.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"\n",
6363
"from sbi.analysis import pairplot\n",
6464
"from sbi.inference import NLE, NPE, simulate_for_sbi\n",
65+
"from sbi.inference.posteriors.posterior_parameters import MCMCPosteriorParameters\n",
6566
"from sbi.simulators.linear_gaussian import (\n",
6667
" linear_gaussian,\n",
6768
" true_posterior_linear_gaussian_mvn_prior,\n",
@@ -220,17 +221,16 @@
220221
"nle_samples = []\n",
221222
"num_samples = 1000\n",
222223
"\n",
223-
"mcmc_parameters = dict(\n",
224+
"mcmc_parameters = MCMCPosteriorParameters(\n",
225+
" method=\"slice_np_vectorized\",\n",
224226
" num_chains=50,\n",
225227
" thin=5,\n",
226228
" warmup_steps=30,\n",
227229
" init_strategy=\"proposal\",\n",
228230
")\n",
229-
"mcmc_method = \"slice_np_vectorized\"\n",
230231
"\n",
231232
"posterior = inferer.build_posterior(\n",
232-
" mcmc_method=mcmc_method,\n",
233-
" mcmc_parameters=mcmc_parameters,\n",
233+
" posterior_parameters=mcmc_parameters,\n",
234234
")\n",
235235
"\n",
236236
"# Generate samples with MCMC given the same set of x_os as above.\n",

docs/advanced_tutorials/13_diagnostics_lc2st.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240
"# sample calibration data\n",
241241
"theta_cal = prior.sample((NUM_CAL,))\n",
242242
"x_cal = simulator(theta_cal)\n",
243-
"post_samples_cal = posterior.sample_batched((1,), x=x_cal)[0]"
243+
"post_samples_cal = posterior.sample_batched((1,), x=x_cal, max_sampling_batch_size=10)[0]"
244244
]
245245
},
246246
{
@@ -881,7 +881,7 @@
881881
"name": "python",
882882
"nbconvert_exporter": "python",
883883
"pygments_lexer": "ipython3",
884-
"version": "3.12.4"
884+
"version": "3.12.9"
885885
},
886886
"toc": {
887887
"base_numbering": 1,

docs/how_to_guide/10_refine_posterior_with_importance_sampling.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "541b890a",
5+
"id": "ebc792ba",
66
"metadata": {},
77
"source": [
88
"# How to refine posterior estimates with importance sampling"
99
]
1010
},
1111
{
1212
"cell_type": "markdown",
13-
"id": "966b0608",
13+
"id": "f91df50c",
1414
"metadata": {},
1515
"source": [
1616
"The `sbi` toolbox does not require that the simulator can **evaluate** the likelihood (it only requires samples). If the likelihood can be evaluated, then one can refine the posterior estimate _after training_ with likelihood evaluations. This is typically done with importance sampling (using the posterior estimate as proposal).\n",
@@ -20,15 +20,15 @@
2020
},
2121
{
2222
"cell_type": "markdown",
23-
"id": "0b3b8ade",
23+
"id": "ea3b6412",
2424
"metadata": {},
2525
"source": [
2626
"## Main syntax"
2727
]
2828
},
2929
{
3030
"cell_type": "markdown",
31-
"id": "779b28e5",
31+
"id": "2abe3610",
3232
"metadata": {},
3333
"source": [
3434
"```python\n",
@@ -54,15 +54,15 @@
5454
},
5555
{
5656
"cell_type": "markdown",
57-
"id": "3907af65",
57+
"id": "9c480c4f",
5858
"metadata": {},
5959
"source": [
6060
"## Example"
6161
]
6262
},
6363
{
6464
"cell_type": "markdown",
65-
"id": "4e5c35cc",
65+
"id": "a10d9afc",
6666
"metadata": {},
6767
"source": [
6868
"More details can be found in the [tutorial on importance sampling for refining the posterior estimate](https://sbi.readthedocs.io/en/latest/advanced_tutorials/15_importance_sampled_posteriors.html)."
@@ -85,7 +85,7 @@
8585
"name": "python",
8686
"nbconvert_exporter": "python",
8787
"pygments_lexer": "ipython3",
88-
"version": "3.12.4"
88+
"version": "3.12.9"
8989
}
9090
},
9191
"nbformat": 4,

docs/how_to_guide/11_iid_sampling_with_nle_or_nre.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "fa7814eb",
5+
"id": "bcc62481",
66
"metadata": {},
77
"source": [
88
"# How to sample the posterior given iid observations (for NLE and NRE)"
99
]
1010
},
1111
{
1212
"cell_type": "markdown",
13-
"id": "dd74fc35",
13+
"id": "8431dd64",
1414
"metadata": {},
1515
"source": [
1616
"> Important: This how-to guide is only relevant if you have iid observations **and** you are using NLE or NRE. If you have iid observations and want to use NPE, you should construct a permutation-invariant embedding net, which is explained in [this how-to guide](https://sbi.readthedocs.io/en/latest/how_to_guide/08_permutation_invariant_embeddings.html).\n",
@@ -20,7 +20,7 @@
2020
},
2121
{
2222
"cell_type": "markdown",
23-
"id": "adfe175c",
23+
"id": "8366c969",
2424
"metadata": {},
2525
"source": [
2626
"```python\n",
@@ -40,15 +40,15 @@
4040
},
4141
{
4242
"cell_type": "markdown",
43-
"id": "e0b37ac2",
43+
"id": "8252b623",
4444
"metadata": {},
4545
"source": [
4646
"## Example and further explanation"
4747
]
4848
},
4949
{
5050
"cell_type": "markdown",
51-
"id": "980ece2c",
51+
"id": "4d8e197d",
5252
"metadata": {},
5353
"source": [
5454
"For more details, see [this tutorial](https://sbi.readthedocs.io/en/latest/advanced_tutorials/12_iid_data_and_permutation_invariant_embeddings.html)."
@@ -71,7 +71,7 @@
7171
"name": "python",
7272
"nbconvert_exporter": "python",
7373
"pygments_lexer": "ipython3",
74-
"version": "3.12.4"
74+
"version": "3.12.9"
7575
}
7676
},
7777
"nbformat": 4,

docs/how_to_guide/13_diagnostics_lc2st.ipynb

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@
2424
"source": [
2525
"```python\n",
2626
"from sbi.diagnostics.lc2st import LC2ST\n",
27-
"from sbi.analysis.plot import pp_plot_lc2st\n",
2827
"\n",
29-
"# Sample calibration data.\n",
30-
"num_lc2st_samples = 1_000\n",
28+
"# Simulate calibration data. Should be at least in the thousands.\n",
29+
"num_lc2st_samples = 10_000\n",
3130
"prior_samples = prior.sample((num_lc2st_samples,))\n",
3231
"prior_predictives = simulator(prior_samples)\n",
3332
"\n",
3433
"# Generate one posterior sample for every prior predictive.\n",
35-
"post_samples_cal = []\n",
36-
"for x in prior_predictives:\n",
37-
" post_samples_cal.append(posterior.sample((1,), x=x)[0])\n",
38-
"post_samples_cal = torch.stack(post_samples_cal)\n",
34+
"post_samples_cal = posterior.sample_batched(\n",
35+
" (1,),\n",
36+
" x=prior_predictives,\n",
37+
" max_sampling_batch_size=10\n",
38+
")[0]\n",
3939
"\n",
4040
"# Train the L-C2ST classifier.\n",
4141
"lc2st = LC2ST(\n",
@@ -50,28 +50,75 @@
5050
"\n",
5151
"# Note: x_o must have a batch-dimension. I.e. `x_o.shape == (1, observation_shape)`.\n",
5252
"post_samples_star = posterior.sample((10_000,), x=x_o)\n",
53-
"probs_data, _ = lc2st.get_scores(\n",
53+
"probs_data, scores_data = lc2st.get_scores(\n",
5454
" theta_o=post_samples_star,\n",
5555
" x_o=x_o,\n",
5656
" return_probs=True,\n",
5757
" trained_clfs=lc2st.trained_clfs\n",
5858
")\n",
59-
"probs_null, _ = lc2st.get_statistics_under_null_hypothesis(\n",
59+
"probs_null, scores_null = lc2st.get_statistics_under_null_hypothesis(\n",
6060
" theta_o=post_samples_star,\n",
6161
" x_o=x_o,\n",
62-
" return_probs=True\n",
62+
" return_probs=True,\n",
6363
")\n",
6464
"\n",
65+
"conf_alpha = 0.05\n",
66+
"p_value = lc2st.p_value(post_samples_star, torch.as_tensor(x_o).unsqueeze(0))\n",
67+
"reject = lc2st.reject_test(post_samples_star, torch.as_tensor(x_o).unsqueeze(0), alpha=conf_alpha)\n",
68+
"\n",
69+
"fig, ax = plt.subplots(1, 1, figsize=(5, 3))\n",
70+
"quantiles = np.quantile(scores_null, [0, 1-conf_alpha])\n",
71+
"ax.hist(scores_null, bins=50, density=True, alpha=0.5, label=\"Null\")\n",
72+
"ax.axvline(scores_data, color=\"red\", label=\"Observed\")\n",
73+
"ax.axvline(quantiles[0], color=\"black\", linestyle=\"--\", label=\"95% CI\")\n",
74+
"ax.axvline(quantiles[1], color=\"black\", linestyle=\"--\")\n",
75+
"ax.set_xlabel(\"Test statistic\")\n",
76+
"ax.set_ylabel(\"Density\")\n",
77+
"ax.set_title(f\"p-value = {p_value:.3f}, reject = {reject}\")\n",
78+
"plt.show()\n",
79+
"```\n",
80+
"\n",
81+
"This will return a figure as the following:"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"<img src=\"data/lc2st_hist_of_statistics.png\" width=\"500\">"
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"If the red line is outside of the two dotted black lines (as above), then L-C2ST rejects the null-hypothesis that the approximate posterior matches the true posterior (i.e., your posterior is likely wrong).\n",
96+
"\n",
97+
"If the posterior is wrong, then you can get insights into whether the posterior is under- or over-confident as follows:"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"```python\n",
105+
"from sbi.analysis.plot import pp_plot_lc2st\n",
106+
"\n",
65107
"pp_plot_lc2st(\n",
66108
" probs=[probs_data],\n",
67109
" probs_null=probs_null,\n",
68110
" conf_alpha=0.05,\n",
69111
" labels=[\"Classifier probabilities \\n on observed data\"],\n",
70112
" colors=[\"red\"],\n",
71113
")\n",
72-
"```\n",
73-
"\n",
74-
"This will return a figure as the following:"
114+
"```"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"This plots something like the following:"
75122
]
76123
},
77124
{
@@ -85,7 +132,7 @@
85132
"cell_type": "markdown",
86133
"metadata": {},
87134
"source": [
88-
"If the red line lies within the gray region, the we cannot reject the null-hypothesis that the approximate posterior matches the true posterior. If the red line is below the gray area, then the `posterior` is over-confident. If the red line is above the gray area, then the `posterior` is under-confident."
135+
"If the red line is below the gray area, then the `posterior` is over-confident. If the red line is above the gray area, then the `posterior` is under-confident."
89136
]
90137
},
91138
{
@@ -108,7 +155,7 @@
108155
"source": [
109156
"## Citation\n",
110157
"\n",
111-
"```\n",
158+
"```text\n",
112159
"@article{linhart2023c2st,\n",
113160
" title={L-c2st: Local diagnostics for posterior approximations in simulation-based inference},\n",
114161
" author={Linhart, Julia and Gramfort, Alexandre and Rodrigues, Pedro},\n",
@@ -137,7 +184,7 @@
137184
"name": "python",
138185
"nbconvert_exporter": "python",
139186
"pygments_lexer": "ipython3",
140-
"version": "3.12.4"
187+
"version": "3.12.9"
141188
},
142189
"toc": {
143190
"base_numbering": 1,

docs/how_to_guide/14_choose_diagnostic_tool.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "82dea70e",
5+
"id": "4d43f851",
66
"metadata": {},
77
"source": [
88
"# How to choose a diagnostic tool"
99
]
1010
},
1111
{
1212
"cell_type": "markdown",
13-
"id": "1c744d13",
13+
"id": "6abcd346",
1414
"metadata": {},
1515
"source": [
1616
"`sbi` implements a range of diagnostic tools. Here, we provide an overview of what they do:\n",
@@ -24,7 +24,7 @@
2424
},
2525
{
2626
"cell_type": "markdown",
27-
"id": "3660182d",
27+
"id": "df220eed",
2828
"metadata": {},
2929
"source": [
3030
"All of these checks can provide a different angle on the correctness of the posterior, but they all require additional simulations. As such, we recommend running as many diagnostic tools as possible with your simulation budget."
@@ -47,7 +47,7 @@
4747
"name": "python",
4848
"nbconvert_exporter": "python",
4949
"pygments_lexer": "ipython3",
50-
"version": "3.12.4"
50+
"version": "3.12.9"
5151
}
5252
},
5353
"nbformat": 4,

0 commit comments

Comments
 (0)