Skip to content

Commit f332613

Browse files
committed
reduce allocations
1 parent 4c0ba62 commit f332613

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/lts.jl

Lines changed: 6 additions & 5 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: residuals, coef, olsf!
7+
import ..OrdinaryLeastSquares: residuals, coef, olsf!, olsf
88

99
import Distributions: sample!, mean
1010

@@ -51,13 +51,14 @@ function iterateCSteps(
5151
iter::Int = 0
5252
sortedresindices = Array{Int}(undef, n)
5353
tempbetas = zeros(size(X, 2))
54+
absres = zeros(n)
5455
while iter < maxiter
5556
olsf!(view(X, subsetindices, :), view(y, subsetindices), tempbetas)
56-
absres = abs.(y - X * tempbetas)
57+
absres .= abs.(y - X * tempbetas)
5758
sortperm!(sortedresindices, absres)
5859
subsetindices = view(sortedresindices, 1:h)
5960
objective = sum(view(sort!(absres .^ 2.0), 1:h))
60-
if isapprox(oldobjective, objective, atol=eps)
61+
if abs(oldobjective - objective) < eps
6162
break
6263
end
6364
oldobjective = objective
@@ -163,7 +164,7 @@ function lts(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters=nothi
163164
objective, hsubsetindices = iterateCSteps(X, y, subsetindices, h)
164165
if objective < bestobjective
165166
bestobjective = objective
166-
besthsubset = hsubsetindices
167+
besthsubset .= hsubsetindices
167168
bestobjectiveunchanged = 0
168169
else
169170
bestobjectiveunchanged += 1
@@ -173,7 +174,7 @@ function lts(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters=nothi
173174
end
174175
end
175176

176-
ltsbetas = view(X, besthsubset, :) \ view(y, besthsubset)
177+
ltsbetas = olsf(view(X, besthsubset, :), view(y, besthsubset))
177178
ltsres = y - X * ltsbetas
178179
ltsS = sqrt(sum((ltsres .^ 2.0)[1:h]) / (h - p))
179180
ltsresmean = mean(ltsres[besthsubset])

0 commit comments

Comments
 (0)