Exercise 1 This exercise is from MacKay, Information Theory, Inference, and Learning Algorithms:
Unstable particles are emitted from a source and decay at a distance x, a real number that has an exponential probability distribution with [parameter] λ. Decay events can only be observed if they occur in a window extending from x=1 cm to x=20 cm. N decays are observed at locations { 1.5, 2, 3, 4, 5, 12 } cm. What is the posterior distribution of λ?
factor = exp(-low * lambda) - exp(-high * lambda) # truncated exponential distribution
return(ifelse(x>=0,lambda * exp(-lambda*x)/factor,0))
}
obs = c(1.5, 2, 3, 4, 5, 12)
lam = seq(0.001,1.5,length.out = 1001)
p = numeric(length(lam))
for(i in 1:length(lam)){
p[i] = exp(sum(log(likelihood(obs,lam[i]))))
}
p = p / sum(p)
sum(p*lam)