Skip to content

Commit

Permalink
fix hadi92 and hadi94
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytecode committed May 26, 2024
1 parent bb6819f commit e289030
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
32 changes: 16 additions & 16 deletions src/hadi1992.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,48 +111,48 @@ function hadi1992(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
ordering_indices_mah1 = sortperm(mah1)

r = p + 1
basic_subset_indices = []
basic_subset = []
sorted_mah1 = []
basic_subset_indices = Int[]
basic_subset = Int[]
sorted_mah1 = Float64[]

Cb = zeros(Float64, p)
Sb = zeros(Float64, p, p)
newSb = zeros(Float64, p, p)
Cb = Array{Float64}(undef, p)
Sb = Matrix{Float64}(undef, p, p)
newSb = Matrix{Float64}(undef, p, p)

msm3 = zeros(Float64, n, n)
msm4 = zeros(Float64, n, n)
msm3 = Matrix{Float64}(undef, n, n)
msm4 = Matrix{Float64}(undef, n, n)

while r < n
cnpr = 1 + (r / (n - p))^2.0
basic_subset_indices = ordering_indices_mah1[1:r]
basic_subset = multivariateData[basic_subset_indices, :]
Cb .= applyColumns(mean, basic_subset)
Sb .= cov(basic_subset)
Cb = applyColumns(mean, basic_subset)
Sb = cov(basic_subset)

r += 1
cfactor = cnpr * sqrt(sort(mah1)[h]) / chi_50_quantile
if det(cfactor * Sb) == 0
@info "singular Sb case"
newSb .= hadi1992_handle_singularity(cfactor * Sb)
# singular Sb case
newSb = hadi1992_handle_singularity(cfactor * Sb)

msm3 .= mahalanobisSquaredMatrix(multivariateData, meanvector = Cb, covmatrix = newSb,)
msm3 = mahalanobisSquaredMatrix(multivariateData, meanvector = Cb, covmatrix = newSb,)

if isnothing(msm3)
throw(ErrorException("Mahalanobis distances are not calculated"))
end

mah1 .= diag(msm3)
mah1 = diag(msm3)

ordering_indices_mah1 = sortperm(mah1)
basic_subset_indices = ordering_indices_mah1[1:r]
else
msm4 .= mahalanobisSquaredMatrix(multivariateData, meanvector = Cb, covmatrix = (cfactor * Sb))
msm4 = mahalanobisSquaredMatrix(multivariateData, meanvector = Cb, covmatrix = (cfactor * Sb))

if isnothing(msm4)
throw(ErrorException("Mahalanobis distances are not calculated"))
end

mah1 .= diag(msm4)
mah1 = diag(msm4)
ordering_indices_mah1 = sortperm(mah1)
basic_subset_indices = ordering_indices_mah1[1:r]
end
Expand Down
20 changes: 10 additions & 10 deletions src/hadi1994.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ function hadi1994(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)

basic_subset_indices = Int[]

basic_subset = []
basic_subset = Int[]

sorted_mah1 = []
sorted_mah1 = Int[]

Cb = zeros(Float64, p)
Sb = zeros(Float64, p, p)
msm3 = zeros(Float64, n, n)
sorted_mah1 = zeros(Float64, n)
Cb = Float64[]
Sb = Matrix{Float64}(undef, p, p)
msm3 = Matrix{Float64}(undef, p, p)
sorted_mah1 = Vector{Float64}(undef, n)

cfactor = 0
isFullRank = false
Expand All @@ -105,8 +105,8 @@ function hadi1994(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
while !isFullRank
basic_subset_indices = ordering_indices_mah1[1:r]
basic_subset = multivariateData[basic_subset_indices, :]
Cb .= applyColumns(mean, basic_subset)
Sb .= cov(basic_subset)
Cb = applyColumns(mean, basic_subset)
Sb = cov(basic_subset)
cfactor = cnp * sqrt(sort(mah1)[h]) / chi_50_quantile
r += 1

Expand All @@ -117,7 +117,7 @@ function hadi1994(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
end
end

msm3 .= mahalanobisSquaredMatrix(
msm3 = mahalanobisSquaredMatrix(
multivariateData,
meanvector = Cb,
covmatrix = (cfactor * Sb),
Expand All @@ -132,7 +132,7 @@ function hadi1994(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
ordering_indices_mah1 = sortperm(mah1)
basic_subset_indices = ordering_indices_mah1[1:r]

sorted_mah1 .= sort(mah1)
sorted_mah1 = sort(mah1)

if sorted_mah1[r] >= critical_quantile
break
Expand Down

0 comments on commit e289030

Please sign in to comment.