我很难为不同的参数制作一个数组数组。换句话说,附加一个与if语句一致的值。
def chisq(exp,th,err):
return np.sum(((exp - th)/err)**2)
def stddv(val,mean,n):
return np.sqrt(np.sum((val - mean)**2)/n)
def p(inputs_w,inputs_phi):
return chisq(inputs_w,wdsigdcos,wdsigdcos_err) + chisq(inputs_phi,phidigdcos,phidsigdcos_err)
initial_val = np.asanyarray([0.8, 0.8, 1, 1, 0.8, 0.8, 1, 1, 0.8, 0.8, 13, 4])
def MCMC(initials,N):
lam_pi_w_p, lam_eta_w_p, b_w_p, beta_w_p, lam_pi_phi_p, lam_eta_phi_p, b_phi_p, beta_phi_p, lam_pi_NN_p, lam_eta_NN_p, g_pi_NN_p, g_eta_NN_p = initials + 0.1*np.random.normal()
lam_pi_w, lam_eta_w, b_w, beta_w, lam_pi_phi, lam_eta_phi, b_phi, beta_phi, lam_pi_NN, lam_eta_NN, g_pi_NN, g_eta_NN = initials
chain = np.asanyarray([initials])
stddev = stddv(chain, np.mean(chain), 16)
randomnum = -2*np.log(np.random.rand())
for i in range(1, N):
# g_pi_NN_i, g_eta_NN_i, g_phi_pi_i, g_w_pi_i, g_phi_eta_i, g_w_eta_i = initials
inputs_w_i = cs_B(wW, wt, m_w, f_w, g_w_pi, g_w_eta, chain[i - 1][0], chain[i - 1][1] ,chain[i - 1][8], chain[i - 1][9],chain[i - 1][2], 2,chain[i - 1][10], chain[i - 1][11], chain[i - 1][3])
inputs_w_p = cs_B(wW, wt, m_w, f_w, g_w_pi, g_w_eta, lam_pi_w_p, lam_eta_w_p, lam_pi_NN_p, lam_eta_NN_p, b_w, 2,
g_pi_NN_p, g_eta_NN_p, beta_w_p)
inputs_phi_i = cs_B(phiW, phit, m_phi, f_phi, g_phi_pi, g_phi_eta, chain[i - 1][4], chain[i - 1][5], chain[i - 1][8], chain[i - 1][9],chain[i - 1][6], 4,chain[i - 1][10], chain[i - 1][11], chain[i - 1][7])
inputs_phi_p = cs_B(phiW, phit, m_phi, f_phi, g_phi_pi, g_phi_eta, lam_pi_phi_p, lam_eta_phi_p, lam_pi_NN_p,
lam_eta_NN_p, b_phi, 4, g_pi_NN_p, g_eta_NN_p, beta_phi_p)
prob_p = p(inputs_w_p, inputs_phi_p)
prob_i = p(inputs_w_i, inputs_phi_i)
ratio = prob_p - prob_i
if ratio < randomnum:
chain.append(lam_pi_w_p, lam_eta_w_p, b_w_p, beta_w_p, lam_pi_phi_p, lam_eta_phi_p, b_phi_p, beta_phi_p, lam_pi_NN_p, lam_eta_NN_p, g_pi_NN_p, g_eta_NN_p)
else:
chain.append(chain[i - 1][0], chain[i - 1][1], chain[i - 1][2], chain[i - 1][3], chain[i - 1][4], chain[i - 1][5], chain[i - 1][6], chain[i - 1][7], chain[i - 1][8], chain[i - 1][9], chain[i - 1][10], chain[i - 1][11])
return [chain,stddev]
mcmc_run = MCMC(initial_val,10)
print('MCMC: ',mcmc_run)
我正在尝试运行名为MCMC的功能。我希望我的函数为多个参数([i-1])取一个初始值,将其与建议值([i])比较,然后附加与if循环一致的值。我正在寻找的结果是每个参数的数组。
最终我想自己绘制每个参数的数组。