Reputation: 501
I want to use pymc to use a MH chain to sample from a custom log likelihood. But I can't seem to get it to work and can't find a decent example online.
def getPymcVariable(data):
def logp(value):
...
return ljps # returns a float
def random():
return np.random.rand(numDims);
dtype = type(random());
initPt = [0.45, 0.24, 0.68];
ret = pymc.Stochastic(logp = logp,
doc = 'SNLS RV',
name = 'SNLS',
parents = {},
random = random,
trace = True,
value = initPt,
dtype = dtype,
observed = False,
cache_depth = 2,
plot = True,
verbose = 0 );
return ret
if __name__ == '__main__':
data = np.loadtxt('../davisdata.txt');
numExperiments = 30;
numSamples = 10000;
SNLS = getPymcVariable(data)
model = pymc.Model([SNLS]);
mcmcModel = pymc.MCMC(model);
mcmcModel.use_step_method(pymc.Metropolis, SNLS, proposal_sd=1);
mcmcModel.sample(numSamples, burn=0, thin=1);
x = mcmcModel.trace('SNLS')[:]
np.savetxt(fileName, x);
Its a 3 dimensional variable, has a uniform prior and a log likelihood computed in logp(). I want to run an MH chain with an adaptive proposal distribution. But each time I run the sampler it just returns a uniform distribution (in fact, it just returns samples from the random function above - when I modified it to 0.5*np.random.rand(numDims) it returned a Unif( (0,1)^3) distribution. )
However, I know that the logp function is being called.
There are a few more questions I have: - What is the purpose of the random function above ? I initially thought it was a prior but doesn't look like it.
Upvotes: 0
Views: 852
Reputation: 2979
In PyMC2, I find it considerably simpler to use built-in distributions and the @potential
decorator for this sort of task. Here is a minimal example:
X = pm.Uniform('X', 0, 1, value=[0.45, 0.24, 0.68])
@pm.potential
def SNLS(X=X):
logp = -X[0]**2 / X[1]
logp += -X[1]**2 / X[2] # or whatever...
return logp
You can select an adaptive metropolis step method as follows:
m = pm.MCMC([X, SNLS])
m.use_step_method(pm.AdaptiveMetropolis, X)
Here is a notebook that puts this together and plots the results.
Upvotes: 1