You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importnumpyasnp# Choose the "true" parameters.m_true=-0.9594b_true=4.294f_true=0.534# Generate some synthetic data from the model.N=50x=np.sort(10*np.random.rand(N))
yerr=0.1+0.5*np.random.rand(N)
y=m_true*x+b_truey+=np.abs(f_true*y) *np.random.randn(N)
y+=yerr*np.random.randn(N)
A=np.vstack((np.ones_like(x), x)).TC=np.diag(yerr*yerr)
cov=np.linalg.inv(np.dot(A.T, np.linalg.solve(C, A)))
b_ls, m_ls=np.dot(cov, np.dot(A.T, np.linalg.solve(C, y)))
deflnlike(theta, x, y, yerr):
m, b, lnf=thetamodel=m*x+binv_sigma2=1.0/(yerr**2+model**2*np.exp(2*lnf))
return-0.5*(np.sum((y-model)**2*inv_sigma2-np.log(inv_sigma2)))
importscipy.optimizeasopnll=lambda*args: -lnlike(*args)
result=op.minimize(nll, [m_true, b_true, np.log(f_true)], args=(x, y, yerr))
m_ml, b_ml, lnf_ml=result["x"]
deflnprior(theta):
m, b, lnf=thetaif-5.0<m<0.5and0.0<b<10.0and-10.0<lnf<1.0:
return0.0return-np.infdeflnprob(theta, x, y, yerr):
lp=lnprior(theta)
ifnotnp.isfinite(lp):
return-np.infreturnlp+lnlike(theta, x, y, yerr)
ndim, nwalkers=3, 100pos= [result["x"] +1e-4*np.random.randn(ndim) foriinrange(nwalkers)]
importemceesampler=emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y, yerr))
f=open("chain.dat", "w")
f.close()
forresinsampler.sample(pos, iterations=500, store=False):
position=res[0]
f=open("chain.dat", "a")
forkinrange(position.shape[0]):
f.write("{0:4d} {1:s}\n".format(k, " ".join(position[k])))
f.close()
# sample code goes here...
When I run the above code, I get the following error message:
TypeError Traceback (most recent call last)
in
1 for res in sampler.sample(pos, iterations=500, store=False):
----> 2 position = res[0]
3 f = open("chain.dat", "a")
4 for k in range(position.shape[0]):
5 f.write("{0:4d} {1:s}\n".format(k, " ".join(position[k])))
TypeError: 'State' object is not subscriptable
The text was updated successfully, but these errors were encountered:
yuanzunli
changed the title
save file
failed to save the state of the chain to a file "chain.dat"
Feb 19, 2021
General information:
Problem description:
I want to save the state of the chain to a file "chain.dat" as the method you introduced in the web
https://emcee.readthedocs.io/en/v2.2.1/user/advanced/
but I failed.
Below is my code:
When I run the above code, I get the following error message:
TypeError Traceback (most recent call last)
in
1 for res in sampler.sample(pos, iterations=500, store=False):
----> 2 position = res[0]
3 f = open("chain.dat", "a")
4 for k in range(position.shape[0]):
5 f.write("{0:4d} {1:s}\n".format(k, " ".join(position[k])))
TypeError: 'State' object is not subscriptable
The text was updated successfully, but these errors were encountered: