Skip to main content

functional_covariance

Function functional_covariance 

Source
pub fn functional_covariance(data: &FdMatrix) -> Result<FdMatrix, FdarError>
Expand description

Compute the M×M sample covariance matrix of functional data (Bessel-corrected, ddof = n-1).

For each pair of evaluation points (j1, j2), computes the sample covariance across the n curves: cov[j1, j2] = sum_i (data[(i,j1)] - mean[j1]) * (data[(i,j2)] - mean[j2]) / (n - 1)

The diagonal equals functional_variance(data) pointwise. The result is a symmetric M×M FdMatrix stored in column-major order.

This is an O(n·m²) operation — may be expensive for large m.

§Arguments

  • data - Functional data matrix (n x m), requires n >= 2.

§Returns

M×M sample covariance FdMatrix.

§Errors

Returns FdarError::InvalidDimension if n < 2, or FdarError::InvalidParameter if m * m overflows usize.

§Examples

use fdars_core::matrix::FdMatrix;
use fdars_core::fdata::{functional_covariance, functional_variance};

let data = FdMatrix::from_column_major(vec![1.0, 3.0, 4.0, 2.0], 2, 2).unwrap();
let cov = functional_covariance(&data).unwrap();
assert_eq!(cov.shape(), (2, 2));
let var = functional_variance(&data).unwrap();
// Diagonal matches variance
assert!((cov[(0, 0)] - var[0]).abs() < 1e-10);
assert!((cov[(1, 1)] - var[1]).abs() < 1e-10);