Exemplo n.º 1
0
    def plot_marginal_posterior(self,
                                plotters,
                                iteration=-1,
                                **marginal_kwargs):
        marginal_kwargs.setdefault("plot_kwargs", {})
        marginal_kwargs["plot_kwargs"]["linewidth"] = self.linewidth
        marginal_kwargs.setdefault('fill_kwargs', {})

        marginal_kwargs["plot_kwargs"].setdefault('color', default_l)
        marginal_kwargs['fill_kwargs'].setdefault('color', default_l)
        marginal_kwargs['fill_kwargs'].setdefault('alpha', .8)

        # Flatten data
        x = plotters[0][2].flatten()[:iteration]
        y = plotters[1][2].flatten()[:iteration]

        for val, ax, rotate in ((x, self.ax_hist_x, False), (y, self.ax_hist_y,
                                                             True)):
            plot_dist(val,
                      textsize=self.xt_labelsize,
                      rotated=rotate,
                      ax=ax,
                      **marginal_kwargs)
Exemplo n.º 2
0
import bokeh.plotting as bkp
from bokeh.layouts import row
import numpy as np
import arviz as az

a = np.random.poisson(4, 1000)
b = np.random.normal(0, 1, 1000)

figure_kwargs = dict(height=500, width=500, output_backend="webgl")
ax_poisson = bkp.figure(**figure_kwargs)
ax_normal = bkp.figure(**figure_kwargs)

az.plot_dist(
    a,
    color="black",
    label="Poisson",
    ax=ax_poisson,
    backend="bokeh",
    show=False,
)
az.plot_dist(b,
             color="red",
             label="Gaussian",
             ax=ax_normal,
             backend="bokeh",
             show=False)

ax = row(ax_poisson, ax_normal)

if az.rcParams["plot.bokeh.show"]:
    bkp.show(ax)
Exemplo n.º 3
0
`az.style` is just and alias of `matplotlib.pyplot.style`, so everything you can do with one of them you can do with the other. 

All styles included with ArviZ use the same color-blind friendly palette. This palette was designed using https://colorcyclepicker.mpetroff.net/. If you need to do plots in grey-scale we recommend to restrict yourself to the first 3 colors of the arviz default palette ('C0', 'C1' and 'C2'), otherwise you may need to use different [lines styles](https://matplotlib.org/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D.set_linestyle) or [different markers](https://matplotlib.org/api/markers_api.html#module-matplotlib.markers)

from matplotlib import lines
print(lines.lineStyles) 

from matplotlib import markers
print(markers.MarkerStyle.markers)

## Continuous and discrete distributions

A discrete distribution represents variables which can only take a countable number of values. Some examples of discrete random variables are number of coins in your pocket, spots on a giraffe, red cars in a city, people with flu etc. As we generally use integers to represent discrete variables, when ArviZ receives integers it will use [histograms](https://en.wikipedia.org/wiki/Histogram) to represent them. ArviZ always tries to associate the binned data with discrete values. For example in the following plot each _bar_ is associated with a integer in the interval [0, 9].

d_values = stats.poisson(3).rvs(500)
az.plot_dist(d_values);

A continuous distribution represents variables taking uncountable number of values. Some examples of continuous random variables are, the temperature during summer, blood pressure of a patience, the time needed to finish a task, etc. By default ArviZ use a kernel density estimation (KDE) to represent continuous distributions.

c_values = stats.gamma(2, 3).rvs(500)
az.plot_dist(c_values);

A kernel density estimation (KDE) is a non-parametric way to estimate the probability density function of a random variable.

Conceptually you place a _kernel function_ like a gaussian _on top_ of a data-point, then you sum all the gaussians, generally evaluated over a grid and not over the data-points. Results are normalized so the total area under the curve is one.

The following block of code shows a very simple example of a KDE.

_, ax = plt.subplots(figsize=(12, 4))
bw = 0.4
np.random.seed(19)
    trace = pm.sample(1000, cores=1, chains=2, return_inferencedata=False)

idata = az.from_pymc3(trace)

pred_dists = (pm.sample_prior_predictive(1000, model)["y_obs"],
              pm.sample_posterior_predictive(idata, 1000, model)["y_obs"])

dist = pred_dists[0]
print(dist.shape)
num_success = dist.sum(1)
print(num_success.shape)

fig, ax = plt.subplots()
az.plot_dist(pred_dists[0].sum(1),
             hist_kwargs={
                 "color": "0.5",
                 "bins": range(0, 22)
             })
ax.set_title(f"Prior predictive distribution", fontweight='bold')
ax.set_xlim(-1, 21)
ax.set_ylim(0, 0.15)
ax.set_xlabel("number of success")

fig, ax = plt.subplots()
az.plot_dist(pred_dists[1].sum(1),
             hist_kwargs={
                 "color": "0.5",
                 "bins": range(0, 22)
             })
ax.set_title(f"Posterior predictive distribution", fontweight='bold')
ax.set_xlim(-1, 21)
Exemplo n.º 5
0
"""
Dist Plot
=========

_thumb: .2, .8
"""
import matplotlib.pyplot as plt
import numpy as np
import arviz as az

az.style.use("arviz-darkgrid")

a = np.random.poisson(4, 1000)
b = np.random.normal(0, 1, 1000)

_, ax = plt.subplots(1, 2, figsize=(10, 4))
az.plot_dist(a, color="C1", label="Poisson", ax=ax[0])
az.plot_dist(b, color="C2", label="Gaussian", ax=ax[1])

plt.show()
ax.set_xticks(list(range(N + 1)))
ax.set_xticklabels(list(range(N + 1)))
ax.set_ylim(0, 0.15)
ax.set_xlabel("number of success")

fig, ax = plt.subplots()
ax.bar(np.arange(N + 1), post_pred_dist, align='center', color='grey')
ax.set_title(f"Posterior predictive distribution", fontweight='bold')
ax.set_xlim(-1, 21)
ax.set_xticks(list(range(N + 1)))
ax.set_xticklabels(list(range(N + 1)))
ax.set_ylim(0, 0.15)
ax.set_xlabel("number of success")

fig, ax = plt.subplots()
az.plot_dist(np.random.beta(a_prior, b_prior, 10000), plot_kwargs={"color": "0.5"},
             fill_kwargs={'alpha': 1})
ax.set_title("Prior distribution", fontweight='bold')
ax.set_xlim(0, 1)
ax.set_ylim(0, 4)
ax.tick_params(axis='both', pad=7)
ax.set_xlabel("θ")

fig, ax = plt.subplots()
az.plot_dist(np.random.beta(a_post, b_post, 10000), plot_kwargs={"color": "0.5"},
             fill_kwargs={'alpha': 1})
ax.set_title("Posterior distribution", fontweight='bold')
#ax.set_xlim(0, 1)
#ax.set_ylim(0, 4)
ax.tick_params(axis='both', pad=7)
ax.set_xlabel("θ")
Exemplo n.º 7
0
"""
Dist Plot
========

_thumb: .2, .8
"""
import matplotlib.pyplot as plt
import numpy as np
import arviz as az

az.style.use('arviz-darkgrid')

a = np.random.poisson(4, 1000)
b = np.random.normal(0, 1, 1000)

_, ax = plt.subplots(1, 2, figsize=(10, 4))
az.plot_dist(a, color='C1', label='Poisson', ax=ax[0])
az.plot_dist(b, color='C2', label='Gaussian', ax=ax[1])