Skip to content

Commit

Permalink
reduce memory allocations in lms() and lts()
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytecode committed May 23, 2024
1 parent e18b034 commit 6a42574
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- More explicit return types, drop `Dict` with `Dict{String, Any}` or `Dict{String, Vector}`
- Add `Julia v.1.10` to GitHub actions
- Reduce memory allocation in `lts()` and `lms()`


# v0.11.3
Expand Down
18 changes: 11 additions & 7 deletions src/lms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,26 @@ function lms(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters = not
iters = minimum([500 * p, 3000])
end
bestobjective = Inf
bestparamaters = []
bestres = []
bestparamaters = zeros(Float64, p)
bestres = zeros(Float64, n)
origres = zeros(Float64, n)
indices = collect(1:n)
kindices = collect(p:n)
betas = zeros(Float64, p)
res = zeros(Float64, n)

for _ = 1:iters
try
k = rand(kindices, 1)[1]
sampledindices = sample(indices, k, replace = false)
betas = X[sampledindices, :] \ y[sampledindices]
origres = y .- X * betas
res = sort(origres .^ 2.0)
betas .= X[sampledindices, :] \ y[sampledindices]
origres .= y .- X * betas
res .= sort(origres .^ 2.0)
m2 = res[h]
if m2 < bestobjective
bestparamaters = betas
bestparamaters .= betas
bestobjective = m2
bestres = origres
bestres .= origres
end
catch e
@warn e
Expand Down
7 changes: 4 additions & 3 deletions src/lts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,17 @@ function lts(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters=nothi

allindices = collect(1:n)
bestobjective = Inf
besthsubset = []
besthsubset = zeros(Int, h)
subsetindices = zeros(Int, p)

bestobjectiveunchanged = 0

for _ = 1:iters
subsetindices = sample(allindices, p, replace=false)
subsetindices .= sample(allindices, p, replace=false)
objective, hsubsetindices = iterateCSteps(X, y, subsetindices, h)
if objective < bestobjective
bestobjective = objective
besthsubset = hsubsetindices
besthsubset .= hsubsetindices
bestobjectiveunchanged = 0
else
bestobjectiveunchanged += 1
Expand Down

0 comments on commit 6a42574

Please sign in to comment.