Uniform Prior, several observations

# 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)
az_data = infer_model(
    distributions_family="uniform_distribution",
    data=y_obs_list
)
az.plot_trace(az_data)
plt.show()
$\mu$, $\mu$, $\sigma$, $\sigma$
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.
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()
Likelihood

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 ).

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()
Likelihood
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()
1.2 Intro to Bayesian Inference
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()
Likelihood

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.

Total running time of the script: (0 minutes 7.598 seconds)

Gallery generated by Sphinx-Gallery