# How to model the sum of two random variable?

I’m trying to find latent variable from observed data produced by the sum of 2 random variables.

This is the python code producing the simulated data :

data = []
for p in range(1000): #paniers
s = np.random.exponential(scale=10,size=2).sum()
data.append(s)


This builds 1000 data points, each of them is the sum of two random draws from the same exponential random variable.

Below is my incomplete model. I’m trying to find the latent variable beta, which is the parameter of the exponential random variable used to create the data.

data {
int<lower=0> N;
real observed[N];
}
parameters {
real<lower=0,upper=100> beta;
}
model {
real e;
real s;
s = 0;
for (i in 1:2)
e ~ exponential(beta);
s += e;
observed ~  ????
}


How do I replace the ??? to ‘connect’ my observed data to my model ?

Any help appreciated.

It’d be helpful if you could write out the maths of your model. If you’re trying to model Y = X_0 + X_1 where X_i \sim \text{exponential}(\lambda) then the probability density function of Y can be written exactly.

1 Like

Yes it’s that model. but it is important to notice that X0 & X1 are not observed.

Your statement about the exponential distribution is interesting but I’d like a more general answer since I used exponential just as an example. I’d like to have a genera answer in term of the distribution from wich X0 & X1 are drawn.

I suspected this was the case. In general I think what you need to do is work out the maths of what the pdf of the actual observed random variable is conditional on the generating parameter. Stan takes a log-density up to normalisation, so that’s what you need to provide.

1 Like