netrd/reconstruction/convergent_cross_mapping.py
def fit(self, TS, tau=1, alpha=0.05):
        """Infer causal relation applying Takens Theorem of dynamical systems.

        Convergent cross-mapping infers dynamical causal relation between
        vairiables from time series data. Time series data portray an attractor
        manifold of the dynamical system of interests. Existing approaches of
        attractor reconstruction involved building the shadow manifold for a
        single variable $X$, which is defined by the time-lagged vectors
        $(X(t), X(t-\tau), X(t-2\tau), ..., X(t-(N-1)\tau))$ where $N$ denotes
        number of variables in the system and $\tau$ is an arbitrary time-
        lagged interval. Takens theorem and its generalization indicated that
        if a variable $X$ is causally influencing another variable $Y$ in a
        dynamical system, then there exists a one-to-one and local-structure-
        preserving mapping from $X$'s shadow manifold to $Y$'s.

        The convergent cross-mapping algorithm first constructs the shadow data
        cloud (a portray of the shadow manifold) for every variable, and it
        then examines the mutual predictability for all pairs of them.
        Specifically, for a point $u(t)$ in $X$'s shadow data cloud, the time
        indices $\{t_1, t_2, ..., t_{N+1}\}$ of its $N+1$ nearest neighbors are
        obtained, which are mapped to a neighborhood in $Y$'s shadow data cloud
        $\{v(t_1), v(t_2), ..., v(t_{N+1})\}$. The estimate $\hat{Y}(t)$ is
        computed as an average over this neighborhood, with weights decaying
        exponentially with corresponding distance in $X$'s shadow data clould.
        The algorithm concludes a causal link from $X$ to $Y$ if correlation
        between $Y$ and $\hat{Y}$ is significant.

        Furthermore, Sugihara et al.'s simulation suggested that the
        correlation converging to 1 as the length of time series data grows is
        a necessary condition for $X$ causally affects $Y$ in a deterministic
        dynamical system. The convergent cross-mapping approach is thus
        numerically validated to infer causal relation from time series data.

        Params
        ------
        TS (np.ndarray): $N \times L$ array consisting of $L$ observations
                         from $N$ sensors.

        tau (int): Number of time steps for a single time-lag.

        alpha (float): Significance level of statistical test for correlation.

        Returns
        -------
        G (nx.Graph): A reconstructed graph with $N$ nodes.

        Notes
        -----
        1. The length of time series data must be long enough such that
           $L \geq 3 + (N-1)(1+\tau)$.

        2. The matrix of correlation between estimates and the observed values
           and the matrix of p-value for correlation test can be found in
           results['correlation'] and resutls['pvalue'] respectively.

        3. The $\[i, j\]$ of the correlation matrix entails the correlation
           between the $j$th variable and its estimate from the $i$th variable.
           A similar rule applies to the p-value matrix.

        4. The computation complexity of this implementation is $O(N^3 L)$.

        """
        data = TS.T  # Transpose the time series to make observations the rows
        L, N = data.shape

        # Raise error if there is not enough data to run the implementation
        if L < 3 + (N-1) * (1+tau):
            message = 'Need more data.'
            message += ' L must be not less than 3+(N-1)*(1+tau).'
            raise ValueError(message)

        # Create shadow data cloud for each variable
        shadows = [shadow_data_cloud(data[:, i], N, tau) for i in range(N)]

        # Obtain nearest neighbors of points in the shadow data clould and
        # their weights for time series estimates
        neighbors, distances = zip(*[nearest_neighbors(shad, L)
                                     for shad in shadows])
        weights = [neighbor_weights(dist) for dist in distances]

        # For every variable X and every other variable Y,
        # construct the estimates of Y from X's shadow data cloud and
        # compute the Pearson correlation between Y and its estimates
        # along with the p-value
        correlation = np.ones((N, N), dtype=float)
        pvalue = np.zeros((N, N), dtype=float)
        for i, j in permutations(range(N), 2):
            estimates = time_series_estimates(data[:, j],
                                              neighbors[i], weights[i])
            M, = estimates.shape
            correlation[i, j], pvalue[i, j] = pearsonr(estimates, data[-M:, j])

        # Build the reconstructed graph by finding significantly correlated
        # variables
        G = nx.DiGraph(pvalue.T < alpha)
        G.remove_edges_from(G.selfloop_edges())  # Filter out self-dependency

        # Save the graph object, matrices of correlation and p-values into the
        # "results" field (dictionary)
        self.results['graph'] = G
        self.results['correlation'] = correlation
        self.results['pvalue'] = pvalue

        return G
