Skip to content

Commit

Permalink
Add a utility for generating the observation kernel, along with unit …
Browse files Browse the repository at this point in the history
…test and update the `./test` env
  • Loading branch information
SamuelBrand1 committed Feb 12, 2024
1 parent f7bf1c6 commit 776d934
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
1 change: 1 addition & 0 deletions EpiAware/src/EpiAware.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using Distributions,
export scan,
create_discrete_pmf,
growth_rate_to_reproductive_ratio,
generate_observation_kernel,
EpiModel,
log_daily_infections,
random_walk
Expand Down
16 changes: 2 additions & 14 deletions EpiAware/src/epimodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ struct EpiModel{T<:Real} <: AbstractEpiModel
@assert sum(gen_int) 1 "Generation interval must sum to 1"
@assert sum(delay_int) 1 "Delay interval must sum to 1"
#construct observation delay kernel
K = zeros(time_horizon, time_horizon) |> SparseMatrixCSC
for i = 1:time_horizon, j = 1:time_horizon
m = i - j
if m >= 0 && m <= (length(delay_int) - 1)
K[i, j] = delay_int[m+1]
end
end
K = generate_observation_kernel(delay_int, time_horizon)

new{eltype(gen_int)}(
gen_int,
Expand Down Expand Up @@ -70,13 +64,7 @@ struct EpiModel{T<:Real} <: AbstractEpiModel

#construct observation delay kernel
#Recall first element is zero delay
K = zeros(time_horizon, time_horizon) |> SparseMatrixCSC
for i = 1:time_horizon, j = 1:time_horizon
m = i - j
if m >= 0 && m <= (length(delay_int) - 1)
K[i, j] = delay_int[m+1]
end
end
K = generate_observation_kernel(delay_int, time_horizon)

new{eltype(gen_int)}(
gen_int,
Expand Down
24 changes: 24 additions & 0 deletions EpiAware/src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,27 @@ function mean_cc_neg_bin(μ, α)
r = μ^2 / ex_σ²
return NegativeBinomial(r, p)
end


"""
generate_observation_kernel(delay_int, time_horizon)
Generate an observation kernel matrix based on the given delay interval and time horizon.
# Arguments
- `delay_int::Vector{Float64}`: The delay PMF vector.
- `time_horizon::Int`: The number of time steps of the observation period.
# Returns
- `K::SparseMatrixCSC{Float64, Int}`: The observation kernel matrix.
"""
function generate_observation_kernel(delay_int, time_horizon)
K = zeros(eltype(delay_int), time_horizon, time_horizon) |> SparseMatrixCSC
for i = 1:time_horizon, j = 1:time_horizon
m = i - j
if m >= 0 && m <= (length(delay_int) - 1)
K[i, j] = delay_int[m+1]
end
end
return K
end
2 changes: 1 addition & 1 deletion EpiAware/test/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "0b01aa91e53bb772f02a49192dfa1019eaa23f4b"
project_hash = "0dea5a2fa6648a3a05ed8cb24ee73213ffe76d33"

[[deps.ADTypes]]
git-tree-sha1 = "41c37aa88889c171f1300ceac1313c06e891d245"
Expand Down
1 change: 1 addition & 0 deletions EpiAware/test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"
Expand Down
16 changes: 15 additions & 1 deletion EpiAware/test/test_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ end

end

@testitem"Testing growth_rate_to_reproductive_ratio function" begin
@testitem "Testing growth_rate_to_reproductive_ratio function" begin
#Test that zero exp growth rate imples R0 = 1
@testset "Test case 1" begin
r = 0
Expand All @@ -99,3 +99,17 @@ end
end

end

@testitem "Testing generate_observation_kernel function" begin
using SparseArrays
@testset "Test case 1" begin
delay_int = [0.2, 0.5, 0.3]
time_horizon = 5
expected_K = SparseMatrixCSC(
[0.2 0 0 0 0 0.5 0.2 0 0 0 0.3 0.5 0.2 0 0 0 0.3 0.5 0.2 0 0 0 0.3 0.5 0.2],
)
K = generate_observation_kernel(delay_int, time_horizon)
@test K == expected_K
end

end

0 comments on commit 776d934

Please sign in to comment.