Hi,
Following Aaron Parsons' example[1] I've set up a curve fit for a sine
wave with both frequency and phase as fitting parameters. Letting the
phase vary, I was expecting to see equally likely solutions show up
every 2 pi n + 0.5, but this did not occur.
Anyone care to comment on whether this is the right tool either for
global optimization, or for proper uncertainty analysis when multiple
solutions are possible?
Thanks in advance,
- Paul
[1]
http://sciencestoriesetc.blogspot.com/2009/04/more-on-mcmc-in-python.html
My example:
import pymc, numpy as n, pylab as p
from pymc import deterministic
class Theory:
def __init__(self, x):
self.x = x
def fn(self, w, phi):
return n.sin(2*n.pi*(w*self.x+ phi))
def __call__(self, *args):
return self.fn(*args)
x = n.arange(-1., 1, .01)
sig = .1*n.ones_like(x)
tau = 1/sig**2
actual_func = Theory(x)
# Generate some fake data
# Normally x, sig and mdata comes from the experiment
noise = n.random.normal(0, scale=sig, size=x.size)
mdata = actual_func(5., 0.5)
mdata += noise
a_fit = pymc.Uniform('w', 4.5, 5.3)
b_fit = pymc.Uniform('phi', 1, 5)
@deterministic
def func(a=a_fit, b=b_fit):
return actual_func(a,b)
mdata_var = pymc.Normal('mdata', mu=func, tau=tau,
value=mdata, observed=True)
mdl = pymc.Model([a_fit, b_fit, mdata_var])
mc = pymc.MCMC(mdl)
mc.sample(iter=30000,burn=10)
atrace = mc.trace('w')[:]
btrace = mc.trace('phi')[:]
a = n.mean(atrace)
b = n.median(btrace) # Hope this is one of the modes
print 'w',n.mean(atrace),n.std(atrace)
print 'phi',n.mean(btrace),n.std(btrace)
p.subplot(311); p.errorbar(x,mdata,yerr=sig); p.plot(x,actual_func
(a,b))
p.subplot(312); p.hist(atrace)
p.subplot(313); p.hist(btrace)
p.show()