Linear Regression from scratch in Julia#

  • toc: true

  • badges: true

  • comments: true

  • author: Nipun Batra

  • categories: [ML]

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 )
../../_images/6de7b90afb776a85982097c54175eee1f0cd96727c358969163a0f0028955b48.svg
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)")
../../_images/964fa569c93b730dda2ff507e1991746ed48451bccb6d57b09d62079120b0dc4.svg
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)
../../_images/a4b2ccf732edc445ba79ec39e5eb3c069e13298bf720b57060fbea8f1417f0c8.svg