Unable to bound my variables- "error occurred during calling the sampler; sampling not done"

fitting-issues

#1

I am trying to fit a mixed model as below. In this model, I want to constraint my parameters such that their values are less than Intercept. However, I am unable to do this as a sampling error is occurring with the error message “Error transforming variable beta_EquBasePrice: ub_free: Upper bounded variable is 0, but must be less than or equal to -1.40317e-016”. Kindly let me know where I am doing it wrong.
data{
int N;
real VolumeVelocity[N];
real EquBasePrice[N];
real DISP[N];
real FEAT[N];
real MobileCoupon[N];
int Retailer[N];
int N_Retailer;
}

parameters{
real Intercept;
real<upper = Intercept> beta_EquBasePrice;
real<upper = Intercept> beta_DISP;
real<upper = Intercept> beta_FEAT;
real<upper = Intercept> beta_MobileCoupon;
real<lower=0> sigma;
real vary_Retailer[N_Retailer];
real<lower=0> sigma_Retailer;
}

model{
real vary[N];
real glm[N];
// Priors
Intercept ~ normal( 0 , 100 );
beta_EquBasePrice ~ normal( 0 , 100 );
beta_DISP ~ normal( 0 , 100 );
beta_FEAT ~ normal( 0 , 100 );
beta_MobileCoupon ~ normal( 0 , 100 );
sigma_Retailer ~ uniform( 0 , 100 );
sigma ~ uniform( 0 , 100 );
// Varying effects
for ( j in 1:N_Retailer ) vary_Retailer[j] ~ normal( 0 , sigma_Retailer );
// Fixed effects
for ( i in 1:N ) {
vary[i] <- vary_Retailer[Retailer[i]];
glm[i] <- vary[i] + Intercept
+ beta_EquBasePrice * EquBasePrice[i]
+ beta_DISP * DISP[i]
+ beta_FEAT * FEAT[i]
+ beta_MobileCoupon * MobileCoupon[i];
}
VolumeVelocity ~ normal( glm , sigma );
}

generated quantities{
real dev;
real vary[N];
real glm[N];
dev <- 0;
for ( i in 1:N ) {
vary[i] <- vary_Retailer[Retailer[i]];
glm[i] <- vary[i] + Intercept
+ beta_EquBasePrice * EquBasePrice[i]
+ beta_DISP * DISP[i]
+ beta_FEAT * FEAT[i]
+ beta_MobileCoupon * MobileCoupon[i];
dev <- dev + (-2) * normal_log( VolumeVelocity[i] , glm[i] , sigma );
}
}"


#2

I made a data-free version of this model and it compiled and ran fine:

parameters{
  real Intercept;
  real<upper = Intercept> beta_EquBasePrice;
  real<upper = Intercept> beta_DISP;
  real<upper = Intercept> beta_FEAT;
  real<upper = Intercept> beta_MobileCoupon;
  real<lower=0> sigma;
  real vary_Retailer[10];
  real<lower=0> sigma_Retailer;
}

model{
  // Priors                                                                                                              
  Intercept ~ normal( 0 , 100 );
  beta_EquBasePrice ~ normal( 0 , 100 );
  beta_DISP ~ normal( 0 , 100 );
  beta_FEAT ~ normal( 0 , 100 );
  beta_MobileCoupon ~ normal( 0 , 100 );
  sigma_Retailer ~ uniform( 0 , 100 );
  sigma ~ uniform( 0 , 100 );
  // Varying effects                                                                                                     
  for ( j in 1:10 ) vary_Retailer[j] ~ normal( 0 , sigma_Retailer );
}

(surround code with triple ` marks to get it to format nicely)

Do you specify the initial conditions for beta_EquBasePrice? It sounds like it is being initialized to 0, but the upper bound is being initialized to 0 as well, and beta_EquBasePrice must be less than it. Or something like that.

For what it’s worth, vary[i] <- vary_Retailer[Retailer[i]]; has been deprecated in favor of vary[i] = vary_Retailer[Retailer[i]];. What version of Stan are you using?


#3

Thanks @bbbales2 for the reply. I am confused about specifying the initial conditions. In my problem statement, the parameters here are the coefficients of a linear mixed model. each coefficient can be transformed to get volume elasticity, which implies the impact that particular variable has on the total volume of sales. Intercept implies the core volume of sales which will be more than the contribution from the other variables. How do I give this condition to the Apriori distribution?
My intuition was If I change the normal distribution in the model to fit the requirement, it would be hard fitting the posterior values, which isn’t what I want. Is this intuition correct?


#4

The question is, are you specifying initial conditions? If you are, then try turning them off. Usually it is better not to. That way the different chains can estimate parameters from a diverse set of starting conditions (which improves robustness of your modelling).

If you aren’t specifying initial conditions, my suggestion isn’t going to help you :P. If this is the case, since the data-free version of the model seems to sample, could you post an R script to build data and run the model? Maybe that’d make it easier to track down what the problem really is.

Your priors are fine. They’re just super weak normals. They’re just going to look like truncated normals. Since the truncation point is moving around with a parameter, I think you’d strictly speaking want to have the truncation adjustment in there, but this isn’t going to stop the sampler from working. Search for “Truncation with upper bounds in Stan” in the Stan manual.