Skip to content

scipy/log-of-scipy-det

Pack Severity Fix Versions
SciPy warning unsafe, with xpd check --fix --unsafe-fixes All versions

linalg.det overflows or underflows for a large matrix, so its log is inf or -inf. Use np.linalg.slogdet(a)[1].

logdet = np.log(linalg.det(cov))
logdet = np.linalg.slogdet(cov)[1]

The rule reports np.log(linalg.det(a)), np.log(np.abs(linalg.det(a))) and np.log(abs(linalg.det(a))), where linalg is scipy.linalg or det was imported from it. On two covariance matrices the old form gave -inf and inf, where slogdet lost less than one digit, as the table shows. np.log(np.linalg.det(a)) belongs to the NumPy rule log-of-det.

Covariance matrix np.log(linalg.det(a)) Digits that slogdet lost
120 features at scale 1e-3 -inf 0.54
80 features at scale 1e4 inf 0.27

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix writes np.linalg.slogdet(a)[1].

For a negative determinant the replacement gives the log of its absolute value, and for a complex determinant it gives the log of its modulus. It can also return a value for nonfinite input that made SciPy's det raise.

$D($A) overflows to inf or underflows to 0 for a large matrix, so its log is inf or -inf. On covariance matrices of 80 and 120 features that lost all 16 digits where slogdet lost less than 1. Write $NP.linalg.slogdet($A)[1].

See all rules in the SciPy pack.