using PyPlot
using CSV
########## load data
data = CSV.read(download("https://jonghank.github.io/ee370/files/fit_data.csv"))
u = data[:,1]
v = data[:,2]
figure(figsize=(6,6), dpi=100)
plot(u, v, "o", alpha=0.5)
grid()
axis("square")
xlim(0, 1)
ylim(0, 1)
xlabel(L"u")
ylabel(L"v")
title("Raw data")
show()
########## linear regression
n = length(u)
d = 6
X = zeros(n,d)
for i = 1:d
X[:,i] = u.^(i-1)
end
y = v
theta_opt = X \ y
println("Optimal theta: $(theta_opt)")
vp = range(0, stop=1, length=100)
X_vp = zeros(length(vp),d)
for i = 1:d
X_vp[:,i] = vp.^(i-1)
end
figure(figsize=(6,6), dpi=100)
plot(u, v, "o", alpha=0.5, label="Raw data")
plot(vp, X_vp*theta_opt, label="Predictor")
grid()
axis("square")
xlim(0, 1)
ylim(0, 1)
xlabel("u")
ylabel("v")
title("Polynomial predictor")
legend()
show()