示例#1
0
def energyplot(energies,
               fill_color=("C0", "C1"),
               fill_alpha=(1, 0.5),
               fig=plt.gcf(),
               sp=GridSpec(1, 1)[:, :]):

    for i, energy in enumerate(energies):
        mean_energy, trans_energy = energy - energy.mean(), np.diff(energy)
        ax = fig.add_subplot(sp)
        pm.kdeplot(mean_energy,
                   label="Marginal Energy",
                   ax=ax,
                   shade=fill_alpha[0],
                   kwargs_shade={"color": fill_color[0]})
        pm.kdeplot(trans_energy,
                   label="Energy Transition",
                   ax=ax,
                   shade=fill_alpha[1],
                   kwargs_shade={"color": fill_color[1]})

        ax.plot([],
                label="chain {:>2} BFMI = {:.2f}".format(
                    i, pm.bfmi({"energy": energy})),
                alpha=0)
    ax.legend()

    ax.set_xticks([])
    ax.set_yticks([])
示例#2
0
    group1 = pm.StudentT('drug', nu=ν, mu=group1_mean, lam=λ1, observed=y1)
    group2 = pm.StudentT('placebo', nu=ν, mu=group2_mean, lam=λ2, observed=y2)

    diff_of_means = pm.Deterministic('difference of means',
                                     group1_mean - group2_mean)
    diff_of_stds = pm.Deterministic('difference of stds',
                                    group1_std - group2_std)
    effect_size = pm.Deterministic(
        'effect size', diff_of_means / np.sqrt(
            (group1_std**2 + group2_std**2) / 2))

    # RUN
    #trace = pm.sample(2000, cores=2)  #  Nota Bene: https://github.com/pymc-devs/pymc3/issues/3388
    trace = pm.sample(1000, tune=1000, cores=1)

pm.kdeplot(np.random.exponential(30, size=10000), shade=0.5)

pm.plot_posterior(trace,
                  varnames=[
                      'group1_mean', 'group2_mean', 'group1_std', 'group2_std',
                      'ν_minus_one'
                  ],
                  color='#87ceeb')

pm.plot_posterior(
    trace,
    varnames=['difference of means', 'difference of stds', 'effect size'],
    ref_val=0,
    color='#87ceeb')

pm.forestplot(trace, varnames=['group1_mean', 'group2_mean'])
示例#3
0
with pm.Model() as model:
    group1_mean = pm.Normal('group1_mean', mu=μ_m, sd=μ_s)
    group2_mean = pm.Normal('group2_mean', mu=μ_m, sd=μ_s)

σ_low = 1
σ_high = 10

with model:
    group1_std = pm.Uniform('group1_std', lower=σ_low, upper=σ_high)
    group2_std = pm.Uniform('group2_std', lower=σ_low, upper=σ_high)

with model:
    ν = pm.Exponential('ν_minus_one', 1 / 29.) + 1

pm.kdeplot(np.random.exponential(30, size=10000), fill_kwargs={'alpha': 0.5})

with model:
    λ1 = group1_std**-2
    λ2 = group2_std**-2

    group1 = pm.StudentT('drug', nu=ν, mu=group1_mean, lam=λ1, observed=y1)
    group2 = pm.StudentT('placebo', nu=ν, mu=group2_mean, lam=λ2, observed=y2)

with model:
    diff_of_means = pm.Deterministic('difference of means',
                                     group1_mean - group2_mean)
    diff_of_stds = pm.Deterministic('difference of stds',
                                    group1_std - group2_std)
    effect_size = pm.Deterministic(
        'effect size', diff_of_means / np.sqrt(
示例#4
0
type(Howell1['height'])
type(Howell1[['height']])
d2 = Howell1[['height']][Howell1['age'] >= 18]
type(d2)
d2.describe()

# plot observed data
x_plot = np.linspace(136, 180, len(d2))[:, np.newaxis]
kde = KernelDensity(kernel='gaussian', bandwidth=2)
kde.fit(d2)
y = np.exp(kde.score_samples(x_plot))
plt.plot(x_plot, y)
plt.show()

pm.kdeplot(d2)
plt.xlabel('height')
plt.ylabel('density')
plt.title('Prior')
plt.show()

# code chunk 4.13 (set up prior)
sample_mu = norm.rvs(loc=178, scale=20, size=1000)
sample_sigma = uniform.rvs(0, 50, 1000)
prior_h = norm.rvs(sample_mu, sample_sigma, 1000)
sns.set_theme(style='darkgrid')
ax = sns.kdeplot(prior_h, bw=2)
ax.set(xlabel='height', title='Prior')
plt.show()

# code chunk 4.14 (grid estimation)