Hey y’all,

How can I calculate LOO and WAIC for GEV distribution in Stan?

# LOOCV for GEV in Stan

**bgoodri**#2

- In the functions block, write a function to evaluate the GEV log-density
- In the generated quantities block, call it on each observation and put the result in the n-th element of
`log_lik`

, which is declared as a real array - Call
`loo`

on the object produced by`stan`

- Forget about WAIC.

**ahartikainen**#3

ArviZ on a Python side has implementation of loo and waic.

(loo is the way to go).

Please double check the scaling if you need to compare models against some paper etc.

**Lazhar**#4

Thank you so much Prof Goodri for your help.

You find my r code bellow.

How can I write the function of GEV log-density in the generated quantities block?

#### R code for GEV in Stan

functions{

real gev_log (real y, real loc, real scale, real shape){

real z;

real inv_shape;

inv_shape = 1.0/shape;

z = 1 + (y - loc) * shape / scale;

return -log(scale) - (1 + inv_shape)*log(z) - pow(z,-inv_shape);
}
// GEV log pdf
real gev_v_lpdf(vector y, vector mu, real sigma, real xi){
vector[rows(y)] z;
vector[rows(y)] logp;
vector[rows(y)] zi;
z = 1 + xi*(y - mu)/sigma;

for(i in 1:rows(y)){

zi[i] = z[i]^(-1/xi);

}

logp = log(sigma) + (1 + 1/xi)*log(z) + zi;

return -sum(logp);

}

}

data {

int<lower=0> ns;

int<lower=0> ns_c;

int<lower=0> ns_m;

int<lower=0> nt;

int<lower=0> np;

matrix [nt,ns] y; //matrix containing data

matrix [nt,np] x;//matrix containing predictors

}

}

parameters {

vector <lower=0, upper=100> [ns] mu;

vector <lower=0, upper=15> [ns] sigma;

vector <lower=-0.5, upper=0.5> [ns] xi;

}

model {

// Priors

// mu ~ normal(0,10);

// sigma ~ normal(0,10);

// xi ~ uniform(0,0.3);

// Complete stations

for (i in 1:(ns_c+ns_m)){

if(i <= ns_c){

y[,i] ~ gev_v(mu[i],sigma[i],xi[i]);

}else{

// Incomplete stations

for(t in 1:nt)

if(y[t,i] != -99)

y[t,i] ~ gev(mu[i],sigma[i],xi[i]);

}

}

}

**bgoodri**#5

It is explained in the vignette

http://mc-stan.org/loo/articles/loo2-with-rstan.html