Skip to content

Conditional Densities ​

When constructing a transport map, the triangular structure of the Knothe-Rosenblatt rearrangement provides a systematic way to factorize the joint density into a product of conditional densities:

In Refs. [3] and [5], the authors show that these conditional densities are obtained sequentially by inverting the map components after one another. We use the reference-to-target map defined in the Introduction, with an independent product reference  .

The conditional density for given is defined as:

Here denotes the first components, inverted sequentially, and is the one-dimensional reference density. The hat distinguishes the fitted model from the true target conditional density.

To sample given , first invert those fixed coordinates to obtain . Draw a fresh  , then evaluate  .

Setting up a Transport Map ​

For this example, we'll use the banana distribution as our target density, also used in Banana: Map from Density. The banana distribution is defined as a:

where is the standard normal PDF.

Define the target log-density and construct a map as in Getting Started:

julia
using TransportMaps
using Distributions

logtarget(x) = logpdf(Normal(), x[1]) + logpdf(Normal(), x[2] - x[1]^2)
target = MapTargetDensity(logtarget)

Define the map and quadrature; and optimize the map:

julia
M = PolynomialMap(2, 2, :normal, Softplus(), HermiteBasis())
quadrature = GaussHermiteWeights(3, M)

# Optimize the map:
optimize!(M, target, quadrature)

Conditional Density Evaluation ​

Now we can compute conditional densities. For simplicity, we look at a two-dimensional example, so we are interested in the conditional density  .

Define the conditioning variable and the variable to evaluate the conditional density:

julia
x₁ = 0.5
x₂ = 0.8
density = conditional_density(M, x₂, x₁)
println("Conditional density π(x₂=$x₂ | x₁=$x₁) = $density")
Conditional density π(x₂=0.8 | x₁=0.5) = 0.3429438568458935

Evaluate conditional density for multiple values:

julia
x₂_values = range(-3, 3, length = 100)
densities = conditional_density(M, x₂_values, x₁)

Plot the conditional density: Conditional DensityConditional Density

Conditional Sampling ​

We can also sample from the conditional density . First, we sample in the standard normal space and then evaluate the conditional map:

Single value sampling:

julia
z₂ = randn()
cond_sample = conditional_sample(M, x₁, z₂)
println("Conditional sample for z₂=$z₂: x₂=$cond_sample")
Conditional sample for z₂=1.4435507858611327: x₂=1.6935507852642648

Multiple samples:

julia
z₂_values = randn(10_000)
cond_samples = conditional_sample(M, x₁, z₂_values)

Create a histogram of the conditional samples and overlay the fitted conditional density: Conditional SamplingConditional Sampling

Comparison with True Conditional Density ​

Let's compare our transport map's conditional density with the true conditional density of the target distribution:

True conditional density for the banana distribution:

julia
true_banana_conditional_density(x₂, x₁) = pdf(Normal(x₁^2, 1), x₂)

Compute true conditional densities:

julia
true_densities = [true_banana_conditional_density(x₂, x₁) for x₂ in x₂_values]

Plot comparison: Conditional ComparisonConditional Comparison

Multiple Conditioning Scenarios ​

Let's explore how the conditional density π(x₂ | x₁) changes as we vary x₁. This shows the nonlinear structure of the banana distribution:

julia
x₁_values = [-0.6, 0.0, 1.0, 2.0]
Multiple ConditioningMultiple Conditioning

Note

For a more comprehensive example with real-world applications, see the Bayesian Inference: Biochemical Oxygen Demand (BOD) Example.


This page was generated using Literate.jl.