Getting an error "... must be nonnegative!" despite constraining variables to positive values with <lower=0>?

I got an error like this:

SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: von_mises_lpdf: Scale parameter is -1.49167e-154, but must be nonnegative! (in 'anon_model', line 59, column 4 to column 64)

but it seemed strage to me because those variables were already constrained to positive values in tha parameters{} block.
Here is the whole stan code (all comments are masked):

data{
  int N_W; //*
  int N_P; //*
  int N_I; //*
  int K_W; //*
  int K_P; //*
  int K_I; //*
  
  //*
  
  real<lower=-pi(),upper=pi()> Y_W[N_W]; //*
  real<lower=-pi(),upper=pi()> Y_P[N_P]; //*
  real<lower=-pi(),upper=pi()> Y_I[N_I]; //*
  int<lower=1,upper=K_W+K_P+K_I> person_W[N_W];
  int<lower=1,upper=K_W+K_P+K_I> person_P[N_P];
  int<lower=1,upper=K_W+K_P+K_I> person_I[N_I];
}

parameters{
  real<lower=-pi(),upper=pi()> mu_W0; //*
  real<lower=-pi(),upper=pi()> mu_P0; //*
  real<lower=-pi(),upper=pi()> mu_I0; //*
  real<lower=-pi(),upper=pi()> mu_W[K_W]; 
  real<lower=-pi(),upper=pi()> mu_P[K_P]; 
  real<lower=-pi(),upper=pi()> mu_I[K_I]; 

  real<lower=0> sig_mu; //*
  
  real<lower=0> kappa_W0; //*
  real<lower=0> kappa_P0; //*
  real<lower=0> kappa_I0; //*
  real<lower=0> kappa_W[K_W]; 
  real<lower=0> kappa_P[K_P]; 
  real<lower=0> kappa_I[K_I]; 
  
  real<lower=0> sig_kappa; //*
  
  //*
}

model{
  //*
  //*
  //*
  for(k in 1:K_W){
    mu_W[k] ~ normal(mu_W0, sig_mu);
    kappa_W[k] ~ normal(kappa_W0, sig_kappa);
  }
  for(k in 1:K_P){
    mu_P[k] ~ normal(mu_P0, sig_mu);
    kappa_P[k] ~ normal(kappa_P0, sig_kappa);
  }
  for(k in 1:K_I){
    mu_I[k] ~ normal(mu_I0, sig_mu);
    kappa_I[k] ~ normal(kappa_I0, sig_kappa);
  }
  //*
  for(n in 1:N_W){
    Y_W[n] ~ von_mises(mu_W[person_W[n]], kappa_W[person_W[n]]);
  }
  for(n in 1:N_P){
    Y_P[n] ~ von_mises(mu_P[person_P[n]], kappa_P[person_P[n]]);
  }
  for(n in 1:N_I){
    Y_I[n] ~ von_mises(mu_I[person_I[n]], kappa_I[person_I[n]]);
  }
}

Then WHY did negative values sampled?

When simplified this to the following model, it worked (The above one considers differences between three groups, β€˜W’, β€˜P’, and β€˜I’, and the below one doesn’t).
So perhaps the multilevel modeling part is OK but something in other parts goes wrong.

data{
  int N;
  int K; 
  real<lower=-pi(),upper=pi()> Y[N];
  int<lower=1,upper=K> person[N];
}

parameters{
  real<lower=-pi(),upper=pi()> mu0;
  real<lower=-pi(),upper=pi()> mu[K]; 

  real<lower=0> sig_mu;
  
  real<lower=0> kappa0;
  real<lower=0> kappa[K]; 

  real<lower=0> sig_kappa;
}

model{
  for(k in 1:K){
    mu[k] ~ normal(mu0, sig_mu);
    kappa[k] ~ normal(kappa0, sig_kappa);
  }
  for(n in 1:N){
    Y[n] ~ von_mises(mu[person[n]], kappa[person[n]]);
  }
}

I’m really new to RStan, so I may have missed some very simple issue.

This looks like it might be a bug in Stan. Can you share some data that reproduces this error for you?

And can you also post your output from:

if (!require("devtools", quietly = TRUE)) {
  install.packages("devtools")
}
devtools::session_info("rstan")

Thank you so much for your replying!

datForSharing.csv (44.4 KB)

Here is the .csv data to create the error above.
And here is the .R code.
(.stan code has been shared in the first post)

