Differences in Stan code between Pystan and ArviZ tutorials

I am an absolute beginner in Stan, and I was looking at the “eight schools” example that is referenced in both the Pystan and the ArviZ “getting started” pages. I noticed that they implement the same model in different ways. Can anyone walk me through the differences between these two models?

Pystan:

    schools_code = """
    data {
      int<lower=0> J;         // number of schools
      real y[J];              // estimated treatment effects
      real<lower=0> sigma[J]; // standard error of effect estimates
    }
    parameters {
      real mu;                // population treatment effect
      real<lower=0> tau;      // standard deviation in treatment effects
      vector[J] eta;          // unscaled deviation from mu by school
    }
    transformed parameters {
      vector[J] theta = mu + tau * eta;        // school treatment effects
    }
    model {
      target += normal_lpdf(eta | 0, 1);       // prior log-density
      target += normal_lpdf(y | theta, sigma); // log-likelihood
    }
    """

ArviZ:

    schools_code = """
    data {
      int<lower=0> J;
      array[J] real y;
      array[J] real<lower=0> sigma;
    }
    
    parameters {
      real mu;
      real<lower=0> tau;
      array[J] real theta;
    }
    
    model {
      mu ~ normal(0, 5);
      tau ~ cauchy(0, 5);
      theta ~ normal(mu, tau);
      y ~ normal(theta, sigma);
    }
    generated quantities {
        vector[J] log_lik;
        vector[J] y_hat;
        for (j in 1:J) {
            log_lik[j] = normal_lpdf(y[j] | theta[j], sigma[j]);
            y_hat[j] = normal_rng(theta[j], sigma[j]);
        }
    }
    """

Thank you!

1 Like

The Arviz model is using the new Stan language syntax for arrays.

I would suggest checking out CmdStanPy which also works with ArviZ. A nice discussion of the differences between the two is here: CmdStanPy 1.0 - #3 by WardBrian

1 Like