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

Add differenced AR models #134

Merged
merged 58 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
0e32d50
first pass at AR model
seabbs Mar 8, 2024
57d4fcf
move files around and add differencing infrastructure
seabbs Mar 8, 2024
47613aa
remove scaffold comment
seabbs Mar 8, 2024
105d8b4
change arg order
seabbs Mar 8, 2024
34635c7
clean up use of new
seabbs Mar 8, 2024
24b29b8
reorg based on main
seabbs Mar 11, 2024
cb53aea
improve testing
seabbs Mar 11, 2024
b54918e
catching out of date export
seabbs Mar 11, 2024
4cd8307
update to use parametric types and use @kwdef to simplify construction
seabbs Mar 12, 2024
3142f20
update to use parameters.jl and drop p calc in struct as don't work w…
seabbs Mar 12, 2024
668b7ee
work through tests
seabbs Mar 12, 2024
50c5673
check tests and add basic docs
seabbs Mar 12, 2024
030193f
tweak docs
seabbs Mar 12, 2024
e11a2e9
clean up merge issues
seabbs Mar 14, 2024
9a172f9
reimplement AR
seabbs Mar 14, 2024
e21e6fc
reimplement AR
seabbs Mar 14, 2024
e34e952
fix AR
seabbs Mar 15, 2024
8f5d203
fix current AR tests
seabbs Mar 15, 2024
4164ec4
current tests passing
seabbs Mar 15, 2024
54676d5
copy doc strings everywhere
seabbs Mar 15, 2024
83b2022
reorder tests for RandomWalk
seabbs Mar 15, 2024
42364fe
add AR(2)
seabbs Mar 15, 2024
03c7c5b
update ar default
seabbs Mar 15, 2024
455c36b
first pass at AR model
seabbs Mar 8, 2024
b8668f2
move files around and add differencing infrastructure
seabbs Mar 8, 2024
d2e0e72
remove scaffold comment
seabbs Mar 8, 2024
f51c17f
clean up use of new
seabbs Mar 8, 2024
3220e4f
reorg based on main
seabbs Mar 11, 2024
2724d96
remove differenced model for its own PR
seabbs Mar 11, 2024
04e7ceb
update to use parametric types and use @kwdef to simplify construction
seabbs Mar 12, 2024
1d76b45
update to use parameters.jl and drop p calc in struct as don't work w…
seabbs Mar 12, 2024
3d2fec1
work through tests
seabbs Mar 12, 2024
5936ae4
check tests and add basic docs
seabbs Mar 12, 2024
a4d2cc7
tweak docs
seabbs Mar 12, 2024
cebe727
first pass at AR model
seabbs Mar 8, 2024
b9c853c
move files around and add differencing infrastructure
seabbs Mar 8, 2024
49b7875
add differenced latent model
seabbs Mar 11, 2024
486c25c
add default diff latent priors
seabbs Mar 11, 2024
e50db06
clean up comments
seabbs Mar 11, 2024
6d23f52
fix simple example
seabbs Mar 11, 2024
3d389fb
improve draft tests
seabbs Mar 11, 2024
d2a967f
Add normal util
SamuelBrand1 Mar 13, 2024
259909c
Change DiffLatentModel constructor
SamuelBrand1 Mar 13, 2024
3262568
distribution tests for DiffLatentModel on randomwalk
SamuelBrand1 Mar 13, 2024
80405ae
doc strings for DiffLatentModel
SamuelBrand1 Mar 14, 2024
b21b920
remove unnecessary default prior creation
SamuelBrand1 Mar 14, 2024
b7c5aba
style change to use _ for unused loop var
SamuelBrand1 Mar 14, 2024
60a673d
doc string for generated_latent method
SamuelBrand1 Mar 14, 2024
8b1e20a
clean up further merging issues
seabbs Mar 14, 2024
bab886e
clena out utils duplicate
seabbs Mar 14, 2024
a343912
update to new module structure and utils
seabbs Mar 15, 2024
5db0d93
add back in scan.jl
seabbs Mar 15, 2024
cbc892b
clean up more leakage from main/git hygiene
seabbs Mar 15, 2024
9b379ed
add defaults to difflatentmodel
seabbs Mar 15, 2024
4581522
get first difflatent test passing
seabbs Mar 15, 2024
b0a7731
fix final DiffLatent test
seabbs Mar 15, 2024
ef49536
Merge branch 'main' into diff-latent-model
seabbs Mar 15, 2024
2f0efae
Update EpiAware.jl
seabbs Mar 15, 2024
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
2 changes: 1 addition & 1 deletion EpiAware/src/EpiAware.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export spread_draws, scan, create_discrete_pmf
include("EpiLatentModels/EpiLatentModels.jl")
using .EpiLatentModels

