Skip to content

Commit 609f1a5

Browse files
test: add discriminating noise-application checks to sparse noise tests
1 parent 11c7553 commit 609f1a5

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

lib/StochasticDiffEq/test/sparse_noise_tests.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
using StochasticDiffEq, SparseArrays, Test, Random
22

3-
Random.seed!(42)
4-
5-
# 4-state system driven by 2 independent Wiener processes.
6-
# Only states 1 and 3 are coupled to noise; states 2 and 4 are purely deterministic.
7-
# The noise matrix is 4×2 sparse: g[1,1] = 0.1*u[1], g[3,2] = 0.1*u[3], rest zero.
8-
93
function f_sparse!(du, u, p, t)
104
return du .= -u
115
end
@@ -25,23 +19,29 @@ noise_prototype[3, 2] = 1.0
2519
prob = SDEProblem(f_sparse!, g_sparse!, u0, tspan, noise_rate_prototype = noise_prototype)
2620

2721
@testset "Sparse noise_rate_prototype — EM IIP" begin
22+
Random.seed!(42)
2823
sol = solve(prob, EM(), dt = 0.01)
2924
@test sol.retcode == ReturnCode.Success
3025
@test length(sol) > 1
31-
# States 2 and 4 have zero noise rows → evolve purely as du/dt = -u → u(1) = exp(-1)
26+
# Dimensions 2 and 4 have no noise: pure drift exp(-t)
3227
@test sol.u[end][2] exp(-1.0) rtol = 0.02
3328
@test sol.u[end][4] exp(-1.0) rtol = 0.02
29+
# Dimensions 1 and 3 receive noise: path must deviate from pure drift
30+
@test !isapprox(sol.u[end][1], exp(-1.0), rtol = 0.005)
31+
@test !isapprox(sol.u[end][3], exp(-1.0), rtol = 0.005)
3432
end
3533

3634
@testset "Sparse noise_rate_prototype — EulerHeun IIP" begin
35+
Random.seed!(42)
3736
sol = solve(prob, EulerHeun(), dt = 0.01)
3837
@test sol.retcode == ReturnCode.Success
3938
@test length(sol) > 1
4039
@test sol.u[end][2] exp(-1.0) rtol = 0.02
4140
@test sol.u[end][4] exp(-1.0) rtol = 0.02
41+
@test !isapprox(sol.u[end][1], exp(-1.0), rtol = 0.005)
42+
@test !isapprox(sol.u[end][3], exp(-1.0), rtol = 0.005)
4243
end
4344

44-
# OOP variant
4545
function f_sparse_oop(u, p, t)
4646
return -u
4747
end
@@ -56,8 +56,11 @@ end
5656
prob_oop = SDEProblem(f_sparse_oop, g_sparse_oop, u0, tspan, noise_rate_prototype = noise_prototype)
5757

5858
@testset "Sparse noise_rate_prototype — EM OOP" begin
59+
Random.seed!(42)
5960
sol = solve(prob_oop, EM(), dt = 0.01)
6061
@test sol.retcode == ReturnCode.Success
6162
@test sol.u[end][2] exp(-1.0) rtol = 0.02
6263
@test sol.u[end][4] exp(-1.0) rtol = 0.02
64+
@test !isapprox(sol.u[end][1], exp(-1.0), rtol = 0.005)
65+
@test !isapprox(sol.u[end][3], exp(-1.0), rtol = 0.005)
6366
end

lib/StochasticDiffEqCore/src/solve.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,6 @@ function _sde_init(
405405
SciMLBase.parameterless_type(u), zeros(randElType, size(noise_rate_prototype, 2))
406406
)
407407
elseif issparse(noise_rate_prototype)
408-
# Wiener increments must always be a dense vector regardless of whether
409-
# the noise matrix g is sparse; sparse dW has no defined mul! with sparse g.
410408
rand_prototype = zeros(randElType, size(noise_rate_prototype, 2))
411409
else
412410
rand_prototype = false .* noise_rate_prototype[1, :]

0 commit comments

Comments
 (0)