SparseVAR.fit#
- SparseVAR.fit(*, penalty='lasso', lam=None, n_lambdas=20, lambda_min_ratio=1e-3)[source]#
Estimate under one penalty family, selecting the level if unstated.
- Parameters:
penalty (
Literal['lasso','adaptive','scad','mcp','group']) –"lasso","adaptive","scad","mcp", or"group"(the lag-group penalty, which selects whole lags).lam (
float|None) – Penalty level, positive, orNonefor rolling-origin selection.n_lambdas (
int) – Candidate levels on the geometric path, at least 2.lambda_min_ratio (
float) – Smallest candidate as a fraction of the level that zeroes everything, in(0, 1).
- Return type:
- Returns:
The fitted
SparseVARResult.- Raises:
SpecificationError – If the penalty specification is malformed or the sample cannot support the rolling validation.
NumericalError – If the solver loses curvature.
Example
The nonconvex penalties keep fewer coefficients than the lasso at their selected levels, and the group penalty keeps or drops whole lags:
>>> import numpy as np >>> rng = np.random.default_rng(0) >>> k, n = 6, 300 >>> first = 0.5 * np.eye(k) >>> first[1, 0], first[3, 2] = 0.3, -0.3 >>> y = np.zeros((n, k)) >>> for t in range(1, n): ... y[t] = first @ y[t - 1] + rng.standard_normal(k) >>> model = SparseVAR(y, order=2) >>> lasso = model.fit(penalty="lasso") >>> mcp = model.fit(penalty="mcp") >>> bool(mcp.n_nonzero < lasso.n_nonzero) True >>> grouped = model.fit(penalty="group") >>> per_lag = [int(np.count_nonzero(block)) for block in grouped.coefficients] >>> all(count in (0, k * k) for count in per_lag) True >>> model.fit(lam=0.0) Traceback (most recent call last): cultivars.exceptions.SpecificationError: lam must be positive when given; got 0.0.