Skip to content

Commit

Permalink
Use reasonable initial values (still pretty fast, and now good results)
Browse files Browse the repository at this point in the history
  • Loading branch information
aflaxman committed May 12, 2011
1 parent 49dea95 commit e8c376b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 150,10 @@ I'm not keeping careful track of if the results change as I modify the
step methods, which is probably not a recommended practice. But it is
fun to see the methods get faster and faster as I group more and more
in the AM step method. (43s wall time for M.B and M.U in one AM,
commit here)
commit here; 32s wall time for all stochs together, but the results
are not pretty.)

Sometimes I've had good luck with choosing initial values for the MCMC
by maximizing the posterior or something related to it. Here is code
to do that, which doesn't take very long (36s wall time, and
respectible looking results are back).
4 changes: 2 additions & 2 deletions analysis1.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 18,14 @@
Z = Z[0:240:1,0:10:1]

## Priors
var_u = pymc.Gamma('var_u', alpha=1, beta=1)
var_u = pymc.Gamma('var_u', alpha=1, beta=1, value=1.)
tau_u = pymc.Lambda('tau_u', lambda v=var_u: v**-1)

B = pymc.Normal('B', mu=[0, 0], tau=10000**-1)

U = pymc.Normal('u', mu=0, tau=tau_u, value=np.zeros(10))

var_e1 = pymc.Uniform('var_e1', lower=0, upper=100)
var_e1 = pymc.Uniform('var_e1', lower=0, upper=100, value=1.)
tau_e1 = pymc.Lambda('tau_e1', lambda v=var_e1: v**-1)

@pymc.deterministic
Expand Down
6 changes: 6 additions & 0 deletions run_analysis1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,12 @@
from pylab import hist, show
from pymc import Matplot

# Generate reasonable initial values by maximizing posterior with
# variances held fixed
M = pymc.MAP([analysis1.B, analysis1.U, analysis1.y_i])
M.fit(method='fmin_powell', verbose=1)

# Sample from full posterior distribution
M = pymc.MCMC(analysis1)
M.use_step_method(pymc.AdaptiveMetropolis, [M.B, M.U, M.var_e1, M.var_u])
M.sample(iter=100000, burn=50000, thin=10, verbose=2)
Expand Down

0 comments on commit e8c376b

Please sign in to comment.