This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plot(beta_1_grid, beta_2_grid, (x,y) -> obj_function(X, y, [beta[1]; x; y]), st=:contour, colorbar_title=L"|X-y\hat{\beta}|^2") | |
scatter!([beta[2]], [beta[3]], markershape = :star5) | |
xlabel!(L"\beta_1") | |
ylabel!(L"\beta_2") | |
# refinement loop | |
beta_hat_sgd = [beta[1]; -9.0; 9.0] #fix the intercept at the true value. Random guess for beta_1 and beta_2 | |
beta_hat = [beta[1]; -9.0; 9.0] | |
grad_n_sgd = zeros(3) #initialize gradient | |
grad_n = zeros(3) #initialize gradient |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dim_input=2 #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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dim_input=2 #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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Calculate the gradient | |
function grad_OLS!(G, beta_hat, X, y) | |
G[:] = transpose(X)*(X*beta_hat - y) | |
end | |
#Gradient descent way | |
function OLS_gd(X::Array, y::Vector; epochs::Int64=1000, r::Float64=1e-5, beta_hat = zeros(size(X,2)), verbose::Bool=false) | |
grad_n = zeros(size(X,2)) | |
for epoch=1:epochs | |
grad_OLS!(grad_n, beta_hat, X, y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y = convert(Array, df_nba[:SHOT_RESULT]); | |
X = convert(Matrix, df_nba[[:SHOT_CLOCK, :SHOT_DIST, :CLOSE_DEF_DIST]]) | |
X = hcat(ones(size(X,1)), X); | |
# Logistic function for a scalar input: | |
function sigma(x::Float64) | |
exp(x)/(1.0 + exp(x)) | |
end | |
# Logistic function for a vector input: | |
function sigma(x::Array{Float64,1}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_nba = CSV.read("/home/julien/Documents/REPOSITORIES/LogisticRegression/data/shot_logs.csv"); | |
names(df_nba) | |
df_nba = df_nba[[:SHOT_RESULT, :SHOT_CLOCK, :SHOT_DIST, :CLOSE_DEF_DIST]] | |
# Drop rows with missings: | |
df_nba = dropmissing(df_nba); | |
# Drop rows with NaN: | |
df_nba = df_nba[completecases(df_nba), :] | |
# Convert SHOT_RESULT to a binary variable (1 for success, 0 for missed) | |
df_nba[:, :SHOT_RESULT] = ifelse.(df_nba[:, :SHOT_RESULT] .== "made", 1.0, 0.0); | |
# Show the first few rows of df_nba: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Training loop | |
# See: https://github.com/FluxML/model-zoo/blob/master/vision/mnist/conv.jl | |
best_acc = 0.0 | |
last_improvement = 0 | |
accuracy_target = 0.97 #Set an accuracy target. When reached, we stop training. | |
max_epochs = 100 #Maximum | |
for epoch_idx in 1:100 | |
global best_acc, last_improvement | |
# Train for a single epoch | |
Flux.train!(loss, params(model), train_set, opt) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Loss function | |
# See: https://github.com/FluxML/model-zoo/blob/master/vision/mnist/conv.jl | |
# `loss()` calculates the crossentropy loss between our prediction `y_hat` | |
function loss(x, y) | |
# Add some noise to the image | |
# we reduce the risk of overfitting the train sample by doing so: | |
x_aug = x .+ 0.1f0*gpu(randn(eltype(x), size(x))) | |
y_hat = model(x_aug) | |
return crossentropy(y_hat, y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Batching | |
# See: https://github.com/FluxML/model-zoo/blob/master/vision/mnist/conv.jl | |
# Bundle images together with labels and group into minibatchess | |
function make_minibatch(X, Y, idxs) | |
X_batch = Array{Float32}(undef, size(X[1])..., 1, length(idxs)) | |
for i in 1:length(idxs) | |
X_batch[:, :, :, i] = Float32.(X[idxs[i]]) | |
end | |
Y_batch = onehotbatch(Y[idxs], 0:9) | |
return (X_batch, Y_batch) |
NewerOlder