fit_kalman¶
Defined in fynance.features.filters
- fit_kalman(y, G, F, *, fit_W=True, fit_V=True, W0=None, V0=None, m0=None, C0=None)[source]
Estimate Kalman filter noise covariances by maximum likelihood.
Optimises W and/or V via the prediction-error log-likelihood computed by
kalman_loglikelihood. Parameters are searched in log-space to guarantee positive-definiteness for diagonal matrices.Only diagonal W and V are supported for estimation. Pass fixed non-diagonal matrices through
W0/V0withfit_W=False/fit_V=False.- Parameters:
- ynp.ndarray of shape (T, n)
Observed data.
- Gnp.ndarray of shape (n, n)
State transition matrix (fixed).
- Fnp.ndarray of shape (n, n)
Observation matrix (fixed).
- fit_Wbool, optional
Whether to estimate W (default True).
- fit_Vbool, optional
Whether to estimate V (default True).
- W0np.ndarray of shape (n, n), optional
Initial / fixed W. If
fit_W=Falsethis value is used as-is. Defaults to identity.- V0np.ndarray of shape (n, n), optional
Initial / fixed V. If
fit_V=Falsethis value is used as-is. Defaults to identity.- m0np.ndarray of shape (n,), optional
Initial state mean (default: zeros).
- C0np.ndarray of shape (n, n), optional
Initial state covariance (default: identity).
- Returns:
- dict with keys:
'W': np.ndarray — estimated (or fixed) process noise covariance.'V': np.ndarray — estimated (or fixed) observation noise covariance.'loglik': float — log-likelihood at the optimum.'success': bool — whether the optimizer converged.
Examples
>>> import numpy as np >>> rng = np.random.default_rng(42) >>> n, T = 1, 200 >>> W_true = np.eye(n) * 0.5 >>> V_true = np.eye(n) * 1.0 >>> x = np.cumsum(rng.multivariate_normal(np.zeros(n), W_true, T), axis=0) >>> y = x + rng.multivariate_normal(np.zeros(n), V_true, T) >>> G = F = np.eye(n) >>> result = fit_kalman(y, G, F) >>> result['success'] True >>> result['W'].shape (1, 1)