Skip to content

Banana: Map from Samples ​

This example demonstrates how to use TransportMaps.jl to approximate a "banana" distribution using polynomial transport maps when only samples from the target distribution are available.

Unlike the density-based approach, this method learns the transport map directly from sample data using optimization techniques.

In the map-from-samples setting, the map also enables density estimation.

julia
using TransportMaps
using Distributions
using Optim
using CairoMakie
using LaTeXStrings
using Statistics

Generate target samples ​

The target density is

so exact samples can be generated by drawing independent standard-normal variables and setting   and   .

julia
function generate_banana_samples(number_samples)
    latent_samples = randn(number_samples, 2)
    return hcat(
        latent_samples[:, 1],
        latent_samples[:, 1] .^ 2 .+ latent_samples[:, 2],
    )
end

We generate the   samples and standardize the samples with the LinearMap:

julia
target_samples = generate_banana_samples(1_000)
linear_map = LinearMap(target_samples)
LinearMap with 2 dimensions
  μ: [-0.05002771798850485, 0.9722628747577637]
  σ: [0.9862398192221139, 1.7093842614811816]

Standard-normal reference ​

Sample-based construction fits a forward map    Hence, evaluate maps target samples to the reference, while inverse generates target samples from the reference. The linear map standardizes the data before the polynomial map is fitted.

julia
normal_map = PolynomialMap(2, 2, Normal(), Softplus())
normal_result = optimize!(normal_map, target_samples, linear_map)
normal_composed_map = ComposedMap(linear_map, normal_map)
ComposedMap{LinearMap} with 2 dimensions:
 linearmap: LinearMap(2-dimensional, μ: [-0.05002771798850485, 0.9722628747577637], σ: [0.9862398192221139, 1.7093842614811816])
 polynomialmap: PolynomialMap(2-dimensional, degree=2, basis=LinearizedHermiteBasis, reference=Distributions.Normal{Float64}(μ=0.0, σ=1.0), rectifier=Softplus, 9 total coefficients)

To check the accuracy of the map, we generate new samples from our target, map them to the reference through our map and check mean and standard-deviation:

julia
validation_samples = generate_banana_samples(1_000)
normal_validation_samples = evaluate(normal_composed_map, validation_samples)

println(
    "Mapped normal-reference mean: ",
    vec(mean(normal_validation_samples; dims = 1)),
)
println(
    "Mapped normal-reference std:  ",
    vec(std(normal_validation_samples; dims = 1)),
)
Mapped normal-reference mean: [0.052865302636260596, 0.05619412482114725]
Mapped normal-reference std:  [0.995740596458062, 0.9875216736285052]

Uniform reference ​

For the bounded reference we use a shifted Legendre basis. The constrained optimizer keeps the fitted training images close to the support of Uniform(0, 1).

julia
uniform_distribution = Uniform(0, 1)
uniform_map = PolynomialMap(
    2,
    5,
    uniform_distribution,
    Softplus(),
    ShiftedLegendreBasis(),
)
uniform_result = optimize!(
    uniform_map,
    target_samples,
    linear_map;
    options = Optim.Options(iterations = 120, x_abstol = 1.0e-7),
)
uniform_composed_map = ComposedMap(linear_map, uniform_map)
ComposedMap{LinearMap} with 2 dimensions:
 linearmap: LinearMap(2-dimensional, μ: [-0.05002771798850485, 0.9722628747577637], σ: [0.9862398192221139, 1.7093842614811816])
 polynomialmap: PolynomialMap(2-dimensional, degree=5, basis=ShiftedLegendreBasis, reference=Distributions.Uniform{Float64}(a=0.0, b=1.0), rectifier=Softplus, 27 total coefficients)

Again, we map our validation samples to the reference space and compare the moments:

julia
uniform_validation_samples = evaluate(uniform_composed_map, validation_samples)

println(
    "Mapped uniform-reference mean: ",
    vec(mean(uniform_validation_samples; dims = 1)),
)
println(
    "Mapped uniform-reference std:  ",
    vec(std(uniform_validation_samples; dims = 1)),
)
Mapped uniform-reference mean: [0.5151743688202401, 0.5831357398485283]
Mapped uniform-reference std:  [0.3076481078872615, 0.35425633905569637]

Target/reference comparison ​

Since we constructed the map from samples, the left panel shows the samples after evaluate maps them into reference space. The right panel shows the validation samples in over the fitted pullback density

julia
x₁ = range(-4, 4, length = 120)
x₂ = range(-3.5, 7, length = 120)

normal_comparison = reference_target_plot(
    normal_composed_map, validation_samples;
    xgrid = x₁, ygrid = x₂,
    reference_axis = (; limits = ((-4, 4), (-4, 4))),
    figure = (; size = (600, 400)),
)
Sample-based banana map to a standard-normal referenceSample-based banana map to a standard-normal reference
julia
uniform_comparison = reference_target_plot(
    uniform_composed_map, validation_samples;
    xgrid = x₁, ygrid = x₂,
    reference_axis = (; limits = ((0, 1), (0, 1))),
    figure = (; size = (600, 400)),
)
Sample-based banana map to a uniform referenceSample-based banana map to a uniform reference

The reference-space panels make the effect of changing the reference density explicit, while the target-space panels keep the fitted data and true banana geometry visible beside them.


This page was generated using Literate.jl.