-
Notifications
You must be signed in to change notification settings - Fork 45
Add RankUpdateEuclideanMetric #443
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -98,6 +98,84 @@ function Base.show(io::IO, dem::DenseEuclideanMetric) | |||||||
return print(io, "DenseEuclideanMetric(diag=$(_string_M⁻¹(dem.M⁻¹)))") | ||||||||
end | ||||||||
|
||||||||
""" | ||||||||
RankUpdateEuclideanMetric{T,AM,AB,AD,F} <: AbstractMetric | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
A Gaussian Euclidean metric whose inverse is constructed by rank-updates. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
ErikQQY marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
# Fields | ||||||||
|
||||||||
$(TYPEDFIELDS) | ||||||||
|
||||||||
# Constructors | ||||||||
|
||||||||
RankUpdateEuclideanMetric(n::Int) | ||||||||
RankUpdateEuclideanMetric(M⁻¹, B, D) | ||||||||
|
||||||||
- Construct a Gaussian Euclidean metric of size `(n, n)` with `M⁻¹` being diagonal matrix. | ||||||||
- Construct a Gaussian Euclidean metric of `M⁻¹`, where `M⁻¹` should be a full rank positive definite matrix, | ||||||||
and `B` `D` must be chose so that the Woodbury matrix `W = M⁻¹ + B D B^\\mathrm{T}` is positive definite. | ||||||||
Comment on lines
+115
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the other metrics There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For Pathfinder to safely convert its There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
# Example | ||||||||
|
||||||||
```julia | ||||||||
julia> RankUpdateEuclideanMetric(3) | ||||||||
RankUpdateEuclideanMetric(diag=[1.0, 1.0, 1.0]) | ||||||||
``` | ||||||||
|
||||||||
# References | ||||||||
|
||||||||
- Ben Bales, Arya Pourzanjani, Aki Vehtari, Linda Petzold, Selecting the Metric in Hamiltonian Monte Carlo, 2019 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The factorized form used here is due to the Pathfinder paper, so I recommend also citing http://jmlr.org/papers/v23/21-0889.html |
||||||||
""" | ||||||||
struct RankUpdateEuclideanMetric{T,AM<:AbstractVecOrMat{T},AB,AD,F} <: AbstractMetric | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More a question than a request: Is there a reason why |
||||||||
"Diagnal of the inverse of the mass matrix" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
M⁻¹::AM | ||||||||
B::AB | ||||||||
D::AD | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
factorization::F | ||||||||
end | ||||||||
|
||||||||
function woodbury_factorize(A, B, D) | ||||||||
cholA = cholesky(A isa Diagonal ? A : Symmetric(A)) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pathfinder's implementation allows for arbitrary PD |
||||||||
U = cholA.U | ||||||||
Q, R = qr(U' \ B) | ||||||||
V = cholesky(Symmetric(muladd(R, D * R', I))).U | ||||||||
return (U=U, Q=Q, V=V) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
end | ||||||||
|
||||||||
function RankUpdateEuclideanMetric(n::Int) | ||||||||
M⁻¹ = Diagonal(ones(n)) | ||||||||
B = zeros(n, 0) | ||||||||
D = zeros(0, 0) | ||||||||
factorization = woodbury_factorize(M⁻¹, B, D) | ||||||||
return RankUpdateEuclideanMetric(M⁻¹, B, D, factorization) | ||||||||
end | ||||||||
Comment on lines
+146
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: this is probably fine for now, but there are multiple ways to form the identity matrix here, and later it might be better to initialize a different way (e.g. for tuning a covariance matrix for factor analysis, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know it breaks with the API of other metric constructors, but I think you want to be able to initialize the rank of the update as well. Otherwise you have no way to fix the rank for future tuning algorithms. |
||||||||
function RankUpdateEuclideanMetric(::Type{T}, n::Int) where {T} | ||||||||
M⁻¹ = Diagonal(ones(T, n)) | ||||||||
B = Matrix{T}(undef, n, 0) | ||||||||
D = Matrix{T}(undef, 0, 0) | ||||||||
factorization = woodbury_factorize(M⁻¹, B, D) | ||||||||
return RankUpdateEuclideanMetric(M⁻¹, B, D, factorization) | ||||||||
end | ||||||||
|
||||||||
function RankUpdateEuclideanMetric(M⁻¹, B, D) | ||||||||
factorization = woodbury_factorize(M⁻¹, B, D) | ||||||||
return RankUpdateEuclideanMetric(M⁻¹, B, D, factorization) | ||||||||
end | ||||||||
|
||||||||
function RankUpdateEuclideanMetric(::Type{T}, sz::Tuple{Int}) where {T} | ||||||||
return RankUpdateEuclideanMetric(T, first(sz)) | ||||||||
end | ||||||||
ErikQQY marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
RankUpdateEuclideanMetric(sz::Tuple{Int}) = RankUpdateEuclideanMetric(Float64, sz) | ||||||||
|
||||||||
renew(::RankUpdateEuclideanMetric, (M⁻¹, B, D)) = RankUpdateEuclideanMetric(M⁻¹, B, D) | ||||||||
|
||||||||
Base.size(metric::RankUpdateEuclideanMetric, dim...) = size(metric.M⁻¹.diag, dim...) | ||||||||
|
||||||||
function Base.show(io::IO, ::MIME"text/plain", metric::RankUpdateEuclideanMetric) | ||||||||
return print(io, "RankUpdateEuclideanMetric(diag=$(diag(metric.M⁻¹)))") | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's another example where calling the full-rank part of the inverse-metric |
||||||||
end | ||||||||
|
||||||||
# `rand` functions for `metric` types. | ||||||||
|
||||||||
function rand_momentum( | ||||||||
|
@@ -131,3 +209,19 @@ function rand_momentum( | |||||||
ldiv!(metric.cholM⁻¹, r) | ||||||||
return r | ||||||||
end | ||||||||
|
||||||||
function rand_momentum( | ||||||||
rng::Union{AbstractRNG,AbstractVector{<:AbstractRNG}}, | ||||||||
metric::RankUpdateEuclideanMetric{T}, | ||||||||
kinetic::GaussianKinetic, | ||||||||
::AbstractVecOrMat, | ||||||||
) where {T} | ||||||||
M⁻¹ = metric.M⁻¹ | ||||||||
r = _randn(rng, T, size(M⁻¹.diag)...) | ||||||||
F = metric.factorization | ||||||||
k = min(size(F.U, 1), size(F.V, 1)) | ||||||||
@views ldiv!(F.V, r isa AbstractVector ? r[1:k] : r[1:k, :]) | ||||||||
lmul!(F.Q, r) | ||||||||
ldiv!(F.U, r) | ||||||||
return r | ||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that because you've defined
M
as just the diagonal part, this equation is incorrect. The full one is https://github.com/mlcolab/Pathfinder.jl/blob/f4ca90dc3d91f077f479d13904a2b6bf99e8ee25/src/integration/advancedhmc.jl#L82, which uses https://github.com/mlcolab/Pathfinder.jl/blob/f4ca90dc3d91f077f479d13904a2b6bf99e8ee25/src/woodbury.jl#L384-L388