Computing Variance Covariance Matrix in a Loop in Rstan

data {
    int<lower=0> N;
    int<lower=0> K;
    matrix[N,K] y;
    matrix[N,K] X;
   
  }
   parameters {
     cov_matrix[K] lambda_hat;
     cov_matrix[K] sigma_0;
     cov_matrix[K] L;
     vector[K] a;
   }
   transformed parameters {
     matrix[K,N] mu;
     vector[4] v;

     for (i in 1:K)
       v[i]=1;
     
     for (i in 1:N)
       mu[,i]=lambda_hat*X[i,]';
       

   }
   model {
     //sigma_0~ inv_wishart(K,diag_matrix(v));
     lambda_hat~ wishart(K,diag_matrix(v));
     a~ multi_normal(v,diag_matrix(v));
     
     for (t in 100:N)
       y[t,]'~ multi_normal(a+mu[,t],diag_matrix(v));
   }

In each iteration in t, i.e.,

  for (t in 100:N)
       y[t,]'~ multi_normal(a+mu[,t],diag_matrix(v));

I want to compute the variance covariance matrix of y[t,]’, Var(y[1:t,]’)=H(t), where H(t) is data-dependent (data on y upto time t), is there any way of computing each H(t) based on data.
Please help me out.

My R code corresponding to data is,

data1<-data[order(nrow(data):1),]
prices<-data1[,c(5,7,9,11)]
orderflow<-data1[,c(6,8,10,12)]

dataList =  list(X=as.matrix(orderflow), y=as.matrix(prices), N=48614, K=ncol(orderflow))
1 Like

I don’t quite follow; when you use the syntax y ~ multi_normal( x, z);, you are specifying the value of the second argument to multi_normal() as your covariance matrix. So in your code, diag_matrix(v) is your covariance matrix. Are you saying that you want to expand this model (which constrains the off-diagonals to zero) to do inference on the off-diagonal elements as possibly-non-zero?

1 Like

@mike-lawrence
I am actually interested in building a time dependent Bayesian Hierarchical Model.
In each time point(denoted by the for loop corresponding to t), I am willing to allocate time variant variance-covariance matrix for my Multiple Response vector Y. So essentially here Y[t,] is the response values corresponding to 4 response variables and I am interested in modelling a Time-dependent variance covariance matrix i.e. V(Y[t,]) is not necessarily equal to V[Y[t-1,]].