# Troubles in "rejecting initial value"

**URL:** <https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827>\
**Category:** Modeling\
**Created:** [September 7, 2017, 11:23pm UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827 "2017-09-07T23:23:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yenan](https://avatars.discourse-cdn.com/v4/letter/y/35a633/32.png) [@Yenan](https://discourse.mc-stan.org/u/Yenan)\
**Post date:** [September 7, 2017, 11:23pm UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827/1 "2017-09-07T23:23:36Z")

</div>

Hi all,  
Right now I encounter a problem regarding the Copula Bayesian estimation using Stan.  
I try to estimate the joint probability of rainfall and streamflow and the parameter of Gumbel Copula. I assume the rainfall data comes from a Gamma distribution and the streamflow data comes from a GEV distribution. And I assume that the location parameter of GEV distribution and the shape parameter of Gamma distribution have a linear relationship with time. The code and data are attached.  
After running the Rstan model, I get following error:

> “Rejecting initial value:  
> Log probability evaluates to log(0), i.e. negative infinity.  
> Stan can’t start sampling from this initial value.  
> Initialization between (-5, 5) failed after 100 attempts.  
> Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.”

I think the problem maybe the priors, but the initial values are only rarely necessary. Is there something wrong with what I am trying to do? Could you guys help me have a look? Thanks a lot!

```
> functions{  
> real gev_lpdf(vector xx, vector mu, real sigma, real xi) {
> vector[rows(xx)] z;
> vector[rows(xx)] lp;
> int N;
> N = rows(xx);
> for(n in 1:N){
> z[n] = 1 + (xx[n] - mu[n]) * xi / sigma;
> lp[n] = (1 + (1 / xi)) * log(z[n]) + pow(z[n], -1/xi);
> }
> return -sum(lp) - N * log(sigma);
> }
>  
> real gev_cdf(vector xx, vector mu, real sigma, real xi){ //fl is streamflow data series
> vector[rows(xx)] z;
> vector[rows(xx)] lp;
> int N;
> N = rows(xx);
> for(n in 1:N){
> z[n] = 1 + (xx[n] - mu[n]) * xi / sigma;
> lp[n]= exp(-pow(z[n],-1/xi));
> }
> return sum(lp);
> } 
> real gumbel_copula(real u, real v, real theta) {
> real neg_log_u = -log(u);
> real neg_log_v = -log(v);
> real theta_m1 = theta - 1;
> real temp = neg_log_u ^ theta + neg_log_v ^ theta;
> return theta_m1 * log(neg_log_u) + theta_m1 * log(neg_log_v) 
> + neg_log_u + neg_log_v - pow(temp, inv(theta)) 
> + (-2+2/theta)*log(temp)
> + log1p(theta_m1*pow(temp,-1/theta));
> }
> real gumbel_copula_cdf(real u, real v, real theta){
> real neg_log_u = -log(u);
> real neg_log_v = -log(v);
> real temp = neg_log_u ^ theta + neg_log_v ^ theta;
> return -pow(temp,-1/theta);
> }
> }
> 
> 
> data {
> int<lower=0> N; //number of years
> vector[N] x; //rainfall data series
> vector[N] y; //streamflow data series
> 
> }
> parameters {
> 
> real <lower=0>sigma;
> real xi;
> // real<lower=0> alpha;
> real<lower=0> beta;
> real mu0;
> real beta_mu;
> real alpha0;
> real beta_alpha;
> real<lower=0,upper=1> tau;
> }
> transformed parameters {
> real<lower=1> theta = 1 / (1 - tau);
> vector<lower=0>[N] alpha;
> vector[N] mu; 
> for (n in 1:N)
> mu[n] = mu0 + beta_mu * n;
>    
> for (n in 1:N)
> alpha[n] = alpha0 + beta_alpha * n;
> }
> model {
>    
> vector[N] z;
> // model
>   
> x~gamma(alpha,beta);
> y ~ gev(mu,sigma,xi);
> 
> for (n in 1:N)
> z[n] = gumbel_copula(gamma_cdf(x[n], alpha, beta),
> gev_cdf(y, mu, sigma,xi), theta);
> target += z;
> // priors
> // mu ~ normal(1000,100);
> mu0~gamma(0.001,0.001);
> beta~gamma(0.001,0.001);
> alpha0~gamma(0.001,0.001);
> beta_alpha~gamma(0.001,0.001);
> sigma ~ normal(750,100);
> xi~ uniform(-1,1);
> alpha ~ normal(5,5);
> beta ~uniform(0,0.1);

> }

```

then I run it:

```
> library(rstan)

```

> dat ← read.csv(“C:/Users/Administrator/Desktop/Rstan/data.csv”)  
> dat$N ← NULL  
> initf\<-function()list(mu=1000,sigma=750,xi=0.5,alpha=5,beta=0.05)  
> post ← stan(“F:/paper\_Copula/R\_code/stan/gumbel\_nonstationary.stan”, data = list(N = nrow(dat), x= dat$x, y = dat$y),init = initf,init\_r=5,iter=12000,warmup=2000,thin=10,chains=3)  
> print(post)

[data.csv](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/1X/e3a33cdb58059a6171725b6d1e5705e086cc405a.csv) (1.0 KB)

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 8, 2017, 12:32am UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827/2 "2017-09-08T00:32:58Z")

