@@ -23,10 +23,13 @@ Note that T processes aren't additive in the same way as GPs, so addition of `TP
2323The following code draws samples from a T process prior with 3 degrees of freedom and a Gaussian process, both with the same covariance matrix.
2424
2525``` {code-cell} ipython3
26+ import arviz as az
2627import matplotlib.pyplot as plt
2728import numpy as np
28- import pymc3 as pm
29- import theano.tensor as tt
29+ import pymc as pm
30+ import pytensor.tensor as pt
31+
32+ from pymc.gp.util import plot_gp_dist
3033
3134%matplotlib inline
3235```
@@ -39,33 +42,34 @@ n = 100 # The number of data points
3942X = np.linspace(0, 10, n)[:, None] # The inputs to the GP, they must be arranged as a column vector
4043
4144# Define the true covariance function and its parameters
42- ℓ_true = 1.0
43- η_true = 3.0
44- cov_func = η_true **2 * pm.gp.cov.Matern52(1, ℓ_true )
45+ ell_true = 1.0
46+ eta_true = 3.0
47+ cov_func = eta_true **2 * pm.gp.cov.Matern52(1, ell_true )
4548
4649# A mean function that is zero everywhere
4750mean_func = pm.gp.mean.Zero()
4851
4952# The latent function values are one sample from a multivariate normal
5053# Note that we have to call `eval()` because PyMC3 built on top of Theano
51- tp_samples = pm.MvStudentT.dist(mu=mean_func(X).eval(), cov =cov_func(X).eval(), nu=3).random(size= 8)
54+ tp_samples = pm.draw(pm. MvStudentT.dist(mu=mean_func(X).eval(), scale =cov_func(X).eval(), nu=3), 8)
5255
5356## Plot samples from TP prior
5457fig = plt.figure(figsize=(12, 5))
55- ax = fig.gca()
56- ax .plot(X.flatten(), tp_samples.T, lw=3, alpha=0.6)
57- ax .set_xlabel("X")
58- ax .set_ylabel("y")
59- ax .set_title("Samples from TP with DoF=3")
58+ ax0 = fig.gca()
59+ ax0 .plot(X.flatten(), tp_samples.T, lw=3, alpha=0.6)
60+ ax0 .set_xlabel("X")
61+ ax0 .set_ylabel("y")
62+ ax0 .set_title("Samples from TP with DoF=3")
6063
6164
62- gp_samples = pm.MvNormal.dist(mu=mean_func(X).eval(), cov=cov_func(X).eval()).random(size= 8)
65+ gp_samples = pm.draw(pm. MvNormal.dist(mu=mean_func(X).eval(), cov=cov_func(X).eval()), 8)
6366fig = plt.figure(figsize=(12, 5))
64- ax = fig.gca()
65- ax.plot(X.flatten(), gp_samples.T, lw=3, alpha=0.6)
66- ax.set_xlabel("X")
67- ax.set_ylabel("y")
68- ax.set_title("Samples from GP");
67+ ax1 = fig.gca()
68+ ax1.plot(X.flatten(), gp_samples.T, lw=3, alpha=0.6)
69+ ax1.set_xlabel("X")
70+ ax1.set_ylabel("y")
71+ ax1.set_ylim(ax0.get_ylim())
72+ ax1.set_title("Samples from GP");
6973```
7074
7175## Poisson data generated by a T process
@@ -79,16 +83,16 @@ n = 150 # The number of data points
7983X = np.linspace(0, 10, n)[:, None] # The inputs to the GP, they must be arranged as a column vector
8084
8185# Define the true covariance function and its parameters
82- ℓ_true = 1.0
83- η_true = 3.0
84- cov_func = η_true **2 * pm.gp.cov.ExpQuad(1, ℓ_true )
86+ ell_true = 1.0
87+ eta_true = 3.0
88+ cov_func = eta_true **2 * pm.gp.cov.ExpQuad(1, ell_true )
8589
8690# A mean function that is zero everywhere
8791mean_func = pm.gp.mean.Zero()
8892
8993# The latent function values are one sample from a multivariate normal
9094# Note that we have to call `eval()` because PyMC3 built on top of Theano
91- f_true = pm.MvStudentT.dist(mu=mean_func(X).eval(), cov =cov_func(X).eval(), nu=3).random(size= 1)
95+ f_true = pm.draw(pm. MvStudentT.dist(mu=mean_func(X).eval(), scale =cov_func(X).eval(), nu=3), 1)
9296y = np.random.poisson(f_true**2)
9397
9498fig = plt.figure(figsize=(12, 5))
@@ -102,23 +106,22 @@ plt.legend();
102106
103107``` {code-cell} ipython3
104108with pm.Model() as model:
105- ℓ = pm.Gamma("ℓ ", alpha=2, beta=2)
106- η = pm.HalfCauchy("η ", beta=3)
107- cov = η **2 * pm.gp.cov.ExpQuad(1, ℓ )
109+ ell = pm.Gamma("ell ", alpha=2, beta=2)
110+ eta = pm.HalfCauchy("eta ", beta=3)
111+ cov = eta **2 * pm.gp.cov.ExpQuad(1, ell )
108112
109113 # informative prior on degrees of freedom < 5
110- ν = pm.Gamma("ν ", alpha=2, beta=1)
111- tp = pm.gp.TP(cov_func =cov, nu=ν )
114+ nu = pm.Gamma("nu ", alpha=2, beta=1)
115+ tp = pm.gp.TP(scale_func =cov, nu=nu )
112116 f = tp.prior("f", X=X)
113117
114- # adding a small constant seems to help with numerical stability here
115- y_ = pm.Poisson("y", mu=tt.square(f) + 1e-6, observed=y)
118+ pm.Poisson("y", mu=pt.square(f), observed=y)
116119
117- tr = pm.sample(1000 )
120+ tr = pm.sample(target_accept=0.9, nuts_sampler="nutpie", chains=2 )
118121```
119122
120123``` {code-cell} ipython3
121- pm.traceplot (tr, var_names=["ℓ ", "ν ", "η "]);
124+ az.plot_trace (tr, var_names=["ell ", "nu ", "eta "]);
122125```
123126
124127``` {code-cell} ipython3
@@ -131,20 +134,21 @@ with model:
131134
132135# Sample from the GP conditional distribution
133136with model:
134- pred_samples = pm.sample_posterior_predictive(tr, vars=[ f_pred], samples=1000 )
137+ pm.sample_posterior_predictive(tr, var_names=[" f_pred" ], extend_inferencedata=True )
135138```
136139
137140``` {code-cell} ipython3
138141fig = plt.figure(figsize=(12, 5))
139142ax = fig.gca()
140- from pymc3.gp.util import plot_gp_dist
141143
142- plot_gp_dist(ax, np.square(pred_samples["f_pred"]), X_new)
144+ f_pred_samples = np.square(
145+ az.extract(tr.posterior_predictive).astype(np.float32)["f_pred"].values
146+ ).T
147+ plot_gp_dist(ax, f_pred_samples, X_new)
143148plt.plot(X, np.square(f_true), "dodgerblue", lw=3, label="True f")
144149plt.plot(X, y, "ok", ms=3, alpha=0.5, label="Observed data")
145150plt.xlabel("X")
146151plt.ylabel("True f(x)")
147- plt.ylim([-2, 20])
148152plt.title("Conditional distribution of f_*, given f")
149153plt.legend();
150154```
0 commit comments