graphld.precision¶
Precision matrix operations using LDGM graphical models.
The PrecisionOperator class is the core abstraction for working with LD matrices. It subclasses SciPy's LinearOperator and represents an LDGM precision matrix or its Schur complement.
Key Concepts¶
- To compute
correlation_matrix[indices, indices] @ vector, useldgm[indices].solve(vector) - To compute
inv(correlation_matrix[indices, indices]) @ vector, useldgm[indices] @ vector
You cannot do this indexing manually - the submatrix of an inverse is not the inverse of a submatrix. See Section 5 of the supplementary material.
For conceptual usage and examples, see the Matrix Operations guide.
precision
¶
Precision matrix operations for LDGM.
This module implements sparse precision matrix operations using scipy's LinearOperator interface for efficient matrix-vector operations.
PrecisionOperator
dataclass
¶
PrecisionOperator(_matrix: csc_matrix, variant_info: DataFrame, _which_indices: Optional[ndarray] = None, _solver: Optional[cholesky] = None, _cholesky_is_up_to_date: bool = False, _matrix_version: Optional[list[int]] = None, _factor_version: int = -1)
Bases: LinearOperator
LDGM precision matrix class implementing the LinearOperator interface.
This class provides an efficient implementation of precision matrix operations using the scipy.sparse.linalg.LinearOperator interface. It supports matrix-vector multiplication and other essential operations for working with LDGM precision matrices.
Attributes:
| Name | Type | Description |
|---|---|---|
_matrix |
csc_matrix
|
The precision matrix in sparse format |
variant_info |
DataFrame
|
Polars DataFrame containing variant information |
_which_indices |
Optional[ndarray]
|
Array of indices for current selection |
_solver |
Optional[cholesky]
|
Previously computed Cholesky factorization |
_cholesky_is_up_to_date |
bool
|
Flag indicating whether the Cholesky factorization is up to date |
diagonal_indices
cached
property
¶
Get indices of diagonal elements corresponding to _which_indices in _matrix.data.
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of indices into self._matrix.data where diagonal elements are stored |
times_scalar
¶
update_matrix
¶
Update the precision matrix by adding values to its diagonal.
If which_indices is set, only updates the corresponding diagonal elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
update
|
ndarray
|
Vector of values to add to the diagonal elements |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If update shape doesn't match or would make diagonal non-positive |
Source code in src/graphld/precision.py
update_element
¶
Update a single diagonal element of the precision matrix.
If which_indices is set, only updates the corresponding element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
The updated diagonal element is self._which_indices[index] |
required |
value
|
float
|
Value to add to the diagonal element |
required |
Note
Updates the Cholesky factorization if it exists using a rank-1 update/downdate.
Source code in src/graphld/precision.py
factor
¶
Update the Cholesky factorization of the precision matrix.
Source code in src/graphld/precision.py
del_factor
¶
logdet
¶
Compute log determinant of the Schur complement.
Returns:
| Type | Description |
|---|---|
float
|
Log determinant of the Schur complement |
Source code in src/graphld/precision.py
copy
¶
Copy the current LDGM instance.
Source code in src/graphld/precision.py
__getitem__
¶
Sets the _which_indices class attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Union[list, slice, ndarray]
|
Index, slice, or tuple of indices/slices to access. Can be: - List of indices - Array of indices - Boolean mask array - Slice object |
required |
Returns:
| Type | Description |
|---|---|
PrecisionOperator
|
New PrecisionOperator instance that shares the underlying matrix |
Source code in src/graphld/precision.py
set_which_indices
¶
Sets the _which_indices class attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Union[list, slice, ndarray, int]
|
Index, slice, or tuple of indices/slices to access. Can be: - List of indices - Array of indices - Boolean mask array - Slice object - Integer index |
required |
Source code in src/graphld/precision.py
solve
¶
solve(b: ndarray, method: str = 'direct', tol: float = 1e-05, callback: Optional[Callable] = None, initialization: Optional[ndarray] = None) -> np.ndarray
Solve the linear system Px = b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
ndarray
|
Right-hand side vector or matrix |
required |
method
|
str
|
Solver method ('direct' or 'pcg') |
'direct'
|
tol
|
float
|
Tolerance for PCG solver |
1e-05
|
callback
|
Optional[Callable]
|
Optional callback for PCG solver |
None
|
initialization
|
Optional[ndarray]
|
Optional initial guess for pcg |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Solution vector x |
Source code in src/graphld/precision.py
solve_Lt
¶
Solve the triangular system L'x = b, where LL' = _matrix. If b ~ MVN(0, I), then x ~ MVN(0, R).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
ndarray
|
Right-hand side vector |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Solution vector x |
Source code in src/graphld/precision.py
variant_solve
¶
Computes correlation_matrix @ b where the dimension is number of variants, not number of indices. If two variants i,j have the same index (and are in perfect LD), b[i] and b[j] are summed, then solve() is called, then the solution vector is assigned the same values in entries i,j.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
ndarray
|
Right-hand side vector |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Solution vector z |
Source code in src/graphld/precision.py
inverse_diagonal
¶
inverse_diagonal(method: str = 'xdiag', initialization: Optional[Tuple[ndarray, ndarray]] = None, n_samples: int = 100, seed: Optional[int] = None) -> np.ndarray
Compute the diagonal elements of the inverse of the precision matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initialization
|
Optional[Tuple[ndarray, ndarray]]
|
Optional tuple of matrices containing: - Initial guess for the diagonal elements - Initial guess for the off-diagonal elements |
None
|
method
|
str
|
Method to use for computing diagonal elements ('exact', 'hutchinson', or 'xdiag') |
'xdiag'
|
n_samples
|
int
|
Number of probe vectors for Hutchinson's method or xdiag |
100
|
seed
|
Optional[int]
|
Random seed for generating probe vectors |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of diagonal elements of the inverse |