FastPrepPosterior of a Poisson Rate After Observing a Total

Posterior of a Poisson Rate After Observing a Total

Walleye Capital logoWalleye Capital● EasyFULLTIMEOA

Problem statement

Let λ have a Gamma prior with shape alpha and rate beta: λ ~ Gamma(alpha, beta).

Given λ, let X1, ..., Xn be independent random variables with Xi | λ ~ Poisson(λ). You observe only their total S = X1 + ... + Xn = s.

Find the posterior distribution of λ given S = s.

Return it as a string "Name(a,b)" with no spaces. Name is the distribution family's name, spelled the way the prior's family is spelled above. a and b are its two parameters, written as integers in the same order and parameterization that the prior uses.

Function

posteriorDistribution(alpha: int, beta: int, n: int, s: int) → String

Examples

Example 1

alpha = 2beta = 1n = 3s = 5return = "Gamma(7,4)"

The prior density is proportional to λ^(alpha-1) * e^(-beta*λ). Given λ, the total S is Poisson(n*λ), so the likelihood of S = s is proportional to λ^s * e^(-n*λ).

Multiplying gives λ^(alpha+s-1) * e^(-(beta+n)*λ). That is a Gamma density with shape alpha + s = 2 + 5 = 7 and rate beta + n = 1 + 3 = 4.

Example 2

alpha = 1beta = 1n = 1s = 0return = "Gamma(1,2)"

One observation of 0 adds nothing to the shape and adds 1 to the rate, so the posterior is Gamma(1, 2).

Example 3

alpha = 3beta = 2n = 10s = 25return = "Gamma(28,12)"

The shape becomes 3 + 25 = 28 and the rate becomes 2 + 10 = 12.

Constraints

  • 1 <= alpha, beta <= 10^6
  • 1 <= n <= 10^6
  • 0 <= s <= 10^9

More Walleye Capital problems

See Walleye Capital hiring insights
Saved
public String posteriorDistribution(int alpha, int beta, int n, int s) {
    // Write your code here.
}
alpha2
beta1
n3
s5
expected"Gamma(7,4)"
Checking account…