Skip to content
DeepForgeby svx
All problems
EasyProbabilitypr-279

Normal-Normal Posterior Variance

Problem

Under the conjugate normal model, posterior precision is the sum of the prior precision and n data precisions, so the posterior variance is 1 / (1 / prior_var + n / data_var). Return prior_var when n <= 0 or data_var is nonpositive, and 0.0 when prior_var is nonpositive.

Hint

More observations and tighter data precision shrink the posterior variance.

Test cases

case 1
in: [1,1,1]
expected: 0.5
case 2
in: [4,2,2]
expected: 0.8
case 3
in: [2,1,0]
expected: 2
case 4
in: [0.5,1,1]
expected: 0.3333333333333333
case 5
in: [0,1,1]
expected: 0
Starter codePython
def normal_normal_posterior_variance(prior_var, data_var, n):
    # Your code here
    pass

5 test cases · run your code against them in the editor