I checked some of the codes in the shared link and came up with the following piece of code:
max_cp_inference = 10
tiled_times = np.arange(T)[:, None].repeat(max_cp_inference, axis=1)
with pm.Model() as model:
intercept = pm.Normal("intercept")
slope_init = pm.Normal("slope_init")
n_cps = pm.DiscreteUniform("n_cps", 0, max_cp_inference)
cp_times = pm.DiscreteUniform("cp_times", 1, T-1, shape=max_cp_inference)
cp_times_sorted = cp_times.sort()
is_timestep_past_cp = (tiled_times >= cp_times_sorted[None, :].repeat(T, axis=0))
# We need to disable transforms so that we can use the posterior points directly
# As the transformed draws are not saved
cp_sd = pm.HalfNormal('cp_sd', sigma=0.01, transform=None)
cp_deltas = pm.Normal('cp_deltas', sigma=cp_sd, shape=max_cp_inference)
cp_contrib = aet.sum(cp_deltas[:n_cps] * is_timestep_past_cp[:, :n_cps], axis=1)
slope = pm.Deterministic(
"slope",
aet.cumsum(slope_init + cp_contrib, axis=-1)
)
μ = pm.Deterministic("μ",
intercept
+ slope
)
σ = pm.HalfNormal("σ", sigma=0.1)
y_hat = pm.Normal("y", mu=μ, sigma=σ, observed=y, shape=100)
I get no divergences, but several warnings instead:
/home/ec2-user/anaconda3/envs/pymc_p310/lib/python3.10/site-packages/pymc/step_methods/metropolis.py:296: RuntimeWarning: overflow encountered in exp
"accept": np.mean(np.exp(self.accept_rate_iter)),
/home/ec2-user/anaconda3/envs/pymc_p310/lib/python3.10/site-packages/pymc/step_methods/metropolis.py:296: RuntimeWarning: overflow encountered in exp
"accept": np.mean(np.exp(self.accept_rate_iter)),
/home/ec2-user/anaconda3/envs/pymc_p310/lib/python3.10/site-packages/pymc/step_methods/metropolis.py:296: RuntimeWarning: overflow encountered in exp
"accept": np.mean(np.exp(self.accept_rate_iter)),
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 64 seconds.
INFO:pymc:Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 64 seconds.
The acceptance probability does not match the target. It is 0.8799, but should be close to 0.8. Try to increase the number of tuning steps.
WARNING:pymc:The acceptance probability does not match the target. It is 0.8799, but should be close to 0.8. Try to increase the number of tuning steps
However, I noticed that the n_cps RV tends to be equal to the maximum number of changepoints allowed max_cp_inference . In addition, the disadvantage of this model is that I’m not able to get the probability of a changepoint and the probability distribution for the magnitude as in the original implementation: