projnormal.quadratic_forms.sampling

Sampling of quadratic forms of multivariate Gaussian random variables.

Functions

empirical_covariance(mean_x, covariance_x, ...)

Compute an empirical approximation to the covariance between two quadratic forms X'MX and X'MX, where X~N(mean_x, covariance_x).

empirical_moments(mean_x, covariance_x, M, ...)

Compute an empirical approximation to the moments of the quadratic form \(x^T M x\), where \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

sample(mean_x, covariance_x, M, n_samples)

Sample from the quadratic form \(x^T M x\), where \(x\) follows a multivariate normal distribution \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

empirical_covariance(mean_x, covariance_x, M1, M2, n_samples)

Compute an empirical approximation to the covariance between two quadratic forms X’MX and X’MX, where X~N(mean_x, covariance_x).

Parameters:
  • mean_x (torch.Tensor) – Mean of x. Shape (n_dim).

  • covariance_x (torch.Tensor) – Covariance of x. Shape (n_dim, n_dim) or scalar (isotropic covariance).

  • M1 (torch.Tensor, optional) – Matrix in quadratic form. If a vector is provided, it is used as the diagonal of M. Default is the identity matrix. Shape (n_dim, n_dim).

  • M2 (torch.Tensor, optional) – Matrix in quadratic form. If a vector is provided, it is used as the diagonal of M. Default is the identity matrix. Shape (n_dim, n_dim).

  • n_samples (int) – Number of samples to generate use.

Returns:

Covariance between the two quadratic forms. Shape is (1,).

Return type:

torch.Tensor

empirical_moments(mean_x, covariance_x, M, n_samples)

Compute an empirical approximation to the moments of the quadratic form \(x^T M x\), where \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

Parameters:
  • mean_x (torch.Tensor) – Mean of x. Shape (n_dim).

  • covariance_x (torch.Tensor) – Covariance of x. Shape (n_dim, n_dim) or scalar (isotropic covariance).

  • M (torch.Tensor, optional) – Matrix in quadratic form. If a vector is provided, it is used as the diagonal of M. Default is the identity matrix. Shape (n_dim, n_dim) or (n_dim,).

  • n_samples (int) – Number of samples to use.

Returns:

Dictionary with fields mean, var, and second_moment.

Return type:

dict

sample(mean_x, covariance_x, M, n_samples)

Sample from the quadratic form \(x^T M x\), where \(x\) follows a multivariate normal distribution \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

Parameters:
  • mean_x (torch.Tensor) – Mean of x. Shape (n_dim).

  • covariance_x (torch.Tensor) – Covariance of x. Shape (n_dim, n_dim) or scalar (isotropic covariance).

  • M (torch.Tensor, optional) – Matrix in quadratic form. If a vector is provided, it is used as the diagonal of M. Default is the identity matrix. Shape (n_dim, n_dim) or (n_dim,).

  • n_samples (int) – Number of samples to generate.

Returns:

Samples from the quadratic form. Shape is (n_samples, n_dim).

Return type:

torch.Tensor