示例#1
0
 def test_pickle(self):
     pass
     startvalue = 0.2
     proposal = sampler.Multivariate_Normal_Sampler(scale=1)
     mcmc = Metropolis_Like_MCMC(ll_normal_noise, startvalue, proposal)
示例#2
0
import numpy as np 
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()

'''
Created on 01.02.2010


@author: floha
'''

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


def model(x):
    scale = (1, 20, 0.1, 2, 20000)
    y = stats.norm.pdf(x, scale=scale)
    return np.multiply.reduce(y)


startvalue = np.array([0, 0, 0, 0, 0])
proposal = sampler.Multivariate_Normal_Sampler(scale=1, dimension_proposal=1)

mcmcsampler = mcmc.Metropolis_Like_MCMC(model, startvalue, proposal)

#mcmcsampler.run(10000)
mcmcsampler.run_adaptive_metropolis(times=20)

print mcmcsampler.chain.get_acceptance_per_dimension(-2000)
print mcmcsampler.chain.get_acceptance_rate(-2000)
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
示例#5
0
'''
Created on 20.03.2010

@author: floha
'''

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

current3 = np.array([0, 0, 0])

generator = sampler.Multivariate_Normal_Sampler((1, 1, 1))
print generator.get_proposal(current3)  # one proposal
print generator.get_proposal(current3, 3)  # several proposals, as list
print generator.get_proposal(current3,
                             1)  # careful, this produces a list with one entry

generator = sampler.Multivariate_Uniform_Sampler()
generator.set_scale(lower=100, upper=200)
x = generator.get_proposal(0.0, 5)
print x