rthor._vectorized
Vectorized operations for RTHOR algorithm.
This module contains high-performance vectorized implementations of core RTHOR operations, replacing nested loops from the original RTHORR implementation with NumPy broadcasting.
| FUNCTION | DESCRIPTION |
|---|---|
build_comparison_matrix |
Build comparison matrix from correlation vector. |
count_agreements |
Count agreements and ties between comparison and hypothesis matrices. |
build_hypothesis_matrix |
Build hypothesis matrix from order array. |
extract_upper_triangle_vector |
Extract upper triangle of matrix as vector (row-major order). |
count_pairwise_agreements |
Count agreement patterns between two matrices. |
calculate_comparison_ci |
Calculate Correspondence Index for pairwise comparison. |
build_comparison_matrix
Build comparison matrix from correlation vector.
Notes
Performance: O(1) broadcast vs O(n²) nested loops.
Original R code (randall.R:135-140) used nested loops.
| PARAMETER | DESCRIPTION |
|---|---|
correlations_vector
|
1D array of correlation values from upper triangle
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Matrix where:
|
Examples:
Source code in src/rthor/_vectorized.py
count_agreements
Count agreements and ties between comparison and hypothesis matrices.
Notes
Performance: O(1) boolean indexing vs O(n²) nested loops.
Original R code (randall.R:143-146) used nested loops.
| PARAMETER | DESCRIPTION |
|---|---|
comparison_matrix
|
Comparison matrix from build_comparison_matrix()
TYPE:
|
hypothesis_matrix
|
Hypothesis matrix (1 where prediction exists, 0 otherwise)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
n_agreements
|
Number of agreements (comparison==1 and hypothesis==1)
TYPE:
|
n_ties
|
Number of ties (comparison==2 and hypothesis==1)
TYPE:
|
Examples:
>>> comp = np.array([[0, 1, 0], [0, 0, 2], [1, 0, 0]])
>>> hyp = np.array([[0, 1, 1], [0, 0, 1], [0, 0, 0]])
>>> n_agr, n_tie = count_agreements(comp, hyp)
Source code in src/rthor/_vectorized.py
build_hypothesis_matrix
Build hypothesis matrix from order array.
Notes
Performance: O(1) broadcast vs O(n²) nested loops.
Original R code (randall.R:71-79) used nested loops.
| PARAMETER | DESCRIPTION |
|---|---|
order_array
|
1D array specifying hypothesized ordering
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
hypothesis_matrix
|
Matrix where hypothesis_matrix[i,j] = 1 if order_array[j] < order_array[i]
TYPE:
|
Examples:
Source code in src/rthor/_vectorized.py
extract_upper_triangle_vector
Extract upper triangle of matrix as vector (row-major order).
Notes
This matches R's row-major ordering for consistency with RTHORR implementation.
Extracts in the same order as R code (randall.R:126-131) for exact parity.
Uses row-major traversal: \((0,1), (0,2), ..., (0,n-1), (1,2), ..., (n-2,n-1)\)
| PARAMETER | DESCRIPTION |
|---|---|
matrix
|
2D square matrix (n x n)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
1D array of upper triangle values (length = n*(n-1)/2) |
Examples:
>>> mat = np.array([[1.0, 0.8, 0.6],
... [0.8, 1.0, 0.7],
... [0.6, 0.7, 1.0]])
>>> vec = extract_upper_triangle_vector(mat)
>>> vec
array([0.8, 0.6, 0.7])
Source code in src/rthor/_vectorized.py
count_pairwise_agreements
count_pairwise_agreements(comp_mat1: ndarray, comp_mat2: ndarray, hypothesis_matrix: ndarray) -> tuple[int, int, int, int]
Count agreement patterns between two matrices.
The logic checks for each hypothesized prediction (hypothesis_matrix==1):
- \(m1 = 1\) if
comp_mat1agrees (==1) - \(m2 = 1\) if
comp_mat2agrees (==1)
Then counts: \((m1=1,m2=1), (m1=1,m2=0), (m1=0,m2=1), (m1=0,m2=0)\)
Notes
Vectorized replacement for quadruple nested loops in randmf().
Performance: O(1) vs O(n⁴) nested loops.
Original R code (randmf.R:256-280) used 4 nested loops.
| PARAMETER | DESCRIPTION |
|---|---|
comp_mat1
|
Comparison matrix for first correlation matrix
TYPE:
|
comp_mat2
|
Comparison matrix for second correlation matrix
TYPE:
|
hypothesis_matrix
|
Hypothesis matrix
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
both_agree
|
Count where both matrices satisfy hypothesis
TYPE:
|
only1
|
Count where only matrix 1 satisfies hypothesis
TYPE:
|
only2
|
Count where only matrix 2 satisfies hypothesis
TYPE:
|
neither
|
Count where neither matrix satisfies hypothesis
TYPE:
|
Source code in src/rthor/_vectorized.py
calculate_comparison_ci
Calculate Correspondence Index for pairwise comparison.
Formula from R code (randmf.R:205):
| PARAMETER | DESCRIPTION |
|---|---|
only1
|
Count where only matrix 1 agrees
TYPE:
|
only2
|
Count where only matrix 2 agrees
TYPE:
|
both_agree
|
Count where both agree
TYPE:
|
neither
|
Count where neither agrees
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Comparison CI |
Source code in src/rthor/_vectorized.py
:::