Moment Matrix Example#

Imports#

import numpy as np

from cristal import IMPLEMENTED_INVERSION_OPTIONS, IMPLEMENTED_POLYNOMIAL_BASIS, DyCF, PolynomialsBasisGenerator

Parameters#

d = 8  # Data dimension
n = 5  # Degree of the polynomial basis
N = 1000  # Number of samples

Data#

np.random.seed(42)
data = np.random.random((N, d))

Method 1: Using DyCF#

Create a DyCF instance#

dycf = DyCF(n, inv_opt=IMPLEMENTED_INVERSION_OPTIONS.PINV)

Fit the DyCF model to the data to compute the moments matrix#

dycf.fit(data)
<cristal.christoffel.DyCF at 0x7f85ff117cb0>

Access the moments matrix and its inverse#

moments_matrix = dycf.moments_matrix.moments_matrix
inversed_moments_matrix = dycf.moments_matrix.inverse_moments_matrix

Method 2: By computing the design matrix X#

Generate the monomials combinations#

monomials = np.asarray(PolynomialsBasisGenerator.generate_combinations(n, d), dtype=np.int8)

Compute the vectors design matrix X#

X = PolynomialsBasisGenerator.make_design_matrix(data, monomials, IMPLEMENTED_POLYNOMIAL_BASIS.MONOMIALS.value)

Construct the moments matrix M and its inverse M_inv#

M = np.dot(X.T, X) / N
M_inv = IMPLEMENTED_INVERSION_OPTIONS.PINV.value.invert(M)

Equality check#

np.allclose(moments_matrix, M, atol=1e-16), np.allclose(inversed_moments_matrix, M_inv, atol=1e-16)
(True, True)