Bo Zhang (NAOC, mailto:bozhang@nao.cas.cn) will have a few lessons on python.
python
to process astronomical data.These lectures are organized as below:
Docs: http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
In [1]:
%pylab inline
np.random.seed(0)
p = [3.2, 5.6, 9.2]
x = np.arange(-8., 5., 0.1)
y = np.polyval(p, x) + np.random.randn(x.shape[0])*1.
plt.plot(x, y);
In [2]:
# STEP 1 - define your model
def my_model(p, x):
return np.polyval(p, x)
# STEP 2 - define your cost function
def my_costfun(p, x, y):
return np.sum((my_model(p, x) - y)**2)
# STEP 3 - minimize cost function
from scipy.optimize import minimize
result = minimize(my_costfun, np.array([2., 3., 5.]), args=(x,y) )
In [3]:
print result
In [4]:
print 'RESULT:\n', result
print ''
print 'RELATIVE ERROR:\n', (result.x - p)/p*100., '%'
print ''
print 'Hessian ERROR:' #err = sqrt(diag(inv(Hessian)))
hess_err = np.sqrt(np.diag(result['hess_inv']))
print hess_err
In [ ]:
In [5]:
from emcee import EnsembleSampler
In [6]:
def lnprob(theta):
theta = np.array(theta)
if np.all(theta>-3.) and np.all(theta<3.):
return 0
return -np.inf
In [7]:
nwalkers = 10
ndim = 3
p0 = [np.random.rand(ndim) for i in range(nwalkers)]
sampler = EnsembleSampler(nwalkers, ndim, lnprob)
pos = sampler.run_mcmc(p0, 1000)
In [8]:
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(311)
ax.plot(sampler.chain[:,:,0].T, '-', color='k', alpha=0.3)
ax = fig.add_subplot(312)
ax.plot(sampler.chain[:,:,1].T, '-', color='k', alpha=0.3)
ax = fig.add_subplot(313)
ax.plot(sampler.chain[:,:,2].T, '-', color='k', alpha=0.3);
In [9]:
import corner
fig = corner.corner(sampler.flatchain, labels=["p0", "p1", "p2"],
truths=[0., 0., 0.])
# fig.savefig("triangle.png")
In [ ]:
1-D Gauassian
$p(x|\mu, \sigma) \propto \exp{(-\frac{(x-\mu)^2}{2\sigma^2})}$
N-D Gauassian
$p(\overrightarrow{x}|\overrightarrow{\mu}, \Sigma) \propto \exp{(-\frac{1}{2}(\overrightarrow{x}-\overrightarrow{\mu})^T\Sigma (\overrightarrow{x}-\overrightarrow{\mu}))}$
where $\Sigma$ is the covariance matrix
In [10]:
def lnprob(x, mu, ivar):
# if np.all(np.abs(x)<100.):
x = x.reshape(-1, 1)
mu = mu.reshape(-1, 1)
return -np.dot(np.dot((x-mu).T, ivar), x-mu)
# else:
# return -np.inf
In [11]:
mu = np.array([0.1, 0.2, 0.5])
cov = np.array([[1.0, 0.0, 0.0],
[0.0, 10, 9],
[0.0, 9, 10]])
ivar = np.linalg.inv(cov)
print 'ivar: \n', ivar
print 'det(cov): \n', np.linalg.det(cov)
print 'det(ivar): \n', np.linalg.det(ivar)
In [12]:
nwalkers = 10
ndim = 3
p0 = [np.random.rand(ndim) for i in range(nwalkers)]
sampler = EnsembleSampler(nwalkers, ndim, lnprob, args=(mu, ivar), threads=10)
pos,prob,state = sampler.run_mcmc(p0, 2000)
In [13]:
p0
Out[13]:
In [14]:
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(311)
ax.plot(sampler.chain[:,:,0].T, '-', color='k', alpha=0.3)
ax = fig.add_subplot(312)
ax.plot(sampler.chain[:,:,1].T, '-', color='k', alpha=0.3)
ax = fig.add_subplot(313)
ax.plot(sampler.chain[:,:,2].T, '-', color='k', alpha=0.3);
In [15]:
fig = corner.corner(sampler.flatchain, labels=["mu1", "mu2", "mu3"],
truths=mu)
In [16]:
print mu
print ivar
In [ ]:
suppose you choose a Gaussian likelihood:
$L(\theta|x_i,model) \propto \exp{(-\frac{(x_i-x_{i, model})^2}{2\sigma^2})} $
$ \log{(L(\theta|x_i,model))} \propto -\frac{(x_i-x_{i, model})^2}{2\sigma^2} = -\frac{1}{2}{\chi^2}$
In [17]:
def lnprior(theta):
if np.all(np.abs(theta)<10000.):
return 0
else:
return -np.inf
In [18]:
def lnlike(theta, x, y):
y_model = np.polyval(theta, x)
return -np.sum((y_model-y)**2)
In [19]:
def lnprob(theta, x, y):
return lnprior(theta)+lnlike(theta, x, y)
In [20]:
nwalkers = 10
ndim = 3
p0 = [np.random.rand(ndim) for i in range(nwalkers)]
sampler = EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y), threads=10)
pos,prob,state = sampler.run_mcmc(p0, 500)
In [21]:
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(311)
ax.plot(sampler.chain[:,:,0].T, '-', color='k', alpha=0.3)
ax = fig.add_subplot(312)
ax.plot(sampler.chain[:,:,1].T, '-', color='k', alpha=0.3)
ax = fig.add_subplot(313)
ax.plot(sampler.chain[:,:,2].T, '-', color='k', alpha=0.3);
In [22]:
fig = corner.corner(sampler.flatchain, labels=["p0", "p1", "p2"],
truths=p)
In [ ]:
In [23]:
sampler.reset()
pos,prob,state = sampler.run_mcmc(pos, 2000)
In [24]:
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(311)
ax.plot(sampler.chain[:,:,0].T, '-', color='k', alpha=0.3)
ax = fig.add_subplot(312)
ax.plot(sampler.chain[:,:,1].T, '-', color='k', alpha=0.3)
ax = fig.add_subplot(313)
ax.plot(sampler.chain[:,:,2].T, '-', color='k', alpha=0.3);
In [29]:
fig = corner.corner(sampler.flatchain, labels=["p0", "p1", "p2"],
truths=p)
fig = corner.corner(sampler.flatchain, labels=["p0", "p1", "p2"],
truths=result.x)
In [26]:
# truth
p
Out[26]:
In [27]:
# MCMC results
np.percentile(sampler.flatchain, [15., 50., 85.], axis=0)
Out[27]:
In [28]:
print result.x - hess_err
print result.x
print result.x + hess_err
In [ ]: