I have a model where I defined the covariance matrix with 2 parameters that I want to infer. To do so,

I wrote the following stan code (that I’m running with pystan):

data {

int<lower=1> Mn;

int<lower=1> Mm;

int<lower=0> N;

row_vector[Mm] y[N];

matrix[Mm,3Mn] Umodes;Mn)];

vector<lower=0>[Mm] singval;

matrix<lower=0>[Mn,Mn] dij;

}

transformed data {

matrix[Mm,Mm] diagSigma;

matrix[Mm,Mn] Umodes1;

matrix[Mm,Mn] Umodes2;

matrix[Mm,Mn] Umodes3;

diagSigma = diag_matrix(singval);

Umodes1[1:Mm,1:Mn] = Umodes[1:Mm,1:Mn];

Umodes2[1:Mm,1:Mn] = Umodes[1:Mm,(Mn+1):(2

Umodes3[1:Mm,1:Mn] = Umodes[1:Mm,(2Mn+1):(3Mn)];

}

parameters {

row_vector[Mm] lambdaHat;

real var1;

real var2;

}

transformed parameters {

row_vector[Mm] lambdaCoeff=lambdaHatdiagSigma;pcordsigma;

cov_matrix[Mm] sigmareduced; //reduced covariance

{

real l=exp(var1);

real nusq=exp(var2);

matrix[Mn,Mn] pcordsigma=exp(-(1.0/l)dij); // Full covariance for a single coordinatepcordsigma;

matrix[Mm,Mn] V1=Umodes1

matrix[Mm,Mn] V2=Umodes2

matrix[Mm,Mn] V3=Umodes3pcordsigma;(V1*(Umodes1’)+V2*(Umodes2’)+V3*(Umodes3’));

sigmareduced=nusq

}

}

model {

lambdaHat ~ std_normal();

y ~ multi_normal(lambdaCoeff, sigmareduced);

}

generated quantities{

real nu=exp(var2/2);

real l=exp(var1);

}

When tested with a dataset of N=14 samples and, Mn=655, Mm=55 all worked fine. When I tried with a data set still formed by N=14 samples and with Mm=55 but with Mn=13569 I got the following error:

`Exception: std::bad_alloc (in 'shapeInferenceReducedUnboundedLog.stan' at line 31) [origin: bad_alloc]`

I guess the problem comes when stan tries to allocate the memory for the matrix pcordsigma; however, with Mn=13569 I’m expecting that the matrix requires 1.4Gb maximum, while the other data no more than 3 Gb,

hence a total of less than 5Gb. I would exclude my Desktop ran out of memory since it has 31.4 Gb of physical memory (and a swap memory od the same size).

Are there workarounds to run the code on my desktop machine with that problem size?

Thanks,

Cesare