-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple GP example causes RecursionError #111
Comments
Continuing to investigate, seems like @dfm has seen a similar pickling-related issue before in pymc-devs/pymc#3844. The suggested fix there is:
but that doesn't seem to help. |
This does go away if |
As you say, this is really more of a PyMC3 bug (they are insistent on mp_ctx=mp.get_context("fork") You're going to have some pain trying to ever get this to work in a Jupyter notebook on Mac or Windows with recent versions of PyMC3, I think. An alternative is to apply the mean function yourself, e.g. something like [untested]: gp = GP(terms.Matern32Term(sigma=noise, rho=20), x, yerr**2)
gp.marginal('gp', observed=y - mean(x)) |
Thanks so much for the quick help! Using your tips, I find that the following works in both a script, an ipython environment and a jupyter notebook: %matplotlib inline
import numpy as np
import pymc3 as pm
from exoplanet.gp import GP, terms
from exoplanet import eval_in_model
import multiprocessing as mp
# Generate some fake data with a heaviside function
x = np.linspace(-10, 10)
true_x0 = 2
true_amp = 10
noise = 0.1
y = true_amp * np.heaviside(x - true_x0, 1) + noise * np.random.randn(*x.shape)
yerr = noise * np.ones_like(y)
class MeanModel:
def __init__(self, x0, amp):
self.x0 = x0
self.amp = amp
def __call__(self, X):
return pm.math.where(pm.math.ge(X - self.x0, 0), self.amp, 0)
# define the GP marginal likelihood
with pm.Model() as model:
x0 = pm.Uniform('x0', lower=-10, upper=10, testval=1.1 * true_x0)
amp = pm.Uniform('amp', lower=0, upper=100, testval=0.8 * true_amp)
mean = MeanModel(x0, amp)
gp = GP(terms.Matern32Term(sigma=noise, rho=20), x, yerr ** 2)
gp.marginal('gp', observed=y - mean(x))
# find MAP solution (this works)
with model:
map_soln = pm.find_MAP()
with model:
trace = pm.sample(start=map_soln,
draws=1000, tune=1000, init='jitter+adapt_full',
mp_ctx=mp.get_context("fork"))
# quick script to plot the results:
import matplotlib.pyplot as plt
plt.errorbar(x, y, yerr, fmt='.', color='k', ecolor='silver')
for i in np.random.randint(0, len(trace), size=10):
with model:
mu, var = eval_in_model(
gp.predict(x, return_var=True), trace[i]
)
mean_eval = eval_in_model(
mean(x), trace[i]
)
plt.fill_between(x, mu + np.sqrt(var) + mean_eval,
mu - np.sqrt(var) + mean_eval, alpha=0.2)
plt.show()
from corner import corner
corner(pm.trace_to_dataframe(trace))
plt.show() To summarize, the key changes are passing:
to
|
Awesome! It's possible that you don't need the context of you subtract the mean manually. Did you try that? |
If I manually subtract the mean but leave out the |
That surprises me - very strange! |
When I define a custom mean model via exoplanet's GP object, I get a recursion depth exceeded error that I don't fully understand. This is almost certainly my misunderstanding of the proper mean model definition for
exoplanet
but I can't figure out what's wrong. Any tips? Thanks!Should run without error, but I'm getting a very long traceback starting with
and ending in:
Setup
Update: fixed some silly parameters for the fake data, still getting the same error.
The text was updated successfully, but these errors were encountered: