{ "cells": [ { "cell_type": "markdown", "id": "8bbfdb92", "metadata": {}, "source": [ "# Moment Matrix Example" ] }, { "cell_type": "markdown", "id": "fe918261", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 1, "id": "df2d21a0", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2025-08-05 14:15:36,039 \t DEBUG \t CRISTAL.__init__. \t CRISTAL version 0.0.1 (date: 2025-08-01) loaded successfully.\n" ] } ], "source": [ "import numpy as np\n", "\n", "from cristal import IMPLEMENTED_INVERSION_OPTIONS, IMPLEMENTED_POLYNOMIAL_BASIS, DyCF, PolynomialsBasisGenerator" ] }, { "cell_type": "markdown", "id": "531d643d", "metadata": {}, "source": [ "## Parameters" ] }, { "cell_type": "code", "execution_count": 2, "id": "39bd8616", "metadata": {}, "outputs": [], "source": [ "d = 8 # Data dimension\n", "n = 5 # Degree of the polynomial basis\n", "N = 1000 # Number of samples" ] }, { "cell_type": "markdown", "id": "deb7a911", "metadata": {}, "source": [ "## Data" ] }, { "cell_type": "code", "execution_count": 3, "id": "cd7dedd5", "metadata": {}, "outputs": [], "source": [ "np.random.seed(42)\n", "data = np.random.random((N, d))" ] }, { "cell_type": "markdown", "id": "8730a0cb", "metadata": {}, "source": [ "## Method 1: Using DyCF" ] }, { "cell_type": "markdown", "id": "86eb82ae", "metadata": {}, "source": [ "### Create a DyCF instance" ] }, { "cell_type": "code", "execution_count": 4, "id": "460f3b31", "metadata": {}, "outputs": [], "source": [ "dycf = DyCF(n, inv_opt=IMPLEMENTED_INVERSION_OPTIONS.PINV)" ] }, { "cell_type": "markdown", "id": "9afe087a", "metadata": {}, "source": [ "### Fit the DyCF model to the data to compute the moments matrix" ] }, { "cell_type": "code", "execution_count": 5, "id": "ca13b241", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dycf.fit(data)" ] }, { "cell_type": "markdown", "id": "f491c3e6", "metadata": {}, "source": [ "### Access the moments matrix and its inverse" ] }, { "cell_type": "code", "execution_count": 6, "id": "63d97675", "metadata": {}, "outputs": [], "source": [ "moments_matrix = dycf.moments_matrix.moments_matrix\n", "inversed_moments_matrix = dycf.moments_matrix.inverse_moments_matrix" ] }, { "cell_type": "markdown", "id": "71934885", "metadata": {}, "source": [ "## Method 2: By computing the design matrix X" ] }, { "cell_type": "markdown", "id": "1b5530dd", "metadata": {}, "source": [ "### Generate the monomials combinations" ] }, { "cell_type": "code", "execution_count": 7, "id": "d4727469", "metadata": {}, "outputs": [], "source": [ "monomials = np.asarray(PolynomialsBasisGenerator.generate_combinations(n, d), dtype=np.int8)" ] }, { "cell_type": "markdown", "id": "d6ebf8f6", "metadata": {}, "source": [ "### Compute the vectors design matrix X" ] }, { "cell_type": "code", "execution_count": 8, "id": "2da49624", "metadata": {}, "outputs": [], "source": [ "X = PolynomialsBasisGenerator.make_design_matrix(data, monomials, IMPLEMENTED_POLYNOMIAL_BASIS.MONOMIALS.value)" ] }, { "cell_type": "markdown", "id": "17c456eb", "metadata": {}, "source": [ "### Construct the moments matrix M and its inverse M_inv" ] }, { "cell_type": "code", "execution_count": 9, "id": "53741232", "metadata": {}, "outputs": [], "source": [ "M = np.dot(X.T, X) / N\n", "M_inv = IMPLEMENTED_INVERSION_OPTIONS.PINV.value.invert(M)" ] }, { "cell_type": "markdown", "id": "e8eb257b", "metadata": {}, "source": [ "## Equality check" ] }, { "cell_type": "code", "execution_count": 10, "id": "2452ea93", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, True)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.allclose(moments_matrix, M, atol=1e-16), np.allclose(inversed_moments_matrix, M_inv, atol=1e-16)" ] } ], "metadata": { "kernelspec": { "display_name": "cristal", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }