Skip to content

Commit

Permalink
expo test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofia Calgaro committed Dec 31, 2024
1 parent c762bcc commit daf90a3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/fitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ function norm_uniform(x::Real,p::NamedTuple,b_name::Symbol,fit_range::Union{Vect

norm =sum(range_h .- range_l)
return 1/norm


end


Expand Down Expand Up @@ -93,15 +91,15 @@ function norm_exponential(x::Float64,p::NamedTuple,b_name::Symbol,fit_range::Uni
range_l, range_h = get_range(fit_range)
center = range_l[1]

centers=[center,center,center]
# could be made faster?
centers=fill(center, length(range_l))
R = p[Symbol(string(b_name)*"_slope")]
delta = range_h[end] - range_l[1]
Rt=R/delta

if (abs(Rt)>1E-6)
norm = (-sum(exp_stable.(-Rt*(centers-range_l)))+sum(exp_stable.(-Rt*(centers-range_h))))/Rt
norm = (-sum(exp_stable.((range_l-centers)*Rt))+sum(exp_stable.((range_h-centers)*Rt)))/Rt
else
norm =sum(range_h .- range_l)
norm = sum(range_h .- range_l)
end

return exp_stable((x-center)*Rt)/norm
Expand Down
1 change: 1 addition & 0 deletions test/statistics/test_all.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Test.@testset "statistics" begin
include("test_get_bkg_info.jl")
include("test_norm_linear.jl")
include("test_exp_stable.jl")
include("test_norm_exponential.jl")
end
60 changes: 60 additions & 0 deletions test/statistics/test_norm_exponential.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Pkg
Pkg.activate(".")
Pkg.instantiate()
using Random
include("../../src/ZeroNuFit.jl")
using .ZeroNuFit
include("../../main.jl")
include("../../src/utils.jl")
include("../../src/constants.jl")

@testset "test_norm_exponential" begin

@info "Testing normalised exponential function (function 'norm_exponential' in src/fitting.jl)"

# high Rt value
x = 1965.0
p = (S = 100, αe_all = 0.1, ω = [1.1], 𝛥 = [0.1], B_l200a_all = 2E-4, B_l200a_all_slope = 10)
b_name = :B_l200a_all
fit_range = [[1920.0, 1930.0], [1960.0, 1970.0]]
expo_func = nothing
try
expo_func = ZeroNuFit.norm_exponential(x, p, b_name, fit_range)
catch e
@error "Error in 'norm_exponential' evaluation: $e"
throw(e)
end

@testset "Check expo_func is valid (high Rt value)" begin
@test !isnothing(expo_func)
end

expected_value = 0.08506327727340002
tolerance = 1e-3
@testset "Check expo_func accuracy (high Rt value)" begin
diff = abs(expo_func - expected_value)
@test diff <= tolerance
end

# low Rt value
p = (S = 100, αe_all = 0.1, ω = [1.1], 𝛥 = [0.1], B_l200a_all = 2E-4, B_l200a_all_slope = 0.00001)
expo_func = nothing
try
expo_func = ZeroNuFit.norm_exponential(x, p, b_name, fit_range)
catch e
@error "Error in 'norm_exponential' evaluation: $e"
throw(e)
end

@testset "Check expo_func is valid (low Rt value)" begin
@test !isnothing(expo_func)
end

expected_value = 0.05000045000202501
tolerance = 1e-3
@testset "Check expo_func accuracy (low Rt value)" begin
diff = abs(expo_func - expected_value)
@test diff <= tolerance
end

end

0 comments on commit daf90a3

Please sign in to comment.