# Covariance matrix priors

**URL:** <https://discourse.mc-stan.org/t/covariance-matrix-priors/33491>\
**Category:** Modeling\
**Tags:** techniques, rstan\
**Created:** [December 3, 2023, 3:17pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-priors/33491 "2023-12-03T15:17:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![levis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/levis/32/8315_2.png) [@levis](https://discourse.mc-stan.org/u/levis)\
**Post date:** [December 3, 2023, 3:17pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-priors/33491/1 "2023-12-03T15:17:00Z")

</div>

i am trying to study whether the age of participants covaries with other hyperparameters with the following model below:

I have a prior on L ~eta(23) and then separately set L(3,2)~beta(9,1) to see how a strong correlation between age and b1 effects the other parameters.

But for some reason, this had no effect on the model fits (parameters fits were the same with or without setting L).

any insight into what im missing?

thanks in advance

```no-highlight
data {

    // Metadata
    int<lower=1> N; 
    int<lower=1> NJ; // number of total subjects; 
// Number of total observations
    array[N] int<lower=1> J; // Subject-indicator per observation
    array[N] int<lower=1> K; // Bandit-indicator per observation
    vector[NJ] age_zscored; // Z-scored age
    vector[NJ] age_squared_zscored; // Z-scored age

    // Data
    array[N] int<lower=0, upper=1> Y; // Response (risky = 1, certain = 0)
    array[N] int<lower=0, upper=1> R; // Outcome (reward = 1, nonreward = 0)

}
transformed data {

   // int NJ = max(J); // Number of total subjects
    int NK = max(K); // Number of total bandits
}
parameters {

    // Participant parameters
    vector[2] theta_mu; //subject means ; 
    matrix[2,NJ] theta_pr_minus2;  
    vector<lower=0>[2] sigma;  
    cholesky_factor_corr[3] L;  

}
transformed parameters {
    matrix[3,NJ] theta_pr;
    vector<lower=0>[3] sigma_pr;
    sigma_pr[3]=1;
    sigma_pr[1:2]=sigma;
    theta_pr[1:2,]=theta_pr_minus2;
    theta_pr[3,] = to_row_vector(age_zscored);
    vector[NJ] b1; // Inverse temperature
    vector[NJ] a1; // Learning rate (pos)

    // Construction block
    {
    
    // Rotate random effects
    matrix[NJ,3] theta = transpose(diag_pre_multiply(sigma_pr, L) * theta_pr);

    
    // Construct random effects
    b1 = (theta_mu[1] + theta[,1]) * 10;
    a1 = Phi_approx(theta_mu[2] + theta[,2]);

    }

}
model {

    // Initialize Q-values

    array[NJ, NK] real Q = rep_array(0.5, NJ, NK);

    // Construct linear predictor
    vector[N] mu;
    for (n in 1:N) {
    
        // Compute (scaled) difference in expected values
        mu[n] = b1[J[n]] * (Q[J[n],K[n]] - 0.5);
        
        // Compute prediction error
        real delta = R[n] - Q[J[n],K[n]];
        
        // Assign learning rate
        real eta = a1[J[n]];
        
        // Update state-action values
        Q[J[n],K[n]] += eta * delta;
        
    }
    
    // Likelihood
    target += bernoulli_logit_lpmf(Y | mu); 
    
    // Priors
    target += normal_lpdf(theta_mu | 0, 2);
   // target += std_normal_lpdf(to_vector(theta_pr_minus2));
    target += std_normal_lpdf(to_vector(theta_pr_minus2));

    target += student_t_lpdf(sigma | 3, 0, 1);
   // age_zscored ~ normal(predicted_age,.05);
  // age_squared_zscored ~ normal(predicted_age_squared,.05);
     target += lkj_corr_cholesky_lpdf(L | 23);
  // L[2,1] ~ beta(9,1);
     L[3,2] ~ beta(9,1);

          // prior correlation

}

generated quantities {
  
  matrix[3,3] Omega = multiply_lower_tri_self_transpose(L);
}

```

---

<div class="post-metadata">

**Author:** ![Corey.Plate](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/corey.plate/32/20600_2.png) [@Corey.Plate](https://discourse.mc-stan.org/u/Corey.Plate)\
**Post date:** [December 4, 2023, 5:29pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-priors/33491/2 "2023-12-04T17:29:33Z")

</div>

Statements in the `model` block are executed sequentially and you have the sampling statements for `L` after the `lkj_corr_cholesky_lpdf()` function that calls them.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [December 4, 2023, 5:39pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-priors/33491/3 "2023-12-04T17:39:45Z")

</div>

@Corey.Plate the order of the below statements makes no difference in this Stan program. `L[3,2] ~ beta(9,1);` is just syntactic sugar for `target += beta_lpdf(L[3,2] | 9, 1)`. Sampling statements in Stan are not like sampling statements in BUGS/JAGS, where the variables are not populated with values prior to being sampled.

> [@levis](#):
>
> ```no-highlight
> target += lkj_corr_cholesky_lpdf(L | 23);
> // L[2,1] ~ beta(9,1);
> L[3,2] ~ beta(9,1);
> 
> ```

---

<div class="post-metadata">

**Author:** ![Corey.Plate](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/corey.plate/32/20600_2.png) [@Corey.Plate](https://discourse.mc-stan.org/u/Corey.Plate)\
**Post date:** [December 4, 2023, 5:47pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-priors/33491/4 "2023-12-04T17:47:01Z")

</div>

Sorry about that, I should have linked to my [source](https://discourse.mc-stan.org/t/correct-ordering-of-lines-in-the-model-block/661). I did not realize that doesn’t apply to sampling statements

---

<div class="post-metadata">

**Author:** ![levis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/levis/32/8315_2.png) [@levis](https://discourse.mc-stan.org/u/levis)\
**Post date:** [December 4, 2023, 6:00pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-priors/33491/5 "2023-12-04T18:00:41Z")

</div>

Thanks so much Corey - you’re saying just changing the order so that L(3,1)~ beta(9,1) is sampled prior to lkj\_corr\_cholesky\_lpdf()  
will allow both to be considered?

best,  
Levi

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [December 4, 2023, 6:08pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-priors/33491/6 "2023-12-04T18:08:46Z")

</div>

It’s true that the statements are executed in order, but since addition is commutative it does not matter the order in which we increment the target.

> [@levis](#):
>
> Thanks so much Corey - you’re saying just changing the order so that L(3,1)~ beta(9,1) is sampled prior to lkj\_corr\_cholesky\_lpdf()  
> will allow both to be considered?

This will not affect inference
