Skip to content

Add click install to fix ci #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
rev: 0.5.6
hooks:
- id: nbqa-black
additional_dependencies: [black==20.8b1]
additional_dependencies: [black==22.3.0]
files: ^(Rethinking_2|BSM)/
- id: nbqa-isort
additional_dependencies: [isort==5.6.4]
Expand Down
2 changes: 1 addition & 1 deletion BSM/Chapter_03_00_Bayesian_CLT.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
" A = Y - 0.5\n",
" B = n - Y - 0.5\n",
" θ_MAP = A / (A + B)\n",
" info = A / θ_MAP ** 2 + B / (1 - θ_MAP) ** 2\n",
" info = A / θ_MAP**2 + B / (1 - θ_MAP) ** 2\n",
"\n",
" post1 = stats.binom(n, θ).pmf(Y) * stats.beta(0.5, 0.5).pdf(θ)\n",
" post1 = post1 / np.sum(post1)\n",
Expand Down
4 changes: 2 additions & 2 deletions BSM/Chapter_03_01_Gibbs_sampling_one_sample_t-test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
" # sample mu|s2,Y\n",
" MN = np.sum(Y) / (n + m)\n",
" VR = s2 / (n + m)\n",
" mu = stats.norm(MN, VR ** 0.5).rvs(1)\n",
" mu = stats.norm(MN, VR**0.5).rvs(1)\n",
"\n",
" # sample s2|mu,Y\n",
" A = a + n / 2\n",
Expand Down Expand Up @@ -291,7 +291,7 @@
}
],
"source": [
"keep_s = keep_s2 ** 0.5\n",
"keep_s = keep_s2**0.5\n",
"plt.hist(keep_s2)\n",
"plt.xlabel(\"sigma\")\n",
"plt.title(\"Marginal posterior\");"
Expand Down
4 changes: 2 additions & 2 deletions BSM/Chapter_03_02_Gibbs_sampling_two_sample_t-test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@
" # sample muY|muZ,s2,Y,Z\n",
" A = np.sum(Y) / s2 + mu_0 / s2_0\n",
" B = n / s2 + 1 / s2_0\n",
" muY = stats.norm(A / B, 1 / B ** 0.5).rvs(1)[0]\n",
" muY = stats.norm(A / B, 1 / B**0.5).rvs(1)[0]\n",
"\n",
" # sample muZ|muY,s2,Y,Z\n",
" A = np.sum(Z) / s2 + mu_0 / s2_0\n",
" B = m / s2 + 1 / s2_0\n",
" muZ = stats.norm(A / B, 1 / B ** 0.5).rvs(1)[0]\n",
" muZ = stats.norm(A / B, 1 / B**0.5).rvs(1)[0]\n",
"\n",
" # sample s2|muY,muZ,Y,Z\n",
" A = n / 2 + m / 2 + a\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@
" # sample alpha\n",
" V = n / s2 + mu_0 / s2_0\n",
" M = np.sum(Y - X * β) / s2 + 1 / s2_0\n",
" α = stats.norm(M / V, 1 / V ** 0.5).rvs(1)[0]\n",
" α = stats.norm(M / V, 1 / V**0.5).rvs(1)[0]\n",
"\n",
" # sample beta\n",
" V = np.sum(X ** 2) / s2 + mu_0 / s2_0\n",
" V = np.sum(X**2) / s2 + mu_0 / s2_0\n",
" M = np.sum(X * (Y - α)) / s2 + 1 / s2_0\n",
" β = stats.norm(M / V, 1 / V ** 0.5).rvs(1)[0]\n",
" β = stats.norm(M / V, 1 / V**0.5).rvs(1)[0]\n",
"\n",
" # sample s2|mu,Y,Z\n",
" A = n / 2 + a\n",
Expand Down
2 changes: 1 addition & 1 deletion BSM/Chapter_03_09_Simple_linear_regression_in_PyMC3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"with pm.Model() as model:\n",
" # Priors\n",
" τ = pm.Gamma(\"τ\", 0.1, 10)\n",
" σ = pm.Deterministic(\"σ\", 1 / (τ ** 0.5))\n",
" σ = pm.Deterministic(\"σ\", 1 / (τ**0.5))\n",
" # σ = pm.HalfNormal('σ', np.std(mass))\n",
" β1 = pm.Normal(\"β1\", 0, 1000)\n",
" β2 = pm.Normal(\"β2\", 0, 1000)\n",
Expand Down
4 changes: 2 additions & 2 deletions Rethinking_2/Chp_04.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3062,7 +3062,7 @@
],
"source": [
"d[\"weight_std\"] = (d.weight - d.weight.mean()) / d.weight.std()\n",
"d[\"weight_std2\"] = d.weight_std ** 2\n",
"d[\"weight_std2\"] = d.weight_std**2\n",
"\n",
"with pm.Model() as m_4_5:\n",
" a = pm.Normal(\"a\", mu=178, sd=100)\n",
Expand Down Expand Up @@ -3329,7 +3329,7 @@
"metadata": {},
"outputs": [],
"source": [
"weight_m = np.vstack((d.weight_std, d.weight_std ** 2, d.weight_std ** 3))"
"weight_m = np.vstack((d.weight_std, d.weight_std**2, d.weight_std**3))"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion Rethinking_2/Chp_06.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@
"\n",
"\n",
"def sim_coll(r=0.9):\n",
" x = np.random.normal(loc=r * d[\"perc.fat\"], scale=np.sqrt((1 - r ** 2) * np.var(d[\"perc.fat\"])))\n",
" x = np.random.normal(loc=r * d[\"perc.fat\"], scale=np.sqrt((1 - r**2) * np.var(d[\"perc.fat\"])))\n",
" _, cov = curve_fit(mv, (d[\"perc.fat\"], x), d[\"kcal.per.g\"])\n",
" return np.sqrt(np.diag(cov))[-1]\n",
"\n",
Expand Down
10 changes: 5 additions & 5 deletions Rethinking_2/Chp_09.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
],
"source": [
"def rad_dist(Y):\n",
" return np.sqrt(np.sum(Y ** 2))\n",
" return np.sqrt(np.sum(Y**2))\n",
"\n",
"\n",
"fig, ax = plt.subplots(1, 1, figsize=[7, 3])\n",
Expand Down Expand Up @@ -207,8 +207,8 @@
"def calc_U_gradient(x, y, q, a=0, b=1, k=0, d=1):\n",
" muy, mux = q\n",
"\n",
" G1 = np.sum(y - muy) + (a - muy) / b ** 2 # dU/dmuy\n",
" G2 = np.sum(x - mux) + (k - mux) / b ** 2 # dU/dmux\n",
" G1 = np.sum(y - muy) + (a - muy) / b**2 # dU/dmuy\n",
" G2 = np.sum(x - mux) + (k - mux) / b**2 # dU/dmux\n",
"\n",
" return np.array([-G1, -G2])"
]
Expand Down Expand Up @@ -257,9 +257,9 @@
" p *= -1\n",
" # Evaluate potential and kinetic energies sat start and end of trajectory\n",
" current_U = U(x, y, current_q)\n",
" current_K = np.sum(current_p ** 2) / 2\n",
" current_K = np.sum(current_p**2) / 2\n",
" proposed_U = U(x, y, q)\n",
" proposed_K = np.sum(p ** 2) / 2\n",
" proposed_K = np.sum(p**2) / 2\n",
" # Accept or reject the state at end of trajectory, returning either\n",
" # the position at the end of the trajectory or the initial position\n",
" accept = False\n",
Expand Down
8 changes: 4 additions & 4 deletions Rethinking_2/Chp_10.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
],
"source": [
"p = 0.7\n",
"A = [(1 - p) ** 2, p * (1 - p), (1 - p) * p, p ** 2]\n",
"A = [(1 - p) ** 2, p * (1 - p), (1 - p) * p, p**2]\n",
"A"
]
},
Expand Down Expand Up @@ -299,9 +299,9 @@
"metadata": {},
"outputs": [],
"source": [
"H = np.zeros(10 ** 5)\n",
"p = np.zeros((10 ** 5, 4))\n",
"for rep in range(10 ** 5):\n",
"H = np.zeros(10**5)\n",
"p = np.zeros((10**5, 4))\n",
"for rep in range(10**5):\n",
" h, p_ = sim_p()\n",
" H[rep] = h\n",
" p[rep] = p_"
Expand Down
14 changes: 7 additions & 7 deletions Rethinking_2/Chp_14.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
],
"source": [
"cov_ab = sigma_a * sigma_b * rho\n",
"Sigma = np.array([[sigma_a ** 2, cov_ab], [cov_ab, sigma_b ** 2]])\n",
"Sigma = np.array([[sigma_a**2, cov_ab], [cov_ab, sigma_b**2]])\n",
"Sigma"
]
},
Expand Down Expand Up @@ -4321,7 +4321,7 @@
"# linear\n",
"ax.plot(xrange, np.exp(-1 * xrange), \"k--\", label=\"linear\")\n",
"# squared\n",
"ax.plot(xrange, np.exp(-1 * xrange ** 2), \"k\", label=\"squared\")\n",
"ax.plot(xrange, np.exp(-1 * xrange**2), \"k\", label=\"squared\")\n",
"\n",
"ax.set_xlabel(\"distance\")\n",
"ax.set_ylabel(\"correlation\")\n",
Expand Down Expand Up @@ -4606,14 +4606,14 @@
"\n",
" etasq = pm.Exponential(\"etasq\", 2.0)\n",
" ls_inv = pm.HalfNormal(\"ls_inv\", 2.0)\n",
" rhosq = pm.Deterministic(\"rhosq\", 0.5 * ls_inv ** 2)\n",
" rhosq = pm.Deterministic(\"rhosq\", 0.5 * ls_inv**2)\n",
"\n",
" # Implementation with PyMC's GP module:\n",
" cov = etasq * pm.gp.cov.ExpQuad(input_dim=1, ls_inv=ls_inv)\n",
" gp = pm.gp.Latent(cov_func=cov)\n",
" k = gp.prior(\"k\", X=Dmat)\n",
"\n",
" lam = (a * P ** b / g) * tt.exp(k[society])\n",
" lam = (a * P**b / g) * tt.exp(k[society])\n",
"\n",
" T = pm.Poisson(\"total_tools\", lam, observed=total_tools)\n",
"\n",
Expand Down Expand Up @@ -4984,7 +4984,7 @@
"# compute posterior median covariance\n",
"x_seq = np.linspace(0, 10, 100)\n",
"post = idata_14_8.posterior.stack(sample=(\"chain\", \"draw\"))\n",
"pmcov_mu = post[\"etasq\"].median().values * np.exp(-post[\"rhosq\"].median().values * (x_seq ** 2))"
"pmcov_mu = post[\"etasq\"].median().values * np.exp(-post[\"rhosq\"].median().values * (x_seq**2))"
]
},
{
Expand Down Expand Up @@ -5016,7 +5016,7 @@
" x_seq,\n",
" (\n",
" post[\"etasq\"][::50].values[:, None]\n",
" * np.exp(-post[\"rhosq\"][::50].values[:, None] * (x_seq ** 2))\n",
" * np.exp(-post[\"rhosq\"][::50].values[:, None] * (x_seq**2))\n",
" ).T,\n",
" \"k\",\n",
" alpha=0.08,\n",
Expand Down Expand Up @@ -5254,7 +5254,7 @@
"source": [
"# convert to correlation matrix\n",
"sigma_post = np.sqrt(np.diag(K))\n",
"Rho = (sigma_post ** -1) * K * (sigma_post ** -1)\n",
"Rho = (sigma_post**-1) * K * (sigma_post**-1)\n",
"\n",
"# add row/col names for convenience\n",
"Rho = pd.DataFrame(\n",
Expand Down
4 changes: 2 additions & 2 deletions Rethinking_2/Chp_16.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"h_ppc = np.linspace(0, 1.5, 100)\n",
"\n",
"for k, p in zip(prior_checks[\"k\"], prior_checks[\"p\"]):\n",
" w_ppc = np.pi * k * p ** 2 * h_ppc ** 3\n",
" w_ppc = np.pi * k * p**2 * h_ppc**3\n",
" ax.plot(h_ppc, w_ppc, c=\"k\", alpha=0.4)\n",
"\n",
"ax.scatter(d.h, d.w, c=\"C0\", alpha=0.3)\n",
Expand Down Expand Up @@ -325,7 +325,7 @@
"source": [
"w_sim = pm.sample_posterior_predictive(trace_16_1, 200, m16_1)\n",
"h_seq = np.linspace(0, d.h.max(), 30)\n",
"mu_mean = np.pi * (trace_16_1[\"k\"] * trace_16_1[\"p\"] ** 2).mean() * h_seq ** 3"
"mu_mean = np.pi * (trace_16_1[\"k\"] * trace_16_1[\"p\"] ** 2).mean() * h_seq**3"
]
},
{
Expand Down
14 changes: 7 additions & 7 deletions Rethinking_2/End_of_chapter_problems/Chapter_7.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@
" b = pm.Normal(\"b\", 0, 0.5, shape=2) # beta prior\n",
" sigma = pm.Lognormal(\"sigma\", 0, 1)\n",
" x = pm.Data(\"x\", Laffer.s_taxRate)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x ** 2)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x**2)\n",
" rev = pm.Normal(\"rev\", mu=mu, sd=sigma, observed=Laffer.s_taxRevenue)\n",
" second_sample = pm.sample(draws=500, chains=4, return_inferencedata=True)\n",
"\n",
Expand All @@ -549,7 +549,7 @@
" b = pm.Normal(\"b\", 0, 0.5, shape=3) # beta prior\n",
" sigma = pm.Lognormal(\"sigma\", 0, 1)\n",
" x = pm.Data(\"x\", Laffer.s_taxRate)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x ** 2 + b[2] * x ** 3)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x**2 + b[2] * x**3)\n",
" rev = pm.Normal(\"rev\", mu=mu, sd=sigma, observed=Laffer.s_taxRevenue)\n",
" third_sample = pm.sample(draws=500, chains=4, return_inferencedata=True)\n",
"\n",
Expand All @@ -558,7 +558,7 @@
" b = pm.Normal(\"b\", 0, 0.5, shape=4) # beta prior\n",
" sigma = pm.Lognormal(\"sigma\", 0, 1)\n",
" x = pm.Data(\"x\", Laffer.s_taxRate)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x ** 2 + b[2] * x ** 3 + b[3] * x ** 4)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x**2 + b[2] * x**3 + b[3] * x**4)\n",
" rev = pm.Normal(\"rev\", mu=mu, sd=sigma, observed=Laffer.s_taxRevenue)\n",
" fourth_sample = pm.sample(draws=500, chains=4, return_inferencedata=True)"
]
Expand Down Expand Up @@ -966,7 +966,7 @@
" b = pm.Normal(\"b\", 0, 1, shape=2) # beta prior\n",
" sigma = pm.Lognormal(\"sigma\", 0, 1)\n",
" x = pm.Data(\"x\", Laffer.s_taxRate)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x ** 2)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x**2)\n",
" rev = pm.Normal(\"rev\", mu=mu, sd=sigma, observed=Laffer.s_taxRevenue)\n",
" second_sample = pm.sample(draws=500, chains=4, return_inferencedata=True)\n",
"\n",
Expand All @@ -975,7 +975,7 @@
" b = pm.Normal(\"b\", 0, 1, shape=3) # beta prior\n",
" sigma = pm.Lognormal(\"sigma\", 0, 1)\n",
" x = pm.Data(\"x\", Laffer.s_taxRate)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x ** 2 + b[2] * x ** 3)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x**2 + b[2] * x**3)\n",
" rev = pm.Normal(\"rev\", mu=mu, sd=sigma, observed=Laffer.s_taxRevenue)\n",
" third_sample = pm.sample(draws=500, chains=4, return_inferencedata=True)\n",
"\n",
Expand All @@ -984,7 +984,7 @@
" b = pm.Normal(\"b\", 0, 1, shape=4) # beta prior\n",
" sigma = pm.Lognormal(\"sigma\", 0, 1)\n",
" x = pm.Data(\"x\", Laffer.s_taxRate)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x ** 2 + b[2] * x ** 3 + b[3] * x ** 4)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x**2 + b[2] * x**3 + b[3] * x**4)\n",
" rev = pm.Normal(\"rev\", mu=mu, sd=sigma, observed=Laffer.s_taxRevenue)\n",
" fourth_sample = pm.sample(draws=500, chains=4, return_inferencedata=True)"
]
Expand Down Expand Up @@ -1899,7 +1899,7 @@
" b = pm.Normal(\"b\", 0, 0.5, shape=2)\n",
" sigma = pm.Lognormal(\"sigma\", 0, 1)\n",
" x = pm.Data(\"x\", Laffer.s_taxRate)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x ** 2)\n",
" mu = pm.Deterministic(\"mu\", a + b[0] * x + b[1] * x**2)\n",
" rev = pm.StudentT(\"rev\", 2, mu=mu, sd=sigma, observed=Laffer.s_taxRevenue)\n",
" robust_second_sample = pm.sample(draws=500, chains=4, return_inferencedata=True)"
]
Expand Down