dist_from_cov: Squared-distance matrix from a covariance matrix

View source: R/genetic_covariance.R

dist_from_covR Documentation

Squared-distance matrix from a covariance matrix

Description

Converts a covariance matrix to its associated squared pairwise distance matrix. This inverts the Gower double-centering used by cov_from_genetic_data.

Usage

dist_from_cov(Cov)

Arguments

Cov

A square numeric covariance matrix. Does not need to be full-rank (e.g. the output of cov_from_biallelic is positive semi-definite).

Details

The squared distance between units i and j is:

D_{ij} = C_{ii} + C_{jj} - 2 C_{ij}

which is the law-of-cosines identity relating a covariance matrix to its implied squared Euclidean distances. The diagonal of the returned matrix is zero.

Use this function to convert a covariance matrix produced by cov_from_biallelic or cov_from_genetic_data into a distance matrix suitable for generalized_wishart or mlpe.

Value

A symmetric numeric matrix of the same dimension as Cov with zero diagonal and non-negative off-diagonal entries.

See Also

cov_from_biallelic, cov_from_genetic_data, dist_from_biallelic

Examples

Cov <- matrix(c(2.0, 1.2, 0.8,
                1.2, 1.5, 0.7,
                0.8, 0.7, 1.1), nrow = 3, byrow = TRUE)
dist_from_cov(Cov)

# Round-trip: covariance -> distance -> (verify non-negative, zero diag)
Y <- matrix(c(2, 1, 0, 1, 1, 1, 0, 1, 2), nrow = 3, byrow = TRUE)
S_cov  <- cov_from_biallelic(Y)
S_dist <- dist_from_cov(S_cov)
diag(S_dist)   # all zeros


landgraph documentation built on Sept. 26, 2026, 1:08 a.m.