projnormal.quadratic_forms.moments

Moments of quadratic forms of multidimensional Gaussian distributions.

Functions

mean(mean_x, covariance_x[, M])

Compute the mean of \(x^T M x\), where \(x\) follows a multivariate normal distribution \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

qf_covariance(mean_x, covariance_x, M, M2)

Compute the covariance between \(x^T M x\) and \(x^T M_2 x\), where \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

qf_linear_covariance(mean_x, covariance_x, M, b)

Compute the covariance between \(x^T M x\) and the linear form \(x^T b\), where \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

variance(mean_x, covariance_x[, M])

Compute the variance of \(x^T M x\), where \(x\) follows a multivariate normal distribution \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

mean(mean_x: Tensor, covariance_x: Tensor, M: Tensor | None = None) Tensor

Compute the mean of \(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,).

Returns:

Expected value of the quadratic form. Shape ().

Return type:

torch.Tensor

qf_covariance(mean_x, covariance_x, M, M2)

Compute the covariance between \(x^T M x\) and \(x^T M_2 x\), where \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

Parameters:
  • mean_x (torch.Tensor) – Mean of x. A vector of shape (n_dim,).

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

  • M (torch.Tensor) – Matrix of the first quadratic form. Shape (n_dim, n_dim)

  • M2 (torch.Tensor) – Matrix of the second quadratic form. Shape (n_dim, n_dim)

Returns:

Covariance of the two quadratic forms. Shape ().

Return type:

torch.Tensor

qf_linear_covariance(mean_x, covariance_x, M, b)

Compute the covariance between \(x^T M x\) and the linear form \(x^T b\), where \(x \sim \mathcal{N}(\mu_x, \Sigma_x)\).

Parameters:
  • mean_x (torch.Tensor) – Mean of x. A vector of shape (n_dim,).

  • covariance_x (torch.Tensor) – Covariance of x. Shape (n_dim, n_dim).

  • M (torch.Tensor) – Matrix for the quadratic form. Shape (n_dim, n_dim).

  • b (torch.Tensor) – Vector for the linear form. Shape (n_dim,).

Returns:

Covariance between the quadratic and linear forms.

Return type:

torch.Tensor

variance(mean_x, covariance_x, M=None)

Compute the variance of \(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).

  • M (torch.Tensor) – Matrix in the 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,).

Returns:

Variance of the quadratic form. Shape ().

Return type:

torch.Tensor