liberata_metrics.utils.utils module

liberata_metrics.utils.utils.random_date(rng: RandomState, start: date, end: date) date[source]

draw a random date in given range

liberata_metrics.utils.utils.sparse_divide(divisor: spmatrix, dividend: spmatrix) spmatrix[source]

Element-wise division of two sparse matrices with zero-division handling. Performs element-wise division by computing the multiplicative inverse of the divisor and multiplying with the dividend. Division by zero is implicitly handled by sparse matrix structure - zero elements in the divisor remain zero in the result. :param divisor: Sparse matrix to divide by. Must have non-zero elements at positions

where division is desired.

Parameters:

dividend (sparse.spmatrix) – Sparse matrix to be divided. Must have compatible shape with divisor.

Returns:

Sparse matrix containing the element-wise division result (dividend / divisor). The result maintains the sparsity pattern of the dividend.

Return type:

sparse.spmatrix

Notes

  • Zero elements in the divisor are treated as undefined divisions and do not

    appear in the output due to sparse matrix structure.

  • Both input matrices must have compatible shapes for element-wise operations.

  • The result is returned as a sparse CSR array format.

Examples

>>> from scipy import sparse
>>> divisor = sparse.csr_matrix([[2, 0], [0, 4]])
>>> dividend = sparse.csr_matrix([[4, 0], [0, 8]])
>>> result = sparse_divide(divisor, dividend)
>>> result.toarray()
array([[2., 0.],
        [0., 2.]])