Infer causal relation applying Takens Theorem of dynamical systems.

        Convergent cross-mapping infers dynamical causal relation between
        vairiables from time series data. Time series data portray an attractor
        manifold of the dynamical system of interests. Existing approaches of
        attractor reconstruction involved building the shadow manifold for a
        single variable $X$, which is defined by the time-lagged vectors
        $(X(t), X(t-\tau), X(t-2\tau), ..., X(t-(N-1)\tau))$ where $N$ denotes
        number of variables in the system and $\tau$ is an arbitrary time-
        lagged interval. Takens theorem and its generalization indicated that
        if a variable $X$ is causally influencing another variable $Y$ in a
        dynamical system, then there exists a one-to-one and local-structure-
        preserving mapping from $X$'s shadow manifold to $Y$'s.

        The convergent cross-mapping algorithm first constructs the shadow data
        cloud (a portray of the shadow manifold) for every variable, and it
        then examines the mutual predictability for all pairs of them.
        Specifically, for a point $u(t)$ in $X$'s shadow data cloud, the time
        indices $\{t_1, t_2, ..., t_{N+1}\}$ of its $N+1$ nearest neighbors are
        obtained, which are mapped to a neighborhood in $Y$'s shadow data cloud
        $\{v(t_1), v(t_2), ..., v(t_{N+1})\}$. The estimate $\hat{Y}(t)$ is
        computed as an average over this neighborhood, with weights decaying
        exponentially with corresponding distance in $X$'s shadow data clould.
        The algorithm concludes a causal link from $X$ to $Y$ if correlation
        between $Y$ and $\hat{Y}$ is significant.

        Furthermore, Sugihara et al.'s simulation suggested that the
        correlation converging to 1 as the length of time series data grows is
        a necessary condition for $X$ causally affects $Y$ in a deterministic
        dynamical system. The convergent cross-mapping approach is thus
        numerically validated to infer causal relation from time series data.

        Params
        ------
        TS (np.ndarray): $N \times L$ array consisting of $L$ observations
                         from $N$ sensors.

        tau (int): Number of time steps for a single time-lag.

        alpha (float): Significance level of statistical test for correlation.

        Returns
        -------
        G (nx.Graph): A reconstructed graph with $N$ nodes.

        Notes
        -----
        1. The length of time series data must be long enough such that
           $L \geq 3 + (N-1)(1+\tau)$.

        2. The matrix of correlation between estimates and the observed values
           and the matrix of p-value for correlation test can be found in
           results['correlation'] and resutls['pvalue'] respectively.

        3. The $\[i, j\]$ of the correlation matrix entails the correlation
           between the $j$th variable and its estimate from the $i$th variable.
           A similar rule applies to the p-value matrix.

        4. The computation complexity of this implementation is $O(N^3 L)$.

        
Infer causal relation applying Takens Theorem of dynamical systems.
Convergent cross-mapping infers dynamical causal relation between
vairiables from time series data. Time series data portray an attractor
manifold of the dynamical system of interests.

The convergent cross-mapping algorithm first constructs the shadow data
cloud (a portray of the shadow manifold) for every variable, and it
then examines the mutual predictability for all pairs of them.

Furthermore, Sugihara et al.'s simulation suggested that the
correlation converging to 1 as the length of time series data grows is
a necessary condition for $X$ causally affects $Y$ in a deterministic
dynamical system. The convergent cross-mapping approach is thus
numerically validated to infer causal relation from time series data.

Params



tau (int): Number of time steps for a single time-lag.

alpha (float): Significance level of statistical test for correlation.

Returns

G (nx.Graph): A reconstructed graph with $N$ nodes.

Notes

1. The length of time series data must be long enough such that
$L \geq 3 + (N-1)(1+\tau)$.

2. The matrix of correlation between estimates and the observed values
and the matrix of p-value for correlation test can be found in
results['correlation'] and resutls['pvalue'] respectively.

3.

4. The computation complexity of this implementation is $O(N^3 L)$.
Infer causal relation applying Takens Theorem of dynamical systems . Convergent cross - mapping infers dynamical causal relation between vairiables from time series data . Time series data portray an attractor manifold of the dynamical system of interests . The convergent cross - mapping algorithm first constructs the shadow data cloud ( a portray of the shadow manifold ) for every variable and it then examines the mutual predictability for all pairs of them . Furthermore Sugihara et al . ' s simulation suggested that the correlation converging to 1 as the length of time series data grows is a necessary condition for $X$ causally affects $Y$ in a deterministic dynamical system . The convergent cross - mapping approach is thus numerically validated to infer causal relation from time series data . Params tau ( int ) : Number of time steps for a single time - lag . alpha ( float ) : Significance level of statistical test for correlation . Returns G ( nx . Graph ) : A reconstructed graph with $N$ nodes . Notes 1 . The length of time series data must be long enough such that $L \ geq 3 + ( N - 1 ) ( 1 + \ tau ) $ . 2 . The matrix of correlation between estimates and the observed values and the matrix of p - value for correlation test can be found in results [ ' correlation ' ] and resutls [ ' pvalue ' ] respectively . 3 . 4 . The computation complexity of this implementation is $O ( N^3 L ) $ .
def fit(self, TS, tau=1, alpha=0.05):
        data = TS.T  
        L, N = data.shape
        if L < 3 + (N-1) * (1+tau):
            message = 'Need more data.'
            message += ' L must be not less than 3+(N-1)*(1+tau).'
            raise ValueError(message)
        shadows = [shadow_data_cloud(data[:, i], N, tau) for i in range(N)]
        neighbors, distances = zip(*[nearest_neighbors(shad, L)
                                     for shad in shadows])
        weights = [neighbor_weights(dist) for dist in distances]
        correlation = np.ones((N, N), dtype=float)
        pvalue = np.zeros((N, N), dtype=float)
        for i, j in permutations(range(N), 2):
            estimates = time_series_estimates(data[:, j],
                                              neighbors[i], weights[i])
            M, = estimates.shape
            correlation[i, j], pvalue[i, j] = pearsonr(estimates, data[-M:, j])
        G = nx.DiGraph(pvalue.T < alpha)
        G.remove_edges_from(G.selfloop_edges())  
        self.results['graph'] = G
        self.results['correlation'] = correlation
        self.results['pvalue'] = pvalue
        return G
