using LinearAlgebra
using Distributions
using Random
using Statistics
using Plots
Σ = [3. 0.; 0. 2.] # variance-covariance matrix
C = cholesky(Σ) # Cholesky decomposition
P = Matrix(C.L)
P*P'
Random.seed!(1234)
Z = randn(1000,2);
D = P*Z'
#var(D[1,:])
#var(D[2,:])
β = [2,5] # β_0 = 2, β_1 = 5
X = hcat(ones(1000), D[1,:])
ϵ = D[2,:];
y = X*β + ϵ;
@time inv(X'*X)*X'*y # It is not very numerically efficient
# F = qr(X) # QR decomposition. More numerically efficient.
# Q,R = Matrix(F.Q), Matrix(F.R)
@time inv(Matrix(qr(X).R))*Matrix(qr(X).Q)'*y # Compare the time and allocations with the results above
# Put everything above in a function
# Note: set seeds to make your results replicable
function β_ols(Σ,βs,seed=1234)
C = cholesky(Σ)
P = Matrix(C.L)
Random.seed!(seed)
Z = Matrix(randn(1000,2)')
D = P*Z
X = hcat(ones(1000),D[1,:])
ϵ = D[2,:]
F = qr(X)
Q,R = Matrix(F.Q), Matrix(F.R)
y = X*β + ϵ
β_ols = inv(R)*Q'*y
return β_ols
end
β_ols(Σ,β,2421) # Test if the functiton works
seed = 123
β_set = []
for i in 1:10000
push!(β_set, β_ols(Σ, β,seed))
seed +=1
end
β_set = hcat(β_set...) # convert the 10000 pairs of estimates into matrix form
mean(β_set[2,:])
mean(β_set[1,:])
histogram(β_set[1,:],bins=30) # Plot a histogram of β_0^hat