liner = pm.Deterministic('liner', tt.exp(u + beta + (beta1[companyABC] * elec_year + \ beta2[companyABC] * elec_Pca_char1 + beta3[ companyABC] * elec_Pca_char2 + \ beta4[companyABC] * elec_year * elec_year))) phic = pm.Uniform('phic', lower=0, upper=1, testval=.3) zck = pm.Bernoulli('zck', p=phic, shape=companyABC.shape) # zij_ = pm.theanof.tt_rng().uniform(size=xij.shape) # zij = pm.Deterministic('zij', tt.lt(zij_, phii[sbjid])) line = tt.constant(np.ones((len(companyABC))) * .5) beta_mu = pm.Deterministic( 'beta_mu', tt.squeeze(tt.switch(tt.eq(zck, 0), liner, line))) Observed = pm.Weibull("Observed", alpha=alpha, beta=beta_mu, observed=elec_faults) # 观测值 # start = pm.find_MAP() step = pm.Metropolis([zck]) # step1 = pm.Slice([am0, am1]) trace = pm.sample(4000, step=[step], init='advi+adapt_diag', tune=1000) # with model1: # s = shared(pm.floatX(1)) # inference = pm.ADVI(cost_part_grad_scale=s) # # ADVI has nearly converged # inference.fit(n=20000) # # It is time to set `s` to zero # s.set_value(0) # approx = inference.fit(n=10000)
# 这里将威布尔参数的alpha 与 beta交换 with pm.Model() as unpooled_model: sigma = pm.HalfCauchy('sigma', 10, testval=1.) # BoundedNormal = pm.Bound(pm.Normal, lower=3) # switchpoint = BoundedNormal('switchpoint', 5, 5) # switchpoint = pm.HalfNormal('switchpoint', 2) early_rate = pm.Normal('early_rate', 0, 100) late_rate = pm.Normal('late_rate', 0, 100) beta1 = pm.math.switch(x_shared <= 5, early_rate, late_rate) beta = pm.Normal('beta', 0, 100) u = pm.Normal('u', 0, 0.0001) mu = pm.Deterministic('mu', tt.exp(beta + beta1 * x_shared + u)) # Observed_pred = pm.Weibull("Observed_pred", alpha=mu, beta=sigma, shape=elec_faults.shape) # 观测值 Observed = pm.Weibull("Observed", alpha=sigma, beta=mu, observed=y_shared) # 观测值 start = pm.find_MAP() # step = pm.Metropolis([switchpoint]) trace2 = pm.sample(3000, start=start) chain2 = trace2[1000:] varnames2 = ['beta', 'early_rate', 'late_rate', 'sigma', 'u'] pm.traceplot(chain2) plt.show() pm.energyplot(trace2) plt.show() # # 画出自相关曲线 # pm.autocorrplot(chain2, varnames2) # plt.show() print(pm.dic(trace2, unpooled_model))
# 调好的基于weibull的模型,但是后面分析有点问题,拟合效果不明显 with pm.Model() as unpooled_model: # define priors sigma = pm.Gamma('sigma', 1, 1.6) # mu = pm.Gamma('mu', 2, 5) beta = pm.Gamma('beta', 20, 2000, shape=companiesABC) beta1 = pm.Gamma('beta1', 20, 2000, shape=companiesABC) beta2 = pm.Gamma('beta2', 4, 80000) mu = pm.Deterministic( 'mu', tt.exp(beta[companyABC] + beta1[companyABC] * elec_year + beta2 * elec_tem)) Observed_pred = pm.Weibull("Observed_pred", alpha=mu, beta=sigma, shape=elec_faults.shape) # 观测值 Observed = pm.Weibull("Observed", alpha=sigma, beta=mu, observed=elec_faults) # 观测值beta为尺度参数 start = pm.find_MAP() # step = pm.Metropolis() trace2 = pm.sample(2000, start=start) chain2 = trace2 # varnames1 = ['beta', 'beta1', 'sigma', 'mu'] pm.traceplot(chain2) plt.show() com_pred = chain2.get_values('Observed_pred')[:].ravel()
beta = pm.Gamma('beta', 2, 200, shape=companiesABC) beta1 = pm.Gamma('beta1', 2, 200, shape=companiesABC) beta2 = pm.Gamma('beta2', 4, 80000) # beta = pm.Normal('beta', 5, 50, shape=companiesABC) # beta1 = pm.Normal('beta1', 0, 10, shape=companiesABC) # beta2 = pm.Normal('beta2', 0, 20) # u = pm.Gamma('u', 2, 200) # mu = tt.exp(beta[companyABC] + beta1[companyABC]*elec_year + beta2*elec_tem) mu = pm.Deterministic( 'mu', tt.exp(beta[companyABC] + beta1[companyABC] * elec_year + beta2 * elec_tem)) # Observed_pred = pm.Weibull("Observed_pred", alpha=mu, beta=sigma, shape=elec_faults.shape) # 观测值 Observed = pm.Weibull("Observed", alpha=mu, beta=sigma, observed=elec_faults) # 观测值 # start = pm.find_MAP() # step = pm.Metropolis([Observed]) trace2 = pm.sample(1000) chain2 = trace2 varnames1 = ['sigma', 'mu'] pm.traceplot(chain2) plt.show() print(pm.dic(trace2, unpooled_model)) print(pm.df_summary(trace2, varnames1)) # com_pred = chain2.get_values('Observed_pred')[:].ravel() # plt.hist(com_pred, range=[0, 2], bins=130, histtype='stepfilled') # plt.axvline(elec_faults.mean(), color='r', ls='--', label='True mean')
# theta = pm.InverseGamma('theta', 0.01, 0.01) # alpha = pm.Normal('alpha', 0, theta) # 建立与时间相关的函数 # define likelihood out_pai = pm.Deterministic( 'out_pai', np.exp(mu + beta * dag_time + gamma1 * z1 + gamma2 * z2 + gamma3 * z3 + gamma4 * z4) / (1 + np.exp(mu + beta * dag_time + gamma1 * z1 + gamma2 * z2 + gamma3 * z3 + gamma4 * z4))) # out_pai = pm.math.invlogit(mu + alpha + beta * dag_time + gamma1 * z1 + gamma2 * z2 + gamma3 * z3 + gamma4 * z4) # out_pai = pm.Deterministic('out_pai', mu + beta * dag_time + gamma1 * z1 + gamma2 * z2 + gamma3 * z3 + gamma4 * z4) Observed = pm.Weibull("Observed", 1, out_pai, observed=dag_faultpro) # 观测值 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()
beta = pm.Normal('beta', 0, 100, shape=companiesABC, testval=-3.) # beta1 = pm.Normal('beta1', 0, 10, shape=companiesABC, testval=.3) # beta2 = pm.Normal('beta2', 0, 100, testval=0.01) # beta3 = pm.Normal('beta3', 0, 100) theta = pm.Normal('theta', 0, 100, shape=companiesABC) theta1 = pm.Normal('theta1', 0, 20, shape=companiesABC) beta1 = theta[companyABC] + theta1[companyABC] * x_shared1 # mu = tt.exp(beta[companyABC] + beta1[companyABC]*elec_year + beta2*elec_tem) beta_mu = pm.Deterministic( 'beta_mu', tt.exp(beta[companyABC] + beta1[companyABC] * x_shared)) # Observed_pred = pm.Weibull("Observed_pred", alpha=mu, beta=sigma, shape=elec_faults.shape) # 观测值 Observed = pm.Weibull("Observed", alpha=alpha, beta=beta_mu, observed=y_shared) # 观测值 start = pm.find_MAP() # step = pm.Slice([beta1, u]) trace2 = pm.sample(2000, start=start) chain2 = trace2[1000:] varnames1 = ['alpha', 'beta_mu'] # varnames2 = ['beta', 'beta1', 'beta2', 'alpha', 'beta3'] # pm.plot_posterior(chain2, varnames2, ref_val=0) pm.traceplot(chain2) plt.show() pm.energyplot(trace2) plt.show() # elec_year1 = np.delete(elec_year, -1)
theta1 = pm.Deterministic('theta1', a0 + (σ_a * Δ_a).cumsum()) # theta = pm.Deterministic('theta', tt.exp(Bx_.dot(theta1) + δ + beta*elec_tem1[0:84]+ beta1*elec_RH1[0:84])) theta = pm.Deterministic('theta', tt.exp(Bx_.dot(theta1) + δ)) theta1B = pm.Deterministic('theta1B', a0B + (σ_aB * Δ_aB).cumsum()) thetaB = pm.Deterministic('thetaB', tt.exp(Bx_B.dot(theta1B) + δB)) theta1C = pm.Deterministic('theta1C', a0C + (σ_aC * Δ_aC).cumsum()) thetaC = pm.Deterministic('thetaC', tt.exp(Bx_C.dot(theta1C) + δC)) # Observed = pm.Poisson('Observed', mu =theta, observed=ys_faultsA) # 观测值 # ObservedB = pm.Poisson('ObservedB', mu =thetaB, observed=ys_faultsB) # 观测值 # ObservedC = pm.Poisson('ObservedC', mu =thetaC, observed=ys_faultsC) # 观测值 Observed = pm.Weibull('Observed', alpha=alpha, beta=theta, observed=ys_faultsA) # 观测值 ObservedB = pm.Weibull('ObservedB', alpha=alpha, beta=thetaB, observed=ys_faultsB) # 观测值 ObservedC = pm.Weibull('ObservedC', alpha=alpha, beta=thetaC, observed=ys_faultsC) # 观测值 # step1 = pm.Slice([Δ_a]) start = pm.find_MAP() trace_1 = pm.sample(1000, start=start, njobs=1) # ================================================================================ # Return a B-spline basis element B(x | t[0], ..., t[k+1])