Skip to content
DeepForgeby svx
All problems
HardML Fundamentalsml-312

Bayesian Posterior Mean

Problem

Compute the posterior mean of a 1D Bayesian linear regression with known noise variance, prior mean, and prior variance: (sum(x*y)/noise_var + prior_mean/prior_var) / (sum(x^2)/noise_var + 1/prior_var).

Return prior_mean when a variance is non-positive.

Hint

The posterior mean is a precision-weighted blend of the data and the prior.

Test cases

case 1
in: [[1,2],[2,4],1,0,1]
expected: 1.6666666666666667
case 2
in: [[1,2],[2,4],1,10,0.1]
expected: 7.333333333333333
case 3
in: [[],[],1,0,1]
expected: 0
Starter codePython
def bayesian_posterior_mean(x, y, noise_var, prior_mean, prior_var):
    # Your code here
    pass

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