## 3 Generalized Causality Algorithm  
### Methodology  
This section introduces the \"generalized causality algorithm\" which captures time-varying lead-lag relations for two time series \(x\in\mathbb{R}^{N}\) and \(y\in\mathbb{R}^{M}\). Specifically, Algorithm 1 outputs the optimal causal path, the corresponding structural breaks, and the estimated lag of each regime. For a pleasant visualization, the algorithm also provides the local costs. Step A determines the 2-dimensional index of the time series \(x\) and \(y\) based on ascending local costs. Therefore, a double loop estimates all local costs between \(x\) and \(y\), i.e., we calculate \(c(x(i),y(j))\forall i\in N,\forall j\in M\). If \(i\) equals \(j\), we measure the distance between \(x\) and \(y\) at the same time points. Next, the algorithm creates the variable \(I\in\mathbb{R}^{(N\cdot M)\times 2}\) by rearranging the local costs in ascending order, e.g., the first row of \(I\) describes the combination of the indices of \(x\) and \(y\) with lowest local cost. Step B specifies the optimal causal path that allows a time-varying lead-lag structure. For this purpose, we use a binary search algorithm, i.e., half of the solution space is eliminated in each iteration step. Specifically, our algorithm considers the first \(n=0.5\cdot N\cdot M\) elements of \(I\) and uses the function \(eval\) to check whether the given set of points is capable of constructing a causal path. If a (no) subset of the regarded \(n\) data points depicts a causal path, we redefine \(n\) by \(n=n-h\) (\(n=n+h\)), where \(h\) describes the step size \(0.5\cdot n\). Next, we conduct a similar process for the updated given set of \(n\) points, i.e., the algorithm checks whether the given set of \(n\) points is capable of constructing a causal path and redefines \(n=n\pm h\) with step size \(h=0.5\cdot h\). Repeating this procedure until the step size \(h\) is less than 1 leads to the optimal \(n\), i.e., the lowest number of required elements of \(I\) that constitute a causal path. We note that there are many points in the first \(n\) elements of \(I\) that are not relevant for our causal path. Finally, we apply the function \(path\) to the first \(n\) elements of \(I\) in order to find the shortest path--the result is the optimal causal path \(P\). Using the binary search algorithm implicates two main advantages: our search runs in logarithmic time in the worst case and we know exactly the number of steps required for given \(N\) and \(M\). Step C identifies the optimal structural breaks and the corresponding lags of the optimal causal path \(P\). Following [61; 62], we estimate the structural breaks and the corresponding confidence bands by minimizing the sum of squared residuals. Furthermore, the model allows for lagged dependent variables and trending regressor. It should be noted that the number of structural breaks equals the number of sub-periods minus one. The algorithm determines the optimal lag of each sub-period by averaging the corresponding lags which could be disturbed by temporally noise terms. The algorithm returns (i) the local costs, (ii) the optimal causal path, (iii) the corresponding structural breaks, and (iv) the optimal lag of each sub-period. ```
Input: Time series \(x\in\mathbb{R}^{N}\) and \(y\in\mathbb{R}^{M}\) as well as local costs measure \(c\). Output: The local costs (Step A), the optimal causal path (Step B), the corresponding structural breaks, and the optimal lag of each sub-period (Step C). Step A--Determine the 2-dimensional index of \(x\) and \(y\) based on ascending local costs. for\(i:=1\)to\(N\)do for\(j:=1\)to\(M\)do \(C[i,j]\gets c(x[i],y[j])\) endfor endfor \(l\gets order(D)\) Step B--Specify the optimal causal path. \(eval:\) Function evaluating if the given set of points is able to construct a causal path. \(path:\) Function finding the shortest path of the given set of points.