pymc-devs / pymc-resources

PyMC educational resources
MIT License
1.96k stars 745 forks source link

Splies for 2nd edition #38

Closed bluesky314 closed 5 years ago

bluesky314 commented 5 years ago

In the second edition, ch4, splines are covered which was not in the 1se edition.

Currently I am doing :


num_knots = 5
knot_list = np.quantile(df2.year, q=np.linspace(0,1, num=num_knots))
knot_list

# Construct our B matrix (example from
# https://patsy.readthedocs.io/en/latest/spline-regression.html#general-b-splines)
B = patsy.dmatrix("bs(year, knots=knots, degree=3, include_intercept=True)",
                   data={'year': df2.year.values,
                         'knots': knot_list[1:-1]})

and then

np.random.seed(123)
with pm.Model() as m4_7:
    a = pm.Normal('a', 6, 10)
    w = pm.MvNormal('w', mu=np.zeros((1, B.shape[1])), cov=np.eye(B.shape[1]), shape=(1, B.shape[1]))
    #w = pm.MvNormal('w', np.zeros(B.shape[1]), np.eye(B.shape[1]))#, shape=B.shape[1])
    #w = pm.Normal('w', mu=0, sd=1)
    mu = a + pm.math.dot(np.asarray(B), w.T)
    sigma = pm.Exponential('sd', 1)
    T = pm.Normal('T', mu, sigma, observed=dfs.temp)
    trace = pm.sample(draws=1000, tune=1000, discard_tuned_samples=True)

However this is very very slow. Is there a quick way to do this?

aloctavodia commented 5 years ago

We are not working on the second edition yet, we will work on it when the author provides us with a more advanced draft of the book than the one already available. Questions like this are best suited to our discourse so I am closing this issue.