def make_plot(trace):
    plot_training_data()
    # plot logistic curve
    theta = trace['θ'].mean(axis=0)
    idx = np.argsort(x_c)
    plt.plot(x_c[idx], theta[idx], color='C2', lw=3)
    az.plot_hpd(x_c, trace['θ'], color='C2')

    # plot decision boundary
    plt.vlines(trace['bd'].mean(), 0, 1, color='k')
    bd_hpd = az.hpd(trace['bd'])
    plt.fill_betweenx([0, 1], bd_hpd[0], bd_hpd[1], color='k', alpha=0.5)
コード例 #2
0
ファイル: viz.py プロジェクト: AmitKus/StatisticalRethinking
def plot_polynomial_regressions(d1,
                                trace_1,
                                xcol,
                                ycol,
                                degree=1,
                                credible_interval=.95,
                                plot_name='hpd',
                                ax=None):

    yval = d1[ycol].values
    # Polynomial features
    polynomial_features = PolynomialFeatures(degree=degree, include_bias=False)
    xval = polynomial_features.fit_transform(d1[xcol].values.reshape(-1, 1))

    if plot_name == 'hpd':
        if ax == None:
            fig, ax = plt.subplots(figsize=(6, 6))
        mu_mean = trace_1['mu']
        ax.scatter(xval[:, 0], yval)
        sorted_xval = np.argsort(xval[:, 0])
        ax.plot(xval[sorted_xval, 0], mu_mean.mean(0)[sorted_xval], 'C1')

        xseq = np.linspace(np.min(xval[:, 0]), np.max(xval[:, 0]), 50)
        xplot = polynomial_features.fit_transform(xseq.reshape(-1, 1)).T
        mu_pred = trace_1['a'].reshape(-1, 1) + np.dot(trace_1['b'], xplot)
        ax.plot(xseq, mu_pred.mean(0), 'k.')
        az.plot_hpd(xseq,
                    mu_pred,
                    fill_kwargs={'alpha': 0},
                    plot_kwargs={
                        'alpha': 1,
                        'color': 'k',
                        'ls': '--'
                    },
                    ax=ax,
                    credible_interval=credible_interval)
        ax.set_xlabel(xcol)
        ax.set_ylabel(ycol)
        ax.set_title('Polynomial fit with degree=%d' % degree)
