Initialization failed normal_lpdf

Hi everyone,

I am new to pystan (on MacOS X).
I am trying to fit a bayesian model developed in this paper:
https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/46001.pdf

My model is the following:

   functions {

  real Hill(real t, real ec, real slope) {
    return 1 / (1 + (t / ec)^(-slope));
  }
  real Adstock(row_vector t, row_vector weights) {
    return dot_product(t, weights) / sum(weights);
  }

}

data {

  int<lower=1> N;
  real<lower=0> Y[N];
  int<lower=1> max_lag;
  row_vector[max_lag] lag_vec;

	row_vector[max_lag] x_tv[N];
	row_vector[max_lag] x_radio[N];
	row_vector[max_lag] x_ooh[N];
	row_vector[max_lag] x_incentive[N];

	real<lower=0> x_nb_users[N];
	real<lower=0> x_is_covid[N];

}

parameters {

	real<lower=0> beta_tv;
	real<lower=0,upper=1> ec_tv;
	real<lower=0> slope_tv;
	real<lower=0,upper=1> retain_rate_tv;
	real<lower=0,upper=max_lag-1> delay_tv;

	real<lower=0> beta_radio;
	real<lower=0,upper=1> ec_radio;
	real<lower=0> slope_radio;
	real<lower=0,upper=1> retain_rate_radio;
	real<lower=0,upper=max_lag-1> delay_radio;

	real<lower=0> beta_ooh;
	real<lower=0,upper=1> ec_ooh;
	real<lower=0> slope_ooh;
	real<lower=0,upper=1> retain_rate_ooh;
	real<lower=0,upper=max_lag-1> delay_ooh;

	real<lower=0> beta_incentive;
	real<lower=0,upper=1> ec_incentive;
	real<lower=0> slope_incentive;

	real<lower=0> gamma_ctrl_nb_users;
	real<lower=0> gamma_ctrl_is_covid;

  real<lower=0> tau;
  real<lower=0> noise_var;

}

transformed parameters {

  real mu[N];
  real cum_effect;
  row_vector[max_lag] lag_weights;

	row_vector[4] cum_effects_hill[N];
	row_vector[4] beta_medias;
	row_vector[2] cum_effects_linear[N];
	row_vector[2] gamma_ctrl;

	beta_medias[1] = beta_tv;
	beta_medias[2] = beta_radio;
	beta_medias[3] = beta_ooh;
	beta_medias[4] = beta_incentive;

	gamma_ctrl[1] = gamma_ctrl_nb_users;
	gamma_ctrl[2] = gamma_ctrl_is_covid;

  for (nn in 1:N) {

		for (lag in 1 : max_lag) {
			lag_weights[lag] <- pow(retain_rate_tv, (lag - 1 - delay_tv) ^ 2);
		}
		cum_effect <- Adstock(x_tv[nn], lag_weights);
		cum_effects_hill[nn, 1] <- Hill(cum_effect, ec_tv, slope_tv);

		for (lag in 1 : max_lag) {
			lag_weights[lag] <- pow(retain_rate_radio, (lag - 1 - delay_radio) ^ 2);
		}
		cum_effect <- Adstock(x_radio[nn], lag_weights);
		cum_effects_hill[nn, 2] <- Hill(cum_effect, ec_radio, slope_radio);

		for (lag in 1 : max_lag) {
			lag_weights[lag] <- pow(retain_rate_ooh, (lag - 1 - delay_ooh) ^ 2);
		}
		cum_effect <- Adstock(x_ooh[nn], lag_weights);
		cum_effects_hill[nn, 3] <- Hill(cum_effect, ec_ooh, slope_ooh);

		cum_effect <- x_incentive[nn, 1];
		cum_effects_hill[nn, 4] <- Hill(cum_effect, ec_incentive, slope_incentive);

		cum_effects_linear[nn, 1] <- x_nb_users[nn];
		cum_effects_linear[nn, 2] <- x_is_covid[nn];

    mu[nn] <- tau + dot_product(cum_effects_hill[nn], beta_medias) + dot_product(cum_effects_linear[nn], gamma_ctrl);

  }
}

