Created on 01.02.2010


@author: floha
'''

import numpy as np
from scipy import stats
from matplotlib import pyplot
from fit import sampler, mcmc

print "================================="
print "explains the recording of additional information, for the rest see 1dnormal"

model = stats.norm.pdf


def testfunction():
    return 3


startvalue = 0
proposal = sampler.Multivariate_Normal_Sampler(covariance=100)
mcmcsampler = mcmc.Metropolis_Like_MCMC(model, startvalue, proposal)
mcmcsampler.set_recording_function(testfunction, 10)
mcmcsampler.run(100)
mcmcsampler.chain.save_data()

print "done, look at the record file in the chain output folder"
print "here's a printout"
print mcmcsampler.chain.records
Example #2
0
from scipy import stats
from matplotlib import pyplot
from fit import sampler, mcmc



def posterior(x):
    return np.square(x)

# remember, np scales with exp( (x/scale)^2 /2)
def pdf(x):
    return (stats.norm.pdf(x, scale = 1.0/np.sqrt(2) ))

startvalue = 0.2
proposal = sampler.Multivariate_Normal_Sampler(scale=2)

mcmcsampler = mcmc.Metropolis_Like_MCMC(posterior,startvalue, proposal, nll=True)
mcmcsampler.save_intervals = 1000
mcmcsampler.run(10000)

mcmcsampler.chain.convert_list_to_nparray()
#plotting 
pyplot.figure(num=3) 
pyplot.plot(np.arange(-1,1,0.01), posterior(np.arange(-1,1,0.01)))
pyplot.title("target distribution")
pyplot.hist(mcmcsampler.chain.statearray[:,0], bins=100, normed=True)
pyplot.plot(np.arange(-5,5,0.01), pdf(np.arange(-5,5,0.01)))
pyplot.show()


Example #3
0
            server.submit(target, (par, ), modules=("math", )) for par in x
        ]
        out = [job() for job in jobs]
        return out
    else:
        return target(x)


prior = bayesian.Uniform_Prior(lower=0)
post = bayesian.Parallel_Posterior(likelihood, prior, nll=False)

startvalue = 0.1
proposal = sampler.Multivariate_Normal_Sampler(scale=1)

mcmcsampler = mcmc.Metropolis_Like_MCMC(post.get_value,
                                        startvalue,
                                        proposal,
                                        parallel_cores=cores)

mcmcsampler.run(5000)

server.print_stats()
print "====================================="
print("acceptancerate =" + str(mcmcsampler.chain.get_acceptance_rate()))
print "The number of MCMC steps done is: ", len(mcmcsampler.chain.states)
print "As the parallel mode creates new model runs just in case other runs are  "
print "rejected, the final number of mcmc steps depends stochastically on the acceptance rate"

#recalculates the model values, can be usefulf for stochastic models
#mcmcsampler.recalculate = True
#mcmcsampler.run(5000)