{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "6bcac793", "metadata": { "tags": [ "hide-output" ] }, "outputs": [], "source": [ "import arviz as az\n", "import pymc as pm\n", "from pymc.math import switch, ge\n", "import pandas as pd\n", "import numpy as np\n", "\n", "%load_ext lab_black" ] }, { "cell_type": "markdown", "id": "f8d8156f", "metadata": {}, "source": [ "# Loading Data, Step Function, and Deterministic Variables*\n", "\n", "This example introduces data containers, tracking of deterministic variables, and shows how to recreate the BUGS step function in PyMC." ] }, { "cell_type": "markdown", "id": "7d49e918-c409-4493-bf64-49a4f3aa5de1", "metadata": {}, "source": [ "## Taste of Cheese\n", "\n", "Adapted from [Unit 6: cheese.odc](https://raw.githubusercontent.com/areding/6420-pymc/main/original_examples/Codes4Unit6/cheese.odc).\n", "\n", "The link in the original .odc file is dead. I downloaded the data from [here](https://www3.nd.edu/~busiforc/handouts/Data%20and%20Stories/multicollinearity/Cheese%20Taste/Cheddar%20Cheese%20Data.html) and have a copy [here](https://raw.githubusercontent.com/areding/6420-pymc/main/data/cheese.csv)." ] }, { "cell_type": "markdown", "id": "85140e7c-6ae9-4b4e-9c6d-8bcdd0b1dc27", "metadata": {}, "source": [ "As cheddar cheese matures, a variety of chemical processes take place. The taste of matured cheese is related to the concentration of several chemicals in the final product. In a study of cheddar cheese from the LaTrobe Valley of Victoria, Australia, samples of cheese were analyzed for their chemical composition and were subjected to taste tests. Overall taste scores were obtained by combining the scores from several tasters.\n", "\n", "Can the score be predicted well by the predictors: Acetic, H2S, and Lactic?" ] }, { "cell_type": "code", "execution_count": 2, "id": "4d1b6b90-c954-4550-b395-1b43b0eb3e68", "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv(\"../data/cheese.csv\", index_col=0)\n", "X = data[[\"Acetic\", \"H2S\", \"Lactic\"]].to_numpy()\n", "# add intercept column to X\n", "X_aug = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)\n", "y = data[\"taste\"].to_numpy()" ] }, { "cell_type": "code", "execution_count": 3, "id": "37b96507-8960-4de0-9749-9fbc0e10deca", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
tasteAceticH2SLactic
112.34.5433.1350.86
220.95.1595.0431.53
339.05.3665.4381.57
447.95.7597.4961.81
55.64.6633.8070.99
625.95.6977.6011.09
737.35.8928.7261.29
821.96.0787.9661.78
918.14.8983.8501.29
1021.05.2424.1741.58
1134.95.7406.1421.68
1257.26.4467.9081.90
130.74.4772.9961.06
1425.95.2364.9421.30
1554.96.1516.7521.52
1640.96.3659.5881.74
1715.94.7873.9121.16
186.45.4124.7001.49
1918.05.2476.1741.63
2038.95.4389.0641.99
2114.04.5644.9491.15
2215.25.2985.2201.33
2332.05.4559.2421.44
2456.75.85510.1992.01
2516.85.3663.6641.31
2611.66.0433.2191.46
2726.56.4586.9621.72
280.75.3283.9121.25
2913.45.8026.6851.08
305.56.1764.7871.25
\n", "
" ], "text/plain": [ " taste Acetic H2S Lactic\n", "1 12.3 4.543 3.135 0.86\n", "2 20.9 5.159 5.043 1.53\n", "3 39.0 5.366 5.438 1.57\n", "4 47.9 5.759 7.496 1.81\n", "5 5.6 4.663 3.807 0.99\n", "6 25.9 5.697 7.601 1.09\n", "7 37.3 5.892 8.726 1.29\n", "8 21.9 6.078 7.966 1.78\n", "9 18.1 4.898 3.850 1.29\n", "10 21.0 5.242 4.174 1.58\n", "11 34.9 5.740 6.142 1.68\n", "12 57.2 6.446 7.908 1.90\n", "13 0.7 4.477 2.996 1.06\n", "14 25.9 5.236 4.942 1.30\n", "15 54.9 6.151 6.752 1.52\n", "16 40.9 6.365 9.588 1.74\n", "17 15.9 4.787 3.912 1.16\n", "18 6.4 5.412 4.700 1.49\n", "19 18.0 5.247 6.174 1.63\n", "20 38.9 5.438 9.064 1.99\n", "21 14.0 4.564 4.949 1.15\n", "22 15.2 5.298 5.220 1.33\n", "23 32.0 5.455 9.242 1.44\n", "24 56.7 5.855 10.199 2.01\n", "25 16.8 5.366 3.664 1.31\n", "26 11.6 6.043 3.219 1.46\n", "27 26.5 6.458 6.962 1.72\n", "28 0.7 5.328 3.912 1.25\n", "29 13.4 5.802 6.685 1.08\n", "30 5.5 6.176 4.787 1.25" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 4, "id": "e977a978-f60e-4c22-8434-4fe61a096af1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1. , 4.543, 3.135, 0.86 ],\n", " [ 1. , 5.159, 5.043, 1.53 ],\n", " [ 1. , 5.366, 5.438, 1.57 ],\n", " [ 1. , 5.759, 7.496, 1.81 ],\n", " [ 1. , 4.663, 3.807, 0.99 ],\n", " [ 1. , 5.697, 7.601, 1.09 ],\n", " [ 1. , 5.892, 8.726, 1.29 ],\n", " [ 1. , 6.078, 7.966, 1.78 ],\n", " [ 1. , 4.898, 3.85 , 1.29 ],\n", " [ 1. , 5.242, 4.174, 1.58 ],\n", " [ 1. , 5.74 , 6.142, 1.68 ],\n", " [ 1. , 6.446, 7.908, 1.9 ],\n", " [ 1. , 4.477, 2.996, 1.06 ],\n", " [ 1. , 5.236, 4.942, 1.3 ],\n", " [ 1. , 6.151, 6.752, 1.52 ],\n", " [ 1. , 6.365, 9.588, 1.74 ],\n", " [ 1. , 4.787, 3.912, 1.16 ],\n", " [ 1. , 5.412, 4.7 , 1.49 ],\n", " [ 1. , 5.247, 6.174, 1.63 ],\n", " [ 1. , 5.438, 9.064, 1.99 ],\n", " [ 1. , 4.564, 4.949, 1.15 ],\n", " [ 1. , 5.298, 5.22 , 1.33 ],\n", " [ 1. , 5.455, 9.242, 1.44 ],\n", " [ 1. , 5.855, 10.199, 2.01 ],\n", " [ 1. , 5.366, 3.664, 1.31 ],\n", " [ 1. , 6.043, 3.219, 1.46 ],\n", " [ 1. , 6.458, 6.962, 1.72 ],\n", " [ 1. , 5.328, 3.912, 1.25 ],\n", " [ 1. , 5.802, 6.685, 1.08 ],\n", " [ 1. , 6.176, 4.787, 1.25 ]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_aug" ] }, { "cell_type": "code", "execution_count": 5, "id": "77a9a95a-6e17-49a9-9711-975a4d25111d", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Auto-assigning NUTS sampler...\n", "Initializing NUTS using jitter+adapt_diag...\n", "Multiprocess sampling (4 chains in 4 jobs)\n", "NUTS: [beta, tau]\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " \n", " 100.00% [24000/24000 00:19<00:00 Sampling 4 chains, 0 divergences]\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "Sampling 4 chains for 1_000 tune and 5_000 draw iterations (4_000 + 20_000 draws total) took 20 seconds.\n", "Sampling: [taste_score]\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " \n", " 100.00% [20000/20000 00:00<00:00]\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "with pm.Model() as m:\n", " # associate data with model (this makes prediction easier)\n", " X_data = pm.Data(\"X\", X_aug, mutable=True)\n", " y_data = pm.Data(\"y\", y, mutable=False)\n", "\n", " # priors\n", " beta = pm.Normal(\"beta\", mu=0, sigma=1000, shape=X.shape[1] + 1)\n", " tau = pm.Gamma(\"tau\", alpha=0.001, beta=0.001)\n", " sigma = pm.Deterministic(\"sigma\", 1 / pm.math.sqrt(tau))\n", "\n", " mu = pm.math.dot(X_data, beta)\n", "\n", " # likelihood\n", " pm.Normal(\"taste_score\", mu=mu, sigma=sigma, observed=y_data)\n", "\n", " # start sampling\n", " trace = pm.sample(5000, target_accept=0.95)\n", " pm.sample_posterior_predictive(trace, extend_inferencedata=True)" ] }, { "cell_type": "code", "execution_count": 6, "id": "6bae98d7-3083-4351-91ee-35afcdd2fb48", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
meansdhdi_2.5%hdi_97.5%mcse_meanmcse_sdess_bulkess_tailr_hat
beta[0]-28.91020.681-71.35810.1560.2440.1777242.08929.01.0
beta[1]0.3474.660-9.1259.1360.0560.0417031.08609.01.0
beta[2]3.9131.3101.3156.4570.0140.0109333.010101.01.0
beta[3]19.6168.9002.88737.9180.0890.06310111.010106.01.0
tau0.0100.0030.0050.0150.0000.00010585.011160.01.0
sigma10.4451.5137.77513.5200.0150.01110585.011160.01.0
\n", "
" ], "text/plain": [ " mean sd hdi_2.5% hdi_97.5% mcse_mean mcse_sd ess_bulk \\\n", "beta[0] -28.910 20.681 -71.358 10.156 0.244 0.177 7242.0 \n", "beta[1] 0.347 4.660 -9.125 9.136 0.056 0.041 7031.0 \n", "beta[2] 3.913 1.310 1.315 6.457 0.014 0.010 9333.0 \n", "beta[3] 19.616 8.900 2.887 37.918 0.089 0.063 10111.0 \n", "tau 0.010 0.003 0.005 0.015 0.000 0.000 10585.0 \n", "sigma 10.445 1.513 7.775 13.520 0.015 0.011 10585.0 \n", "\n", " ess_tail r_hat \n", "beta[0] 8929.0 1.0 \n", "beta[1] 8609.0 1.0 \n", "beta[2] 10101.0 1.0 \n", "beta[3] 10106.0 1.0 \n", "tau 11160.0 1.0 \n", "sigma 11160.0 1.0 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "az.summary(trace, hdi_prob=0.95)" ] }, { "cell_type": "code", "execution_count": 7, "id": "e1fbd660-9aaf-49c5-83f3-db9e7cf97f9c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "r2 0.576223\n", "r2_std 0.075841\n", "dtype: float64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_pred = trace.posterior_predictive.stack(sample=(\"chain\", \"draw\"))[\n", " \"taste_score\"\n", "].values.T\n", "az.r2_score(y, y_pred)" ] }, { "cell_type": "markdown", "id": "1cc69158-d9a4-4cab-9e65-78acc4c61bfc", "metadata": {}, "source": [ "Results are pretty close to OpenBUGS:\n", "\n", "| mean | sd | MC_error | val2.5pc | median | val97.5pc | start | sample | |\n", "|--------------|----------|-----------------|------------|---------|-------------|---------|--------|--------|\n", "| beta0 | -29.75 | 20.24 | 0.7889 | -70.06 | -29.75 | 11.11 | 1000 | 100001 |\n", "| beta1 | 0.4576 | 4.6 | 0.189 | -8.716 | 0.4388 | 9.786 | 1000 | 100001 |\n", "| beta2 | 3.906 | 1.291 | 0.02725 | 1.345 | 3.912 | 6.47 | 1000 | 100001 |\n", "| beta3 | 19.79 | 8.893 | 0.2379 | 2.053 | 19.88 | 37.2 | 1000 | 100001 |\n", "| tau | 0.009777 | 0.002706 | 2.29E-05 | 0.00522 | 0.009528 | 0.01575 | 1000 | 100001 |" ] }, { "cell_type": "markdown", "id": "8547889b-0b27-4657-87ff-93efe9ad3532", "metadata": {}, "source": [ "PyMC gives some warnings about the model unless we increase the ```target_accept``` parameter of ```pm.sample```, while BUGS doesn't. This is because PyMC uses more diagnostics to check if there are any problems with its exploration of the parameter space. Divergences indicate bias in the results. BUGS will happily run this model without reporting any problems, but it doesn't mean that there aren't any.\n", "\n", "For further reading, check out [Diagnosing Biased Inference with Divergences](https://docs.pymc.io/en/v3/pymc-examples/examples/diagnostics_and_criticism/Diagnosing_biased_Inference_with_Divergences.html)." ] }, { "cell_type": "markdown", "id": "6925a83f", "metadata": {}, "source": [ "## Stress, Diet, and Acids\n", "\n", "Adapted from [Unit 6: stressacids.odc](https://raw.githubusercontent.com/areding/6420-pymc/main/original_examples/Codes4Unit6/stressacids.odc).\n", "\n", "In the study [Interrelationships Between Stress, Dietary Intake, and Plasma Ascorbic Acid During Pregnancy](https://vtechworks.lib.vt.edu/handle/10919/74486) conducted at the Virginia Polytechnic Institute and State University, the plasma ascorbic acid levels of pregnant women were compared for smokers versus non-smokers. Thirty-two women in the last three months of pregnancy, free of major health disorders, and ranging in age from 15 to 32 years were selected for the study. Prior to the collection of 20 ml of blood, the participants were told to avoid breakfast, forego their vitamin supplements, and avoid foods high in ascorbic acid content. From the blood samples, the plasma ascorbic acid values of each subject were determined in milligrams per 100 milliliters.\n", "\n", "---" ] }, { "cell_type": "markdown", "id": "858157aa", "metadata": {}, "source": [ "The purpose of this example in lectures was mostly just to show different ways to load data in BUGS. I'm not going to go into that too much, since there are a million ways to prepare your data in Python. In the next cell, I start with the data pasted from ```stressacids.odc```, then use list comprehensions to create one list for smokers and one for nonsmokers." ] }, { "cell_type": "code", "execution_count": 8, "id": "627f4609", "metadata": {}, "outputs": [], "source": [ "# fmt: off\n", "plasma = [0.97, 0.72, 1.00, 0.81, 0.62, 1.32, 1.24, 0.99, 0.90, 0.74,\n", " 0.88, 0.94, 1.06, 0.86, 0.85, 0.58, 0.57, 0.64, 0.98, 1.09,\n", " 0.92, 0.78, 1.24, 1.18, 0.48, 0.71, 0.98, 0.68, 1.18, 1.36,\n", " 0.78, 1.64]\n", "\n", "smo = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \n", " 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2]\n", "# fmt: on\n", "\n", "nonsmokers = [x for x, y in zip(plasma, smo) if y == 1]\n", "smokers = [x for x, y in zip(plasma, smo) if y == 2]" ] }, { "cell_type": "markdown", "id": "d12bf8fc", "metadata": {}, "source": [ "### BUGS step function\n", "\n", "I think this is the first time we've seen the BUGS step function.\n", "\n", "BUGS defines the step function like this:\n", "\n", "```step(e)``` is 1 if e >= 0; 0 otherwise.\n", "\n", "Keep in mind that in PyMC, step functions are how they refer to the algorithms used for sampling, as in NUTS or Metropolis. Just different terminology.\n", "\n", "We can recreate the BUGS step function with ```pm.math.switch()```:\n", "\n", "```\n", "pm.math.switch(e >= 0, 1, 0)\n", "\n", "```\n", "\n", "We should also probably use pm.math.ge for greater than or equal, as well, so:\n", "\n", "```\n", "pm.math.switch(ge(e,0), 1, 0)\n", "\n", "```" ] }, { "cell_type": "markdown", "id": "b9751b38", "metadata": {}, "source": [ "### How do I track non-random variables in PyMC?\n", "\n", "One nice thing about BUGS is you can easily track both deterministic and non-deterministic variables while sampling. For PyMC, you can wrap these in [```pm.Deterministic()```](https://docs.pymc.io/en/v3/api/model.html). Just make sure to use [```pm.math```](https://docs.pymc.io/en/v3/api/math.html) functions where possible." ] }, { "cell_type": "code", "execution_count": 9, "id": "87127e8d", "metadata": { "tags": [ "hide-output" ] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Auto-assigning NUTS sampler...\n", "Initializing NUTS using jitter+adapt_diag...\n", "Multiprocess sampling (4 chains in 4 jobs)\n", "NUTS: [tau_nonsmokers, mu_nonsmokers, tau_smokers, mu_smokers]\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " \n", " 100.00% [24000/24000 00:02<00:00 Sampling 4 chains, 0 divergences]\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "Sampling 4 chains for 1_000 tune and 5_000 draw iterations (4_000 + 20_000 draws total) took 3 seconds.\n" ] } ], "source": [ "with pm.Model() as m:\n", " # priors\n", " tau_nonsmokers = pm.Gamma(\"tau_nonsmokers\", alpha=0.0001, beta=0.0001)\n", " sigma_nonsmokers = 1 / pm.math.sqrt(tau_nonsmokers)\n", " mu_nonsmokers = pm.Normal(\"mu_nonsmokers\", mu=0, sigma=100)\n", "\n", " tau_smokers = pm.Gamma(\"tau_smokers\", alpha=0.0001, beta=0.0001)\n", " sigma_smokers = 1 / pm.math.sqrt(tau_smokers)\n", " mu_smokers = pm.Normal(\"mu_smokers\", mu=0, sigma=100)\n", "\n", " # likelihood\n", " plasma_aa_ns = pm.Normal(\n", " \"nonsmokers_aa\", mu=mu_nonsmokers, sigma=sigma_nonsmokers, observed=nonsmokers\n", " )\n", " plasma_aa_s = pm.Normal(\n", " \"smokers_aa\", mu=mu_smokers, sigma=sigma_smokers, observed=smokers\n", " )\n", "\n", " testmu = pm.Deterministic(\"test_mu\", switch(ge(mu_smokers, mu_nonsmokers), 1, 0))\n", " r = pm.Deterministic(\"prec_ratio\", tau_nonsmokers / tau_smokers)\n", "\n", " # start sampling\n", " trace = pm.sample(5000)" ] }, { "cell_type": "code", "execution_count": 10, "id": "f7700966", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
meansdhdi_2.5%hdi_97.5%mcse_meanmcse_sdess_bulkess_tailr_hat
mu_nonsmokers0.9120.0450.8261.0040.0000.00013842.012195.01.0
mu_smokers0.9760.1650.6461.2990.0020.00113965.09774.01.0
tau_nonsmokers22.6056.7589.90635.4760.0540.03815096.013040.01.0
tau_smokers6.5623.4930.86613.4130.0260.01915445.010342.01.0
test_mu0.6640.4720.0001.0000.0040.00316226.016226.01.0
prec_ratio4.8054.1570.68811.7250.0410.03115058.011605.01.0
\n", "
" ], "text/plain": [ " mean sd hdi_2.5% hdi_97.5% mcse_mean mcse_sd \\\n", "mu_nonsmokers 0.912 0.045 0.826 1.004 0.000 0.000 \n", "mu_smokers 0.976 0.165 0.646 1.299 0.002 0.001 \n", "tau_nonsmokers 22.605 6.758 9.906 35.476 0.054 0.038 \n", "tau_smokers 6.562 3.493 0.866 13.413 0.026 0.019 \n", "test_mu 0.664 0.472 0.000 1.000 0.004 0.003 \n", "prec_ratio 4.805 4.157 0.688 11.725 0.041 0.031 \n", "\n", " ess_bulk ess_tail r_hat \n", "mu_nonsmokers 13842.0 12195.0 1.0 \n", "mu_smokers 13965.0 9774.0 1.0 \n", "tau_nonsmokers 15096.0 13040.0 1.0 \n", "tau_smokers 15445.0 10342.0 1.0 \n", "test_mu 16226.0 16226.0 1.0 \n", "prec_ratio 15058.0 11605.0 1.0 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "az.summary(trace, hdi_prob=0.95)" ] }, { "cell_type": "code", "execution_count": 11, "id": "65589dc9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Last updated: Sat Aug 05 2023\n", "\n", "Python implementation: CPython\n", "Python version : 3.11.0\n", "IPython version : 8.9.0\n", "\n", "pytensor: 2.11.1\n", "\n", "pymc : 5.3.0\n", "numpy : 1.24.2\n", "arviz : 0.15.1\n", "pandas: 1.5.3\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -n -u -v -iv -p pytensor" ] }, { "cell_type": "code", "execution_count": null, "id": "0266f05f", "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.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }