Forecasting model does not contain samples

Hello, I am struggling to get a forecasting model to run in Stan. The situation is that I need to predict monthly sales of a consumable item. The data I have to use is the monthly sales of units that consume the item and the historical consumable sales. The assumption is that each cohort of units sold in a given month experiences a similar decay, or retirement rate over time. So the consuming unit base in the field is growing as we sell new units, but is also declining due to units being taken out of service. I have run a similar model successfully before, but now I am trying to feed in multiple groups and estimate 3 parameters for each: two parameters that define a gamma decay in install base for each cohort of units and one additional one for the consumption rate for each unit. I believe my problem comes from not understanding how to properly code in and segment the data - the model reads in ok, but says it contains no samples. I am bringing in the consumable and unit sell-out as two vectors and trying to segment out the groups in the transformed parameters block. Below is my code, I’m pretty new to Stan and greatly appreciate any insights !

Edit: I did get this to run finally, there were some issues with all the index gymnastics in the transformed parameters block, but the main problem was trying to use the ‘segment’ function there, perhaps incorrectly. Turns out I didn’t really need it. At it’s heart, this was a ragged array problem.


data {
  int gnum;                       // number of groups
  int N;                          // total number of observations
  int g_length[gnum];             // vector of group lengths
  real g_y[N];                    // actual monthly consumable sell-out
  real g_sellin[N];               // actual monthly unit sell-out 
  int g_ind[gnum+1];              // vector of start index of each group
}
  
parameters {
  vector[gnum] g_alpha;             // group level retirement gamma curve alpha parameter
  vector[gnum] g_beta;              // group level retirement gamma curve beta parameter
  vector[gnum] g_rate;              // average monthly consumption rate per unit
  real<lower=0> sigma;              // sd for monthly consumable sellout
}
  
transformed parameters {
  real pr[N];                               // cumulative units retired each month
  real mu[N];                               // average consumable sell-out each month
  real gammap[N];                           // cumulative gamma at month
  for(g in 1:gnum){                         // for each group
    for(i in 1:(g8_segl[g])){               // for each month within group
    gammap[i+g_ind[g]-1] = gamma_p(alphag[g],((i)/betag[g]));    
    pr[i+g_ind[g]-1] = dot_product(segment(g_sellin,g_ind[g],(g_ind[g+1]-1))[:i],sort_desc(gammap[1:i]));
    mu[i+g_ind[g]-1] = ampvg[g]*(cumulative_sum(segment(g_sellin,g_ind[g],(g_ind[g+1]-1)))[i] - pr[i]);
    }
  }
}
  
model {
  g_alpha ~ normal(7,3);                          // prior for group alpha
  g_beta ~ normal(10,3);                          // prior for group beta
  sigma ~ exponential(2e-09);                     // prior for sigma
  g_rate ~ normal(4000,2000);                     // prior for group rate
  for(n in 1:N){
  g_y[n] ~ normal(mu[n], sigma);
  }
}
1 Like

Good stuff!

Does that mean question answers?