Pseudo-code
Each algorithm step by step, with the function that carries it out
The pseudo-code below follows the code of version 0.4 step by step, without the bookkeeping (batching, devices, saving). The right-hand column names the function that carries out each step. Notation: \(x_n\) are the training samples (delay windows), \(N\) their number, \(\langle u, w\rangle = \frac1N\sum_n u(x_n) w(x_n)\), \(\varphi_j\) the kernel eigenfunctions and \(\varphi'_j\) their duals, \(f\) the target, and \(t_n\) the time of the last snapshot of sample \(n\).
The kernel analog forecasting script, examples/enso/kernel_analog_forecasting.py, from data to skill scores:
INPUT predictor fields, target series, training and test periods
settings: window half-width h, cone ζ, tuning ranges, normalization,
number of eigenfunctions L, forecast steps Q
OUTPUT forecasts F[n, q] for every test sample n and lead q = 0..Q; NRMSE(q), ACC(q)
1 initialize JAX: devices, precision clim.initialize_jax
2 for period in (training, test):
3 read the data; remove climatology; smooth; standardize clim.extract_data_arrays
4 form the delay windows x_n = (y_n, ..., y_{n+2h}) clim.make_data_driven_l2_space
5 if a variable bandwidth is used:
6 tune a Gaussian kernel on the states knl.tune_bandwidth
7 σ(x) ← (c / density(x))^(1/m) knl.make_data_driven_bandwidth_function
8 d²(x, x') ← cone (or Euclidean) distance / (σ(x) σ(x')) dst.make_sqcone,
knl.make_data_driven_scaled_sqdist
9 ε ← bandwidth_scl × (bandwidth maximizing the dimension D) knl.tune_bandwidth
10 k(x, x') ← exp(−d²(x, x') / ε²) knl.make_data_driven_rbf_kernel
11 (λ_j, φ_j, φ'_j), j < L ← eigendecomposition of normalized k knl.compute_eigen
12 c[q, j] ← ⟨φ'_j, f(· + q)⟩ for q = 0..Q clim.compute_kaf_expansion_coeffs
13 for each test sample x: F[x, q] ← Σ_j c[q, j] φ_j(x) knl.compute_kaf_preds
14 NRMSE(q), ACC(q) ← compare F[·, q] with the observed target clim.compute_skill_scores
The Koopman script, examples/enso/koopman_diffusion_regularization_kaf.py, runs steps 1–11, then:
12 G ← generator matrix in the basis φ_1..φ_L koop.compute_generator_matrix
13 (γ_k, ζ_k) ← eigenpairs of ½(G − Gᵀ) − τ diag(η), by energy koop.compute_diffusion_regularized_generator_eigen
14 z(t) ← Re ζ_k on the training windows and Q windows after koop.evaluate_eigenfunction
15 c[q, j] ← ⟨φ'_j, z(· + q)⟩ clim.compute_kaf_expansion_coeffs(responses=z)
16 F[x, q] ← Σ_j c[q, j] φ_j(x) for each test sample knl.compute_kaf_preds
17 NRMSE(q), ACC(q) ← compare with Re ζ_k on the test windows clim.compute_skill_scores
FUNCTION extract_data_arrays(data_pars) clim.extract_data_arrays
for each data specification S (predictors, then target):
D ← all files in S.io.input_path with extension S.io.file_format
D ← D sorted by latitude, cut to S.domain
D ← spatial sampling of D (pointwise, coarsened, or averaged)
if S.climatology.remove:
D ← D − (mean of D for the same calendar month, or day of year,
over the climatology period)
if S.time.rolling_window:
D ← running mean of D over rolling_window samples
if S.climatology.standardize:
D ← (D − mean over the climatology period) / scale see the Data page
D ← D for the dates in S.time.date_range
A_S ← D as a matrix: time × (grid points and variables);
drop columns with missing values
Y ← [A_S1 A_S2 ...] predictors side by side
f ← A_target as a vector target
if velocity_covariate:
V ← central finite difference of Y along time, order velocity_fd_order
Y ← (Y, V)
return NPData(time, Y, f)
FUNCTION make_data_driven_l2_space(data_pars) clim.make_data_driven_l2_space
T ← number of dates; n_v ← velocity_fd_order (0 without velocities)
N ← T − 2h − num_before − num_after − n_v DataPars.num_samples
i0 ← n_v/2 + num_before DataPars.delay_embedding_origin
x_n ← (Y[i0+n], Y[i0+n+1], ..., Y[i0+n+2h]), n = 0..N−1 assembled on the fly
t_n ← i0 + 2h + n time of the last snapshot
⟨u, w⟩ ← (1/N) Σ_n u(x_n) w(x_n)
FUNCTION tune_bandwidth(pars, samples, d²) knl.tune_bandwidth
for ε in 10^linspace(lims[0], lims[1], num_bandwidths):
S(ε) ← (1/N²) Σ_{n, n'} exp(−d²(x_n, x_n') / ε²)
D(ε) ← d log S / d log ε closed form or automatic differentiation
ε* ← argmax D(ε) or argmin |D(ε) − manifold_dim| if manifold_dim is set
ε ← bandwidth_scl × ε*; m ← D(ε)
return TuneInfo(ε, m, ...)
FUNCTION compute_eigen(kernel_pars, samples, k) knl.compute_eigen
# diffusion maps; α = 1 for "laplace", ½ for "fokkerplanck"
q(x) ← (1/N) Σ_n k(x, x_n)
k_α(x, x') ← k(x, x') / (q(x) q(x'))^α
d(x) ← (1/N) Σ_n k_α(x, x_n)
M[n, n'] ← k_α(x_n, x_n') / (N √(d(x_n) d(x_n'))) symmetric N × N
(λ_j, u_j) ← leading num_eigs eigenpairs of M "eigh" (dense) or "eigsh" (iterative)
φ_j ← u_j / u_0, normalized eigenfunctions; φ_0 constant
φ'_j ← N u_j u_0, scaled so that ⟨φ'_i, φ_j⟩ = δ_ij duals
return KernelEigen(λ, φ, φ', weights = u_0², ε)
FUNCTION φ_j(x) for a new sample x Nyström extension, in
p(x, x_n) ← k_α(x, x_n) / ((1/N) Σ_m k_α(x, x_m)) knl.make_data_driven_eigenbasis
return (1/λ_j) (1/N) Σ_n p(x, x_n) φ_j(x_n)
FUNCTION generator_eigen(koopman_pars, samples, φ, λ, ε)
B ← (φ_1, ..., φ_L) which_eigs_galerkin; φ_0 handled separately
for each training sample n: clim.make_data_driven_tangent_
ẋ_n ← central finite difference of order p of the windows evaluation_functional_fd
around x_n needs p/2 samples before and after
G[i, j] ← (1/N) Σ_n φ'_i(x_n) (∇φ_j(x_n) · ẋ_n) koop.compute_generator_matrix
gradient of the Nyström extension, by automatic differentiation
η_j ← −4 ln λ_j / ε² Laplace eigenvalues
L_τ ← ½ (G − Gᵀ) − τ diag(η) (G − τ diag(η) if antisym is False)
(γ_k, c_k) ← eigenpairs of L_τ; duals c'_k from (C C*)⁻¹ C koop.compute_diffusion_
E_k ← Σ_j |c_jk|² (1/λ_j − 1) Dirichlet energy regularized_generator_eigen
sort by E_k (sort_by="energy") or |Im γ_k| ("frequency")
prepend ζ_0 = 1 with γ_0 = 0; keep num_eigs
ζ_k(x) ← Σ_j c_jk φ_j(x); ω_k ← Im γ_k; period_k ← 2π / ω_k
return KoopmanEigen(γ, E, ω, period, c, c')
For the compactified integral transforms (KoopmanParsTransf), the generator matrix is replaced by:
w_m ← quadrature weight × e^(−z m Δt) (Laplace) or e^(−(z m Δt)²) (Gauss), m = 0..num_quad
(Tφ_j)(x_n) ← Σ_m w_m φ_j(x_{n+m}) koop.compute_integral_
A[i, j] ← ⟨φ'_i, Tφ_j⟩; A ← ½ (A − Aᵀ) transform_matrix
Λ ← diag(λ_j^τ) or diag((1 − √(η_j/η_max))^τ) koop.compute_integral_
eigenpairs of Λ A Λ; generator eigenvalues by inverting the transform transform_eigen_comp
FUNCTION kaf_coefficients(Q, J, f) clim.compute_kaf_expansion_coeffs
for q = 0..Q, j ∈ J:
c[q, j] ← (1/N) Σ_n φ'_j(x_n) f(t_n + q) needs num_after ≥ Q
return c (Q + 1) × |J|
FUNCTION kaf_forecast(c, x) knl.compute_kaf_preds
for q = 0..Q:
F[q] ← Σ_{j ∈ J} c[q, j] φ_j(x) φ_j(x) by the Nyström extension
return F
FUNCTION iterative_kaf(x, Q) knl.compute_iterative_kaf_preds
c ← kaf coefficients of the next snapshot y(t_n + 1)
repeat Q times:
y_next ← Σ_j c_j φ_j(x); x ← window shifted by one snapshot, ending in y_next
record y_next
FUNCTION koopman_forecast(x, t) koop.compute_koopman_preds
a_k ← ⟨ζ'_k, f⟩ clim.compute_koopman_response_coeffs
return Σ_k e^(γ_k t) a_k ζ_k(x)
FUNCTION skill_scores(test_pars, test, F) clim.compute_skill_scores
e_n ← test_pars.delay_embedding_end + n index of the initial time of test sample n
for q = 0..Q:
a[n] ← f_test[e_n + q], n = 0..N_test − 1 observed target
NRMSE[q] ← √( Σ_n (a[n] − F[n, q])² / Σ_n a[n]² )
ACC[q] ← correlation over n of a[n] and F[n, q]
return SkillScores(nrmses = NRMSE, accs = ACC)
FUNCTION spatial_pattern(k, field F, t_0, w) clim.compute_eigenfunction_projections
return mode_k(s) = w Σ_n ψ_k(x_n) F(s, t_0 + n) ψ_k: dual eigenfunction (kernel or Koopman)
Quantum mechanical data assimilation as implemented in MATLAB/nlsa/utils/forecasting/qmda.m, with the set-up of the ENSO example ensoQMDA.m. The quantum state is a row vector \(\xi\) of coefficients in the basis \(\varphi_1, \dots, \varphi_L\) (nL eigenfunctions, with weights \(\mu_n\) on the training samples).
INPUT basis φ_l (l = 1..L) on the training samples, weights μ, target y on the training samples,
observed states x_τ at the analysis times
settings: L = nL, nTO (steps between observations), nTF (forecast steps),
kernel shape (bump), bandwidth scale epsilonScl, ifVB, ifSqrtm
OUTPUT mean, standard deviation and probabilities of y at leads 0..nTF after each analysis
1 U_q[i, j] ← Σ_n μ_n φ_i(x_n) φ_j(x_{n+q}), q = 1..nTF koopmanOperator.m
2 A[i, j] ← Σ_n μ_n φ_i(x_n) y(x_n) φ_j(x_n); A2 ← A² multiplicationOperator.m
3 bins of y from its quantiles; group the eigenvectors e of A by bin (for probabilities)
4 for each analysis time τ:
κ_τ(x_n) ← √ shape( d²(x_n, x_τ) / (ε · epsilonScl)² ) feature function of the observation
K_τ[i, j] ← Σ_n μ_n φ_i(x_n) κ_τ(x_n) φ_j(x_n) multiplicationOperator.m
5 ξ ← (1, 0, ..., 0) constant wavefunction: the invariant distribution
6 ξ ← ξ U_nTO / ‖ξ U_nTO‖ prior for the first analysis
7 for each analysis time τ: qmda.m
8 ξ ← ξ K_τ / ‖ξ K_τ‖ analysis: update with the observation
9 for q = 0..nTF (U_0 = identity):
10 ξ_q ← ξ U_q / ‖ξ U_q‖ forecast
11 mean_q ← ξ_q A ξ_qᴴ; std_q ← √(ξ_q A2 ξ_qᴴ − mean_q²)
12 prob_q(bin) ← Σ_{e ∈ bin} |ξ_q e|²
13 ξ ← ξ_nTO prior for the next analysis
A Python version would use the same ingredients: the kernel eigenfunctions of block 4, Koopman operator matrices estimated from time-shifted eigenfunctions, and multiplication operators for the target and the observations. The Python toolkit has the forecast–analysis combinator (nlsa.qmda.make_da) but not yet these operators.