Skip to content

Commit

Permalink
Fix graph generation
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericWantiez committed Jun 26, 2022
1 parent 0bb8fde commit 768b42c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/literate.jl
Expand Up @@ -15,4 +15,4 @@ using Literate: Literate

# Convert to markdown and notebook
const SCRIPTJL = joinpath(EXAMPLEPATH, "script.jl")
Literate.markdown(SCRIPTJL, OUTDIR; name=EXAMPLE, documenter=false, execute=true)
Literate.markdown(SCRIPTJL, OUTDIR; name=EXAMPLE, execute=true)
2 changes: 1 addition & 1 deletion docs/make.jl
Expand Up @@ -61,7 +61,7 @@ makedocs(;
)...,
],
],
strict=true,
#strict=true,
checkdocs=:exports,
doctestfilters=[
# Older versions will show "0 element Array" instead of "Type[]".
Expand Down
16 changes: 5 additions & 11 deletions examples/gaussian-ssm/script.jl
Expand Up @@ -83,12 +83,10 @@ for t in 1:Tₘ
end

# Here are the latent and obseravation timeseries
plot(x; label="x")
xlabel!("t")
plot(x; label="x", xlabel="t")

#
plot(y; label="y")
xlabel!("x")
#
plot(y; label="y", xlabel="t")

# `AdvancedPS` subscribes to the `AbstractMCMC` API. To sample we just need to define a Particle Gibbs kernel
# and a model interface.
Expand All @@ -103,11 +101,9 @@ mean_trajectory = mean(particles; dims=2);
#md nothing #hide

# This toy model is small enough to inspect all the generated traces:
scatter(particles; label=false, opacity=0.01, color=:black)
scatter(particles; label=false, opacity=0.01, color=:black, xlabel="t", ylabel="state")
plot!(x; color=:darkorange, label="Original Trajectory")
plot!(mean_trajectory; color=:dodgerblue, label="Mean trajectory", opacity=0.9)
xlabel!("t")
ylabel!("State")

# We used a particle gibbs kernel with the ancestor updating step which should help with the particle
# degeneracy problem and improve the mixing.
Expand All @@ -116,7 +112,5 @@ update_rate = sum(abs.(diff(particles; dims=2)) .> 0; dims=2) / Nₛ
#md nothing #hide

# and compare it to the theoretical value of $1 - 1/Nₚ$.
plot(update_rate; label=false, ylim=[0, 1], legend=:bottomleft)
plot(update_rate; label=false, ylim=[0, 1], legend=:bottomleft, xlabel="Iteration", ylabel="Update rate")
hline!([1 - 1 / Nₚ]; label="N: $(Nₚ)")
xlabel!("Iteration")
ylabel!("Update rate")
18 changes: 6 additions & 12 deletions examples/particle-gibbs/script.jl
Expand Up @@ -67,12 +67,11 @@ for t in 1:Tₘ
end

# Here are the latent and observation series:
plot(x; label="x")
xlabel!("t")
plot(x; label="x", xlabel="t")

#
plot(y; label="y", xlabel="t")

#
plot(y; label="y")
xlabel!("x")

# Each model takes an `AbstractRNG` as input and generates the logpdf of the current transition:
function (model::NonLinearTimeSeries)(rng::Random.AbstractRNG)
Expand Down Expand Up @@ -125,20 +124,15 @@ mean_trajectory = mean(particles; dims=2)
# We can now plot all the generated traces.
# Beyond the last few timesteps all the trajectories collapse into one. Using the ancestor updating step can help
# with the degeneracy problem.
plot()
scatter(particles; label=false, opacity=0.01, color=:black)
scatter(particles; label=false, opacity=0.01, color=:black, xlabel="t", ylabel="state")
plot!(x; color=:darkorange, label="Original Trajectory")
plot!(mean_trajectory; color=:dodgerblue, label="Mean trajectory", opacity=0.9)
xlabel!("t")
ylabel!("State")

# We can also check the mixing as defined in the Gaussian State Space model example. As seen on the
# scatter plot above, we are mostly left with a single trajectory before timestep 150. The orange
# bar is the optimal mixing rate for the number of particles we use.
update_rate = sum(abs.(diff(particles; dims=2)) .> 0; dims=2) / Nₛ
#md nothing #hide

plot(update_rate; label=false, ylim=[0, 1], legend=:bottomleft)
plot(update_rate; label=false, ylim=[0, 1], legend=:bottomleft, xlabel="Iteration", ylabel="Update rate")
hline!([1 - 1 / Nₚ]; label="N: $(Nₚ)")
xlabel!("Iteration")
ylabel!("Update rate")

0 comments on commit 768b42c

Please sign in to comment.