Skip to content

Instantly share code, notes, and snippets.

@JulienPascal
JulienPascal / OLS_ML_4.jl
Created February 28, 2022 14:11
OLS_ML_4
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
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));
@JulienPascal
JulienPascal / OLS_ML_3.jl
Created February 28, 2022 14:09
OLS_ML_3
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));
@JulienPascal
JulienPascal / OLS_ML_2.jl
Last active February 28, 2022 14:07
OLS_ML_2.jl
#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)
@JulienPascal
JulienPascal / OLS_ML_1.jl
Last active February 28, 2022 14:03
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
@JulienPascal
JulienPascal / Logistic2.jl
Last active February 20, 2022 17:21
Logistic2
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})
@JulienPascal
JulienPascal / Logistic1.jl
Last active February 20, 2022 17:10
LogisticRegression
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:
@JulienPascal
JulienPascal / CNNPrimer5
Last active January 13, 2020 15:20
CNN with Julia
# 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)
@JulienPascal
JulienPascal / CNNPrimer4
Last active January 13, 2020 15:21
CNN with Julia
# 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)
@JulienPascal
JulienPascal / CNNPrimer3
Last active January 13, 2020 15:21
CNN with Julia
# 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)