using Plots
theme(:default)
using LinearAlgebra
using LaTeXStrings
= 1:20; y = 4*x + 8*(0.5.-rand(20));
x 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)
= norm(y .- a[1] .- (b[1] .* x))
err end
error (generic function with 1 method)
= b = -5:0.1:7 a
-5.0:0.1:7.0
error.([1, 2]', [1, 4])
2×2 Matrix{Float64}:
152.259 148.372
12.56 15.7808
= error.(a', b)
z = Tuple(argmin(z)) argmin_b, argmin_a
(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)