Skip to main content

6. Forecasting

Kernel analog forecasts, forecasts of Koopman indices, Koopman forecasts, and the status of QMDA

The forecasting block of the NLSA Python toolkit: kernel analog forecasting (direct and iterative), forecasts of Koopman eigenfunctions, Koopman forecasts, exceedance probabilities by kernel analog forecasting, and the status of quantum mechanical data assimilation in Python and MATLAB.

The toolkit makes forecasts in four ways. All of them are trained on the training samples and then applied to every sample of the test period:

Forecasting methods of the toolkit
Method Forecasts Main functions Used in
Kernel analog forecasting (KAF) A target variable, such as the Niño 3.4 index, at leads 0 to Q clim.compute_kaf_expansion_coeffs
knl.compute_kaf_preds
ENSO KAF script; Lorenz 63
KAF of a Koopman eigenfunction A Koopman index, such as the ENSO or MJO eigenfunction koop.evaluate_eigenfunction, then the KAF functions ENSO Koopman script
Iterative KAF The whole predictor vector, one step at a time knl.compute_iterative_kaf_preds Lorenz 63
Koopman forecast A target, by letting each Koopman eigenfunction oscillate koop.compute_koopman_preds Lorenz 63

Quantum mechanical data assimilation is implemented in the MATLAB code; the Python toolkit has only some of its building blocks so far.

Kernel analog forecasting

Kernel analog forecasting (Zhao and Giannakis 2016; Alexander and Giannakis 2020) predicts a target f at lead time q from the current delay window x as an expansion in the kernel eigenfunctions:

\[ \hat f_q(x) = \sum_{j} c_{qj}\, \varphi_j(x), \]

\[ c_{qj} = \langle \varphi'_j, U^q f\rangle = \frac{1}{N}\sum_{n} \varphi'_j(x_n)\, f(t_n + q), \]

where \(t_n\) is the time of the last snapshot of training sample n, so that \(f(t_n + q)\) is the target q steps after it. The coefficients for all lead times form an array of size (Q + 1) × L (kaf_coeffs.pkl; plot "expansion_coeffs"). The forecast from a test sample uses the Nyström extension of the eigenfunctions, which amounts to a weighted average of what followed similar training samples (the analogs). With more eigenfunctions L the forecast follows the training data more closely; with fewer it is smoother, and it relaxes toward the climatology at long leads.

coeffs = clim.compute_kaf_expansion_coeffs(
    (pars.train.data, pars.train.kernel), impl_l2, train_data, kernel, kernel_eigen,
    pars.train.pred.num_steps,          # Q
    pars.train.pred.which_eigs,         # eigenfunctions used
)
preds = knl.compute_kaf_preds(
    pars.train.kernel, impl_l2, train_data, kernel, kernel_eigen, coeffs,
    impl_l2_tst, test_data,
    pars.train.pred.which_eigs,         # the same selection as above
)
# preds[n, q]: forecast from test sample n at lead time q

The forecast settings are held in the script’s PredPars:

PredPars (examples/enso/kernel_analog_forecasting.py, defined in examples/enso/kernel_analog_forecasting.py)
Option Type or values Default Description
dt int required Prediction time step. Currently ignored: the KAF implementation assumes a step of one sample.
num_steps int required Number of prediction steps; forecasts are made at leads 0, 1, …, num_steps samples.
which_eigs int or tuple[int, int] or list[int] required Kernel eigenfunctions used in the forecast: an integer n means φ₀ … φ_n−1; a tuple (a, b) means φ_a … φ_b; a list gives the indices.

Requirements: the training data need num_after ≥ num_steps, so that the target is known Q steps after every training sample, and the same which_eigs must be passed to both functions.

Probabilities of exceeding a threshold

KAF of an indicator function (1 when the target exceeds a threshold, 0 otherwise) estimates the probability of exceedance given the current state, which is one way to obtain the probabilistic forecasts planned for the demonstration. The toolkit does not yet do this in the scripts, but compute_kaf_expansion_coeffs accepts any target series through its responses argument. A sketch, not tested:

i0 = train_pars.delay_embedding_end                  # first initial time
i1 = i0 + train_pars.num_samples + num_steps         # last verifying time + 1
exceed = (train.response[i0:i1] > threshold).astype(jax_env.real_dtype)
coeffs_p = clim.compute_kaf_expansion_coeffs((train_pars, kernel_pars), impl_l2, train,
                                             kernel, kernel_eigen, num_steps, which_eigs,
                                             responses=exceed)
probs = knl.compute_kaf_preds(kernel_pars, impl_l2, train, kernel, kernel_eigen, coeffs_p,
                              impl_l2_tst, test, which_eigs)
probs = jnp.clip(probs, 0, 1)                        # the expansion can overshoot slightly

Forecasting a Koopman index

The Koopman script forecasts a Koopman eigenfunction \(\zeta_k\), such as the ENSO index, with KAF. The target is not read from data but computed: \(\zeta_k\) is evaluated on the training windows and the Q windows that follow them, and its real part is passed to compute_kaf_expansion_coeffs as responses. For verification, \(\zeta_k\) is evaluated on the test windows by the Nyström extension.

impl_eval_kaf = clim.make_data_driven_evaluation_functional(
    data_pars=pars.train.data, dtype=jax_env.real_dtype,
    num_before=0, num_after=pars.train.pred.num_steps,
)
koop_efun_vals = koop.evaluate_eigenfunction(
    (pars.train.kernel, pars.train.koopman), c_k, impl_l2, impl_eval_kaf, kernel,
    pars.train.pred.idx_koopman_eig, train_data, kernel_eigen, koopman_eigen, train_data,
)
coeffs = clim.compute_kaf_expansion_coeffs(
    (pars.train.data, pars.train.kernel), impl_l2, train_data, kernel, kernel_eigen,
    pars.train.pred.num_steps, pars.train.pred.which_kernel_eigs,
    responses=koop_efun_vals.real,
)

Only the real part is forecast in the script; the imaginary part, and with it the amplitude and phase of the index, can be forecast the same way with koop_efun_vals.imag. The settings:

PredPars (examples/enso/koopman_diffusion_regularization_kaf.py, defined in examples/enso/koopman_diffusion_regularization_kaf.py)
Option Type or values Default Description
dt float required Prediction time step (redundant for KAF; set to 1).
num_steps int required Number of prediction steps.
idx_koopman_eig int required Index of the Koopman eigenfunction to forecast (the ENSO eigenfunction in this example).
which_kernel_eigs int or tuple[int, int] or list[int] required Kernel eigenfunctions used in the KAF forecast of that eigenfunction, as for which_eigs.

Iterative KAF

Instead of one expansion per lead time, iterative KAF learns the map from the current window to the next snapshot of all predictors, and applies it repeatedly; with delays, the window is shifted by one snapshot after each step. It is used in examples/lorenz63/kernel_analog_forecasting_iterative.py, with coefficients from nlsa_models.lorenz63.compute_iterative_kaf_covariate_coeffs (built on knl.make_iterative_kaf_analysis_operator) and forecasts from knl.compute_iterative_kaf_preds(kernel_pars, impl_l2, train_data, kernel, kernel_eigen, coeffs, impl_l2_tst, test_data, num_steps, num_delays, which_eigs). Errors can accumulate as the map is iterated, which direct KAF avoids for a single target; iterative KAF in exchange forecasts the whole state at once.

Koopman forecasts

A Koopman forecast expands the target in Koopman eigenfunctions and lets each evolve with its own eigenvalue:

