All problems
Thompson Linear Posterior
Problem
Update a scalar Bayesian linear regression posterior after observing xs and ys.
post_precision = prior_precision + sum(x^2) / obs_var and post_mean = (prior_precision * prior_mean + sum(x * y) / obs_var) / post_precision. Return [post_mean, post_precision]; with no data return the prior.
Hint
Precision adds; the posterior mean is the precision-weighted average of prior and data.
Test cases
case 1
in: [0,1,[1,2,3],[1,2,3],1]
expected: [0.9333333333333333,15]
case 2
in: [0,1,[1],[2],2]
expected: [0.6666666666666666,1.5]
case 3
in: [2,0.5,[],[],0.5]
expected: [2,0.5]
case 4
in: [2,0.5,[1,-1,2],[3,-3,6],0.5]
expected: [2.96,12.5]
Starter codePython
def thompson_linear_posterior(prior_mean, prior_precision, xs, ys, obs_var):
# Your code here
pass4 test cases · run your code against them in the editor