library("rstan")
df <- read.csv(".../datForSharing.csv")
stratified <- stan_model("***.stan")
stanData <- list(
  N_W=sum(df$condition=="W"),
  N_P=sum(df$condition=="P"),
  N_I=sum(df$condition=="I"),
  K_W=length(unique(subset(df,condition=="W")$IDnum)),
  K_P=length(unique(subset(df,condition=="P")$IDnum)),
  K_I=length(unique(subset(df,condition=="I")$IDnum)),
  Y_W=subset(df,condition=="W")$aa,
  Y_P=subset(df,condition=="P")$aa,
  Y_I=subset(df,condition=="I")$aa,
  person_W=as.numeric(subset(df,condition=="W")$IDnum),
  person_P=as.numeric(subset(df,condition=="P")$IDnum),
  person_I=as.numeric(subset(df,condition=="I")$IDnum))
stf_model <- sampling(stratified,data=stanData, chains=1)

The attached .csv data has been cropped from the original one for som reason but the error message is alike:
SAMPLING FOR MODEL β€˜anon_model’ NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: von_mises_lpdf: Scale parameter is nan, but must be nonnegative! (in β€˜anon_model’, line 56, column 4 to column 64)
Chain 1: Rejecting initial value:

Actually, this type of massage can be also found for the original .csv data.
(I don’t really know why errors change slightly with each run.)

Thank you for sharing a code.
Here is my output:

> devtools::session_info("rstan")
─ Session info ─────────────────────────────────────────
 setting  value
 version  R version 4.2.2 (2022-10-31)
 os       macOS Big Sur 11.4
 system   aarch64, darwin20
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Asia/Tokyo
 date     2023-09-24
 rstudio  2022.07.2+576 Spotted Wakerobin (desktop)
 pandoc   2.18 @ /opt/homebrew/bin/pandoc

─ Packages ─────────────────────────────────────────────
 package      * version   date (UTC) lib source
 backports      1.4.1     2021-12-13 [1] CRAN (R 4.2.0)
 BH             1.78.0-0  2021-12-15 [1] CRAN (R 4.2.0)
 callr          3.7.3     2022-11-02 [1] CRAN (R 4.2.0)
 checkmate      2.1.0     2022-04-21 [1] CRAN (R 4.2.0)
 cli            3.6.0     2023-01-09 [1] CRAN (R 4.2.0)
 colorspace     2.1-0     2023-01-23 [1] CRAN (R 4.2.0)
 crayon         1.5.2     2022-09-29 [1] CRAN (R 4.2.0)
 desc           1.4.2     2022-09-08 [1] CRAN (R 4.2.0)
 fansi          1.0.4     2023-01-22 [1] CRAN (R 4.2.0)
 farver         2.1.1     2022-07-06 [1] CRAN (R 4.2.0)
 ggplot2        3.4.0     2022-11-04 [1] CRAN (R 4.2.0)
 glue           1.6.2     2022-02-24 [1] CRAN (R 4.2.0)
 gridExtra      2.3       2017-09-09 [1] CRAN (R 4.2.0)
 gtable         0.3.1     2022-09-01 [1] CRAN (R 4.2.0)
 inline         0.3.19    2021-05-31 [1] CRAN (R 4.2.0)
 isoband        0.2.7     2022-12-20 [1] CRAN (R 4.2.0)
 jsonlite       1.8.4     2022-12-06 [1] CRAN (R 4.2.0)
 labeling       0.4.2     2020-10-20 [1] CRAN (R 4.2.0)
 lattice        0.20-45   2021-09-22 [1] CRAN (R 4.2.2)
 lifecycle      1.0.3     2022-10-07 [1] CRAN (R 4.2.0)
 loo            2.6.0     2023-03-31 [1] CRAN (R 4.2.0)
 magrittr       2.0.3     2022-03-30 [1] CRAN (R 4.2.0)
 MASS           7.3-58.1  2022-08-03 [1] CRAN (R 4.2.2)
 Matrix         1.5-3     2022-11-11 [1] CRAN (R 4.2.0)
 matrixStats    0.63.0    2022-11-18 [1] CRAN (R 4.2.0)
 mgcv           1.8-41    2022-10-21 [1] CRAN (R 4.2.2)
 munsell        0.5.0     2018-06-12 [1] CRAN (R 4.2.0)
 nlme           3.1-161   2022-12-15 [1] CRAN (R 4.2.0)
 pillar         1.8.1     2022-08-19 [1] CRAN (R 4.2.0)
 pkgbuild       1.4.0     2022-11-27 [1] CRAN (R 4.2.0)
 pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 4.2.0)
 prettyunits    1.1.1     2020-01-24 [1] CRAN (R 4.2.0)
 processx       3.8.0     2022-10-26 [1] CRAN (R 4.2.0)
 ps             1.7.2     2022-10-26 [1] CRAN (R 4.2.0)
 QuickJSR       1.0.6     2023-09-12 [1] CRAN (R 4.2.0)
 R6             2.5.1     2021-08-19 [1] CRAN (R 4.2.0)
 RColorBrewer   1.1-3     2022-04-03 [1] CRAN (R 4.2.0)
 Rcpp           1.0.10    2023-01-22 [1] CRAN (R 4.2.0)
 RcppEigen      0.3.3.9.3 2022-11-05 [1] CRAN (R 4.2.0)
 RcppParallel   5.1.5     2022-01-05 [1] CRAN (R 4.2.0)
 rlang          1.0.6     2022-09-24 [1] CRAN (R 4.2.0)
 rprojroot      2.0.3     2022-04-02 [1] CRAN (R 4.2.0)
 rstan        * 2.26.23   2023-09-08 [1] CRAN (R 4.2.0)
 scales         1.2.1     2022-08-20 [1] CRAN (R 4.2.0)
 StanHeaders  * 2.26.28   2023-09-07 [1] CRAN (R 4.2.0)
 tibble         3.1.8     2022-07-22 [1] CRAN (R 4.2.0)
 utf8           1.2.3     2023-01-31 [1] CRAN (R 4.2.0)
 vctrs          0.5.2     2023-01-23 [1] CRAN (R 4.2.0)
 viridisLite    0.4.1     2022-08-22 [1] CRAN (R 4.2.0)
 withr          2.5.0     2022-03-03 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

────────────────────────────────────────────────────────

This is a data-indexing issue that wasn’t being properly caught in older versions of Stan, you’re indexing values greater than the length of some of your arrays and so you’re getting uninitialised/nan results.

Note here in the parameters where you declare mu_W as an array with K_W elements.

parameters {
  ...
  real<lower=-pi(),upper=pi()> mu_W[K_W]; 
}

In your model block, you index mu_W with the person_W array:

model {
  ...
  for(n in 1:N_W){
    Y_W[n] ~ von_mises(mu_W[person_W[n]], kappa_W[person_W[n]]);
  }
}

However, some elements of person_W are greater than K_W, which leads to indexing areas outside of the array.

You can also see this if you change your data declaration to:

data {
  ...
  int<lower=1,upper=K_W> person_W[N_W] ;
}

Which would result in the error:

Error : Exception: mod__namespace::mod_: person_W[39] is 75, but must be less than or equal to 71.000000 (in 'anon_model', line 14, column 2 to column 45)
3 Likes

Thank you very much for sharing your solution.
This is exactly what I needed and I think the problems have been perfectly solved.

Finally the code is like below and can be run without stopping:

library("rstan")
df <- read.csv(".../datForSharing.csv")
stratified <- stan_model("***.stan")

vW <- as.numeric(subset(df,condition=="W")$IDnum)
vW2 <- as.numeric((vW-head(c(0,vW),-1))>0)
pW <- purrr::accumulate(vW2, ~.x + .y)

vP <- as.numeric(subset(df,condition=="P")$IDnum)
vP2 <- as.numeric((vP-head(c(0,vP),-1))>0)
pP <- purrr::accumulate(vP2, ~.x + .y)

vI <- as.numeric(subset(df,condition=="I")$IDnum)
vI2 <- as.numeric((vI-head(c(0,vI),-1))>0)
pI <- purrr::accumulate(vI2, ~.x + .y)

stanData <- list(
  N_W=length(pW),
  N_P=length(pP),
  N_I=length(pI),
  K_W=max(pW),
  K_P=max(pP),
  K_I=max(pI),
  Y_W=subset(df,condition=="W")$aa,
  Y_P=subset(df,condition=="P")$aa,
  Y_I=subset(df,condition=="I")$aa,
  person_W=pW,
  person_P=pP,
  person_I=pI)
stf_model <- sampling(stratified,data=stanData, chains=1)

The essential part is the definitions of person_W, person_P and person_I.
As you pointed out, Y_*[n] in the .stan code provided values that exceeds the range of mu_* and I fixed it by transforming vW to pW, vP to pP, and vI to pI.
(If vW is {2 3 7 7 10 10 10 11}, pW becomes {1 2 3 3 4 4 4 5} that each value don’t exceed the length of those vecs)

Ns and Ks are also modified but it is just for readability, so there is no change of values.

I had no idea that the error messages I’ve got said about a problem of index.
Thank you so much for rescuing me from the pit.

1 Like