.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\2-examples\1.2_Intro_to_Bayesian_Inference.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_2-examples_1.2_Intro_to_Bayesian_Inference.py: Uniform Prior, several observations =================================== .. GENERATED FROM PYTHON SOURCE LINES 6-23 .. code-block:: default # sphinx_gallery_thumbnail_number = -1 import arviz as az import matplotlib.pyplot as plt import pyro import torch from matplotlib.ticker import StrMethodFormatter from gempy_probability.plot_posterior import PlotPosterior from _aux_func import infer_model y_obs = torch.tensor([2.12]) y_obs_list = torch.tensor([2.12, 2.06, 2.08, 2.05, 2.08, 2.09, 2.19, 2.07, 2.16, 2.11, 2.13, 1.92]) pyro.set_rng_seed(4003) .. GENERATED FROM PYTHON SOURCE LINES 24-31 .. code-block:: default az_data = infer_model( distributions_family="uniform_distribution", data=y_obs_list ) az.plot_trace(az_data) plt.show() .. image-sg:: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_001.png :alt: $\mu$, $\mu$, $\sigma$, $\sigma$ :srcset: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Warmup: 0%| | 0/1100 [00:00, ?it/s] Warmup: 2%|▏ | 17/1100 [00:00, 168.29it/s, step size=2.86e-02, acc. prob=0.714] Warmup: 3%|▍ | 34/1100 [00:00, 69.60it/s, step size=1.81e-02, acc. prob=0.745] Warmup: 4%|▌ | 48/1100 [00:00, 87.56it/s, step size=1.91e-02, acc. prob=0.757] Warmup: 5%|▋ | 60/1100 [00:00, 94.01it/s, step size=2.88e-02, acc. prob=0.766] Warmup: 7%|▊ | 72/1100 [00:00, 98.77it/s, step size=6.74e-03, acc. prob=0.760] Warmup: 8%|▉ | 85/1100 [00:00, 105.60it/s, step size=1.44e-02, acc. prob=0.769] Sample: 10%|█ | 112/1100 [00:00, 150.09it/s, step size=6.11e-01, acc. prob=0.869] Sample: 14%|█▌ | 152/1100 [00:01, 219.72it/s, step size=6.11e-01, acc. prob=0.886] Sample: 17%|█▉ | 188/1100 [00:01, 259.92it/s, step size=6.11e-01, acc. prob=0.875] Sample: 21%|██▎ | 226/1100 [00:01, 285.55it/s, step size=6.11e-01, acc. prob=0.877] Sample: 24%|██▋ | 265/1100 [00:01, 315.30it/s, step size=6.11e-01, acc. prob=0.887] Sample: 27%|███ | 301/1100 [00:01, 327.49it/s, step size=6.11e-01, acc. prob=0.894] Sample: 31%|███▎ | 337/1100 [00:01, 334.83it/s, step size=6.11e-01, acc. prob=0.898] Sample: 34%|███▋ | 371/1100 [00:01, 326.08it/s, step size=6.11e-01, acc. prob=0.895] Sample: 37%|████ | 410/1100 [00:01, 343.98it/s, step size=6.11e-01, acc. prob=0.893] Sample: 41%|████▍ | 448/1100 [00:01, 353.13it/s, step size=6.11e-01, acc. prob=0.891] Sample: 44%|████▊ | 485/1100 [00:02, 358.07it/s, step size=6.11e-01, acc. prob=0.886] Sample: 47%|█████▏ | 521/1100 [00:02, 349.56it/s, step size=6.11e-01, acc. prob=0.887] Sample: 51%|█████▌ | 558/1100 [00:02, 348.40it/s, step size=6.11e-01, acc. prob=0.888] Sample: 55%|██████ | 601/1100 [00:02, 371.89it/s, step size=6.11e-01, acc. prob=0.884] Sample: 58%|██████▍ | 639/1100 [00:02, 367.21it/s, step size=6.11e-01, acc. prob=0.885] Sample: 62%|██████▊ | 677/1100 [00:02, 369.76it/s, step size=6.11e-01, acc. prob=0.887] Sample: 65%|███████▏ | 715/1100 [00:02, 369.50it/s, step size=6.11e-01, acc. prob=0.890] Sample: 68%|███████▌ | 753/1100 [00:02, 357.84it/s, step size=6.11e-01, acc. prob=0.888] Sample: 73%|███████▉ | 798/1100 [00:02, 383.36it/s, step size=6.11e-01, acc. prob=0.886] Sample: 76%|████████▍ | 839/1100 [00:02, 389.01it/s, step size=6.11e-01, acc. prob=0.885] Sample: 80%|████████▊ | 879/1100 [00:03, 365.09it/s, step size=6.11e-01, acc. prob=0.886] Sample: 83%|█████████▏ | 916/1100 [00:03, 346.02it/s, step size=6.11e-01, acc. prob=0.887] Sample: 87%|█████████▌ | 957/1100 [00:03, 355.73it/s, step size=6.11e-01, acc. prob=0.887] Sample: 90%|█████████▉ | 993/1100 [00:03, 347.37it/s, step size=6.11e-01, acc. prob=0.886] Sample: 94%|█████████▍| 1033/1100 [00:03, 361.23it/s, step size=6.11e-01, acc. prob=0.886] Sample: 97%|█████████▋| 1070/1100 [00:03, 360.25it/s, step size=6.11e-01, acc. prob=0.886] Sample: 100%|██████████| 1100/1100 [00:03, 296.89it/s, step size=6.11e-01, acc. prob=0.887] C:\Users\MigueldelaVarga\PycharmProjects\VisualBayesic\venv\lib\site-packages\arviz\data\io_pyro.py:157: UserWarning: Could not get vectorized trace, log_likelihood group will be omitted. Check your model vectorization or set log_likelihood=False warnings.warn( posterior predictive shape not compatible with number of chains and draws.This can mean that some draws or even whole chains are not represented. .. GENERATED FROM PYTHON SOURCE LINES 32-41 .. code-block:: default p = PlotPosterior(az_data) p.create_figure(figsize=(9, 3), joyplot=False, marginal=False) p.plot_normal_likelihood('$\mu$', '$\sigma$', '$y$', iteration=-1, hide_bell=True) p.likelihood_axes.set_xlim(1.90, 2.2) p.likelihood_axes.xaxis.set_major_formatter(StrMethodFormatter('{x:,.2f}')) for tick in p.likelihood_axes.get_xticklabels(): tick.set_rotation(45) plt.show() .. image-sg:: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_002.png :alt: Likelihood :srcset: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 42-53 No matter which probability density function we choose, for real applications we will never find the exact data generated process---neither we will be able to say if we have found it for that matter---due to an oversimplification of reality. For most applications, the usual families of probability density functions and transformations of those are more than enough approximations for the purpose of the model. In Chapter [sec:model_selection], we will delve into this topic. Once the model is defined we need to infer the set of parameters \( \varTheta \) of the family of density functions over the observational space, \( \pi_S(y;\varTheta) \). In the case of the normal family, we need to infer the value of the mean, \( \mu \) and standard deviation \( \sigma \). Up to this point, all the description of the probabilistic modelling is agnostic in relation to Frequentist or Bayesian views. These two methodologies diverge on how they infer \( \varTheta \). .. GENERATED FROM PYTHON SOURCE LINES 56-65 .. code-block:: default p = PlotPosterior(az_data) p.create_figure(figsize=(9, 3), joyplot=False, marginal=False) p.plot_normal_likelihood('$\mu$', '$\sigma$', '$y$', iteration=-1, hide_lines=True) p.likelihood_axes.set_xlim(1.70, 2.40) p.likelihood_axes.xaxis.set_major_formatter(StrMethodFormatter('{x:,.2f}')) for tick in p.likelihood_axes.get_xticklabels(): tick.set_rotation(45) plt.show() .. image-sg:: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_003.png :alt: Likelihood :srcset: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-72 .. code-block:: default p = PlotPosterior(az_data) p.create_figure(figsize=(9, 9), joyplot=True, marginal=False, likelihood=False, n_samples=31) p.plot_joy(('$\mu$', '$\sigma$'), '$y$', iteration=14) plt.show() .. image-sg:: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_004.png :alt: 1.2 Intro to Bayesian Inference :srcset: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-86 .. code-block:: default p = PlotPosterior(az_data) p.create_figure(figsize=(9, 5), joyplot=False, marginal=True, likelihood=True) p.plot_marginal(var_names=['$\mu$', '$\sigma$'], plot_trace=False, credible_interval=.93, kind='kde', joint_kwargs={'contour': True, 'pcolormesh_kwargs': {}}, joint_kwargs_prior={'contour': False, 'pcolormesh_kwargs': {}}) p.plot_normal_likelihood('$\mu$', '$\sigma$', '$y$', iteration=-1, hide_lines=True) p.likelihood_axes.set_xlim(1.70, 2.40) plt.show() .. image-sg:: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_005.png :alt: Likelihood :srcset: /examples/2-examples/images/sphx_glr_1.2_Intro_to_Bayesian_Inference_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-97 License ======= The code in this case study is copyrighted by Miguel de la Varga and licensed under the new BSD (3-clause) license: https://opensource.org/licenses/BSD-3-Clause The text and figures in this case study are copyrighted by Miguel de la Varga and licensed under the CC BY-NC 4.0 license: https://creativecommons.org/licenses/by-nc/4.0/ Make sure to replace the links with actual hyperlinks if you're using a platform that supports it (e.g., Markdown or HTML). Otherwise, the plain URLs work fine for plain text. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.598 seconds) .. _sphx_glr_download_examples_2-examples_1.2_Intro_to_Bayesian_Inference.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 1.2_Intro_to_Bayesian_Inference.py <1.2_Intro_to_Bayesian_Inference.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 1.2_Intro_to_Bayesian_Inference.ipynb <1.2_Intro_to_Bayesian_Inference.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_