Skip to content

Commit f2c8996

Browse files
FedericoFloriostecrotti
authored andcommitted
Enhancement with sparse matrices
1 parent 061ae16 commit f2c8996

File tree

6 files changed

+12589
-12487
lines changed

6 files changed

+12589
-12487
lines changed

notebooks/sis_heterogeneous_compare_homogeneous.ipynb

Lines changed: 12470 additions & 12471 deletions
Large diffs are not rendered by default.

notebooks/test.jl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using SparseArrays
2+
using MatrixProductBP, MatrixProductBP.Models
3+
using UnPack, Graphs, IndexedGraphs, Plots, Statistics, ProgressMeter, Random
4+
ProgressMeter.ijulia_behavior(:clear)
5+
using JLD2
6+
import Measurements: value
7+
8+
9+
T = 3
10+
N = 5
11+
12+
A = [0 1 1 0 0;
13+
1 0 1 0 0;
14+
1 1 0 1 0;
15+
0 0 1 0 1;
16+
0 0 0 1 0]
17+
g = IndexedGraph(A)
18+
19+
λ_unif = 0.15
20+
ρ_unif = 0.12
21+
λ = λ_unif .* A
22+
λ = sparse(λ)
23+
ρ = fill(ρ_unif,N)
24+
γ = 0.13
25+
26+
sis_u = SIS(g, λ_unif, ρ_unif, T; γ)
27+
sis_h = SIS_heterogeneous(λ, ρ, T; γ)
28+
29+
30+
svd_trunc = TruncBond(3)
31+
32+
bp_u = mpbp(sis_u)
33+
iters_u, cb_u = iterate!(bp_u, maxiter=200; svd_trunc, tol=1e-12)
34+
b_bp_u = beliefs(bp_u)
35+
p_bp_u = [[bᵗ[INFECTIOUS] for bᵗ in bb] for bb in b_bp_u]
36+
37+
bp_h = mpbp(sis_h)
38+
iters_h, cb_h = iterate!(bp_h, maxiter=200; svd_trunc, tol=1e-12)
39+
b_bp_h = beliefs(bp_h)
40+
p_bp_h = [[bᵗ[INFECTIOUS] for bᵗ in bb] for bb in b_bp_h]
41+
42+
err = maximum([(p_bp_h[i][j]-p_bp_u[i][j])/p_bp_u[i][j] for i in eachindex(p_bp_u) for j in eachindex(p_bp_u[i])])
43+
44+
println(err 0)
45+
46+
47+
A_ = copy(A)
48+
A_[3,2] = 0
49+
A_[4,3] = 0
50+
λ_ = copy(λ)
51+
λ_[3,2] = 0.0
52+
λ_[4,3] = 0.0
53+
54+
sis_u_ = SIS(g, λ_unif, ρ_unif, T; γ)
55+
sis_h_ = SIS_heterogeneous(λ, ρ, T; γ)
56+
57+
58+
bp_u_ = mpbp(sis_u_)
59+
iters_u_, cb_u_ = iterate!(bp_u_, maxiter=200; svd_trunc, tol=1e-12)
60+
b_bp_u_ = beliefs(bp_u_)
61+
p_bp_u_ = [[bᵗ[INFECTIOUS] for bᵗ in bb] for bb in b_bp_u_]
62+
63+
bp_h_ = mpbp(sis_h_)
64+
iters_h_, cb_h_ = iterate!(bp_h_, maxiter=200; svd_trunc, tol=1e-12)
65+
b_bp_h_ = beliefs(bp_h_)
66+
p_bp_h_ = [[bᵗ[INFECTIOUS] for bᵗ in bb] for bb in b_bp_h_]
67+
68+
err_ = maximum([(p_bp_h_[i][j]-p_bp_u_[i][j])/p_bp_u_[i][j] for i in eachindex(p_bp_u_) for j in eachindex(p_bp_u_[i])])
69+
70+
println(err_ 0)

