# How to reduce sampling time?

Hi to everyone, as title says, I would like to reduce the time of gradient evaluation, for instance I see that it could depends from the initial value; there is a way to select many samples of initial value, to evaluate the gradient evaluation and after that I can choose the sample that minimize this time?
Do you have others suggestions to improve my perform?
You can find attached a simulation of gradient evaluation and a part of the rstan code.

``````model {
//Priors:

R0 ~ normal(3,2);
nu ~ uniform(0,1);

omega_0_19 ~ uniform(0,1); // blu
omega_20_39 ~ uniform(0,1); // viola
omega_40_59 ~ uniform(0,1); // rosso
omega_60_79 ~ uniform(0,1); // verde
omega_over80 ~ uniform(0,1); // grigio

psi_0_19 ~ uniform(0,1);
psi_20_39 ~ uniform(0,1);
psi_40_59 ~ uniform(0,1);
psi_60_79 ~ uniform(0,1);
psi_over80 ~ uniform(0,1);

alpha_0_19 ~ uniform(0,1);
alpha_20_39 ~ uniform(0,1);
alpha_40_59 ~ uniform(0,1);
alpha_60_79 ~ uniform(0,1);
alpha_over80 ~ uniform(0,1);

delta_0_19 ~ uniform(0,1);
delta_20_39 ~ uniform(0,1);
delta_40_59 ~ uniform(0,1);
delta_60_79 ~ uniform(0,1);
delta_over80 ~ uniform(0,1);

xi_0_19 ~ uniform(0,1);
xi_20_39 ~ uniform(0,1);
xi_40_59 ~ uniform(0,1);
xi_60_79 ~ uniform(0,1);
xi_over80 ~ uniform(0,1);

zeta ~ uniform(0,1);
mu ~ uniform(0,1);
r ~ uniform(0,1);

i0 ~ uniform(5,1800);
phi ~ uniform(0,10);

gamma ~ uniform(0,1);
sigma ~ uniform(0.125,0.5);

// Likelihood:
// I suppose the data distributed following a negative binomial with mean the solution of the ode's system
// and variance the parameter phi

for (i in 1:n_fit){
H_0_19_data[i]  ~  neg_binomial_2((H_inc_0_19[seed-3+i] +H_inc_0_19[seed-2+i] +H_inc_0_19[seed-1+i]
+H_inc_0_19[seed+i] +H_inc_0_19[seed+1+i]
+H_inc_0_19[seed+2+i] +H_inc_0_19[seed+3+i]) ./7 ,phi);

H_20_39_data[i] ~  neg_binomial_2((H_inc_20_39[seed-3+i] +H_inc_20_39[seed-2+i] +H_inc_20_39[seed-1+i]
+H_inc_20_39[seed+i] +H_inc_20_39[seed+1+i]
+H_inc_20_39[seed+2+i] +H_inc_20_39[seed+3+i]) ./7 ,phi);

H_40_59_data[i] ~  neg_binomial_2((H_inc_40_59[seed-3+i] +H_inc_40_59[seed-2+i] +H_inc_40_59[seed-1+i]
+H_inc_40_59[seed+i] +H_inc_40_59[seed+1+i]
+H_inc_40_59[seed+2+i] +H_inc_40_59[seed+3+i]) ./7 ,phi);

H_60_79_data[i] ~  neg_binomial_2((H_inc_60_79[seed-3+i] +H_inc_60_79[seed-2+i] +H_inc_60_79[seed-1+i]
+H_inc_60_79[seed+i] +H_inc_60_79[seed+1+i]
+H_inc_60_79[seed+2+i] +H_inc_60_79[seed+3+i]) ./7 ,phi);

H_over80_data[i] ~ neg_binomial_2((H_inc_over80[seed-3+i] +H_inc_over80[seed-2+i] +H_inc_over80[seed-1+i]
+H_inc_over80[seed+i] +H_inc_over80[seed+1+i]
+H_inc_over80[seed+2+i] +H_inc_over80[seed+3+i]) ./7 ,phi);

IC_0_19_data[i]  ~  neg_binomial_2((IC_inc_0_19[seed-3+i] +IC_inc_0_19[seed-2+i] +IC_inc_0_19[seed-1+i]
+IC_inc_0_19[seed+i] +IC_inc_0_19[seed+1+i]
+IC_inc_0_19[seed+2+i] +IC_inc_0_19[seed+3+i]) ./7 ,phi);

IC_20_39_data[i] ~  neg_binomial_2((IC_inc_20_39[seed-3+i] +IC_inc_20_39[seed-2+i] +IC_inc_20_39[seed-1+i]
+IC_inc_20_39[seed+i] +IC_inc_20_39[seed+1+i]
+IC_inc_20_39[seed+2+i] +IC_inc_20_39[seed+3+i]) ./7 ,phi);

IC_40_59_data[i] ~  neg_binomial_2((IC_inc_40_59[seed-3+i] +IC_inc_40_59[seed-2+i] +IC_inc_40_59[seed-1+i]
+IC_inc_40_59[seed+i] +IC_inc_40_59[seed+1+i]
+IC_inc_40_59[seed+2+i] +IC_inc_40_59[seed+3+i]) ./7 ,phi);

IC_60_79_data[i] ~  neg_binomial_2((IC_inc_60_79[seed-3+i] +IC_inc_60_79[seed-2+i] +IC_inc_60_79[seed-1+i]
+IC_inc_60_79[seed+i] +IC_inc_60_79[seed+1+i]
+IC_inc_60_79[seed+2+i] +IC_inc_60_79[seed+3+i]) ./7 ,phi);

IC_over80_data[i] ~ neg_binomial_2((IC_inc_over80[seed-3+i] +IC_inc_over80[seed-2+i] +IC_inc_over80[seed-1+i]
+IC_inc_over80[seed+i] +IC_inc_over80[seed+1+i]
+IC_inc_over80[seed+2+i] +IC_inc_over80[seed+3+i]) ./7 ,phi);

D_0_19_data[i]  ~  neg_binomial_2((D_inc_0_19[seed-3+i] +D_inc_0_19[seed-2+i] +D_inc_0_19[seed-1+i]
+D_inc_0_19[seed+i] +D_inc_0_19[seed+1+i]
+D_inc_0_19[seed+2+i] +D_inc_0_19[seed+3+i]) ./7 ,phi);

D_20_39_data[i] ~  neg_binomial_2((D_inc_20_39[seed-3+i] +D_inc_20_39[seed-2+i] +D_inc_20_39[seed-1+i]
+D_inc_20_39[seed+i] +D_inc_20_39[seed+1+i]
+D_inc_20_39[seed+2+i] +D_inc_20_39[seed+3+i]) ./7 ,phi);

D_40_59_data[i] ~  neg_binomial_2((D_inc_40_59[seed-3+i] +D_inc_40_59[seed-2+i] +D_inc_40_59[seed-1+i]
+D_inc_40_59[seed+i] +D_inc_40_59[seed+1+i]
+D_inc_40_59[seed+2+i] +D_inc_40_59[seed+3+i]) ./7 ,phi);

D_60_79_data[i] ~  neg_binomial_2((D_inc_60_79[seed-3+i] +D_inc_60_79[seed-2+i] +D_inc_60_79[seed-1+i]
+D_inc_60_79[seed+i] +D_inc_60_79[seed+1+i]
+D_inc_60_79[seed+2+i] +D_inc_60_79[seed+3+i]) ./7 ,phi);

D_over80_data[i] ~ neg_binomial_2((D_inc_over80[seed-3+i] +D_inc_over80[seed-2+i] +D_inc_over80[seed-1+i]
+D_inc_over80[seed+i] +D_inc_over80[seed+1+i]
+D_inc_over80[seed+2+i] +D_inc_over80[seed+3+i]) ./7 ,phi);
}

}
``````

It’s hard to say without knowing knowing precisely what version of rstan you’re using but the version of rstan on CRAN is severely out of date and doesn’t have support for a lot of the recent improvements so either installing the development version or using cmdstanr instead may help.

``````install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
``````

That said, the priors you have set are doing you absolutely no favors in terms of constraining the parameter space for more efficient sampling so changing \sim Uniform(0, 1) to \sim Normal(0.5, 0.5) may help if your goal is to constrain the parameters between 0 and 1.

While loops are often convenient, they’re seldom computationally efficient so if possible, vectorizing your likelihood will likely provide substantial speed gains.

Someone with a better knowledge of this particular model (I don’t have much familiarity with ODE) may be able to provide some more detailed suggestions regarding possible reparameterization but this should get you started at least.

2 Likes

Thank you.

install.packages(“StanHeaders”, repos = c(“Repository for distributing (some) stan-dev R packages | r-packages”, getOption(“repos”)))
install.packages(“rstan”, repos = c(“Repository for distributing (some) stan-dev R packages | r-packages”, getOption(“repos”)))

However now I have this error when I run:

model ← stan_model(“Model.stan”)

Error in system(command, as.integer(flag), f, stdout, stderr, timeout) :
character string expected as first argument