コード例 #1
0
ファイル: Figure1.py プロジェクト: ZacKeskin/keskin_aste_rsos
        T = 1

        ## Generate coupled Gaussian Random Walks
        DF = coupled_random_walks(  S1 = S1, S2 = S2, T = T, 
                                    N = N, mu1 = mu1, mu2 = mu2, 
                                    sigma1 = sigma1, sigma2 = sigma2, 
                                    alpha = SIMILARITY, epsilon = 0,
                                    lag = LAG, seed = None)

        ## Difference data using % change (Gaussian transition probability distribution)
        DF = DF.pct_change().iloc[1:]


        ## Calculate Linear TE using Geweke-Granger Causality Formula
        PC = TransferEntropy(DF = DF,
                             endog = 'S2',
                             exog = 'S1',
                             lag = LAG)
        PC.linear_TE(n_shuffles=N_SHUFFLES)

        ## Update results with linear TE from X->Y and from Y->X
        (lTE[i,j,0], lTE[i,j,1]) = (PC.results['TE_linear_XY'], PC.results['TE_linear_YX'])
        (lZ[i,j,0], lZ[i,j,1]) = (PC.results['z_score_linear_XY'], PC.results['z_score_linear_YX'])

        ## Note equiprobable bins is a misnomer; these bins are a marginal-equiprobable partition
        AB = AutoBins(DF,LAG)                       
        equal_bins = AB.equiprobable_bins(N_BINS)

        ## Calculate TE using information-theoretic method
        PC.nonlinear_TE(pdf_estimator = 'histogram',
                        bins = equal_bins,
                        n_shuffles = N_SHUFFLES)
コード例 #2
0
                                  sigma1=0.5,
                                  sigma2=0.25,
                                  alpha=alpha,
                                  epsilon=AUTOSIMILARITY,
                                  lag=LAG,
                                  seed=None)

        ## To get data which is Gaussian distributed, we use a scaled %-change with mu = 0
        DF = DF.pct_change().iloc[1:]

        #plot_pdf(DF,show=True)

        ## Initialise TE object
        causality = TransferEntropy(
            DF=DF,
            endog='S2',  # Dependent Variable
            exog='S1',  # Independent Variable
            lag=LAG)

        ## Generate histogram bins
        auto = AutoBins(DF[['S1', 'S2']], lag=LAG)
        edges = auto.sigma_bins(max_bins=5)

        ## Calculate TE using Histogram
        TE_Hists[observation,
                 i, :] = causality.nonlinear_TE(pdf_estimator='histogram',
                                                bins=edges,
                                                n_shuffles=N_SHUFFLES)
        Z_Hists[observation, i, :] = (causality.results['z_score_XY'].iloc[0],
                                      causality.results['z_score_YX'].iloc[0])
コード例 #3
0
ファイル: coupled_map.py プロジェクト: yangxhcaf/PyCausality
            ## Randomise initial conditions
            S1 = np.random.rand()
            S2 = np.random.rand()
            DF = coupled_logistic_map(S1=S1,
                                      S2=S2,
                                      T=1,
                                      N=N,
                                      alpha=0.4,
                                      epsilon=1,
                                      r=4)

            ## Initialise TE object
            causality = TransferEntropy(
                DF=DF,
                endog='S2',  # Dependent Variable
                exog='S1',  # Independent Variable
                lag=1)

            ## Calculate NonLinear TE
            (TE_XY, TE_YX) = causality.nonlinear_TE(pdf_estimator='histogram',
                                                    bins=bins,
                                                    n_shuffles=N_SHUFFLES)

            observations.append((TE_XY, TE_XY))

        ## Store average over multiple observations
        observations = np.array(observations).reshape(N_OBSERVATIONS, 2)

        TE_XYs.append(np.average(observations[:, 0]))
        errors.append(np.std(observations[:, 0]))
コード例 #4
0
DF.index = pd.to_datetime(DF.index, unit='s') 

## Forward-Fill Missing Data
DF = DF[[SM,Return]].replace(0, np.nan).fillna(method='ffill')


## Difference the data using log-returns
DF[SM] = np.log(DF[SM]).diff()
DF[Return] = np.log(DF[Return]).diff()
DF = DF[1:-1]


## Initialise TE Objects
linearPC = TransferEntropy(DF = DF,
                           endog = Return,
                           exog = SM,
                           lag = LAG,
                           window_size = WINDOW_SIZE,
                           window_stride = WINDOWS_STRIDE)

nonlinearPC = TransferEntropy(DF = DF,
                              endog = Return,
                              exog = SM,
                              lag = LAG,
                              window_size = WINDOW_SIZE,
                              window_stride = WINDOWS_STRIDE)


## Calculate marginal equipartion
DF = LaggedTimeSeries(DF, LAG).df
auto = AutoBins(DF, LAG)
equal_bins = auto.equiprobable_bins(n_bins)
コード例 #5
0
TEs_YX = []
z_scoresXY = []
z_scoresYX = []

##------------------------------ Compare TE at Different Lags ------------------------------##

for LAG in LAGs:
    print('LAG', LAG)

    ## Create Lagged Time Series
    DF = LaggedTimeSeries(walks, LAG).df

    ## Initialise TE object
    linear_causality = TransferEntropy(
        DF=DF,
        endog='S2',  # Dependent Variable
        exog='S1',  # Independent Variable
        lag=LAG)
    causality = TransferEntropy(
        DF=DF,
        endog='S2',  # Dependent Variable
        exog='S1',  # Independent Variable
        lag=LAG)

    ## Define Bins
    bins = {
        'S1': [-5, -2.5, -1, -0.5, 0, 0.5, 1, 2.5, 5],
        'S1_lag' + str(LAG): [-5, -2.5, -1, -0.5, 0, 0.5, 1, 2.5, 5],
        'S2': [-5, -2.5, -1, -0.5, 0, 0.5, 1, 2.5, 5],
        'S2_lag' + str(LAG): [-5, -2.5, -1, -0.5, 0, 0.5, 1, 2.5, 5]
    }
コード例 #6
0
                                  sigma1=0.05,
                                  sigma2=0.05,
                                  alpha=SIMILARITY,
                                  epsilon=AUTOSIMILARITY,
                                  lag=LAG,
                                  seed=SEED)
        ## For Gaussian-Distributed data, a scaled %-change with mu = 0
        DF = DF.pct_change().iloc[1:]

        ## Calculate TE for different bins
        for j, n_bins in enumerate(N_BINS):
            print('bins:', n_bins)

            ## Initialise TE objects
            causality_equal = TransferEntropy(DF=DF,
                                              endog='S2',
                                              exog='S1',
                                              lag=LAG)
            causality_MIC = TransferEntropy(DF=DF,
                                            endog='S2',
                                            exog='S1',
                                            lag=LAG)
            causality_Sigma = TransferEntropy(DF=DF,
                                              endog='S2',
                                              exog='S1',
                                              lag=LAG)

            ## Initialise bins
            equal_bins = {
                'S1':
                list(np.linspace(DF['S1'].min(), DF['S1'].max(), n_bins + 1)),
                'S1_lag' + str(LAG):