0

Given that the likelihood is Y|n~Binomial(n, theta) and the prior is n~Poisson(5), I tried to calculate the posterior distribution of sample size n with Y=0 and theta=0.2. My code is as follows:

Y <- 0
theta <- 0.2
n_grid <- seq(0,1,length=1000)
like <- dbinom(Y,n_grid,theta)
prior <- dpois(n_grid,5)
fy <- sum(like*prior)
post <- like*prior/fy
plot(n_grid,post,type="l")

I keep getting NaN results when computing the likelihood function and priors. Any help would be appreciated!

1 Answers1

0

So I realize it might be unconventional to answer my own question, but I figured out my solution and thought I would post the answer to help someone else out.

Y          <- 0
theta      <- 0.2
N          <- 0:0.01:100
like       <- dbinom(Y,N,theta)
prior      <- dpois(N,5)
fy         <- sum(like*prior)
post       <- like*prior/fy

plot(N,post,type="l")
  • The expression `0:0.01:100` actually evaluates the same as `0:100`; I'd definitely suggest the latter as being clearer about what's going on. The rest looks fine. – user2554330 Feb 27 '22 at 19:10