model {

	slope_tv ~ gamma(3, 1);
	ec_tv ~ beta(2,2);
	beta_tv ~ normal(0.2, 1);
	retain_rate_tv ~ beta(3,3);
	delay_tv ~ uniform(0, max_lag - 1);

	slope_radio ~ gamma(3, 1);
	ec_radio ~ beta(2,2);
	beta_radio ~ normal(0.17, 1);
	retain_rate_radio ~ beta(3,3);
	delay_radio ~ uniform(0, max_lag - 1);

	slope_ooh ~ gamma(3, 1);
	ec_ooh ~ beta(2,2);
	beta_ooh ~ normal(0.05, 1);
	retain_rate_ooh ~ beta(3,3);
	delay_ooh ~ uniform(0, max_lag - 1);

	slope_incentive ~ gamma(3, 1);
	ec_incentive ~ beta(2,2);
	beta_incentive ~ normal(0.5, 1);

	gamma_ctrl_nb_users ~ normal(0, 0.5);
	gamma_ctrl_is_covid ~ normal(0, 0.25);

  tau ~ normal(0, 1);

  noise_var ~ inv_gamma(0.05, 0.05 * 0.01);

  Y ~ normal(mu, sqrt(noise_var));

}

I am getting the following error and my mu is getting nan value.

Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: normal_lpdf: Location parameter[1] is nan, but must be finite!
(in 'unknown file name' at line 141)
Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.

My print statements in the loop are having the following outputs:

cum_effects_hill: [0,0.0265629,0,0.681911]
cum_effects_linear: [1,1]
dot_product(cum_effects_hill[nn], beta_medias): 1.17828
dot_product(cum_effects_linear[nn], gamma_ctrl): 4.99926
tau: 0.227982
mu (calculated): 6.40552
mu: nan

I am not really sure how to edit my code so that it doesn’t yield a nan for mu.
I would appreciate the community helps on this.

Thanks a lot!

I agree it looks like there shouldn’t be NaNs.

Can you print the entire mu array?

There is also an is_nan function you can use to check if values are NaN programmatically: https://mc-stan.org/docs/2_23/functions-reference/logical-functions.html (so you can check for NaNs without printing everything).

Variables are by default set to NaN, so if they don’t get initialized they’ll end up being NaNs.

1 Like

Thanks for your response bbbales2.

It seems than the first few values of mu are nan.

My mu has no prior. For initialisation, i reduced init_r to 0.1 as I saw it advised in other posts.

cum_effects_hill: [0,0.0390628,0,0.373905]
cum_effects_linear: [1,1]
dot_product(cum_effects_hill[nn], beta_medias): 0.412979
dot_product(cum_effects_linear[nn], gamma_ctrl): 2.00008
tau: 0.999926
mu (calculated): 3.41299

mu[nn]: nan
mu: [nan,nan,nan,nan,nan,2.04153,1.90739,1.56374,1.39082,1.5552,1.98385,2.3
889,2.65159,2.6208,2.67021,2.53714,2.34462,2.32687,2.31707,2.19087,2.68945,
2.51112,2.1334,2.19261,2.54989,2.71771,2.88359,2.75827,2.71561,2.71257,2.67
908,2.72756,2.67384,2.77408,3.02543,3.04823,3.32857,3.58104,3.30385,2.67737
,2.55787,3.23087,3.57896,3.88188,3.76012,3.56111,3.45219,3.44076,3.36908,na
n]

Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: normal_lpdf: Location parameter[1] is nan, but must be finite!
(in ‘unknown file name’ at line 142)

1 Like

The next thing to do is figure out what the other variables are when that is NaN.

Like:

mu[nn] <- tau + dot_product(cum_effects_hill[nn], beta_medias) + dot_product(cum_effects_linear[nn], gamma_ctrl);

if(is_nan(mu[nn])) {
  print(dot_product(cum_effects_hill[nn], beta_medias));
  print(dot_product(cum_effects_linear[nn], gamma_ctrl));
}

Etc.

Thanks bbbales2. Some input variables were indeed nan causing mu to also become nan.

Now my mu has no nan value anymore!

2 Likes

Great! Print is a useful tool for figuring out these NaN/initialization issues!

2 Likes