Skip to content

Commit 6aa8241

Browse files
committed
robcov doesn't use try and catch any more
1 parent b4c0742 commit 6aa8241

2 files changed

Lines changed: 35 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- Replace `@assert` macro with `throw(ErrorException())` in whole code
77
- `depestregression` returns `Dict` instead of a `vector` of betas like other regression methods.
88
- `summary()` throws `ErrorException` rather than simply prompting with `@error` macro.
9-
9+
- `robcov` doesn't use try and catch any more.
1010

1111

1212
# v0.11.3

src/mve.jl

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import LinearAlgebra: diag, det
88
import Distributions: median, cov, mean, quantile, sample, Chisq
99

1010
import ..Basis:
11-
RegressionSetting,
11+
RegressionSetting,
1212
@extractRegressionSetting, designMatrix, responseVector, applyColumns, applyColumns!
1313

1414
import ..Diagnostics: mahalanobisSquaredMatrix
@@ -18,14 +18,14 @@ function enlargesubset(initialsubset, data::AbstractMatrix, h::Int)
1818
p = size(data, 2)
1919

2020
basicsubset = copy(initialsubset)
21-
21+
2222
meanvector = Array{Float64}(undef, p)
2323

2424
while length(basicsubset) < h
2525
applyColumns!(meanvector, mean, data[basicsubset, :])
2626
covmatrix = cov(data[basicsubset, :])
2727
md2mat =
28-
mahalanobisSquaredMatrix(data, meanvector = meanvector, covmatrix = covmatrix)
28+
mahalanobisSquaredMatrix(data, meanvector=meanvector, covmatrix=covmatrix)
2929
md2 = diag(md2mat)
3030
md2sortedindex = sortperm(md2)
3131
basicsubset = md2sortedindex[1:(length(basicsubset)+1)]
@@ -34,8 +34,8 @@ function enlargesubset(initialsubset, data::AbstractMatrix, h::Int)
3434
end
3535

3636

37-
function robcov(data::Matrix; alpha = 0.01, estimator = :mve)
38-
37+
function robcov(data::Matrix; alpha=0.01, estimator=:mve)
38+
3939
n, p = size(data)
4040
chisquared = Chisq(p)
4141
chisqcrit = quantile(chisquared, 1.0 - alpha)
@@ -46,10 +46,10 @@ function robcov(data::Matrix; alpha = 0.01, estimator = :mve)
4646
mingoal = Inf
4747

4848
maxiter = minimum([p * 500, 3000])
49-
49+
5050
initialsubset = Array{Int}(undef, k)
5151
bestinitialsubset = Array{Int}(undef, k)
52-
52+
5353
hsubset = Array{Int}(undef, h)
5454
besthsubset = Array{Int}(undef, h)
5555

@@ -58,51 +58,50 @@ function robcov(data::Matrix; alpha = 0.01, estimator = :mve)
5858
fill!(meanvector, 0.0)
5959

6060

61-
for iter = 1:maxiter
61+
for _ = 1:maxiter
6262
goal = Inf
6363

6464

65-
try
66-
initialsubset = sample(indices, k, replace = false)
67-
hsubset = enlargesubset(initialsubset, data, h)
68-
covmatrix = cov(data[hsubset, :])
69-
if estimator == :mve
70-
applyColumns!(meanvector, mean, data[hsubset, :])
71-
md2mat = mahalanobisSquaredMatrix(
72-
data,
73-
meanvector = meanvector,
74-
covmatrix = covmatrix,
75-
)
65+
initialsubset = sample(indices, k, replace=false)
66+
hsubset = enlargesubset(initialsubset, data, h)
67+
covmatrix = cov(data[hsubset, :])
68+
if estimator == :mve
69+
applyColumns!(meanvector, mean, data[hsubset, :])
70+
md2mat = mahalanobisSquaredMatrix(
71+
data,
72+
meanvector=meanvector,
73+
covmatrix=covmatrix,
74+
)
75+
if !isnothing(md2mat)
7676
DJ = sqrt(sort(diag(md2mat))[h])
7777
goal = (DJ / c)^p * det(covmatrix)
78-
else
79-
goal = det(covmatrix)
8078
end
81-
catch e
82-
# Possibly singularity
79+
else
80+
goal = det(covmatrix)
8381
end
8482

8583

84+
8685
if goal < mingoal
8786
mingoal = goal
8887
bestinitialsubset = initialsubset
8988
besthsubset = hsubset
9089
end
9190
end
92-
91+
9392

9493

9594
applyColumns!(meanvector, mean, data[besthsubset, :])
9695
covmatrix = cov(data[besthsubset, :])
9796
md2 = diag(
9897
mahalanobisSquaredMatrix(
9998
data,
100-
meanvector = meanvector,
101-
covmatrix = covmatrix,
99+
meanvector=meanvector,
100+
covmatrix=covmatrix,
102101
),
103102
)
104103
outlierset = filter(x -> md2[x] > chisqcrit, 1:n)
105-
result = Dict{String, Any}()
104+
result = Dict{String,Any}()
106105
result["goal"] = mingoal
107106
result["best.subset"] = sort(besthsubset)
108107
result["robust.location"] = meanvector
@@ -146,12 +145,12 @@ are directly comparible with quantiles of a ChiSquare Distribution with `p` degr
146145
Van Aelst, Stefan, and Peter Rousseeuw. "Minimum volume ellipsoid." Wiley
147146
Interdisciplinary Reviews: Computational Statistics 1.1 (2009): 71-82.
148147
"""
149-
function mve(data::DataFrame; alpha = 0.01)
150-
robcov(Matrix(data), alpha = alpha, estimator = :mve)
148+
function mve(data::DataFrame; alpha=0.01)
149+
robcov(Matrix(data), alpha=alpha, estimator=:mve)
151150
end
152151

153-
function mve(data::AbstractMatrix{Float64}; alpha = 0.01)
154-
robcov(data, alpha = alpha, estimator = :mve)
152+
function mve(data::AbstractMatrix{Float64}; alpha=0.01)
153+
robcov(data, alpha=alpha, estimator=:mve)
155154
end
156155

157156

@@ -190,12 +189,12 @@ However, details about number of iterations are slightly different.
190189
Rousseeuw, Peter J., and Katrien Van Driessen. "A fast algorithm for the minimum covariance
191190
determinant estimator." Technometrics 41.3 (1999): 212-223.
192191
"""
193-
function mcd(data::DataFrame; alpha = 0.01)
194-
robcov(Matrix(data), alpha = alpha, estimator = :mcd)
192+
function mcd(data::DataFrame; alpha=0.01)
193+
robcov(Matrix(data), alpha=alpha, estimator=:mcd)
195194
end
196195

197-
function mcd(data::AbstractMatrix{Float64}; alpha = 0.01)
198-
robcov(data, alpha = alpha, estimator = :mcd)
196+
function mcd(data::AbstractMatrix{Float64}; alpha=0.01)
197+
robcov(data, alpha=alpha, estimator=:mcd)
199198
end
200199

201200

0 commit comments

Comments
 (0)