當前位置: 首頁>>代碼示例>>Python>>正文


Python stattools.coint方法代碼示例

本文整理匯總了Python中statsmodels.tsa.stattools.coint方法的典型用法代碼示例。如果您正苦於以下問題:Python stattools.coint方法的具體用法?Python stattools.coint怎麽用?Python stattools.coint使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在statsmodels.tsa.stattools的用法示例。


在下文中一共展示了stattools.coint方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: coint

# 需要導入模塊: from statsmodels.tsa import stattools [as 別名]
# 或者: from statsmodels.tsa.stattools import coint [as 別名]
def coint(df, intercept = True, sig_level = 0.01):
    """
    Find pairs (of 2 time series) that passes the cointegration test.

    Parameters
    ----------
    df: pandas dataframe
        each column is the time series of a certain stock
    
    intercept: boolean
        if True, OLS and ADF test are done manually
        if False, the coint() function from statsmodels.tsa.stattools, which 
        does not include intercept term while doing OLS regression, is used.
    
    sig_level: if p_value of cointegration test is below this level, then we
        can reject the NULL hypothesis, which says that the two series are not
        cointegrated
    
    Return
    ------
    A list of tuples of the form (name of stock 1, name of stock 2, p_value of
    cointegration test).
    
    """
    cointegrated_pairs = []
    
    stock_names = df.columns.values.tolist()
    N = len(stock_names)
    
    stock_pairs = list(itertools.combinations(stock_names, 2))
    
    for pair in stock_pairs:
        stock_1, stock_2 = pair

        p_value = 0

        if not intercept:
            p_value = smts.coint(df[stock_1].values.astype(float), df[stock_2].values.astype(float), trend='c')[1]
        else:
            Y = df[stock_1].values.astype(float)
            X = df[stock_2].values.astype(float)
            X = sm.add_constant(X)

            model = sm.OLS(Y, X)
            results = model.fit()
            intercept, slope = results.params

            p_value = smts.adfuller(results.resid)[1]

        if p_value < sig_level and slope > 0:
            cointegrated_pairs.append(tuple([stock_1, stock_2, p_value]))

    return cointegrated_pairs 
開發者ID:wywongbd,項目名稱:pairstrade-fyp-2019,代碼行數:55,代碼來源:pair_selector.py

示例2: get_p_value

# 需要導入模塊: from statsmodels.tsa import stattools [as 別名]
# 或者: from statsmodels.tsa.stattools import coint [as 別名]
def get_p_value(x, y):
        _, p_val, _ = coint(x, y)
        return p_val 
開發者ID:wai-i,項目名稱:Pair-Trading-Reinforcement-Learning,代碼行數:5,代碼來源:Cointegration.py

示例3: find_cointegrated_pairs

# 需要導入模塊: from statsmodels.tsa import stattools [as 別名]
# 或者: from statsmodels.tsa.stattools import coint [as 別名]
def find_cointegrated_pairs(data):
    n = data.shape[1]
    pvalue_matrix = np.ones((n, n))
    keys = data.keys()
    pairs = []
    for i in range(n):
        for j in range(i+1, n):
            result = coint(data[keys[i]], data[keys[j]])
            pvalue_matrix[i, j] = result[1]
            if result[1] < 0.02:
                pairs.append((keys[i], keys[j]))
    return pvalue_matrix, pairs 
開發者ID:PacktPublishing,項目名稱:Learn-Algorithmic-Trading---Fundamentals-of-Algorithmic-Trading,代碼行數:14,代碼來源:ch4_pairs_correlation_init.py


注:本文中的statsmodels.tsa.stattools.coint方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。