Skip to content

Commit

Permalink
adapt tests to changes in num steps
Browse files Browse the repository at this point in the history
  • Loading branch information
rmsrosa committed Jul 2, 2023
1 parent 7a5b903 commit e663cd0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 69 deletions.
46 changes: 23 additions & 23 deletions test/test_convergence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ custom_solver = function(xt::Vector{T}, t0::T, tf::T, x0::T, f::F, yt::Vector{T}
DimensionMismatch("The vectors `xt` and `yt` must match indices")
)

n = size(xt, 1)
dt = (tf - t0) / (n - 1)
n = size(xt, 1) - 1
dt = (tf - t0) / n
i1 = firstindex(xt)
xt[i1] = x0
integral = zero(T)
Expand Down Expand Up @@ -35,7 +35,7 @@ end

suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f, noise, target, method, ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
str = @test_nowarn generate_error_table(results)
Expand All @@ -52,7 +52,7 @@ end

suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f, noise, target, method, ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
str = @test_nowarn generate_error_table(results)
Expand All @@ -70,11 +70,11 @@ end
f = (t, x, y) -> (y[1] + 3y[2])/4 * x

target_exact! = function (xt, t0, tf, x0, f, yt, rng)
ntgt = size(xt, 1)
dt = (tf - t0) / (ntgt - 1)
ntgt = size(xt, 1) - 1
dt = (tf - t0) / ntgt
xt[1] = x0
integral = 0.0
for n in 2:ntgt
for n in 2:ntgt+1
integral += (yt[n, 1] + yt[n-1, 1] + 3yt[n, 2] + 3yt[n-1, 2]) * dt / 8 + sqrt(dt^3 / 12) * randn(rng)
xt[n] = x0 * exp(integral)
end
Expand All @@ -85,7 +85,7 @@ end

suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f, noise, target, method, ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
str = @test_nowarn generate_error_table(results)
Expand All @@ -97,7 +97,7 @@ end
suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f, noise, target, method, ntgt, ns, m)
suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f, noise, RandomEuler(), RandomEuler(), ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
str = @test_nowarn generate_error_table(results)
Expand All @@ -117,11 +117,11 @@ end
f! = (dx, t, x, y) -> (dx .= y .* x)

target_exact! = function (xt, t0, tf, x0, f!, yt, rng)
ntgt = size(xt, 1)
dt = (tf - t0) / (ntgt - 1)
ntgt = size(xt, 1) - 1
dt = (tf - t0) / ntgt
xt[1, :] .= x0
integral = 0.0
for n in 2:ntgt
for n in 2:ntgt+1
integral += (yt[n] + yt[n-1]) * dt / 2 + sqrt(dt^3 / 12) * randn(rng)
xt[n, :] .= exp(integral) * x0
end
Expand All @@ -132,7 +132,7 @@ end

suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f!, noise, target, method, ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
str = @test_nowarn generate_error_table(results)
Expand All @@ -143,7 +143,7 @@ end

suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f!, noise, target, method, ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
str = @test_nowarn generate_error_table(results)
Expand All @@ -167,11 +167,11 @@ end
f! = (dx, t, x, y) -> (dx .= (y[1] + 3y[2]) .* x ./ 4)

target_exact! = function (xt, t0, tf, x0, f!, yt, rng)
ntgt = size(xt, 1)
dt = (tf - t0) / (ntgt - 1)
ntgt = size(xt, 1) - 1
dt = (tf - t0) / ntgt
xt[1, :] .= x0
integral = 0.0
for n in 2:ntgt
for n in 2:ntgt+1
integral += (yt[n, 1] + yt[n-1, 1] + 3yt[n, 2] + 3yt[n-1, 2]) * dt / 8 + sqrt(dt^3 / 12) * randn(rng)
xt[n, :] .= exp(integral) * x0
end
Expand All @@ -182,7 +182,7 @@ end

suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f!, noise, target, method, ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
str = @test_nowarn generate_error_table(results)
Expand All @@ -193,7 +193,7 @@ end

suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f!, noise, target, method, ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
str = @test_nowarn generate_error_table(results)
Expand All @@ -213,11 +213,11 @@ end
f = (t, x, y) -> y * x / 10

