Skip to content

ConditionalGaussianMixture API

ConditionalGaussianMixture is the result of GaussianMixture.condition(x_obs, obs_dims): the distribution of the unobserved coordinates given the observed ones, one Gaussian mixture per conditioning row. Its mean() is Gaussian mixture regression. GaussianMixture.marginal(dims) (and StudentTMixture.marginal) return the marginal mixture over a subset of the coordinates. See the design note for the equations and conventions.

ConditionalGaussianMixture

The distribution of the unobserved coordinates of a fitted GaussianMixture given observed ones, one Gaussian mixture per row of x_obs. Created by GaussianMixture.condition; not meant to be constructed directly.

Attributes:

Name Type Description
weights_ (Tensor, shape(n, K))

Conditional component weights :math:w_{ik} (rows sum to 1).

means_ (Tensor, shape(n, K, d_m))

Conditional means :math:m_{ik}.

covariances_ (Tensor, shape(K, d_m, d_m))

Conditional covariances :math:C_k, shared by all samples.

obs_dims list of int

Observed coordinates, in the order of the columns of x_obs.

missing_dims list of int

The sorted complement of obs_dims; the coordinates the distribution is over, in increasing order.

n_components int
n_features int

d_m, the number of unobserved coordinates.

device, dtype
Source code in tgmm/mixtures/conditional.py
class ConditionalGaussianMixture:
    r"""
    The distribution of the unobserved coordinates of a fitted
    `GaussianMixture` given observed ones, one Gaussian mixture per row of
    ``x_obs``. Created by `GaussianMixture.condition`; not meant to be
    constructed directly.

    Attributes
    ----------
    weights_ : torch.Tensor, shape (n, K)
        Conditional component weights :math:`w_{ik}` (rows sum to 1).
    means_ : torch.Tensor, shape (n, K, d_m)
        Conditional means :math:`m_{ik}`.
    covariances_ : torch.Tensor, shape (K, d_m, d_m)
        Conditional covariances :math:`C_k`, shared by all samples.
    obs_dims : list of int
        Observed coordinates, in the order of the columns of ``x_obs``.
    missing_dims : list of int
        The sorted complement of ``obs_dims``; the coordinates the
        distribution is over, in increasing order.
    n_components : int
    n_features : int
        ``d_m``, the number of unobserved coordinates.
    device, dtype
    """

    def __init__(self, weights, means, covariances, obs_dims, missing_dims):
        self.weights_ = weights
        self.means_ = means
        self.covariances_ = covariances
        self.obs_dims = list(obs_dims)
        self.missing_dims = list(missing_dims)
        self.n_components = weights.shape[1]
        self.n_features = means.shape[2]
        self.device = weights.device
        self.dtype = weights.dtype
        self._chol = None

    @property
    def n_samples(self) -> int:
        r"""Number of conditioning rows."""
        return self.weights_.shape[0]

    def _cholesky(self) -> torch.Tensor:
        if self._chol is None:
            self._chol = torch.linalg.cholesky(self.covariances_)
        return self._chol

    def mean(self) -> torch.Tensor:
        r"""
        Gaussian mixture regression: :math:`E[y\mid x_{o,i}]=\sum_kw_{ik}m_{ik}`.

        Returns
        -------
        torch.Tensor, shape (n, d_m)
        """
        return (self.weights_.unsqueeze(-1) * self.means_).sum(dim=1)

    def log_prob(self, Y: torch.Tensor) -> torch.Tensor:
        r"""
        :math:`\log p(y_i\mid x_{o,i})` for one query point per conditioning row.

        Parameters
        ----------
        Y : torch.Tensor, shape (n, d_m)
            Row ``i`` is evaluated under the mixture of row ``i``. A conditional
            with a single row (``n == 1``) is broadcast against any number of
            query points ``(m, d_m)``, which is how a single conditional
            density is evaluated on a grid.

        Returns
        -------
        torch.Tensor, shape (n,) (or (m,) in the single-row case)
        """
        Y = torch.as_tensor(Y, dtype=self.dtype, device=self.device)
        if Y.dim() != 2 or Y.shape[1] != self.n_features:
            raise ValueError(f"Y must have shape (n, {self.n_features}), got {tuple(Y.shape)}")
        if Y.shape[0] != self.n_samples and self.n_samples != 1:
            raise ValueError(f"Y has {Y.shape[0]} rows but the conditional has {self.n_samples}")
        L = self._cholesky()                                                    # (K, dm, dm)
        eye = torch.eye(self.n_features, dtype=self.dtype, device=self.device)
        L_inv = torch.linalg.solve_triangular(L, eye.expand_as(L), upper=False)  # (K, dm, dm)
        diff = Y.unsqueeze(1) - self.means_                                      # (n|m, K, dm)
        z = torch.einsum('kde,nke->nkd', L_inv, diff)
        maha = z.pow(2).sum(-1)                                                  # (n|m, K)
        log_det = 2.0 * torch.log(torch.diagonal(L, dim1=-2, dim2=-1)).sum(-1)   # (K,)
        log_n = -0.5 * (self.n_features * math.log(2.0 * math.pi) + log_det.unsqueeze(0) + maha)
        return torch.logsumexp(torch.log(self.weights_.clamp_min(1e-300)) + log_n, dim=1)

    def sample(self, n_draws: int = 1) -> torch.Tensor:
        r"""
        Draw from each row's conditional mixture.

        Parameters
        ----------
        n_draws : int, default=1
            Number of independent draws per conditioning row.

        Returns
        -------
        torch.Tensor, shape (n, n_draws, d_m)
        """
        if n_draws < 1:
            raise ValueError(f"n_draws must be >= 1, got {n_draws}")
        n = self.n_samples
        comp = torch.multinomial(self.weights_, n_draws, replacement=True)       # (n, n_draws)
        rows = torch.arange(n, device=self.device).unsqueeze(1).expand(n, n_draws)
        out = self.means_[rows, comp].clone()                                    # (n, n_draws, dm)
        eps = torch.randn(n, n_draws, self.n_features, dtype=self.dtype, device=self.device)
        L = self._cholesky()
        for k in range(self.n_components):                                       # loop over components, not samples
            sel = comp == k
            if sel.any():
                out[sel] += eps[sel] @ L[k].T
        return out

    def as_gmm(self, i: int):
        r"""
        The conditional distribution of row ``i`` as a fitted
        `GaussianMixture` over the missing coordinates (full covariance).
        """
        from ..gmm import GaussianMixture

        if not -self.n_samples <= i < self.n_samples:
            raise IndexError(f"row {i} out of range for {self.n_samples} conditioning rows")
        gmm = GaussianMixture(
            n_components=self.n_components, n_features=self.n_features, covariance_type='full',
            device=self.device,
        )
        gmm.dtype = self.dtype
        gmm.weights_ = self.weights_[i].clone()
        gmm.means_ = self.means_[i].clone()
        gmm.covariances_ = self.covariances_.clone()
        gmm.fitted_ = True
        gmm.converged_ = True
        gmm.n_components_ = self.n_components
        gmm.n_iter_ = 0
        gmm.lower_bound_ = float('nan')
        return gmm

    def __repr__(self) -> str:
        return (f"ConditionalGaussianMixture(n_samples={self.n_samples}, n_components={self.n_components}, "
                f"obs_dims={self.obs_dims}, missing_dims={self.missing_dims})")

