{"action":"created","issue":{"url":"https://api.github.com/repos/stan-dev/stan/issues/1308","labels_url":"https://api.github.com/repos/stan-dev/stan/issues/1308/labels{/name}","comments_url":"https://api.github.com/repos/stan-dev/stan/issues/1308/comments","events_url":"https://api.github.com/repos/stan-dev/stan/issues/1308/events","html_url":"https://github.com/stan-dev/stan/issues/1308","id":58543048,"number":1308,"title":"NaN error message for <lower=0> parameter","user":{"login":"bob-carpenter","id":3383807,"avatar_url":"https://avatars.githubusercontent.com/u/3383807?v=3","gravatar_id":"","url":"https://api.github.com/users/bob-carpenter","html_url":"https://github.com/bob-carpenter","followers_url":"https://api.github.com/users/bob-carpenter/followers","following_url":"https://api.github.com/users/bob-carpenter/following{/other_user}","gists_url":"https://api.github.com/users/bob-carpenter/gists{/gist_id}","starred_url":"https://api.github.com/users/bob-carpenter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bob-carpenter/subscriptions","organizations_url":"https://api.github.com/users/bob-carpenter/orgs","repos_url":"https://api.github.com/users/bob-carpenter/repos","events_url":"https://api.github.com/users/bob-carpenter/events{/privacy}","received_events_url":"https://api.github.com/users/bob-carpenter/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/stan-dev/stan/labels/Bug","name":"Bug","color":"e11d21"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/stan-dev/stan/milestones/16","html_url":"https://github.com/stan-dev/stan/milestones/v2.6.3++","labels_url":"https://api.github.com/repos/stan-dev/stan/milestones/16/labels","id":1026323,"number":16,"title":"v2.6.3++","description":"Next version after v2.6.3.","creator":{"login":"syclik","id":425751,"avatar_url":"https://avatars.githubusercontent.com/u/425751?v=3","gravatar_id":"","url":"https://api.github.com/users/syclik","html_url":"https://github.com/syclik","followers_url":"https://api.github.com/users/syclik/followers","following_url":"https://api.github.com/users/syclik/following{/other_user}","gists_url":"https://api.github.com/users/syclik/gists{/gist_id}","starred_url":"https://api.github.com/users/syclik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/syclik/subscriptions","organizations_url":"https://api.github.com/users/syclik/orgs","repos_url":"https://api.github.com/users/syclik/repos","events_url":"https://api.github.com/users/syclik/events{/privacy}","received_events_url":"https://api.github.com/users/syclik/received_events","type":"User","site_admin":false},"open_issues":40,"closed_issues":17,"state":"open","created_at":"2015-03-17T22:51:26Z","updated_at":"2015-03-25T18:46:37Z","due_on":null,"closed_at":null},"comments":23,"created_at":"2015-02-23T05:07:28Z","updated_at":"2015-03-25T19:18:56Z","closed_at":null,"body":"The following program and data produce errors like this and can't actually sample.\r\n\r\nThe final message is:\r\n\r\n```\r\n No acceptably small step size could be found. Perhaps the posterior is not continuous?\r\nerror occurred during calling the sampler; sampling not done\r\nStan model 'heckman_hete' does not contain samples.\r\n```\r\n\r\nThe interemdiate messages are\r\n\r\n```\r\nSAMPLING FOR MODEL 'heckman_hete' NOW (CHAIN 1).\r\nsigma_eq=0.894267sigma_beta=[0.797875,0.359337,0.184746,2.56627,2.46375,0.196795,0.472598,0.611689,5.98179,1.11878,0.689196,0.300781,1.58627]sigma_eq=nanInformational Message: The current Metropolis proposal is about to be rejected because of the following issue:stan::prob::cauchy_log(N4stan5agrad3varE): Random variable is nan:0, but must not be nan!If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.sigma_eq=0.894267sigma_beta=[0.797875,0.359337,0.184746,2.56627,2.46375,0.196795,0.472598,0.611689,5.98179,1.11878,0.689196,0.300781,1.58627]sigma_eq=nanInformational Message: The current Metropolis proposal is about to be rejected because of the following issue:stan::prob::cauchy_log(N4stan5agrad3varE): Random variable is nan:0, but must not be nan!If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.sigma_eq=0.894267sigma_beta=[0.797875,0.359337,0.184746,2.56627,2.46375,0.196795,0.472598,0.611689,5.98179,1.11878,0.689196,0.300781,1.58627]sigma_eq=nan...\r\n```\r\n\r\n* [ ] fix formatting issues so there are newlines and spaces where necessary\r\n\r\n```\r\ndata {\r\n  int<lower=0> N;           // number of customers\r\n  int<lower=0> M;           // total number of observations\r\n  int<lower=0> nB1;         // number of beta in equation 1\r\n  int<lower=0> nB2;          // number of beta in equation 2\r\n  int<lower=0> nB;\r\n  int<lower=0> nZ;           // number of individual demographic variables plus intercept\r\n  int<lower=0> ID[M];        // index for the observations\r\n  matrix[N,nZ] Z;\r\n  matrix[M,nB1] X1;\r\n  matrix[M,nB2] X2;\r\n  vector[M] Y1;\r\n  vector[M] Y2;\r\n}\r\nparameters {\r\n\r\n  matrix[nB,nZ] mu;                           // prior matrix for beta\r\n  matrix[nB,N] error_beta;                    // a trick on the variance of beta\r\n\r\n  // cholesky_factor_corr[nB] L_Omega_beta;   // the correlation matrix of the two equations\r\n  vector<lower=0>[nB] sigma_beta;             // the s.t.d of the second equation \r\n\r\n  real<lower=-1,upper=1> rho_eq;              // the correlation matrix of the two equations\r\n  real<lower=0> sigma_eq;                     // the s.t.d of the second equation \r\n \r\n}\r\ntransformed parameters {\r\n  \r\n  matrix[N,nB] beta;\r\n  // beta <- Z*mu' + (diag_pre_multiply(sigma_beta,L_Omega_beta)*error_beta)';\r\n  beta <- Z*mu' + (diag_matrix(sigma_beta)*error_beta)';\r\n\r\n}\r\nmodel{ \r\n\r\n  matrix[N,nB1] beta1;\r\n  matrix[N,nB2] beta2;\r\n\r\n  vector[M] lp;\r\n  vector[M] value;\r\n\r\nprint(\"sigma_eq=\",sigma_eq);\r\n  sigma_eq ~ cauchy(0,2);                      // constrain it to be positive   \r\n\r\n  //L_Omega_beta ~ lkj_corr_cholesky(2);       // correlation matrix of beta vector\r\nprint(\"sigma_beta=\",sigma_beta);\r\n  sigma_beta ~ cauchy(0,2);                    // s.t.d. of the covariance matrix (here cause problems)\r\n\r\n  to_vector(mu) ~ normal(0,3); \r\n  to_vector(error_beta) ~ normal(0,1);\r\n\r\n  // transform beta for two equations for each customer\r\n  beta1 <- block(beta,1,1,N,nB1);\r\n  beta2 <- block(beta,1,nB1+1,N,nB2);\r\n\r\n  // calculate the likelihood\r\n  for( m in 1:M){    \r\n    if( Y1[m] == 0 ){\r\n      lp[m] <- normal_cdf_log(-X1[m]*beta1[ID[m]]',0,1);\r\n    }\r\n    else{\r\n      value[m] <- (X1[m]*beta1[ID[m]]'+rho_eq/sigma_eq*(Y2[m]-X2[m]*beta2[ID[m]]'))/(1-rho_eq^2)^0.5;\r\n      lp[m] <- normal_cdf_log(value[m],0,1) - log(sigma_eq) - (Y2[m]-X2[m]*beta2[ID[m]]')^2/2/sigma_eq^2;\r\n    }\r\n  }\r\n\r\n  increment_log_prob( log_sum_exp( lp ) );\r\n}\r\ngenerated quantities {\r\n  \r\n  vector[nB] m_beta;\r\n  vector[nB] sd_beta; \r\n\r\n  // heterogeneity \r\n  for( i in 1:nB ){\r\n    m_beta[i] <- mean( col(beta,i) );\r\n    sd_beta[i] <- sd( col(beta,i) );\r\n  }\r\n\r\n}\r\n```\r\n\r\nAnd here's some R code to simulate and test\r\n\r\n```\r\n###################################################################################\r\n# This code will try to run the homogeneous heckman model using a small\r\n# sample of the customers. The mcmc will be conducted using stan.\r\n# I will first use a simulated data based on the following parameterization \r\n# \r\n# Stan doesn't have the cdf of multivariate normal, so writing the likelihood \r\n# will not work \r\n#################################################################################\r\n\r\n# version 2.6.0 for stan\r\n# version 3.1.2 for R\r\nrequire(rstan)  \r\n\r\nlibrary(MASS)\r\n\r\n#----------------------------------------------------------------------------\r\n#                                     simulate data\r\n#----------------------------------------------------------------------------\r\n\r\n# set-up for observations\r\nN = 100;\r\nnT = sample(10:30,N,replace=T);\r\nM = sum(nT);\r\nID = rep(1:N,nT)     # ID must go from 1 to M \r\n\r\n# numbers of covariates for the two equations\r\nnB1 = 5;\r\nnB2 = 8;\r\nnB = nB1 + nB2;\r\n\r\n# covariance of the two equations\r\nrho = 0.2;\r\nsigma = 2;\r\nCov = matrix(c(1,rho*sigma,rho*sigma,sigma^2),2,2);\r\n\r\n# demographics for hete\r\nzC = matrix(c(2,0.6,0.6,2),2,2);\r\nZ = as.matrix(cbind(1,mvrnorm(N,c(0,0),zC)));\r\nnZ = ncol(Z);\r\n\r\n#  prior for beta\r\nmu = matrix(runif(nB*nZ,-1,1),nB,);\r\nLambda = diag(nB);\r\n\r\n# covariates\r\nX1 = cbind(1, matrix(rnorm(M*(nB1-1)),,nB1-1)); \r\nX2 = cbind(1, matrix(rnorm(M*(nB2-1)),,nB2-1)); \r\n\r\n# simulate actions and preferences\r\nvisit = array(0,M);\r\nspend = array(0,M);\r\nbeta = matrix(0,N,nB);\r\nfor( i in 1:N ){\r\n  beta[i,] = mvrnorm(1, mu%*%Z[i,],Lambda);\r\n  beta1 = beta[1:nB1];\r\n  beta2 = beta[(nB1+1):nB];\r\n  for( t in 1:nT[i] ){\r\n    if( i == 1){\r\n      ind = t;\r\n    }else{\r\n      ind = sum(nT[1:(i-1)]) + t; \r\n    }   \r\n    error = mvrnorm(1,c(0,0),Cov);\r\n    u_it = X1[ind,]%*%beta1 + error[1];\r\n    if( u_it >0 ){\r\n      visit[ind] = 1;\r\n      spend[ind] = X2[ind,]%*%beta2 + error[2] + \r\n        Cov[1,2]*dnorm(X1[ind,]%*%beta1,0,1)/pnorm(X1[ind,]%*%beta1,0,1);\r\n    }    \r\n  }  \r\n}\r\n\r\n\r\n#----------------------------------------------------------------------------------\r\n#                                   define the model\r\n#----------------------------------------------------------------------------------\r\n\r\n\r\n#----------------------------------------------------------------------------------\r\n#                                   run the model\r\n#----------------------------------------------------------------------------------\r\n\r\n#------------------\r\n# initialization\r\n#------------------         \r\n\r\ndata_list = list(N=N, M=M, nB1=nB1, nB2=nB2, nB=nB, nZ=nZ, ID=ID,\r\n                 Z=Z, X1=X1, X2=X2, Y1=visit, Y2=spend);\r\n\r\n#-------------------\r\n#  mcmc\r\n#-------------------\r\n\r\nfit = stan(\"heckman_hete.stan\",data=data_list,iter=10,chains=1)\r\n\r\n#-------------------\r\n#  analysis\r\n#-------------------\r\nprint(fit, pars = c(\"m_beta\", \"sigma_eq\",\"rho_eq\"))\r\n```"},"comment":{"url":"https://api.github.com/repos/stan-dev/stan/issues/comments/86179979","html_url":"https://github.com/stan-dev/stan/issues/1308#issuecomment-86179979","issue_url":"https://api.github.com/repos/stan-dev/stan/issues/1308","id":86179979,"user":{"login":"syclik","id":425751,"avatar_url":"https://avatars.githubusercontent.com/u/425751?v=3","gravatar_id":"","url":"https://api.github.com/users/syclik","html_url":"https://github.com/syclik","followers_url":"https://api.github.com/users/syclik/followers","following_url":"https://api.github.com/users/syclik/following{/other_user}","gists_url":"https://api.github.com/users/syclik/gists{/gist_id}","starred_url":"https://api.github.com/users/syclik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/syclik/subscriptions","organizations_url":"https://api.github.com/users/syclik/orgs","repos_url":"https://api.github.com/users/syclik/repos","events_url":"https://api.github.com/users/syclik/events{/privacy}","received_events_url":"https://api.github.com/users/syclik/received_events","type":"User","site_admin":false},"created_at":"2015-03-25T19:18:56Z","updated_at":"2015-03-25T19:18:56Z","body":"I just checked with the current version of CmdStan and I'm not seeing any issues. Can I close this?\r\n@jl3631, does your model run now?"}}