Extracted main text — title through conclusion, appendix excluded. This is what our citation measures are computed over, published so the extraction can be checked by eye.
165,906 characters · 33 sections · 73 citation commands
Nonlinear Dynamic Factor Analysis With a Transformer Network
The paper develops a Transformer architecture for estimating dynamic factors from multivariate time series data under flexible identification assumptions. Performance on small datasets is improved substantially by using a conventional factor model as prior information via a regularization term in the training objective. The results are interpreted with Attention matrices that quantify the relative importance of variables and their lags for the factor estimate. Time variation in Attention patterns can help detect regime switches and evaluate narratives. Monte Carlo experiments suggest that the Transformer is more accurate than the linear factor model, when the data deviate from linear-Gaussian assumptions. An empirical application uses the Transformer to construct a coincident index of U.S. real economic activity.
\noindentKeywords: Transformer network, dynamic factor, coincident index
\noindentJEL: C32, C45, C38, C52
There are three methodological contributions. The first contribution is to design a Transformer architecture that can estimate latent dynamic factors from time series data. A Transformer neural network is a flexible machine learning algorithm for processing information, proposed by Vaswani2017. Time series Transformers have mostly been customized for predicting future values of observable variables. Even Transformers that relate to state-space modeling, such as tang2021probabilistic, or those that compress features from the input data, such as Zhou2021informer, tend to use these internal representations just to aid prediction. Going further, the Transformer in this study compresses the input data down to one real number per lag position, with an interpretation as a latent state variable or a dynamic factor. The factor estimates are then re-embedded and used to predict observable variables. The prediction errors are only used to condition the factor estimates on data during the training of the Transformer. To the best of my knowledge, Transformers have not been used for nonlinear dynamic factor analysis before.
The second contribution relates to improving the results on small datasets with prior information. A Transformer usually requires a large training or pre-training dataset to function properly, whereas typical macroeconomic datasets tend to be small, at least in the time dimension. As a remedy, I use conventional factor models as prior information to guide the training of the Transformer. Prior information is supplied through a regularization term, which is added to the prediction-error loss function. This regularization minimizes the distance between two factor estimates, one obtained from the Transformer and the other from a chosen conventional model. When a linear factor model is used as prior information, the Transformer is incentivized to learn the linear approximation. If the relative weight of prior information is modest, the Transformer can also find unspecified nonlinearities, which help minimize the prediction-error further. Training the Transformer with prior information helps narrow down its large parameter space in a useful manner, which would otherwise be hard with limited training examples. Prior information is found to improve the results on small datasets.
The third contribution is the development of time-series-specific tools for interpreting results with Attention patterns. The defining feature of a Transformer is the Attention mechanism, which connects each input token to all others to learn which relationships matter. With granular tokens, the Attention mechanism quantifies the contribution of every variable-lag pair to the factor estimate in each period. I analyze how the Attention patterns evolve over time and show how these changes can help detect regime switches, analyze shocks and assess narratives.
The Transformer is put to the test in a series of Monte Carlo experiments. Behind each simulated dataset, a state-transition equation determines the stochastic process for one latent factor, and a measurement equation describes the noisy mapping between the factor and several observed variables. The aim is to estimate the factor's values based on the observable variables, without any additional information about the underlying processes. Each subsequent dataset deviates from linearity and Gaussianity in different ways, cumulating complexity. Only 800 time periods are used for training per dataset, to reflect the typical scarcity of macro data. The Transformer is found to outperform the Kalman filter on most of the datasets, often by 20% more accurate factor estimates. The results are summarized in Table (ref).
In an empirical application, I use the Transformer to construct a coincident index of real business-cycle conditions in the U.S. economy. The dynamic factor is estimated from variables related to industrial production, sales, labor income, and hours worked, as proposed in a seminal study by stock1989new. I compare the resulting index to a linear factor model during three recent recessions, and to recession dates published by the NBER Business Cycle Dating Committee. The Transformer based index is mainly consistent with the linear benchmark and the NBER recession chronology.
Dynamic factors are important in macroeconomic analysis and policy work. Factors can be used for monitoring the economy, like the Aruoba--Diebold--Scotti business conditions index by diebold2009real, for compressing data, as with factor-augmented vector autoregression (FAVAR) by bernanke2005measuring, and for nowcasting real GDP, as in giannone2008nowcasting. It is typical to use a factor model with linear--Gaussian state space representation and the Kalman filter procedure by kalman1960new to estimate dynamic factors. This is also used as a baseline model in this study, referred to by the shorthand "Kalman filter factor estimate". Macroeconomic processes are rarely linear or Gaussian. The Kalman filter can be augmented in several ways to relax the linearity assumption sarkka2023bayesian and the particle filter is capable of dealing with nonlinear and non-normal processes, see fernandez2005estimating for the macroeconomic context. Nonetheless, it holds for all conventional methods that the model has to be specified before it can be estimated. This gives rise to the model selection problem: How to specify the functional form and stochasticity, so that they reasonably describe the data generating process?
The Transformer offers an attractive alternative to conventional factor models by improving on the model selection problem. Given a misspecified baseline model that is used as prior information, the Transformer can find unspecified complex nonlinear conditional patterns between variables and across time to improve the accuracy beyond the baseline. The functional forms of the state-transition and measurement equations do not require any rigid assumptions beyond the number of factors to be estimated. Because all models are misspecified in the real world, the chosen conventional model's accuracy could potentially be improved by using it in the Transformer framework as prior information.
There are several benefits to using Transformers for factor analysis, compared also to other machine learning alternatives. The Attention mechanism makes the output of a Transformer more transparent and easier to interpret than the output from a recurrent network, such as the Long Short Term Memory (LSTM) by Hochreiter1997LSTM. An Autoencoder based on a vanilla feed forward network lacks the power of Transformer modules and interpretability of the Attention mechanism. I propose new time series specific Attention based tools in Subsection (ref) to help analyze the results. A Transformer can also find dependencies over longer horizons than the LSTM network, because the Attention mechanism does not suffer from vanishing gradients. Additionally, it is possible to specify a sharp lag length for the Transformer, which is useful when the results are compared to other models with the same information set, such as the vector autoregression.
The rest of the Introduction highlights the different choices in the proposed Transformer to other existing Transformers, and explains how the differences allow and improve upon estimating factors from small macroeconomic datasets.
The definition of a token, the partition of data into basic computational units, is chosen to be one variable's one lag. A similar choice was made in zerveas2020multivariate. This choice is maximally granular and improves the interpretability of results at the expense of computational cost, differing from what is popular in the literature on time series Transformers. Patching by nie2022time treats a set of subsequent values of one variable as a token, which allows increasing the length of the input sequences processed at once. Patching typically leads to univariate analysis, where the cross-correlations between variables are not accounted for explicitly. On the other hand, typical feature-mixing models treat all variable values from one time period as one token, as in Zhou2021informer. Both token definitions make it possible to pack more information into the context window of a Transformer, at the cost of interpretability. This is desirable when there is an abundance of data, and when only the prediction accuracy matters. The situation is very different with scarce macroeconomic data, where the computational constraints are hardly ever a problem. This allows the use of more granular tokens to capture multivariate dynamics and to improve the researcher's ability to interpret the results. With the token definition used in this study, the Transformer resembles a nonlinear vector autoregression model. It is possible to analyze how much the Transformer paid attention to each variable's every lag, when it created the factor estimate.
The Transformer has an Encoder--Encoder architecture; see Figure (ref). In the beginning, an initial representation is created for the factor estimate series. The first, State Encoder, then refines this factor series using information in the data. The refined factor estimate is then used in the second, Measurement Encoder, to predict the data onward. It is a crucial feature of the algorithm that the prediction error heavily relies on the factor estimate. This informational bottleneck is further enforced by omitting the residual skip around the Measurement Attention. Minimizing the prediction error loss in training teaches the Transformer how to construct the factor estimate from data. More precisely, the State Encoder functions as a nonlinear filter, which infers a state-transition dynamics from the data. The Measurement Encoder uses the estimated factor series and the observable variables up to period t, to predict the values of the observable variables at period $t+1$. The outcome of interest comes from the State Encoder of a trained Transformer, the dynamic factor estimate. The Monte Carlo only includes one-factor datasets, but it is technically straightforward to extend the Transformer to estimate multiple factors at the same time, albeit at the cost of additional identification challenges.
The key benefit of creating an initial factor representation prior to the State Encoder is the enhanced interpretability of results. The State Attention gets the Queries from the factor series and the Keys and Values from the data. Therefore the Attention matrices directly describe the construction of the factor estimate, based on the observable variables and their lags. Additionally, the output layers after both of the Encoder stacks only compress each token representation from a vector back into a real number, without combining information across tokens. This ensures that most of the heavy lifting is done by the Encoders, especially the Attention mechanism. Attention is the only mechanism, where different variables and their lags are mapped directly into each other, to find out which connections matter.
The reason for specifying two Encoder stacks instead of a more typical Encoder-Decoder architecture stems from the specialties of macroeconomic data, and from the emphasis on estimating factors instead of predicting observables. Transformers operate on fixed-size context windows, meaning that a Transformer receiving $P$ lags will output $P$ next token predictions simultaneously for the $k$ observable variables. This means that when the context window includes positions $t, \dots, t-P$, then the outputs from the Measurement Encoder correspond to predictions for $t+1,\dots,t-P+1$. Only the last $t+1$ period predictions are used in the loss function during training, as they are based on the whole $P$-lag information set. The rest of the $P-1$ predictions are discarded. If the input sequences are long, conducting the forward pass this way becomes computationally costly, and instead it would be reasonable to use all $P$ predictions in the loss function, called teacher forcing. Teacher forcing requires using a measurement Decoder instead of an Encoder to generate the predictions, which uses a mask to guarantee that each prediction is only based on past information. Additional to the computational benefit of a Measurement Decoder, the Transformer could also learn to work with variable length information sets, but this will not be required at the inference stage in macroeconomic factor analysis. Furthermore, the predictions based on smaller information sets are likely inferior in quality to those predictions that are based on the full information set, introducing noise to the training process. Because of the relatively small input sequences in typical macroeconomic applications, it is computationally feasible to only use the last prediction for position $t+1$ in the loss function, to improve the quality of the parameter updates in training. The last prediction has only attended to past values, and therefore it can be obtained from an unmasked Encoder. Using a Measurement Encoder allows for richer dynamic interaction between the factor estimate and observable variables across the whole $P$ lag context window.
The next Subsection (ref) gives a general overview of dynamic factor models and Transformers. Section (ref) details the proposed Transformer algorithm and the training process. Subsection (ref) describes how prior information is supplied to the loss function to guide the training. Subsection (ref) presents the tools for analyzing the results, based on the Attention mechanism. Section (ref) conducts a Monte Carlo experiment to evaluate the performance of the Transformer, and analyzes the impact of prior information on the results. Section (ref) applies the Transformer to U.S. data. Section (ref) discusses the upsides and improvement areas of conducting factor analysis with the Transformer and the Section (ref) concludes.
The idea of distilling information from multiple relevant variables into a latent factor that represents an underlying phenomenon was formalized by spearman1904iq, teasing out a measure for general intelligence in humans. The concept of a factor has since become a central tool in many fields, including economics. The importance of using the co-movement across many macroeconomic variables in assessing the business cycle was emphasized already by burns1946measuring. Early dynamic factor models were developed for time series analysis by geweke1977dynamic, sargent1977business and stock1989new. Some recent efforts aim to increase the flexibility of the conventional filtering approach Villaverde2024filtering.
The earlier machine learning paradigm for sequential data was centered around the Recurrent neural networks, Long Short Term Memory (LSTM) and Gated Recurrent Unit (GRU), going back to Hochreiter1997LSTM. LSTM networks performed well with larger datasets and GRU networks were functional even with smaller datasets. The paradigm changed quickly after the publication of the Transformer white paper by Vaswani2017. Currently most contemporary large language models are based on Transformers, including the Generative Pre-trained Transformer (ChatGPT) by gpt2023, which has excelled in natural language translation, sentiment analysis and text production. It speaks to the flexibility of Transformers that they have been found useful in modeling spatial inputs such as pictures and video, sequential inputs such as language, sound and time series data, and even tabular data, consisting of discrete, ordinal and categorical variables Huang2020tabtransformer. Due to the lack of recurrence, the estimation of Transformers can be parallelized, enabling extensive pre-training using GPUs. This has been one of the key elements behind the ability of Transformers to generalize their success to new datasets with little to no additional fine-tuning. The recurrent structure made it computationally infeasible to pre-train the LSTM and GRU models in a similar manner, contributing to their inability to generalize.
The particular architecture for a Transformer depends on the task at hand. The key operations of a Transformer can be collected into Decoder and Encoder blocks, where the main difference is analogous to filtering and smoothing, respectively; whether it is permitted or not for an input to access its successors within a context window in the Attention mapping. Many well known generative Transformers such as LLaMA by Meta AI, touvron2023llama, use a Decoder-only structure. As the idea of those models is to extrapolate the input prompt, only one backwards looking Decoder is needed. In tasks involving time series prediction where the same sequence is extrapolated further, a Decoder-only architecture can also suffice. The Bidirectional Encoder Representations from Transformers (BERT by Google, Devlin2018bert) is an Encoder-only. This structure focuses on distilling information from the whole input sequence at once for the output, like in sentiment analysis or imputing missing values. The original Encoder-Decoder structure by Vaswani2017 was meant for a language translation task, where the whole input sequence in one language can be first analyzed in the Encoder and then the output sequence is generated based on the Encoder's instructions word by word in the Decoder. This architecture was the inspiration for the Transformer presented here.
gruver2023large show that large language models like LLaMA-2 and GPT-3, which have been pre-trained using natural language data, can nonetheless be used as such also for predicting time series data. Google research improved the results by training a Transformer foundation model specifically for time series prediction, with 200 million parameters and 100 gigabytes of pre-training data das2023decoder.
In economics Transformers have been so far mainly applied to univariate problems, like predicting the S&P 500 in Wang2022stock, where the Transformer is found to surpass the LSTM network. Economic multivariate applications include a Transformer for portfolio allocation by lezmi2023time.
There are some other time series Transformers, which also use a dual structure of Encoder and Decoder stacks, such as the Informer by Zhou2021informer. The Encoder is used to compress features from data that help in long range predictions. The number of features is typically much larger than the number of potential factors driving the data. These vector-valued features are not assigned with an interpretation as latent variables or extracted out to be used in themselves as objects of interest. tang2021probabilistic combine a state-space structure with a Transformer network, but yet again, with the aim of improving forecasts instead of estimating scalar latent factors. Additionally, sarapisto2024subsystem use a masked-autoencoder Transformer to cluster variables in a high-dimensional dataset into functional subsystems. They compress the data into latent embeddings which summarize the clustering structure, but not into dynamic factors which summarize the evolution of the system over time. I am not aware of prior work that uses dual-stack Transformers for explicitly estimating dynamic factors as latent real-valued time series variables.
The proposed prior information regularizer relates to the subspace shrinkage literature, which penalizes high-dimensional models typically towards a linear subspace within their parameter space, see for example Shin2020, Huber2023, and Sewell2025. The prior information regularizer can use any (nonlinear) pre-estimated factor model as a subspace prior. To the best of my knowledge, conventional models have not been utilized in this way before to train Transformers.
Recently there have been innovative efforts to make better use of the machine learning approach in macroeconometric modeling. For example, chernis2025bayesian uses Gaussian process priors to estimate time-varying factor loadings with a dynamic factor model. Klieber2024 uses an autoencoder to augment a vector autoregression model with a nonlinear factor. coulombe2022neural estimates latent inflation expectations and output gap using a neural network. hauzenberger2025machine use a Bayesian neural network to estimate nonlinear impulse responses to structural shocks in macro data. My aim is to establish foundations for using Transformers in macroeconomic modelling, with an emphasis on latent-state estimation and interpretable dynamics.
This section presents the Encoder-Encoder Transformer in detail\footnote{I chose a procedural way of explaining the algorithm to reduce the prerequisite knowledge requirements about machine learning and Transformers.}, and points out the differences from other Transformers. An outlook of the architecture is presented in Figure (ref). Subsections (ref)--(ref) describe the forward pass -- how data is transformed into factor estimates and predictions. Subsection (ref) introduces a way to use prior information in the loss function, and Subsection (ref) discusses the process of training the Transformer. Finally, Subsections (ref)--(ref) detail the tools for analyzing the results.
Let a time series dataset consist of $i=1,2,\dots,k$ variables and $t=1,2,\dots,N$ time periods. The data is standardized variable-wise to remove the impact of scale differences, $y_{ti} = \frac{\text{y}_{ti}-\bar {\textbf{y}_i}}{\sigma_{i}}$, with variable-wise mean $\bar {\textbf{y}_i} = \sum_{t=1}^N\frac{\text{y}_{ti}}{N}$ and standard deviation obtained from $\sigma_{i}^2 = \sum_{t=1}^N\frac{(\text{y}_{ti}-\bar {\textbf{y}_i})^2}{N}$. The data is tokenized, embedded, and augmented with positional and variable information. Figure (ref) offers an intuition on the procedure.
Tokenizing partitions a context window $Y_t$ of data into computational units, tokens. Defining what a token is has a large impact on the computational efficiency and interpretability of the results. Here a token is defined as the value of one variable on one time period, $y_{ti} \in \mathbb R$. Hence, the input block $Y_t$ is transposed and vectorized $\text{vec}(Y_t^\prime)$ (flattened) into a vector of $Pk$ tokens.
Embedding is a process of creating initial representations for tokens, which include relevant information about them. These representations are spacious information containers, into which the Transformer can encode more nuanced information, based on the dependencies and conditional patterns uncovered from the data.
Time series Embedding is a linear projection $\mathbb R \rightarrow \mathbb R^{d_m}$ that maps each real number into a unique $d_m \in \mathbb N^{+}$ dimensional vector. Each token is embedded separately using the same one--row weight matrix $W_{\text{Embed}} \in \mathbb R^{1 \times d_m}$
to create a matrix of value--embedded representations for the block of input data, $TE_t \in \mathbb R^{Pk \times d_m}$. The parameters in $W_{\text{Embed}}$ are learned during the training. The elements $z\in \{1,\dots,d_m\}$ of an embedding vector are called features. This step is loosely similar to how natural language data is embedded \footnote{Given a lexicon of Unicode tokens of size N, the input sequence of $P$ tokens is one-hot-encoded into a sparse $P \times N$ matrix Y. This matrix is projected into an embedding space by a dense weight matrix $W^E \in \mathbb R^{N\times d_m}$, where $d_m$ gives the dimensionality of the representations in the embedding space. In large language models, $d_m$ is typically on the order of thousands. It is typical to use a trained embedding projection $W^E$ such as Word2vec by Mikolov2013, which organizes the vector representations of tokens in a way that preserves syntactic and semantic properties.}. It is noteworthy that all value--embedded tokens are initially in the same span in the $d_m$-dimensional representation space. The representations are pushed off from this span by adding positional- and variable encoding information to them.
Positional Encoding vectors $pe_p \in \mathbb R^{d_m}$ for p=$1,\dots,P$ add sequential information to the embeddings. There are no recurrent elements in the Transformer network and it is permutation invariant with respect to the token sequence. For this reason the sequential structure of the data is supplied directly to the embedding vectors to differentiate the meaning of values from different time periods. All $k$ tokens at lag $p$ share the same positional encoding vector $pe_p$, which differs from positional encoding vectors added to tokens at other lags. The values of the elements in these encoding vectors are learned similarly to the embedding weights\footnote{It is also common to insert fixed positional information to the tokens is by using sinusoidal functions, $PE_{t,2i} = \sin(\frac{t}{10000^\frac{2i}{d_m}})$ and $PE_{t,2i+1} = \cos(\frac{t}{10000^\frac{2i}{d_m}})$ for $t=1, \dots, P$ and $i=1,\dots, d_m/2$, with $PE_{t,2i} \in [-1,1]$.}. The positional encoding vectors are collected into a matrix by repeating each positional vector p=$1,\dots,P$ for $k$ times $[pe_1, pe_1, \dots, pe_P]^\prime =: PE_y \in \mathbb R^{Pk \times d_m}$, where the prime stands for a transpose. The positional encoding matrix is added to the embedding matrix.
Variable Encoding vectors $ve_i \in \mathbb R^{d_m}$ for $i=1,\dots, k, k+1$, amend the token representations with information about which variable the token is standing for. There is a unique vector for each of the $k$ observable variables and one for the estimated dynamic factor. Similarly to the positional encoding matrix, the variable encoding vectors are used to construct a matrix $[ve_1, ve_2, \dots, ve_k] =: VE_y \in \mathbb R^{Pk \times d_m}$, this time repeating the cycle of i=$1,\dots, k$ vectors $P$ times. Variable encoding with a separate vector for the factor also conveys the difference between latent and observable variables, when only one factor is estimated. If multiple factors were to be estimated instead of one, then additional binary type encoding could be added to supply additional latency information.
All of the different characteristics are added together to create the initial token representations for the Transformer to analyze
It is useful to scale down the positional, variable and type-encoding matrices with $\gamma<1$ to prevent them from overriding the value embedding information, especially with small variable values. The representations in matrix $Y_t^{\text{emb}}$ will be called as the "embedded token representations" for short.
An initial crude factor estimate is created from the pointwise means of the block of input data, $\hat x_{\text{init}} = mean(Y_t)$, resulting in a vector with $P$ real numbers. The factor representation can also be initialized with the values of one of the observable variables, or with a factor estimate from another model. It is potentially useful to initialize the factor with a different model than what is used as prior information, to encourage exploration.
The initialized factor estimate series is embedded with the same time series embedding projection as the observable variables, $W_{\text{Embed}}$, and augmented with the same position and variable encoding vectors $pe_p, ve_x$ as in equation (ref).
with $X_{\text{init}} \in \mathbb R^{P \times d_m}$. Initializing the factor series before the State Encoder, and treating it as a latent variable, is one of the contributions in this study.
The benefit of initializing the factor estimate right in the beginning is in the enhanced interpretability. The State Encoder's Cross-Attention mechanism now directly describes the construction of the factor estimate. An alternative option would be to compress the factor estimate on an output layer after the State Encoder. In that case the State Encoder's Self-Attention would describe only the construction of intermediate representations for the input tokens, which only later are used to compress the factor. Additionally, as the output compression would combine information from several tokens, it could potentially dilute the information in the self-Attention matrices. Instead, the initial factor estimate is refined in the State Encoder and the output layer merely projects each period's representation of a factor estimate into a real number. The Cross-Attention matrices describe the relative importance of each variable and their lag for the factor estimate.
The raw input representations $Y_t^{\text{emb}}$ are first refined in an initial Encoder, to augment the token representations with cross-dependencies in the data. The Encoder has standard Self-Attention and Feed Forward network modules, with residual connections and normalization steps.
Without an initial Encoder, the Cross-Attention of the State Encoder could not use direct information about the cross-dependencies across input variables and time, to refine the factor representations. Because the initial Encoder is very similar to the State Encoder, the operations will not be presented here in detail, but instead the differences will be pointed out in the next subsection.
There is a stack of $H\in \mathbb N^+$ identical State Encoders, which refine the factor estimate further. The first State Encoder takes in the data $Y_t^{\text{init}}$ and the initial factor estimates $X_{\text{init}}$. The rest of the State Encoders take the refined factor estimate from their predecessor as their input, but use the same data $Y_t^{\text{init}}$. The final State Encoder sends its output to the output layer, equation (ref), which projects each period's factor estimate $\hat x_{t}$ into a real number. Specifying a deeper architecture with multiple subsequent Encoders can help improve the results, especially if there is a lot of data, but it can also make the training process harder. The indices for the different State Encoders in the stack, $j = \{1, 2, \dots, H\}$, are omitted for notational brevity. Also, only one State and Measurement Encoder is used in the Monte Carlo experiment.
The inputs to the State Attention are first normalized token-wise.
Layer Normalization, $\text{Norm}_1(z)=$ $\gamma \frac{z-\bar z}{\sigma_z} + \beta$, standardizes token representations. Each feature z of a token's representation vector is expressed as units of standard deviation from the mean of that vector, and then scaled and shifted, with trained parameters $\gamma, \beta \in \mathbb R$. Each token is normalized separately and the operation is applied to all of its $d_m$ features. The normalization step is a solution to the internal covariance shift problem in layered Transformers, where the distribution of the inputs to the subsequent layers changes during training ioffe2015batch. The scale $\gamma$ and the bias vector $\beta$ are trainable parameters, which re-scale and shift the input representations. ba2016layernorm show that the normalization improves training speed and generalization accuracy. According to Lu2021, $\gamma$ and $\beta$ of a pre-trained Transformer are the most important parameters to tweak during the fine tuning stage. In the original article Vaswani2017 normalization was conducted in the residual stream, after the two operations, Attention and Feed forward network. Xiong2020 argues that the normalization should instead be applied before the operations, for a copy of data read from the residual stream. Especially with subsequent Encoders it is useful to normalize the previous Encoder's output before applying Attention. In the Initial Encoder there is only one input $Y_t^{\text{emb}}$ which is layer normalized. A copy of the un-normalized inputs $X_{\text{init}}$ also bypass the State Attention in the residual stream, to be combined with the output from the Attention later on.
The Attention mechanism is at the heart of the Transformer network. At this step the inputs communicate with each other. Each State Encoder has a Multi-Headed Scaled Dot-Product Cross-Attention module, with h parallel Attention heads. An Attention head first performs three projections, creating Query, Key, and Value representations for the input.\footnote{An information retrieval system ranks the Values (movies) of different options in the database based on the similarity between their Key (description) to the Query (search words). In time series context, for intuition, think of a Query as representing the situation of a certain input, Keys representing what information other inputs have to offer, and Values representing for different interpretations about the future.}.
where each $i=1, \dots, h$ Attention head has separate learned weight matrices $W_i^Q,W_i^K, W_i^V \in \mathbb R^{d_m\times d_k}$. The Transformer learns through training to create the Query, Key and Value representations for the input data in such a way that allows the Attention mechanism to uncover useful information for constructing the series of factor estimates. The Query $Q_i$, Key $K_i$, and Value $V_i$ matrices compress information by representing the data in a lower dimensional vector space, where typically $d_k\approx d_m/h$. The qualifier "Cross" in Cross-Attention means that the Query representation is derived from a different source than the Key and Value representations. Importantly, the initial factor estimate "queries" the observable data, and its refined values are based on the "answers" (keys and values) provided by the data. Projections in the Attention mechanism don't include biases, as the subsequent normalization in equation (ref) will de-mean the representations anyway. The difference to the Initial Encoder's Self-Attention is that there all three representations Q, K and V are derived from the same input $Y_t^{\text{emb}}$, hence the qualifier "Self".
The following is the most important equation of a Transformer. Using the three representations, the head i calculates a Scaled Dot-Product Attention
The matrix multiplication $Q_i K_i^\prime$ measures the cosine similarity between the Query and Key representations of input tokens, where the prime denotes a transpose. The j$^{\text{th}}$ row of the outcome signifies the relevance of other k*P tokens for the j$^{\text{th}}$ token. These scores are normalized with the dimensionality of the Key representation, $\frac{1}{\sqrt{d_k}}$, to help avoid the consequent Softmax function from converging towards a max function, with large choices of $d_k$. The vector-valued function, Softmax($z_j$) $=e^{z_j} / \sum_{h=1}^{k*P} e^{z_h}$, is then applied to each row, enforcing the rows of the Attention matrix to sum up to unity.
The matrix of Attention scores, $\text{Softmax}\left( \frac{Q_i K_i^\prime}{\sqrt{d_k}} \right)$, is the central tool for interpreting the results of a Transformer. The elements on row j of the matrix are weights for how to construct a new representation for token j, from alternative options presented in the Value matrix $V_i$. The output from the Attention head, matrix $\text{head}_i \in \mathbb R^{Pk \times d_k}$, consists of updated representations for the factor estimates, based on the contextual information of how they relate to the input tokens.
In other words, the Attention scores convey how much attention was paid to each variable's every lag when creating the factor estimate. Notice that the matrix for State Attention scores is not a square, $\text{Softmax}\left( \frac{Q_i K_i^\prime}{\sqrt{d_k}} \right) \in \mathbb R^{P\times Pk}$, due to $Y_0$ and $X_0$ having different number of rows. Subsection (ref) shows how the Attention scores can be used to create Attention matrices and how to use them to interpret the results.
Multi-Head Attention concatenates the outputs of each h parallel Attention heads and combines their information with a linear projection
where the projection $W^O \in \mathbb R^{h*d_k\times d_m}$ retains the model dimensionality $d_m$ for the new representation matrix $X_1\in \mathbb R^{P \times d_m}$ of the factor estimate.
It is important to have separate Attention heads for two reasons. They learn to focus on uncovering different relations between the inputs, and they allow for further parallelization to improve training speed. With time series data the heads can for example specialize in observing different frequencies, conditional patterns, rare events or structural breaks. The original Transformer article by Vaswani2017 used eight parallel Attention heads, but Voita2019 suggests that more than three might be redundant. Also Cordonnier2021 finds that different heads independently tend to focus on the same representational subspaces, making them redundant.
Output of the Multi-Head Attention is added back to the residual stream. Dropout de-activates a fraction of the features randomly.
It is useful to think of the input representations of the data as flowing through the Transformer in a (residual) stream. Information is read from and written back into the residual stream during operations such as the Attention. Instead of overriding the previous representations with the new ones, the representations are refined, encoding them with new information. Dong2021a finds that without the residual stream, the Transformer's output degenerates to a rank 1 matrix, creating a uniformity between token representations. Residual stream is also called a skip connection, as a copy of the token representations skip an operation. Dropout is applied directly to the input representations in Transformers. Dropout is only applied during training to help avoid overfitting, but disabled during inference.
The representation $X_2$ is normalized for the second time, similarly as in equation (ref), but with different parameters $\gamma$ and $\beta$.
A Feed Forward Network (FFN) processes each token representation in $X_3$ individually, through a fully connected expand and contract network with a GELU activation on the first layer
with $W_1^{ff}\in \mathbb R^{d_m\times d_{ff}}$ and $W_2^{ff}\in \mathbb R^{d_{ff}\times d_m}$, keeping the output dimensions the same as the input, $X_3,X_4\in \mathbb R^{P\times d_m}$, but expanding in the middle, typically $d_{ff}\approx 4*d_m$. The biases have dimensions $b_1\in \mathbb R^{1\times d_{ff}}$ and $b_2\in \mathbb R^{1\times d_{m}}$. The Gaussian Error Linear Units (GELU) activation function is applied elementwise to introduce nonlinearities, by $GELU(x)=x \Phi(x)$, where $\Phi$ is the Gaussian CDF. The GELU activation was proposed by hendrycks2016gelu, who find it to improve performance compared to the popular alternative Rectified Linear Units (ReLU) activation, $ReLU(x)=\max\{0, x\}$. The FFN is called the computation step, in contrast to the communication step of the Attention mechanism, because in FFN each token is processed independently.
After the FFN the output is subjected to another dropout and added back to the residual stream (second skip connection)
The output $X_5$ is sent to the next State Encoder in the stack as an input. From the last State Encoder the output is sent to the output layer.
The factor estimate is created from the output $X_5$ of the last State Encoder, with a linear projection
with matrix $W_{\text{factor}} \in \mathbb R^{d_m \times 1}$ producing the local P-lag time series for the dynamic factor $\hat x_{t} \in \mathbb R^{P \times 1}$. The projection $W_{\text{factor}}$ compresses a $d_m$-element vector representation of one factor at one time period into a real number. The projection is done separately for each lag position in the context window, $\{t-P, \dots, t\}$. Factor estimate series $\hat x_{t}$, and particularly its most recent value, is the main output of interest from the Transformer proposed in this study. The benefit of only conducting a simple representation-wise output projection is that most of the analysis is left to be done by the operations in the Encoder. The aim is to maximize interpretability of results with Attention matrices, and therefore it is useful to prevent the Transformer from relying too heavily on other means of combining information across inputs, such as a pooling output layer.
The loss function uses the distance between $\hat x_{t}$ and the factor estimate from the Kalman filter (or any other factor model), $\hat x_{t}^{\text{Kalman}}$, as prior information regularizer, equation (ref), to guide the training.
The factor estimate $\hat x_{\text{t}}$ is re-embedded and augmented with the same positional and variable information as the input data in equation (ref), before being sent to the Measurement Encoders as
resulting in the matrix of re-embedded representations $\hat X_{\text{t}} \in \mathbb R^{P \times d_m}$ for the final factor estimates.
The key idea is to compress the factor representation all the way down to one real number per lag position, interpret it as a latent variable, and then re-embed it again to be used as Keys and Values in the Measurement Encoder's Attention. This conditions the factor estimate $\hat x_{t}$ with data during training, while posing minimal strict identifying assumptions for the functional form of the implicit state space system.
The first Measurement Encoder receives the same input data $Y_t^{\text{init}}$ as the State Encoder, and the re-embedded factor representations $\hat X_{\text{t}}$ coming out of the State Encoder stack. The State Encoders and Measurement Encoders have separate weight matrices and operations, but stack-specific indices are omitted for simplicity. A Measurement Encoder conducts separate Layer Normalizations for the data and for the factor.
There are two differences in the Measurement Encoder's Cross-Attention mechanism, compared to that of the State Encoder. The Query matrix is obtained from the input data $Y_0$, but the Key and Value matrices are constructed from the factor representation $\hat X_0$.
where $W_i^Q, W_i^V, W_i^K \in \mathbb R^{d_m\times d_k}$ are otherwise similar to the State Encoder. There is only one Cross-Attention module in the Measurement Encoder, without an initial Self-Attention module as in Vaswani2017, as the input representations have already been refined in the Initial Encoder's self-Attention.
The rest of the steps in the Attention module are mostly identical to the State Encoder
Masking is not needed in the Measurement Encoder's Attention, because only the fully backwards-looking last period's prediction will be used in the loss function.\footnote{If a measurement Decoder was specified instead with teacher forcing, the Attention scores would have to be censored with an upper-block-diagonal mask of large negative values, with zeros elsewhere. The mask has to be block-recursive due to the granular definition of a token.} Importantly, the residual stream passes only through the Attention module with no skip connection, in equation (ref). This is to enforce the key information bottleneck of the Transformer: The predictions for the observable variables must rely on the factor estimate via the cross-Attention with no bypass. Like the State Attention, the Measurement Attention matrix is also not a square, $\text{Softmax}\left( \frac{Q_i K_i^\prime}{\sqrt{d_k}} \right) \in \mathbb R^{Pk\times P}$.
This is followed by the second Layer Normalization, Feed Forward Network, and this time a standard skip connection
The Measurement Encoder's output $Y_5$ becomes an input for the next Measurement Encoder in the stack. Every subsequent Measurement Encoder derives its Queries from the previous Encoder's output, but uses the same factor representation $\hat X_{\text{t}}$ to get Keys and Values. Last Measurement Encoder's output $Y_5$ is sent to the output layer of the Measurement Encoder stack to create predictions for the next period values of the observable variables.
with $W_{\text{predict}} \in \mathbb R^{d_m \times 1}$, producing the prediction output matrix $\hat Y_{\text{t+1}} \in \mathbb R^{Pk \times 1}$. Each real number is a next period's predicted value for the corresponding input token. Similarly to the State Encoder's output projection, also here the output projection only compresses individual token representations into real numbers. This forces the Transformer to rely more on the Measurement Attention to identify dependencies between the inputs and the factor.
Only the last $k$ values of $\hat Y_{\text{t+1}}$ are saved in a k-value vector $\hat y_{\text{t+1}} \in \mathbb R^k$. These values are the t+1 period predictions for the $k$ observable variables. These predictions are based on the full $P$ lag information set and only include past information. The prediction errors between $\hat y_{\text{t+1}}$ and the real values $y_{t+1}$ are used in the loss function, equation (ref), as the main objective for the training.
The loss function used for training the Transformer is comprised of two main parts, Prior Information and Prediction Error
where
and $\lambda \in [0,1]$ controls for the relative weight of prior information to the prediction errors in training. The loss function measures Mean Absolute Errors (MAE). MAE loss is less sensitive to non-Gaussian errors, which are very typical in macro data, than the Mean Squared Error (MSE) loss function.\footnote{MAE loss is optimal for random variables that follow the Laplace distribution, $f(x|\mu, b)=\frac{1}{2b}e^{-\frac{|x-\mu|}{b}}$, where $\mu\in \mathbb R$ is the location parameter and $b>0$ is the scale parameter. The loss function can be upgraded to also estimate b as a measure of uncertainty and to discount large deviations if they occur under heightened uncertainty.}
Prior Information, PI$_t$, is the proposed new regularization term, which minimizes the distance between the factor estimates from the Transformer and the Kalman filter. The Transformer's factor estimate $\hat x_{T}\in\mathbb R^P$ is the output from the State Encoder, equation (ref), and $\hat x_{\text{K}}\in \mathbb R^P$ is the factor estimate generated with the Kalman filter, as outlined in Subsection (ref). Prior information pulls the training process towards a reasonable direction in the Transformer's parameter space. This helps the Transformer avoid getting stuck in a bad local optimum. The simulated real factor is never observed at any stage of the training. Any factor model, other than the Kalman filter, can also be easily plugged in as prior information.
Prediction Errors, PE$_t$, measure the distance between the observable variables and the Transformer's next token predictions $\hat y_{t+1}\in \mathbb R^k$, coming from the Measurement Encoder in equation (ref).
The objective in minimizing the loss function is to improve the accuracy of the Transformer's factor estimates. Whether reducing the loss actually corresponds to factor accuracy can be studied using simulated data, where the true factor is known. During each epoch, both the loss and an accuracy metric is documented. A particular metric Fit is introduced in equation (ref), but other metrics can also be used. Figure (ref) regresses the resulting factor accuracies against the accompanying validation losses. Negative regression coefficients indicate that the Transformer improves the accuracy in training, as the loss diminishes.
The data is divided into non-overlapping training, validation and test sets across the time dimension. This division has to be done with care when dealing with real-world macroeconomic time series data, to get representative samples to each set.
Training data is used for updating parameters, validation data for monitoring how well the results generalize out-of-sample during the training and test set for analyzing the out-of-sample results after the training has concluded. When the Transformer starts to overfit the training data, the prediction error in the validation loss begins to increase.
The Transformer processes the data in blocks $Y_t\in\mathbb R^{P\times k}$, where the number of lags $P\leq N$ defines the context window, or information set, of time periods $\{t-P, \ t-P+1, \ \dots, \ t\}$. The time index $t$ denotes the most recent time period in the context window $Y_t$.
For each of the sets, a rolling window with a stride of one is applied to create input matrices with all possible P-lag information sets. However, there is no overlap between training, validation, and test sets. Figure (ref) illustrates the outcome. These matrices are also called training examples and their size corresponds to the context window of the Transformer. Each matrix consists of P+1 consecutive time periods, where the first $P$ periods are used to predict the most recent time period. Because only the last period's predictions for the observable variables are used in the loss function, it is important to use these partially overlapping context windows in training.
The training examples are further grouped into batches, each consisting of several P-lag training examples, $\{Y_{1}^{\text{emb}}, Y_{2}^{\text{emb}}, \dots, Y_{\text{batchsize}}^{\text{emb}}\}$. All training examples in one batch are processed in parallel through the Transformer. The context windows in the validation set are treated as one big batch -- same for the test set.
The Transformer calculates the loss for each training example in a batch according to equation (ref). The average loss across a batch is backpropagated to the Transformer's parameters. The gradients are calculated using the automatic differentiation tools of Pytorch. After all of the training data has been processed, Transformer's performance is evaluated on the validation data without updating parameters, after which an epoch concludes. Those parameter values are retrospectively chosen for the Transformer, after the training has concluded, which resulted in the lowest validation loss during the training. There is a risk that the Transformer found a way to overfit the training data in a way that also happens to overfit the validation data. For this reason there is also a separate test data which is analyzed only after the training process has concluded.
Local filtering in parallel. The series of factor estimates for the $N^{\text{test}}$ period test set are created in parallel. Each P-lag context window is processed in parallel, and only the last period's factor estimate from each of them is appended into $\hat x^{(i)} \in \mathbb R^{N^{\text{test}}}$. This sequence is the outcome from one training run i=$1,\dots,r$ of the Transformer, used in the Monte Carlo in Section (ref).
The Transformer is trained multiple times for the same data to counter the path dependent impact of random parameter initialization. The actual series of estimated factor values $\hat x$ is constructed from the pointwise averages over the runs. Model averaging evens out some of the biases of individual models. All individual factor estimates are also included in the background of the visualizations in thin lines. The consistency of the results is proxied by the standard deviation of the accuracy of the model average $\hat x$ across different seeds of the same process. The within-seed variation between training runs is also a measure of uncertainty of the factor value.
Factor identification, the scale and sign of the factor, cannot be identified from the data alone. Because the factor describes relative changes, the scale is not important. To enable comparison, the Transformer's factor estimates can be scaled to best match the estimated series from the model under comparison. The sign of the factor can be identified with narratives based on the data. For example, a coincident index for the business cycle should decline during historic recessions.
Real time factor projections extend the factor estimate over the next n time periods with only current information at hand. The forward pass is repeated for n times recursively, each time adding the previous predictions $\hat y_{\text{t+1}}$ to the end of the input data, and saving the last factor estimate $\hat x_{t}$ from each round. This procedure is conducted separately for each time period in the test set, creating the real time predictions. The resulting "tentacle plot" was inspired by a similar type of plot in Diebold2020tentacle, but instead of looking backwards, here the factor estimates are extended into the future. The benefit of looking forward is that it is possible to see what kind of dynamics the Transformer has learned for the state-transition. The real time projections use one trained Transformer, so the results may deviate from the average of training runs.
The matrix of State Attention scores, $\text{Softmax}\left( \frac{Q_i K_i^\prime}{\sqrt{d_k}} \right) \in \mathbb R^{P \times Pk}$, equation (ref), is used to interpret how important different variables and their lags were in the process of refining the Transformer's factor estimate. As the Transformer is not restricted only to search for constant or linear relations, the impact of different inputs can change through time. Using the Attention scores, it is possible to analyze which variables and their lags were paid most attention to at any given moment. The last row of the Attention score matrix describes the most recent factor estimate (period $t$ in the context window), with an Attention distribution over the $k$ variables and their $P$ lags summing up to unity.
State Attention matrix organizes these last-row Attention scores into a $P\times k$ matrix, where each column corresponds to P-lags of one input variable. The State Attention matrix is visualized with a heatmap, Figure (ref), where darker colors reflect higher importance of the corresponding variable's lag. Only the Attention scores from the last State Encoder in the stack are considered. The scores are averaged over the Attention heads for simplicity, although more information could be obtained by observing each of the heads separately. The whole $P \times k$ matrix describes the construction of one real number -- the factor estimate at time t.
All of the visualizations in this subsection are examples of a process with k=5 observable variables and P=9 lags.
Using the State Attention matrix, I construct new time series specific visualizations, which show how the Attention patterns change over time. The columns of the State Attention matrix are summed to get a vector of variable contributions. This vector describes the relative importance of the variables at time t. These variable contributions can be obtained similarly for each time period in the test set. Visualizing these contributions as time series for each variable helps to analyze how the importance of different variables change over time, Figure (ref). Changes in the relative importance of suitable macroeconomic variables can potentially help to evaluate whether a sudden change is a demand or a supply shock.
Similarly, the State Attention matrices for each time period can be summed row-wise into lag contributions, Figure (ref). Changes in the allocation of Attention over lags can inform about important events in time. When a regime switch or a large shock occurs in the factor process, the Attention to most recent lag position spikes. The same influential moment can still remains useful in the near future for constructing the factor estimate and predicting the observables. In that case the Attention to each of the lags elevate one after another as time moves on. This also helps to detect heavy-tailed stochasticity in the state-transition shocks, which causes these spike patterns to emerge in the Attention plot at uneven intervals. Gaussian shocks on the other hand rarely cause such disruptions. The Attention patterns over lags can potentially also signal about increased uncertainty for example during a transition between steady states, where the Attention to more recent information increases permanently, or the volatility of the pattern increases.
Measurement Attention matrix is composed of the last $k$ rows of the Measurement Attention score matrix in equation (ref). These rows include the $k$ next period predictions for the period $t+1$, after the context window. The matrix is transposed to preserve a similar $P\times k$ look as with the State Attention matrix. However, the interpretation is different, in that each column i of the Measurement Attention matrix corresponds to a prediction for a different variable $\hat y_{i,t+1}$, for $i=1,\dots,k$. The Measurement Attention matrix is used to evaluate how relevant each lag of the factor estimate was for the predictions of the observable variables. The matrix conveys information about the temporal factor loadings.
By summing the rows of the Measurement Attention matrices over time, it is possible to describe how the relevance of factor lags changes over time, Figure (ref). The analysis of these patterns can reveal similar information as the State Attention patterns over lags, explained above.
Attention patterns can be smoothed to highlight the signal from noise. The ragged State Attention patterns $a_{it}$ for variables $i=1,\dots,5$ on the left panel of Figure (ref) have been processed through an exponential smoother, $\tilde a_{it} = \alpha a_{it} + (1-\alpha)b_{t-1}$, where $b_0=a_{i0}$ and $\alpha=0.1$. The starting point bias of the smoother can be overcome by smoothing both ways and taking the average. It can be easier to observe long term changes in the Attention patterns with smoothed curves in the panel on the right. However, smoothing also loses some information about local attention patterns and volatility changes in those patterns, so it will not be used in this study.
There is a caveat for interpreting results with Attention matrices. The factor estimate is constructed in the Transformer during several subsequent stages, only one of which is the Attention mechanism. This means that the impact of the Attention on the factor estimate cannot be completely isolated. However, the Attention is the only operation, where different inputs can directly impact each other, so it is plausible that what is found important in the Attention mechanism describes also the end result of the respective Encoder stack. The numeric values of the Attention matrix are also not to be interpreted as coefficients, as they do not reflect the sign or magnitude of the impact channel between the factor and the observable input, only its relative importance in that moment.
The residual stream refers to the sequence of representations for the factor estimate in one forward pass of the Transformer's State Encoder. Operations, such as the Attention and the Feed Forward Network, read information from the residual stream and write back into it, updating the representations. By visualizing the representations from different stages of the residual stream, it is possible to analyze how the Attention and other operations impact the factor estimate. To do this, I map all of the intermediate representations described in Table (ref), through the output projection $W_{\text{factor}}$ from equation (ref). For the initial representation $X_{init}$, this means bypassing the whole Encoder machinery, whereas the output from FFN corresponds to the final estimate. Figure (ref) visualizes all of the intermediate representations and Figure (ref) focuses on the two key operations, Attention and Feed Forward, scaled based on the true factor for comparison.
In this section, the performance of the Transformer is evaluated on several simulated datasets against a Kalman filter baseline model, and an oracle particle filter. The datasets are designed to impose a series of different accumulating challenges, while remaining somewhat representative of typical macroeconomic data. The functional form and parameterization of the underlying state-space processes deviate from linear--Gaussian, but not excessively so. The results from the Monte Carlo are summarized in Table (ref), and Figure (ref), demonstrating that the Transformer can outperform the Kalman filter baseline when the process deviates from linear--Gaussian. This section presents the benchmark models, evaluation criteria, Transformer hyperparameters, data generating processes, and finally the results.
The Kalman filter is purposefully misspecified for the datasets. If the conventional baseline model matched the data generating process perfectly, it would be an "oracle" and the Transformer couldn't consistently outperform it. Furthermore, if we knew how to specify perfect models for real-world tasks, we would not need Transformers to begin with. Instead, the Monte Carlo is a simplified representation of the real-world situation, where all models are misspecified to a different degree. The Monte Carlo study explores whether a somewhat misspecified baseline model can guide an overparameterized Transformer, so that the Transformer not only performs reasonably but also surpasses the baseline in accuracy. An "oracle filter" with a functional form matching the underlying process and true parameters is used to evaluate how much of the explanatory gap between the Kalman filter and the oracle can the Transformer cover.
The Monte Carlo results should not be interpreted as conclusive evidence of the Transformer's general performance on arbitrary nonlinear datasets. Rather, the Monte Carlo showcases some particular interesting cases where the Transformer can improve upon the accuracy of a factor series estimated with a Kalman filter.
The benchmarks include a Kalman filter with and without the estimation step, and an oracle particle filter with the correct functional form and stochasticity for each process, the simple piecewise mean of observable variables, and finally a Transformer which "peeks under the hood" to stop the training optimally. Only the estimated Kalman filter and oracle filter are visualized alongside with the Transformer and the true factor, to keep the figures readable.
Kalman filtering procedure, proposed by kalman1960new, is an optimal way to filter the latent dynamic factor(s), when the underlying process is fully linear and Gaussian. All but one of the processes violate either the linearity or the Gaussian assumption. The following linear state space model is estimated for all of the datasets with the Kalman filter, with a state-transition equation for the dynamic factor $x_t\in\mathbb R$
and a measurement equation for the i=$1,\dots,k$ observable variables
The maximum likelihood estimation is conducted on the combined training and validation data, totaling 800 time periods, to estimate $\mu_x, \mu_y \in \mathbb R$, $\alpha \in [0,1]$, $\sigma_x \in \mathbb R^+$, $\beta \in \mathbb R^{k}$, and a symmetric and positive semi-definite measurement error covariance $R \in \mathbb R^{k \times k}$. The Kalman filter is then used with the estimated parameters to obtain the factor estimate $\hat x_K$ on the 1000 period test data. For simplicity, the results from this estimation procedure will be referred to as the factor estimate by the "Kalman filter" or just "Kalman". Additionally to the estimated Kalman results, also the results from the Kalman filter with true linear parameters are reported with a shorthand "Kalman max".
The estimation of the Kalman filter benchmark is conducted in R using the 'kalmanfilter' package. For a detailed description of the Kalman filtering procedure, see for example sarkka2023bayesian.\\
Oracle particle filter represents the best possible situation, where the optimal factor estimate is constructed using the true data generating process with the correct parameters. For the linear--Gaussian process 1 the Kalman filter is the oracle. For the rest of the processes the oracle is the auxiliary particle filter, in pseudocode:
For more information, see for example DoucetJohansen2011. See fernandez2005estimating for an example on using the particle filter in economic modeling.
The performance of the Transformer is evaluated using several metrics.
In order to make the results comparable, all factor estimates are first oriented and scaled optimally based on the true latent factor series. This is done by minimizing the mean absolute errors with the true factor
where $\tilde x_{t,model}$ is the raw output from a factor model, model $\in$ \{Transformer, Kalman, Oracle\}. Mean Absolute Errors match the loss function, and the assumption of non-Gaussian data. The optimal scaling parameters $\gamma_0, \gamma_1$ are calculated on the combined training and validation data of 800 periods. These parameters are then used to scale the factor estimate of the model in the 1000 period test set.
Mean squared errors $\text{MSE}_{\text{model}} = \sum_{t=1}^{N^{test}} \frac{(\hat x_{model,t} - x_{t})^2}{N^{\text{test}}}$ are used to calculate further metrics, such as the correlation coefficient $R^2$.
$R^2$ describes how much of the variation of the factor a model can explain. The rest of the metrics are relative to a baseline model.
Fit is a comparative metric of accuracy, defined as the relative change in explanatory power, when using the Transformer instead of the baseline Kalman filter.
where $\hat x_T$ is the average of Transformer's factor estimates from equation (ref) over r training runs with different initial parameters. Kalman filter's factor estimate is denoted by $\hat x_K$. Best possible result is $100\%$, meaning that the Transformer estimated the factor series perfectly. This is infeasible due to stochasticity. Fit of 0% means a tie and a negative value indicates that the Kalman filter was more accurate. The Fit is calculated only for the test sets of each process. The standard deviation in Fit across seeds is considered as a measure of consistency of the results.
Two different Fit scores are reported for the Transformer, one attainable with real data and one currently unattainable. The realistic result comes from early stopping the training based on validation loss, and the best achievable result comes from early stopping based on test Fit. The previous result is reported under "Fit" and the latter under "Fit max".
Gain describes the share of the explanatory gap between the Kalman baseline and the oracle filter, which the Transformer manages to cover up.
with a similar interpretation of the number as with Fit.
Correlation measures the scale- and shift-invariant linear alignment between the factor and the factor estimate, $corr(x, \hat x)=\frac{cov(x, \hat x)}{sd(x) sd(\hat x)}$. Also the validation loss is reported.
Further analysis could be done to evaluate whether the prediction accuracies differ meaningfully, for example by using the Diebold-Mariano test diebold2002comparing.
Hyperparameters govern the Transformer's complexity and training process, and they can have a large impact on the outcome. Hyperparameters can be tuned for each dataset using a search algorithm to improve performance, with some added complications. Only one set of hyperparameters is used for all processes for simplicity. The hyperparameters were chosen while keeping an eye on the accuracy of the factor estimate. Further work is needed to create a tuning protocol based only on validation loss for real data, where the accuracy is unobservable.
I chose a modest and simple, but reasonably powerful size for the Transformer. The recent double descent literature highlights the benefits of large overparameterized models nakkiran2021deep. Even though the Transformer with 26 000 parameters is overparameterized for the training data, it is orders of magnitude smaller than what is typical. The length of the context window is 9 lags, corresponding to three quarters of a year with monthly data, being long enough to observe short term dependencies but short enough to enable using granular tokens, which improve interpretability. The batch size is set to a small value of 32, to not miss rare events which reveal important information about the processes. With a large batch size the average gradient can lose nuances.
The weights are initialized using the Glorot2010 scheme. Learning rates are adjusted during training for each parameter separately with standard Adaptive Moment (AdamW) optimizer with a decoupled weight decay. Learning rate scheduler conducts a linear warmup followed by a cosine decay in cycles of $T_0=100$ epochs, changing the maximum allowed learning rate to avoid rushing past a good local optimum or getting stuck in a bad one. The linear warmup dampens the initial large gradients, to give the optimizer time to calibrate the momentum estimates for reasonable parameter updates. This helps to prevent off-shooting a good local optimum or getting stuck in a bad local optimum. Restarting the cycle gives the Transformer a possibility to continue improving, with early stopping patience set to the length of one cycle.
Overfitting is reduced with dropout and L2 regularization. Dropout is a form of model averaging, which disables a fraction of the parameters during each training iteration to prevent the Transformer from relying on memorizing idiosyncrasies with individual parameters. The L2 regularization penalizes excess model complexity, similarly to ridge regression when used with AdamW. Both dropout and regularization are used sparingly, in order not to waste too much information in the small datasets.
There are six processes, which are thematically organized into three groups. Each process is used to generate 10 datasets, where only the random shocks and errors change between the 10 seeds.\footnote{Out of the 10 seeds, 0-2 were discarded per process due to the Kalman filter baseline being clearly mis-estimated, with a flatlining optimally scaled factor series. The reported results are means over the 8-10 reasonable seeds per process.} The Transformer is trained 10 times for each seed, each time with different randomly initialized parameters. The Transformer's accuracy for one seed is calculated from the mean of the 10 runs.
All of the six processes have one state variable (dynamic factor) and k=5 observable variables. The training data consists of N=800 observations, of which 20% are used for validation.\footnote{All datasets are simulated with R, with a burn-in of the first 1000 observations. Each dataset is stationary, non-cyclical and standardized. All observable variables have about the same signal-to-noise ratio. The level of nonlinearity and heavy tails were chosen to make the estimated Kalman filter baseline miss but not completely off.} The small amount of training data was chosen to reflect the typical situation with scarce macroeconomic data, corresponding to monthly observations over 65 years. The trained Transformer is evaluated on a test data of 1000 periods to guarantee that the results generalize. All of the reported statistics are calculated only from the test set, which was not involved in the parameter updates during training.
Sign preserving power function (spow) is used to introduce nonlinearities to all of the processes
Spow is just a power function $z^\gamma$ for positive inputs, but with capability of dealing also with negative inputs z. The scale c controls the exposure of the input to the nonlinear regions where the slope changes fast, making it possible to calibrate the level of challenge for each process.\footnote{For computational stability, the spow is smoothed with $\epsilon=0.0001$, to remove a kink in the derivative at 0, with $spow(z, \gamma, c) = \ c_{}* \ \text{sign}(z) \left[ (\text{abs}(\frac{z}{c_{}}) + \epsilon)^{\gamma_{}} - \epsilon^{\gamma_{}} \right ]$.}
Parameters. State-transition is defined to be persistent with $\alpha=0.96$ to reflect typical macro data. State curvature is set with $\phi=0.8$ spow-exponent. Constant $\mu_x$ is set to zero so that the state travels across the nonlinear areas of the spow-function, without needing to use mean reverting structure for the state-space. The state is initialized as $x_0=0$. The linear coefficients related to measurements were drawn randomly from $\beta_i \sim \mathbb U(0.3, 1.7)$ and the exponents from $\gamma_i \sim \mathbb U(0.6, 1.4), i=1,\dots,k$, with $\mu_{yi} \sim \mathbb U(-1,1)$. Drawing only positive linear coefficients makes the system co-monotone, which enables using the mean of data to initialize the Transformer's factor series in the beginning of the State Encoder. The choice of having only positive $\beta$-coefficients is not very restrictive. In many cases a system with mixed negative and positive loadings of monotone observable variables can be transformed into a co-monotone system by re-orienting signs. However, it is noteworthy that the mean of the data has a relatively high correlation coefficient $R^2$ with the true factor in many of the processes, which might add positive bias to the results.
The measurement error covariance matrix
is constructed from a vector of standard deviations $D=\text{diag}(\sigma_{y1}, \dots, \sigma_{yk})$ and a random correlation matrix R=($p_{i,j}$) with a unit diagonal $p_{i,i}=1$, and 0.3 average cross-correlation and 0.15 jitter off-diagonal $p_{i,j}, i\neq j$. The error covariance matrix is positive semi-definite with serially uncorrelated errors which are independent of the state shock. The volatility for the measurement error $\sigma_{y,i}$ of variable $i=1,\dots,k$ is chosen to enforce a uniform signal-to-noise ratio over the variables. The measurement error covariance $\Sigma_y$ is constructed separately for each of the six processes with process specific D and a random R.
The results are summarized in Table (ref). The reported values for a process are averages over seeds, with standard deviations in brackets. Figure (ref) shows a 30-period test set snapshot of factor estimates from each process with one seed. The value for one seed is an average over the training runs with different initial parameters. Additionally, processes 2, 4, and 6, are looked upon more closely in Subsubsections (ref), (ref) and (ref), where I chose one representative seed and visualize the data, factor estimates, real time factor projections, training process, residual stream, and Attention patterns. All of the 1000-period test set factor estimates can be found in the Appendix with zoomable vector graphics, alongside with information about the training process. The Appendix also holds 20-period look-ahead prediction plots for the observable variables of processes 2, 4 and 6. These visualizations can be used to assess what kind of dynamics the Transformer learns for the measurement equation. Table (ref) in the Appendix documents the results from all seeds.
Process 1
is linear--Gaussian in both state-transition and measurements.
Process 2
has linear--Gaussian state-transition, but nonlinear and non-Gaussian measurements. Error vector $u_{t} = (u_{1t}, \dots, u_{kt})^\prime$, $f(\cdot)$ is the spow function described in equation (ref) and the p.s.d. measurement error covariance matrix $\Sigma_y$ is defined in equation (ref).\footnote{Randomly drawn parameters used in simulating Process 1 are $\mu_y$: c(0.11, 0.61, 0.70, -0.74, 0.65) and $\beta$: c(1.01, 1.25, 0.60, 0.98, 0.91), while Process 2 has $\mu_y$ = c(0.79, -0.47, -0.256, 0.146, 0.82), $\beta$ c(0.58, 1.56, 1.62, 1.23, 1.18), $\gamma$ = c(0.55, 1.37, 0.57, 1.48, 0.61), $c_y=0.77$.}
Table (ref) summarizes the results from comparison to the estimated Kalman filter on one seed. As expected, the Transformer clearly loses to the estimated Kalman filter with the linear--Gaussian Process 1. It should not be possible for the Transformer to outperform the Kalman filter when the filtering is conducted with true parameters without estimation. Indeed, the Transformer did not exceed Kalman max once even when the early stopping was executed based on test Fit. However, the estimation process with Kalman filter can still sometimes create inadequate factor estimates due to small sample noise, persistent factor and a sensitive scaling procedure. The Transformer won the estimated Kalman once out of 10 seeds, with 5% better accuracy. The Transformer's performance can be easily improved on process 1 by increasing the weight on prior information, as can be seen in Subsection (ref).
In Process 2 the Transformer managed to outperform the estimated Kalman filter by 25% (Fit), explaining about 32% (Gain) of what is left to explain between the Kalman filter and the oracle particle filter.
Table (ref) compares the explanatory power between benchmark models, in terms of R$^2$. In Process 1 the oracle particle filter, estimated Kalman filter and the Kalman max all reached the same result of $R^2= 0.82$ with the chosen representative seed. Using a different early stopping criterion did not make much difference for the Transformer either. In Process 2 the gap is substantial between the oracle and the two Kalman filters. Also the gap between the two Transformers with different early stopping criteria grew similarly.
The results for Process 2 are analyzed further by focusing on a representative segment from the middle of the test set. Figure (ref) shows that the Transformer's factor estimate moves quite similarly to the Kalman filter, but deviates constantly towards the true factor. The variation across training runs is decently small, depicted in thin blue lines.
The 20-periods-ahead real time factor projections in panel (a) of Figure (ref) use the Transformer with best Fit across training runs, and therefore the results deviate somewhat from the average factor estimate depicted in panel (b). In applications where Fit is unobservable, a random training run or the run with lowest validation loss could be chosen instead. The factor projections mostly predict convergence towards the mean, which is appropriate for a stationary and simple underlying state-transition. The projections start to predict an upward trend by the end of the visualized segment, when the estimated factor series has multiple subsequent positive values. Real time predictions for the observable variables can be found in the Appendix, Figure (ref). The Transformer is modified for estimating factors, and the predictions come as a side effect, but they can still be useful for deducing what kind of dynamics the Transformer has found for the measurements.
Figure (ref) visualizes the residual stream in panel (a). In this case the Attention (Attn) and FFN ($\hat x$) steps don't dramatically change the shape of the initial factor series, but there are some changes beyond scaling and shifting. Panel (b) shows that the Transformer learns more accurate representations for the factor estimate through training. On most training runs the lowering validation loss corresponds to a higher Fit, as can be seen from the declining regression lines between loss and fit. The few runs where the regression line is inclining got likely stuck in some bad local optima. The Y-axis is standardized for the plot. Further aspects of the training process are visualized in Figure (ref) in the Appendix.
Figure (ref) prints out the State- and Measurement Attention matrices, describing the factor estimate on period 550 and the predictions for period 551. The State Attention matrix seems to regard variables two and four most important at that period. Higher attention to some of the lags further in the past indicate that an important moment occurred seven or eight periods ago. Seemingly, this moment was when the factor began a strong incline from a low starting point, as an aftermath of several positive shocks. It is possible that variables two and four delivered stronger local information about the shocks than the rest of the variables in that moment, based on how their individual measurement curvature reacts to the location of the state x via the measurement link, sign preserving power function. The Measurement Attention uses the whole factor series in predicting the observables, with more weight on the recent values.
Figure (ref) combines snapshots from every period to visualize how the Attention patterns change over time. The construction of these figures from Attention matrices is explained in Subsection (ref). Panel (a) shows the relative importance of different variables for the factor estimate. The importance of variables 2 and 4 develop similarly from low to high over the time span. This is likely because they both have a similar curvature governed by an exponent larger than one, $\gamma_2=1.37$ and $\gamma_4=1.48$, with respect to the factor. With the sign preserving power function this means that they convey more information about the factor when the factor's value deviates further from zero. Panel (b) shows the importance of different lags over time. The Attention pattern seems to vary a lot locally, but the pattern is actually quite stable in that only relatively small changes occur between the lag weights. The reason for this is probably related to the Gaussian stochasticity in the state-transition, as heavier tails create a very different looking lag-Attention-pattern for Process 4, Figure (ref). The volatility of the pattern does increases after period 560, corresponding to the upward trend of the factor, potentially signaling about increased uncertainty. Smoothing the curves could help detect a possible hierarchy of importance between the lags and long term changes in it, as shown in Figure (ref). Panel (c) describes the importance of factor lags on the predictions. The pattern shows that the most recent factor estimate is typically most relevant, with other lags supplementing information to varying degrees.
Process 3
has nonlinear and non-Gaussian state-transition but linear--Gaussian measurements. \\
Process 4
is fully nonlinear and non-Gaussian in both state-transition and measurements. Function $f(\cdot)$ is the sign preserving power function from equation (ref) and measurement error covariance matrix $\Sigma_y$ is given by equation (ref).\footnote{Parameters used in simulating Process 3 datasets: $\mu_y$ = (0.79, -0.47, -0.26, 0.15, 0.82), $\beta$ = (0.58, 1.56, 1.62, 1.23, 1.18), $\gamma$ = (0.61, 1.24, 0.63, 1.34, 0.68) and $\phi$=0.36 and $\sigma_x=1.2$ and $c_x=0.13$. Process 4: $\mu_y$=(-0.46, -0.43, 0.24, 0.85, 0.10), $\beta$=(1.41, 1.50, 1.60, 0.94, 0.51) $\gamma$=(1.08, 0.67, 1.03, 1.02, 1.06), $\phi=0.8$, $\sigma_x=0.65$, $c_x=1$ and $c_y=15$.}
The Transformer's accuracy in process 3 is high and its variability is low. Gain of 94% tells that the Transformer managed to explain most of what could be explained beyond the estimated Kalman filter. Fit in process 4 is very high, but also the variability over training runs is considerable.
Figure (ref) shows that the Transformer moves somewhat similarly to the Kalman filter, with constant small departures towards the true factor. The Transformer also departures in the wrong direction during periods 570--580, when the factor peaks. Real time predictions for the observable variables can be found in the Appendix, Figure (ref).
In Figure (ref) the State Attention matrix emphasizes the past lags more than the recent ones. It might be the case that there had been a sequence of less consequential shocks up to that point in the context window, which contribute very little to the system dynamics. The lags 8 and 9 seem to correspond to the time when the factor stopped declining. Most of the relevant information is inferred from the two most recent factor lags in the Measurement Attention.
\noindentProcess 5
has three lags in both the state and measurement equations, and one lag for the state shock, where $\alpha=[0.74, 0.15, 0.074]$ and $\gamma=[0.2, 0.05, 0.02]$. Otherwise the parameters are the same as before, equations (ref) and (ref).\\
\noindentProcess 6
switches between two regimes $s\in\{0,1\}$ corresponding to downturn and growth periods, with different dynamics and volatility in both, inspired by hamilton1989new and diebold1996measuring. Parameters are increased to $\alpha_1^s=1.01 \alpha_1$ for s=0 and decreased to $\alpha_1^s=0.98 \alpha_1$ for s=1, with the same scalars 1.01 and 0.98 used similarly for the rest of the regime-dependent parameters $\phi^s, \beta_i^s$ and $\gamma_i^s$.\footnote{Parameters for Process 5: $\phi$ 0.8, $\sigma_x$ 0.387, $c_y=1.28$, $\mu_y$ c(0.79, -0.47, -0.26, 0.15, 0.82), $\beta$ c(0.58, 1.56, 1.62, 1.23, 1.18), $\gamma$ c(0.68, 1.12, 0.70, 1.21, 0.75). Process 6: $\sigma_x$ 0.51, $c_x=2.24$, $c_y=1.83$ $\phi$ 0.8, $\mu_y$ c(0.79, -0.47, -0.26, 0.15, 0.82), $\beta$ c(0.58, 1.56, 1.62, 1.23, 1.18), $\gamma$ c(0.57, 1.34, 0.59, 1.45, 0.62)} The Markov switching to growth occurs during each downturn period with 3% probability, $\mathbb E [s=1|s=0]=0.03$, and going the other way around with 1% probability, $\mathbb E [s=0|s=1]=0.01$. Function $f(\cdot)$ is the sign preserving power function, equation (ref), and measurement error covariance matrix $\Sigma_y$ comes from equation (ref).
Processes 5 and 6 test the Transformer with a mixture of simultaneous challenges. In both of the challenging processes the Transformer explains about 60% of the gap between the Kalman filter and the oracle filter for the seed, Table (ref). Across seeds the Transformer's results vary. The average result in Table (ref) is skewed downwards due to a very poor performance on a few of the seeds. On other seeds the Transformer managed to perform quite well with reasonable variation across training runs.
The real time projections in Figure (ref) reveals two directions to which the factor is expected to move, corresponding to the means of the two regimes. The Transformer correctly identifies a regime switch around period 350 and starts to project the factor downwards. The Transformer also seems to predict the regime change prematurely for a few times. Interestingly, the regime shift predictions do not result from a simple cutoff in terms of estimated factor value, as some of the downward trajectories begin from a higher starting point than some of the upward projections. The projection is therefore based on some local dynamic pattern which is mostly accurate.
In Figure (ref) the State Attention patterns look very different in the two regimes. At the peak of the growth regime in (a) the Transformer pays most attention to multiple past values of variables 2 and 4. In the direct aftermath of a switch to downturn, panel (b), only the most recent values of variables 1, 4 and 5 receive attention. Also the Measurement Attention pattern changes. During the growth regime the Transformer has seemingly found a way to use the information in factor lags 1 and 6 for the predictions. In the new regime, at least during transition, some of the predictions have started to utilize information from the whole factor series.
Figure (ref) show a clear change in the attention patterns around period 350, hinting at a regime switch. Panel (a) shows a change in the composition of variables used for constructing the factor estimate. Variable 1 was unimportant in the first regime, and now becomes the most important conveyor of information about the factor's movements, especially during the transitory period. Panels (b) and (c) show a temporary change in the time-allocation of attention at the moment of the switch. The State Attention strongly increases the focus to the most recent variable values at the time of the regime shift. This moment remains important through the lags in subsequent periods. After the transitory period the underlying process reaches the new mean and the Attention patterns stabilize.
The results in Subsection (ref) were obtained using prior information in the training loss function with a 60% weight. Functioning of prior information is explained in Subsection (ref). In order to analyze the impact of prior information on the results, the Monte Carlo is repeated twice more: With a training loss function consisting only of prediction errors and no prior information, and then with a training loss function consisting only of prior information and no prediction errors. The 60% weight prior information is found to substantially improve the accuracy and consistency of the results.
Without prior information, $\lambda=0$, the loss function in equation (ref) reduces to
The accuracy of factor estimates and the consistency of results are reduced markedly in the absence of the prior information. The reason for this is probably the small number of training examples. The prior information gives useful structure to the Transformer's parameter space. Using a weak prior is in some ways analogous to the situation, where the Transformer is pre-trained with large amounts of data from a process that the prior-model resembles. Table (ref) summarizes the results.
When only prior information is used to guide the estimation, $\lambda=1$, the loss function in equation (ref) becomes
The Transformer learns to estimate the dynamic factors for each dataset, as if it was a linear--Gaussian model.
The Transformer learns to emulate the arbitrary restrictions of a conventional model, merely by observing examples of the factor estimates from that model. Conventional factor models can be seen as special cases of the Transformer. The Monte Carlo experiment suggests that the results of a conventional model could in some cases be improved if it was estimated using the Transformer. The Transformer can also be used for testing how much the data seems to rebel against the restrictions of a conventional model, such as the linearity postulate by the Kalman filter.
GouletCoulombe2025 shows that there is a conceptual link between linear regression and the Attention mechanism, where the linear coefficients can be interpreted as a static Attention map. Building on this link, I visualize the Attention patterns for the three cases above to highlight the difference that prior information brings about. When the Transformer is trained using only prior information and different conventional factor models, it is possible to evaluate the Attention patterns implied by those models, and to compare them to the Transformer's less restricted patterns.
Figure (ref) compares the Attention patterns for Process 6 under different strengths of prior information regularization. The rightmost column show how the Kalman filter's Attention patterns look like. Even though Kalman filter has time invariant linear coefficients, the Attention patterns can still change over time due to measurement errors and heteroskedastic shocks. Row one shows the relative importance of different variables on the factor estimate. The Attention pattern remains somewhat similar regardless of the prior information: The importance of variables 1 and 4 seem to correlate negatively with each other and the variable 3 is generally less important for determining the factor estimate. The impact of prior information can be better seen in the time allocation of Attention for the variable and for the factor lags, on rows two and three.
I use the Transformer to estimate a measure of real activity for the U.S. economy, from variables related to industrial production, sales, labor income, and hours worked. These variables were proposed for constructing a coincident index in a seminal study by stock1989new. The resulting factor is regarded to coincide with the business cycle. The Transformer's coincident index is compared to a baseline linear--Gaussian coincident index, which follows the methodology in stock1989new.
To compare the results from both models, I focus on three recent recession episodes: The Covid crisis, The 2008 financial crisis, and the recession of 1990. The Transformer's factor estimate behaves quite similarly to the Kalman filter's factor estimate during the Covid recession. During the 2008 crisis and the recession of 1990, the Transformer dampens the volatility somewhat compared to the baseline. Both models tend to agree with the recession dates by the Business Cycle Dating Committee of the National Bureau of Economic Research (NBER).
The time series dataset consists of $i=1,2,\dots, 4$ variables, with $t=1,2,\dots,670$ monthly observations from 1967 to 2025. The four variables, describing the macroeconomy and labor markets in the United States, are the same as those which stock1989new used to construct a coincident index for the U.S. economy. All of the variables are seasonally adjusted, expressed as logarithmic differences, and standardized variable-wise to the same scale, $y_{ti} = \frac{\text{y}_{ti}-\bar {\textbf{y}_i}}{\text{sd}(\text{y}_i)}$.
Production is a monthly index of industrial production, covering a large part of the variation in national output. It includes manufacturing, mining, and electric and gas utilities. Data is from the Board of Governors of the Federal Reserve System (US). More information about the construction of the industrial production index can be found in the Appendix.
Sales includes the real manufacturing and trade sales, expressed in millions of Chained 2017 Dollars. Data is from Federal Reserve Bank of St. Louis.
Income is the real personal income excluding current transfer receipts, expressed in billions of chained 2017 dollars, annual rate. Data is from the U.S. Bureau of Economic Analysis.
Hours worked consists of average weekly hours of production and nonsupervisory employees in Manufacturing. Only those hours are counted which were paid for, divided by the number of workers receiving payments. Non-supervisory employees include for example lawyers and working-supervisors such as group leaders. The data comes from U.S. Bureau of Labor Statistics. More information about the determination of hours worked can be found in the Appendix.
The Business Cycle Dating Committee under the National Bureau of Economic Research (NBER) publishes a retrospective analysis of expansions and recessions in the U.S. economy. The recession dates are used to analyze, whether the factor estimates from the Transformer and Kalman models declined during a crisis event as expected.
The data is divided into non-overlapping training (80%) and validation (20%) sets by hand. The gradients and parameter updates are computed from the training data, whereas the validation data is only used to monitor how well the results generalize out-of-sample. Both training and validation sets include segments of time periods from different eras, and they both include influential crisis events. Within the training and validation sets, a rolling window is applied to create input matrices with all possible information sets. Each matrix consists of $P+1$ consecutive time periods, first $P$ of which are used to predict the most recent time period. Because only the last period's predictions for the observable variables are used in the loss function, it is important to create overlapping context windows with the rolling window.
The benchmark dynamic factor in this study is estimated with the following state space model, using the Kalman filter. This analysis is in line with the classical analysis by stock1989new, which uses the same data and a similar procedure to derive a coincident index.
State-transition equation for the dynamic factor $x_t\in\mathbb R$ is given by
and the measurement equation for the four observable variables is
Parameters $\mu_x, \mu_i, \alpha, \beta_i \in \mathbb R$ for $i\in \{1,\dots,4\}$, $\sigma_x, \sigma_y \in \mathbb R^+$ are estimated with maximum likelihood. The Kalman filter is then used with the estimated model to obtain the factor estimate $\hat x_K$. For simplicity, the result from this process will be referred to as the factor estimate by the Kalman filter. The specific model used in stock1989new had more lags. I used the same simple one-lag linear factor model to enable better comparison to the Monte Carlo.
This subsection focuses on three recent recessions, to showcase the similarities and differences between the Transformer and Kalman filter factor models. Each period's factor estimate from the Transformer is based on the look-back window of P=9, past three quarters of a year of observable variables. The Transformer's factor estimate is the average over 20 alternatives, each obtained with a similar training process but different initial parameter values. The factor estimates are scaled to match the baseline, to enable comparison. The hyperparameters are otherwise the same as in Table (ref), except for a lower weight on prior information, $\lambda=0.2$, and lower regularization with $dr=0.1, L2=0.01$, due to the even smaller size of the empirical dataset.
The real time factor projections are generated with the trained Transformer, which had the lowest validation loss out of 20 runs. The predicted next period's values are used as the most recent lags on the next round, to recursively predict the next 6 months. This results in a forward-looking series of factor estimates, starting from each period. Because the real time projections are computed from one trained Transformer, the results may deviate from the average of all training runs.
The behavior of the Transformer is analyzed by visualizing the Attention matrices from the State and Measurement Encoders, describing the first period of each recession. Additionally, the distribution of Attention for variables and lags are followed through the recession.
Figure (ref) (b) indicates that the factor estimates from both models behave very similarly during the Covid recession, with the Transformer estimating a slightly slower recovery after the initial response. The real time projections for the factor estimate in (c) show downward pressure on the first period of recession, even though the factor estimate for that period is still above zero.
Figure (ref) shows a steady distribution of Attention prior to Covid. From panel (a) we can see that what is regarded important are most lags of Production and Income, and some past lags of Sales and Hours. During the recession the pattern tweaks in panel (b), increasing the importance of income over production. Panel (c) shows temporal changes in Attention patterns during and after the recession. The final panel (d) shows that the most recent factor estimate increases in importance during the turmoil, for predicting the next period values of the four observable variables.
During the 2008 crisis, both coincident indices estimate a slower gradual decline, compared to the Covid recession.
The final event study is the recession of the early 1990s in the U.S. The Transformer's coincident index deviates more from the baseline during this episode than during the more recent crises, mostly with smaller local fluctuations.
It is difficult to select a particular model specification for analyzing real-world data. Yet, this is the standard challenge that has to be dealt with in most macroeconomic applications. It is typical to select a linear model, or some well-known simple nonlinear model, as a reasonable first approximation. Then, based on an initial analysis and several metrics, the model can be tweaked so that it accommodates the data better. Machine learning makes it possible to approach the problem from a different angle, by reducing the level of involvement needed from the researcher in model selection. The Transformer presented here tries to make the best of both worlds by allowing different identifying assumptions to be imposed on the data softly, while remaining open to findings that disobey the assumptions.
The Transformer can entertain a large set of hypotheses, or models, that can explain the data. This strength can also be a weakness in some situations. Many complicated models could explain the same small set of observations without generalizing out-of-sample. Therefore a lot of training examples are needed to falsify wrong complex hypotheses. As the space of potential models is very large, the Transformer can get stuck in a bad local optimum. It is useful to constrict and guide the search for models on small macroeconomic datasets, by prioritizing on where to look first. The Kalman filter is able to learn good approximations more easily from smaller samples, because it is restricted to only look for linear relations. Linear--Gaussian model is often a good approximation also for nonlinear and non-Gaussian factors, enabling the use of the Kalman filter as prior information. The way to use prior information, proposed in this study, pulls the Transformer towards an area in the parameter space, from where it is more useful to search for nonlinear improvements for the factor estimate. In effect, this gives an informed hyper-prior for the model-selection task. Adding even more structure from economic theory and from conventional models can help Transformers reach new levels of usefulness in macroeconomic analysis in the future.
Despite the good performance in most of the Monte Carlo datasets, the results are not yet conclusive of the Transformer's power and reach. The processes were kept somewhat simple and well behaved, partly to enable getting consistent results with the baseline linear factor model with the Kalman filter. By introducing stronger nonlinearities and heavier tails, the estimation procedure with Kalman filter becomes unreliable. Therefore, the current datasets can still be approximated linearly to a high degree. This can be seen from the results obtained with Kalman filter, by using the true linear parameters without estimation, yielding often a high $R^2$ not too far from that of the oracle filter. Surprisingly, the mean of the observable variables also often tracks the true factor robustly. As the mean is used to initialize the Transformer's factor representation, it could have improved the Transformer's results by offering a good starting point. However, in Process 2 the "Transformer max" managed to surpass both of the baselines, the estimated Kalman filter and the mean of data, when the early stopping was conducted based on factor accuracy on the test set. This shows that the Transformer is not bounded by the initialization or prior information. But improving the performance might require developing new tools to early stop the training more optimally.
The Monte Carlo results could be further improved by choosing a more appropriate prior for each dataset. The prior information can be of great use in training, but perhaps also lead astray, by pulling the Transformer towards a sub-optimal area in its parameter space. The Transformer seems to perform especially well compared to the Kalman filter, when the measurement equation is not linear--Gaussian. On the other hand, Kalman filter can often reach a high accuracy by relying on the linear measurements, if only the state-transition deviates from linear--Gaussian. The linear--Gaussian prior information can be easily replaced with a more suitable factor model, if such can be inferred from a preliminary analysis of the data.
Another way to improve the results is by tuning the hyperparameters separately for each dataset. Tuning has potentially a drastic impact on the results. The tuning step was deliberately omitted in this study for the sake of simplicity. This Monte Carlo setup also made it reasonable to use the same Kalman filter as a baseline for all datasets, even though better filtering tools exist for the datasets 2--6. The linear baseline model can also be updated in many ways, for example by adding lags and altering the filtering procedure. Using a better benchmark as prior information could also improve the Transformer's results.
One path forward is to pre-train the Transformer on a large corpus of multivariate time series data, and then fine-tune it with the macroeconomic data. The datasets used for pre-training only need to be loosely similar to the applications in mind. The order by which different datasets are introduced to the Transformer in pre-training should follow an ascending level of complexity - for example, by first teaching the Transformer simple simulated VARMA(1,1) dynamics, moving then to weather data with mild stochasticity, and finally using macro-financial datasets with evolving data generating processes and wild stochasticity. The Transformer learns general and some particular features of time series data, and also what is not typical for dynamic sequences. Pre-training finds an informed hyper-prior to the model selection, restricting and directing the subsequent fine-tuning.
This study applies the Transformer to monitoring the real economy. The policy-analysis can be improved in several ways, to make it more useful in practice. In order to see how the factor estimate would have looked like exactly in the past at period $t$, each estimate should be obtained from a Transformer that was trained only with data up to t. The vintage data should not include revisions. In the current study the baseline Kalman filter, also used as prior regularization, was applied to the whole dataset. To be more thorough, the Kalman filter should be estimated only from training data to avoid leakage.
All of the validation data could be slowly transferred to the training dataset as the training progresses, to utilize all of the information in the small dataset. If this is done gradually, while reducing the learning rate at the same time, the risk of overfitting should be smaller than if all of the data were allocated in the training set from the beginning.
The Transformer's coincident index acts reasonably, but there is room for improvement. The coincident index behaves somewhat similarly to the process 3 of the Monte Carlo experiment, with nonlinear state-transition but linear measurements. The Attention patterns show some peculiarities, such as a constant higher attention to some of the past lags. It is possible that the Transformer has found an idiosyncratic way to distill useful information into particular token representations. Or that the particular scheme used for partitioning the data into training and validation sets had an impact on the factor estimate. Due to the double-Encoder architecture, the Transformer cannot learn from predicting the first $P$ periods of each training segment, as it needs the full context window of $P$ lags to operate. Using a rolling window to create overlapping information sets does help, but the first $P$ periods of each training segment still go unobserved by design. Using a masked Measurement Decoder with teacher forcing could be useful, if it is needed to thinly slice and shuffle the time series data to get representative training and validation sets.
This study proposes a Transformer network for flexible estimation of nonlinear dynamic factors. The architecture, determining how inputs are transformed into outputs, is designed for analyzing and interpreting latent variables with macroeconomic time series data. The accuracy on small datasets is improved by using the identifying restrictions of conventional factor models, but more softly as prior-information regularization. Prior information is supplied to the loss function with a weight between zero and one, to guide the Transformer's training. I demonstrate the impact of prior-information regularization using a linear factor model with the Kalman filter, but any conventional factor model can be used instead. The choice of granular tokens makes the Transformer resemble a nonlinear vector autoregression. This helps interpret the results with the Attention mechanism, which indicates the most important input variables and their lags for the factor estimate at any given moment. Attention matrices are used to observe how the attention pattern for variables and their lags evolve over time, indicating for example regime switches.
In the proof-of-concept Monte Carlo experiment the Transformer was able to estimate factors more accurately than the linear factor model with the Kalman filter, in many cases where the process behind the datasets deviated from linear--Gaussian.
The Transformer is used to estimate a dynamic factor from the U.S. macroeconomic data. The factor is interpreted as a measure of real economic activity, coincident with the business cycle. The results are compared to those obtained with a linear factor model, and to the retrospective NBER--determined recession dates. The Transformer generally agrees with the baseline coincident index and the NBER recession bars during the Covid crisis, with some deviations during the 2008-09 recession and the early-1990s downturn.
\printbibliography[heading=subbibintoc, title={References}]