Attributes

n_samples property

Number of conditioning rows.

Methods:

mean()

Gaussian mixture regression: :math:E[y\mid x_{o,i}]=\sum_kw_{ik}m_{ik}.

Returns:

Type Description
(Tensor, shape(n, d_m))
Source code in tgmm/mixtures/conditional.py
def mean(self) -> torch.Tensor:
    r"""
    Gaussian mixture regression: :math:`E[y\mid x_{o,i}]=\sum_kw_{ik}m_{ik}`.

    Returns
    -------
    torch.Tensor, shape (n, d_m)
    """
    return (self.weights_.unsqueeze(-1) * self.means_).sum(dim=1)

log_prob(Y)

:math:\log p(y_i\mid x_{o,i}) for one query point per conditioning row.

Parameters:

Name Type Description Default
Y (Tensor, shape(n, d_m))

Row i is evaluated under the mixture of row i. A conditional with a single row (n == 1) is broadcast against any number of query points (m, d_m), which is how a single conditional density is evaluated on a grid.

required

Returns:

Type Description
torch.Tensor, shape (n,) (or (m,) in the single-row case)
Source code in tgmm/mixtures/conditional.py
def log_prob(self, Y: torch.Tensor) -> torch.Tensor:
    r"""
    :math:`\log p(y_i\mid x_{o,i})` for one query point per conditioning row.

    Parameters
    ----------
    Y : torch.Tensor, shape (n, d_m)
        Row ``i`` is evaluated under the mixture of row ``i``. A conditional
        with a single row (``n == 1``) is broadcast against any number of
        query points ``(m, d_m)``, which is how a single conditional
        density is evaluated on a grid.

    Returns
    -------
    torch.Tensor, shape (n,) (or (m,) in the single-row case)
    """
    Y = torch.as_tensor(Y, dtype=self.dtype, device=self.device)
    if Y.dim() != 2 or Y.shape[1] != self.n_features:
        raise ValueError(f"Y must have shape (n, {self.n_features}), got {tuple(Y.shape)}")
    if Y.shape[0] != self.n_samples and self.n_samples != 1:
        raise ValueError(f"Y has {Y.shape[0]} rows but the conditional has {self.n_samples}")
    L = self._cholesky()                                                    # (K, dm, dm)
    eye = torch.eye(self.n_features, dtype=self.dtype, device=self.device)
    L_inv = torch.linalg.solve_triangular(L, eye.expand_as(L), upper=False)  # (K, dm, dm)
    diff = Y.unsqueeze(1) - self.means_                                      # (n|m, K, dm)
    z = torch.einsum('kde,nke->nkd', L_inv, diff)
    maha = z.pow(2).sum(-1)                                                  # (n|m, K)
    log_det = 2.0 * torch.log(torch.diagonal(L, dim1=-2, dim2=-1)).sum(-1)   # (K,)
    log_n = -0.5 * (self.n_features * math.log(2.0 * math.pi) + log_det.unsqueeze(0) + maha)
    return torch.logsumexp(torch.log(self.weights_.clamp_min(1e-300)) + log_n, dim=1)

