Skip to content

Commit

Permalink
introduce applyColumns! for inplace replacement of calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytecode committed May 24, 2024
1 parent 645d791 commit eb0d23d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/LinRegOutliers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ import .Basis:
createRegressionSetting,
@extractRegressionSetting,
applyColumns,
applyColumns!,
find_minimum_nonzero,
designMatrix,
responseVector

export RegressionSetting
export createRegressionSetting
export designMatrix
export responseVector
export applyColumns
export applyColumns!
export find_minimum_nonzero
export @extractRegressionSetting

Expand Down
10 changes: 9 additions & 1 deletion src/basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Basis
export RegressionSetting
export createRegressionSetting
export @extractRegressionSetting
export applyColumns
export applyColumns, applyColumns!
export find_minimum_nonzero


Expand Down Expand Up @@ -297,6 +297,14 @@ function applyColumns(f::F, data::AbstractMatrix{Float64}) where {F <: Function}
end


function applyColumns!(target::Vector, f::F, data::AbstractMatrix) where {F <: Function}
for i in 1:size(data, 2)
target[i] = f(data[:, i])
end
return target
end



"""
Expand Down
50 changes: 26 additions & 24 deletions src/mve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@ import LinearAlgebra: diag, det
import Distributions: median, cov, mean, quantile, sample, Chisq

import ..Basis:
RegressionSetting, @extractRegressionSetting, designMatrix, responseVector, applyColumns
RegressionSetting,
@extractRegressionSetting, designMatrix, responseVector, applyColumns, applyColumns!

import ..Diagnostics: mahalanobisSquaredMatrix


function enlargesubset(initialsubset, data::AbstractMatrix, h::Int)
n, p = size(data)
p = size(data, 2)

basicsubset = copy(initialsubset)

meanvector = Array{Float64}(undef, p)
covmatrix = Matrix{Float64}(undef, p, p)
md2mat = Matrix{Float64}(undef, n, n)
md2 = Array{Float64}(undef, n)
md2sortedindex = Array{Int}(undef, n)

while length(basicsubset) < h
meanvector .= applyColumns(mean, data[basicsubset, :])
covmatrix .= cov(data[basicsubset, :])
md2mat .=
applyColumns!(meanvector, mean, data[basicsubset, :])
covmatrix = cov(data[basicsubset, :])
md2mat =
mahalanobisSquaredMatrix(data, meanvector = meanvector, covmatrix = covmatrix)
md2 .= diag(md2mat)
md2sortedindex .= sortperm(md2)
md2 = diag(md2mat)
md2sortedindex = sortperm(md2)
basicsubset = md2sortedindex[1:(length(basicsubset)+1)]
end
return basicsubset
Expand All @@ -55,21 +53,22 @@ function robcov(data::Matrix; alpha = 0.01, estimator = :mve)
hsubset = Array{Int}(undef, h)
besthsubset = Array{Int}(undef, h)

covmatrix = Matrix{Float64}(undef, p, p)

meanvector = Array{Float64}(undef, p)
md2mat = Matrix{Float64}(undef, n, n)
fill!(meanvector, 0.0)

md2 = Array{Float64}(undef, n)

for iter = 1:maxiter
goal = Inf


try
initialsubset .= sample(indices, k, replace = false)
hsubset .= enlargesubset(initialsubset, data, h)
covmatrix .= cov(data[hsubset, :])
initialsubset = sample(indices, k, replace = false)
hsubset = enlargesubset(initialsubset, data, h)
covmatrix = cov(data[hsubset, :])
if estimator == :mve
meanvector .= applyColumns(mean, data[hsubset, :])
md2mat .= mahalanobisSquaredMatrix(
applyColumns!(meanvector, mean, data[hsubset, :])
md2mat = mahalanobisSquaredMatrix(
data,
meanvector = meanvector,
covmatrix = covmatrix,
Expand All @@ -82,17 +81,20 @@ function robcov(data::Matrix; alpha = 0.01, estimator = :mve)
catch e
# Possibly singularity
end


if goal < mingoal
mingoal = goal
bestinitialsubset .= initialsubset
besthsubset .= hsubset
bestinitialsubset = initialsubset
besthsubset = hsubset
end
end


meanvector .= applyColumns(mean, data[besthsubset, :])
covmatrix .= cov(data[besthsubset, :])
md2 .= diag(

applyColumns!(meanvector, mean, data[besthsubset, :])
covmatrix = cov(data[besthsubset, :])
md2 = diag(
mahalanobisSquaredMatrix(
data,
meanvector = meanvector,
Expand Down

0 comments on commit eb0d23d

Please sign in to comment.