# CQFormer: Cyclic Quasi-Dynamic Transformers for Hourly Origin-Destination Estimation
## 4 Methodology  
Given the \(n_{p}\) traffic analysis zones \(\mathbb{P}=\{P_{1},P_{2},\cdots,P_{n_{p}}\}\) and \(n_{l}\) roads \(\mathbb{L}=\{l_{1},l_{2},\cdots,l_{n_{l}}\}\), a trip from origin to destination can be written as \(P_{\varphi_{\varphi}}\to l_{a_{1}}\to l_{a_{2}}\to\cdots\to l_{a_{q}}\to P_{ \varphi_{d}}\), with \(P_{\varphi_{\varphi}}\in\mathbb{P}\) and \(l_{a_{1}},\cdots,l_{a_{q}}\in\mathbb{L}\). With regard to the temporal dimension, the sub-period when the trip passes \(l_{a_{i}}\) is marked as \(T_{\beta_{i}}\), in this form, the spatiotemporal trajectory of a trip is expressed as below:  
\[r_{k}:=P_{\varphi_{\varphi}}\rightarrow\left(l_{a_{1}},T_{\beta_{1}} \right)\rightarrow\left(l_{a_{2}},T_{\beta_{2}}\right)\rightarrow\ldots \rightarrow\left(l_{a_{q}},T_{\beta_{q}}\right)\to P_{\varphi_{ d}} \tag{4}\]  
Defining the state of arriving at road \(l_{a_{i}}\) at moment \(T_{\beta_{i}}\) as \(A_{i}=\left(l_{a_{i}},T_{\beta_{i}}\right)\), then each element in \(F_{T}\) can be written as Equation (5). When the spatiotemporal trajectory \(r_{k}\) passes through \(l_{a_{i}}\) in subperiod \(T_{\beta_{i}}\), the trajectory \(r_{k}\) contains state \(A_{i}\) and \(\delta_{A_{i}\in r_{k}}\) equals to \(1\) otherwise \(0\). The total number of trajectories containing the state \(A_{i}\) determines the value of \(F_{T}[\alpha_{i},\beta_{i}]\) together, and the number of trajectories \(N(r_{k})\) is related to distribution of path selection and traffic demand structures through Equation (6), where the OD pair form traffic analysis zone \(P_{m}\) to zone \(P_{n}\) has \(\gamma\) candidate trajectories \(r_{k}\), and \(\delta_{\varphi_{\varphi},m}\) and \(\delta_{\varphi_{\varphi},n}\) indicate the origin and destination of \(r_{k}\) are \(P_{m}\) and \(P_{n}\), respectively. From the above description, it can be seen that the traffic flow matrix \(F_{T}\) and traffic demand matrix \(D_{T}\) are correlated through the spatiotemporal trajectory distribution, and their correlation can be given by Equation (7), where \(P(r_{k})\) indicates the probability of selecting the trajectory \(r_{k}\) among all candidate trajectories between origin \(\varphi_{o}\) and destination \(\varphi_{d}\). \[F_{T}[\alpha_{i},\beta_{i}]=N(A_{i})=\sum_{r_{k}}N(r_{k})\delta_{A_{i}\in r_{ k}} \tag{5}\]  
\[D_{T}[m,n]=\sum_{k=1}^{\gamma}N(r_{k})\delta_{\varphi_{o},m}\delta_{\varphi_{ d},n} \tag{6}\]  
\[F_{T}[\alpha_{i},\beta_{i}]=\sum_{\varphi_{\varphi_{i}},\varphi_{d}}\sum_{r_{ k}}D_{T}[\varphi_{o},\varphi_{d}]P(r_{k})\delta_{A_{i}\in r_{k}} \tag{7}\]  
However, the coupling of temporal and spatial dimensions in \(r_{k}\) and the sharing of multiple trajectories for the same state \(A_{i}\) make the equation groups from Equation (7) hard to resolve explicitly. From Equation (7), the known variables \(F_{T}\) and target variables\(D_{T}\) are related through a series of distribution probabilities, and neural networks are powerful tools for capturing mapping relationships from known distributions to target distributions. We adopt a bidirectional encoder-decoder architecture to approximate the mapping relationship, with forward network estimating OD matrix and backward network assigning traffic flows, as illustrated in Figure 1.  
In the forward encoder, each column vector \(F_{T}[:,\beta_{i}]\) in \(F_{T}\) contains information about the spatial distribution of traffic flow in each subperiod, and they are embedded individually to feature space with dimensions of \(n_{f}\) while maintaining the temporal dimension, and produces \(n_{t}\) feature vectors, where \(n_{t}\) is the number of subperiods. The embedded layer applies two fully connected layers as Equation (8). \[FC(x)=activation(xW+b) \tag{8}\]  
where \(x\) is the input data (here is \(F_{T}\)), and \(W\), \(b\) are learnable parameters. Here, LeakyReLU, an extension of Rectified Linear Unit (ReLU), is used as the activation function.