def fit ( self , TS , tau = 1 , alpha = 0.05 ) : data = TS . T L , N = data . shape if L < 3 + ( N - 1 ) * ( 1 + tau ) : message = 'Need more data.' message += ' L must be not less than 3+(N-1)*(1+tau).' raise ValueError ( message ) shadows = [ shadow_data_cloud ( data [ : , i ] , N , tau ) for i in range ( N ) ] neighbors , distances = zip ( * [ nearest_neighbors ( shad , L ) for shad in shadows ] ) weights = [ neighbor_weights ( dist ) for dist in distances ] correlation = np . ones ( ( N , N ) , dtype = float ) pvalue = np . zeros ( ( N , N ) , dtype = float ) for i , j in permutations ( range ( N ) , 2 ) : estimates = time_series_estimates ( data [ : , j ] , neighbors [ i ] , weights [ i ] ) M , = estimates . shape correlation [ i , j ] , pvalue [ i , j ] = pearsonr ( estimates , data [ - M : , j ] ) G = nx . DiGraph ( pvalue . T < alpha ) G . remove_edges_from ( G . selfloop_edges ( ) ) self . results [ 'graph' ] = G self . results [ 'correlation' ] = correlation self . results [ 'pvalue' ] = pvalue return G
Infer causal relation applying Takens Theorem of dynamical systems.
Infer causal relation applying Takens Theorem of dynamical systems .
"""Infer causal relation applying Takens Theorem of dynamical systems.

        Convergent cross-mapping infers dynamical causal relation between
        vairiables from time series data. Time series data portray an attractor
        manifold of the dynamical system of interests. Existing approaches of
        attractor reconstruction involved building the shadow manifold for a
        single variable $X$, which is defined by the time-lagged vectors
        $(X(t), X(t-\tau), X(t-2\tau), ..., X(t-(N-1)\tau))$ where $N$ denotes
        number of variables in the system and $\tau$ is an arbitrary time-
        lagged interval. Takens theorem and its generalization indicated that
        if a variable $X$ is causally influencing another variable $Y$ in a
        dynamical system, then there exists a one-to-one and local-structure-
        preserving mapping from $X$'s shadow manifold to $Y$'s.

        The convergent cross-mapping algorithm first constructs the shadow data
        cloud (a portray of the shadow manifold) for every variable, and it
        then examines the mutual predictability for all pairs of them.
        Specifically, for a point $u(t)$ in $X$'s shadow data cloud, the time
        indices $\{t_1, t_2, ..., t_{N+1}\}$ of its $N+1$ nearest neighbors are
        obtained, which are mapped to a neighborhood in $Y$'s shadow data cloud
        $\{v(t_1), v(t_2), ..., v(t_{N+1})\}$. The estimate $\hat{Y}(t)$ is
        computed as an average over this neighborhood, with weights decaying
        exponentially with corresponding distance in $X$'s shadow data clould.
        The algorithm concludes a causal link from $X$ to $Y$ if correlation
        between $Y$ and $\hat{Y}$ is significant.

        Furthermore, Sugihara et al.'s simulation suggested that the
        correlation converging to 1 as the length of time series data grows is
        a necessary condition for $X$ causally affects $Y$ in a deterministic
        dynamical system. The convergent cross-mapping approach is thus
        numerically validated to infer causal relation from time series data.

        Params
        ------
        TS (np.ndarray): $N \times L$ array consisting of $L$ observations
                         from $N$ sensors.

        tau (int): Number of time steps for a single time-lag.

        alpha (float): Significance level of statistical test for correlation.

        Returns
        -------
        G (nx.Graph): A reconstructed graph with $N$ nodes.

        Notes
        -----
        1. The length of time series data must be long enough such that
           $L \geq 3 + (N-1)(1+\tau)$.

        2. The matrix of correlation between estimates and the observed values
           and the matrix of p-value for correlation test can be found in
           results['correlation'] and resutls['pvalue'] respectively.

        3. The $\[i, j\]$ of the correlation matrix entails the correlation
           between the $j$th variable and its estimate from the $i$th variable.
           A similar rule applies to the p-value matrix.

        4. The computation complexity of this implementation is $O(N^3 L)$.

        """ # Transpose the time series to make observations the rows # Raise error if there is not enough data to run the implementation # Create shadow data cloud for each variable # Obtain nearest neighbors of points in the shadow data clould and # their weights for time series estimates # For every variable X and every other variable Y, # construct the estimates of Y from X's shadow data cloud and # compute the Pearson correlation between Y and its estimates # along with the p-value # Build the reconstructed graph by finding significantly correlated # variables # Filter out self-dependency # Save the graph object, matrices of correlation and p-values into the # "results" field (dictionary)
self
TS
tau
alpha