当前位置: 首页>>代码示例>>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;未经允许,请勿转载。