Banana: Map from Density
This example demonstrates how to use TransportMaps.jl to approximate a "banana" distribution using polynomial transport maps.
The banana distribution is a common test case in transport map literature [3], defined as a standard normal in the first dimension and a normal distribution centered at
We will construct the transport map for two different reference distributions and associated bases:
standard normal reference
uniform reference
using TransportMaps
using Distributions
using CairoMakie
using LaTeXStringsTarget density
The banana density is defined as
where
We define its log-density as:
logtarget(x) = logpdf(Normal(), x[1]) + logpdf(Normal(), x[2] - x[1]^2)
target = MapTargetDensity(logtarget)Standard-normal reference
A degree-two map can represent the characteristic quadratic bend directly. Constructing the quadrature from the map ensures that it matches the configured reference density.
normal_map = PolynomialMap(2, 2, Normal(), Softplus())
normal_quadrature = SparseSmolyakWeights(3, normal_map)
normal_result = optimize!(normal_map, target, normal_quadrature)
println(normal_result) * Status: success
* Candidate solution
Final objective value: 1.740527e-03
* Found with
Algorithm: L-BFGS
* Convergence measures
|x - x'| = 5.20e-07 ≰ 0.0e+00
|x - x'|/|x'| = 2.09e-07 ≰ 0.0e+00
|f(x) - f(x')| = 1.71e-13 ≰ 0.0e+00
|f(x) - f(x')|/|f(x')| = 9.82e-11 ≰ 0.0e+00
|g(x)| = 2.50e-09 ≤ 1.0e-08
* Work counters
Seconds run: 0 (vs limit Inf)
Iterations: 16
f(x) calls: 19
∇f(x) calls: 19
∇f(x)ᵀv calls: 0Next, we generate new samples in the reference density to assess the accuracy of the fitted map. For this, we evaluate the variance_diagnostic, defined as half the variance of the log-ratio between the target density represented in reference coordinates and the reference density:
And we compare this with the KL-divergence from the optimization.
normal_ref_samples = randn(2000, 2)
normal_target_samples = evaluate(normal_map, normal_ref_samples)
var_diag = variance_diagnostic(normal_map, target, normal_ref_samples)
println("Normal-reference KL divergence: ", normal_result.minimum)
println("Normal-reference variance diag: ", var_diag)Normal-reference KL divergence: 0.001740526943175369
Normal-reference variance diag: 0.000306826688063156Uniform reference
A uniform reference requires a (shifted) Legendre basis and associated quadrature on the domain
uniform_distribution = Uniform(0, 1)
uniform_map = PolynomialMap(
2,
5,
uniform_distribution,
Softplus(),
ShiftedLegendreBasis(),
)
uniform_quadrature = SparseSmolyakWeights(3, uniform_map)
uniform_result = optimize!(uniform_map, target, uniform_quadrature)
println(uniform_result) * Status: success
* Candidate solution
Final objective value: 7.400995e-02
* Found with
Algorithm: L-BFGS
* Convergence measures
|x - x'| = 2.42e-08 ≰ 0.0e+00
|x - x'|/|x'| = 9.35e-09 ≰ 0.0e+00
|f(x) - f(x')| = 2.36e-16 ≰ 0.0e+00
|f(x) - f(x')|/|f(x')| = 3.19e-15 ≰ 0.0e+00
|g(x)| = 4.62e-09 ≤ 1.0e-08
* Work counters
Seconds run: 0 (vs limit Inf)
Iterations: 80
f(x) calls: 88
∇f(x) calls: 88
∇f(x)ᵀv calls: 0Again, we generate new samples to assess the accuracy of the fitted map:
uniform_ref_samples = rand(uniform_distribution, 2000, 2)
uniform_target_samples = evaluate(uniform_map, uniform_ref_samples)
var_diag_uniform = variance_diagnostic(uniform_map, target, uniform_ref_samples)
println("Uniform-reference KL divergence: ", uniform_result.minimum)
println("Uniform-reference variance diag: ", var_diag_uniform)Uniform-reference KL divergence: 0.07400994787962466
Uniform-reference variance diag: 0.04594138792065879Target/reference comparison
We use the same plot construction for both maps: reference samples on the left and their transported target samples over the true banana-density contours on the right.
Choose the target-space grid for the density contours:
x₁ = range(-4, 4, length = 120)
x₂ = range(-3, 7, length = 120)
normal_comparison = reference_target_plot(
normal_map, normal_ref_samples;
density = target,
xgrid = x₁, ygrid = x₂,
reference_axis = (; limits = ((-4, 4), (-4, 4))),
)uniform_comparison = reference_target_plot(
uniform_map, uniform_ref_samples;
density = target,
xgrid = x₁, ygrid = x₂,
reference_axis = (; limits = ((0, 1), (0, 1))),
)Both maps recover the curved target geometry. The standard-normal reference is especially economical for this target, while the uniform-reference map shows that density-based construction is not restricted to a Gaussian reference.
Visualizing the map
A regular grid makes the nonlinear transformation visible: straight reference lines become curved target-space lines under the fitted map.
grid = range(-2, 2; length = 100)
mapping_fig = Figure(size = (600, 400))
reference_ax = Axis(mapping_fig[1, 1]; title = "Reference", xlabel = L"z_1", ylabel = L"z_2")
target_ax = Axis(mapping_fig[1, 2]; title = "Mapped grid", xlabel = L"x_1", ylabel = L"x_2")
identity_map = LinearMap(zeros(2), ones(2))
mappingplot!(reference_ax, identity_map, (grid, grid); gridlines = 9)
mappingplot!(target_ax, normal_map, (grid, grid); gridlines = 9)Contributions of individual terms
Coefficients describe the polynomial parameterization before rectification. To measure their effect on the final component, we can also set each coefficient to zero in turn and compute the RMS output change over the same reference samples. These removal scores are sample-dependent, non-additive diagnostics, without refitting.
term_fig = Figure(size = (600, 350))
termplot(
term_fig[1, 1], normal_map[2];
axis = (; title = "Coefficients", xlabel = L"a_\alpha")
)
termplot(
term_fig[1, 2], normal_map[2];
measure = :removal_rms, samples = normal_ref_samples[1:300, :],
axis = (; title = "Removal effect", xlabel = "RMS change")
)This page was generated using Literate.jl.