-
Notifications
You must be signed in to change notification settings - Fork 4
Turing.jl Integration #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
THargreaves
wants to merge
35
commits into
main
Choose a base branch
from
th/pgas-nuts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
bf6022e
Implement PGAS with NUTS
THargreaves 436ac64
Implement CSMC loop
THargreaves fe9c83c
Add ESS benchmarks to RB-PGAS test
THargreaves d51a0b0
Refactor backward simulation for new CSMC interface
THargreaves e78a30e
Correct Kalman gradients for H and c
THargreaves 82682b2
Implement ChainRules and LogDensityProblems interface for SSM
THargreaves d82d212
Implement particle Gibbs
THargreaves 9a6fb7d
Refactor augemented LGSSM code
THargreaves 04d52b0
Implement Turing integration
THargreaves 518ff99
Remove prototype/legacy code
THargreaves e01b0d5
Refactor PG to use arbitrary param sampler, update AHMC/AMH compat bo…
THargreaves 1f64c8d
Unified duplicate elements of code-base, generalised for other analyt…
THargreaves f4bddfc
Correct CSMC-AS loglik output and add unit test
THargreaves a74471e
Refactor duplicate code
THargreaves 98afb47
Formatting
THargreaves aa98556
Add example script
THargreaves f245460
Improve type stability
THargreaves 100ac33
Fix formatting
THargreaves 5a86410
Merge branch 'main' into th/pgas-nuts
THargreaves fd14793
Merge branch 'main' into th/pgas-nuts
THargreaves 2de0c92
Add return statements to model definitions
THargreaves 77a093e
Add simple PGAS example
THargreaves d6932af
Add alternative AD frameworks and compare to MH
THargreaves db7d20a
Reduce allocations in resampling
THargreaves 918021e
Implement rrule for mooncake
THargreaves a465b65
Add Mooncake benchmarks
THargreaves 3575468
Cleaned Mooncake integration
THargreaves ebfb8fe
Generalise Mooncake AD support to all AbstractPDMat
THargreaves fa2f02a
Add unit tests for Mooncake integration
THargreaves f3e480e
Format
THargreaves ac95f71
Add missing jitter to KF gradient
THargreaves 4973637
Move Mooncake integration to extension
THargreaves 0ec1da0
Create package extension for CUDA
THargreaves 09da0b5
Make ForwardDiff a test dep
THargreaves 25f1632
Format
THargreaves File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,15 @@ | ||
| [deps] | ||
| ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" | ||
| AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" | ||
| AdvancedHMC = "0bf59076-c3b1-5ca4-86bd-e02cd72cde3d" | ||
| ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
| Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
| GeneralisedFilters = "3ef92589-7ab8-43f9-b5b9-a3a0c86ecbb7" | ||
| MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" | ||
| Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6" | ||
| PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150" | ||
| SSMProblems = "26aad666-b158-4e64-9d35-0e672562fa48" | ||
| StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
| Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | ||
| Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" | ||
| Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" |
This file contains hidden or 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,97 @@ | ||
| using GeneralisedFilters | ||
| using AbstractMCMC: AbstractMCMC | ||
| using AdvancedHMC | ||
| using ADTypes: ADTypes | ||
| using MCMCChains: MCMCChains | ||
| using Turing: @model | ||
| using Distributions | ||
| using PDMats | ||
| using LinearAlgebra | ||
| using Random | ||
| using Statistics | ||
| using SSMProblems | ||
| using StaticArrays | ||
| using Zygote | ||
| using Mooncake | ||
|
|
||
| rng = MersenneTwister(1234) | ||
|
|
||
| a = 0.8 | ||
| c_val = 0.5 # coupling: inner offset = b + c_val * outer_state | ||
| q² = 0.1 | ||
| r² = 0.5 | ||
| σ₀² = 1.0 | ||
| σ_b² = 4.0 | ||
| T_len = 100 | ||
| N_particles = 50 | ||
| N_iter = 50 | ||
| N_adapts = 10 | ||
|
|
||
| # HierarchicalSSM with inner drift b: | ||
| # outer state — AR(1), sampled via particles | ||
| # inner state — AR(1) with offset b + c_val * outer_state, marginalised via KF | ||
| # observations — from inner state only | ||
| # | ||
| # All constant parts are pre-built so that PDMat constructors never appear in the | ||
| # Zygote trace — only getfield accesses, which Zygote handles natively. | ||
| const _fixed_ssm = let | ||
| outer_prior = HomogeneousGaussianPrior(SVector{1}(0.0), PDMat(SMatrix{1,1}(σ₀²))) | ||
| outer_dyn = HomogeneousLinearGaussianLatentDynamics( | ||
| SMatrix{1,1}(a), SVector{1}(0.0), PDMat(SMatrix{1,1}(q²)) | ||
| ) | ||
| inner_prior = HomogeneousGaussianPrior(SVector{1}(0.0), PDMat(SMatrix{1,1}(σ₀²))) | ||
| inner_dyn = GeneralisedFilters.GFTest.InnerDynamics( | ||
| SMatrix{1,1}(a), | ||
| SVector{1,Float64}([0.0]), | ||
| SMatrix{1,1}(c_val), | ||
| PDMat(SMatrix{1,1}(q²)), | ||
| ) | ||
| inner_obs = HomogeneousLinearGaussianObservationProcess( | ||
| SMatrix{1,1}(1.0), SVector{1}(0.0), PDMat(SMatrix{1,1}(r²)) | ||
| ) | ||
| HierarchicalSSM(outer_prior, outer_dyn, inner_prior, inner_dyn, inner_obs) | ||
| end | ||
|
|
||
| function build_ssm_rb(b) | ||
| dyn = _fixed_ssm.inner_model.dyn | ||
| new_inner_dyn = GeneralisedFilters.GFTest.InnerDynamics( | ||
| dyn.A, SVector{1,Float64}(b), dyn.C, dyn.Q | ||
| ) | ||
| return HierarchicalSSM( | ||
| _fixed_ssm.outer_prior, | ||
| _fixed_ssm.outer_dyn, | ||
| _fixed_ssm.inner_model.prior, | ||
| new_inner_dyn, | ||
| _fixed_ssm.inner_model.obs, | ||
| ) | ||
| end | ||
|
|
||
| true_b = 1.5 | ||
| _, _, _, _, ys = AbstractMCMC.sample(rng, build_ssm_rb([true_b]), T_len) | ||
|
|
||
| @model function drift_model_rb(ys) | ||
| b ~ MvNormal([0.0], σ_b² * I) | ||
| ssm = build_ssm_rb(b) | ||
| x ~ SSMTrajectory(ssm, KF(), ys) | ||
| return nothing | ||
| end | ||
|
|
||
| m = drift_model_rb(ys) | ||
| param_sampler = HMC(0.01, 10) | ||
| adtype = ADTypes.AutoZygote() | ||
| # adtype = ADTypes.AutoMooncake() | ||
| pg = ParticleGibbs(CSMCAS(RBPF(BF(N_particles), KF())), param_sampler; adtype=adtype) | ||
|
|
||
| chain = AbstractMCMC.sample( | ||
| rng, m, pg, N_iter; n_adapts=N_adapts, progress=true, chain_type=MCMCChains.Chains | ||
| ) | ||
|
|
||
| @profview begin | ||
| chain = AbstractMCMC.sample( | ||
| rng, m, pg, N_iter; n_adapts=N_adapts, progress=true, chain_type=MCMCChains.Chains | ||
| ) | ||
| end | ||
|
|
||
| @benchmark AbstractMCMC.sample( | ||
| rng, m, pg, N_iter; n_adapts=N_adapts, progress=true, chain_type=MCMCChains.Chains | ||
| ) |
This file contains hidden or 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,68 @@ | ||
| using GeneralisedFilters | ||
| using AbstractMCMC: AbstractMCMC | ||
| using AdvancedHMC: NUTS | ||
| using MCMCChains: MCMCChains | ||
| using Turing: @model | ||
| using Distributions | ||
| using PDMats | ||
| using LinearAlgebra | ||
| using Random | ||
| using Statistics | ||
| using SSMProblems | ||
| using StaticArrays | ||
| using Zygote | ||
|
|
||
| rng = MersenneTwister(1234) | ||
|
|
||
| a = 0.8 | ||
| q² = 0.1 | ||
| r² = 0.5 | ||
| σ₀² = 1.0 | ||
| σ_b² = 4.0 | ||
| T_len = 100 | ||
| N_particles = 50 | ||
| N_iter = 5000 | ||
| N_adapts = 1000 | ||
|
|
||
| function build_ssm_reg(drift) | ||
| return create_homogeneous_linear_gaussian_model( | ||
| SVector{1}(0.0), | ||
| PDMat(SMatrix{1,1}(σ₀²)), | ||
| SMatrix{1,1}(a), | ||
| SVector{1}(only(drift)), | ||
| PDMat(SMatrix{1,1}(q²)), | ||
| SMatrix{1,1}(1.0), | ||
| SVector{1}(0.0), | ||
| PDMat(SMatrix{1,1}(r²)), | ||
| ) | ||
| end | ||
|
|
||
| true_b = 1.5 | ||
| true_ssm = build_ssm_reg([true_b]) | ||
| _, _, ys = SSMProblems.sample(rng, true_ssm, T_len) | ||
|
|
||
| @model function drift_model_reg(ys) | ||
| b ~ MvNormal([0.0], σ_b² * I) | ||
| ssm = build_ssm_reg(b) | ||
| x ~ SSMTrajectory(ssm, ys) | ||
| return nothing | ||
| end | ||
|
|
||
| m = drift_model_reg(ys) | ||
| # pg = ParticleGibbs(CSMC(BF(N_particles)), NUTS(0.8)) | ||
| pg = ParticleGibbs(CSMCAS(BF(N_particles)), NUTS(0.8)) | ||
| # pg = ParticleGibbs(CSMCBS(BF(N_particles)), NUTS(0.8)) | ||
|
|
||
| chain = AbstractMCMC.sample( | ||
| rng, m, pg, N_iter; n_adapts=N_adapts, progress=false, chain_type=MCMCChains.Chains | ||
| ) | ||
|
|
||
| display( | ||
| @benchmark AbstractMCMC.sample( | ||
| rng, m, pg, N_iter; n_adapts=N_adapts, progress=false, chain_type=MCMCChains.Chains | ||
| ) | ||
| ) | ||
|
|
||
| @profview AbstractMCMC.sample( | ||
| rng, m, pg, N_iter; n_adapts=N_adapts, progress=false, chain_type=MCMCChains.Chains | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.