Esempio n. 1
0
## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)

model_idx_sample = trace['model_index'][burnin::thin]
pM1 = sum(model_idx_sample == 0) / len(model_idx_sample)
pM2 = 1 - pM1

nu_sample_M1 = trace['nu'][burnin::thin][model_idx_sample == 0]
eta_sample_M2 = trace['eta'][burnin::thin][model_idx_sample == 1]

plt.figure()
plt.subplot(2, 1, 1)
plot_post(nu_sample_M1,
          xlab=r'$\nu$',
          show_mode=False,
          labelsize=9,
          framealpha=0.5)
plt.xlabel(r'$\nu$')
plt.ylabel('frequency')
plt.title(r'p($\nu$|D,M2), with p(M2|D)=%.3f' % pM1, fontsize=14)
plt.xlim(-8, 8)

plt.subplot(2, 1, 2)
plot_post(eta_sample_M2,
          xlab=r'$\eta$',
          show_mode=False,
          labelsize=9,
          framealpha=0.5)
plt.xlabel(r'$\eta$')
plt.ylabel('frequency')
Esempio n. 2
0
#pm.traceplot(trace[burnin::thin])
pm.traceplot(trace)

# Create arrays with the posterior sample
mu1_sample = trace['mu'][:, 0][burnin::thin]
mu2_sample = trace['mu'][:, 1][burnin::thin]
mu3_sample = trace['mu'][:, 2][burnin::thin]
mu4_sample = trace['mu'][:, 3][burnin::thin]

fig = plt.figure(figsize=(15, 6))

# Plot differences among filtrations experiments
plt.subplot(1, 3, 1)
plot_post((mu1_sample - mu2_sample),
          xlab=r'$\mu1-\mu2$',
          show_mode=False,
          comp_val=0,
          framealpha=0.5)

# Plot differences among condensation experiments
plt.subplot(1, 3, 2)
plot_post((mu3_sample - mu4_sample),
          xlab=r'$\mu3-\mu4$',
          show_mode=False,
          comp_val=0,
          framealpha=0.5)

