Truncated exponential distribution

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 λ?

likelihood = function(x,lambda,low=1.0,high=20){
  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)

发表评论

邮箱地址不会被公开。 必填项已用*标注