export RandomWalk, default_rw_priors
export RandomWalk, AR, DiffLatentModel

include("EpiInfModels/EpiInfModels.jl")
using .EpiInfModels
Expand Down
1 change: 1 addition & 0 deletions EpiAware/src/EpiAwareBase/EpiAwareBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export AbstractModel, AbstractEpiModel, AbstractLatentModel,
AbstractObservationModel, generate_latent,
generate_latent_infs, generate_observations

include("docstrings.jl")
include("types.jl")
include("functions.jl")

Expand Down
24 changes: 24 additions & 0 deletions EpiAware/src/EpiAwareBase/docstrings.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@template (FUNCTIONS, METHODS, MACROS) = """
$(TYPEDSIGNATURES)
$(DOCSTRING)
"""

@template (TYPES) = """
$(TYPEDEF)
$(DOCSTRING)

---
## Fields
$(TYPEDFIELDS)
"""

@template MODULES = """
$(DOCSTRING)

---
## Exports
$(EXPORTS)
---
## Imports
$(IMPORTS)
"""
1 change: 1 addition & 0 deletions EpiAware/src/EpiAwareUtils/EpiAwareUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using DocStringExtensions, QuadGK

export scan, spread_draws, create_discrete_pmf

include("docstrings.jl")
include("prior-tools.jl")
include("distributions.jl")
include("scan.jl")
Expand Down
24 changes: 24 additions & 0 deletions EpiAware/src/EpiAwareUtils/docstrings.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@template (FUNCTIONS, METHODS, MACROS) = """
$(TYPEDSIGNATURES)
$(DOCSTRING)
"""

@template (TYPES) = """
$(TYPEDEF)
$(DOCSTRING)

---
## Fields
$(TYPEDFIELDS)
"""

@template MODULES = """
$(DOCSTRING)

---
## Exports
$(EXPORTS)
---
## Imports
$(IMPORTS)
"""
1 change: 1 addition & 0 deletions EpiAware/src/EpiInfModels/EpiInfModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using Turing, Distributions, DocStringExtensions, LinearAlgebra
export EpiData, DirectInfections, ExpGrowthRate, Renewal,
R_to_r, r_to_R

include("docstrings.jl")
include("epidata.jl")
include("directinfections.jl")
include("expgrowthrate.jl")
Expand Down
24 changes: 24 additions & 0 deletions EpiAware/src/EpiInfModels/docstrings.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@template (FUNCTIONS, METHODS, MACROS) = """
$(TYPEDSIGNATURES)
$(DOCSTRING)
"""

@template (TYPES) = """
$(TYPEDEF)
$(DOCSTRING)

---
## Fields
$(TYPEDFIELDS)
"""

@template MODULES = """
$(DOCSTRING)

---
## Exports
$(EXPORTS)
---
## Imports
$(IMPORTS)
"""
1 change: 1 addition & 0 deletions EpiAware/src/EpiInference/EpiInference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using DynamicPPL, DocStringExtensions

export manypathfinder

include("docstrings.jl")
include("manypathfinder.jl")

end
24 changes: 24 additions & 0 deletions EpiAware/src/EpiInference/docstrings.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@template (FUNCTIONS, METHODS, MACROS) = """
$(TYPEDSIGNATURES)
$(DOCSTRING)
"""

@template (TYPES) = """
$(TYPEDEF)
$(DOCSTRING)

---
## Fields
$(TYPEDFIELDS)
"""

@template MODULES = """
$(DOCSTRING)

---
## Exports
$(EXPORTS)
---
## Imports
$(IMPORTS)
"""
7 changes: 5 additions & 2 deletions EpiAware/src/EpiLatentModels/EpiLatentModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ using ..EpiAwareBase

using Turing, Distributions, DocStringExtensions