コード例 #3
0
# get the mean of the parameters
alpha_m = trace["alpha"].mean()
beta_m = trace["beta"].mean()
# take some draws from the posterior of parameters
draws = range(0,len(trace["alpha"]),10)
ax.plot(x,trace["alpha"][draws] + trace["beta"][draws] * x[:,np.newaxis], c = "gray", alpha = 0.5)
ax.plot(x, alpha_m + beta_m * x, c = "k",label=f'y = {alpha_m:.2f} + {beta_m:.2f} * x')
ax.set_xlabel("x")
ax.set_ylabel("y",rotation = 0)
plt.legend()
plt.show()
# plot 2 
fig, ax = plt.subplots(figsize = (8,4))
ax.scatter(x,y)
ax.plot(x, alpha_m + beta_m * x, c = "k",label=f'y = {alpha_m:.2f} + {beta_m:.2f} * x')
az.plot_hpd(x,trace["mu"],credible_interval = 0.98,color = "k")
ax.set_xlabel("x")
ax.set_ylabel("y",rotation = 0)
plt.legend()
plt.show()
# plot 3
# get the y from the posterior distribution 
ppc = pm.sample_posterior_predictive(trace,samples = 2000,model = linear_model)
plt.figure()
plt.plot(x,y,"b.")
plt.plot(x,alpha_m + beta_m * x,c = "k",label=f'y = {alpha_m:.2f} + {beta_m:.2f} * x')
az.plot_hpd(x,ppc["obs"],credible_interval = 0.5, color = "gray")
az.plot_hpd(x,ppc["obs"],color = "gray")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
コード例 #4
0
ファイル: plot_hpd.py プロジェクト: kyleabeauchamp/arviz
"""
Plot HPD
====================

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

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

x_data = np.random.normal(0, 1, 100)
y_data = 2 + x_data * 0.5
y_data_rep = np.random.normal(y_data, 0.5, (200, 100))
plt.plot(x_data, y_data, 'C6')
az.plot_hpd(x_data, y_data_rep, color='k', plot_kwargs={'ls': '--'})
m_5_1_trace["bA"].shape

# %%
age_std_seq = np.linspace(-3, 3.2, 30)

m_5_1_trace = m_5_1_trace[::10]
mu_pred = np.zeros((len(age_std_seq), len(m_5_1_trace) * m_5_1_trace.nchains))

for i, age_std in enumerate(age_std_seq):
    mu_pred[i] = m_5_1_trace["a"] + m_5_1_trace["bA"] * age_std

# %%
mu_mean = mu_pred.mean(axis=1)
plt.plot(age_std_seq, mu_mean)

ax = az.plot_hpd(age_std_seq, mu_pred.T)
ax.set_xlabel("Median age marriage")
ax.set_ylabel("Divorce rate")

# %%
with pm.Model() as m_5_2:
    a = pm.Normal("a", 0, 0.2)
    bM = pm.Normal("bM", 0, 0.5)
    sigma = pm.Exponential("sigma", 1)
    mu = pm.Deterministic("mu", a + bM * data["Marriage_std"])

    divorce_rate_std = pm.Normal("divorce_rate_std",
                                 mu=mu,
                                 sigma=sigma,
                                 observed=data["Divorce_std"].values)
    prior_samples = pm.sample_prior_predictive()
コード例 #6
0
with model_rlg:
    log.info("The summary on the trace is as follows: %s",
             az.summary(trace, var_names=["alpha", "beta", "db", "pie"]))
    az.plot_trace(trace, var_names=["alpha", "beta", "db", "pie"])

# ------------------- plots ------------------------------------------- #

# get the mean of theta
theta_mean = trace["theta"].mean(0)
# get idx
idx = np.argsort(x_c)
# plot the predicted p of the data
plt.figure()
plt.plot(x_c[idx], theta_mean[idx], color="C2", lw=3)
# set a vertical line at the mean of the decision boundary
plt.vlines(trace["db"].mean(), 0, 1, color="k")
# get the hpd of db
db_hpd = az.hpd(trace["db"])
plt.fill_betweenx([0, 1], db_hpd[0], db_hpd[1], color="k", alpha=0.5)
plt.scatter(x_c,
            np.random.normal(y_0, 0.02),
            marker='.',
            color=[f'C{x}' for x in y_0])
az.plot_hpd(x_c, trace["theta"], color="C2")
plt.xlabel("petal length")
plt.ylabel("theta", rotation=0)
locs, _ = plt.xticks()
plt.xticks(locs, np.round(locs + x_0.mean(), 1))
plt.show()
コード例 #7
0
plt.plot(x, alpha_m + beta_m * x, c='k',
         label=f'y = {alpha_m:.2f} + {beta_m:.2f} * x')

plt.xlabel('x')
plt.ylabel('y', rotation=0)
plt.legend()
plt.savefig('B11197_03_05.png', dpi=300)


# In[9]:


plt.plot(x, alpha_m + beta_m * x, c='k',
         label=f'y = {alpha_m:.2f} + {beta_m:.2f} * x')

sig = az.plot_hpd(x, trace_g['μ'], credible_interval=0.98, color='k')

plt.xlabel('x')
plt.ylabel('y', rotation=0)
plt.legend()
plt.savefig('B11197_03_06.png', dpi=300)


# In[10]:


ppc = pm.sample_posterior_predictive(trace_g, samples=2000, model=model_g)


# In[11]:
コード例 #8
0
    # get the theta and db
    theta = pm.Deterministic("theta",
                             pm.math.sigmoid(alpha + pm.math.dot(x_0, beta)))
    db = pm.Deterministic("db",
                          -alpha / beta[1] - beta[0] / beta[1] * x_0[:, 0])
    # specify a likelihood
    y_obs = pm.Bernoulli("y_obs", p=theta, observed=y_0)
    # inference step
    trace = pm.sample(1500, tune=1000)

# --------------------- analyse the posterior -------------------------- #

with log_model:
    log.info("the summary of the trace is: %s",
             az.summary(trace, var_names=["alpha", "beta"]))

# ---------------------- plot the data with the decision boundary ------------- #

# initialize a figure
plt.figure(figsize=(12, 5))
# get the index to order the independent variable
idx = np.argsort(x_0[:, 0])
# get the mean of the decision boundary to plot
db = trace["db"].mean(0)[idx]
# scatter the true data
plt.scatter(x_0[:, 0], x_0[:, 1], c=[f'C{x}' for x in y_0])
# plot the decision boundary
plt.plot(x_0[:, 0][idx], db, c="k")
# get the hpd
az.plot_hpd(x_0[:, 0], trace["db"], color="k")
plt.show()
コード例 #9
0
# %%
mass_shared.set_value(np.log(mass))
neocortex_shared.set_value(neocortex)
post_pred = pm.sample_posterior_predictive(trace_m6_14, samples=10000, model=m6_14)

# %%
milk_ensemble = pm.sample_posterior_predictive_w(
    traces, 10000, models, weights=compare_df.weight.sort_index(ascending=True)
)

# %%
plt.figure(figsize=(8, 6))

plt.plot(neocortex, post_pred["kcal"].mean(0), ls="--", color="k")
az.plot_hpd(
    neocortex,
    post_pred["kcal"],
    fill_kwargs={"alpha": 0},
    plot_kwargs={"alpha": 1, "color": "k", "ls": "--"},
)

plt.plot(neocortex, milk_ensemble["kcal"].mean(0), color="C1")
az.plot_hpd(neocortex, milk_ensemble["kcal"])

plt.scatter(d["neocortex"], d["kcal.per.g"], facecolor="None", edgecolors="C0")

plt.ylim(0.3, 1)
plt.xlabel("neocortex")
plt.ylabel("kcal.per.g")
    bd = pm.Deterministic('bd', -α / β)  # decision boundary

    yl = pm.Bernoulli('yl', p=θ, observed=y_0)

    trace_0 = pm.sample(1000)

varnames = ['α', 'β', 'bd']
az.summary(trace_0, varnames)

theta = trace_0['θ'].mean(axis=0)
idx = np.argsort(x_c)

plt.figure()
# plot logistic curve
plt.plot(x_c[idx], theta[idx], color='C2', lw=3)
az.plot_hpd(x_c, trace_0['θ'], color='C2')

# plot decision boundary
plt.vlines(trace_0['bd'].mean(), 0, 1, color='k')
bd_hpd = az.hpd(trace_0['bd'])
plt.fill_betweenx([0, 1], bd_hpd[0], bd_hpd[1], color='k', alpha=0.5)

# plot jittered data
plt.scatter(x_c,
            np.random.normal(y_0, 0.02),
            marker='.',
            color=[f'C{x}' for x in y_0])

plt.xlabel(x_n)
plt.ylabel('p(y=1)', rotation=0)
# use original scale for xticks
with pm.Model() as model_simple:
    α = pm.Normal('α', mu=0, sd=10)
    β = pm.Normal('β', mu=0, sd=10)
    μ = α + pm.math.dot(x_c, β)    
    θ = pm.Deterministic('θ', pm.math.sigmoid(μ))
    bd = pm.Deterministic('bd', -α/β)
    y_1 = pm.Bernoulli('y_1', p=θ, observed=y_simple)
    trace_simple = pm.sample(1000, tune=1000)
#%% this is slow convergence
theta = trace_simple['θ'].mean(axis=0)
idx = np.argsort(x_c)
plt.plot(x_c[idx], theta[idx], color='C2', lw=3)
plt.vlines(trace_simple['bd'].mean(), 0, 1, color='k')
bd_hpd = az.hpd(trace_simple['bd'])
plt.fill_betweenx([0, 1], bd_hpd[0], bd_hpd[1], color='k', alpha=0.5)
plt.scatter(x_c, np.random.normal(y_simple, 0.02),
            marker='.', color=[f'C{x}' for x in y_simple])
az.plot_hpd(x_c, trace_simple['θ'], color='C2')
plt.xlabel(x_n)
plt.ylabel('θ', rotation=0)
locs, _ = plt.xticks()
plt.xticks(locs, np.round(locs + x_0.mean(), 1));
#%% 
data['age'] = data['age'] / 10
data['age2'] = np.square(data['age'])
with pm.Model() as logistic_model:
    pm.glm.GLM.from_formula('outcome ~ age + age2 + job + marital + education + default + housing + loan + contact + month + day_of_week + duration + campaign + pdays + previous + euribor3m', data, family = pm.glm.families.Binomial())
    trace = pm.sample(500, tune = 500, init = 'adapt_diag')
az.plot_trace(trace);
コード例 #12
0
loo_list = pm.loo(trace_m5, model=m5, pointwise=True)

pl.plot(waic_list.WAIC_i, marker='.', ls='', color='k');
pl.plot(loo_list.LOO_i, marker='s', ls='', markeredgecolor='r');


dfinal.head()
# Plotting the interaction

% matplotlib inline
_, axs = pl.subplots(ncols=2, figsize=(8,4))
ttls = ['Non-African', 'African']
df_m5 = pm.trace_to_dataframe(trace_m5)
for i, (axi, ttl) in enumerate(zip(axs, ttls)):
    axs[i].scatter(dfinal[dfinal.cont_africa==i].rugged_s, dfinal[dfinal.cont_africa==i].log_gdp_s)
    m5_μ = link(df_m5.filter(regex=f'{i}', axis=1), x)
    m5_μ_mean = m5_μ.mean(axis=0)
    m5_ci = pm.hpd(m5_μ, alpha=0.03)
    axs[i].plot(rugged_seq, m5_μ_mean, lw=1.5, color='k')
    axs[i].set_title(f'{ttl} nations')
    axs[i].set_xlabel('ruggedness (standardized)')
    axs[i].set_ylabel('log GDP (as prop. of mean)')
    #axs[i].fill_between(rugged_seq.flatten(), y1=m5_ci[:, 0], y2=m5_ci[:, 1], alpha=0.5)
    az.plot_hpd(rugged_seq, m5_μ, credible_interval=0.97, ax=axs[i], smooth=True)

rugged_seq.shape
df_m5_A0 = pm.trace_to_dataframe(trace_m5).filter(regex='0', axis=1)
m5_μ = link(df_m5_A0, x)
pm.hpd(m5_μ, alpha=0.03).shape
cond = d.condition.unique()
prosoc_l = d.prosoc_left.unique()

for i in range(len(rt)):
    tmp = []
    if rt[i].size < 2:
        continue
    for cp in cond:
        for pl in prosoc_l:
            tmp.append(
                np.mean(rt[i][(d.prosoc_left == pl) & d.chose_prosoc == cp]))
    pred_mean[i] = tmp

ticks = range(4)
mp = pred_mean.mean(0)
az.plot_hpd(ticks, pred_mean, color="k", smooth=False)
plt.plot(mp, color="k")
plt.xticks(ticks, ("0/0", "1/0", "0/1", "1/1"))
chimps = (d.groupby(["actor", "prosoc_left", "condition"
                     ]).agg("mean")["pulled_left"].values.reshape(7, -1))
for i in range(7):
    plt.plot(chimps[i], "C0")

plt.ylim(0, 1.1)

# %%
with pm.Model() as model_10_4:
    a = pm.Normal("alpha", 0, 10, shape=len(d.actor.unique()))
    bp = pm.Normal("bp", 0, 10)
    bpC = pm.Normal("bpC", 0, 10)
    p = pm.math.invlogit(a[d.actor.values] +
コード例 #14
0
_, ax = plt.subplots(1, 2, figsize=(12, 6))

ax[0].scatter(islands.lon2, islands.lat, psize, zorder=3)
ax[1].scatter(islands.logpop, islands.total_tools, psize, zorder=3)

for i, itext in enumerate(culture_labels):
    ax[0].text(islands.lon2[i] + 1, islands.lat[i] + 1, itext)
    ax[1].text(islands.logpop[i] + .1, islands.total_tools[i] - 2.5, itext)

ax[1].plot(log_pop_seq, np.median(lambda_post, axis=0), 'k--')

az.plot_hpd(log_pop_seq,
            lambda_post,
            fill_kwargs={'alpha': 0},
            plot_kwargs={
                'color': 'k',
                'ls': '--',
                'alpha': 1
            })

for i in range(10):
    for j in np.arange(i + 1, 10):
        ax[0].plot((islands.lon2[i], islands.lon2[j]),
                   (islands.lat[i], islands.lat[j]),
                   'C1-',
                   alpha=ρ.iloc[i, j]**2,
                   lw=4)
        ax[1].plot((islands.logpop[i], islands.logpop[j]),
                   (islands.total_tools[i], islands.total_tools[j]),
                   'C1-',
                   alpha=ρ.iloc[i, j]**2,
コード例 #15
0
"""
Plot HPD
========

