Skip to content

Simplify worked example in README further #424

@yebai

Description

@yebai

Current log density definition:

using AdvancedHMC, AbstractMCMC
using LogDensityProblems, LogDensityProblemsAD, ADTypes # For defining the target distribution & its gradient
using ForwardDiff # An example AD backend
using Random # For initial parameters

# 1. Define the target distribution using the LogDensityProblems interface
struct LogTargetDensity
    dim::Int
end
# Log density of a standard multivariate normal distribution
LogDensityProblems.logdensity(p::LogTargetDensity, θ) = -sum(abs2, θ) / 2
LogDensityProblems.dimension(p::LogTargetDensity) = p.dim
# Declare that the log density function is defined
function LogDensityProblems.capabilities(::Type{LogTargetDensity})
    return LogDensityProblems.LogDensityOrder{0}()
end

# Set parameter dimensionality
D = 10

# 2. Wrap the log density function and specify the AD backend.
#    This creates a callable struct that computes the log density and its gradient.
ℓπ = LogTargetDensity(D)
model = AdvancedHMC.LogDensityModel(LogDensityProblemsAD.ADgradient(AutoForwardDiff(), ℓπ))

It could be replaced with a one-liner, e.g., via logpdf from Distributions.jl.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions