Singular Spectrum Analysis: PyTorch Architecture¶
Navigation:
Mathematical Theory: See SSA.md
Spectrum API Overview: See The Spectrum API
Overview & Formula Tokens¶
Singular Spectrum Analysis is exposed via ssa():
# Singular Spectrum Analysis (SSA) causal Hankel EOF decomposition
formula = "Y ~ ssa(x1, window=72, k=4)"
Parameter Specification¶
SSAEffect (ssa())¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Hankel embedding window length \(L\). |
|
|
|
Top empirical orthogonal functions (EOFs) retained. |
Complexity & Performance¶
\(\mathcal{O}(B\,T\,L\,k)\) time and memory (one windowed matrix product); the EOFs come from one \(L \times L\) eigendecomposition at initialization.
State, Complexity and Limits¶
Class and file:
SSAEffect,src/tam/model/spectrum/_ssa.py; formula tokenssa(). Label: (BETA).State:
{"hist": (B, window - 1, 1)}. Layout:(B, T), one column.Extrapolation:
'linear'and'saturation'are refused at construction.Limits: the EOFs are those of the training period; a regime with another dominant period projects on them with a small amplitude.
Chunk equivalence bound:
tests/spectrum/test_stateful_contract.pychecks that the second of two chunks, started from the state of the first, equals the matching rows of the one-pass features withinatol=1e-8(float64), and that every group computed alone equals the same group inside a batch withinatol=1e-10.
Code¶
Feature map of SSAEffect (build_feature_map):
def build_feature_map(self, x_col: torch.Tensor, state_in: Optional[Dict[str, Any]] = None) -> torch.Tensor:
r"""Constructs causal sliding-window projection onto fitted EOFs.
Args:
x_col: Input sequence tensor ``(B, T)`` (or ``(T,)``); every group is read alone.
state_in: ``{"hist": (B, window - 1, 1)}``, the last centered samples of the pass before.
Returns:
Design matrix ``(B, T, k)``.
"""
x3, shape, layout = to_btd(x_col, False)
if x3.shape[-1] != 1:
raise ValueError(f"ssa reads one column, got {x3.shape[-1]}")
if self.eof_vectors is None:
self.initialize(x_col)
T = x3.shape[1]
L = self.window
U = self.eof_vectors.to(device=x3.device, dtype=x3.dtype)
x_cent = x3 - self._x_mean.to(device=x3.device, dtype=x3.dtype)
x_hist = causal_history(x_cent, self._n_hist, state_in)
self._state_out = {"hist": tail_state(x_hist, self._n_hist)}
windows = causal_window_unfold(x_hist, L, t_len=T)[..., 0] # (B, T, L)
return from_btk(windows @ U, shape, layout) # (B, T, k)
Penalty of SSAEffect (build_penalty_matrix):
def build_penalty_matrix(self) -> torch.Tensor:
r"""Constructs spectral smoothness penalty:
.. math::
P_{\text{ssa}} = \operatorname{diag}\left( \frac{\sigma_1}{\sigma_r + 10^{-6}} \right)_{r=1}^k \cdot \lambda_p
"""
if self.singular_values is None:
weights = torch.ones(self.k, device=TORCH_DEVICE, dtype=torch.get_default_dtype())
else:
sig = self.singular_values.to(device=TORCH_DEVICE, dtype=torch.get_default_dtype())
sig_max = sig[0]
weights = sig_max / (sig + 1e-6)
return torch.diag(weights) * self.lambda_p
Parsing of the ssa(...) token in the factory:
elif ttype == 'ssa':
window = int(params_resolved.get('window', params_resolved.get('L', 48)))
k = int(params_resolved.get('k', 10))
extrap_val = params_resolved.get('extrapolate', 'continue')
effects_list.append(SSAEffect(
feature_name=feature_name,
window=window,
k=k,
lambda_p=lambda_p,
extrapolate=extrap_val,
))