{"id":24333716,"url":"https://github.com/charlesknipp/sequential_monte_carlo","last_synced_at":"2026-05-20T16:39:20.142Z","repository":{"id":44709959,"uuid":"419510233","full_name":"charlesknipp/sequential_monte_carlo","owner":"charlesknipp","description":"This module is an efficient and flexible implementation of various Sequential Monte Carlo (SMC) methods. Bayesian updates occur for both latent states and model parameters using joint inference.","archived":false,"fork":false,"pushed_at":"2023-04-18T01:37:52.000Z","size":11056,"stargazers_count":1,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-18T03:46:05.188Z","etag":null,"topics":["julia","mcmc-methods","sequential-monte-carlo"],"latest_commit_sha":null,"homepage":"","language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/charlesknipp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-10-20T22:40:42.000Z","updated_at":"2024-07-23T03:00:39.000Z","dependencies_parsed_at":"2023-12-12T06:34:31.595Z","dependency_job_id":null,"html_url":"https://github.com/charlesknipp/sequential_monte_carlo","commit_stats":null,"previous_names":["charlesknipp/sequential_monte_carlo","classicharlie/sequential_monte_carlo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesknipp%2Fsequential_monte_carlo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesknipp%2Fsequential_monte_carlo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesknipp%2Fsequential_monte_carlo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesknipp%2Fsequential_monte_carlo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/charlesknipp","download_url":"https://codeload.github.com/charlesknipp/sequential_monte_carlo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243056134,"owners_count":20229159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["julia","mcmc-methods","sequential-monte-carlo"],"created_at":"2025-01-18T03:46:13.653Z","updated_at":"2026-05-20T16:39:15.104Z","avatar_url":"https://github.com/charlesknipp.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SequentialMonteCarlo.jl\n\nSequentialMonteCarlo.jl presents a set of particle filters and Sequential Monte Carlo algorithms for both latent state and model parameter inference.\n\n## State Space Models\n\nTo begin using this module, we must first properly establish the construction of state space models (SSMs) under my framework. Typical model construction requires a small set of constructors for: (1) an initial distribution, (2) a transition density, and (3) an observation density.\n\nTo construct a univariate linear Gaussian SSM, we can define the following constructor, where `θ::Vector{Float64}` represents a vector of parameters.\n\n```julia\nlg_mod(θ) = StateSpaceModel(\n    LinearGaussian(θ[1],1.0,θ[2],θ[3],0.0),\n    (1,1)\n)\n```\n\nCalling `lg_mod(⋅)` constructs a `StateSpaceModel` object, which can be called to simulate data, or to be used in a particle filter. As long as the model in question has the required methods defined. For demonstration define `lg_example` and simulate 100 periods.\n\n```julia\nlg_example = lg_mod([0.5,0.9,0.8])\n_,y = simulate(lg_example,100)\n```\n\nThis construction weighs heavily on multiple dispatch, which is a nice feature of Julia but also requires more work from the user. As such, a better method is in the works.\n\n### Particle Filters\n\nParticle filters are primarily used for online inference for latent states, which is ideally defined as a mutating operation given a set of particles `x` and weights `w`.\n\nSuppose we observe a vector `y` simulated from model `lg_example`. To construct a bootstrap filter with 1024 particles and capture summary statistics at each time period we do the following:\n\n```julia\n# preallocate quantile vector and log likelihood\nxq = fill(zeros(Float64,3),length(y))\nlogZ = 0.0\n\n# initialize bootstrap filter at time t=1\nx,w,logμ = bootstrap_filter(1024,y[1],lg_example)\n\nxq[1] = quantile(x,[0.25,0.5,0.75])\nlogZ += logμ\n\n# subsequent iterations of the bootstrap filter\nfor t in 2:eachindex(y)\n    # run filter and print ess\n    logμ,w,ess = bootstrap_filter!(x,w,y[t],lg_example)\n    @printf(\"t = %4d\\tess = %4.3f\",t,ess)\n\n    # update summary statistics\n    xq[t] = quantile(x,[0.25,0.5,0.75])\n    logZ += logμ\nend\n```\n\nIt should be noted that `x` and `w` are both operated in-place and thus update with each call of `bootstrap_filter!()`. Furthermore, to avoid such long winded code, one can instead call the function `log_likelihood()` to run a particle filter over all periods of a model and return the log likelihood (or `logZ` as its referred to above).\n\n```julia\n# for a bootstrap filter leave the proposal argument empty\nlogZ = log_likelihood(1024,y,lg_example)\n```\n\nThe construction of these particle filters is not perfect on its own, since it is meant specifically to perform joint estimation. However, they are fully functional and meant to be flexible enough when called in high volumes. Something that `LowLevelParticleFilters.jl` actually fails to do (more on this in issue...).\n\n## Joint Inference\n\nSuppose a given state space model has an uncertain construction such that the model parameters are unobserved. Now the problem becomes twofold: what can we infer about the latent states as well as the parameters?\n\nTo solve this problem, I introduce two main algorithms: [SMC²](https://arxiv.org/pdf/1101.1528.pdf) and [density tempered SMC](https://www.tandfonline.com/doi/pdf/10.1080/07350015.2014.940081).\n\nRunning these algorithms requires a prior for the parameter space as well as a model constructor as we defined above with `lg_mod()`. Below we define `lg_prior` such that `length(lg_prior) == length(θ)` where `θ` is the input to the function `lg_mod`.\n\n```julia\n# model constructor\nlg_mod(θ) = StateSpaceModel(\n    LinearGaussian(θ[1],1.0,θ[2],θ[3],0.0),\n    (1,1)\n)\n\n# prior definition\nlg_prior = product_distribution([\n    TruncatedNormal(0,1,-1,1),\n    LogNormal(),\n    LogNormal()\n])\n```\n\nUpon declaration of the prior and model constructor, we define a generic SMC sampler with 513 parameter particles, 1024 state particles, and ESS threshold of 0.5, and 3 MCMC steps. To demonstrate, we can run density tempered SMC like so...\n\n```julia\nlg_dt_smc = SMC(512,1024,lg_mod,lg_prior,3,0.5)\ndensity_tempered(lg_dt_smc,y)\n```\n\nFor an online algorithm like SMC², we treat it similarly to the particle filter by using mutating functions to change properties of `lg_smc²` as time progresses.\n\n```julia\nlg_smc² = SMC(512,1024,lg_mod,lg_prior,3,0.5)\nsmc²(lg_smc²,y)\n\nfor t in 2:T\n    smc²!(lg_smc²,y,t)\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharlesknipp%2Fsequential_monte_carlo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharlesknipp%2Fsequential_monte_carlo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharlesknipp%2Fsequential_monte_carlo/lists"}