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
The conditional density for
Here
To sample given
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
Define the target log-density and construct a map as in Getting Started:
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:
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:
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.3429438568458935Evaluate conditional density for multiple values:
x₂_values = range(-3, 3, length = 100)
densities = conditional_density(M, x₂_values, x₁)Plot the conditional density:
Conditional Sampling
We can also sample from the conditional density
Single value sampling:
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.6935507852642648Multiple samples:
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:
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:
true_banana_conditional_density(x₂, x₁) = pdf(Normal(x₁^2, 1), x₂)Compute true conditional densities:
true_densities = [true_banana_conditional_density(x₂, x₁) for x₂ in x₂_values]Plot 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:
x₁_values = [-0.6, 0.0, 1.0, 2.0]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.