Closed hposborn closed 4 years ago
Ah. This actually only happens when I have previously run some sort of radvel model with 1 planet (hence the 9 parameters) - the code I wrote to reproduce the error doesn't work if it's run "fresh". So clearly radvel.mcmc is remembering the 1-planet model, despite the fact it's initiated with a whole new model... Is there any way to "wipe" radvel's memory, or do I need to restart my Jupyter notebook kernel every time?
It's not easy to adjust the number of planets in a Posterior
object after it is initialized with a given set of parameters. Try re-initializing the RVmodel
, GPLikelihood
, CompositeLikelihood
, and Posterior
objects.
Thanks for the quick response!
I'm not sure how to reinitialise those classes deeper than I have tried here. I guess my initial model isn't there, but params2 is a separate and disconnected dictionary from the initial params, and each of the created objects has a different name (gppost2, gplike2, etc). Yet, when fed into radvel.mcmc, it keeps some sort of memory! So is there some variable to feed to RVmodel
, GPLikelihood
, CompositeLikelihood
, and Posterior
(and maybe radvel.mcmc) which reinitialises completely?
Here's an example. If you run this snippet in a Jupyter notebook after running the above example, it fails:
params3 = radvel.Parameters(1,basis='per tc secosw sesinw k')
params3['per1'] = radvel.Parameter(value=6.9)
params3['tc1'] = radvel.Parameter(value=17.3)
params3['sesinw1'] = radvel.Parameter(value=0.,vary=False) # fix eccentricity = 0
params3['secosw1'] = radvel.Parameter(value=0.,vary=False)
params3['k1'] = radvel.Parameter(value=4)
params3['dvdt'] = radvel.Parameter(value=0.,vary=False)
params3['curv'] = radvel.Parameter(value=0.,vary=False)
params3['gp_amp'] = radvel.Parameter(value=25.0)
params3['gp_explength'] = radvel.Parameter(value=gp_explength_mean)
params3['gp_per'] = radvel.Parameter(value=gp_per_mean)
params3['gp_perlength'] = radvel.Parameter(value=gp_perlength_mean)
gpmodel3 = radvel.model.RVModel(params3)
like3 = radvel.likelihood.GPLikelihood(gpmodel3, t, vel,
errvel, hnames, suffix='_harps',
kernel_name="QuasiPer"
)
params3['gamma_harps'] = radvel.Parameter(value=np.average(vel))
params3['jit_harps'] = radvel.Parameter(value=0.5)
gplike3 = radvel.likelihood.CompositeLikelihood([like3])
gppost3 = radvel.posterior.Posterior(gplike3)
gppost3.priors += [radvel.prior.Gaussian('per1', 6.9, 0.5)]
gppost3.priors += [radvel.prior.Gaussian('tc1', 17.3, 2.5)]
gppost3.priors += [radvel.prior.Jeffreys('k1', 0.01, 10.)]
gppost3.priors += [radvel.prior.Jeffreys('gp_amp', 0.01, 100.)]
gppost3.priors += [radvel.prior.Jeffreys('jit_harps', 0.01,10.)]
gppost3.priors += [radvel.prior.Gaussian('gp_explength', gp_explength_mean, gp_explength_unc)]
gppost3.priors += [radvel.prior.Gaussian('gp_per', gp_per_mean, gp_per_unc)]
gppost3.priors += [radvel.prior.Gaussian('gp_perlength', gp_perlength_mean, gp_perlength_unc)]
from scipy import optimize
res3 = optimize.minimize(gppost3.neglogprob_array, gppost3.get_vary_params(), method='Powell')
print(gppost3)
chains3 = radvel.mcmc(gppost3,nrun=100, savename='P2_chains.h5')
OK, I found the bug. The radvel.statevars
namespace/class which is used to coordinate the MCMC run across multiple threads was not being reset between MCMC runs. For now you can add a radvel.statevars.reset()
call between your mcmc runs.
I'll add this reset call into the radvel.mcmc
function so that it is automatically called at the start of any new MCMC run. This change will be released with v1.3.8.
Please verify that the radvel.statevars.reset()
trick works for you and close the issue if it does.
Yep, that works. Thanks for the fix (and for a very useful bit of code in general)!
I adapted this GP tutorial to work with 2 planets. However, the radvel.mcmc step fails when computing the GR statistic, and I'm not entirely sure why.
Here's some code to reproduce the error:
And the output I get: