Skip to content

Instantly share code, notes, and snippets.

@JulienPascal
Last active February 28, 2022 14:03
Show Gist options
  • Save JulienPascal/a991c64aed986be23b561a19f3f057f5 to your computer and use it in GitHub Desktop.
Save JulienPascal/a991c64aed986be23b561a19f3f057f5 to your computer and use it in GitHub Desktop.
OLS_ML_1
using LinearAlgebra
using Distributions
using Plots
using Distributions
using Random
using LaTeXStrings
n_points=10000
dim_input=100 #dim of input, without the intercept
dim_output=1
# Normal noise
d = Normal()
# True parameters
beta = rand(d, dim_input + 1);
# Noise
e = rand(d, n_points);
# Input data:
X = rand(d, (n_points,dim_input));
# Add the intercept:
X = hcat(ones(n_points),X);
#Linear Model
y = X*beta .+ e;
#OLS way
function OLS_direct(X::Array, y::Vector)
inv(transpose(X)*X)*transpose(X)*y
end
@time beta_hat = OLS_direct(X, y);
plot(beta, beta_hat, seriestype=:scatter, label="OLS (Direct)")
plot!(beta, beta, seriestype=:line, label="45° line")
xlabel!(L"True value $\beta$")
ylabel!(L"Estimated value $\hat{\beta}$ (Direct)")
savefig("OLS_direct.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment