using Pkg;Pkg.activate("probprog/workspace/");Pkg.instantiate(); IJulia.clear_output(); using PyPlot, LinearAlgebra # Model specification: y|x ~ 𝒩(f(x), v(x)) f(x) = 5*x .- 2 v(x) = 10*exp.(2*x.^2) .- 9.5 # input dependent noise variance x_test = [0.0, 1.0] plot(x_test, f(x_test), "k--") # plot f(x) # Generate N samples (x,y), where x ~ Unif[0,1] N = 50 x = rand(N) y = f(x) + sqrt.(v(x)) .* randn(N) plot(x, y, "kx"); xlabel("x"); ylabel("y") # Plot samples # Add constant to input so we can estimate both the offset and the slope _x = [x ones(N)] _x_test = hcat(x_test, ones(2)) # LS regression w_ls = pinv(_x) * y plot(x_test, _x_test*w_ls, "b-") # plot LS solution # Weighted LS regression W = Diagonal(1 ./ v(x)) # weight matrix w_wls = inv(_x'*W*_x) * _x' * W * y plot(x_test, _x_test*w_wls, "r-") # plot WLS solution ylim([-5,8]); legend(["f(x)", "D", "LS linear regr.", "WLS linear regr."],loc=2); open("../../styles/aipstyle.html") do f display("text/html", read(f, String)) end