_thumb: .8, .8
"""
import bokeh.plotting as bkp
import numpy as np
import arviz as az

x_data = np.random.normal(0, 1, 100)
y_data = 2 + x_data * 0.5
y_data_rep = np.random.normal(y_data, 0.5, (200, 100))
x_data_sorted = np.sort(x_data)

ax = az.plot_hpd(x_data, y_data_rep, color="red", backend="bokeh", show=False)
ax.line(x_data_sorted,
        2 + x_data_sorted * 0.5,
        line_color="black",
        line_width=3)

if az.rcParams["plot.bokeh.show"]:
    bkp.show(ax)
        f_pred = gp.conditional('f_pred', X_new)
        pred_samples = pm.sample_posterior_predictive(
            trace_iris, vars=[f_pred], samples=1000)
    
    # Plot results
    _, ax = plt.subplots(figsize=(10, 6))
    
    fp = logistic(pred_samples['f_pred'])
    fp_mean = np.mean(fp, 0)
    
    ax.plot(X_new[:, 0], fp_mean)
    # plot the data (with some jitter) and the true latent function
    ax.scatter(x_1, np.random.normal(y, 0.02),
               marker='.', color=[f'C{x}' for x in y])
    
    az.plot_hpd(X_new[:, 0], fp, color='C2')
    
    db = np.array([find_midpoint(f, X_new[:, 0], 0.5) for f in fp])
    db_mean = db.mean()
    db_hpd = az.hpd(db)
    ax.vlines(db_mean, 0, 1, color='k')
    ax.fill_betweenx([0, 1], db_hpd[0], db_hpd[1], color='k', alpha=0.5)
    ax.set_xlabel('sepal_length')
    ax.set_ylabel('θ', rotation=0)
    plt.savefig('../figures/gp_classify_iris1.pdf', dpi=300)

# Change kernel to be sum of SE and linear, to improve tail behavior

with pm.Model() as model_iris2:
    #ℓ = pm.HalfCauchy("ℓ", 1)
    ℓ = pm.Gamma('ℓ', 2, 0.5)
コード例 #17
0
        label = f'y={alpha_m:.2f} + {beta_m:.2f}*x')

plt.xlabel('x', fontsize=15)
plt.ylabel('y', rotation=0, fontsize=15)
plt.legend(fontsize=15)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.show()


# In[7]:


plt.plot(x, alpha_m + beta_m * x, c = 'k',
        label = f'y={alpha_m:.2f} + {beta_m:.2f}*x')
sig = az.plot_hpd(x, trace_g['μ'], credible_interval=0.98, color='0.5', plot_kwargs={'alpha':0.1})
plt.xlabel('x', fontsize=15)
plt.ylabel('y', rotation=0, fontsize=15)
plt.legend(fontsize=15)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.show()


# Another option is to plot the HPD of the posterior predictive.

# In[8]:


ppc = pm.sample_posterior_predictive(trace_g, samples=2000, model=model_g)
    
コード例 #18
0
    for cp in cond:
        for pl in prosoc_l:
            tmp.append(
                np.mean(
                    rt[i][
                        (d.prosoc_left[sel_actor].values == pl)
                        & (d.condition[sel_actor].values == cp)
                    ]
                )
            )
    pred_mean[i] = tmp

mp = pred_mean.mean(0)

_, ax = plt.subplots(1, 1, figsize=(5, 5))
az.plot_hpd(range(4), pred_mean, credible_interval=0.89, color="k")

ax.plot(mp, color="k")
chimps = (
    d.groupby(["actor", "prosoc_left", "condition"])
    .agg("mean")["pulled_left"]
    .values.reshape(7, -1)
)
ax.plot(chimps[chimp], "C0")

ax.set_ylim(0, 1.1)
ax.set_xlabel("prosoc_left/condition")
ax.set_ylabel("proportion pulled left")
plt.xticks(range(4), ("0/0", "1/0", "0/1", "1/1"))

# %%
    h = pm.Normal('h', mu=mu, sigma=sigma, observed=kcal_per_g)
    trace_neocortex_mass = pm.sample(cores=2)

#%%
# Plots
az.plot_forest([trace_neocortex_mass, trace_log_mass, trace_neocortex],
               model_names=['both_br_bl', 'only_br', 'only_bl'],
               var_names=['bl', 'br'])

#%%
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))

mu_mean = trace_neocortex['mu']
ax[0, 0].scatter(neocortex_perc, kcal_per_g)
ax[0, 0].plot(neocortex_perc, mu_mean.mean(0), 'C1')
az.plot_hpd(neocortex_perc, mu_mean, ax=ax[0, 0])

mu_mean = trace_log_mass['mu']
ax[0, 1].scatter(log_mass, kcal_per_g)
ax[0, 1].plot(log_mass, mu_mean.mean(0), 'C1')
az.plot_hpd(log_mass, mu_mean, ax=ax[0, 1])

seq = np.linspace(55, 76, 50)
mu_pred = trace_neocortex_mass['a'] + trace_neocortex_mass[
    'bl'] * seq[:, None] + trace_neocortex_mass['br'] * log_mass.mean()
ax[1, 0].plot(seq, mu_pred.mean(1), 'k')
az.plot_hpd(seq,
            mu_pred.T,
            fill_kwargs={'alpha': 0},
            plot_kwargs={
                'alpha': 1,
    β = pm.Normal('β', mu=0, sd=2, shape=len(x_n))

    μ = α + pm.math.dot(x_1, β)
    θ = pm.Deterministic('θ', 1 / (1 + pm.math.exp(-μ)))
    bd = pm.Deterministic('bd', -α / β[1] - β[0] / β[1] * x_1[:, 0])

    yl = pm.Bernoulli('yl', p=θ, observed=y_1)

    trace_1 = pm.sample(2000)

varnames = ['α', 'β']
#az.plot_forest(trace_1, var_names=varnames);

idx = np.argsort(x_1[:, 0])
bd = trace_1['bd'].mean(0)[idx]

plt.figure()
plt.scatter(x_1[:, 0], x_1[:, 1], c=[f'C{x}' for x in y_1])
plt.plot(x_1[:, 0][idx], bd, color='k')

az.plot_hpd(x_1[:, 0], trace_1['bd'], color='k')

plt.xlabel(x_n[0])
plt.ylabel(x_n[1])

plt.tight_layout()
if unbalanced:
    plt.savefig('../figures/logreg_iris_bayes_2d_unbalanced.pdf', dpi=300)
else:
    plt.savefig('../figures/logreg_iris_bayes_2d.pdf', dpi=300)
コード例 #21
0
ファイル: 03_working.py プロジェクト: nathanbraun/BAP
         alpha=0.5)

plt.plot(x,
         alpha_m + beta_m * x,
         c='k',
         label=f'y = {alpha_m:.2f} + {beta_m:.2f} * x')

plt.xlabel('x')
plt.ylabel('y', rotation=0)

# v2 - plotting hpd
plt.plot(x,
         alpha_m + beta_m * x,
         c='k',
         label=f'y = {alpha_m:.2f} + {beta_m:.2f} * x')
sig = az.plot_hpd(x, trace_g['mu'], credible_interval=0.98, color='k')

plt.xlabel('x')
plt.ylabel('y', rotation=0)
plt.legend()

# v3 - plotting interval for future data
ppc = pm.sample_posterior_predictive(trace_g, samples=2000, model=model_g)

# way to get r2
az.r2_score(y, ppc['y_pred'])

# In[11]:

plt.plot(x, y, 'b.')
plt.plot(x,
コード例 #22
0
az.summary(trace_0, varnames)

# In[10]:

theta = trace_0['theta'].mean(axis=0)
idx = np.argsort(x_c)
plt.plot(x_c[idx], theta[idx], color='C2', lw=3)
plt.vlines(trace_0['bd'].mean(), 0, 1, color='k')
bd_hpd = az.hpd(trace_0['bd'])
plt.fill_betweenx([0, 1], bd_hpd[0], bd_hpd[1], color='k', alpha=0.5)

plt.scatter(x_c,
            np.random.normal(y_0, 0.02),
            marker='.',
            color=[f'C{x}' for x in y_0])
az.plot_hpd(x_c, trace_0['theta'], color='C2')

plt.xlabel(x_n)
plt.ylabel('theta', rotation=0)
# use original scale for xticks
locs, _ = plt.xticks()
plt.xticks(locs, np.round(locs + x_0.mean(), 1))
plt.savefig('B11197_04_04.png', dpi=300)

# # Multiple logistic regression

# In[11]:

df = iris.query("species == ('setosa', 'versicolor')")
y_1 = pd.Categorical(df['species']).codes
x_n = ['sepal_length', 'sepal_width']
コード例 #23
0
ファイル: mpl_plot_hpd.py プロジェクト: smit-s/arviz
"""
Plot HPD
========

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

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

