From adb400cdeca5f80fa20ab9c07127239f6b7872d7 Mon Sep 17 00:00:00 2001 From: "mhsatman@gmail.com" Date: Sun, 26 May 2024 20:34:56 +0300 Subject: [PATCH] add stress test for lms and lts --- src/lms.jl | 16 ++++++++-------- src/lts.jl | 8 ++++---- test/testlms.jl | 12 +++++++++++- test/testlts.jl | 19 ++++++++++++++++++- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/lms.jl b/src/lms.jl index d2d1bc0..7950125 100644 --- a/src/lms.jl +++ b/src/lms.jl @@ -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 diff --git a/src/lts.jl b/src/lts.jl index 7dfe379..cbb637c 100644 --- a/src/lts.jl +++ b/src/lts.jl @@ -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 diff --git a/test/testlms.jl b/test/testlms.jl index 7c4cbc7..cae5ec8 100644 --- a/test/testlms.jl +++ b/test/testlms.jl @@ -1,4 +1,4 @@ -@testset "LMS" begin +@testset "LMS" verbose = true begin @testset "LMS - Algorithm - Random data" begin # Create simple data @@ -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 diff --git a/test/testlts.jl b/test/testlts.jl index 777a6b9..2215b53 100644 --- a/test/testlts.jl +++ b/test/testlts.jl @@ -1,4 +1,4 @@ -@testset "LTS" begin +@testset "LTS" verbose = true begin @testset "LTS - Algorithm - Random data" begin # Create simple data @@ -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