src/Models/Models.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using MatrixProductBP
1111
import IndexedGraphs: IndexedGraph, IndexedBiDiGraph, AbstractIndexedDiGraph, ne, nv,
1212
outedges, idx, src, dst, inedges, neighbors, edges, vertices, IndexedEdge
1313
import UnPack: @unpack
14-
import SparseArrays: nonzeros, nzrange, rowvals, Symmetric
14+
import SparseArrays: nonzeros, nzrange, rowvals, Symmetric, SparseMatrixCSC
1515
import TensorCast: @reduce, @cast, TensorCast
1616
import ProgressMeter: Progress, next!, ProgressUnknown
1717
import LogExpFunctions: xlogx, xlogy
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
struct SIS_heterogeneous{T, N, F<:Real}
22
g :: IndexedGraph
3-
λ :: Matrix{F}
3+
λ :: SparseMatrixCSC{F,Int64}
44
ρ :: Vector{F}
55
ϕ :: Vector{Vector{Vector{F}}} # site observations
66
ψ :: Vector{Vector{Matrix{F}}} # edge observations
7-
function SIS_heterogeneous(g::IndexedGraph, λ::Matrix{F}, ρ::Vector{F},
7+
function SIS_heterogeneous(g::IndexedGraph, λ::SparseMatrixCSC{F,Int64}, ρ::Vector{F},
88
ϕ::Vector{Vector{Vector{F}}},
99
ψ::Vector{Vector{Matrix{F}}}) where {F<:Real}
1010
@assert size(λ)[1] == size(λ)[2] == nv(g)
1111
@assert length(ρ) == nv(g)
12-
@assert all(0 λᵢⱼ 1 for λᵢⱼ in λ)
12+
@assert all(0 λᵢⱼ 1 for λᵢⱼ in !iszero(λ))
1313
@assert all(0 ρᵢ 1 for ρᵢ in ρ)
1414
N = nv(g)
1515
@assert length(ϕ) == N
@@ -21,13 +21,21 @@ struct SIS_heterogeneous{T, N, F<:Real}
2121
end
2222
end
2323

24-
function SIS_heterogeneous(g::IndexedGraph{Int}, λ::Matrix{F}, ρ::Vector{F}, T::Int;
24+
function SIS_heterogeneous(g::IndexedGraph{Int}, λ::SparseMatrixCSC{F,Int64}, ρ::Vector{F}, T::Int;
2525
ψ = [[ones(2,2) for t in 0:T] for _ in 1:2*ne(g)],
2626
γ = 0.5,
2727
ϕ = [[t == 0 ? (length(γ)==1 ? [1-γ, γ] : [1-γ[i],γ[i]]) : ones(2) for t in 0:T] for i in vertices(g)]) where {F<:Real}
28+
2829
return SIS_heterogeneous(g, λ, ρ, ϕ, ψ)
2930
end
3031

32+
function SIS_heterogeneous::SparseMatrixCSC{F,Int64}, ρ::Vector{F}, T::Int; γ=0.5) where {F<:Real}
33+
A = ones(Int,size(λ)[1],size(λ)[2]) - iszero.(λ)
34+
g = IndexedGraph(A)
35+
36+
return SIS_heterogeneous(g, λ, ρ, T; γ=γ)
37+
end
38+
3139
function sis_heterogeneous_factors(sis::SIS_heterogeneous{T,N,F}) where {T,N,F}
32-
[fill(SIS_heterogeneousFactor([sis.λ[x,i] for x in eachindex(sis.λ[:,i]) if x!=i], sis.ρ[i]), T + 1) for i in vertices(sis.g)]
40+
[fill(SIS_heterogeneousFactor([sis.λ[j,i] for j in sis.λ.rowval[nzrange(sis.λ,i)]], sis.ρ[i]), T + 1) for i in vertices(sis.g)]
3341
end

src/Models/epidemics/sis_heterogeneous_bp.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# import MatrixProductBP: mpbp, nstates, prob_y, prob_xy, prob_yy
2-
3-
41
const SUSCEPTIBLE = 1
52
const INFECTIOUS = 2
63

test/sis_heterogeneous_compare_homogeneous.jl

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
@testset "SIS heterogeneous compare homogeneous" begin
22
T = 3
33
N = 5
4-
4+
55
A = [0 1 1 0 0;
6-
1 0 1 0 0;
7-
1 1 0 1 0;
8-
0 0 1 0 1;
9-
0 0 0 1 0]
6+
1 0 1 0 0;
7+
1 1 0 1 0;
8+
0 0 1 0 1;
9+
0 0 0 1 0]
1010
g = IndexedGraph(A)
1111

1212
λ_unif = 0.15
1313
ρ_unif = 0.12
1414
λ = λ_unif .* A
15+
λ = sparse(λ)
1516
ρ = fill(ρ_unif,N)
1617
γ = 0.13
1718

1819
sis_u = SIS(g, λ_unif, ρ_unif, T; γ)
19-
sis_h = SIS_heterogeneous(g, λ, ρ, T; γ)
20+
sis_h = SIS_heterogeneous(λ, ρ, T; γ)
21+
2022

21-
2223
svd_trunc = TruncBond(3)
2324

2425
bp_u = mpbp(sis_u)
@@ -34,4 +35,31 @@
3435
err = maximum([(p_bp_h[i][j]-p_bp_u[i][j])/p_bp_u[i][j] for i in eachindex(p_bp_u) for j in eachindex(p_bp_u[i])])
3536

3637
@test err 0
38+
39+
40+
A_ = copy(A)
41+
A_[3,2] = 0
42+
A_[4,3] = 0
43+
λ_ = copy(λ)
44+
λ_[3,2] = 0.0
45+
λ_[4,3] = 0.0
46+
47+
sis_u_ = SIS(g, λ_unif, ρ_unif, T; γ)
48+
sis_h_ = SIS_heterogeneous(λ, ρ, T; γ)
49+
50+
51+
bp_u_ = mpbp(sis_u_)
52+
iters_u_, cb_u_ = iterate!(bp_u_, maxiter=200; svd_trunc, tol=1e-12)
53+
b_bp_u_ = beliefs(bp_u_)
54+
p_bp_u_ = [[bᵗ[INFECTIOUS] for bᵗ in bb] for bb in b_bp_u_]
55+
56+
bp_h_ = mpbp(sis_h_)
57+
iters_h_, cb_h_ = iterate!(bp_h_, maxiter=200; svd_trunc, tol=1e-12)
58+
b_bp_h_ = beliefs(bp_h_)
59+
p_bp_h_ = [[bᵗ[INFECTIOUS] for bᵗ in bb] for bb in b_bp_h_]
60+
61+
err_ = maximum([(p_bp_h_[i][j]-p_bp_u_[i][j])/p_bp_u_[i][j] for i in eachindex(p_bp_u_) for j in eachindex(p_bp_u_[i])])
62+
63+
@test err_ 0
64+
3765
end

0 commit comments

Comments
 (0)