Search code examples
rstan

infer posterior of two variables constrained by their sum in STAN


Motivation: answer the following question: the sum of 2 men's height is 4 meters, what's the most probable height of each man ?

I'm trying to model this using STAN (it might be overkill for this use case, but the goal is to have a general framework from which I can extend), and I assume that people's height is Normal(1.8, 0.2).

I've come up with the following .stan code, but when I look at the results, it seems that the constraint of the sum isn't taken into account. What am I doing wrong ?

experiment.stan

data {
}
parameters {
  real y1;
  real y2;
}
transformed parameters {
  real S;
  S = y1+y2;
  S = 4;
}
model {  
  y1 ~ normal(1.8,0.2);
  y2 ~ normal(1.8,0.2);
}

experiment.R

#load libraries
library(rstan)
library(coda)

#the model
heights<-stan(file="experiment.stan",
             pars = c("y1", "y2", "S"))

#plotting the posterior distribution for the parameters
post_beta<-As.mcmc.list(heights,pars=c("y1", "y2", "S"))
plot(post_beta)

Solution

  • A simple solution to create a constraint between parameters is to express one of the parameters as a transform of the other. This solution keeps things very simple and flexible:

    experiment.stan

    data {
     }
    parameters {
      real y1;
    }
    transformed parameters {
      real y2;
      y2 = 4-y1;
    }
    model {  
      y1 ~ normal(1.8,0.2);
      y2 ~ normal(1.8,0.2);
    }