{'fraction_non_alphanumeric': 0.02880427609871981, 'fraction_numerical': 0.010756235977299723, 'mean_word_length': 4.729489603024574, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Jacobian Granger Causal Neural Networks for Analysis of Stationary and Nonstationary Data

Granger causality is a commonly used method for uncovering information flow and dependencies in a time series. Here we introduce JGC (Jacobian Granger Causality), a neural network-based approach to Granger causality using the Jacobian as a measure of variable importance, and propose a thresholding procedure for inferring Granger causal variables using this measure. The resulting approach performs consistently well compared to other approaches in identifying Granger causal variables, the associated time lags, as well as interaction signs. Lastly, through the inclusion of a time variable, we show that this approach is able to learn the temporal dependencies for nonstationary systems whose Granger causal structures change in time.


Introduction
The evolution of an observed system usually occurs with some definite and possibly nonstationary couplings between its constituents. As a starting point, information flow allows one to begin uncovering such a structure, which also serves as a precursor to a causal understanding of the system. Among other methods, Granger causality (GC) [1,2,3] has been used for such a purpose in various fields including finance and economics [4,5,6,7], neuroscience [8,9,10,11], climate and environmental studies [12,13,14], as well as biological networks [15,16,17]. More generally, GC can be framed as feature selection in multivariate time series [18]. Another common approach to information flow is the transfer entropy and its extensions [19,20], which has also been shown to be equivalent to Granger causality under some conditions [21,22].
Recent efforts on Granger causality worked towards extending it to high dimensional and nonlinear data in a model-free manner using neural networks without the need for exponentially large data size. These developments were extensively tested for their ability to infer the correct Granger causal variables. While some of these approaches are also capable of extracting the time lag for each discovered interaction, no prior study was conducted to specifically test the ability to correctly infer the interaction time lag. This information can be useful for theoretical modeling and understanding of the system at hand, and it is also studied in the field of dynamical systems [23]. In addition, it may also provide information for confounder analysis [24].
In addition, these approaches (including information flow measures such as the transfer entropy) require the structure of the data to be stationary, i.e. the interaction between variables in the system do not change in time. We see this being violated in systems with structural breaks in financial modeling [25], as well as in neuroscience where the system may shift between different functional networks in a single time series [26].
The contributions of this work are as follows: 1. We augment the model for Granger causality to include contemporaneous variables. While they have not been widely used in most of the recent related works on Granger causality using neural networks, we provide an argument to illustrate its importance.
2. We propose the use of the Jacobian with respect to input as a measure of variable importance to infer Granger causality. We denote this approach as Jacobian Granger Causality (JGC). arXiv:2205.09573v1 [cs.
LG] 19 May 2022 3. We compare the proposed approach with other neural network-based methods for Granger causality across multiple test cases. In particular, we consider separately the inference of Granger causal variables, Granger causal variables with time lag, as well as effect sign. 4. Lastly, we consider the problem of nonstationary data whose structure changes with time. By augmenting the model with a measure of time, we show empirically that the proposed approach is capable of discovering these structural changes.
2 Background and Related Work

Granger Causality
Given an N-dimensional time series {x i (t)} i=1,...,N with time represented in units of integer timesteps and a specific target variable x j (t), we want to find the smallest possible subset of {x i (t − α)} i=1,...,N ;α=1,...,η such that where τ ≤ η with τ representing the maximum interaction time lag discovered in the system, and η the maximum lag tested by the procedure. j is a noise term. The specification of η may be informed by prior knowledge of the system and may also be constrained by the amount of data available. In any case, η should be sufficiently large in order to capture all the lagged interactions. We designate the minimal set of variables {x i } i=1,...,k in the right hand side of (1) as the Granger causal variables for x j and the remaining variables as irrelevant variables. In other words, all variables in the set must contribute to the prediction of the target variable given the information about all other variables in the set.
Granger causality can be represented as a directed graph [27] with the vertices representing the variables and edges representing the directed relationship from the Granger causal variables to the corresponding target variables. On the other hand, the interaction time may also be incorporated into the graph by labeling the edges [24]. While it renders the graph more informative, it also allows the determination of constraints that confounders must satisfy, if they exist.

Related Work
Recent years saw the development of multiple approaches of using neural network to infer Granger causality. Initially conceived in 2018, [28] saw the use of MLP and LSTM, denoted cMLP and cLSTM, where inference is done by a novel loss function with sparsity constraints and optimized by proximal gradient descent. Granger causality is then made unambiguous as all irrelevant variables have zero weights in the first hidden layer. This approach was subsequently extended by [29] through the use of statistical recurrent units (SRU), dubbed economy-SRU (eSRU) due to its sample-efficient property. Convolutional nets were also used, in an approach called temporal causal discovery framework (TCDF), where attention scores were used to determine the relevance of variables [30]. More recently, [31] extended self-explaining neural networks towards Granger causality by modeling the system as a generalized vector autoregression (GVAR), in which case the coefficients in GVAR measures the relevance of the variables. Unlike the other mentioned approaches, TCDF and GVAR return nonzero measures of variable importance, hence a subsequent thresholding procedure was required to infer Granger causal variables. TCDF utilizes a permutation test, while GVAR compares with the analysis when the data is given in reverse temporal order. The latter works on the premise of stability [32], where the true Granger causal variables are expected to be consistent across different runs. Out of these approaches, information on the interaction time lag could be extracted from cMLP, TCDF, and GVAR.

Augmenting the Granger Causal Model
We begin with a fresh formulation of Granger causality. As discussed in (1), formulations of Granger causality typically consider candidate variables at some nonzero delay with respect to the target variable. Here, we augment the model by adding contemporaneous variables, which are the other variables measured at the same time as the target variable that we are interested to predict. If the target variable is x j (t), then the contemporaneous variables are {x i (t)} i =j . With this, (1) is rewritten as with i = j. To motivate this augmentation, we consider a simple system evolving under a nonlinear differential equation with some function g, Since we are dealing with data measured at discrete timesteps, we can rewrite (3) in terms of the given data as Denoting the antiderivative of g as G, we then have We emphasize from (3) that since we are testing for Granger causality, the function g should contain only the direct causes of z. However, as (5) indicates, information on both x(t) and x(t − 1) are required to accurately predict z(t). In the absence of x(t), i.e. given only x(t − 1) and z(t − 1), accurate prediction of z(t) would then necessitate modeling the evolution of x(t) as well, in which case g potentially requires causes of x that are not causes of z, which may lead to false positives. Neglecting contemporaneous variables (where such causality exists) also has an impact on autoregressive systems, which has been discussed at length in [33]. Lastly, we note that contemporaneous variables were in fact incorporated in TCDF, although other more recent approaches do not make use of them.

Jacobian as a Measure of Variable Importance
We approximate the unknown function f j in (2) using a neural network. The architecture of the network is a simple feedforward architecture with the exception of the first hidden layer. This layer is not fully connected from the input layer, but is instead wired one-to-one ( Fig. 1). This allows the first hidden layer to serve as a gate for input information through its weights. We can therefore bias the network into using only the information from relevant inputs. Given N variables and a predetermined maximum test lag η, each variable at each lag is fed into the network as a separate input, hence the dimension of the input layer is D = N η. We note that a separate neural network would be required for each target variable.
The network is then trained in a supervised manner with the target variable x j (t) as the output. The training is then done with the loss function defined as M SE + k λ|w k | where w k is the weight of the kth neuron in the first hidden layer.
From (2), the contribution of some variable x i (t − α) at a given lag α on the evolution of x j (t) can be written to first order as the Jacobian with respect to the input, With the trained neural network serving as a representation of f j , we can then use the Jacobian of the trained network with respect to each input as a measure of variable importance. This quantity then serves as an estimator of the true Jacobian in (6), in the same way that the neural network serves as an approximation of the unknown function f j . This is a more holistic measure compared to weights as it summarizes the contribution of each variable over all layers. We denote the estimator of (6), as computed from the neural network, asf j,iα . Since the dataset consists of time series data, each input x i (t − α) is indexed by time t, which therefore yields a vector of derivativesf j,iα,t also indexed by t, which serves as a measure of variable importance. This then naturally allows us to visualize how the importance of each variables evolves in time. However, since information on time is not given to the network, it is unable to learn the time dependence of the variable importance measures, thereby encoding an implicit assumption where the importance is a function of all the other variables x i but not t explicitly. This is true only if the dependency structure is stationary. We will address the nonstationary case in section 3.4.

Stationary Case: Inferring the Granger Causal Variables
Under the assumption of a stationary structure, which is typically the case in Granger causality, we can infer the Granger causal structure by first condensing the importance measuresf j,iα,t into a single summary statistic. We achieve this Figure 1: The neural network architecture for nonlinear Granger causality.

Algorithm 1 Thresholding procedure for inferring Granger causal variables
Input: s 1 , s 2 , s 3 (sets of variable importance scores for all input for target variable x j from 3 runs), cutoff Output: GC, the inferred Granger causal variables for x j with time lag information for i = 1 to 3 do set to zero all elements of by computing the absolute mean value |f j,iα |, which summarizes the contribution of each input. As the discussion in section 3.1 indicates, dependencies that occur without a time lag (eq. (3)) may nevertheless require information on the relevant variables at a time lag of 1 timestep (eq. (5)), owing to the process of measurement which occurs at discrete time. Therefore, the contribution of a variable at a time lag of 1 timestep may in fact indicate dependencies at zero lag. This is also how other approaches with no contemporaneous variables measure such dependencies (e.g. in the Lorenz96 system in sections 4.1.2 and 4.2.1). Since both quantities contribute towards the same conclusion, we combine |f j,iα | at lags of zero and one into a single number by summing them. We therefore do not distinguish between dependencies with lags of zero and one.
With this, we can then identify the Granger causal variable by applying an appropriate threshold. While a good threshold can in fact sometimes be identified by eye, we discuss here a procedure that allows a more systematic way for determining the threshold. The procedure operates under the assumption that the value of |f j,iα | is consistently larger for the Granger causal variables relative to the irrelevant ones. While the Granger causal variables consistently contribute to the prediction, the contributions from the irrelevant variables should essentially be noisy and therefore inconsistent in their ordering. This idea is similar to the basis for stability selection [32].
To make use of these properties, we run multiple independent runs of the procedure, which can be run in parallel; in our implementation we make use of 3 independent runs.
To simplify the subsequent step, variables with negligible contributions are first removed, which also reduces the false positive rates. There can be multiple ways for performing such a step; here we simply remove all variables whose |f j,iα | is less than 0.01 of the largest value in the respective run; a different cut-off may be chosen if required. In a high dimensional system with sparse couplings, this step should remove most of the tested variables, leaving a smaller set of potential Granger causal variables for each run.
Subsequently, we compute the intersection among all the sets from respective runs. Each set is then modified by imposing an additional condition where we only retain a variable x k (t − β) in each set if all other variables in the set with |f j,iα | larger or equal than that of x k (t − β) in each set are in the intersection. The intersection of the sets is taken again and this procedure is repeated once. The final intersection of these modified sets then constitute the inferred Granger causal variables at the respective time lag. This procedure is summarized in algorithm 1. For ease of subsequent comparisons, we refer to our proposed method outlined here in sections 3.2 and 3.3 as Jacobian GC (JGC).

Nonstationary Case: Visualizing Instantaneous Information Flow
The formulation in (2) expresses x j (t) as a function of all other variables x i and not t explicitly, encoding the assumption that f j (and therefore the dependency structure) does not change directly with time. To handle systems where this assumption does not hold, we propose to augment f j with a measure of time such that there is a definite way of distinguishing the temporal order of the data, thereby allowing the model to adapt its connections and their strengths as a function of time. The model then becomes Under this formulation, the network can then learn how the importance measuresf j,iα,t change with time, and this time dependence can then be visualized.
Experiments indicate that the inclusion of time may lead to slightly worse performance for Granger causality when the causal structure has no time dependence. Therefore, we suggest that a measure of time can be added when time-dependence is suspected, after which the time-dependence could be determined (as seen in section 5). This then serves as a stationarity test. If no significant time-dependence in the dependencies is observed, i.e. if the variable importance measures do not appear to vary significantly with time, the procedure may still proceed without the time variable.

Experiments on Stationary Granger Causality
In this section, we examine how the introduced approach performs in different simulated systems where the true Granger causal structure is known. In addition, the dependency structure is assumed to be stationary as per the standard Granger causality setting. We then compare it with the other known approaches through two scores, namely the area under receiver operating characteristic curve (AUROC) and the area under precision-recall curve (AUPRC). Each method was run through a range of sparsity hyperparameter values and the hyperparameters corresponding to the highest average AUPRC were taken for comparison, similar to [31]. The reported score for each test case was averaged over 5 independently generated realizations of the system. The hyperparameters associated with each method for each test case are given in the appendix. Lastly, the results for JGC over all experiments in this section are computed without augmenting the model with time.
We organize this section into five subsections, the first three comparing the different approaches on the identification of Granger causal variables, time lag, and interaction sign respectively. The fourth subsection averages the results across the first three subsection for an overall comparison of the top performing models. As AUROC and AUPRC are computed without thresholding the variables, we evaluate the thresholding procedures by instead analyzing their stability relative to the regularization parameter(s) in the fifth subsection. Computations were run on a computer with AMD Ryzen 7 3700X 8-core processor (3.59 GHz) and NVIDIA GeForce RTX 2070 SUPER. The code for the implementation and data used for these experiments are available at [link].

Identification of Granger Causal Variables
In this section, we consider the identification of the Granger causal variables, ignoring time lag information. In other words, the identification of the correct variable at the wrong time lag is considered correct. We describe each tested system below, while the results are collated in Table 1.

Vector Autoregression
We begin by considering vector autoregressions (VARs) as defined below with dimension N = 10: Here A α denotes the connectivity matrix of the system acting at a delay of α and is a noise vector with each element ∼ N (0, 1). The system is set to be sparsely connected with random connections, hence any pair of variables can potentially be connected at only a single or multiple arbitrary time lags. For this system, we simulated up to T = 500 observations with the maximum lag τ = 5. Table 1 indicates that cMLP, GVAR, and JGC performed very well for this dataset, while the remaining approaches are weaker. This is likely because the causal connections in this test case were randomly generated across multiple lags, hence there are many connections that only exist at larger lags, which may not be as easily detected by some of the tested approaches.

Lorenz96
The next test case is the Lorenz96 system [28] with the evolution defined as: with i = 1, . . . , N where F is a driving constant and the interaction defined symmetrically with x −1 = x N . Here we simulated the system with dimension N = 20 and sampled at a temporal sampling rate of ∆ t = 0.1. Similar to the baselines used for the other approaches, we consider two cases at F = 10, 40. As above, we simulated this system up to T = 500. As is the case in [31], we exclude the trivial self-connections when computing the scores for this system.
We see that cMLP, eSRU, GVAR, and JGC all performed well for the two cases of Lorenz96, while cLSTM did significantly worse for the F = 40 case compared to F = 10. While cMLP has the highest scores for the F = 40 case, we see later in section 4.5 that the F-score of the thresholded results perform worse compared to GVAR and JGC.

fMRI Simulations
Lastly, we consider the realistic and highly nonlinear fMRI simulations with blood-oxygen-level-dependent (BOLD) signals [34]. As the name suggests, the signals in the timeseries involves modeling both the brain activity as well as the associated vascular dynamics. Similar to the baselines in the other approaches, we use the first 5 samples from the third simulation of the original dataset, with dimension N = 15 and length T = 200 for each sample. As with Lorenz96 above, we exclude self-connections for this system.
For this test case, JGC and TCDF have significantly better performance compared to the rest. As these two approaches are the only ones with contemporaneous variables, the improvement is likely due to this factor.

Identification of the Interaction Time Lag
Here we test the correct identification of the interaction time lag in addition to the Granger causal variable itself. In other words, a Granger causal variable identified at the wrong time lag is considered a false positive. Due to this, we include self-connections when computing the score to ensure that the correct time lag is inferred even for the self-connection. For all these test cases, we consider interactions at time lags of zero and one to be the same category as discussed in section 3.3. Here we only tested cMLP, TCDF, GVAR, and JGC as the remaining approaches do not yield information on time lag. The same datasets from the preceding section were used with the exception of fMRI data as it lacks the temporal ground truth, with an additional dataset we will subsequently describe. The resulting scores are collated in Table 2.

VAR and Lorenz96
We see that cMLP, GVAR, and JGC performed well in identifying the correct time lag for these datasets, significantly outdoing TCDF.

Nonlinear Map
In addition to the above systems, we simulated a two-dimensional nonlinear map [35]: where in our case we set τ = 10. This system was not tested in the preceding section since in the absence of information on time lag, both variables are Granger causal variables of themselves and each other, in which case both AUROC and AUPRC could not be computed. Table 2 indicates that cMLP and JGC continue to perform well for this dataset, while GVAR performed comparatively worse next to these two in its AUPRC score. This is likely due to the fact that the nonlinear map contains a nonlinear interaction for two variables at different time lags: one of the terms for y(t) is y(t − 1) · x(t − τ ). The formulation of GVAR is such that the model is linear for variables at different time lags, which may then fail to capture this interaction.

Inference of Interaction Sign
Here we consider the detection of interaction sign in the same manner as [31], through the multi-species Lotka-Volterra system with the following evolution [36]: where x, y describe the population of prey and predator species respectively, with P a(·) containing the indices of the variables in the respective set of Granger causes. The remaining parameter, which modulate the strength of the interactions, were simulated at α = ρ = 1.1, β = δ = 0.2, η = 2.75 × 10 −5 , |P a x i | = |P a y j | = 2 up to T = 2000 observations, following the parameters given in [31]. The system has a dimension N = 20 consisting of 10 preys and 10 predators. As is expected intuitively, a larger prey population would boost the predator population (positive interaction) while a larger predator population would reduce the prey population (negative interaction). We therefore test if the different appproaches can reveal such a relationship.  To score the approaches in a similar way as the preceding sections, we compute for each interaction sign the sensitivity of each approach, i.e. the proportion of the correct interaction sign identified by each approach. Similar as before, we run each approach over a range of sparsity hyperparameter values and select the best average performance over the average sensitivity and AUPRC. The results are given in Table 3, which indicates that most approaches, with the exception of TCDF and eSRU, are able to assign the correct interaction sign for both the positive and negative interactions. We also computed the AUROC and AUPRC for inferring the Granger causal variables, where we see that GVAR and JGC significantly outperform the other models.

Summary of Experimental Results
The performance across the various test cases is consistently dominated by cMLP, GVAR, and JGC. We show on Table  4 their AUROC and AUPRC scores averaged over all test cases for inferring Granger causal variables (from Tables 1  and 3) and time lag (from Table 2). The comparison over all models are given in the appendix. Here we see that JGC performs best overall.

Stability of the Thresholding Procedures
In the preceding sections, we restricted the comparison to using AUROC and AUPRC which used only the variable importance scores, and not the inferred Granger causal variables resulting from thresholding the importance scores. For approaches such as cMLP whose results are thresholded by default (due to the training algorithm), the magnitude of the nonzero weights nevertheless served as importance scores.
In this section, we examine the performance and stability of the thresholded results in the top performing methods (cMLP, GVAR, JGC) over the tested range of regularization parameter λ for two test systems: VAR and Lorenz96 (F = 40). Since GVAR has two regularization parameters (λ, γ), we maximize the F-scores over γ for each λ and inspect how these F-scores change with λ. Fig. 2 shows that both GVAR (center) and JGC (right) are relatively stable across λ, while cMLP is comparatively sensitive to λ which may present difficulty when used in practical settings. In addition, while cMLP achieved high AUROC and AUPRC for both the VAR and Lorenz96 (F = 40) systems, we see that the F-scores are consistently lower than GVAR and JGC, indicating a weakness in the thresholding procedure for cMLP. While the F-score for GVAR appears to drop dramatically when λ = 0 for one of the test cases, this is not unexpected as the network is completely unconstrained in its representation when λ = 0; what is more important is the stability when λ > 0. For both GVAR and JGC, then, the main consideration is to not set an overly large λ which may naturally cause the network to drop the weaker causal variables, but otherwise we see that there is a safe margin for λ for these two test cases. The corresponding plots for all the other test cases are given in the appendix.

Experiments on Systems with Nonstationary Structures
Here we test the performance of our proposed approach for analyzing data whose dependency structures are nonstationary, a phenomenon also known as structural breaks [25]. For such systems, the analysis is done in JGC by visualizing the Jacobian, which measures the importance of each variable, as a function of time.

Piecewise Vector Autoregression
We first consider VARs defined similarly as (8) in section 4.1.1, now with the connectivity matrix A α being defined piecewise in time. Here we simulated the system with N = 10 variables and maximum lag τ = 5 up to T = 900, with two structural break points at t = 300, 600 such that A α abruptly changes in these two time points, being replaced by a matrix with completely different elements. This system therefore has 3 piecewise regimes separated by the break points at t = 300, 600. The results were plotted for two target variables in Figure 3, showing that our proposed approach is able to detect the 3 piecewise regimes. In the same figure (second row), we show that without providing the network with information on time, this piecewise structure would not be discovered.

Historical Stock Prices
We now apply the approach to the weekly log returns for stock prices of banks and financial institutions from Jan 1st, 2006 to Nov 1st, 2020 [37]. This period includes, among others, major events such as the 2007-2008 financial crisis and COVID. Excluding those with incomplete observations, the resulting system has a dimension of 44 with 751  For both target variables in the top row, we see at least one variable whose importance measure moves from a nontrivial value towards zero over time.
observations. Our analysis revealed a mixture of stationary and nonstationary structures for the different target variables in the system. Figure 4 displays the results for two target variables with nonstationary structures, with particularly striking nonstationarity occuring around the 2007-2008 financial crisis. As in the preceding section, we see that without providing temporal information, the network is unable to learn the nonstationary structures.

Conclusion
In this work we introduced JGC, a neural network-based approach for nonlinear Granger causality using the Jacobian as a holistic measure of variable importance. We augmented the Granger causality model with both contemporaneous variables and a measure of time (for nonstationary data). When tested for the identification of Granger causal variables, interaction time lags, and interaction signs, our proposed approach is on par with other approaches across many test systems and outperforms them in some others. In addition, the thresholding procedure for inferring Granger causality was shown to be relatively stable with respect to the regularization parameter. Lastly, we empirically showed that this approach allows one to visualize the instantaneous information flow between variables as a function of time and is capable of detecting time dependencies in the structure in nonstationary systems where the Granger causal structure itself changes with time.

Stability of Thresholding Procedures
Here we examine how the stability of the thresholding procedures implemented in cMLP, GVAR, and JGC fare with respect to the regularization parameter λ for each of the test cases. In each case, we consider where applicable the inference just the Granger causal variables (section 4.1) and the inference of GC variables at the correct time lag (section 4.2). Interestingly, while cMLP achieves high AUROC and AUPRC scores, we see that the F-scores tend to be significantly worse than GVAR and JGC. We note that the implementation of GVAR does not include a thresholding procedure for inferring the time lag, therefore this aspect could not be evaluated.