-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start of Bayesian notebooks -- imported various sample codez
- Loading branch information
1 parent
2c21793
commit efe595e
Showing
9 changed files
with
5,078 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "95a6eda2-a640-4817-9eaf-88bad67ca3d9", | ||
"metadata": {}, | ||
"source": [ | ||
"# Chapter 5: Extra code, drafts, and cut material" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b818a09a-90ac-408e-b39c-f30dd038c6fc", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1ad57ed2-8583-4d1f-a16c-ddd481ee3e5f", | ||
"metadata": {}, | ||
"source": [ | ||
"## Bayesian p-value" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "48fc8af8-bfee-4239-8ae7-8419c072ed21", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n", | ||
"Auto-assigning NUTS sampler...\n", | ||
"Initializing NUTS using jitter+adapt_diag...\n", | ||
"Multiprocess sampling (4 chains in 4 jobs)\n", | ||
"NUTS: [beta0, beta1, sigma]\n", | ||
"WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n", | ||
"WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n", | ||
"WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n", | ||
"WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"\n", | ||
"<style>\n", | ||
" /* Turns off some styling */\n", | ||
" progress {\n", | ||
" /* gets rid of default border in Firefox and Opera. */\n", | ||
" border: none;\n", | ||
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n", | ||
" background-size: auto;\n", | ||
" }\n", | ||
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n", | ||
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n", | ||
" }\n", | ||
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n", | ||
" background: #F44336;\n", | ||
" }\n", | ||
"</style>\n" | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"\n", | ||
" <div>\n", | ||
" <progress value='12000' class='' max='12000' style='width:300px; height:20px; vertical-align: middle;'></progress>\n", | ||
" 100.00% [12000/12000 00:06<00:00 Sampling 4 chains, 0 divergences]\n", | ||
" </div>\n", | ||
" " | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Sampling 4 chains for 1_000 tune and 2_000 draw iterations (4_000 + 8_000 draws total) took 17 seconds.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import pymc as pm\n", | ||
"import numpy as np\n", | ||
"import arviz as az\n", | ||
"\n", | ||
"# Simulated data\n", | ||
"np.random.seed(42)\n", | ||
"x = np.random.normal(0, 1, 100)\n", | ||
"y = 3 + 2 * x + np.random.normal(0, 1, 100)\n", | ||
"\n", | ||
"# Bayesian Linear Regression Model\n", | ||
"with pm.Model() as model:\n", | ||
" # Priors\n", | ||
" beta0 = pm.Normal(\"beta0\", mu=0, sigma=10)\n", | ||
" beta1 = pm.Normal(\"beta1\", mu=0, sigma=10)\n", | ||
" sigma = pm.HalfNormal(\"sigma\", sigma=1)\n", | ||
" \n", | ||
" # Likelihood\n", | ||
" mu = beta0 + beta1 * x\n", | ||
" y_obs = pm.Normal(\"y_obs\", mu=mu, sigma=sigma, observed=y)\n", | ||
" \n", | ||
" # Sampling\n", | ||
" trace = pm.sample(2000, return_inferencedata=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "623fce90-4525-40df-b2ff-498d801a2a3b", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Sampling: [y_obs]\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"\n", | ||
"<style>\n", | ||
" /* Turns off some styling */\n", | ||
" progress {\n", | ||
" /* gets rid of default border in Firefox and Opera. */\n", | ||
" border: none;\n", | ||
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n", | ||
" background-size: auto;\n", | ||
" }\n", | ||
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n", | ||
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n", | ||
" }\n", | ||
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n", | ||
" background: #F44336;\n", | ||
" }\n", | ||
"</style>\n" | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"\n", | ||
" <div>\n", | ||
" <progress value='8000' class='' max='8000' style='width:300px; height:20px; vertical-align: middle;'></progress>\n", | ||
" 100.00% [8000/8000 00:00<00:00]\n", | ||
" </div>\n", | ||
" " | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"# Posterior Predictive Check\n", | ||
"# with model:\n", | ||
"# ppc = pm.sample_posterior_predictive(trace) \n", | ||
"# Posterior Predictive Check\n", | ||
"ppc = pm.sample_posterior_predictive(trace, model=model)\n", | ||
" \n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "b526666e-ace3-4b0c-af87-bd0fbcb95aad", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Bayesian p-value: 0.4925\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Extract the posterior predictive samples for 'y_obs'\n", | ||
"y_rep = ppc.posterior_predictive[\"y_obs\"].values\n", | ||
"\n", | ||
"# Calculate Bayesian p-value for the slope\n", | ||
"test_stat_observed = np.mean(y) # Example test statistic\n", | ||
"test_stat_rep = np.mean(y_rep, axis=1)\n", | ||
"bayesian_p_value = np.mean(test_stat_rep >= test_stat_observed)\n", | ||
"print(\"Bayesian p-value:\", bayesian_p_value)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d446b936-8610-4914-975e-ada7d078db7e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.