Calculating determinants via Cholesky decomposition in PyTorch

614 Views Asked by At

I've been trying to calculate the determinant of a 2x2 matrix via Cholesky decomposition in PyTorch and it won't give the same number as Numpy and I'm not sure why. From my understanding, you can calculate the determinant of a square positive-definite matrix via decomposing it into a lower triangular matrix and its transpose, i.e. M = LL^T.

Then by the law of determinants, the determinant of M is equal to the determinant of L multiplied by the determinant of L^T. Which, in the case of lower triangular matrices, is just the product of the diagonal. So, M would be equal to product of the diagonal of L multiplied by the product of the diagonal of L^T.

However, when I implement this in PyTorch, I get the wrong value. I've copied an example code below.

import torch
import numpy as np

matrix = torch.Tensor(2,2).uniform_()
print("Matrix: \n", matrix.detach().numpy(), "\n")

print("Positive-definite?: ", np.all(np.linalg.eigvals(matrix.detach().numpy()) > 0))
det_np = np.linalg.det(matrix.detach().numpy())

det_tor = torch.cholesky(matrix, upper=False).diag().prod()**2

print("determinant (numpy) %8.4f" % (det_np))
print("determinant (torch) %8.4f" % (det_tor))

An example output would be something like this,

Matrix: 
 [[0.5305128  0.2795679 ]
 [0.41778737 0.40350497]] 

Positive-definite?:  True
determinant (numpy)   0.0973
determinant (torch)   0.0395

What is it that is wrong? Why is there a difference between these two methods?

1

There are 1 best solutions below

0
On

As of version 1.8, PyTorch has native support for numpy-style torch.linalg operations, including both Cholesky decompositions and determinants:

torch.linalg.det(input)

Computes the determinant of a square matrix input, or of each square matrix in a batched input.