Skip to content

Commit 38ce11c

Browse files
committed
Reduce memory allocations in hs93
1 parent 63912a2 commit 38ce11c

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Add `Julia v.1.10` to GitHub actions
55
- Reduce memory allocations in `lts()` and `lms()`
66
- Reduce memory allocations in `hadi92()` and `hadi94()`
7+
- Reduce memory allocations in `hs93()`
78

89
# v0.11.3
910

src/hs93.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,12 @@ function hs93basicsubset(
115115
h = floor((n + p - 1) / 2)
116116
s = length(initialindices)
117117
indices = initialindices
118+
betas = zeros(Float64, p)
119+
d = zeros(Float64, n)
120+
orderingd = zeros(Int, n)
121+
118122
for i in range(s + 1, stop = h)
119-
betas = X[indices, :] \ y[indices]
120-
d = zeros(Float64, n)
123+
betas = X[indices, :] \ y[indices]
121124
XM = X[indices, :]
122125
for j = 1:n
123126
if det(XM'XM) > 0
@@ -132,7 +135,7 @@ function hs93basicsubset(
132135
d[j] = maximum(y)
133136
end
134137
end
135-
orderingd = sortperm(abs.(d))
138+
orderingd .= sortperm(abs.(d))
136139
indices = orderingd[1:Int(i)]
137140
end
138141
return indices
@@ -200,13 +203,15 @@ function hs93(
200203
indices = basicsubsetindices
201204
n, p = size(X)
202205
s = length(indices)
203-
betas = []
204-
206+
betas = zeros(Float64, p)
207+
d = zeros(Float64, n)
208+
orderingd = zeros(Int, n)
209+
205210
while s < n
206-
betas = X[indices, :] \ y[indices]
211+
betas .= X[indices, :] \ y[indices]
207212
resids = y[indices] - X[indices,:] * betas
208213
sigma = sqrt(sum(resids .^ 2.0) / (length(resids) - p))
209-
d = zeros(Float64, n)
214+
210215
XM = X[indices, :]
211216

212217
if det(XM'XM) <= 0
@@ -228,7 +233,7 @@ function hs93(
228233
d[j] = (y[j] - sum(X[j, :] .* betas)) / (sigma * sqrt(abs(1.0 + xMMx)))
229234
end
230235
end
231-
orderingd = sortperm(abs.(d))
236+
orderingd .= sortperm(abs.(d))
232237
tdist = TDist(s - p)
233238
tcalc = quantile(tdist, alpha / (2 * (s + 1)))
234239

0 commit comments

Comments
 (0)