-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement quantile regression estimator
- Loading branch information
Showing
9 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# v0.8.16 (Upcoming Release) | ||
# v0.8.17 (Upcoming release) | ||
|
||
|
||
# v0.8.16 | ||
- Quantile Regression implemented | ||
|
||
|
||
# v0.8.15 | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "LinRegOutliers" | ||
uuid = "6d4de0fb-32d9-4c65-aac1-cc9ed8b94b1a" | ||
authors = ["Mehmet Hakan Satman <[email protected]>", "Shreesh Adiga <[email protected]>", "Guillermo Angeris <[email protected]>", "Emre Akadal <[email protected]>"] | ||
version = "0.8.15" | ||
version = "0.8.16" | ||
|
||
[deps] | ||
Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5" | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
module QuantileRegression | ||
|
||
export quantileregression | ||
|
||
using JuMP | ||
using GLPK | ||
|
||
import ..Basis: | ||
RegressionSetting, @extractRegressionSetting, designMatrix, responseVector, applyColumns | ||
|
||
""" | ||
quantileregression(setting; tau = 0.5) | ||
Perform Quantile Regression for a given regression setting (multiple linear regression). | ||
# Arguments | ||
- `setting::RegressionSetting`: RegressionSetting object with a formula and dataset. | ||
- `tau::Float64`: Quantile level. Default is 0.5. | ||
# Description | ||
The Quantile Regression estimator searches for the regression parameter estimates that minimize the | ||
Min z = (1 - tau) (u1(-) + u2(-) + ... + un(-)) + tau (u1(+) + u2(+) + ... + un(+)) | ||
Subject to: | ||
y_1 - beta0 - beta1 * x_2 + u1(-) - u1(+) = 0 | ||
y_2 - beta0 - beta1 * x_2 + u2(-) - u2(+) = 0 | ||
. | ||
. | ||
. | ||
y_n - beta0 - beta1 * x_n + un(-) - un(+) = 0 | ||
where | ||
ui(-), ui(+) >= 0 | ||
i = 1, 2, ..., n | ||
beta0, beta1 in R | ||
n : Number of observations | ||
model is the y = beta1 + beta2 * x + u | ||
# Output | ||
- `["betas"]`: Estimated regression coefficients | ||
- `["residuals"]`: Regression residuals | ||
- `["model"]`: Linear Programming Model | ||
# Examples | ||
```julia-repl | ||
julia> reg0001 = createRegressionSetting(@formula(calls ~ year), phones); | ||
julia> quantileregression(reg0001) | ||
``` | ||
""" | ||
function quantileregression(setting::RegressionSetting; tau::Float64 = 0.5) | ||
X, y = @extractRegressionSetting setting | ||
return quantileregression(X, y, tau = tau) | ||
end | ||
|
||
|
||
""" | ||
quantileregression(X, y, tau = 0.5) | ||
Estimates parameters of linear regression using Quantile Regression Estimator for a given regression setting. | ||
# Arguments | ||
- `X::Array{Float64, 2}`: Design matrix of the linear model. | ||
- `y::Array{Float64, 1}`: Response vector of the linear model. | ||
- `tau::Float64`: Quantile level. Default is 0.5. | ||
# Examples | ||
```julia-repl | ||
julia> income = [420.157651, 541.411707, 901.157457, 639.080229, 750.875606]; | ||
julia> foodexp = [255.839425, 310.958667, 485.680014, 402.997356, 495.560775]; | ||
julia> n = length(income) | ||
julia> X = hcat(ones(Float64, n), income) | ||
julia> result = quantileregression(X, foodexp, tau = 0.25) | ||
``` | ||
""" | ||
function quantileregression(X::Array{Float64,2}, y::Array{Float64,1}; tau::Float64 = 0.5) | ||
n, p = size(X) | ||
|
||
m = JuMP.Model(GLPK.Optimizer) | ||
|
||
JuMP.@variable(m, d[1:(2n)]) | ||
JuMP.@variable(m, beta[1:p]) | ||
|
||
JuMP.@objective( | ||
m, | ||
Min, | ||
sum((1 - tau) * d[i] for i = 1:n) + sum(tau * d[i] for i = (n+1):2n) | ||
) | ||
|
||
for i = 1:n | ||
c = JuMP.@constraint(m, y[i] - sum(X[i, :] .* beta) + d[i] - d[n+i] == 0) | ||
end | ||
|
||
for i = 1:(2n) | ||
JuMP.@constraint(m, d[i] >= 0) | ||
end | ||
|
||
JuMP.optimize!(m) | ||
|
||
betahats = JuMP.value.(beta) | ||
residuals = y .- X * betahats | ||
|
||
result = Dict() | ||
result["betas"] = betahats | ||
result["residuals"] = residuals | ||
result["model"] = m | ||
return result | ||
end | ||
|
||
end # end of module QuantileRegression |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@testset "Quantile Regression" begin | ||
|
||
eps = 0.001 | ||
|
||
@testset "Quantile Regression - q = 0.5" begin | ||
income = [420.157651, 541.411707, 901.157457, 639.080229, 750.875606] | ||
foodexp = [255.839425, 310.958667, 485.680014, 402.997356, 495.560775] | ||
|
||
n = length(income) | ||
X = hcat(ones(Float64, n), income) | ||
|
||
result = quantileregression(X, foodexp, tau = 0.5) | ||
|
||
betas2 = result["betas"] | ||
@test abs(betas2[1] - 55.0716060) < eps | ||
@test abs(betas2[2] - 0.4778393) < eps | ||
end | ||
|
||
@testset "Quantile Regression - q = 0.25" begin | ||
income = [420.157651, 541.411707, 901.157457, 639.080229, 750.875606] | ||
foodexp = [255.839425, 310.958667, 485.680014, 402.997356, 495.560775] | ||
|
||
n = length(income) | ||
X = hcat(ones(Float64, n), income) | ||
|
||
result = quantileregression(X, foodexp, tau = 0.25) | ||
|
||
betas2 = result["betas"] | ||
@test abs(betas2[1] - 48.0057823) < eps | ||
@test abs(betas2[2] - 0.4856801 ) < eps | ||
end | ||
|
||
@testset "Quantile Regression - q = 0.95" begin | ||
income = [420.157651, 541.411707, 901.157457, 639.080229, 750.875606] | ||
foodexp = [255.839425, 310.958667, 485.680014, 402.997356, 495.560775] | ||
|
||
n = length(income) | ||
X = hcat(ones(Float64, n), income) | ||
|
||
result = quantileregression(X, foodexp, tau = 0.95) | ||
|
||
betas2 = result["betas"] | ||
@test abs(betas2[1] - (-48.7124077)) < eps | ||
@test abs(betas2[2] - 0.7248513) < eps | ||
end | ||
|
||
end |
7be933b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
7be933b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/74773
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: