예제 #1
0
파일: test.py 프로젝트: carkey/MarkovPy
def main():
    """Run a Markov chain Monte Carlo to fit a high-dimensional pdf"""
    
    # fit the data -- sigma sampled in log
    np.random.seed()
    p0 = []
    for i in range(dim):
        p0.append([-2.,2.])
        
    samps,post,frac = mcmc.mcfit(logpost,p0,burnin=5000,N=3000,outfile='test.mcmc')
    
    # pl.figure()
    # pl.plot(post)
    
    vtens_est = np.cov(samps.T)
    # print np.sum(vtens_est-vtensor)
    
    for i in range(dim):
        pl.figure().add_subplot(111)
        
        # x = np.dot(samps,evecs[:,i])
        # pl.hist(x,100,normed=True,histtype="step",color="g")
        
        x = samps
        x = x[x < 3.*np.sqrt(evals[i])]
        x = x[x > -3.*np.sqrt(evals[i])]
        pl.hist(x,100,normed=True,histtype="step",color="k")
        
        xs = np.linspace(-3.*np.sqrt(evals[i]),3.*np.sqrt(evals[i]),500)
        pl.plot(xs,np.exp(-xs**2/evals[i]/2)/np.sqrt(2*np.pi*evals[i]))
        
        pl.xlim([-3.*np.sqrt(evals[i]),3.*np.sqrt(evals[i])])
        
    
    pl.show()
예제 #2
0
def sampleAlpha(x):
    nStep = 30
    pars = -2. * np.zeros(nStep)
    if len(x) > 0:
        imax = np.ceil(np.max(x) * nStep).astype('int')
        pars[imax:] = pars[imax:] - 2.
    p0 = []
    for i in range(len(pars)):
        p0.append([pars[i]-0.001,pars[i]+0.001])
    parsChain,lnPChain,afrac = mcmc.mcfit(lnPosterior,p0,burnin=0,N=3000,
                                           args=(x,),outfile='foo.mcmc')
    return parsChain