regressioninc.linear module#
Regressors for estimating parameters for linear problems
These are built on numpy as numpy.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.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 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.complex_to_glr(X: ndarray, y: ndarray) tuple[numpy.ndarray, numpy.ndarray] [source]#
Convert complex-valued linear problem to a real-valued generalised 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 import add_intercept, complex_to_glr >>> 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_glr, y_glr = complex_to_glr(X, y) >>> X_glr 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_glr array([-2., -4., -1., -3., 5., 6.])
- regressioninc.linear.glr_coef_to_complex(coef: ndarray) ndarray [source]#
Transform coefficients from real to complex-values for complex-valued problems that were posed
- Parameters:
coef (np.ndarray) – Coefficients array
- Returns:
The complex-valued coefficients
- Return type:
np.ndarray
Examples
Let’s generate a complex-valued linear problem, pose it as a linear problem and then convert the returned coefficients 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 import add_intercept, LeastSquares >>> from regressioninc.linear import complex_to_glr, glr_coef_to_complex >>> coef = np.array([3 + 2j]) >>> grid = ComplexGrid(r1=-1, r2=1, nr=3, i1=4, i2=6, ni=3) >>> X, y = generate_linear_grid(coef, [grid], intercept=10) >>> X = add_intercept(X)
Convert the complex-valued problem to a real-valued problem
>>> X_glr, y_glr = complex_to_glr(X, y)
Solve the real-valued linear problem
>>> model = LeastSquares() >>> model.fit(X_glr, y_glr) LeastSquares...
Look at the real-valued coefficients
>>> model.coef array([ 3., 2., 10., 0.])
Convert the coefficients back to the complex domain
>>> glr_coef_to_complex(model.coef) array([ 3.+2.j, 10.+0.j])
- pydantic model regressioninc.linear.LinearRegressor[source]#
Bases:
Regressor
Base class for LinearRegression
Show JSON schema
{ "title": "LinearRegressor", "description": "Base class for LinearRegression", "type": "object", "properties": { "coef": { "title": "Coef" }, "residuals": { "title": "Residuals" }, "rank": { "title": "Rank", "type": "integer" }, "singular": { "title": "Singular" } } }
- field rank: int | None = None#
The rank of the predictors
- pydantic model regressioninc.linear.LeastSquares[source]#
Bases:
LinearRegressor
Standard linear regression
Show JSON schema
{ "title": "LeastSquares", "description": "Standard linear regression", "type": "object", "properties": { "coef": { "title": "Coef" }, "residuals": { "title": "Residuals" }, "rank": { "title": "Rank", "type": "integer" }, "singular": { "title": "Singular" } } }
- pydantic model regressioninc.linear.WeightedLeastSquares[source]#
Bases:
LinearRegressor
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.
Show JSON schema
{ "title": "WeightedLeastSquares", "description": "Transform X and y using the weights to perform a weighted least squares\n\n.. math::\n \\sqrt{weights} y = \\sqrt{weights} X coef ,\n\nis equivalent to,\n\n.. math::\n X^H weights y = X^H weights X coef ,\n\nwhere :math:`X^H` is the hermitian transpose of X.\n\nIn this method, both the observations y and the predictors X are multipled\nby the square root of the weights and then returned.", "type": "object", "properties": { "coef": { "title": "Coef" }, "residuals": { "title": "Residuals" }, "rank": { "title": "Rank", "type": "integer" }, "singular": { "title": "Singular" } } }
- 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
- Raises:
ValueError – If the size of weights does not match the size of the observations y
- pydantic model regressioninc.linear.M_estimate[source]#
Bases:
LinearRegressor
Show JSON schema
{ "title": "M_estimate", "description": "Base class for LinearRegression", "type": "object", "properties": { "coef": { "title": "Coef" }, "residuals": { "title": "Residuals" }, "rank": { "title": "Rank", "type": "integer" }, "singular": { "title": "Singular" }, "n_iter": { "title": "N Iter", "default": 50, "type": "integer" }, "early_stopping": { "title": "Early Stopping", "default": 1e-05, "type": "number" } } }
- field n_iter: int = 50#
- field early_stopping: float = 1e-05#
- pydantic model regressioninc.linear.MM_estimate[source]#
Bases:
LinearRegressor
Show JSON schema
{ "title": "MM_estimate", "description": "Base class for LinearRegression", "type": "object", "properties": { "coef": { "title": "Coef" }, "residuals": { "title": "Residuals" }, "rank": { "title": "Rank", "type": "integer" }, "singular": { "title": "Singular" } } }
- pydantic model regressioninc.linear.ComplexAsGLR[source]#
Bases:
LinearRegressor
Show JSON schema
{ "title": "ComplexAsGLR", "description": "Base class for LinearRegression", "type": "object", "properties": { "coef": { "title": "Coef" }, "residuals": { "title": "Residuals" }, "rank": { "title": "Rank", "type": "integer" }, "singular": { "title": "Singular" } } }