Skip to content

Commit

Permalink
Fix rule of thumbs for gamma initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Englhardt authored and englhardt committed Apr 7, 2020
1 parent 5390d34 commit ac68d43
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/init_strategies/strategies_gamma.jl
Expand Up @@ -16,7 +16,8 @@ Silverman, Bernard W. Density estimation for statistics and data analysis. Routl
struct RuleOfThumbSilverman <: InitializationStrategyGamma end

function rule_of_thumb_silverman(data::Array{T,2}) where T <: Real
return (size(data, 2) * (size(data, 1) + 2) / 4.0)^(-1.0 / (size(data,1) + 4.0))
s = (size(data, 2) * (size(data, 1) + 2) / 4.0)^(-1.0 / (size(data,1) + 4.0))
return 1 / (2 * s^2)
end

calculate_gamma(model, strategy::RuleOfThumbSilverman) = rule_of_thumb_silverman(model.data)
Expand All @@ -32,7 +33,8 @@ Scott, David W. Multivariate density estimation: theory, practice, and visualiza
struct RuleOfThumbScott <: InitializationStrategyGamma end

function rule_of_scott(data::Array{T,2}) where T <: Real
return size(data, 2)^(-1.0/(size(data, 1) + 4))
s = size(data, 2)^(-1.0/(size(data, 1) + 4))
return 1 / (2 * s^2)
end

calculate_gamma(model, strategy::RuleOfThumbScott) = rule_of_scott(model.data)
Expand Down
14 changes: 4 additions & 10 deletions test/init_strategies/init_strategies_test.jl
Expand Up @@ -6,16 +6,10 @@
pools = fill(:U, size(dummy_data, 2))
model = SVDD.VanillaSVDD(dummy_data)

@testset "RuleOfThumbSilverman" begin
# see https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.stats.gaussian_kde.html
expected = (n * (d + 2) / 4.0)^(-1.0 / (d + 4))
@test expected == SVDD.calculate_gamma(model, SVDD.RuleOfThumbSilverman())
end

@testset "RuleOfThumbScott" begin
# see https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.stats.gaussian_kde.html
expected = n^(-1.0 / (d + 4))
@test expected == SVDD.calculate_gamma(model, SVDD.RuleOfThumbScott())
for s in [:RuleOfThumbSilverman, :RuleOfThumbScott]
@testset "$s" begin
@test SVDD.calculate_gamma(model, SVDD.eval(s)()) > 0
end
end

@testset "TaxErrorEstimate" begin
Expand Down

0 comments on commit ac68d43

Please sign in to comment.