export RandomWalk, default_rw_priors
export RandomWalk, AR, DiffLatentModel

include("docstrings.jl")
include("randomwalk.jl")

include("autoregressive.jl")
include("difflatentmodel.jl")
include("utils.jl")
end
95 changes: 95 additions & 0 deletions EpiAware/src/EpiLatentModels/autoregressive.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
@doc raw"
The autoregressive (AR) model struct.

# Constructors
- `AR(damp_prior::Distribution, std_prior::Distribution, init_prior::Distribution; p::Int = 1)`: Constructs an AR model with the specified prior distributions for damping coefficients, standard deviation, and initial conditions. The order of the AR model can also be specified.

- `AR(; damp_priors::Vector{D} = [truncated(Normal(0.0, 0.05))], std_prior::Distribution = truncated(Normal(0.0, 0.05), 0.0, Inf), init_priors::Vector{I} = [Normal()]) where {D <: Distribution, I <: Distribution}`: Constructs an AR model with the specified prior distributions for damping coefficients, standard deviation, and initial conditions. The order of the AR model is determined by the length of the `damp_priors` vector.

- `AR(damp_prior::Distribution, std_prior::Distribution, init_prior::Distribution, p::Int)`: Constructs an AR model with the specified prior distributions for damping coefficients, standard deviation, and initial conditions. The order of the AR model is explicitly specified.

# Examples

```julia
using Distributions
using EpiAware
ar = AR()
ar_model = generate_latent(ar, 10)
rand(ar_model)
```
"
struct AR{D <: Sampleable, S <: Sampleable, I <: Sampleable, P <: Int} <:
AbstractLatentModel
"Prior distribution for the damping coefficients."
damp_prior::D
"Prior distribution for the standard deviation."
std_prior::S
"Prior distribution for the initial conditions"
init_prior::I
"Order of the AR model."
p::P
function AR(damp_prior::Distribution, std_prior::Distribution,
init_prior::Distribution; p::Int = 1)
damp_priors = fill(damp_prior, p)
init_priors = fill(init_prior, p)
return AR(; damp_priors = damp_priors, std_prior = std_prior,
init_priors = init_priors)
end

function AR(; damp_priors::Vector{D} = [truncated(Normal(0.0, 0.05), 0, 1)],
std_prior::Distribution = truncated(Normal(0.0, 0.05), 0.0, Inf),
init_priors::Vector{I} = [Normal()]) where {
D <: Distribution, I <: Distribution}
p = length(damp_priors)
damp_prior = _expand_dist(damp_priors)
init_prior = _expand_dist(init_priors)
return AR(damp_prior, std_prior, init_prior, p)
end

function AR(damp_prior::Distribution, std_prior::Distribution,
init_prior::Distribution, p::Int)
@assert p>0 "p must be greater than 0"
@assert length(damp_prior)==length(init_prior) "damp_prior and init_prior must have the same length"
@assert p==length(damp_prior) "p must be equal to the length of damp_prior"
new{typeof(damp_prior), typeof(std_prior), typeof(init_prior), typeof(p)}(
damp_prior, std_prior, init_prior, p
)
end
end

@doc raw"
Generate a latent AR series.

# Arguments

- `latent_model::AR`: The AR model.
- `n::Int`: The length of the AR series.

# Returns
- `ar::Vector{Float64}`: The generated AR series.
- `params::NamedTuple`: A named tuple containing the generated parameters (`σ_AR`, `ar_init`, `damp_AR`).

# Notes
- The length of `damp_prior` and `init_prior` must be the same.
- `n` must be longer than the order of the autoregressive process.
"
@model function EpiAwareBase.generate_latent(latent_model::AR, n)
p = latent_model.p
ϵ_t ~ MvNormal(ones(n - p))
σ_AR ~ latent_model.std_prior
ar_init ~ latent_model.init_prior
damp_AR ~ latent_model.damp_prior

@assert n>p "n must be longer than order of the autoregressive process"

# Initialize the AR series with the initial values
ar = Vector{Float64}(undef, n)
ar[1:p] = ar_init

# Generate the rest of the AR series
for t in (p + 1):n
ar[t] = damp_AR' * ar[(t - p):(t - 1)] + σ_AR * ϵ_t[t - p]
end

return ar, (; σ_AR, ar_init, damp_AR)
end
Loading