Skip to content
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

Make the 99th perc default constructor a censored_pmf feature #278

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions EpiAware/src/EpiAwareUtils/censored_pmf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,29 @@ for double censored delays is then found using simple differencing on the CDF.
Arguments:
- `dist`: The distribution from which to create the PMF.
- `Δd`: The step size for discretizing the domain. Default is 1.0.
- `D`: The upper bound of the domain. Must be greater than `Δd`.
- `D`: The upper bound of the domain. Must be greater than `Δd`. Default `D = nothing`
indicates that the distribution should be truncated at its 99th percentile rounded
to nearest multiple of `Δd`.

Returns:
- A vector representing the PMF.

Raises:
- `AssertionError` if the minimum value of `dist` is negative.
- `AssertionError` if `Δd` is not positive.
- `AssertionError` if `D` is not greater than `Δd`.
- `AssertionError` if `D` is shorter than `Δd`.
- `AssertionError` if `D` is not a multiple of `Δd`.
"""
function censored_pmf(dist::Distribution; Δd = 1.0, D)
function censored_pmf(dist::Distribution; Δd = 1.0, D = nothing)
@assert minimum(dist)>=0.0 "Distribution must be non-negative."
@assert Δd>0.0 "Δd must be positive."
@assert D>Δd "D must be greater than Δd."

if isnothing(D)
x_99 = invlogcdf(dist, log(0.99))
D = round(Int64, x_99 / Δd) * Δd
end

@assert D>=Δd "D can't be shorter than Δd."

ts = 0.0:Δd:D |> collect

Expand Down
7 changes: 2 additions & 5 deletions EpiAware/src/EpiObsModels/LatentDelay.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ observed data.
where {M <: AbstractTuringObservationModel, C <: ContinuousDistribution}`: Constructs
a `LatentDelay` object with the given underlying observation model and continuous
distribution. The `D` parameter specifies the right truncation of the distribution,
with default `D = nothing` indicating that the distribution should be truncated at its
99th percentile rounded to nearest integer. The `Δd` parameter specifies the
with default `D = nothing` indicates that the distribution should be truncated
at its 99th percentile rounded to nearest multiple of `Δd`. The `Δd` parameter specifies the
width of each delay interval.

- `LatentDelay(model::M, pmf::T) where {M <: AbstractTuringObservationModel, T <: AbstractVector{<:Real}}`: Constructs a `LatentDelay` object with the given underlying observation model and delay PMF.
Expand All @@ -35,9 +35,6 @@ struct LatentDelay{M <: AbstractTuringObservationModel, T <: AbstractVector{<:Re
function LatentDelay(model::M, distribution::C; D = nothing,
Δd = 1.0) where {
M <: AbstractTuringObservationModel, C <: ContinuousDistribution}
if isnothing(D)
D = invlogcdf(distribution, log(0.99)) |> round
end
pmf = censored_pmf(distribution; Δd = Δd, D = D)
return LatentDelay(model, pmf)
end
Expand Down
10 changes: 10 additions & 0 deletions EpiAware/test/EpiAwareUtils/censored_pmf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@
dist = Exponential(1.0)
@test_throws AssertionError censored_pmf(dist, Δd = 1.0, D = 3.5)
end

@testset "Test case 7: testing default choice of D" begin
dist = Exponential(1.0)
pmf = censored_pmf(dist, Δd = 1.0)
#Check the normalisation constant is > 0.99 for analytical solution
expected_pmf_uncond = [exp(-1)
[(1 - exp(-1)) * (exp(1) - 1) * exp(-s)
for s in 1:length(pmf)]]
@test sum(expected_pmf_uncond) > 0.99
end
end
Loading