Skip to content

Stabilize priors #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/priors.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# This file is a part of LegendSpecFits.jl, licensed under the MIT License (MIT).

"""
LegendSpecFits.stabilize_dist(d::Distribution)

Return a numerically stable version of the distribution `d`.

Tries to ensure that the (log-)PDF of the result is finite over it's whole
support.
"""
stabilize_dist(d::Distribution) = d

Check warning on line 11 in src/priors.jl

View check run for this annotation

Codecov / codecov/patch

src/priors.jl#L11

Added line #L11 was not covered by tests

function stabilize_dist(d::Weibull)
α = d.α
ϵ = oftype(α, max(eps(typeof(α)), eps(Float32)))
lthresh = isinf(logpdf(d, 0)) ? ϵ : zero(α)
return truncated(d, lthresh, Inf)
end


"""
weibull_from_mx(m::Real, x::Real, p_x::Real = 0.6827)::Weibull
Expand All @@ -12,6 +29,6 @@
function weibull_from_mx(m::Real, x::Real, p_x::Real = 0.6827)
α = log(-log(1-p_x) / log(2)) / log(x/m)
θ = m / log(2)^(1/α)
Weibull(α, θ)
return stabilize_dist(Weibull(α, θ))
end
export weibull_from_mx
4 changes: 2 additions & 2 deletions src/pseudo_prior.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ function get_standard_pseudo_prior(h::Histogram, ps::NamedTuple{(:peak_pos, :pea
μ = ifelse(fixed_position, ConstValueDist(ps.peak_pos), Normal(ps.peak_pos, 0.2*ps.peak_sigma)),
σ = weibull_from_mx(ps.peak_sigma, 1.5*ps.peak_sigma),
n = weibull_from_mx(ps.peak_counts, 1.5*ps.peak_counts),
skew_fraction = ifelse(low_e_tail, truncated(weibull_from_mx(0.002, 0.008), 0.0, 0.5), ConstValueDist(0.0)),
skew_fraction = ifelse(low_e_tail, truncated(weibull_from_mx(0.002, 0.008), 1e-7, 0.5), ConstValueDist(0.0)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you quickly explain why 1e-7?

skew_width = ifelse(low_e_tail, weibull_from_mx(ps.peak_sigma/ps.peak_pos, 1.2*ps.peak_sigma/ps.peak_pos), ConstValueDist(1.0)),
background = weibull_from_mx(ps.mean_background, ps.mean_background + 5*ps.mean_background_std),
step_amplitude = weibull_from_mx(ps.mean_background_step, ps.mean_background_step + 5*ps.mean_background_std),
skew_fraction_highE = ifelse(low_e_tail, truncated(weibull_from_mx(0.002, 0.008), 0.0, 0.1), ConstValueDist(0.0)),
skew_fraction_highE = ifelse(low_e_tail, truncated(weibull_from_mx(0.002, 0.008), 1e-7, 0.1), ConstValueDist(0.0)),
skew_width_highE = ifelse(low_e_tail, weibull_from_mx(ps.peak_sigma/ps.peak_pos, 1.2*ps.peak_sigma/ps.peak_pos), ConstValueDist(1.0)),
background_slope = ifelse(ps.mean_background < 5, ConstValueDist(0), truncated(Normal(0, 0.1*ps.mean_background_std / (window_left + window_right)), - ps.mean_background / window_right, 0)),
background_exp = weibull_from_mx(3e-2, 5e-2)
Expand Down
Loading