x_data = np.random.normal(0, 1, 100)
y_data = 2 + x_data * 0.5
y_data_rep = np.random.normal(y_data, 0.5, (200, 100))
plt.plot(x_data, y_data, "C6")
az.plot_hpd(x_data, y_data_rep, color="k", plot_kwargs={"ls": "--"})

plt.show()
コード例 #24
0
Nsamp, Nbin = 1000, 30
log_pop_seq = np.linspace(6, 14, Nbin)
a_post = trace_13_7.get_values(varname="a", combine=True)[:, None]
bp_post = trace_13_7.get_values(varname="bp", combine=True)[:, None]
lambda_post = np.exp(a_post + bp_post * log_pop_seq)

_, axes = plt.subplots(1, 1, figsize=(5, 5))
cred_interval = 0.8

# display posterior predictions
axes.plot(log_pop_seq, np.median(lambda_post, axis=0), "--", color="k")

az.plot_hpd(
    log_pop_seq,
    lambda_post,
    credible_interval=cred_interval,
    color="k",
    fill_kwargs={"alpha": cred_interval * 0.5},
)

# plot raw data and labels
axes.scatter(dk["logpop"], dk["total_tools"], psize)
labels = dk["culture"].values
for i, itext in enumerate(labels):
    axes.text(dk["logpop"][i] + 0.1, dk["total_tools"][i] - 2.5, itext)

# overlay correlations
for i in range(10):
    for j in np.arange(i + 1, 10):
        axes.plot(
            [dk["logpop"][i], dk["logpop"][j]],
コード例 #25
0
# plot the non robust linear regression
plt.plot(x,
         alpha_c + beta_c * x,
         c="k",
         label="Non-robust regression",
         alpha=0.5)
# plot the data
plt.plot(x, y, 'C0o')
# get the mean of the intercept from the posterior
alpha_m = trace["alpha"].mean()
# get the mean of the coefficient from the posterior
beta_m = trace["beta"].mean()
# plot the robust linear regression
plt.plot(x, alpha_m + beta_m * x, c="k", label="Robust linear regression")
# plot the variety of predicted results
az.plot_hpd(x, ppc["obs"])
# set the last details of the graph
plt.xlabel("x")
plt.ylabel("y", rotation=0)
#plt.legend(loc=2)
plt.tight_layout()
plt.show()

# ----------------- analyse the posterior -------------------- #

with model_t:
    az.plot_trace(trace, var_names=["alpha", "beta", "sigma", "vu"])
    # get the summary
    log.info("the trace summary is: %s", az.summary(trace))
    # let's also run a posterior predictive check
    ppc = pm.sample_posterior_predictive(trace, samples=2000)