glearn.LinearModel.generate_design_matrix#

LinearModel.generate_design_matrix(x, orthonormalize=False)#

Generates design matrix on test points.

Note

The design matrix on data points is automatically generated during the internal training process and can be accessed by X attribute. Use this function to generate the design matrix on arbitrary test points.

Parameters:
xnumpy.ndarray

A 2D array of data points where each row of the array is the coordinate of a point \(\boldsymbol{x}=(x_1, \dots, x_d)\). The array size is \(n \times d\) where \(n\) is the number of the points and \(d\) is the dimension of the space of points.

orthonormalizebool, default=False

If True, the design matrix \(\mathbf{X}\) of the basis functions will be orthonormalized.

Returns:
Xnumpy.ndarray[float]

Design matrix of the size \(n \times m\) where \(n\) is the number of points and \(m\) is the number of basis functions.

Notes

This function generates the design matrix \(\mathbf{X}^{\ast}\) on a set of test points \(\{ \boldsymbol{x}^{\ast}_i \}_{i=1}^n\) with the components \(X_{ij}^{\ast}\) defined as

\[X_{ij}^{\ast} = \phi_j(\boldsymbol{x}_i).\]

The size of the matrix is \(n \times m\) where \(n\) is the number of test points and \(m\) is the total number of basis functions, which can be obtained by

\[m = m_p + 2m_t + 2m_h + m_f,\]

where

  • \(m_p\) is the number of polynomial basis functions.

  • \(m_t\) is the number of coefficients of the trigonometric functions.

  • \(m_h\) is the number of coefficients of the hyperbolic functions.

  • \(m_h\) is the number of user-defined basis functions.

Orthonormalization:

If orthonormalize is set to True, the output matrix \(\mathbf{X}^{\ast}\) is orthonormalized so that

\[(\mathbf{X}^{\ast})^{\intercal} \mathbf{X}^{\ast} = \mathbf{I},\]

where \(\mathbf{I}\) is the \(m \times m\) identity matrix.

This function uses detkit.orthogonalize() function to orthonormalize the matrix \(\mathbf{X}\).

Examples

First, create a set of 50 random points in the interval \([0, 1]\).

>>> # Generate a set of points
>>> from glearn.sample_data import generate_points
>>> x = generate_points(num_points=30, dimension=1)

Define the function \(\boldsymbol{\phi}\) as follows:

>>> # Create a function returning the Legendre Polynomials
>>> import numpy
>>> def func(x):
...     phi_1 = 0.5 * (3.0*x**2 - 1)
...     phi_2 = 0.5 * (5.0*x**3 - 3.0*x)
...     phi = numpy.array([phi_1, phi_2])
...     return phi

Create Linear Model:

Create a linear model with first order polynomial basis functions, both trigonometric and hyperbolic functions, and the user-defined functions created in the above.

>>> # Create basis functions
>>> from glearn import LinearModel
>>> f = LinearModel(x, polynomial_degree=1,
...                 trigonometric_coeff=[1.0, 2.0],
...                 hyperbolic_coeff=[3.0], func=func)

Generate Design Matrix:

Generate the design matrix \(\mathbf{X}^{\ast}\) on arbitrary test points \(\boldsymbol{x}^{\ast}\):

>>> # Generate 100 test points
>>> x_test = generate_points(num_points=100, dimension=1)

>>> # Generate design matrix on test points
>>> X_test = f.generate_design_matrix(x_test)
>>> X_test.shape
(100, 10)

Orthonormalize Basis Functions:

Repeat the above example but set orthonormalize to True:

>>> # Generate design matrix on data points
>>> X = f.generate_design_matrix(x, orthonormalize=True)

>>> # Check the orthonormality of X
>>> I = numpy.eye(X_test.shape[1])
>>> numpy.allclose(X_test.T @ X_test, I, atol=1e-6)
True