Title: Problem to reproduce Figure 1.5 · Issue #211 · BayesianModelingandComputationInPython/BookCode_Edition1 · GitHub
Open Graph Title: Problem to reproduce Figure 1.5 · Issue #211 · BayesianModelingandComputationInPython/BookCode_Edition1
X Title: Problem to reproduce Figure 1.5 · Issue #211 · BayesianModelingandComputationInPython/BookCode_Edition1
Description: Hello, I'm trying to reproduce the figure 1.5 from the book but since Pymc current version is different from the book, I'm struggling. My problem is with the Prior and Posterior predictive distribution (2nd and 4th graphs). I have define...
Open Graph Description: Hello, I'm trying to reproduce the figure 1.5 from the book but since Pymc current version is different from the book, I'm struggling. My problem is with the Prior and Posterior predictive distribu...
X Description: Hello, I'm trying to reproduce the figure 1.5 from the book but since Pymc current version is different from the book, I'm struggling. My problem is with the Prior and Posterior predictive ...
Opengraph URL: https://github.com/BayesianModelingandComputationInPython/BookCode_Edition1/issues/211
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Problem to reproduce Figure 1.5","articleBody":"Hello,\r\nI'm trying to reproduce the figure 1.5 from the book but since Pymc current version is different from the book, I'm struggling.\r\nMy problem is with the Prior and Posterior predictive distribution (2nd and 4th graphs).\r\n\r\nI have defined the model as in the book:\r\n\r\n```\r\nimport numpy as np\r\nimport pymc as pm\r\nimport matplotlib.pyplot as plt\r\nimport pandas as pd\r\nimport scipy\r\nimport scipy.stats as stats\r\nimport arviz as az\r\n\r\naz.style.use(\"arviz-grayscale\")\r\nplt.rcParams['figure.dpi'] = 300\r\nnp.random.seed(521)\r\nviridish = [(0.2823529411764706, 0.11372549019607843, 0.43529411764705883, 1.0),\r\n (0.1450980392156863, 0.6705882352941176, 0.5098039215686274, 1.0),\r\n (0.6901960784313725, 0.8666666666666667, 0.1843137254901961, 1.0)]\r\n\r\nY = stats.bernoulli(0.7).rvs(20)\r\n# Declare a model in PyMC3\r\nwith pm.Model() as model:\r\n # Specify the prior distribution of unknown parameter\r\n θ = pm.Beta(\"θ\", alpha=1, beta=1)\r\n\r\n # Specify the likelihood distribution and condition on the observed data\r\n y_obs = pm.Binomial(\"y_obs\", n=1, p=θ, observed=Y)\r\n\r\n # Sample from the posterior distribution\r\n idata = pm.sample(1000, return_inferencedata=True)\r\n\r\npred_dists = (pm.sample_prior_predictive(1000, model=model),\r\n pm.sample_posterior_predictive(idata,model=model))\r\n```\r\nAnd next I separate the variables I want to plot:\r\n\r\n```\r\n# Prior\r\nprior_samples = pred_dists[0]['prior']['θ'].values # Prior observed\r\nprior_pred_samples = pred_dists[0]['prior_predictive'] # Prior predictions\r\n\r\n# Posterior\r\npost_distribution = idata.posterior[\"θ\"] # Posterior observed\r\nposterior_pred_samples = pred_dists[1]['posterior_predictive'] # Posterior Predictions\r\n\r\nposterior_pred_graph = posterior_pred_samples['y_obs'].sum(dim=['draw','chain'])\r\nprior_pred_samples_graph = prior_pred_samples['y_obs'].sum(dim=['draw','chain'])\r\n\r\nfig,axes =plt.subplots(4,1,gridspec_kw={'hspace': 0.1})\r\n\r\naz.plot_dist(prior_samples, plot_kwargs={\"color\":\"0.5\"},\r\n fill_kwargs={'alpha':1}, ax=axes[0])\r\naxes[0].set_title(\"Prior distribution\", fontweight='bold',fontsize=10)\r\naxes[0].set_xlim(0, 1)\r\naxes[0].set_ylim(0, 4)\r\naxes[0].tick_params(axis='both', pad=7)\r\naxes[0].set_xlabel(\"θ\")\r\n\r\n\r\naz.plot_dist(prior_pred_samples_graph, plot_kwargs={\"color\":\"0.5\"},\r\n fill_kwargs={'alpha':1}, ax=axes[1])\r\naxes[1].set_title(\"Prior predictive distribution\", fontweight='bold',fontsize=10)\r\n# axes[1].set_xlim(-1, 21)\r\n# axes[1].set_ylim(0, 0.15)\r\naxes[1].tick_params(axis='both', pad=7)\r\naxes[1].set_xlabel(\"number of success\")\r\n\r\naz.plot_dist(post_distribution, plot_kwargs={\"color\":\"0.5\"},\r\n fill_kwargs={'alpha':1},ax=axes[2])\r\naxes[2].set_title(\"Posterior distribution\", fontweight='bold',fontsize=10)\r\naxes[2].set_xlim(0, 1)\r\naxes[2].set_ylim(0, 5)\r\naxes[2].tick_params(axis='both', pad=7)\r\naxes[2].set_xlabel(\"θ\")\r\n\r\naz.plot_dist(posterior_pred_graph, plot_kwargs={\"color\":\"0.5\"},\r\n fill_kwargs={'alpha':1}, ax=axes[3])\r\naxes[3].set_title(\"Posterior predictive distribution\", fontweight='bold',fontsize=10)\r\n# axes[3].set_xlim(-1, 21)\r\n# axes[3].set_ylim(0, 0.15)\r\naxes[3].tick_params(axis='both', pad=7)\r\naxes[3].set_xlabel(\"number of success\") ```\r\n\r\n\r\n\r\n\r\n\r\nAnd I guess I'm not evaluating the posterior_pred_graph and prior_pred_samples_graph correctly? shouldn't be this sum? Could someone help me on this?","author":{"url":"https://github.com/jc-barreto","@type":"Person","name":"jc-barreto"},"datePublished":"2023-08-07T15:46:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/211/BookCode_Edition1/issues/211"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:764d1bd2-4087-ff4a-7313-7b0cb32eae2b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E50:C6FAD:15909:1CB75:698F9660 |
| html-safe-nonce | 020bf398fd5d4bbc73d3db558b1adb608adbc504124cac8cb4638e9f3def38a3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTUwOkM2RkFEOjE1OTA5OjFDQjc1OjY5OEY5NjYwIiwidmlzaXRvcl9pZCI6IjcwMzE0OTM5NDY1MDE5OTQwODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8b1156408b1933a14c309032c3a3dcb499a28e743c7331ba4548c9554867141a |
| hovercard-subject-tag | issue:1839758459 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/BayesianModelingandComputationInPython/BookCode_Edition1/211/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f67c2bfe1d8364b909c74ebbaaf2580c0973bf20c40abe2b431f4443b8aabfba/BayesianModelingandComputationInPython/BookCode_Edition1/issues/211 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f67c2bfe1d8364b909c74ebbaaf2580c0973bf20c40abe2b431f4443b8aabfba/BayesianModelingandComputationInPython/BookCode_Edition1/issues/211 |
| og:image:alt | Hello, I'm trying to reproduce the figure 1.5 from the book but since Pymc current version is different from the book, I'm struggling. My problem is with the Prior and Posterior predictive distribu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jc-barreto |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f47eb8d0aaafbfcb6a8220a40bd81431acf688857c575e6489670c394cfa36f |
| turbo-cache-control | no-preview |
| go-import | github.com/BayesianModelingandComputationInPython/BookCode_Edition1 git https://github.com/BayesianModelingandComputationInPython/BookCode_Edition1.git |
| octolytics-dimension-user_id | 84690770 |
| octolytics-dimension-user_login | BayesianModelingandComputationInPython |
| octolytics-dimension-repository_id | 397286760 |
| octolytics-dimension-repository_nwo | BayesianModelingandComputationInPython/BookCode_Edition1 |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 397286760 |
| octolytics-dimension-repository_network_root_nwo | BayesianModelingandComputationInPython/BookCode_Edition1 |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 09a8136f6e8f8fd3078d54ce5f8b7adb493d44a7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width