Could someone help me understand the memory usage of this model, that does lots of matrix exponential calculations serially without storing them?
On either Windows (Rstudio), or on Linux, using rstan, memory goes up from a baseline of 200MB to arond 1.7 GB while the sampling is running. Is this to be expected, if so, what is it all being used for?
The memory use seems to scale with the size of the data (5000 here), and the memory appears to not be freed when the sampler stops. Number of iterations doesn’t seem to affect it. Optimisation is a little bit cheaper, but still uses about 1.1GB.
This is an artificial model to illustrate the issue - in reality Q will be different for each i, so I’ll need to recalculate those exponentials. I’m trying to determine the scalability of continuous-time Markov models that look somewhat like this.
Thanks,
Chris
library(rstan)
mtest <- stan_model(model_code="data {
int<lower=1> nobs;
array[nobs] int<lower=1,upper=5> state;
}
parameters {
array[10] real<lower=0> q;
}
transformed parameters {
matrix[5,5] Q = rep_matrix(0, 5, 5);
matrix[5,5] P;
real loglik=0;
Q[1,2] = q[1]; Q[1,5] = q[2]; Q[2,1] = q[3]; Q[2,3] = q[4]; Q[2,5] = q[5];
Q[3,2] = q[6]; Q[3,4] = q[7]; Q[3,5] = q[8]; Q[4,3] = q[9]; Q[4,5] = q[10];
for (k in 1:5) {
Q[k,k] = - sum(Q[k,1:5]); // constrain rows to add to zero
}
for (i in 2:nobs){
P = matrix_exp(Q);
loglik += categorical_lpmf(state[i] | to_vector(P[state[i-1],]));
}
}
model {
target += loglik;
for (i in 1:10) {
q[i] ~ lognormal(0,2);
}
}
")
Q5 <- rbind(c(0, 1, 0, 0, 1), c(1, 0, 1, 0, 1), c(0, 1, 0, 1, 1), c(0, 0, 1, 0, 1), c(0, 0, 0, 0, 0))
diag(Q5) <- -rowSums(Q5)
P <- expm::expm(Q5)
nobs <- 5000
## simulate data from a continuous-time Markov model with equally-spaced observations
state <- numeric(nobs)
set.seed(1)
state[1] <- 1
for (i in 2:nobs){
state[i] <- sample(1:5, size=1, prob=P[state[i-1],])
}
opt <- optimizing(mtest, data=list(nobs=nobs, state=state))
sam <- sampling(mtest, data=list(nobs=nobs, state=state),
chains=1, iter=100)