Skip to content

Commit acbc4e1

Browse files
committedMar 7, 2025
reorganized and added Mooncake MWE
1 parent e99be16 commit acbc4e1

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed
 

Diff for: ‎research/maximum_likelihood/Project.toml

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
[deps]
22
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
3+
DistributionsAD = "ced4e74d-a319-5a8a-b0ac-84af2272839c"
4+
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
35
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
6+
GeneralisedFilters = "3ef92589-7ab8-43f9-b5b9-a3a0c86ecbb7"
47
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
8+
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
59
Optimisers = "3bd65402-5787-11e9-1adc-39752487f4e2"
610
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
711
SSMProblems = "26aad666-b158-4e64-9d35-0e672562fa48"

Diff for: ‎research/maximum_likelihood/mle_demo.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ end
3636
# check type stability (important for use with Enzyme)
3737
@code_warntype logℓ([1.0], ys)
3838

39-
## MLE #####################################################################################
39+
## NEWTONS METHOD ##########################################################################
4040

4141
using DifferentiationInterface
42-
using ForwardDiff
42+
import ForwardDiff
4343
using Optimisers
4444

4545
# initial value

Diff for: ‎research/maximum_likelihood/mooncake_test.jl

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using GeneralisedFilters
2+
using SSMProblems
3+
using LinearAlgebra
4+
using Random
5+
6+
## TOY MODEL ###############################################################################
7+
8+
# this is taken from an example in Kalman.jl
9+
function toy_model::T) where {T<:Real}
10+
μ0 = T[1.0, 0.0]
11+
Σ0 = Diagonal(ones(T, 2))
12+
13+
A = T[0.8 θ/2; -0.1 0.8]
14+
Q = Diagonal(T[0.2, 1.0])
15+
b = zeros(T, 2)
16+
17+
H = Matrix{T}(I, 1, 2)
18+
R = Diagonal(T[0.2])
19+
c = zeros(T, 1)
20+
21+
return create_homogeneous_linear_gaussian_model(μ0, Σ0, A, b, Q, H, c, R)
22+
end
23+
24+
# data generation process with small sample
25+
rng = MersenneTwister(1234)
26+
true_model = toy_model(1.0)
27+
_, _, ys = sample(rng, true_model, 20)
28+
29+
## RUN MOONCKAE TESTS ######################################################################
30+
31+
using DifferentiationInterface
32+
import Mooncake
33+
using DistributionsAD
34+
35+
function build_objective(rng, θ, algo, data)
36+
_, ll = GeneralisedFilters.filter(rng, toy_model(θ[]), algo, data)
37+
return -ll
38+
end
39+
40+
# kalman filter likelihood testing (works, but is slow)
41+
logℓ1 = θ -> build_objective(rng, θ, KF(), ys)
42+
Mooncake.TestUtils.test_rule(rng, logℓ1, [0.7]; is_primitive=false, debug_mode=true)
43+
44+
# bootstrap filter likelihood testing (shouldn't work)
45+
logℓ2 = θ -> build_objective(rng, θ, BF(512), ys)
46+
Mooncake.TestUtils.test_rule(rng, logℓ2, [0.7]; is_primitive=false, debug_mode=true)
47+
48+
## FOR USE WITH DIFFERENTIATION INTERFACE ##################################################
49+
50+
# data should be part of the objective, but be held constant by DifferentiationInterface
51+
logℓ3 = (θ, data) -> build_objective(rng, θ, KF(), data)
52+
53+
# set the backend with default configuration
54+
backend = AutoMooncake(; config=nothing)
55+
56+
# prepare the gradient for faster subsequent iteration
57+
grad_prep = prepare_gradient(logℓ3, backend, [0.7], Constant(ys))
58+
59+
# evaluate gradients and iterate to show proof of concept
60+
DifferentiationInterface.gradient(logℓ3, grad_prep, backend, [0.7], Constant(ys))
61+
DifferentiationInterface.gradient(logℓ3, grad_prep, backend, [0.8], Constant(ys))
62+
DifferentiationInterface.gradient(logℓ3, grad_prep, backend, [0.9], Constant(ys))
63+
DifferentiationInterface.gradient(logℓ3, grad_prep, backend, [1.0], Constant(ys))

0 commit comments

Comments
 (0)