GraphicalVAR.fit#

GraphicalVAR.fit(*, lam=None, n_lambdas=15, lambda_min_ratio=1e-3, n_folds=5, seed=None)[source]#

Estimate both networks.

Parameters:
  • lam (float | None) – Stage-one (coefficient) penalty level, positive, or None for rolling-origin selection; the nodewise stage always selects its own per-node levels by K-fold cross-validation.

  • n_lambdas (int) – Candidate levels for both stages’ paths, at least 2.

  • lambda_min_ratio (float) – Path floor as a fraction of the zeroing level, in (0, 1).

  • n_folds (int) – Cross-validation folds for the nodewise stage, at least 2.

  • seed (int | Generator | None) – Seed or generator for the fold shuffle.

Return type:

GraphicalVARResult

Returns:

The fitted GraphicalVARResult.

Raises:

Example

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y = np.zeros((300, 4))
>>> for t in range(1, 300):
...     y[t] = 0.5 * y[t - 1] + rng.standard_normal(4)
>>> model = GraphicalVAR(y, order=1)
>>> res = model.fit(n_lambdas=10, n_folds=4, seed=0)
>>> res.lambda_path.shape, res.cv_errors.shape, res.lam_nodes.shape
((10,), (10,), (4,))
>>> again = model.fit(n_lambdas=10, n_folds=4, seed=0)
>>> bool(np.array_equal(res.precision, again.precision))
True
>>> model.fit(n_folds=1)
Traceback (most recent call last):
cultivars.exceptions.SpecificationError: n_folds must be at least 2; got 1.