regressioninc.linear.models module#
This module implements estimators for linear complex problems
These are built on scipy as scipy.linalg.lstsq does allow and support complex-valued inputs. However, it does not output residuals for complex-valued variables, therefore these are calculated out explicitly.
- regressioninc.linear.models.add_intercept(X: ndarray) ndarray[source]#
Add an intercept to the regressors
- Parameters:
X (np.ndarray) – The regressors
- Returns:
The regressors with an extra column of ones to represent to allow solving for the intercept term
- Return type:
np.ndarray
Examples
>>> import numpy as np >>> from regressioninc.linear.models import add_intercept >>> X = np.array([[2,3], [4,5]]) >>> X array([[2, 3], [4, 5]])
Now add the intercept
>>> X = add_intercept(X) >>> X array([[2, 3, 1], [4, 5, 1]])
- regressioninc.linear.models.complex_to_real(X: ndarray, y: ndarray) tuple[numpy.ndarray, numpy.ndarray][source]#
Convert complex-valued linear problem to a real-valued linear regression
- Parameters:
X (np.ndarray) – The regressors
y (np.ndarray) – The observations
- Returns:
X, y converted to real-valued generalised linear regression
- Return type:
tuple[np.ndarray, np.ndarray]
Examples
>>> from regressioninc.linear.models import add_intercept, complex_to_real >>> X = np.array([3 + 4j, 2 + 8j, 6 + 4j]).reshape(3,1) >>> X = add_intercept(X) >>> y = np.array([-2 - 4j, -1 - 3j, 5 + 6j]) >>> X_real, y_real = complex_to_real(X, y) >>> X_real array([[ 3., -4., 1., -0.], [ 4., 3., 0., 1.], [ 2., -8., 1., -0.], [ 8., 2., 0., 1.], [ 6., -4., 1., -0.], [ 4., 6., 0., 1.]]) >>> y_real array([-2., -4., -1., -3., 5., 6.])
- regressioninc.linear.models.real_params_to_complex(params: ndarray) ndarray[source]#
Convert real-valued parameters to complex-valued ones for problems that were transformed from complex-valued to real-valued.
- Parameters:
params (np.ndarray) – Real-valued parameters array
- Returns:
The complex-valued parameters
- Return type:
np.ndarray
Examples
Let’s generate a complex-valued linear problem, pose it as a real-valued linear problem and then convert the returned parameters back to complex.
Generate the linear problem and add an intercept column to the regressors
>>> import numpy as np >>> np.set_printoptions(precision=3, suppress=True) >>> from regressioninc.testing.complex import ComplexGrid, generate_linear_grid >>> from regressioninc.linear.models import add_intercept, OLS >>> from regressioninc.linear.models import complex_to_real, real_params_to_complex >>> params = np.array([3 + 2j]) >>> grid = ComplexGrid(r1=-1, r2=1, nr=3, i1=4, i2=6, ni=3) >>> X, y = generate_linear_grid(params, [grid], intercept=10) >>> X = add_intercept(X)
Convert the complex-valued problem to a real-valued problem
>>> X_real, y_real = complex_to_real(X, y)
Solve the real-valued linear problem
>>> model = OLS() >>> model.fit(X_real, y_real) OLS...
Look at the real-valued coefficients
>>> model.estimate.params array([ 3., 2., 10., 0.])
Convert the coefficients back to the complex domain
>>> real_params_to_complex(model.estimate.params) array([ 3.+2.j, 10.+0.j])
- regressioninc.linear.models.apply_weights(X: ndarray, y: ndarray, weights: ndarray) tuple[numpy.ndarray, numpy.ndarray][source]#
Transform X and y using the weights to perform a weighted least squares
\[\sqrt{weights} y = \sqrt{weights} X coef ,\]is equivalent to,
\[X^H weights y = X^H weights X coef ,\]where \(X^H\) is the hermitian transpose of X.
In this method, both the observations y and the predictors X are multipled by the square root of the weights and then returned.
- Parameters:
X (np.ndarray) – The regressors
y (np.ndarray) – The regressands
weights (np.ndarray) – The weights
- Returns:
The weighted regressors and the weights regrassands
- Return type:
tuple[np.ndarray, np.ndarray]
Examples
>>> import numpy as np >>> X = [5, 3] >>> y = [2, 8] >>> weights = []
- pydantic model regressioninc.linear.models.LinearEstimate[source]#
Bases:
EstimateA linear estimate
- field rank: int [Required]#
- pydantic model regressioninc.linear.models.LinearEstimator[source]#
Bases:
EstimatorBase class for a linear estimator
- Fields:
- regressioninc.linear.models.lstsq(X: ndarray, y: ndarray, weights: ndarray | None = None, cond=None, overwrite_a=False, overwrite_b=False, check_finite=True, lapack_driver=None)[source]#
OLS from scipy wrapped here in case support for complex is dropped
- pydantic model regressioninc.linear.models.OLS[source]#
Bases:
LinearEstimatorOrdinary least square regression
- Fields:
- pydantic model regressioninc.linear.models.WLS[source]#
Bases:
LinearEstimatorWeighted least squares
- Fields:
- fit(X: ndarray, y: ndarray, weights: ndarray) ndarray[source]#
Apply weights to observations and predictors and find the coefficients of the linear model
- Parameters:
X (np.ndarray) – The predictors
y (np.ndarray) – The observations
weights (np.ndarray) – The weights to apply to the samples
- Returns:
The coefficients for the model
- Return type:
np.ndarray
- pydantic model regressioninc.linear.models.MEstimate[source]#
Bases:
LinearEstimate- Fields:
- field scale: float [Required]#
An estimate of scale to be used weighting the rows of the linear problem
- pydantic model regressioninc.linear.models.MEstimator[source]#
Bases:
LinearEstimator- field max_iter: int = 50#
The maximum number of iterations
- field early_stopping: float = 1e-08#
Minimum change required in residues between iterations to continue
- field warm_start: bool = False#
Use existing solution to initialise a new call to fit