From 2996d387d7a0fe10d60fac3431244e524a124c88 Mon Sep 17 00:00:00 2001 From: Ravin Kumar Date: Sun, 1 May 2022 06:34:14 -0700 Subject: [PATCH] Add click install to fix ci (#193) * Add black --- .pre-commit-config.yaml | 2 +- BSM/Chapter_03_00_Bayesian_CLT.ipynb | 2 +- ...er_03_01_Gibbs_sampling_one_sample_t-test.ipynb | 4 ++-- ...er_03_02_Gibbs_sampling_two_sample_t-test.ipynb | 4 ++-- ...bbs_sampling_for_simple_linear_regression.ipynb | 6 +++--- ...r_03_09_Simple_linear_regression_in_PyMC3.ipynb | 2 +- Rethinking_2/Chp_04.ipynb | 4 ++-- Rethinking_2/Chp_06.ipynb | 2 +- Rethinking_2/Chp_09.ipynb | 10 +++++----- Rethinking_2/Chp_10.ipynb | 8 ++++---- Rethinking_2/Chp_14.ipynb | 14 +++++++------- Rethinking_2/Chp_16.ipynb | 4 ++-- .../End_of_chapter_problems/Chapter_7.ipynb | 14 +++++++------- 13 files changed, 38 insertions(+), 38 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e89ca74..ccf7ace 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] diff --git a/BSM/Chapter_03_00_Bayesian_CLT.ipynb b/BSM/Chapter_03_00_Bayesian_CLT.ipynb index 8a37b9f..f5d2a99 100644 --- a/BSM/Chapter_03_00_Bayesian_CLT.ipynb +++ b/BSM/Chapter_03_00_Bayesian_CLT.ipynb @@ -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", diff --git a/BSM/Chapter_03_01_Gibbs_sampling_one_sample_t-test.ipynb b/BSM/Chapter_03_01_Gibbs_sampling_one_sample_t-test.ipynb index 23d361d..2ebf2b2 100644 --- a/BSM/Chapter_03_01_Gibbs_sampling_one_sample_t-test.ipynb +++ b/BSM/Chapter_03_01_Gibbs_sampling_one_sample_t-test.ipynb @@ -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", @@ -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\");" diff --git a/BSM/Chapter_03_02_Gibbs_sampling_two_sample_t-test.ipynb b/BSM/Chapter_03_02_Gibbs_sampling_two_sample_t-test.ipynb index 0838b0e..7fa78c2 100644 --- a/BSM/Chapter_03_02_Gibbs_sampling_two_sample_t-test.ipynb +++ b/BSM/Chapter_03_02_Gibbs_sampling_two_sample_t-test.ipynb @@ -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", diff --git a/BSM/Chapter_03_03_Gibbs_sampling_for_simple_linear_regression.ipynb b/BSM/Chapter_03_03_Gibbs_sampling_for_simple_linear_regression.ipynb index 067a788..1a3f6b5 100644 --- a/BSM/Chapter_03_03_Gibbs_sampling_for_simple_linear_regression.ipynb +++ b/BSM/Chapter_03_03_Gibbs_sampling_for_simple_linear_regression.ipynb @@ -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", diff --git a/BSM/Chapter_03_09_Simple_linear_regression_in_PyMC3.ipynb b/BSM/Chapter_03_09_Simple_linear_regression_in_PyMC3.ipynb index 072a1aa..778e071 100644 --- a/BSM/Chapter_03_09_Simple_linear_regression_in_PyMC3.ipynb +++ b/BSM/Chapter_03_09_Simple_linear_regression_in_PyMC3.ipynb @@ -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", diff --git a/Rethinking_2/Chp_04.ipynb b/Rethinking_2/Chp_04.ipynb index db23321..36b6713 100644 --- a/Rethinking_2/Chp_04.ipynb +++ b/Rethinking_2/Chp_04.ipynb @@ -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", @@ -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))" ] }, { diff --git a/Rethinking_2/Chp_06.ipynb b/Rethinking_2/Chp_06.ipynb index 4d77079..558e91b 100644 --- a/Rethinking_2/Chp_06.ipynb +++ b/Rethinking_2/Chp_06.ipynb @@ -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", diff --git a/Rethinking_2/Chp_09.ipynb b/Rethinking_2/Chp_09.ipynb index 08890ac..206fb17 100644 --- a/Rethinking_2/Chp_09.ipynb +++ b/Rethinking_2/Chp_09.ipynb @@ -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", @@ -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])" ] @@ -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", diff --git a/Rethinking_2/Chp_10.ipynb b/Rethinking_2/Chp_10.ipynb index 4cba205..d75c636 100644 --- a/Rethinking_2/Chp_10.ipynb +++ b/Rethinking_2/Chp_10.ipynb @@ -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" ] }, @@ -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_" diff --git a/Rethinking_2/Chp_14.ipynb b/Rethinking_2/Chp_14.ipynb index c99c1d2..d817d53 100644 --- a/Rethinking_2/Chp_14.ipynb +++ b/Rethinking_2/Chp_14.ipynb @@ -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" ] }, @@ -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", @@ -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", @@ -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))" ] }, { @@ -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", @@ -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", diff --git a/Rethinking_2/Chp_16.ipynb b/Rethinking_2/Chp_16.ipynb index ee70636..aeb2d85 100644 --- a/Rethinking_2/Chp_16.ipynb +++ b/Rethinking_2/Chp_16.ipynb @@ -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", @@ -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" ] }, { diff --git a/Rethinking_2/End_of_chapter_problems/Chapter_7.ipynb b/Rethinking_2/End_of_chapter_problems/Chapter_7.ipynb index eb254a1..7d9385c 100644 --- a/Rethinking_2/End_of_chapter_problems/Chapter_7.ipynb +++ b/Rethinking_2/End_of_chapter_problems/Chapter_7.ipynb @@ -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", @@ -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", @@ -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)" ] @@ -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", @@ -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", @@ -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)" ] @@ -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)" ]