\[ \hat f(x, t) = \sum_{k} e^{\gamma_k t}\, \langle \zeta'_k, f\rangle\, \zeta_k(x). \]

The coefficients are computed by clim.compute_koopman_response_coeffs (or its Lorenz 63 version) and the forecasts by koop.compute_koopman_preds(pars, c_l, impl_l2, train_data, kernel, kernel_eigen, koopman_eigen, coeffs, impl_l2_tst, test_data, num_steps, dt, which_eigs). Because the regularized eigenvalues have negative real parts, the forecast relaxes toward the mean at long leads. Only the leading which_eigs Koopman eigenfunctions are used, which acts as a filter on the target. Both Lorenz 63 Koopman scripts make these forecasts.

Quantum mechanical data assimilation

Quantum mechanical data assimilation (QMDA) (Freeman et al. 2023) represents the state of knowledge as a quantum state on the space spanned by kernel eigenfunctions, evolves it with Koopman operators between observations, and updates it with each observation. Its output is a forecast distribution: mean, spread and probabilities of the target.

Where QMDA is implemented
Where What is available
Python (nlsa.qmda, nlsa.quantum) Building blocks only: the parameter container Qmda, the forecast–analysis cycle make_da(forecast, analysis), and prediction from a pure state (make_pure_state_prediction, pure_state_prediction). There is no QMDA pipeline or example script yet.
MATLAB (MATLAB/nlsa/utils/forecasting/) The complete algorithm, qmda.m, with the operators it needs, and the ENSO example of the QMDA paper (pubs/FreemanEtAl23_PNAS/enso_qmda/ensoQMDA.m).

The Python parameter container mirrors the MATLAB settings:

Qmda (nlsa.qmda, defined in src/nlsa/qmda.py)
Option Type or values Default Description
nTO int 1 Number of time steps between observations.
nTF int 1 Number of forecast time steps.
nL int 1 Number of basis functions.
shape_fun str "bump" Kernel shape function (default "bump").
epsilon float 1 Kernel bandwidth scaling.
ifVB bool True Use a variable-bandwidth kernel.
ifSqrtm bool False Perform the analysis (Bayesian update) step with the matrix square root of the feature operator.

The MATLAB functions:

QMDA and related functions in MATLAB/nlsa/utils/forecasting/
Function What it computes
qmda.m The assimilation cycle: forecast with Koopman operators, update with each observation, and the mean, standard deviation and (optionally) probabilities of the targets at every lead time
koopmanOperator.m Koopman operator matrices for given time shifts, \(U_{ij} = \sum_n \mu_n\, \varphi_i(x_n)\, \varphi_j(x_{n+q})\)
multiplicationOperator.m
sqrtMultiplicationOperator.m
Matrices of multiplication by a function (a target, or an observation’s feature function), and their square roots
featureVector.m
multiplicationOperator_kernel.m
Kernel feature vectors and the corresponding operators
qmExpectation.m Expectation of an observable in a quantum state
quantumForecast.m Probabilistic forecast with a density matrix
aggregateProbability.m Coarsening of probabilities into bins
analogForecast.m
generatorForecast.m
Analog (KAF) forecasts and forecasts with the generator, for comparison
forecastError.m
rmse.m, pc.m
Error, RMSE and pattern correlation scores

The steps of qmda.m are written out on the Pseudo-code page.

References

Alexander, R., and D. Giannakis. 2020. “Operator-Theoretic Framework for Forecasting Nonlinear Time Series with Kernel Analog Techniques.” Physica D 409: 132520. https://doi.org/10.1016/j.physd.2020.132520.
Freeman, D. C., D. Giannakis, B. Mintz, A. Ourmazd, and J. Slawinska. 2023. “Data Assimilation in Operator Algebras.” Proceedings of the National Academy of Sciences 120 (8): e2211115120. https://doi.org/10.1073/pnas.2211115120.
Zhao, Z., and D. Giannakis. 2016. “Analog Forecasting with Dynamics-Adapted Kernels.” Nonlinearity 29: 2888–939. https://doi.org/10.1088/0951-7715/29/9/2888.