Skip to content

Getting Started with TransportMaps.jl ​

Turn independent standard-normal draws into samples from a correlated Gaussian. In four steps, you will define a target, fit a map, generate samples, and check the result. For the concepts and notation, start with the Introduction. Independent reference samples transformed into a correlated Gaussian targetIndependent reference samples transformed into a correlated Gaussian target

Install and Load ​

Install the package and the distribution tools used in this guide:

julia
using Pkg
Pkg.add(["TransportMaps", "Distributions"])
julia
using TransportMaps, Distributions

1. Define the Target ​

Our target is a two-dimensional Gaussian with zero mean, unit marginal variances, and correlation . Wrap its log-density in MapTargetDensity:

julia
gaussian = MvNormal(zeros(2), [1.0 0.8; 0.8 1.0])
target = MapTargetDensity(x -> logpdf(gaussian, x))

Pass a log-density

The callable must return the log of the target density. It may be unnormalized, which is useful for Bayesian posteriors.

2. Fit the Map ​

Create a two-dimensional map with polynomial degree one. Its defaults use a standard-normal reference, a Hermite-based expansion, and softplus rectification. Construct quadrature for that reference, then fit the coefficients:

julia
M = PolynomialMap(2, 1)
quadrature = GaussHermiteWeights(2, M)
result = optimize!(M, target, quadrature)

optimize! updates M in place. The returned result contains the solver's convergence information. You can reuse the fitted map to generate new samples.

3. Generate Samples ​

Draw from the reference and transform the draws with evaluate:

julia
reference_samples = randn(1_000, 2)
target_samples = evaluate(M, reference_samples)

Each row is one sample; each column is one coordinate. target_samples contains independent draws from the fitted approximation to the target. In the figure above, the circular reference cloud becomes a tilted target cloud; the contours show the known Gaussian target density.

4. Check the Result ​

Evaluate the variance diagnostic on the reference samples:

julia
variance_diagnostic(M, target, reference_samples)
3.895180585436825e-18

A value close to zero indicates little variation in the log-density mismatch at the sampled points. For this simple target, it should be small. This is a useful diagnostic, rather than a guarantee of accuracy over the entire domain.

Plot Your Map ​

The reference_target_plot result is shown at the top of this page. To reproduce it, install a Makie backend once:

julia
Pkg.add("CairoMakie")

Then load it and call reference_target_plot:

julia
using CairoMakie
fig = reference_target_plot(M, reference_samples; density = target)

mappingplot shows the transformation itself by mapping a regular grid. Here the grid becomes sheared, reflecting the correlation in the target:

julia
grid = range(-2, 2; length = 100)
fig, ax, plt = mappingplot(
    M, (grid, grid); gridlines = 9,
    axis = (; xlabel = "x₁", ylabel = "x₂", aspect = DataAspect()),
    figure = (; size = (400, 500))
)
Regular reference grid transformed by the fitted Gaussian mapRegular reference grid transformed by the fitted Gaussian map

Where to Go Next ​

Your next taskGuide
Learn from observations instead of a densityBanana: Map from Samples
Fit a nonlinear target densityBanana: Map from Density
Infer parameters from measurementsBayesian inference with BOD data
Choose bases, rectifiers, and map structureBasis Functions and Map Parameterization
Tune integration or solver settingsQuadrature Methods and Optimization

This page was generated using Literate.jl.