Esempio n. 1
0
 def compute_dic(self):
     if not isinstance(self.model, list):
         self.dic = pm.dic(trace=self.trace, model=self.model)
     else:
         self.dic = np.array([
             pm.dic(trace=trace, model=model)
             for (trace, model) in zip(self.trace, self.model)
         ])
Esempio n. 2
0
def test_dic_warns_on_transformed_rv():
    """
    Test that deviance information criterion calculation warns when an RV is transformed
    See https://github.com/pymc-devs/pymc3/issues/789
    """
    x_obs = np.arange(6)

    with pm.Model() as model:
        p = pm.Beta('p', 1., 1.)
        x = pm.Binomial('x', 5, p, observed=x_obs)

        step = pm.Metropolis()
        trace = pm.sample(100, step)

    with warnings.catch_warnings(record=True) as w:
        calculated = pm.dic(trace, model)

        assert (len(w) == 1)
Esempio n. 3
0
def test_dic():
    """Test deviance information criterion calculation"""
    x_obs = np.arange(6)

    with pm.Model() as model:
        p = pm.Beta('p', 1., 1., transform=None)
        x = pm.Binomial('x', 5, p, observed=x_obs)

        step = pm.Metropolis()
        trace = pm.sample(100, step)
        calculated = pm.dic(trace)

    mean_deviance = -2 * st.binom.logpmf(np.repeat(np.atleast_2d(x_obs), 100, axis=0), 5,
                                         np.repeat(np.atleast_2d(trace['p']), 6, axis=0).T).sum(axis=1).mean()
    deviance_at_mean = -2 * st.binom.logpmf(x_obs, 5, trace['p'].mean()).sum()
    actual = 2 * mean_deviance - deviance_at_mean

    assert_almost_equal(calculated, actual, decimal=2)
Esempio n. 4
0
def test_dic_warns_on_transformed_rv():
    """
    Test that deviance information criterion calculation warns when an RV is transformed
    See https://github.com/pymc-devs/pymc3/issues/789
    """
    x_obs = np.arange(6)

    with pm.Model() as model:
        p = pm.Beta('p', 1., 1.)
        x = pm.Binomial('x', 5, p, observed=x_obs)

        step = pm.Metropolis()
        trace = pm.sample(100, step)

    with warnings.catch_warnings(record=True) as w:
        calculated = pm.dic(trace, model)

        assert(len(w) == 1)
Esempio n. 5
0
def test_dic():
    """Test deviance information criterion calculation"""
    x_obs = np.arange(6)

    with pm.Model() as model:
        p = pm.Beta('p', 1., 1., transform=None)
        x = pm.Binomial('x', 5, p, observed=x_obs)

        step = pm.Metropolis()
        trace = pm.sample(100, step)
        calculated = pm.dic(trace)

    mean_deviance = -2 * st.binom.logpmf(np.repeat(np.atleast_2d(x_obs), 100, axis=0), 5,
                                         np.repeat(np.atleast_2d(trace['p']), 6, axis=0).T).sum(axis=1).mean()
    deviance_at_mean = -2 * st.binom.logpmf(x_obs, 5, trace['p'].mean()).sum()
    actual = 2 * mean_deviance - deviance_at_mean

    assert_almost_equal(calculated, actual, decimal=2)
Esempio n. 6
0
# plt.fill_between(x_ord, sig_y1[:, 0], sig_y1[:, 1], color='gray', alpha=0.5)
plt.xlabel(u"时间t/年", fontsize=14, fontproperties=font)
plt.ylabel(u"故障率/%", fontsize=14, fontproperties=font)
ax.legend([u'拟合曲线'], loc='upper left', prop=font)
plt.grid()
plt.show()

plt.figure(figsize=(5, 3), facecolor=(1, 1, 1))
ax = plt.subplot(1, 1, 1)
# for jx in range(14, 21, 1):
#     k1 = 6
#     if jx==14:
#         ax.plot(elec_year[j:(j + k1)], elec_faults[j:(j + k1)], 'k--',  linewidth=0.9)
#     else:
#         plt.plot(elec_year[j:(j + k1)], elec_faults[j:(j + k1)], 'k--', linewidth=0.9)
#     j = j + k1
ax.plot(elec_year[91:97],
        tt.exp(org_beta00 + org_beta111 * elec_year[91:97] +
               org_beta2 * elec_tem[91:97]),
        'b-',
        linewidth=4)
# plt.fill_between(x_ord[:125], sig_y11[:125, 0], sig_y11[:125, 1], color='gray', alpha=0.5)
plt.xlabel(u"时间t/年", fontsize=14, fontproperties=font)
plt.ylabel(u"故障率/%", fontsize=14, fontproperties=font)
ax.legend([u'拟合曲线'], loc='upper left', prop=font)
plt.grid()
plt.show()
print(pm.dic(trace2, unpooled_model))
# Waic = pm.compare([trace1, trace3, trace2], [pooled_model, partial_model, unpooled_model,], ic='WAIC')
# print(Waic)
Esempio n. 7
0
plt.plot(x_1s[0][idx], y_l_post[idx], label='Linear')

alpha_p_post = chain_p['alpha'].mean()
beta_p_post = chain_p['beta'].mean(axis=0)
y_p_post = alpha_p_post + np.dot(beta_p_post, x_1s)

plt.plot(x_1s[0][idx], y_p_post[idx], label='Pol order {}'.format(order))

plt.scatter(x_1s[0], y_1s)
plt.xlabel('$x$', fontsize=16)
plt.ylabel('$y$', fontsize=16, rotation=0)
plt.legend()
plt.savefig('img605.png')


print (pm.dic(trace=trace_l, model=model_l))
print( pm.dic(trace=trace_p, model=model_p))

waic_l = pm.waic(trace=trace_l, model=model_l)
waic_p = pm.waic(trace=trace_p, model=model_p)
loo_l = pm.loo(trace=trace_l, model=model_l)
loo_p = pm.loo(trace=trace_p, model=model_p)

plt.figure()
plt.subplot(121)
for idx, ic in enumerate((waic_l, waic_p)):
	plt.errorbar(ic[0], idx, xerr=ic[1], fmt='bo')
plt.title('WAIC')
plt.yticks([0, 1], ['linear', 'quadratic'])
plt.ylim(-1, 2)
Esempio n. 8
0
    # Observed = pm.StudentT("Observed", mu=theta, sd=sigma, nu=nu, observed=elec_faults1)  # 观测值

    start = pm.find_MAP()
    # step = pm.Metropolis()
    trace3 = pm.sample(5000, start=start, tune=1000)
chain3 = trace3
varnames1 = ['beta', 'beta1', 'beta2', 'beta3', 'beta4']
pm.traceplot(chain3, varnames1)
plt.show()
varnames1 = ['sigma', 'mu_a', 'sigma_a', 'mu_a1', 'sigma_a1']
pm.traceplot(chain3, varnames1)
plt.show()
# 画出自相关曲线
pm.autocorrplot(chain3)
plt.show()

tracedf = pm.trace_to_dataframe(trace3, varnames=['beta1', 'beta2', 'beta3', 'beta4'])
sns.pairplot(tracedf)
plt.show()
print(pm.dic(trace3, mulpartial_model))
# ======================================================================
# 模型对比与后验分析
# ======================================================================
# Waic = pm.compare([traces_ols_glm, trace1], [mdl_ols_glm, pooled_model], ic='WAIC')
# Waic = pm.compare([trace2, trace3], [partial_model, mulpartial_model], ic='WAIC')
# print(Waic)




Esempio n. 9
0
    start = pm.find_MAP()
    # step = pm.Metropolis()
    trace = pm.sample(10000, start=start)
chain = trace
# logistic(chain, locals())
varnames = ['gamma1', 'mu', 'beta', 'out_pai']
varnames1 = ['out_pai']
pm.traceplot(chain, varnames)
plt.show()

# 画出自相关曲线
pm.autocorrplot(chain)
plt.show()

print(pm.dic(trace, XZ_model))

# 画出A公司的产品曲线
sig0 = pm.hpd(trace['out_pai'], alpha=0.6)[0]
sig = pm.hpd(trace['out_pai'], alpha=0.6)[1]
sig1 = pm.hpd(trace['out_pai'], alpha=0.6)[2]
sig2 = pm.hpd(trace['out_pai'], alpha=0.6)[3]
sig3 = pm.hpd(trace['out_pai'], alpha=0.6)[4]
sig4 = pm.hpd(trace['out_pai'], alpha=0.6)[5]

plt.figure()
ax = sns.distplot(sig0)
ax = sns.distplot(sig1)
ax = sns.distplot(sig2)
ax = sns.distplot(sig3)
ax = sns.distplot(sig4)
Esempio n. 10
0
    Observed = pm.Normal("Observed", theta, sd=sigma,
                         observed=elec_faults1)  # 观测值

    # start = pm.find_MAP()
    # step = pm.Metropolis()
    trace2 = pm.sample(4000, tune=500)
chain2 = trace2
varnames2 = ['sigma', 'beta', 'beta1', 'beta2']
pm.traceplot(chain2, varnames2)
plt.show()

# 画出自相关曲线
pm.autocorrplot(chain2)
plt.show()

print(pm.dic(trace2, unpooled_model))

# ======================================================================
# partial_model
# ======================================================================
with pm.Model() as partial_model:
    # define priors
    mu_a = pm.Normal('mu_a', mu=0., tau=0.0001)
    sigma_a = pm.HalfCauchy('sigma_a', 100)

    beta = pm.Normal('beta', 0, 1000, shape=companies)
    beta1 = pm.Normal('beta1', mu=mu_a, sd=sigma_a, shape=companies)
    beta2 = pm.Normal('beta2', 0, 1000)

    gamma = pm.Normal('gamma', 0, 100)  # 误差项
    sigma = pm.HalfCauchy('sigma', 5)  # Model error
