# Running code return error when add Loglikelihood to generated quantities block

**URL:** <https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950>\
**Category:** Modeling\
**Created:** [December 26, 2020, 8:58pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950 "2020-12-26T20:58:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroush](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@soroush](https://discourse.mc-stan.org/u/soroush)\
**Post date:** [December 26, 2020, 8:58pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/1 "2020-12-26T20:58:00Z")

</div>

I want to add loglikelihood to generated quantities to my stan code :

```stan
functions {  
  real E_Riess(real zz, real om,real w,real w_a) {
    return 1/ sqrt(om*(1+zz)^3
                    +(1-om)*exp(-3*w_a/(1.0+zz))*(1+zz)^(3*(1+w+w_a)));
  }
    real loglikelihood_lpdf(vector mu, vector muth, matrix invCov) {
    real logL;
    logL = ((mu-muth)')*(invCov*(mu-muth));
    logL = logL*-0.5;
    return logL;
  }
}
  data {
    int<lower=1> nobs;                        
    vector[nobs] mu;                               
     vector[nobs] z; 
    cov_matrix[nobs] invCov;
}
transformed data { 

}
parameters{
      real<lower=0, upper=1.0> om;    
      real<lower=0> sigint;        
      real<lower=-2, upper=.1> w;    
      real<lower=-2, upper=.1> w_a;
}
transformed parameters{                                 
      vector[nobs] mu_th;
     for (i in 1:nobs) { 
     mu_th[i] = E_Riess(z[i],om,w,w_a);
}
}
model {
   target += loglikelihood_lpdf(mu| mu_th, invCov);
}

generated quantities{
  vector[nobs] log_lik;

    for (i in 1:nobs){
     log_lik[i] =loglikelihood_lpdf(mu[i]| mu_th[i],invCov);
    }

}

```

When I run this code I get this error:

```
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for: 

  loglikelihood_lpdf(real, real, matrix)

Available argument signatures for loglikelihood_lpdf:

  loglikelihood_lpdf(vector, vector, matrix)

 error in 'modelf6c62144cd7_0466e8e40e3cc2338230babdeb5630ac' at line 43, column 60
  -------------------------------------------------
    41: 
    42: for (i in 1:nobs){
    43: log_lik[i] =loglikelihood_lpdf(mu[i]| mu_th[i],invCov);
                                                                   ^
    44: }

```

I used loglikelihood function from ([https://github.com/hanyao8/CosmoBayes2018](https://github.com/hanyao8/CosmoBayes2018)).

---

<div class="post-metadata">

**Author:** ![caesoma](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/caesoma/32/19767_2.png) [@caesoma](https://discourse.mc-stan.org/u/caesoma)\
**Post date:** [December 27, 2020, 5:00pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/2 "2020-12-27T17:00:48Z")

</div>

It seems like you are defining `loglikelihood_lpdf` inside another function, so it wouldn’t be available outside of that scope. Try specifying it as a standalone function, which I think is the best practice for writing functions anyway (although in this case the `E_Riess` function is called only once so it wouldn’t make a practical difference).

---

<div class="post-metadata">

**Author:** ![soroush](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@soroush](https://discourse.mc-stan.org/u/soroush)\
**Post date:** [December 27, 2020, 8:06pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/3 "2020-12-27T20:06:50Z")

</div>

Thanks for your reply. But if I used stan function `multi_normal_lpdf` instead of `loglikelihood_lpdf` I get the same error. What I don’t understand is why both arguments assign real even though I declared both as vectors.

---

<div class="post-metadata">

**Author:** ![Cynon](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Cynon](https://discourse.mc-stan.org/u/Cynon)\
**Post date:** [December 27, 2020, 11:07pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/4 "2020-12-27T23:07:09Z")

</div>

In generated quantities you are running your function for each i separately. You defined the lpdf to ingest vectors, but line 43 passes reals rather than vectors. You need to write a different function that ingests reals and is not vectorised in your case.  
I suggest something like;  
real loglikelihood\_lpdf(int i, real mu, real muth, matrix sigma) {

```
return normal_lpdf(mu|muth,sigma[i,i]);

```

}

as far as I can tell, you want the marginal likelihood. but i suggest that this makes little sense if you are using a multivariate normal. In that case the log\_lik should be defined as a scalar because you only get a loglikelihood at each point in your n dimensional space (nobs number of dimensions). So only one loglikelihood for each nobs dimensional vector.

I hope that helps.

---

<div class="post-metadata">

**Author:** ![caesoma](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/caesoma/32/19767_2.png) [@caesoma](https://discourse.mc-stan.org/u/caesoma)\
**Post date:** [December 28, 2020, 1:52am UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/5 "2020-12-28T01:52:03Z")

</div>

Yes. As the message says, it is about the signature. It requires type `vector` for the first two arguments. You can probably fix it by using [mixed operation](https://mc-stan.org/docs/2_25/functions-reference/mixed-operations.html) functions like `to_vector`.

---

<div class="post-metadata">

**Author:** ![Cynon](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Cynon](https://discourse.mc-stan.org/u/Cynon)\
**Post date:** [December 28, 2020, 7:13pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/6 "2020-12-28T19:13:01Z")

</div>

potentially, you are asking for an evaluation of the lpdf at a point in parameter space that assumes you are at the mean of the multidimensional normal for all observations bar one i.e vector\_x[j != i] = mu\_th[j], vector\_x[i] = mu[i]. then log\_lik = loglikelihood\_lpdf(vector\_x | mu\_th,invCov);  
It is just a little unclear what you are trying to achieve in the generated quantities section.

---

<div class="post-metadata">

**Author:** ![soroush](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@soroush](https://discourse.mc-stan.org/u/soroush)\
**Post date:** [December 28, 2020, 10:27pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/7 "2020-12-28T22:27:31Z")

</div>

Thanks for your reply. I tried to use your function but unfortunately, I get an error. without generated quantities block my code run correctly but for model checking and leave one out cross-validation approach, I need to calculate loglikelihood. Here is my code and data:

```
library(rstan)
Cov_inv = read.table(invcovRiess.dat'))
zmu_data = read.table(Riessinv.dat'))
data1 = list(nobs = 6, z = zmu_data[,1], mu = zmu_data[,2],invCov = Cov_inv)

model_code1= "
functions {  
  real E_Riess(real zz, real om,real w,real w_a) {
   
    return 1/ sqrt(om*(1+zz)^3
                    +(1-om)*exp(-3*w_a/(1.0+zz))*(1+zz)^(3*(1+w+w_a)));
  }
    
  real loglikelihood_lpdf(vector mu, vector muth, matrix invCov) {
    real logL;
    logL = ((mu-muth)')*(invCov*(mu-muth));
    logL = logL*-0.5;
     return logL;
  }

}
  data {
    int<lower=1> nobs;                        
    vector[nobs] mu; 
    vector[nobs] z; 
    cov_matrix[nobs] invCov;
    cov_matrix[nobs] Cov;
}
transformed data {

}
parameters{
      real<lower=0, upper=1.0> om;    
      real<lower=0> sigint;        
      real<lower=-2, upper=.1> w;    
      real<lower=-2, upper=.1> w_a;
}
transformed parameters{                                 
       vector[nobs] mu_th;
      for (i in 1:nobs) { 
          mu_th[i] = E_Riess(z[i],om,w,w_a);
     }
}
model {
   target += loglikelihood_lpdf(mu| mu_th, invCov);
}

generated quantities{

}
"

fit <- stan(model_code = model_code1,
                data = data1,
                seed = 42,
                chains = 1,
                iter =1000,
                cores= 1,
                warmup=500,control = list(adapt_delta = 0.99999)
)
print(fit,pars=c("om","w","w_a"),intervals=c(0.025, 0.975), digits=3)

```

---

<div class="post-metadata">

**Author:** ![soroush](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@soroush](https://discourse.mc-stan.org/u/soroush)\
**Post date:** [December 28, 2020, 10:30pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/9 "2020-12-28T22:30:01Z")

</div>

#invcovRiess.dat

```
4780.91114701 -2248.9991722 -2371.43843245 -1233.50074943 -164.79641424 -15.13670414
-2248.9991722 5212.92722488 1351.05006976 -277.83986982 -13.90103618 127.03339451
-2371.43843245 1351.05006976 2428.4352452 680.52827273 -7.83016316 28.23181015
-1233.50074943 -277.83986982 680.52827273 1597.26135705 312.99619915 -55.41571448
-164.79641424 -13.90103618 -7.83016316 312.99619915 458.89779762 36.37939321
-15.13670414 127.03339451 28.23181015 -55.41571448 36.37939321 171.07692591

```

---

<div class="post-metadata">

**Author:** ![soroush](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@soroush](https://discourse.mc-stan.org/u/soroush)\
**Post date:** [December 28, 2020, 10:31pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/10 "2020-12-28T22:31:04Z")

</div>

#Riessinv.dat

```
0.07 1.003 0.023 
0.2 0.901 0.017 
0.35 0.887 0.029 
0.55 0.732 0.033 
0.9 0.656 0.052 
1.5 0.342 0.079

```

---

<div class="post-metadata">

**Author:** ![Cynon](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Cynon](https://discourse.mc-stan.org/u/Cynon)\
**Post date:** [December 29, 2020, 12:49pm UTC](https://discourse.mc-stan.org/t/running-code-return-error-when-add-loglikelihood-to-generated-quantities-block/19950/11 "2020-12-29T12:49:40Z")

</div>

my function was not designed to work. it was to provide guidance to you regarding what you are trying to achieve in generated quantities.

Now that you have removed generated quantities, there is no issue with your code. Your loglikelihood\_lpdf function calculates the loglikelihood as you desired and it is stored in the model results as you need.

the loglikelihood is returned for each MCMC step as part of the final results and you can extract it several ways e.g. the extract() function.

So you don’t need the LL to be calculated in the generated quantities block as far as I can tell.
