Skip to content

Commit a386fcf

Browse files
committed
reduce memory allocations in lts
1 parent 9461985 commit a386fcf

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

src/lts.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export iterateCSteps
66
import ..Basis: RegressionSetting, @extractRegressionSetting, designMatrix, responseVector
77
import ..OrdinaryLeastSquares: ols, residuals, coef
88

9-
import Distributions: sample, mean
9+
import Distributions: sample!, mean
1010

1111
"""
1212
@@ -52,10 +52,10 @@ function iterateCSteps(
5252
sortedresindices = Array{Int}(undef, n)
5353
while iter < maxiter
5454
tempols = ols(view(X, subsetindices, :), view(y, subsetindices))
55-
res = y - X * coef(tempols)
56-
sortperm!(sortedresindices, abs.(res))
55+
absres = abs.(y - X * coef(tempols))
56+
sortperm!(sortedresindices, absres)
5757
subsetindices = view(sortedresindices, 1:h)
58-
objective = sum(view(sort!(res .^ 2.0), 1:h))
58+
objective = sum(view(sort!(absres .^ 2.0), 1:h))
5959
if isapprox(oldobjective, objective, atol=eps)
6060
break
6161
end
@@ -158,7 +158,7 @@ function lts(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters=nothi
158158
bestobjectiveunchanged = 0
159159

160160
for _ = 1:iters
161-
subsetindices = sample(allindices, p, replace=false)
161+
sample!(allindices, subsetindices, replace=false)
162162
objective, hsubsetindices = iterateCSteps(X, y, subsetindices, h)
163163
if objective < bestobjective
164164
bestobjective = objective

0 commit comments

Comments
 (0)