# Plot differences between filtration and condensation experiments
plt.subplot(1, 3, 3)
a = (mu1_sample + mu2_sample) / 2 - (mu3_sample + mu4_sample) / 2
plot_post(a,
for x_idx in range(len(x_post_pred)):
    y_HDI_lim[x_idx] = hpd(y_post_pred[x_idx])


# Display believable beta0 and b1 values
plt.figure()
thin_idx = 5
plt.plot(b1[::thin_idx], b0[::thin_idx], '.')
plt.ylabel("Intercept")
plt.xlabel("Slope")
plt.savefig('Figure_16.x0.png')

# Display the posterior of the b1:
plt.figure()
plot_post(b1, xlab=r'Slope ($\Delta$ tar  / $\Delta$ weight)', comp_val=0.0,
            show_mode=False, bins=30)
plt.title('Mean tdf = %.2f' % tdf_m)
plt.savefig('Figure_16.8a.png')

# Display data with believable regression lines and posterior predictions.
plt.figure()
plt.plot(x, y, 'k.')
plt.title('Data with credible regression lines')
plt.xlabel('tar')
plt.ylabel('weight')
plt.xlim(x_lim)
plt.ylim(y_lim)
# Superimpose a smattering of believable regression lines:
for i in range(0, len(b0), 5):
    plt.plot(x, b0[i] + b1[i]*x  , c='k', alpha=0.05 )
plt.savefig('Figure_16.8x1.png')
## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)

model_idx_sample = trace['model_index'][burnin::thin]
pM1 = sum(model_idx_sample == 1) / len(model_idx_sample)
pM2 = 1 - pM1

plt.figure(figsize=(15, 15))
plt.subplot2grid((3,3), (0,0), colspan=3)
plt.plot(model_idx_sample, label='p(DiffMu|D) = %.3f ; p(SameMu|D) = %.3f' % (pM1, pM2));
plt.xlabel('Step in Markov Chain')
plt.legend(loc='upper right', framealpha=0.75)

count = 0
position = [(1,0), (1,1), (1,2), (2,0), (2,1), (2,2)]
for i in range(0, 4):
    mui_sample = trace['mu1'][:,i][burnin::thin][model_idx_sample == 0]
    for j in range(i+1, 4):
        muj_sample = trace['mu1'][:,j][burnin::thin][model_idx_sample == 0]
        plt.subplot2grid((3,3), position[count])
        plot_post(mui_sample-muj_sample, xlab=r'$\mu_%s - \mu_%s$' % (i+1, j+1), show_mode=False, comp_val=0, framealpha=0.5)
        plt.xlim(-0.3, 0.3)
        count += 1


plt.tight_layout()
plt.savefig('Figure_12.5.png')
plt.show()

plt.plot(theta1_sample[:-500], theta2_sample[:-500], marker='o')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel(r'$\theta1$')
plt.ylabel(r'$\theta2$')

# Display means in plot.
plt.plot(0, label='M = %.3f, %.3f' % (np.mean(theta1_sample), np.mean(theta2_sample)), alpha=0.0)

plt.legend(loc='upper left')
plt.savefig('Figure_8.6.png')

# Plot a histogram of the posterior differences of theta values.
plt.figure()
theta_diff = theta1_sample - theta2_sample
plot_post(theta_diff, xlab=r'$\theta_1 - \theta_2$', comp_val=0.0, show_mode=False, bins=30)
plt.savefig('Figure_8.8.png')


# For Exercise 8.5:
# Posterior prediction. For each step in the chain, use the posterior thetas 
# to flip the coins.
chain_len = len(theta1_sample)
# Create matrix to hold results of simulated flips:
y_pred = np.zeros((2, chain_len))
for step_idx in range(chain_len):  # step through the chain
    # flip the first coin:
    p_head1 = theta1_sample[step_idx]
    y_pred[0, step_idx] = np.random.choice([0,1], p=[1-p_head1, p_head1])
    # flip the second coin:
    p_head2 = theta2_sample[step_idx]
Esempio n. 6
0
for x_idx in range(len(x_post_pred)):
    y_HDI_lim[x_idx] = hpd(y_post_pred[x_idx])


# Display believable beta0 and b1 values
plt.figure()
thin_idx = 5
plt.plot(b1[::thin_idx], b0[::thin_idx], '.')
plt.ylabel("Intercept")
plt.xlabel("Slope")
plt.savefig('Figure_16.x0.png')

# Display the posterior of the b1:
plt.figure()
plot_post(b1, xlab=r'Slope ($\Delta$ tar  / $\Delta$ weight)', comp_val=0.0,
            show_mode=False, bins=30)
plt.title('Mean tdf = %.2f' % tdf_m)
plt.savefig('Figure_16.8b.png')

# Display data with believable regression lines and posterior predictions.
plt.figure()
plt.plot(x, y, 'k.')
plt.title('Data with credible regression lines')
plt.xlabel('weight')
plt.ylabel('tar')
plt.xlim(x_lim)
plt.ylim(y_lim)
# Superimpose a smattering of believable regression lines:
for i in range(0, len(b0), 5):
    plt.plot(x, b0[i] + b1[i]*x  , c='k', alpha=0.05 )
plt.savefig('Figure_16.8x1.png')
## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin::thin])
pm.traceplot(trace)


# Create arrays with the posterior sample
mu1_sample = trace['mu'][:,0][burnin::thin]
mu2_sample = trace['mu'][:,1][burnin::thin]
mu3_sample = trace['mu'][:,2][burnin::thin]
mu4_sample = trace['mu'][:,3][burnin::thin]


fig = plt.figure(figsize=(15, 6))

# Plot differences among filtrations experiments
plt.subplot(1, 3, 1)
plot_post((mu1_sample-mu2_sample), xlab=r'$\mu1-\mu2$', show_mode=False, comp_val=0, framealpha=0.5)

# Plot differences among condensation experiments
plt.subplot(1, 3, 2)
plot_post((mu3_sample-mu4_sample), xlab=r'$\mu3-\mu4$', show_mode=False, comp_val=0, framealpha=0.5)

# Plot differences between filtration and condensation experiments
plt.subplot(1, 3, 3)
a = (mu1_sample+mu2_sample)/2 - (mu3_sample+mu4_sample)/2
plot_post(a, xlab=r'$(\mu1+\mu2)/2 - (\mu3+\mu4)/2$', show_mode=False, comp_val=0, framealpha=0.5)

plt.tight_layout()
plt.savefig('Figure_9.16.png')
plt.show()
theta3_sample = trace['theta'][:,2][burnin::thin]
mu_sample = trace['mu'][burnin::thin]
kappa_sample = trace['kappa'][burnin::thin]

fig = plt.figure(figsize=(12,12))

# Scatter plot hyper-parameters
plt.subplot(4, 3, 1)
plt.scatter(mu_sample, kappa_sample, marker='o')
plt.xlim(0,1)
plt.xlabel(r'$\mu$')
plt.ylabel(r'$\kappa$')

# Plot mu histogram
plt.subplot(4, 3, 2)
plot_post(mu_sample, xlab=r'$\mu$', show_mode=False, labelsize=9, framealpha=0.5)
plt.xlim(0,1)

# Plot kappa histogram
plt.subplot(4, 3, 3)
plot_post(kappa_sample, xlab=r'$\kappa$', show_mode=False, labelsize=9, framealpha=0.5)

# Plot theta 1
plt.subplot(4, 3, 4)
plot_post(theta1_sample, xlab=r'$\theta1$', show_mode=False, labelsize=9, framealpha=0.5)
plt.xlim(0,1)

# Scatter theta 1 vs mu
plt.subplot(4, 3, 5)
plt.scatter(theta1_sample, mu_sample, marker='o')
plt.xlim(0,1)
plt.plot(theta1_sample[:-500], theta2_sample[:-500], marker='o')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel(r'$\theta1$')
plt.ylabel(r'$\theta2$')

# Display means in plot.
plt.plot(0, label='M = %.3f, %.3f' % (np.mean(theta1_sample), np.mean(theta2_sample)), alpha=0.0)

plt.legend(loc='upper left')
plt.savefig('Figure_8.6.png')

# Plot a histogram of the posterior differences of theta values.
plt.figure()
theta_diff = theta1_sample - theta2_sample
plot_post(theta_diff, xlab=r'\theta1-theta2', comp_val=0.0, show_mode=False, bins=30)
plt.savefig('Figure_8.8.png')


# For Exercise 8.5:
# Posterior prediction. For each step in the chain, use the posterior thetas 
# to flip the coins.
chain_len = len(theta1_sample)
# Create matrix to hold results of simulated flips:
y_pred = np.zeros((2, chain_len))
for step_idx in range(chain_len):  # step through the chain
    # flip the first coin:
    p_head1 = theta1_sample[step_idx]
    y_pred[0, step_idx] = np.random.choice([0,1], p=[1-p_head1, p_head1])
    # flip the second coin:
    p_head2 = theta2_sample[step_idx]
Esempio n. 10
0
# Create the model, generate initialization values and sample its posterior.

model = pymc.Model([like, mu, tau])
map_ = pymc.MAP(model)
map_.fit()
mcmc = pymc.MCMC(model)
mcmc.sample(iter=60000, burn=40000, thin=2)

# Sample the posterior for the parameter estimates.

mu_sample = mcmc.trace('mu')[:]
tau_sample = mcmc.trace('tau')[:]

# Keeping the same idea as the book: convert the posterior samples to SD.

sigma_sample = 1 / np.sqrt(tau_sample)

# Plot the results.

plot.figure(figsize=(8.0, 8.0))

plot.subplot(211)
plot_post(mu_sample, title=r'$\mu$ posterior distribution')

plot.subplot(212)
plot_post(sigma_sample, title=r'$\sigma$ posterior distribution')

plot.subplots_adjust(wspace=0.2, hspace=0.2)
plot.show()
# Extract values of 'a'
a0_sample = trace["a0"][burnin::thin]
b1_sample = trace["b1"][burnin::thin]
b2_sample = trace["b2"][burnin::thin]
b1b2_sample = trace["b1b2"][burnin::thin]

b0_sample = a0_sample * np.std(y) + np.mean(y)
b1_sample = b1_sample * np.std(y)
b2_sample = b2_sample * np.std(y)
b1b2_sample = b1b2_sample * np.std(y)


plt.figure(figsize=(25, 20))
plt.subplot(451)
plot_post(b0_sample, xlab=r"$\beta0$", show_mode=False, framealpha=0.5, bins=50, title="Baseline")
plt.xlim(b0_sample.min(), b0_sample.max())

count = 2
for i in range(len(b1_sample[0])):
    plt.subplot(4, 5, count)
    plot_post(
        b1_sample[:, i],
        xlab=r"$\beta1_{%s}$" % i,
        show_mode=False,
        framealpha=0.5,
        bins=50,
        title="x1: %s" % x1names[i],
    )
    count += 1
z1_sample = mcmc.trace('b1')[:]
ztau_sample = mcmc.trace('tau')[:]
tdf_sample = mcmc.trace('tdf')[:]

# Convert the data back to scale.

b0_sample = convert_intercept(x, y, z0_sample, z1_sample)
b1_sample = convert_slope(x, y, z1_sample)
sigma_sample = convert_tau_sigma(y, ztau_sample)

# Plot the results

plot.figure(figsize=(8.0, 8.0))

plot.subplot(221)
plot_post(b0_sample, title=r'$\beta_0$ posterior')

plot.subplot(222)
plot_post(b1_sample, title=r'$\beta_1$ posterior')

plot.subplot(223)
plot_post(sigma_sample, title=r'$\sigma$ posterior')

plot.subplot(224)
plot_post(tdf_sample, title=r'tDF posterior')

plot.subplots_adjust(wspace=0.2, hspace=0.2)

# Plot the data with some credible regression lines.

plot.figure(figsize=(8.0, 8.0))
Esempio n. 13
0
# Display means in plot.
plt.plot(0,
         label='M = %.3f, %.3f' %
         (np.mean(theta1_sample), np.mean(theta2_sample)),
         alpha=0.0)

plt.legend(loc='upper left')
plt.savefig('Figure_8.6.png')

# Plot a histogram of the posterior differences of theta values.
plt.figure()
theta_diff = theta1_sample - theta2_sample
plot_post(theta_diff,
          xlab=r'\theta1-theta2',
          comp_val=0.0,
          show_mode=False,
          bins=30)
plt.savefig('Figure_8.8.png')

# For Exercise 8.5:
# Posterior prediction. For each step in the chain, use the posterior thetas
# to flip the coins.
chain_len = len(theta1_sample)
# Create matrix to hold results of simulated flips:
y_pred = np.zeros((2, chain_len))
for step_idx in range(chain_len):  # step through the chain
    # flip the first coin:
    p_head1 = theta1_sample[step_idx]
    y_pred[0, step_idx] = np.random.choice([0, 1], p=[1 - p_head1, p_head1])
    # flip the second coin:
plt.plot(z1[::thin_idx], z0[::thin_idx], 'b.', alpha=0.7)
plt.ylabel('Standardized Intercept')
plt.xlabel('Standardized Slope')
plt.subplot(1, 2, 2)
plt.plot(b1[::thin_idx], b0[::thin_idx], 'b.', alpha=0.7)
plt.ylabel('Intercept (ht when wt=0)')
plt.xlabel('Slope (pounds per inch)')
plt.tight_layout()
plt.savefig('Figure_16.4.png')

# Display the posterior of the b1:
plt.figure(figsize=(8, 5))
plt.subplot(1, 2, 1)
plot_post(z1,
          xlab='Standardized slope',
          comp_val=0.0,
          bins=30,
          show_mode=False)
plt.subplot(1, 2, 2)
plot_post(b1,
          xlab='Slope (pounds per inch)',
          comp_val=0.0,
          bins=30,
          show_mode=False)
plt.tight_layout()
plt.savefig('Figure_16.5.png')

# Display data with believable regression lines and posterior predictions.
plt.figure()
# Plot data values:
x_rang = np.max(x) - np.min(x)
# Convert baseline to the original scale
m_sample = a0_sample.repeat(NxLvl).reshape(len(a0_sample), NxLvl) + a_sample
b0_sample = m_sample.mean(axis=1)
b0_sample = b0_sample * np.std(y) + np.mean(y)
# Convert baseline to the original scale
n_sample = b0_sample.repeat(NxLvl).reshape(len(b0_sample), NxLvl)
b_sample = (m_sample - n_sample)
b_sample = b_sample * np.std(y)



plt.figure(figsize=(20, 4))
for i in range(5):
    plt.subplot(1, 5, i+1)
    plot_post(b_sample[:,i], xlab=r'$\beta1_{%s}$' % i,
              show_mode=False, framealpha=0.5,
              bins=50, title='x:%s' % i)
plt.tight_layout()
plt.savefig('Figure_18.2a.png')


nContrasts = len(contrast_dict)
if nContrasts > 0:
    plt.figure(figsize=(20, 8))
    count = 0
    for key, value in contrast_dict.items():
        contrast = np.dot(b_sample, value)
        plt.subplot(2, 4, count)
        plot_post(contrast, title='Contrast %s' % key, comp_val=0.0, 
                  show_mode=False, framealpha=0.5, 
                  bins=50)
map_ = pymc.MAP(model)
map_.fit()

mcmc = pymc.MCMC(model)
mcmc.sample(iter=60000, burn=10000, thin=2)

# Extracting the parameter samples.

mu_sample = mcmc.trace('mu')[:]
kappa_sample = mcmc.trace('kappa')[:]
theta_sample = mcmc.trace('theta')[:]

# And plot them.

plot.figure(figsize=(8.0, 8.0))

plot.subplot(221)
plot_post(mu_sample, comp=0.5, title=r'$\mu$ posterior distribution')

plot.subplot(222)
plot_post(kappa_sample, title=r'$\kappa$ posterior distribution')

plot.subplot(223)
plot_post(theta_sample[:, 0], title=r'$\theta_1$ posterior distribution')

plot.subplot(224)
plot_post(theta_sample[:, 27], title=r'$\theta_{28}$ posterior distribution')

plot.subplots_adjust(wspace=0.2, hspace=0.2)
plot.show()
## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)


model_idx_sample = trace['model_index'][burnin::thin]
pM1 = sum(model_idx_sample == 0) / len(model_idx_sample)
pM2 = 1 - pM1


nu_sample_M1 = trace['nu'][burnin::thin][model_idx_sample == 0]
eta_sample_M2 = trace['eta'][burnin::thin][model_idx_sample == 1]

plt.figure()
plt.subplot(2, 1, 1)
plot_post(nu_sample_M1, xlab=r'$\nu$', show_mode=False, labelsize=9, framealpha=0.5)
plt.xlabel(r'$\nu$')
plt.ylabel('frequency')
plt.title(r'p($\nu$|D,M2), with p(M2|D)=%.3f' % pM1, fontsize=14)
plt.xlim(-8, 8)

plt.subplot(2, 1, 2)
plot_post(eta_sample_M2, xlab=r'$\eta$', show_mode=False, labelsize=9, framealpha=0.5)
plt.xlabel(r'$\eta$')
plt.ylabel('frequency')
plt.title(r'p($\eta$|D,M2), with p(M2|D)=%.3f' % pM2, fontsize=14)
plt.xlim(0, 8)
plt.savefig('figure_ex_10.2_a.png')
plt.show()
Esempio n. 18
0
np.random.seed(1)
post = stats.beta.rvs(5, 11, size=1000)
naive_hpd(post)
plt.xlim(0, 1)

save = input("Save the figure? y/n")
if save == y:
    plt.savefig(NAIVE_HPD, dpi=300, figsize=(5.5, 5.5))

# +
np.random.seed(1)
gauss_a = stats.norm.rvs(loc=4, scale=0.9, size=3000)
gauss_b = stats.norm.rvs(loc=-2, scale=1, size=2000)
mix_norm = np.concatenate((gauss_a, gauss_b))

naive_hpd(mix_norm)
print('Wrong HPD =( ')
# -

# Multiple peak
MULTIPLE_PEAK_HPD = '../images/Multiple_peak_hpd.png'
from plot_post import plot_post

plot_post(mix_norm, roundto=2, alpha=0.05)
plt.legend(loc=0, fontsize=10)
plt.xlabel(r"$\theta$", fontsize=14)
save = input("Save the figure? y/n")
if save == y:
    plt.savefig(MULTIPLE_PEAK_HPD, dpi=300, figsize=(12, 8))
# Display means in plot.
plt.plot(0,
         label='M = %.3f, %.3f' %
         (np.mean(theta1_sample), np.mean(theta2_sample)),
         alpha=0.0)

plt.legend(loc='upper left')
plt.savefig('Figure_8.6.png')

# Plot a histogram of the posterior differences of theta values.
plt.figure()
theta_diff = theta1_sample - theta2_sample
plot_post(theta_diff,
          xlab=r'$\theta_1 - \theta_2$',
          comp_val=0.0,
          show_mode=False,
          bins=30)
plt.savefig('Figure_8.8.png')

# For Exercise 8.5:
# Posterior prediction. For each step in the chain, use the posterior thetas
# to flip the coins.
chain_len = len(theta1_sample)
# Create matrix to hold results of simulated flips:
y_pred = np.zeros((2, chain_len))
for step_idx in range(chain_len):  # step through the chain
    # flip the first coin:
    p_head1 = theta1_sample[step_idx]
    y_pred[0, step_idx] = np.random.choice([0, 1], p=[1 - p_head1, p_head1])
    # flip the second coin:
## Check for mixing and autocorrelation
#pm.autocorrplot(trace[burnin::thin], vars =[mu, tau])
#pm.autocorrplot(trace, vars =[mu, tau])

## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)

mu_sample = trace['mu'][burnin::thin]
tau_sample = trace['tau'][burnin::thin]
sigma_sample = 1 / np.sqrt(tau_sample) # Convert precision to std


plt.figure(figsize=(10, 6))
plt.subplot(1, 2, 1)
plot_post(mu_sample, xlab='mu', bins=30, title='Posterior', show_mode=False)
plt.xlim(98, 102)

plt.subplot(1, 2, 2)

mu_mean = np.mean(mu_sample)
sigma_mean = np.mean(sigma_sample)

plt.scatter(mu_sample, sigma_sample , c='gray')
plt.plot(mu_mean, sigma_mean, 'r*',
        label=r'$\mu$ = %.1f, $\sigma$ = %.1f' % (mu_mean, sigma_mean))
plt.xlabel('mu')
plt.ylabel('sigma')
plt.title('Posterior')
plt.legend(loc=0)
plt.savefig('figure_15.3.png')
a_sample = trace['a'][burnin::thin]
# Convert baseline to the original scale
m_sample = a0_sample.repeat(NxLvl).reshape(len(a0_sample), NxLvl) + a_sample
b0_sample = m_sample.mean(axis=1)
b0_sample = b0_sample * np.std(y) + np.mean(y)
# Convert baseline to the original scale
n_sample = b0_sample.repeat(NxLvl).reshape(len(b0_sample), NxLvl)
b_sample = (m_sample - n_sample)
b_sample = b_sample * np.std(y)

plt.figure(figsize=(20, 4))
for i in range(5):
    plt.subplot(1, 5, i + 1)
    plot_post(b_sample[:, i],
              xlab=r'$\beta1_{%s}$' % i,
              show_mode=False,
              framealpha=0.5,
              bins=50,
              title='x:%s' % i)
plt.tight_layout()
plt.savefig('Figure_18.2a.png')

nContrasts = len(contrast_dict)
if nContrasts > 0:
    plt.figure(figsize=(20, 8))
    count = 0
    for key, value in contrast_dict.items():
        contrast = np.dot(b_sample, value)
        plt.subplot(2, 4, count)
        plot_post(contrast,
                  title='Contrast %s' % key,
                  comp_val=0.0,
Esempio n. 22
0
## Check for mixing and autocorrelation
#pm.autocorrplot(trace[burnin::thin], vars =[mu, tau])
#pm.autocorrplot(trace, vars =[mu, tau])

## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)

mu_sample = trace['mu'][burnin::thin]
tau_sample = trace['tau'][burnin::thin]
sigma_sample = 1 / np.sqrt(tau_sample)  # Convert precision to std

plt.figure(figsize=(10, 6))
plt.subplot(1, 2, 1)
plot_post(mu_sample, xlab='mu', bins=30, title='Posterior', show_mode=False)
plt.xlim(98, 102)

plt.subplot(1, 2, 2)

mu_mean = np.mean(mu_sample)
sigma_mean = np.mean(sigma_sample)

plt.scatter(mu_sample, sigma_sample, c='gray')
plt.plot(mu_mean,
         sigma_mean,
         'r*',
         label=r'$\mu$ = %.1f, $\sigma$ = %.1f' % (mu_mean, sigma_mean))
plt.xlabel('mu')
plt.ylabel('sigma')
plt.title('Posterior')
Esempio n. 23
0
mcmc = pymc.MCMC(model)
mcmc.sample(40000, 10000, 1)

# Use PyMC built-in plot function to show graphs of the samples.

# pymc.Matplot.plot(mcmc)
# plot.show()

# Let's try plotting using Matplotlib's 'pyplot'.
# First, we extract the traces for the parameters of interest.

theta1_samples = mcmc.trace('theta1')[:]
theta2_samples = mcmc.trace('theta2')[:]
theta_diff = theta2_samples - theta1_samples

# Then, we plot a histogram of their individual sample values.

plot.figure(figsize=(8.0, 10))

plot.subplot(311)
plot_post(theta1_samples, title=r'Posterior of $\theta_1$')

plot.subplot(312)
plot_post(theta2_samples, title=r'Posterior of $\theta_2$')

plot.subplot(313)
plot_post(theta_diff, title=r'Posterior of $\Delta\theta$', comp=0.0)

plot.subplots_adjust(hspace=0.5)
plot.show()
model_idx_sample = trace['model_index'][burnin::thin]
pM1 = sum(model_idx_sample == 1) / len(model_idx_sample)
pM2 = 1 - pM1

plt.figure(figsize=(15, 15))
plt.subplot2grid((3, 3), (0, 0), colspan=3)
plt.plot(model_idx_sample,
         label='p(DiffMu|D) = %.3f ; p(SameMu|D) = %.3f' % (pM1, pM2))
plt.xlabel('Step in Markov Chain')
plt.legend(loc='upper right', framealpha=0.75)

count = 0
position = [(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
for i in range(0, 4):
    mui_sample = trace['mu1'][:, i][burnin::thin][model_idx_sample == 0]
    for j in range(i + 1, 4):
        muj_sample = trace['mu1'][:, j][burnin::thin][model_idx_sample == 0]
        plt.subplot2grid((3, 3), position[count])
        plot_post(mui_sample - muj_sample,
                  xlab=r'$\mu_%s - \mu_%s$' % (i + 1, j + 1),
                  show_mode=False,
                  comp_val=0,
                  framealpha=0.5)
        plt.xlim(-0.3, 0.3)
        count += 1

plt.tight_layout()
plt.savefig('Figure_12.5.png')
plt.show()
plt.subplot(1, 2, 1)
thin_idx = 50
plt.plot(z1[::thin_idx], z0[::thin_idx], 'b.', alpha=0.7)
plt.ylabel('Standardized Intercept')
plt.xlabel('Standardized Slope')
plt.subplot(1, 2, 2)
plt.plot(b1[::thin_idx], b0[::thin_idx], 'b.', alpha=0.7)
plt.ylabel('Intercept (ht when wt=0)')
plt.xlabel('Slope (pounds per inch)')
plt.tight_layout()
plt.savefig('Figure_16.4.png')

# Display the posterior of the b1:
plt.figure(figsize=(8, 5))
plt.subplot(1, 2, 1)
plot_post(z1, xlab='Standardized slope', 
          comp_val=0.0, bins=30, show_mode=False)
plt.subplot(1, 2, 2)
plot_post(b1, xlab='Slope (pounds per inch)', 
          comp_val=0.0, bins=30, show_mode=False)
plt.tight_layout()
plt.savefig('Figure_16.5.png')

# Display data with believable regression lines and posterior predictions.
plt.figure()
# Plot data values:
x_rang = np.max(x) - np.min(x)
y_rang = np.max(y) - np.min(y)
lim_mult = 0.25
x_lim = [np.min(x)-lim_mult*x_rang, np.max(x)+lim_mult*x_rang]
y_lim = [np.min(y)-lim_mult*y_rang, np.max(y)+lim_mult*y_rang]
plt.plot(x, y, 'k.')
Esempio n. 26
0
#pm.summary(trace[burnin::thin])
#pm.summary(trace)

## Check for mixing and autocorrelation
#pm.autocorrplot(trace[burnin::thin], vars =[mu, tau])
#pm.autocorrplot(trace, vars =[mu, tau])

## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)

## Extract chains
muG_sample = trace['muG'][burnin::thin]
tauG_sample = trace['tauG'][burnin::thin]
m_sample = trace['m'][burnin::thin]
d_sample = trace['d'][burnin::thin]

# Plot the hyperdistributions:
plt.figure(figsize=(20, 5))
plt.subplot(1, 4, 1)
plot_post(muG_sample, xlab=r'$\mu_g$', bins=30, show_mode=False)
plt.subplot(1, 4, 2)
plot_post(tauG_sample, xlab=r'$\tau_g$', bins=30, show_mode=False)
plt.subplot(1, 4, 3)
plot_post(m_sample, xlab='m', bins=30, show_mode=False)
plt.subplot(1, 4, 4)
plot_post(d_sample, xlab='d', bins=30, show_mode=False)

plt.savefig('Figure_15.9.png')
plt.show()
Esempio n. 27
0
z1_sample = mcmc.trace('b1')[:]
ztau_sample = mcmc.trace('tau')[:]
tdf_sample = mcmc.trace('tdf')[:]

# Convert the data back to scale.

b0_sample = convert_intercept(x, y, z0_sample, z1_sample)
b1_sample = convert_slope(x, y, z1_sample)
sigma_sample = convert_tau_sigma(y, ztau_sample)

# Plot the results

plot.figure(figsize=(8.0, 8.0))

plot.subplot(221)
plot_post(b0_sample, title=r'$\beta_0$ posterior')

plot.subplot(222)
plot_post(b1_sample, title=r'$\beta_1$ posterior')

plot.subplot(223)
plot_post(sigma_sample, title=r'$\sigma$ posterior')

plot.subplot(224)
plot_post(tdf_sample, title=r'tDF posterior')

plot.subplots_adjust(wspace=0.2, hspace=0.2)

# Plot the data with some credible regression lines.

plot.figure(figsize=(8.0, 8.0))
Esempio n. 28
0
columns = ['Sigma y', 'Intercept']
[columns.append('Slope_%s' % i) for i in predictorNames[:n_predictors]]
traces = np.array([Sigma_samp, b0_samp, b_samp[:,0], b_samp[:,1]]).T
df = pd.DataFrame(traces, columns=columns)
sns.set_style('dark')
g = sns.PairGrid(df)
g.map(plt.scatter)
plt.savefig('Figure_17.5b.png')

## Display the posterior:
sns.set_style('darkgrid')

plt.figure(figsize=(16,4))
plt.subplot(1, n_predictors+2, 1)
plot_post(Sigma_samp, xlab=r'$\sigma y$', show_mode=False, framealpha=0.5)
plt.subplot(1, n_predictors+2, 2)
plot_post(b0_samp, xlab='Intercept', show_mode=False, framealpha=0.5)

for i in range(0, n_predictors):
    plt.subplot(1, n_predictors+2, 3+i)
    plot_post(b_samp[:,i], xlab='Slope_%s' % predictorNames[i],
              show_mode=False, framealpha=0.5, comp_val=0)
plt.tight_layout()
plt.savefig('Figure_17.5a.png')


# Posterior prediction:
# Define matrix for recording posterior predicted y values for each xPostPred.
# One row per xPostPred value, with each row holding random predicted y values.
y_post_pred = np.zeros((len(x), chain_length))
#pm.summary(trace)

## Check for mixing and autocorrelation
#pm.autocorrplot(trace[burnin::thin], vars =[mu, tau])
#pm.autocorrplot(trace, vars =[mu, tau])

## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)


## Extract chains
muG_sample = trace['muG'][burnin::thin]
tauG_sample = trace['tauG'][burnin::thin]
m_sample = trace['m'][burnin::thin]
d_sample = trace['d'][burnin::thin]

# Plot the hyperdistributions:
plt.figure(figsize=(20, 5))
plt.subplot(1, 4, 1)
plot_post(muG_sample, xlab=r'$\mu_g$', bins=30, show_mode=False)
plt.subplot(1, 4, 2)
plot_post(tauG_sample, xlab=r'$\tau_g$', bins=30, show_mode=False)
plt.subplot(1, 4, 3)
plot_post(m_sample, xlab='m', bins=30, show_mode=False)
plt.subplot(1, 4, 4)
plot_post(d_sample, xlab='d', bins=30, show_mode=False)

plt.savefig('Figure_15.9.png')
plt.show()
a0_sample = trace['a0'][burnin::thin]
b1_sample = trace['b1'][burnin::thin]
b2_sample = trace['b2'][burnin::thin]
b1b2_sample = trace['b1b2'][burnin::thin]

b0_sample = a0_sample * np.std(y) + np.mean(y)
b1_sample = b1_sample * np.std(y)
b2_sample = b2_sample * np.std(y)
b1b2_sample = b1b2_sample * np.std(y)



plt.figure(figsize=(25,20))
plt.subplot(451)
plot_post(b0_sample, xlab=r'$\beta0$',
              show_mode=False, framealpha=0.5,
              bins=50, title='Baseline')
plt.xlim(b0_sample.min(), b0_sample.max());

count = 2
for i in range(len(b1_sample[0])):
    plt.subplot(4, 5, count)
    plot_post(b1_sample[:,i], xlab=r'$\beta1_{%s}$' % i,
              show_mode=False, framealpha=0.5,
              bins=50, title='x1: %s' % x1names[i])
    count += 1

for i in range(len(b2_sample[0])):
    plt.subplot(4, 5, count)
    plot_post(b2_sample[:,i], xlab=r'$\beta2_{%s}$' % i,
              show_mode=False, framealpha=0.5,
## Plot KDE and sampled values for each parameter.
pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)

# Create arrays with the posterior sample
theta1_sample = trace['theta'][:,0][burnin::thin]
theta28_sample = trace['theta'][:,27][burnin::thin]
mu_sample = trace['mu'][burnin::thin]
kappa_sample = trace['kappa'][burnin::thin]

fig = plt.figure(figsize=(12,12))

# Plot mu histogram
plt.subplot(2, 2, 1)
plot_post(mu_sample, xlab=r'$\mu$', show_mode=False, labelsize=9, framealpha=0.5)

# Plot kappa histogram
plt.subplot(2, 2, 2)
plot_post(kappa_sample, xlab=r'$\kappa$', show_mode=False, labelsize=9, framealpha=0.5)

# Plot theta 1
plt.subplot(2, 2, 3)
plot_post(theta1_sample, xlab=r'$\theta1$', show_mode=False, labelsize=9, framealpha=0.5)

# Plot theta 28
plt.subplot(2, 2, 4)
plot_post(theta28_sample, xlab=r'$\theta28$', show_mode=False, labelsize=9, framealpha=0.5)

plt.tight_layout()
plt.savefig('Figure_9.14.png')
Esempio n. 32
0
# Extract values of 'a'
a0_sample = trace['a0'][burnin::thin]
b1_sample = trace['b1'][burnin::thin]
b2_sample = trace['b2'][burnin::thin]
b1b2_sample = trace['b1b2'][burnin::thin]

b0_sample = a0_sample * np.std(y) + np.mean(y)
b1_sample = b1_sample * np.std(y)
b2_sample = b2_sample * np.std(y)
b1b2_sample = b1b2_sample * np.std(y)

plt.figure(figsize=(25, 20))
plt.subplot(451)
plot_post(b0_sample,
          xlab=r'$\beta0$',
          show_mode=False,
          framealpha=0.5,
          bins=50,
          title='Baseline')
plt.xlim(b0_sample.min(), b0_sample.max())

count = 2
for i in range(len(b1_sample[0])):
    plt.subplot(4, 5, count)
    plot_post(b1_sample[:, i],
              xlab=r'$\beta1_{%s}$' % i,
              show_mode=False,
              framealpha=0.5,
              bins=50,
              title='x1: %s' % x1names[i])
    count += 1
Esempio n. 33
0
a_sd_sample = mcmc.trace('a_sd')[:]

# Convert the values.

b0_sample = convert_baseline(a0_sample, a_sample, x_levels, y)
b_sample = convert_deflection(a0_sample, a_sample, x_levels, y)

sig_sample = convert_sigma(y, sigma_sample)
b_sd_sample = convert_sigma(y, a_sd_sample)

# Plot the results.

plot.figure(figsize=(6.0, 4.0))

plot.subplot(211)
plot_post(sig_sample, title=r'$\sigma$ (cell SD) posterior')

plot.subplot(212)
plot_post(b_sd_sample, title=r'$aSD$ posterior')

plot.subplots_adjust(wspace=0.2, hspace=0.5)

plot.figure(figsize=(18.0, 3.0))
total_subplot = len(b_sample[0, :])
plot_n = 100 + (total_subplot + 1) * 10 + 1

plot.subplot(plot_n)
plot_post(b0_sample, title=r'$\beta_0$ posterior')

for i in range(total_subplot):
    plot.subplot(plot_n + i + 1)