def _rebuild_covariance_matrix(self, covariance): """Rebuild the covariance matrix from its parameter values. This method follows the steps: * Rebuild a square matrix out of a triangular one. * Add the missing half of the matrix by adding its transposed and then removing the duplicated diagonal values. * ensure the matrix is positive definite Args: covariance (list): covariance values after unflattening model parameters. Result: list[list[float]]: Symmetric positive semi-definite matrix. """ covariance = np.array(square_matrix(covariance)) covariance = (covariance + covariance.T - (np.identity(covariance.shape[0]) * covariance)) if not check_matrix_symmetric_positive_definite(covariance): covariance = make_positive_definite(covariance) return covariance.tolist()
def test_check_matrix_symmetric_positive_definite(): """Test check matrix numpy.""" # Run matrix = np.array([[0.5, 0.5], [0.5, 0.5]]) result = check_matrix_symmetric_positive_definite(matrix) # Asserts assert result
def test_check_matrix_symmetric_positive_definite_np_error(): """Test check matrix numpy raise error.""" # Run matrix = np.array([[-1, 0], [0, 0]]) result = check_matrix_symmetric_positive_definite(matrix) # Asserts assert not result
def test_check_matrix_symmetric_positive_definite_shape_error(): """Test check matrix shape error.""" # Run matrix = np.array([]) result = check_matrix_symmetric_positive_definite(matrix) # Asserts assert not result
def _prepare_sampled_covariance(self, covariance): """Prepare a covariance matrix. Args: covariance (list): covariance after unflattening model parameters. Result: list[list]: symmetric Positive semi-definite matrix. """ covariance = np.array(square_matrix(covariance)) covariance = (covariance + covariance.T - (np.identity(covariance.shape[0]) * covariance)) if not check_matrix_symmetric_positive_definite(covariance): covariance = make_positive_definite(covariance) return covariance.tolist()