Skip to content

Commit

Permalink
add stress test for lms and lts
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytecode committed May 26, 2024
1 parent e289030 commit adb400c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
16 changes: 8 additions & 8 deletions src/lms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ function lms(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters = not
iters = minimum([500 * p, 3000])
end
bestobjective = Inf
bestparamaters = zeros(Float64, p)
bestres = zeros(Float64, n)
origres = zeros(Float64, n)
bestparamaters = Array{Float64}(undef, p)
bestres = Array{Float64}(undef, n)
origres = Array{Float64}(undef, n)
indices = collect(1:n)
kindices = collect(p:n)
betas = zeros(Float64, p)
res = zeros(Float64, n)
betas = Array{Float64}(undef, p)
res = Array{Float64}(undef, n)

for _ = 1:iters
try
k = rand(kindices, 1)[1]
sampledindices = sample(indices, k, replace = false)
betas .= X[sampledindices, :] \ y[sampledindices]
origres .= y .- X * betas
res .= sort(origres .^ 2.0)
betas = X[sampledindices, :] \ y[sampledindices]
origres = y .- X * betas
res = sort(origres .^ 2.0)
m2 = res[h]
if m2 < bestobjective
bestparamaters .= betas
Expand Down
8 changes: 4 additions & 4 deletions src/lts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ function lts(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters=nothi

allindices = collect(1:n)
bestobjective = Inf
besthsubset = zeros(Int, h)
subsetindices = zeros(Int, p)
besthsubset = Array{Int}(undef, h)
subsetindices = Array{Int}(undef, p)

bestobjectiveunchanged = 0

for _ = 1:iters
subsetindices .= sample(allindices, p, replace=false)
subsetindices = sample(allindices, p, replace=false)
objective, hsubsetindices = iterateCSteps(X, y, subsetindices, h)
if objective < bestobjective
bestobjective = objective
besthsubset .= hsubsetindices
besthsubset = hsubsetindices
bestobjectiveunchanged = 0
else
bestobjectiveunchanged += 1
Expand Down
12 changes: 11 additions & 1 deletion test/testlms.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testset "LMS" begin
@testset "LMS" verbose = true begin

@testset "LMS - Algorithm - Random data" begin
# Create simple data
Expand Down Expand Up @@ -33,4 +33,14 @@
@test 21 in outset
end


@testset "LMS - Stress test (n=1000, p=10)" begin
n = 1000
p = 10
x = rand(Float64, n, p)
y = rand(Float64, n)
result = lms(x, y)
@test true
end

end
19 changes: 18 additions & 1 deletion test/testlts.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testset "LTS" begin
@testset "LTS" verbose = true begin

@testset "LTS - Algorithm - Random data" begin
# Create simple data
Expand Down Expand Up @@ -32,4 +32,21 @@
@test 21 in outset
end

@testset "LTS - Stress test (n = 1000, p = 10)" begin
n = 1000
p = 10
x = rand(Float64, n, p)
y = rand(Float64, n)
result = lts(x, y)
@test true
end

@testset "LTS - Stress test (n = 10000, p = 10)" begin
n = 10000
p = 10
x = rand(Float64, n, p)
y = rand(Float64, n)
result = lts(x, y)
@test true
end
end

0 comments on commit adb400c

Please sign in to comment.