Skip to content

Commit 4c0ba62

Browse files
committed
reduce allocations in ols and lts
1 parent a386fcf commit 4c0ba62

5 files changed

Lines changed: 43 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# v0.11.8 (Upcoming Release)
22

3+
- Reduce allocations in OLS and LTS. Implements new olsf() and olsf!() functions.
34

45
# v0.11.7
56

src/LinRegOutliers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import .DataSets: hills, softdrinkdelivery, animals
6363
# Ordinary least squares type and functions
6464
# for fast regression tasks in outlier detection algorithms
6565
include("ols.jl")
66-
import .OrdinaryLeastSquares: OLS, ols, wls, residuals, predict, coef
66+
import .OrdinaryLeastSquares: OLS, ols, wls, residuals, predict, coef, olsf, olsf!
6767

6868
# Regression diagnostics
6969
include("diagnostics.jl")
@@ -250,7 +250,7 @@ export diagnose
250250

251251

252252
# Ordinary least squares
253-
export OLS, ols, wls, residuals, predict, coef
253+
export OLS, ols, wls, residuals, predict, coef, olsf, olsf!
254254

255255
# Algorithms
256256
export hs93, hs93initialset, hs93basicsubset

src/lts.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export lts
44
export iterateCSteps
55

66
import ..Basis: RegressionSetting, @extractRegressionSetting, designMatrix, responseVector
7-
import ..OrdinaryLeastSquares: ols, residuals, coef
7+
import ..OrdinaryLeastSquares: residuals, coef, olsf!
88

99
import Distributions: sample!, mean
1010

@@ -50,9 +50,10 @@ function iterateCSteps(
5050
objective::Float64 = Inf64
5151
iter::Int = 0
5252
sortedresindices = Array{Int}(undef, n)
53+
tempbetas = zeros(size(X, 2))
5354
while iter < maxiter
54-
tempols = ols(view(X, subsetindices, :), view(y, subsetindices))
55-
absres = abs.(y - X * coef(tempols))
55+
olsf!(view(X, subsetindices, :), view(y, subsetindices), tempbetas)
56+
absres = abs.(y - X * tempbetas)
5657
sortperm!(sortedresindices, absres)
5758
subsetindices = view(sortedresindices, 1:h)
5859
objective = sum(view(sort!(absres .^ 2.0), 1:h))

src/ols.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module OrdinaryLeastSquares
22

33

44
export OLS, ols, wls, residuals, predict, coef
5+
export olsf, olsf!
56

67
import LinearAlgebra: ColumnNorm, qr, Diagonal
78
import ..Basis:
@@ -51,8 +52,14 @@ julia> reg.betas
5152
5253
"""
5354
ols(X::AbstractMatrix{Float64}, y::AbstractVector{Float64})::OLS = OLS(X, y, qr(X, ColumnNorm()) \ y)
54-
#ols(X::AbstractMatrix{Float64}, y::AbstractVector{Float64})::OLS = OLS(X, y, qr(X, Val(true)) \ y)
5555

56+
function olsf(X::AbstractMatrix{Float64}, y::AbstractVector{Float64})::Vector{Float64}
57+
return qr(X, ColumnNorm()) \ y
58+
end
59+
60+
function olsf!(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}, betas::AbstractVector{Float64})::Vector{Float64}
61+
betas .= qr(X, ColumnNorm()) \ y
62+
end
5663

5764
function ols(setting::RegressionSetting)::OLS
5865
X, y = @extractRegressionSetting setting

test/testols.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@
2323
@test isapprox(result.betas[2], 5.04147826086957, atol = eps)
2424
end
2525

26+
@testset "Ordinary Least Squares (olsf)" begin
27+
tol = 0.0001
28+
#  The model is exactly y = 5 + 5x
29+
var1 = Float64[1, 2, 3, 4, 5]
30+
X = hcat(ones(5), var1)
31+
betas = [5.0, 5.0]
32+
y = X * betas
33+
34+
# OLS
35+
olsreg = olsf(X, y)
36+
@test isapprox(olsreg, betas, atol = tol)
37+
end
38+
39+
@testset "Ordinary Least Squares (olsf!)" begin
40+
tol = 0.0001
41+
#  The model is exactly y = 5 + 5x
42+
var1 = Float64[1, 2, 3, 4, 5]
43+
X = hcat(ones(5), var1)
44+
betas = [5.0, 5.0]
45+
y = X * betas
46+
47+
# OLS
48+
tempbetas = zeros(size(X, 2))
49+
olsf!(X, y, tempbetas)
50+
@test isapprox(tempbetas, betas, atol = tol)
51+
end
52+
53+
2654

2755
@testset "Weighted Least Squares" begin
2856
tol = 0.0001

0 commit comments

Comments
 (0)