Skip to content

Commit 109a142

Browse files
committed
replace zeros() with Array{Type, Dim}(undef, ...) if possible to reduce redundant assignments
1 parent d918a5c commit 109a142

13 files changed

Lines changed: 38 additions & 35 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Reduce allocations in OLS and LTS. Implements new olsf() and olsf!() functions.
44
- Recude allocations in hs93.
5+
- Replace zeros() with Array{Type, dim}(undef, ...) if possible to reduce redundant assignments.
56

67

78
# v0.11.7

src/atkinson94.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,22 @@ function atkinson94(
7474
end
7575

7676
bestobjective = Inf
77-
bestparameters = zeros(Float64, p)
78-
bestres = zeros(Float64, n, n)
77+
bestparameters = Array{Float64, 1}(undef, p)
78+
bestres = Array{Float64, 2}(undef, n, n)
7979
bestindex = 0
8080
indices = collect(1:n)
8181

8282
# store all the sigma values across all iterations
83-
sigmas = zeros(Float64, iters, n)
83+
sigmas = Array{Float64, 2}(undef, iters, n)
8484

8585
for iter = 1:iters
8686
m_subset_indices = sample(indices, p, replace = false)
8787

8888
# stores the n - p sigma values of a forward run
89-
sigma_tilde = zeros(Float64, n)
89+
sigma_tilde = Array{Float64, 1}(undef, n)
9090

9191
# stores the (n - p) * n residuals during the forward run
92-
studentized_residuals = zeros(Float64, n, n)
92+
studentized_residuals = Array{Float64, 2}(undef, n, n)
9393
copy_parameters = false
9494

9595
for m = p:n

src/bacon.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function bacon_multivariate_outlier_detection(
107107
r_prev = 0
108108
r = length(initial_basic_subset)
109109
subset = initial_basic_subset
110-
distances = zeros(Float64, n)
110+
distances = Array{Float64, 1}(undef, n)
111111

112112
# iterate until the size of the subset no longer changes
113113
iter = 0
@@ -156,7 +156,7 @@ function compute_t_distance(X::AbstractMatrix{Float64}, y::Vector{Float64}, subs
156156

157157
n, p = size(X)
158158

159-
t = zeros(Float64, n)
159+
t = Array{Float64, 1}(undef, n)
160160

161161
least_squares_fit = ols(X[subset, :], y[subset])
162162

src/deepestregression.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function deepestregression(X::AbstractMatrix{Float64}, y::AbstractVector{Float64
4444
n, p = size(drdata)
4545
n = Int32(n)
4646
p = Int32(p)
47-
betas = zeros(Float64, p)
47+
betas = Array{Float64, 1}(undef, p)
4848
maxit = Int32(maxit)
4949
iter = Int32(1)
5050
MDEPAPPR = Int32(p)

src/diagnostics.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Computational Statistics & Data Analysis 43.4 (2003): 541-552.
4444
"""
4545
function euclideanDistances(dataMatrix::AbstractMatrix{Float64})::AbstractMatrix{Float64}
4646
n = size(dataMatrix, 1)
47-
d = zeros(Float64, n, n)
47+
d = Array{Float64, 2}(undef, n, n)
4848
for i 1:n
4949
for j i:n
5050
if i != j
@@ -60,7 +60,7 @@ end
6060

6161
function mahalanobisSquaredBetweenPairs(pairs::AbstractMatrix{Float64}; covmatrix = nothing)::Union{Nothing, AbstractMatrix}
6262
n = size(pairs, 1)
63-
newmat = zeros(Float64, n, n)
63+
newmat = Array{Float64, 2}(undef, n, n)
6464
if isnothing(covmatrix)
6565
covmatrix = cov(pairs)
6666
end
@@ -105,7 +105,7 @@ function mahalanobisBetweenPairs(dataMatrix::AbstractMatrix{Float64})::Union{Not
105105

106106
n = size(dataMatrix, 1)
107107

108-
d = zeros(Float64, n, n)
108+
d = Array{Float64, 2}(undef, n, n)
109109

110110
covmat = cov(dataMatrix)
111111

@@ -499,7 +499,7 @@ function cooks(X::AbstractMatrix{Float64}, y::AbstractVector{Float64})::Abstract
499499
res = residuals(olsreg)
500500
hat = hatmatrix(X)
501501
s2 = sum(res .* res) / (n - p)
502-
d = zeros(Float64, n)
502+
d = Array{Float64, 1}(undef, n)
503503
for i 1:n
504504
d[i] = ((res[i]^2.0) / (p * s2)) * (hat[i, i] / (1 - hat[i, i])^2.0)
505505
end
@@ -719,7 +719,7 @@ function hadimeasure(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; c::
719719
res2 = res .^ 2.0
720720
sumres = sum(res2)
721721
hat = hatmatrix(X)
722-
H = zeros(Float64, n)
722+
H = Array{Float64, 1}(undef, n)
723723
for i 1:n
724724
H[i] =
725725
(p * res2[i]) / ((1 - hat[i, i]) * (sumres - res2[i])) +

src/hadi1992.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function hadi1992_handle_singularity(S::AbstractMatrix{Float64})::AbstractMatrix
3434
values = eigen_structure.values
3535
vectors = eigen_structure.vectors
3636
lambda_s = find_minimum_nonzero(values)
37-
W = zeros(Float64, p, p)
37+
W = Array{Float64, 2}(undef, p, p)
3838
for i = 1:p
3939
W[i, i] = 1 / max(values[i], lambda_s)
4040
end

src/hs93.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ function hs93basicsubset(
164164
if h > s
165165
resize!(indices, h)
166166
end
167-
betas = zeros(Float64, p)
168-
d = zeros(Float64, n)
167+
betas = Array{Float64, 1}(undef, p)
168+
d = Array{Float64, 1}(undef, n)
169169
orderingd = Array{Int}(undef, n)
170170
xtx = Matrix{Float64}(undef, p, p)
171171
insubset = falses(n)
@@ -273,9 +273,9 @@ function hs93(
273273
indices = basicsubsetindices
274274
n, p = size(X)
275275
s = length(indices)
276-
betas = zeros(Float64, p)
277-
d = zeros(Float64, n)
278-
orderingd = Array{Int}(undef, n)
276+
betas = Array{Float64, 1}(undef, p)
277+
d = Array{Float64, 1}(undef, n)
278+
orderingd = Array{Int, 1}(undef, n)
279279
if s < n
280280
workingsubset = copy(indices)
281281
resize!(workingsubset, n)

src/imon2005.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function imon2005(X::AbstractMatrix{Float64}, y::AbstractVector{Float64})
6464
XRXR = transpose(XR) * XR
6565
invXRXR = inv(XRXR)
6666
wiiR = [X[i, :]' * invXRXR * X[i, :] for i in allindex]
67-
wiiRAsterix = zeros(Float64, n)
67+
wiiRAsterix = Array{Float64, 1}(undef, n)
6868
for i in allindex
6969
if i in R
7070
wiiRAsterix[i] = wiiR[i] / (1.0 - wiiR[i])
@@ -76,7 +76,7 @@ function imon2005(X::AbstractMatrix{Float64}, y::AbstractVector{Float64})
7676
BetaHatR = coef(RegR)
7777
resR = y - X * BetaHatR
7878
sigmaR = sum(resR .^ 2.0) / (n - p)
79-
tAsterix = zeros(Float64, n)
79+
tAsterix = Array{Float64, 1}(undef, n)
8080
for i in allindex
8181
if i in R
8282
tAsterix[i] = resR[i] / (SigmaWithoutIndex(X, y, R, i) * sqrt(1.0 - wiiR[i]))
@@ -85,7 +85,7 @@ function imon2005(X::AbstractMatrix{Float64}, y::AbstractVector{Float64})
8585
end
8686
end
8787

88-
GDFFITS = zeros(Float64, n)
88+
GDFFITS = Array{Float64, 1}(undef, n)
8989
for i in allindex
9090
GDFFITS[i] = sqrt(wiiRAsterix[i]) * tAsterix[i]
9191
end

src/ks89.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ function ks89(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; alpha = 0.
9494
orderingindices = sortperm(abs.(stdres))
9595
n, p = size(X)
9696
basisindices = orderingindices[1:p]
97-
w = zeros(Float64, n)
98-
s = zeros(Float64, n)
99-
ws = zeros(Float64, n)
97+
w = Array{Float64, 1}(undef, n)
98+
s = Array{Float64, 1}(undef, n)
99+
ws = Array{Float64, 1}(undef, n)
100100
for i = (p+1):n
101101
index = orderingindices[i]
102102
w[index] = ks89RecursiveResidual(X, y, basisindices, index)

src/py95.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function py95ProcessEigenVector(v::AbstractVector{Float64})
3333
c1 = Int(floor(n / 4.0))
3434
c2 = Int(floor(n / 4.0))
3535
ordered_coordinates = sortperm(v)
36-
a = zeros(Float64, n)
37-
b = zeros(Float64, n)
36+
a = Array{Float64, 1}(undef, n)
37+
b = Array{Float64, 1}(undef, n)
3838
for i = n:(-1):(n-c1)
3939
a[i] = v[i] / v[i-1]
4040
end
@@ -88,8 +88,10 @@ function py95SuspectedObservations(X::AbstractMatrix{Float64}, y::AbstractVector
8888
resids = residuals(olsreg)
8989
H = hatmatrix(X)
9090
s2 = sum(resids .^ 2.0) / (n - p)
91-
D = zeros(Float64, (n, n))
92-
E = zeros(Float64, (n, n))
91+
D = Array{Float64, 2}(undef, n, n)
92+
E = Array{Float64, 2}(undef, n, n)
93+
fill!(D, 0.0)
94+
fill!(E, 0.0)
9395
for i = 1:n
9496
D[i, i] = 1 / (1 - H[i, i])
9597
E[i, i] = resids[i]

0 commit comments

Comments
 (0)