Skip to content

Commit

Permalink
Start of Bayesian notebooks -- imported various sample codez
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanistheone committed Aug 21, 2024
1 parent 2c21793 commit efe595e
Show file tree
Hide file tree
Showing 9 changed files with 5,078 additions and 7 deletions.
401 changes: 401 additions & 0 deletions datasets/movies.csv

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions notebooks/46_generalized_linear_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 122,
"id": "f3357c58-0d49-4019-84b7-34f16a452685",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -292,13 +292,13 @@
"\n",
"# plot the expit function, a.k.a. the logistic function\n",
"xs = np.linspace(-6, 6, 500)\n",
"sns.lineplot(x=xs, y=expit(xs), ax=ax1, label=\"$\\\\text{expit}(x)$\")\n",
"sns.lineplot(x=xs, y=expit(xs), ax=ax1, label=\"$\\\\mathrm{expit}(x)$\")\n",
"ax1.set_xlabel(\"$x$\")\n",
"ax1.set_ylabel(\"$p$\")\n",
"\n",
"# plot the logit function\n",
"ps = np.linspace(0, 1, 600)\n",
"sns.lineplot(x=ps, y=logit(ps), ax=ax2, label=\"$\\\\text{logit}(p)$\", color=\"C1\")\n",
"sns.lineplot(x=ps, y=logit(ps), ax=ax2, label=\"$\\\\mathrm{logit}(p)$\", color=\"C1\")\n",
"ax2.set_xlabel(\"$p$\")\n",
"ax2.set_ylabel(\"$x$\")\n",
"\n",
Expand Down Expand Up @@ -369,7 +369,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 123,
"id": "104042bd-f21c-4e6b-8b81-bd92932ddb0c",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -417,7 +417,7 @@
" # Plot the logistic regression model\n",
" xs = np.linspace(xlims[0], xlims[1], 200)\n",
" ps = expit_model(xs)\n",
" sns.lineplot(x=xs, y=ps, ax=ax, label=r\"$p(x) = \\text{expit}(\\beta_0 + \\beta_1x)$\", linewidth=2)\n",
" sns.lineplot(x=xs, y=ps, ax=ax, label=r\"$p(x) = \\mathrm{expit}(\\beta_0 + \\beta_1x)$\", linewidth=2)\n",
"\n",
" # Plot Bernoulli distributions at specified x positions\n",
" x_positions = [2,4,5,6,8,10]\n",
Expand Down Expand Up @@ -4133,7 +4133,7 @@
"So the model equation is \n",
"\n",
"$$\n",
" \\log(p/(1-p)) = \\text{logit}(p) = -9.793942 + .1563404 \\cdot \\tt{math}\n",
" \\log(p/(1-p)) = \\texttt{logit}(p) = -9.793942 + .1563404 \\cdot \\texttt{math}\n",
"$$"
]
},
Expand Down
247 changes: 247 additions & 0 deletions notebooks/50_extra_bayesian_stuff.ipynb
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&lt;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&lt;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
}
531 changes: 531 additions & 0 deletions notebooks/51_intro_to_Bayesian_stats.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit efe595e

Please sign in to comment.