sample(n_draws=1)

Draw from each row's conditional mixture.

Parameters:

Name Type Description Default
n_draws int

Number of independent draws per conditioning row.

1

Returns:

Type Description
(Tensor, shape(n, n_draws, d_m))
Source code in tgmm/mixtures/conditional.py
def sample(self, n_draws: int = 1) -> torch.Tensor:
    r"""
    Draw from each row's conditional mixture.

    Parameters
    ----------
    n_draws : int, default=1
        Number of independent draws per conditioning row.

    Returns
    -------
    torch.Tensor, shape (n, n_draws, d_m)
    """
    if n_draws < 1:
        raise ValueError(f"n_draws must be >= 1, got {n_draws}")
    n = self.n_samples
    comp = torch.multinomial(self.weights_, n_draws, replacement=True)       # (n, n_draws)
    rows = torch.arange(n, device=self.device).unsqueeze(1).expand(n, n_draws)
    out = self.means_[rows, comp].clone()                                    # (n, n_draws, dm)
    eps = torch.randn(n, n_draws, self.n_features, dtype=self.dtype, device=self.device)
    L = self._cholesky()
    for k in range(self.n_components):                                       # loop over components, not samples
        sel = comp == k
        if sel.any():
            out[sel] += eps[sel] @ L[k].T
    return out

as_gmm(i)

The conditional distribution of row i as a fitted GaussianMixture over the missing coordinates (full covariance).

Source code in tgmm/mixtures/conditional.py
def as_gmm(self, i: int):
    r"""
    The conditional distribution of row ``i`` as a fitted
    `GaussianMixture` over the missing coordinates (full covariance).
    """
    from ..gmm import GaussianMixture

    if not -self.n_samples <= i < self.n_samples:
        raise IndexError(f"row {i} out of range for {self.n_samples} conditioning rows")
    gmm = GaussianMixture(
        n_components=self.n_components, n_features=self.n_features, covariance_type='full',
        device=self.device,
    )
    gmm.dtype = self.dtype
    gmm.weights_ = self.weights_[i].clone()
    gmm.means_ = self.means_[i].clone()
    gmm.covariances_ = self.covariances_.clone()
    gmm.fitted_ = True
    gmm.converged_ = True
    gmm.n_components_ = self.n_components
    gmm.n_iter_ = 0
    gmm.lower_bound_ = float('nan')
    return gmm