Linear Regression from scratch in Julia

ML
Author

Nipun Batra

Published

September 1, 2021

using Plots
theme(:default)

using LinearAlgebra
using LaTeXStrings
x = 1:20; y = 4*x + 8*(0.5.-rand(20));
plot(x, y, seriestype = :scatter, title = "Dataset", xlabel = "X", ylabel= "Y", label="Noisy dataset", legend = :outertopright)
plot!(x, 4*x, seriestype = :line, label="True relationship", lw=2 )

function error(a, b)
    err = norm(y .- a[1] .- (b[1] .* x))
end
error (generic function with 1 method)
a = b = -5:0.1:7
-5.0:0.1:7.0
error.([1, 2]', [1, 4])
2×2 Matrix{Float64}:
 152.259  148.372
  12.56    15.7808
z = error.(a', b)
argmin_b, argmin_a = Tuple(argmin(z))
(90, 54)
surface(a, b, z , xlabel=L"\theta_0", ylabel=L"\theta_1", zlabel=L"Cost~(\theta_0, \theta_1)")

a[argmin_a], b[argmin_b]
(0.3, 3.9)
contourf(a, b, error.(a', b), xlabel=L"\theta_0", ylabel=L"\theta_1", title="Contour Plot")
plot!([a[argmin_a]], [b[argmin_b]], seriestype=:scatter, label="MLE", markersize=10)