TaylorTest/test/gauss.jl
2025-02-10 12:08:26 +01:00

50 lines
1.4 KiB
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function J_gauss(p::Vector{Float64}; x::Float64=0.0)
λ, μ, σ = p
c = [1 / λ (2 * (x - μ) / σ^2) (-1 + 2 * ((x - μ) / σ)^2) / σ]
return c .* gauss(p; x=x)
end
function H_gauss(p::Vector{Float64}; x::Float64=0.0)
λ, μ, σ = p
c = zeros(3, 3)
# c[1,1] = 0
c[1, 2] = c[2, 1] = (2x) / (λ * σ^2) - (2 * μ) / (λ * σ^2)
c[1, 3] = c[3, 1] = (2 * (x - μ)^2 - σ^2) / (λ * σ^3)
c[2, 2] = (4 * (x - μ)^2 - 2 * σ^2) / σ^4
c[2, 3] = c[3, 2] = (2λ * (x - μ) * (2μ^2 - 3σ^2 + 2x^2 - 4μ * x)) / (λ * σ^5)
c[3, 3] = (2 * (σ^4 - 5σ^2 * (x - μ)^2 + 2 * (x - μ)^4)) / σ^6
return c .* gauss(p; x=x)
end
@testset "Gauss function" begin
λ = 10 * (rand() - 0.5)
μ = 10 * (rand() - 0.5)
σ = 10rand()
x = σ * (2 * rand() - 0.5) + μ
p = [λ, μ, σ]
@test TaylorTest.check(gauss, J_gauss, p; x=x)
@test TaylorTest.check(J_gauss, H_gauss, p; x=x)
end
@testset "Composed Gauss function" begin
p = 1 .+ 5 * rand(6)
x = p[3] + p[4] + 2p[5] + sqrt(p[6]) * (2rand() - 1)
p_aux = _p -> [_p[1]/_p[2], _p[3] + _p[4] + 2_p[5], _p[6]]
J_p_aux = _p -> [1/_p[2] -_p[1]/_p[2]^2 0 0 0 0;
0 0 1 1 2 0;
0 0 0 0 0 1]
g = (_p; x=x) -> gauss(p_aux(_p); x=x)
Jg = (_p; x=x) -> J_gauss(p_aux(_p); x=x) * J_p_aux(p)
Hg = (_p; x=x) -> J_gauss(p_aux(_p); x=x) * J_p_aux(p)
@test TaylorTest.check(g, Jg, p; x=x)
end