-
Notifications
You must be signed in to change notification settings - Fork 9
Add duration distribution helpers #150
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b26eb8d
Add duration distributions
rsenne 68e3b6c
Format
rsenne 25b4055
re-add docs
rsenne 525dcd7
Retool PR
rsenne 73d3aaa
Add lightweight helpers
rsenne 972c3e5
Merge remote-tracking branch 'upstream/main' into hsmm-duration-distr…
rsenne f56e0f6
Review
rsenne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| """ | ||
| $(SIGNATURES) | ||
|
|
||
| Log-probability that a state with sojourn-time distribution `dist` is held for exactly `k` | ||
| consecutive timesteps. | ||
|
|
||
| `dist` is supplied by the user with support on `{0, 1, 2, ...}` and is interpreted as the law | ||
| of `(sojourn time - 1)`. The sojourn time itself lives on `{1, 2, 3, ...}`, so this returns | ||
| `logdensityof(dist, k - 1)`. Durations `k < 1` fall outside the user's support and yield | ||
| `-Inf` through `dist` (e.g. a `Distributions.Distribution` returns `-Inf` for a negative | ||
| argument). | ||
|
|
||
| See also [`rand_duration`](@ref). | ||
| """ | ||
| duration_logdensityof(dist, k::Integer) = logdensityof(dist, k - one(k)) | ||
|
|
||
| """ | ||
| $(SIGNATURES) | ||
|
|
||
| Sample a sojourn time on `{1, 2, 3, ...}` from the duration distribution `dist`. | ||
|
|
||
| `dist` is interpreted as the law of `(sojourn time - 1)` (see [`duration_logdensityof`](@ref)), | ||
| so this returns `rand(rng, dist) + 1`. | ||
| """ | ||
| function rand_duration(rng::AbstractRNG, dist) | ||
| t = rand(rng, dist) | ||
| return t + one(t) | ||
| end | ||
|
|
||
| rand_duration(dist) = rand_duration(default_rng(), dist) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using HiddenMarkovModels | ||
| using HiddenMarkovModels: duration_logdensityof, rand_duration | ||
| using DensityInterface: DensityInterface, DensityKind, HasDensity, logdensityof | ||
| using Distributions: Geometric, NegativeBinomial, Poisson | ||
| using Random: AbstractRNG | ||
| using StableRNGs: StableRNG | ||
| using Statistics: mean | ||
| using Test | ||
|
|
||
| # A minimal user-defined duration distribution that only implements the required interface, | ||
| # to confirm we never reach into `Distributions`-specific internals. | ||
| struct ConstDuration{T} end | ||
| DensityInterface.DensityKind(::ConstDuration) = HasDensity() | ||
| DensityInterface.logdensityof(::ConstDuration, x) = iszero(x) ? 0.0 : -Inf | ||
| Base.rand(::AbstractRNG, ::ConstDuration{T}) where {T} = zero(T) | ||
|
|
||
| @testset "Duration convention" begin | ||
| @testset "Shift relationship" begin | ||
| for dist in (Poisson(2.0), Geometric(0.3), NegativeBinomial(4.0, 0.5)) | ||
| for k in -2:20 | ||
| @test duration_logdensityof(dist, k) == logdensityof(dist, k - 1) | ||
| end | ||
| # Support is pushed onto the strictly positive integers. | ||
| @test duration_logdensityof(dist, 0) == -Inf | ||
| @test duration_logdensityof(dist, -1) == -Inf | ||
| @test duration_logdensityof(dist, 1) > -Inf | ||
| end | ||
| end | ||
|
|
||
| @testset "Normalization over sojourn times" begin | ||
| for dist in (Poisson(2.0), Geometric(0.3), NegativeBinomial(4.0, 0.5)) | ||
| pmf_sum = sum(exp(duration_logdensityof(dist, k)) for k in 1:5000) | ||
| @test pmf_sum ≈ 1.0 atol = 1e-6 | ||
| end | ||
| end | ||
|
|
||
| @testset "Sampling is shifted by one" begin | ||
| rng = StableRNG(63) | ||
| # mean(dist) + 1, since the sojourn is (the sampled value) + 1. | ||
| for (dist, expected_mean) in ( | ||
| (Poisson(2.0), 3.0), # mean 2 -> 3 | ||
| (Geometric(0.25), 4.0), # mean 3 -> 4 | ||
| (NegativeBinomial(4.0, 0.5), 5.0), # mean 4 -> 5 | ||
| ) | ||
| samples = [rand_duration(rng, dist) for _ in 1:10_000] | ||
| @test all(s isa Integer for s in samples) | ||
| @test all(s >= 1 for s in samples) | ||
| @test mean(samples) ≈ expected_mean rtol = 0.05 | ||
| end | ||
| end | ||
|
|
||
| @testset "Sample type is preserved" begin | ||
| # The `+1` shift must keep the sampled type. | ||
| for T in (Int, Int32, Float32) | ||
| d = ConstDuration{T}() | ||
| @test rand_duration(StableRNG(1), d) === one(T) | ||
| @test rand_duration(d) === one(T) # default rng overload | ||
| end | ||
| end | ||
|
|
||
| @testset "Custom user distribution (interface only)" begin | ||
| d = ConstDuration{Int}() | ||
| @test duration_logdensityof(d, 1) == 0.0 | ||
| @test duration_logdensityof(d, 2) == -Inf | ||
| @test duration_logdensityof(d, 0) == -Inf | ||
| @test rand_duration(StableRNG(1), d) == 1 | ||
| @test rand_duration(d) == 1 # default rng overload | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.