@@ -5,9 +5,9 @@ jupytext:
55 format_name : myst
66 format_version : 0.13
77kernelspec :
8- display_name : pymc-examples
8+ display_name : pymc
99 language : python
10- name : pymc-examples
10+ name : python3
1111---
1212
1313(hsgp-advanced)=
@@ -44,6 +44,7 @@ A secondary goal of this implementation is flexibility via an accessible impleme
4444import arviz as az
4545import matplotlib.pyplot as plt
4646import numpy as np
47+ import preliz as pz
4748import pymc as pm
4849import pytensor.tensor as pt
4950```
@@ -140,7 +141,9 @@ cov_mu = cov_trend + cov_short
140141# Define the delta GPs
141142n_gps = 10
142143eta_delta_true = 3
143- ell_delta_true = pm.draw(pm.Lognormal.dist(mu=np.log(ell_mu_short_true), sigma=0.5), draws=n_gps)
144+ ell_delta_true = pm.draw(
145+ pm.Lognormal.dist(mu=np.log(ell_mu_short_true), sigma=0.5), draws=n_gps, random_seed=rng
146+ )
144147
145148cov_deltas = [
146149 eta_delta_true**2 * pm.gp.cov.Matern52(input_dim=1, ls=ell_i) for ell_i in ell_delta_true
@@ -166,12 +169,14 @@ def generate_gp_samples(x, cov_mu, cov_deltas, noise_dist, rng):
166169 """
167170 n = len(x)
168171 # One draw from the mean GP
169- f_mu_true = pm.draw(pm.MvNormal.dist(mu=np.zeros(n), cov=cov_mu(x[:, None])))
172+ f_mu_true = pm.draw(pm.MvNormal.dist(mu=np.zeros(n), cov=cov_mu(x[:, None])), random_seed=rng )
170173
171174 # Draws from the delta GPs
172175 f_deltas = []
173176 for cov_delta in cov_deltas:
174- f_deltas.append(pm.draw(pm.MvNormal.dist(mu=np.zeros(n), cov=cov_delta(x[:, None]))))
177+ f_deltas.append(
178+ pm.draw(pm.MvNormal.dist(mu=np.zeros(n), cov=cov_delta(x[:, None])), random_seed=rng)
179+ )
175180 f_delta = np.vstack(f_deltas)
176181
177182 # The hierarchical GP
@@ -435,10 +440,9 @@ with pm.Model(coords=coords) as model:
435440 ell_mu_short = pm.Deterministic("ell_mu_short", pt.softplus(log_ell_mu_short))
436441
437442 eta_mu_trend = pm.Gamma("eta_mu_trend", mu=3.5, sigma=1)
438- ell_mu_trend_params = pm.find_constrained_prior (
439- pm.InverseGamma, lower=5, upper=12, mass=0.95, init_guess={"mu": 9.0, "sigma": 3.0}
443+ ell_mu_trend = pz.maxent(pz.InverseGamma(), lower=5, upper=12, mass=0.95, plot=False).to_pymc (
444+ "ell_mu_trend"
440445 )
441- ell_mu_trend = pm.InverseGamma("ell_mu_trend", **ell_mu_trend_params)
442446
443447 ## Prior for the offsets
444448 log_ell_delta_offset = pm.ZeroSumNormal("log_ell_delta_offset", dims="gp_ix")
@@ -473,7 +477,7 @@ Now, what do these priors mean? Good question. As always, it's crucial to do **p
473477
474478```{code-cell} ipython3
475479with model:
476- idata = pm.sample_prior_predictive()
480+ idata = pm.sample_prior_predictive(random_seed=rng )
477481```
478482
479483```{code-cell} ipython3
@@ -564,7 +568,7 @@ Once we're satisfied with our priors, which is the case here, we can... sample t
564568
565569```{code-cell} ipython3
566570with model:
567- idata.extend(pm.sample(nuts_sampler="numpyro", target_accept=0.9))
571+ idata.extend(pm.sample(nuts_sampler="numpyro", target_accept=0.9, random_seed=rng ))
568572```
569573
570574```{code-cell} ipython3
@@ -669,6 +673,7 @@ with model:
669673 var_names=["f_mu", "f"],
670674 predictions=True,
671675 compile_kwargs={"mode": "NUMBA"},
676+ random_seed=rng,
672677 ),
673678 )
674679```
@@ -863,7 +868,11 @@ cov_t = pm.gp.cov.Matern52(input_dim=1, ls=ell_t_true)
863868Kt = cov_t(t[:, None])
864869
865870K = pt.slinalg.kron(Kx, Kt)
866- f_true = pm.draw(pm.MvNormal.dist(mu=np.zeros(n_gps * n_t), cov=K)).reshape(n_gps, n_t).T
871+ f_true = (
872+ pm.draw(pm.MvNormal.dist(mu=np.zeros(n_gps * n_t), cov=K), random_seed=rng)
873+ .reshape(n_gps, n_t)
874+ .T
875+ )
867876
868877# Additive gaussian noise
869878sigma_noise = 0.5
@@ -947,17 +956,11 @@ with pm.Model() as model:
947956 Xs_x = Xx - xx_center
948957
949958 ## covariance on time GP
950- ell_t_params = pm.find_constrained_prior(
951- pm.Lognormal, lower=0.5, upper=4.0, mass=0.95, init_guess={"mu": 1.0, "sigma": 1.0}
952- )
953- ell_t = pm.Lognormal("ell_t", **ell_t_params)
959+ ell_t = pz.maxent(pz.LogNormal(), lower=0.5, upper=4.0, mass=0.95, plot=False).to_pymc("ell_t")
954960 cov_t = pm.gp.cov.Matern52(1, ls=ell_t)
955961
956962 ## covariance on space GP
957- ell_x_params = pm.find_constrained_prior(
958- pm.Lognormal, lower=0.5, upper=4.0, mass=0.95, init_guess={"mu": 1.0, "sigma": 1.0}
959- )
960- ell_x = pm.Lognormal("ell_x", **ell_x_params)
963+ ell_x = pz.maxent(pz.LogNormal(), lower=0.5, upper=4.0, mass=0.95, plot=False).to_pymc("ell_x")
961964 cov_x = pm.gp.cov.Matern52(1, ls=ell_x)
962965
963966 ## Kronecker GP
@@ -981,7 +984,7 @@ pm.model_to_graphviz(model)
981984
982985```{code-cell} ipython3
983986with model:
984- idata = pm.sample_prior_predictive()
987+ idata = pm.sample_prior_predictive(random_seed=rng )
985988```
986989
987990```{code-cell} ipython3
@@ -1015,7 +1018,7 @@ axs[1].set(ylim=ylims, title=r"Prior GPs, $\pm 1 \sigma$ posterior intervals");
10151018
10161019```{code-cell} ipython3
10171020with model:
1018- idata.extend(pm.sample(nuts_sampler="numpyro"))
1021+ idata.extend(pm.sample(nuts_sampler="numpyro", random_seed=rng ))
10191022```
10201023
10211024```{code-cell} ipython3
@@ -1075,6 +1078,7 @@ And isn't this beautiful?? Now go on, and HSGP-on!
10751078## Authors
10761079
10771080* Created by [Bill Engels](https://github.com/bwengals), [Alexandre Andorra](https://github.com/AlexAndorra) and [Maxim Kochurov](https://github.com/ferrine) in 2024 ([pymc-examples#668](https://github.com/pymc-devs/pymc-examples/pull/668))
1081+ * Use `pz.maxent` instead of `pm.find_constrained_prior`, and add random seed. [Osvaldo Martin](https://aloctavodia.github.io/). August 2024
10781082
10791083+++
10801084
0 commit comments