idx = np.argsort(x_1s[0])
y_l_post = alpha_l_post + betas_l_post * x_1s[0]
plt.plot(x_1s[0][idx], y_l_post[idx], label='Linear')

alpha_p_post = chain_p['alpha'].mean()
betas_p_post = chain_p['beta'].mean(axis=0)
y_p_post = alpha_p_post + np.dot(betas_p_post, x_1s)
plt.plot(x_1s[0][idx], y_p_post[idx], label='Pol order {}'.format(order))

plt.scatter(x_1s[0], y_1s)
plt.xlabel('$x$', fontsize=16)
plt.ylabel('$y$', fontsize=16, rotation=0)
plt.legend()
plt.show()

dic_l = pm.dic(trace=trace_l, model=model_l)
dic_p = pm.dic(trace=trace_p, model=model_p)

waic_l = pm.waic(trace=trace_l, model=model_l)
waic_p = pm.waic(trace=trace_p, model=model_p)

loo_l = pm.loo(trace=trace_l, model=model_l)
loo_p = pm.loo(trace=trace_p, model=model_p)

plt.figure(figsize=(8, 4))
plt.subplot(121)
for idx, ic in enumerate((waic_l, waic_p)):
    plt.errorbar(ic[0], idx, xerr=ic[1], fmt='bo')
plt.title('WAIC')
plt.yticks([0, 1], ['linear', 'quadratic'])
plt.ylim(-1, 2)
F1, F2, Y = dataloader.subject_data(range(10), acc_filter=(0.92, 1))
x, y = get_trial_covariates(F1, F2, Y, T=T, inf=inf)
print "x:", x.shape
print x.shape, y.shape

m = additive_model_lin_exp()

decay_0 = []

for ii in range(1):
    trace, model = m.fit(x, y, mcmc_samples=samples, decay_0=decay_0)
    decay = trace["decay"]
    a, b, _ = plt.hist(decay)
    decay_0 = b[a.argmax()]
    print decay_0
    xp, m_, s_ = m.posterior_summary(trace)
    plt.figure()
    for lag in range(T + inf):
        plt.fill_between(xp, y1=(m_ - s_)[lag, :], y2=(m_ + s_)[lag, :], alpha=0.5)
        plt.plot(xp, m_[lag, :])
    plt.savefig(OUTPUT_PATH + "DIC:_" + str(np.round(pm.dic(trace, model))) + ".svg")


decay = trace["decay"]
w = trace["w"]

plt.figure()
plt.scatter(decay[:, 0], w[:, 0])
# plt.imshow(H)
plt.savefig(OUTPUT_PATH + "im.svg")
Esempio n. 13
0
    start = pm.find_MAP()
    step = pm.Metropolis()
    trace = pm.sample(7000, start=start, step=step)

map_estimate = pm.find_MAP(model=nonliner)
print(map_estimate)
chain = trace

com_pred = chain.get_values('pred')[::100].ravel()
plt.hist(com_pred,  histtype='stepfilled')
plt.show()
varnames1 = ['beta', 'beta1',  'beta4', 'sigma']
# pm.traceplot(chain3, varnames1)
# plt.show()
pm.traceplot(chain)
plt.show()
# 画出自相关曲线
pm.autocorrplot(chain)
plt.show()
print(pm.dic(trace, nonliner))
# ======================================================================
# 模型对比与后验分析
# ======================================================================
# Waic = pm.compare([traces_ols_glm, trace1], [mdl_ols_glm, pooled_model], ic='WAIC')
# Waic = pm.compare([trace2, trace3], [partial_model, mulpartial_model], ic='WAIC')
# print(Waic)




Esempio n. 14
0
# ax.plot(x_plot, XZ_meanC, label="true estimate")
# ax.plot(elec_year[116:], betaMAPC[:], marker='*', alpha=.8, label="Fitting estimate")
# ax.set_xlim(0, 1)
ax.legend()
plt.show()

# ================================================================================
ax = pm.energyplot(trace_2)
bfmi = pm.bfmi(trace_2)
ax.set_title(f"BFMI = {bfmi:.2f}")
plt.show()

WAIC1 = pm.compare([trace_1, trace_2], [model_1, model_2])
print('WAIC1: ', WAIC1)
WAIC = pm.waic(trace=trace_1, model=model_1)
DIC = pm.dic(trace=trace_1, model=model_1)
print(WAIC)
print('DIC: ', DIC)


# ================================================================================
# 计算均方误差
def Rmse(predictions, targets):
    return np.sqrt(np.mean((predictions - targets)**2))


# 计算均方误差
ALL_faults = (elec_data.Fault.values / elec_data.Nums.values)  # 数组形式,计算故障率大小
MAP_tmp = MAP_tmp / 1000
rmse2 = {}
for ip in np.arange(3):