I am trying to compute the complexity of my model using rStan. I have two nested integrals to compute which are \int_{m} P(m|x) (\int_{x} P(y|m,x) P(x)) dm.

There are 2 for loops in my Stan model that are related to model complexity. In addition to that, Stan computes the parameters where they have a complexity that I don’t know how to compute. Here is my model:

```
model_code <- "
functions {
real get_p_y_given_m_rng(real m, real y, real mu_x, real sigma_x, real b_x_y, real b_m_y, real sigma_y){
// Estimate inner integral
real x_sample;
real lp_x_sample;
real lp_y_x_m_sample;
real p_y_m_over_x;
p_y_m_over_x = 0;
for(i in 1:100){ // This is a number of samples for the inner integral. Complexity depends on this number.
x_sample = normal_rng(mu_x, sigma_x);
lp_x_sample = normal_lpdf(x_sample| mu_x, sigma_x);
lp_y_x_m_sample = normal_lpdf(y| b_x_y * x_sample + b_m_y * m, sigma_y);
p_y_m_over_x = p_y_m_over_x + exp(lp_x_sample + lp_y_x_m_sample);
}
return p_y_m_over_x;
}
}
data {
vector[100] X;
real x; // intervention value
real y; // target value of Y in P(Y=y|do(X=x))
vector[100] M;
vector[100] Y;
}
parameters {
real mu_x;
real<lower=0> sigma_x;
real b_x_m;
real<lower=0> sigma_m;
real b_m_y;
real b_x_y;
real<lower=0> sigma_y;
}
model {
X ~ normal(mu_x, sigma_x);
M ~ normal(b_x_m * X, sigma_m);
Y ~ normal(b_x_y * X + b_m_y * M, sigma_y);
}
generated quantities{
real p_m_given_x;
real p_y_given_m;
real p_y_given_do_x;
real m;
p_y_given_do_x = 0;
for (j in 1:100){ // This is a number of samples for outer integral. Complexity depends on this number.
m = normal_rng(b_x_m * x, sigma_m);
p_m_given_x = exp(normal_lpdf(m|b_x_m * x, sigma_m ));
p_y_given_m = get_p_y_given_m_rng(m, y, mu_x, sigma_x, b_x_y, b_m_y, sigma_y);
p_y_given_do_x = p_y_given_do_x + p_y_given_m * p_m_given_x;
}
}
"
compiled_model <- stan_model(model_code = model_code)
samples <- sampling(compiled_model, data = c(.data, list(x = 2.0, y = 3.0)))
```

Where .data is just a tibble that contains values for X, M, and Y. Any help would be greatly appreciated.