target_exact! = function (xt, t0, tf, x0, f, yt, rng)
ntgt = size(xt, 1)
dt = (tf - t0) / (ntgt - 1)
ntgt = size(xt, 1) - 1
dt = (tf - t0) / ntgt
xt[1] = x0
integral = 0.0
for n in 2:ntgt
for n in 2:ntgt+1
integral += (yt[n] + yt[n-1]) * dt / 2 + sqrt(dt^3 / 12) * randn(rng)
xt[n] = x0 * exp(integral / 10)
end
Expand All @@ -228,7 +228,7 @@ end

suite = @test_nowarn ConvergenceSuite(t0, tf, x0law, f, noise, target, method, ntgt, ns, m)
results = @test_nowarn solve(rng, suite)
@test results.deltas (tf - t0) ./ (ns .- 1)
@test results.deltas (tf - t0) ./ ns
@test results.p 1.0 (atol = 0.1)
@test results.pmin results.p results.pmax
end
Expand Down
26 changes: 13 additions & 13 deletions test/test_noalloc_conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ target_exact1! = function (xt::AbstractVector{T}, t0::T, tf::T, x0::T, f::F, yt:
DimensionMismatch("The vectors `xt` and `yt` must match indices")
)

n = size(xt, 1)
dt = (tf - t0) / (n - 1)
n = size(xt, 1) - 1
dt = (tf - t0) / n
i1 = firstindex(xt)
xt[i1] = x0
integral = zero(T)
Expand All @@ -16,37 +16,37 @@ target_exact1! = function (xt::AbstractVector{T}, t0::T, tf::T, x0::T, f::F, yt:
end

target_exact2! = function (xt::AbstractVector{T}, t0::T, tf::T, x0::T, f::F, yt::AbstractMatrix{T}, rng::AbstractRNG) where {T, F}
ntgt = size(xt, 1)
dt = (tf - t0) / (ntgt - 1)
ntgt = size(xt, 1) - 1
dt = (tf - t0) / ntgt
xt[1] = x0
integral = 0.0
for n in 2:ntgt
for n in 2:ntgt+1
integral += (yt[n, 1] + yt[n-1, 1] + 3yt[n, 2] + 3yt[n-1, 2]) * dt / 8 + sqrt(dt^3 / 12) * randn(rng)
xt[n] = x0 * exp(integral)
end
end

target_exact3! = function (xt::AbstractMatrix{T}, t0::T, tf::T, x0::AbstractVector{T}, f::F, yt::AbstractVector{T}, rng::AbstractRNG) where {T, F}
ntgt = size(xt, 1)
dt = (tf - t0) / (ntgt - 1)
ntgt = size(xt, 1) - 1
dt = (tf - t0) / ntgt
for j in eachindex(axes(xt, 2), x0)
xt[1, j] = x0[j]
end
integral = 0.0
for n in 2:ntgt
for n in 2:ntgt+1
integral += (yt[n] + yt[n-1]) * dt / 2 + sqrt(dt^3 / 12) * randn(rng)
xt[n, :] .= exp(integral) .* x0
end
end

target_exact4! = function (xt::AbstractMatrix{T}, t0::T, tf::T, x0::AbstractVector{T}, f::F, yt::AbstractMatrix{T}, rng::AbstractRNG) where {T, F}
ntgt = size(xt, 1)
dt = (tf - t0) / (ntgt - 1)
ntgt = size(xt, 1) - 1
dt = (tf - t0) / ntgt
for j in eachindex(axes(xt, 2), x0)
xt[1, j] = x0[j]
end
integral = 0.0
for n in 2:ntgt
for n in 2:ntgt+1
integral += (yt[n, 1] + yt[n-1, 1] + 3yt[n, 2] + 3yt[n-1, 2]) * dt / 8 + sqrt(dt^3 / 12) * randn(rng)
xt[n, :] .= exp(integral) .* x0
end
Expand All @@ -59,8 +59,8 @@ end
ntgt = 2^4
ns = 2 .^ (2:3)
m = 100
trajerrors = zeros(last(ns), length(ns))
trajstderrs = zeros(last(ns), length(ns))
trajerrors = zeros(last(ns) + 1, length(ns) + 1)
trajstderrs = zeros(last(ns) + 1, length(ns) + 1)

@testset "Scalar/Scalar Euler" begin

Expand Down
38 changes: 19 additions & 19 deletions test/test_noises.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
m = 2_000
ythf = Vector{Float64}(undef, m)
ytf = Vector{Float64}(undef, m)
yt = Vector{Float64}(undef, n)
yt = Vector{Float64}(undef, n+1)
@testset "Wiener process" begin
rng = Xoshiro(123)
y0 = 0.0
Expand All @@ -18,7 +18,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end
@test mean(ythf) y0 (atol = 0.1)
Expand All @@ -42,7 +42,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end
@test mean(ythf) y0 * exp( -ν * (tf / 2)) (atol = 0.1)
Expand All @@ -65,7 +65,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end
@test mean(ythf) y0 * exp* (tf / 2)) (atol = 0.1)
Expand All @@ -90,7 +90,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end
@test mean(ythf) y0 * exp* (tf / 2)) (atol = 0.1)
Expand All @@ -115,7 +115,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end

