Skip to content

Comments

feat: vcov and precision matrices + move float ops to f64#86

Merged
Neclow merged 12 commits intosbhattlab:mainfrom
Neclow:vcov
Jul 2, 2025
Merged

feat: vcov and precision matrices + move float ops to f64#86
Neclow merged 12 commits intosbhattlab:mainfrom
Neclow:vcov

Conversation

@Neclow
Copy link
Collaborator

@Neclow Neclow commented Jul 2, 2025

Features

Refactors

  • Move all float types in Rust to f64. This avoids many conversions in R and Python, where base R and numpy use float64 by default, and even Rust too https://github.com/rust-lang/rfcs/blob/master/text/0212-restore-int-fallback.md.
    • This was important to ensure precision of the new functions (precision and variance-covariance functions), where tests become imprecise in f32.
    • However, as mentioned in Make rust module accept f32/f64? #83, a more flexible option which accepts both remains desirable.
  • Python: Rename metrics to stats: metrics never made a lot of sense (cophenetic distances are not really a "metric" of a tree, so currently renamed to stats which will contain all sort of operations either between nodes (nodewise in the Python package) or between trees (treewise, upcoming...).
  • Update ndarray version in extendr to match the one used in the Rust API

Notes

  • Using scipy.linalg.solve(..., assume_a="pos") instead of np.linalg.solve in precision is 50% faster for n = 1000 leaves

@Neclow Neclow requested a review from Copilot July 2, 2025 14:42

This comment was marked as outdated.

@Neclow Neclow requested a review from Copilot July 2, 2025 14:51

This comment was marked as outdated.

Neclow and others added 2 commits July 2, 2025 16:55
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Neclow Neclow requested a review from Copilot July 2, 2025 14:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds variance–covariance (vcv) and precision matrix support to both R and Python bindings, consolidates all floating-point operations in Rust to f64, and renames the “metrics” module to “stats” throughout the codebase.

  • Introduce vcv_from_vector, vcv_from_matrix, and precision functions (with Schur complement) in R and Rust, plus corresponding Python cov and precision APIs.
  • Convert all Rust matrix and graph code from f32 to f64 to align with downstream R/Python and improve numerical precision.
  • Rename the R/Python “metrics” module to “stats”, update imports/tests, and remove obsolete test_metrics.R.

Reviewed Changes

Copilot reviewed 32 out of 33 changed files in this pull request and generated no comments.

Show a summary per file
File Description
r-phylo2vec/tests/testthat/test_stats.R Add R tests for vcv and precision functions
r-phylo2vec/src/rust/src/lib.rs Switch f32f64, implement new vcv_from_* and pre_precision_from_* exports
r-phylo2vec/R/stats.R Add R wrappers for vcv and precision (Schur complement)
py-phylo2vec/phylo2vec/stats/nodewise.py Rename metrics → stats, implement cov and precision APIs
py-phylo2vec/src/lib.rs Update Python bindings for f64 arrays and new functions
Comments suppressed due to low confidence (2)

py-phylo2vec/phylo2vec/stats/nodewise.py:83

  • The implementation references core.vcv and core.vcv_with_bls but core is never imported; add an import such as import phylo2vec._phylo2vec_core as core (or from phylo2vec import core) at the top of the file to avoid a NameError.
def cov(vector_or_matrix):

py-phylo2vec/phylo2vec/stats/nodewise.py:3

  • [nitpick] The warnings import is unused in this module; consider removing it to keep the namespace clean.
import warnings

@Neclow
Copy link
Collaborator Author

Neclow commented Jul 2, 2025

R: renamed "cov" to "vcovp" to avoid collisions with base/stats (cov/vcov) and ape (vcv)

@Neclow
Copy link
Collaborator Author

Neclow commented Jul 2, 2025

Note: the benchmarks are slightly worse now for matrix operations due to the f32 -> f64 conversion, although still within a reasonable margin in my opinion.

@Neclow Neclow merged commit f08fad4 into sbhattlab:main Jul 2, 2025
7 checks passed
@Neclow Neclow deleted the vcov branch July 2, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Precision and variance-covariance matrices

1 participant