</div>

Those priors are a classic example of bad BUGS practices, but I don’t think that is the initial problem. I see a bunch of things like

```
Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: validate transformed params: alpha[1] is -0.11941, but must be greater than or equal to 0

```

If `alpha` must be positive, then you need to constrain `alpha0` to ensure that inequality constraint holds by construction.

---

<div class="post-metadata">

**Author:** ![huiying](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@huiying](https://discourse.mc-stan.org/u/huiying)\
**Post date:** [November 7, 2017, 3:26am UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827/3 "2017-11-07T03:26:31Z")

</div>

Hi,

I have encountered the similar problems related to “Rejecting initial value: Error evaluating the log probability at the initial value”. It also gave me many lines such as below:

Exception thrown at line 90: beta\_lpdf: Random variable is 1.08213, but must be less than or equal t 1  
Exception thrown at line 91: beta\_lpdf: Random variable is 2, but must be less than or equal to 1 1  
Exception thrown at line 92: beta\_lpdf: Random variable is 1.99995, but must be less than or equal t 1  
Exception thrown at line 93: beta\_lpdf: Random variable is 1.17511, but must be less than or equal t 1  
Exception thrown at line 93: beta\_lpdf: Random variable is 1.34667, but must be less than or equal t 1  
Exception thrown at line 93: beta\_lpdf: Random variable is 1.71095, but must be less than or equal t 1

These points to my priors, despite having specified boundaries for my priors (i.e. real\<lower=0, upper=2\> or1).

Attached is my Stan code  
[mystancode.stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/b/b785cdfafe29f49d2622b27e95f113a142e03aed.stan) (5.3 KB)

Can someone please advise where did I get wrong?

Thanks !

Huiying

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 7, 2017, 3:45am UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827/4 "2017-11-07T03:45:39Z")

</div>

The beta distribution is defined for random variables between 0 and 1 (not 2).

---

<div class="post-metadata">

**Author:** ![huiying](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@huiying](https://discourse.mc-stan.org/u/huiying)\
**Post date:** [November 7, 2017, 3:51am UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827/5 "2017-11-07T03:51:00Z")

</div>

Yes, but I have redefined the range for my parameters to be estimated or1, or2, or3 and or4, such that their maximum value can be 2.

Is that not possible to alter the beta distribution?

Or is there an alternative way to allow beta distribution to take value greater than 1?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 7, 2017, 5:17am UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827/6 "2017-11-07T05:17:41Z")

</div>

What you perhaps need is the so-called generalized beta distribution

> **[Beta distribution | Four unknown parameters](https://en.wikipedia.org/wiki/Beta_distribution#Four_unknown_parameters_2)**
>
> The procedure is similar to the one followed in the two unknown parameter case. If Y1, ..., YN are independent random variables each having a beta distribution with four parameters, the joint log likelihood function for N iid observations is:

  
which is not implemented in the Stan Math Library but is easy enough for you to implement yourself with a user-defined function in the Stan Language.

---

<div class="post-metadata">

**Author:** ![huiying](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@huiying](https://discourse.mc-stan.org/u/huiying)\
**Post date:** [November 13, 2017, 8:20am UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827/7 "2017-11-13T08:20:03Z")

</div>

Hi there,

I’m trying to implement generalised beta distribution using user-defined function, but having troubles with mismatch of declaration of parameters types. I have attached my Stan code and the error messages.

[mystancode2.stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/9/9bb49358a87e6acf6563d2e7a304e5c799633737.stan) (6.1 KB)

Error messages:  
No matches for:

real ~ genbeta(int, int)

Available argument signatures for genbeta:

real[] ~ genbeta(int, int)

require real scalar return type for probability function.

ERROR at line 100

98: pi3 ~ normal(0.5,1);  
99: pi4 ~ normal(0.5,1);  
100: or1 ~ genbeta(20,20);  
^  
101: or2 ~ genbeta(20,20);

I understood where the error is and have tried and error but can’t seem to find a good solution. My or1-or4 has to be real numbers.

Can someone help please?

Thank you.

Huiying

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [November 21, 2017, 8:03am UTC](https://discourse.mc-stan.org/t/troubles-in-rejecting-initial-value/1827/8 "2017-11-21T08:03:23Z")

</div>

> [@huiying](#):
>
> No matches for:
> 
> real ~ genbeta(int, int)
> 
> Available argument signatures for genbeta:
> 
> real ~ genbeta(int, int)

The error message is telling you what the problem is. You gave the `genbeta_log` function a `real` first argument (namely `or1`) when it is declared to require a `real[]` (array of reals) as the first argument. You either need to rewrite `genbeta_log` to take a single argument or pass it in an array.