Expand All @@ -142,7 +142,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end
@test mean(ythf) μ * λ * tf / 2 (rtol = 0.1)
Expand All @@ -166,7 +166,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end

Expand All @@ -193,7 +193,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end

Expand Down Expand Up @@ -223,7 +223,7 @@

for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end
@test mean(ythf) mean(mean(sin(tf / 2r) for r in rand(rng, ylaw, nr)) for _ in 1:m) (atol = 0.1)
Expand All @@ -232,7 +232,7 @@
@test var(ytf) var(mean(sin(tf / r) for r in rand(rng, ylaw, nr)) for _ in 1:m) (atol = 0.1)
end

@testset "fBm process" begin
#= @testset "fBm process" begin
rng = Xoshiro(123)
y0 = 0.0
H = 0.25
Expand All @@ -245,14 +245,14 @@
for mi in 1:m
rand!(rng, noise, yt)
ythf[mi] = yt[div(n, 2)]
ythf[mi] = yt[div(n, 2) + 1]
ytf[mi] = last(yt)
end
@test mean(ythf) ≈ y0 (atol = 0.1)
@test var(ythf) ≈ (tf/2)^(2H) (atol = 0.1)
@test mean(ytf) ≈ y0 (atol = 0.1)
@test var(ytf) ≈ tf^(2H) (atol = 0.1)
end
end =#

@testset "Product process I" begin
rng = Xoshiro(123)
Expand All @@ -263,7 +263,7 @@

@test eltype(noise) == Float64

ymt = Matrix{Float64}(undef, n, length(noise))
ymt = Matrix{Float64}(undef, n + 1, length(noise))
ymtf = Matrix{Float64}(undef, m, length(noise))

@test_nowarn rand!(rng, noise, ymt)
Expand Down Expand Up @@ -317,7 +317,7 @@

@test eltype(noise) == Float64

ymt = Matrix{Float64}(undef, n, length(noise))
ymt = Matrix{Float64}(undef, n + 1, length(noise))
ymtf = Matrix{Float64}(undef, m, length(noise))

@test_nowarn rand!(rng, noise, ymt)
Expand All @@ -326,7 +326,7 @@

@test_nowarn (@inferred rand!(rng, noise,ymt))

noise = ProductProcess(
#= noise = ProductProcess(
WienerProcess(t0, tf, y0),
OrnsteinUhlenbeckProcess(t0, tf, y0, ν, σ),
GeometricBrownianMotionProcess(t0, tf, y0, μ, σ),
Expand All @@ -339,7 +339,7 @@
@test eltype(noise) == Float64
ymt = Matrix{Float64}(undef, n, length(noise))
ymt = Matrix{Float64}(undef, n + 1, length(noise))
ymtf = Matrix{Float64}(undef, m, length(noise))
@test_nowarn rand!(rng, noise, ymt)
Expand All @@ -349,7 +349,7 @@
# Anyway, I changed it to a generated function with specialized rolled out loop and it is fine, now!
@test (@ballocated rand!($rng, $noise, $ymt)) == 0
@test_nowarn (@inferred rand!(rng, noise,ymt))
@test_nowarn (@inferred rand!(rng, noise, ymt))
for mi in 1:m
rand!(rng, noise, ymt)
Expand Down Expand Up @@ -386,6 +386,6 @@
@test vars[7] ≈ var(mean(sin(tf / r) for r in rand(rng, ylaw, nr)) for _ in 1:m) (rtol = 0.2)
@test means[8] ≈ y0 (rtol = 0.2)
@test vars[8] tf^(2hurst) (rtol = 0.2)
@test vars[8] ≈ tf^(2hurst) (rtol = 0.2) =#
end
end
Loading

0 comments on commit e663cd0

Please sign in to comment.