-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix #53 * Test deploy * Missing base package * Develop local * Formatx * Typos * Print * Update src/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Color * Seed * Particle Gibbs Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
cc2b444
commit 6b8bc0c
Showing
6 changed files
with
199 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[deps] | ||
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" | ||
AdvancedPS = "576499cb-2369-40b2-a588-c64705576edc" | ||
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
Libtask = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f" | ||
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" | ||
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
Random123 = "74087812-796a-5b5d-8853-05524746bad3" | ||
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# # Particle Gibbs with Ancestor Sampling | ||
using AdvancedPS | ||
using Random | ||
using Distributions | ||
using Plots | ||
using AbstractMCMC | ||
using Random123 | ||
using Libtask: TArray | ||
using Libtask | ||
|
||
Parameters = @NamedTuple begin | ||
a::Float64 | ||
q::Float64 | ||
r::Float64 | ||
T::Int | ||
end | ||
|
||
mutable struct NonLinearTimeSeries <: AbstractMCMC.AbstractModel | ||
X::TArray | ||
θ::Parameters | ||
NonLinearTimeSeries(θ::Parameters) = new(TArray(Float64, θ.T), θ) | ||
end | ||
|
||
f(model::NonLinearTimeSeries, state, t) = Normal(model.θ.a * state, model.θ.q) # Transition density | ||
g(model::NonLinearTimeSeries, state, t) = Normal(state, model.θ.r) # Observation density | ||
f₀(model::NonLinearTimeSeries) = Normal(0, model.θ.q) # Initial state density | ||
|
||
# Everything is now ready to simulate some data. | ||
|
||
a = 0.9 # Scale | ||
q = 0.32 # State variance | ||
r = 1 # Observation variance | ||
Tₘ = 300 # Number of observation | ||
Nₚ = 20 # Number of particles | ||
Nₛ = 500 # Number of samples | ||
seed = 9 # Reproduce everything | ||
|
||
θ₀ = Parameters((a, q, r, Tₘ)) | ||
|
||
rng = Random.MersenneTwister(seed) | ||
|
||
x = zeros(Tₘ) | ||
y = zeros(Tₘ) | ||
|
||
reference = NonLinearTimeSeries(θ₀) | ||
x[1] = 0 | ||
for t in 1:Tₘ | ||
if t < Tₘ | ||
x[t + 1] = rand(rng, f(reference, x[t], t)) | ||
end | ||
y[t] = rand(rng, g(reference, x[t], t)) | ||
end | ||
|
||
function (model::NonLinearTimeSeries)(rng::Random.AbstractRNG) | ||
x₀ = rand(rng, f₀(model)) | ||
model.X[1] = x₀ | ||
score = logpdf(g(model, x₀, 1), y[1]) | ||
Libtask.produce(score) | ||
|
||
for t in 2:(model.θ.T) | ||
state = rand(rng, f(model, model.X[t - 1], t - 1)) | ||
model.X[t] = state | ||
score = logpdf(g(model, state, t), y[t]) | ||
Libtask.produce(score) | ||
end | ||
end | ||
|
||
Libtask.tape_copy(model::NonLinearTimeSeries) = deepcopy(model) | ||
|
||
Random.seed!(rng, seed) | ||
model = NonLinearTimeSeries(θ₀) | ||
pgas = AdvancedPS.PG(Nₚ) | ||
chains = sample(rng, model, pgas, Nₛ; progress=false) | ||
|
||
# Utility to replay a particle trajectory | ||
function replay(particle::AdvancedPS.Particle) | ||
trng = deepcopy(particle.rng) | ||
Random123.set_counter!(trng.rng, 0) | ||
trng.count = 1 | ||
model = NonLinearTimeSeries(θ₀) | ||
trace = AdvancedPS.Trace(AdvancedPS.GenericModel(model, trng), trng) | ||
score = AdvancedPS.advance!(trace, true) | ||
while !isnothing(score) | ||
score = AdvancedPS.advance!(trace, true) | ||
end | ||
return trace | ||
end | ||
|
||
trajectories = map(chains) do sample | ||
replay(sample.trajectory) | ||
end | ||
|
||
particles = hcat([trajectory.model.f.X for trajectory in trajectories]...) # Concat all sampled states | ||
mean_trajectory = mean(particles; dims=2) | ||
|
||
scatter(particles; label=false, opacity=0.01, color=:black) | ||
plot!(x; color=:red, label="Original Trajectory") | ||
plot!(mean_trajectory; color=:orange, label="Mean trajectory", opacity=0.9) | ||
xlabel!("t") | ||
ylabel!("State") | ||
|
||
update_rate = sum(abs.(diff(particles; dims=2)) .> 0; dims=2) / Nₛ | ||
plot(update_rate; label=false, ylim=[0, 1], legend=:bottomleft) | ||
hline!([1 - 1 / Nₚ]; label="N: $(Nₚ)") | ||
xlabel!("Iteration") | ||
ylabel!("Update rate") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters