Calculating Dataframe Covariance in Rust Polars

41 views Asked by At

I am trying to calculate the covariance matrix of an NxM Polars DataFrame. The docs seem to suggest there is covariance functionality as a crate feature, but I cant find any implementation for it.

I looked at other implementations including ndarray_stats::CorrelationExt or even RustQuant but seems to be just across 2 vectors.

fn covariance_matrix(matrix: &DataFrame) -> Result<ndarray::Array2<f64>, PolarsError> {
    // Convert DataFrame to ndarray
    let matrix_arr = matrix.to_ndarray::<Float64Type>(IndexOrder::C)?;
    let covariance = matrix_arr.cov(0.).unwrap();
    return Ok(covariance);
}

I was playing with this by converting to an ndarray, but I need it ndimensional not 2D. Maybe you can do this with generics, but I cant figure it out.

I would also like to do it without converting to an ndarray if possible.

0

There are 0 answers