Skip to content

Commit 289d074

Browse files
committed
avoid a deepcopy
1 parent 4570681 commit 289d074

File tree

4 files changed

+35
-40
lines changed

4 files changed

+35
-40
lines changed

src/Models/epidemics/sirs_bp.jl

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ nstates(::SIRSFactor, l::Integer) = l == 0 ? 1 : 2
1717

1818

1919
function mpbp(sirs::SIRS{T,N,F}; kw...) where {T,N,F}
20-
sirs_ = deepcopy(sirs)
21-
g = IndexedBiDiGraph(sirs_.g.A)
22-
w = sirs_factors(sirs_)
23-
return mpbp(g, w, fill(3, nv(g)), T, ϕ=sirs_.ϕ, ψ=sirs_.ψ; kw...)
20+
g = IndexedBiDiGraph(sirs.g.A)
21+
w = sirs_factors(sirs)
22+
return mpbp(g, w, fill(3, nv(g)), T, ϕ=sirs.ϕ, ψ=sirs.ψ; kw...)
2423
end
2524

2625
function prob_y(wᵢ::SIRSFactor, xᵢᵗ⁺¹, xᵢᵗ, yᵗ, d)

src/Models/epidemics/sis_bp.jl

+6-8
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@ function (fᵢ::SISFactor)(xᵢᵗ⁺¹::Integer, xₙᵢᵗ::AbstractVector{<:I
3737
end
3838

3939
function mpbp(sis::SIS{T,N,F}; kw...) where {T,N,F}
40-
sis_ = deepcopy(sis)
41-
g = IndexedBiDiGraph(sis_.g.A)
42-
w = sis_factors(sis_)
43-
return mpbp(g, w, fill(2, nv(g)), T, ϕ=sis_.ϕ, ψ=sis_.ψ; kw...)
40+
g = IndexedBiDiGraph(sis.g.A)
41+
w = sis_factors(sis)
42+
return mpbp(g, w, fill(2, nv(g)), T, ϕ=sis.ϕ, ψ=sis.ψ; kw...)
4443
end
4544

4645
function periodic_mpbp(sis::SIS{T,N,F}; kw...) where {T,N,F}
47-
sis_ = deepcopy(sis)
48-
g = IndexedBiDiGraph(sis_.g.A)
49-
w = sis_factors(sis_)
50-
return periodic_mpbp(g, w, fill(2, nv(g)), T, ϕ=sis_.ϕ, ψ=sis_.ψ; kw...)
46+
g = IndexedBiDiGraph(sis.g.A)
47+
w = sis_factors(sis)
48+
return periodic_mpbp(g, w, fill(2, nv(g)), T, ϕ=sis.ϕ, ψ=sis.ψ; kw...)
5149
end
5250

5351
# neighbor j is susceptible -> does nothing

src/Models/epidemics/sis_heterogeneous_bp.jl

+6-8
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,15 @@ function (fᵢ::SIS_heterogeneousFactor)(xᵢᵗ⁺¹::Integer, xᵗ::AbstractVe
4040
end
4141

4242
function mpbp(sis::SIS_heterogeneous{T,N,F}; kw...) where {T,N,F}
43-
sis_ = deepcopy(sis)
44-
g = IndexedBiDiGraph(sis_.g.A)
45-
w = sis_heterogeneous_factors(sis_)
46-
return mpbp(g, w, fill(2, nv(g)), T, ϕ=sis_.ϕ, ψ=sis_.ψ; kw...)
43+
g = IndexedBiDiGraph(sis.g.A)
44+
w = sis_heterogeneous_factors(sis)
45+
return mpbp(g, w, fill(2, nv(g)), T, ϕ=sis.ϕ, ψ=sis.ψ; kw...)
4746
end
4847

4948
function periodic_mpbp(sis::SIS_heterogeneous{T,N,F}; kw...) where {T,N,F}
50-
sis_ = deepcopy(sis)
51-
g = IndexedBiDiGraph(sis_.g.A)
52-
w = sis_heterogeneous_factors(sis_)
53-
return periodic_mpbp(g, w, fill(2, nv(g)), T, ϕ=sis_.ϕ, ψ=sis_.ψ; kw...)
49+
g = IndexedBiDiGraph(sis.g.A)
50+
w = sis_heterogeneous_factors(sis)
51+
return periodic_mpbp(g, w, fill(2, nv(g)), T, ϕ=sis.ϕ, ψ=sis.ψ; kw...)
5452
end
5553

5654
# neighbor j is susceptible -> does nothing

test/sis_small_tree.jl

+20-20
Original file line numberDiff line numberDiff line change
@@ -110,35 +110,35 @@
110110
@test logl_bp logp
111111
end
112112

113-
### Periodic version
114-
bp = periodic_mpbp(sis)
115-
rng = MersenneTwister(111)
116-
X, _ = onesample(bp; rng)
113+
@testset "SIS small tree - periodic" begin
114+
bp = periodic_mpbp(sis)
115+
rng = MersenneTwister(111)
116+
X, _ = onesample(bp; rng)
117117

118-
draw_node_observations!(bp.ϕ, X, N, last_time=true; rng)
118+
reset!(bp; observations=true)
119+
draw_node_observations!(bp.ϕ, X, N, last_time=true; rng)
119120

120-
svd_trunc = TruncBondMax(10)
121-
iterate!(bp, maxiter=10; svd_trunc, showprogress=false)
121+
svd_trunc = TruncBondMax(10)
122+
iterate!(bp, maxiter=10; svd_trunc, showprogress=false)
122123

123-
b_bp = beliefs(bp)
124-
p_bp = [[bbb[2] for bbb in bb] for bb in b_bp]
124+
b_bp = beliefs(bp)
125+
p_bp = [[bbb[2] for bbb in bb] for bb in b_bp]
125126

126-
p_exact, Z_exact = exact_prob(bp)
127-
b_exact = exact_marginals(bp; p_exact)
128-
p_ex = [[bbb[2] for bbb in bb] for bb in b_exact]
127+
p_exact, Z_exact = exact_prob(bp)
128+
b_exact = exact_marginals(bp; p_exact)
129+
p_ex = [[bbb[2] for bbb in bb] for bb in b_exact]
129130

130-
f_bethe = bethe_free_energy(bp)
131-
Z_bp = exp(-f_bethe)
131+
f_bethe = bethe_free_energy(bp)
132+
Z_bp = exp(-f_bethe)
132133

133-
local f(x,i) = x-1
134+
local f(x,i) = x-1
134135

135-
r_bp = autocorrelations(f, bp)
136-
r_exact = exact_autocorrelations(f, bp; p_exact)
136+
r_bp = autocorrelations(f, bp)
137+
r_exact = exact_autocorrelations(f, bp; p_exact)
137138

138-
c_bp = autocovariances(f, bp)
139-
c_exact = exact_autocovariances(f, bp; r = r_exact)
139+
c_bp = autocovariances(f, bp)
140+
c_exact = exact_autocovariances(f, bp; r = r_exact)
140141

141-
@testset "SIS small tree - periodic" begin
142142
@test Z_exact Z_bp
143143
@test p_ex p_bp
144144
@test r_bp r_exact

0 commit comments

Comments
 (0)