Bad Initial Energy when observed var contains nan

I’m trying to predict using the model below:

with pm.Model() as model:
        smoothing_param = shared(smoothing)
        mu = pm.HalfNormal("mu", sigma=1e3)
        tau = pm.Exponential("tau", 1.0/1e3)
        z = pm.GaussianRandomWalk("z",
                               tau=tau / (1.0 - smoothing_param),
        y = pm.Normal("y",
                        tau=tau / smoothing_param,

The model samples fine when the y_shared values are:

array([0.3373, 0.3919, 0.3656, 0.427 , 0.5428, 0.58  , 0.636 , 0.6937,
       0.7287, 0.7345, 0.7517, 0.7874, 0.8003, 0.8062, 0.81  , 0.8394,
       0.8432, 0.8794])

but if I pad the observation with NaNs to predict, I get the following error:

Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [sigma, intercept, step_m, glb_m]
Sampling 4 chains:   0%|          | 0/10000 [00:00<?, ?draws/s]/.../lib/python3.7/site-packages/numpy/core/ RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/.../lib/python3.7/site-packages/numpy/core/ RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/.../lib/python3.7/site-packages/numpy/core/ RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/...lib/python3.7/site-packages/numpy/core/ RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
Sampling 4 chains:   0%|          | 0/10000 [00:00<?, ?draws/s]
Bad initial energy, check any log probabilities that are inf or -inf, nan or very small:
y   NaN

Having read through a number of posts and comments on predicting/interpolating, my understanding was that this is the way to sample from unseen/oos data.

What should I do differently here?

c.f. Getting 'Bad initial energy: inf' when trying to sample simple model

Likely the jitter is causing problem here, Try: pm.sample(init=‘adapt_diag’)

Hi @junpenglao, thanks for the quick response. I’ve tried changing the initiation of the sampler, but it doesn’t resolve the issue.

here is the observed var I’m testing on:

array([0.4676, 0.5269, nan, nan, nan, 0.63 , 0.66 , 0.6945,
0.7167, 0.7425, 0.76 , 0.7949, 0.79 , 0.8345, 0.84 , 0.8456,
0.8824, 0.8654])

The MAP of the model is:

{‘mu_log__’: array(5.68196393),
‘tau_log__’: array(6.54124236),
‘z’: array([-7.37099586e-05, 1.62284820e-08, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 1.94039545e-08, 2.03279524e-08, 2.13905499e-08,
2.20743083e-08, 2.28689464e-08, 2.34079451e-08, 2.44828626e-08,
2.43319430e-08, 2.57025398e-08, 2.58719394e-08, 2.60444190e-08,
2.71778563e-08, 7.37510149e-05]),
‘mu’: array(293.52532794),
‘tau’: array(693.14718246)}

and the test_point is:

mu_log__ -7.700000e-01
tau_log__ -1.060000e+00
z -1.500322e+10
y NaN
Name: Log-probability of test_point, dtype: float64

you’ll see in the MAP output that the missing / nan values in the observed var are returned as 0. This seems normal, and yet, still no go. Any ideas?

Oh, if your observation contains nan it’s not going to work. You can either used a masked array or remove the nans

hi, thanks for the reply. yea, the odd thing is that if I enter the observation as a Shared tensor it doesn’t work, but if I enter it as a pandas.Series it works, even with the nans. this was recommended to me by DanWeitzenfeld, at: Out-of-sample random-walk time series prediction

1 Like