My response variable is counts [B, T] where at each time period, there are B categories whose

probability being in bin b at time t is theta[b,t]

theta[b,t] is modelled by the following function

```
for(b in 1:B){
for(t in 1:tt){
theta[b,t] = lambda[t] * ( normal_cdf(int_bounds[(b+1),t], y_mean[t,1], sigma[1])- normal_cdf(int_bounds[b,t], y_mean[t,1], sigma[1]) ) +
(1-lambda[t]) * (normal_cdf(int_bounds[(b+1),t], y_mean[t,2], sigma[2])- normal_cdf(int_bounds[b,t], y_mean[t,2], sigma[2]));
}
} //raw p_bt and we need to standardise it
for(b in 1:B){
for(t in 1:tt){
theta_std[t,b] = theta[b,t] /sum(theta[,t]) ;
}
}
```

int_bounds are data, y_mean, sigma lambda are parameters to be estimated where lambda is a probability between (0,1).

theta[b,t] is unnormalised and thus I normalised and called it theta_std [b,t] before using it in the multinomial likelihood function

However, I keep getting the error message that theta_std is not a valid simplex. I debugged this problem by printing out theta and theta_std. I found that sometimes theta is completely fine but there are times they are estimated as all zeros and the resulting normalised theta_std to be na, causing divergent transitions in my model. I am a bit confused about why it can be estimated fine sometimes while other times it cannot and perhaps some ways that I can use to fix my model to solve this problem?

**output from the print**

```
Iteration: 1 / 1 [100%] (Sampling)
theta=[[0.0128309,0.0246669,0.0105406,0.00857686,0.00618963,0.00339627,0.00272815,0.0162677,0.00302745,0.0166727,0.0176373,0.030756,0.0394797,0.0273606,0.0333928,0.0346152,0.0385122,0.0296332,0.033669,0.0289281,0.0433364,0.0427811,0.0327213,0.0314117],[0.00964845,0.0203007,0.00773848,0.00690544,0.00546539,0.00301671,0.00216195,0.0120791,0.0024735,0.0142189,0.0149331,0.0253678,0.0329118,0.0195693,0.0271032,0.0250392,0.0313865,0.0226862,0.0261797,0.0193635,0.0322829,0.036499,0.025693,0.0244181],[0.00715083,0.0164166,0.00560917,0.0055164,0.00480449,0.00267197,0.00170794,0.00876152,0.00201405,0.0120003,0.0125237,0.020574,0.0267834,0.0135102,0.0215668,0.0173852,0.0249821,0.0169334,0.0198747,0.0124775,0.0233958,0.0304668,0.0197771,0.0185765],[0.00523892,0.0130455,0.00402374,0.00437413,0.00420473,0.00235985,0.00134597,0.00620548,0.0016354,0.0100229,0.0104067,0.0164335,0.0212844,0.00904429,0.0168537,0.011637,0.0194517,0.0123669,0.0147845,0.00781909,0.0165932,0.0248802,0.0149295,0.0138428],[0.0... <truncated>
theta_std=[[0.331763,0.249476,0.184896,0.135461,0.0984047],[0.291515,0.239915,0.194012,0.154173,0.120384],[0.342518,0.251462,0.18227,0.130752,0.0929975],[0.297639,0.239637,0.191433,0.151794,0.119497],[0.254428,0.224657,0.197491,0.172838,0.150587],[0.251148,0.223081,0.197588,0.174507,0.153676],[0.303038,0.240146,0.189715,0.149508,0.117593],[0.341739,0.253748,0.184055,0.13036,0.0900979],[0.289004,0.236123,0.192264,0.156118,0.12649],[0.272434,0.232339,0.196086,0.163775,0.135366],[0.275279,0.233073,0.195466,0.162425,0.133757],[0.289943,0.239147,0.193955,0.154922,0.122033],[0.288218,0.24027,0.195529,0.155385,0.120599],[0.362929,0.259579,0.179208,0.119969,0.0783146],[0.298503,0.242279,0.192788,0.150657,0.115772],[0.359723,0.260208,0.180668,0.120932,0.0784688],[0.298161,0.242993,0.193411,0.150594,0.114841],[0.327476,0.250706,0.187131,0.136667,0.09802],[0.319683,0.248573,0.188707,0.140376,0.10266],[0.394096,0.263795,0.169985,0.106522,0.0656021],[0.340794,0.25387,0.183983,0.130488,0.0908652],[0... <truncated>
theta=[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
theta_std=[[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan],[nan,nan,nan,nan,nan]]
In file included from C:/Program Files/R/R-3.3.1/library/BH/include/boost/config.hpp:39:0,
from C:/Program Files/R/R-3.3.1/library/BH/include/boost/math/tools/config.hpp:13,
from C:/Program Files/R/R-3.3.1/library/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from C:/Program Files/R/R-3.3.1/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from C:/Program Files/R/R-3.3.1/library/StanHeaders/include/stan/math/rev/core.hpp:12,
from C:/Program Files/R/R-3.3.1/library/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/Program Files/R/R-3.3.1/library/StanHeaders/include/stan/math.hpp:4,
from C:/Program Files/R/R-3.3.1/library/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file211039751bd1.cpp:8:
C:/Program Files/R/R-3.3.1/library/BH/include/boost/config/compiler/gcc.hpp:186:0: warning: "BOOST_NO_CXX11_RVALUE_REFERENCES" redefined
# define BOOST_NO_CXX11_RVALUE_REFERENCES
^
<command-line>:0:0: note: this is the location of the previous definition
SAMPLING FOR MODEL 'test_classical_gauss_mix_bspline_hist' NOW (CHAIN 1).
Rejecting initial value:
Error evaluating the log probability at the initial value.
validate transformed params: theta_std[k0__] is not a valid simplex. sum(theta_std[k0__]) = nan, but should be 1
```

Any suggestions are greatly appreciated,

Thanks,

Jaslene

[edit: escaped code]