Straight line fit in julia

$$ \newcommand{\eg}{{\it e.g.}} \newcommand{\ie}{{\it i.e.}} \newcommand{\argmin}{\operatornamewithlimits{argmin}} \newcommand{\mc}{\mathcal} \newcommand{\mb}{\mathbb} \newcommand{\mf}{\mathbf} \newcommand{\minimize}{{\text{minimize}}} \newcommand{\diag}{{\text{diag}}} \newcommand{\cond}{{\text{cond}}} \newcommand{\rank}{{\text{rank }}} \newcommand{\range}{{\mathcal{R}}} \newcommand{\null}{{\mathcal{N}}} \newcommand{\tr}{{\text{trace}}} \newcommand{\dom}{{\text{dom}}} \newcommand{\dist}{{\text{dist}}} \newcommand{\R}{\mathbf{R}} \newcommand{\SM}{\mathbf{S}} \newcommand{\ball}{\mathcal{B}} \newcommand{\bmat}[1]{\begin{bmatrix}#1\end{bmatrix}} $$

EE787: Machine learning, Kyung Hee University.
Jong-Han Kim (jonghank@khu.ac.kr)

In [1]:
using LinearAlgebra
using PyPlot

n = 50
d = 2

theta_true = randn(d)
u = randn(n)
v = theta_true[1] .+ theta_true[2]*u + 0.2*randn(n)

figure(dpi=100)
plot(u, v, alpha=0.5, "o", label="Raw data")
xlabel("u")
ylabel("v")
legend()
grid()
show()
In [2]:
X = [ones(n) u]
y = v

theta_opt = X\y

y_hat = X*theta_opt
RMSE = norm(y-y_hat)/sqrt(n)
println("theta_true:", theta_true)
println("theta_opt:", theta_opt)
println("RMSE: ",RMSE)
theta_true:[-0.570999, -0.739793]
theta_opt:[-0.551347, -0.758609]
RMSE: 0.198982054529931
In [3]:
u_test = -3:0.01:3
n_test = length(u_test)
X_test = [ones(n_test) u_test]
y_test = X_test*theta_opt
v_test = y_test

figure(dpi=100)
plot(u, v, alpha=0.5, "o", label="Raw data")
plot(u_test, v_test, label="Straight line fit")
xlabel("u")
ylabel("v")
legend()
grid()
show()
In [ ]: