Skip to content

Commit 2dec709

Browse files
committed
add doc tests and unit tests + start on helper fn
1 parent c3b9392 commit 2dec709

File tree

7 files changed

+84
-46
lines changed

7 files changed

+84
-46
lines changed

EpiAware/Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1717
LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
1818
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
1919
ManifoldDiff = "af67fdf4-a580-4b9f-bbec-742ef357defd"
20-
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
2120
Pathfinder = "b1d3bc72-d0e7-4279-b92f-7fa5d6d2d454"
2221
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
2322
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
@@ -41,7 +40,6 @@ LinearAlgebra = ">= 1.9"
4140
LogExpFunctions = "0.3"
4241
MCMCChains = "6.0"
4342
ManifoldDiff = "0.3.10"
44-
NNlib = "0.9.22"
4543
Pathfinder = "0.8"
4644
QuadGK = "2.9"
4745
Random = ">= 1.9"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function arma()
2+
AR(; ϵ_t = MA())
3+
end

EpiAware/src/EpiLatentModels/models/AR.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@ The autoregressive (AR) model struct.
1010
1111
# Examples
1212
13-
```julia
14-
using Distributions
15-
using EpiAware
13+
```jldoctest AR
14+
using Distributions, Turing, EpiAware
1615
ar = AR()
17-
ar_model = generate_latent(ar, 10)
18-
rand(ar_model)
16+
ar
17+
# output
18+
19+
```
20+
21+
```jldocttest AR; filter = r\"\b\d+(\.\d+)?\b\" => \"*\"
22+
mdl = generate_latent(ar, 10)
23+
mdl()
24+
# output
25+
```
26+
27+
```jldoctest AR; filter = r\"\b\d+(\.\d+)?\b\" => \"*\"
28+
rand(mdl)
29+
# output
1930
```
2031
"
2132
struct AR{D <: Sampleable, S <: Sampleable, I <: Sampleable,
@@ -83,7 +94,7 @@ Generate a latent AR series.
8394
σ_AR ~ latent_model.std_prior
8495
ar_init ~ latent_model.init_prior
8596
damp_AR ~ latent_model.damp_prior
86-
@submodel ϵ_t = generate_latent(latent_model.ϵ_t, n)
97+
@submodel ϵ_t = generate_latent(latent_model.ϵ_t, n - p)
8798

8899
ar = accumulate_scan(ARStep(damp_AR), ar_init, σ_AR * ϵ_t)
89100

EpiAware/src/EpiLatentModels/models/HierarchicalNormal.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ The `HierarchicalNormal` struct represents a non-centered hierarchical normal di
88
99
## Examples
1010
11-
```julia
12-
using Distributions, EpiAware
13-
hnorm = HierarchicalNormal(0.0, truncated(Normal(0, 1), 0, Inf))
14-
hnorm_model = generate_latent(hnorm, 10)
15-
hnorm_model()
11+
```jldoctest HierarchicalNormal
12+
using Distributions, Turing, EpiAware
13+
hn = HierarchicalNormal()
14+
# output
15+
HierarchicalNormal{Float64, Truncated{Normal{Float64}, Continuous, Float64}}(mean=0.0, std_prior=Truncated{Normal{Float64}, Continuous, Float64}(a=0.0, b=Inf, x=Normal{Float64}(μ=0.0, σ=1.0)))
16+
```
17+
18+
```jldoctest HierarchicalNormal; filter=r\"\b\d+(\.\d+)?\b\" => \"*\"
19+
mdl = generate_latent(hn, 10)
20+
mdl()
21+
# output
22+
```
23+
24+
```jldoctest HierarchicalNormal; filter=r\"\b\d+(\.\d+)?\b\" => \"*\"
25+
rand(mdl)
26+
# output
1627
```
1728
"
1829
@kwdef struct HierarchicalNormal{R <: Real, D <: Sampleable} <: AbstractTuringLatentModel

EpiAware/src/EpiLatentModels/models/Intercept.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ broadcasts a single intercept value to a length `n` latent process.
99
1010
## Examples
1111
12-
```julia
12+
```jldoctest Intercept
1313
using Distributions, Turing, EpiAware
1414
int = Intercept(Normal(0, 1))
15-
int_model = generate_latent(int, 10)
16-
rand(int_model)
17-
int_model()
15+
int
16+
# output
17+
Intercept{Normal{Float64}}(intercept_prior=Normal{Float64}(μ=0.0, σ=1.0))
18+
```
19+
20+
```jldoctest Intercept; filter=r\"\b\d+(\.\d+)?\b\" => \"*\"
21+
mdl = generate_latent(int, 10)
22+
mdl()
23+
# output
24+
```
25+
26+
```jldoctest Intercept; filter=r\"\b\d+(\.\d+)?\b\" => \"*\"
27+
rand(mdl)
28+
# output
1829
```
1930
"
2031
@kwdef struct Intercept{D <: Sampleable} <: AbstractTuringIntercept

EpiAware/src/EpiLatentModels/models/MA.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ The moving average (MA) model struct.
1010
1111
# Examples
1212
13-
```jldoctest; filter = r\"\b\d+(\.\d+)?\b\" => \"*\"
14-
using EpiAware, Distributions
15-
ma = MA(Normal(0.0, 0.05), HalfNormal(0.1); q = 7)
16-
ma_model = generate_latent(ma, 10)
17-
ma_model()
13+
```jldoctest MA
14+
using Distributions, Turing, EpiAware
15+
ma = MA()
16+
ma
17+
# output
18+
19+
```
1820
21+
```jldoctest MA; filter = r\"\b\d+(\.\d+)?\b\" => \"*\"
22+
mdl = generate_latent(ma, 10)
23+
mdl()
1924
# output
25+
```
2026
27+
```jldoctest MA; filter = r\"\b\d+(\.\d+)?\b\" => \"*\"
28+
rand(mdl)
29+
# output
2130
```
2231
"
32+
2333
struct MA{C <: Sampleable, S <: Sampleable, Q <: Int, E <: AbstractTuringLatentModel} <:
2434
AbstractTuringLatentModel
2535
"Prior distribution for the MA coefficients."
@@ -73,7 +83,7 @@ Generate a latent MA series.
7383
q = latent_model.q
7484
@assert n>q "n must be longer than order of the moving average process"
7585

76-
σ_MA ~ latent_model.std_prior
86+
σ ~ latent_model.std_prior
7787
coef_MA ~ latent_model.coef_prior
7888
@submodel ϵ_t = generate_latent(latent_model.ϵ_t, n)
7989
scaled_ϵ_t = σ_MA * ϵ_t

EpiAware/src/EpiLatentModels/models/RandomWalk.jl

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,25 @@ Constructing a random walk requires specifying:
1919
2020
- `RandomWalk(; init_prior, std_prior, ϵ_t)`
2121
22-
## Example usage with `generate_latent`
22+
## Example usage
2323
24-
`generate_latent` can be used to construct a `Turing` model for the random walk ``Z_t``.
25-
26-
First, we construct a `RandomWalk` struct with priors,
27-
28-
```julia
24+
```jldoctest RandomWalk
2925
using Distributions, Turing, EpiAware
30-
31-
# Create a RandomWalk model
32-
rw = RandomWalk(init_prior = Normal(2., 1.),
33-
std_prior = HalfNormal(0.1))
26+
rw = RandomWalk()
27+
rw
28+
# output
29+
RandomWalk{Normal{Float64}, HalfNormal{Float64}, IDD{Normal{Float64}}}(init_prior=Normal{Float64}(μ=0.0, σ=1.0), std_prior=HalfNormal{Float64}(σ=0.25), ϵ_t=IDD{Normal{Float64}}(ϵ_t=Normal{Float64}(μ=0.0, σ=1.0)))
3430
```
3531
36-
Then, we can use `generate_latent` to construct a Turing model for a 10 step random walk.
37-
38-
```julia
39-
# Construct a Turing model
40-
rw_model = generate_latent(rw, 10)
32+
```jldoctest RandomWalk; filter=r\"\b\d+(\.\d+)?\b\" => \"*\"
33+
mdl = generate_latent(rw, 10)
34+
mdl()
35+
# output
4136
```
4237
43-
Now we can use the `Turing` PPL API to sample underlying parameters and generate the
44-
unobserved infections.
45-
46-
```julia
47-
#Sample random parameters from prior
48-
θ = rand(rw_model)
49-
#Get random walk sample path as a generated quantities from the model
50-
Z_t, _ = generated_quantities(rw_model, θ)
38+
```jldoctest RandomWalk; filter=r\"\b\d+(\.\d+)?\b\" => \"*\"
39+
rand(mdl)
40+
# output
5141
```
5242
"
5343
@kwdef struct RandomWalk{
@@ -58,6 +48,10 @@ Z_t, _ = generated_quantities(rw_model, θ)
5848
ϵ_t::E = IDD(Normal())
5949
end
6050

51+
function RandomWalk(init_prior::D, std_prior::S) where {D <: Sampleable, S <: Sampleable}
52+
return RandomWalk(; init_prior = init_prior, std_prior = std_prior)
53+
end
54+
6155
@doc raw"
6256
Implement the `generate_latent` function for the `RandomWalk` model.
6357

0 commit comments

Comments
 (0)