Skip to content

Instantly share code, notes, and snippets.

View wupeifan's full-sized avatar
coffee is my fuel

Peifan Wu wupeifan

coffee is my fuel
View GitHub Profile
@wupeifan
wupeifan / forward_kalman.jl
Created October 16, 2020 01:42
Forward Kalman code
# Forward Kalman
function solve_kalman(m::AbstractFirstOrderExpectationalDifferenceModel, sol::FirstOrderSolution, Q, obs, Ω, x_0 = nothing)
@unpack n, n_x, n_y, n_p, n_ϵ, η = m
@unpack h_x, g_x, h_x_p, g_x_p, Σ, Σ_p = sol
(isnothing(x_0) || length(x_0) == n_x) ||
throw(ArgumentError("Length of x_0 mismatches model"))
T = size(obs, 1)
n_z = size(Q, 1)
z = [zeros(n_z) for _ in 1:T]
@wupeifan
wupeifan / solve_kalman.jl
Last active June 28, 2020 15:21
Kalman filter + Zygote + Turing
using DifferentiableStateSpaceModels, ModelingToolkit, SparseArrays, LinearAlgebra, Parameters, Test, TimerOutputs, BenchmarkTools
using Turing, Zygote, ChainRules, Optim
Turing.setadbackend(:zygote)
# Generate fake data
H, mod_vals = Examples.rbc()
model = FirstOrderStateSpaceModel(H; mod_vals...)
p = [0.2, 0.02, 0.01]
θ_value = [0.5, 0.95]
solution, _ = solve_model!(model, θ = θ_value, p = p)
@wupeifan
wupeifan / estimate_epsilon.jl
Last active June 26, 2020 04:01
Example of DSSM+Zygote+Turing
using DifferentiableStateSpaceModels, ModelingToolkit, SparseArrays, LinearAlgebra, Parameters, Test, TimerOutputs, BenchmarkTools
using Turing, Zygote, ChainRules
Turing.setadbackend(:zygote)
# Generate fake data
H, mod_vals = Examples.rbc()
model = FirstOrderStateSpaceModel(H; mod_vals...)
p = [0.2, 0.02, 0.01]
θ_value = [0.5, 0.95]
solution, _ = solve_model!(model, θ = θ_value, p = p)