fracridge
.fracridge¶
- fracridge(X, y, fracs=None, tol=1e-10, jit=True)[source]¶
Approximates alpha parameters to match desired fractions of OLS length.
- Parameters
- Xndarray, shape (n, p)
Design matrix for regression, with n number of observations and p number of model parameters.
- yndarray, shape (n, b)
Data, with n number of observations and b number of targets.
- fracsfloat or 1d array, optional
The desired fractions of the parameter vector length, relative to OLS solution. If 1d array, the shape is (f,). This input is required to be sorted. Otherwise, raises ValueError. Default: np.arange(.1, 1.1, .1).
- jitbool, optional
Whether to speed up computations by using a just-in-time compiled version of core computations. This may not work well with very large datasets. Default: True
- Returns
- coefndarray, shape (p, f, b)
The full estimated parameters across units of measurement for every desired fraction. If fracs is a float or y is a vector, the second or third dimensions are squeezed, correspondingly.
- alphasndarray, shape (f, b)
The alpha coefficients associated with each solution
Examples
Generate random data:
>>> np.random.seed(0) >>> y = np.random.randn(100) >>> X = np.random.randn(100, 10)
Calculate coefficients with naive OLS:
>>> coef = np.linalg.inv(X.T @ X) @ X.T @ y >>> print(np.linalg.norm(coef)) 0.35
Call fracridge function:
>>> coef2, alpha = fracridge(X, y, 0.3) >>> print(np.linalg.norm(coef2)) 0.10 >>> print(np.linalg.norm(coef2) / np.linalg.norm(coef)) 0.3
Calculate coefficients with naive RR:
>>> alphaI = alpha * np.eye(X.shape[1]) >>> coef3 = np.linalg.inv(X.T @ X + alphaI) @ X.T @ y >>> print(np.linalg.norm(coef2 - coef3)) 0.0