为什么我在Python代码错误和crumb错误中得到发散收敛测试?

yizd12fk  于 2023-07-01  发布在  Python
关注(0)|答案(1)|浏览(110)

这就是代码:

import numpy as np
import pandas as pd

import statsmodels
from statsmodels.tsa.stattools import coint
# just set the seed for the random number generator
np.random.seed(107)

import matplotlib.pyplot as plt

生成一个假的证券X,并通过从正态分布中绘制来模拟其每日回报。然后执行累积求和以获得每一天的X值。

# Generate daily returns
Xreturns = np.random.normal(0, 1, 100) 
# sum them and shift all the prices up
X = pd.Series(np.cumsum(
Xreturns), name='X') + 50
X.plot(figsize=(15,7))
plt.show()

产生与X有着深刻经济联系的Y,所以Y的价格应该与X的价格变化非常相似。

noise = np.random.normal(0, 1, 100)
Y = X + 5 + noise
Y.name = 'Y'
pd.concat([X, Y], axis=1).plot(figsize=(15,7))
plt.show()

绘制两者之间的比率:

(Y/X).plot(figsize=(15,7)) 
plt.axhline((Y/X).mean(), color='red', linestyle='--') 
plt.xlabel('Time')
plt.legend(['Price Ratio', 'Mean'])
plt.show()

# compute the p-value of the cointegration test
# will inform us as to whether the ratio between the 2 timeseries is stationary
# around its mean
score, pvalue, _ = coint(X,Y)
print (pvalue)

ret1 = np.random.normal(1, 1, 100)
ret2 = np.random.normal(2, 1, 100)

s1 = pd.Series( np.cumsum(ret1), name='X')
s2 = pd.Series( np.cumsum(ret2), name='Y')

pd.concat([s1, s2], axis=1 ).plot(figsize=(15,7))
plt.show()
print 'Correlation: ' + str(X_diverging.corr(Y_diverging))
score, pvalue, _ = coint(X_diverging,Y_diverging)
print 'Cointegration test p-value: ' + str(pvalue)

错误消息:文件“",第9行打印“相关性:' + str(X_diverging.corr(Y_diverging))语法错误:无效语法

Y2 = pd.Series(np.random.normal(0, 1, 800), name='Y2') + 20
Y3 = Y2.copy()

Y3[0:100] = 30
Y3[100:200] = 10
Y3[200:300] = 30
Y3[300:400] = 10
Y3[400:500] = 30
Y3[500:600] = 10
Y3[600:700] = 30
Y3[700:800] = 10
Y2.plot(figsize=(15,7))
Y3.plot()
plt.ylim([0, 40])
plt.show()
# correlation is nearly zero
print 'Correlation: ' + str(Y2.corr(Y3))
score, pvalue, _ = coint(Y2,Y3)
print 'Cointegration test p-value: ' + str(pvalue)

错误消息:文件“",第14行打印“相关性:' + str(Y2.corr(Y3))语法错误:无效语法

def find_cointegrated_pairs(data):
n = data.shape[1]
score_matrix = np.zeros((n, n))
pvalue_matrix = np.ones((n, n))
keys = data.keys()
pairs = []
for i in range(n):
    for j in range(i+1, n):
        S1 = data[keys[i]]
        S2 = data[keys[j]]
        result = coint(S1, S2)
        score = result[0]
        pvalue = result[1]
        score_matrix[i, j] = score
        pvalue_matrix[i, j] = pvalue
        if pvalue < 0.02:
            pairs.append((keys[i], keys[j]))
 return score_matrix, pvalue_matrix, pairs

pip安装auquan-toolbox并执行以下代码片段:

from backtester.dataSource.yahoo_data_source import YahooStockDataSource
 from datetime import datetime
 startDateStr = '2007/12/01'
 endDateStr = '2017/12/01'
 cachedFolderName = 'yahooData/'
 dataSetId = 'testPairsTrading'
 instrumentIds = ['SPY','AAPL','ADBE','SYMC','EBAY','MSFT','QCOM',
             'HPQ','JNPR','AMD','IBM']
 ds = YahooStockDataSource(cachedFolderName=cachedFolderName,
                        dataSetId=dataSetId,
                        instrumentIds=instrumentIds,
                        startDateStr=startDateStr,
                        endDateStr=endDateStr,
                        event='history')
 data = ds.getBookDataByFeature()['Adj Close']
 data.head(3)

错误消息:文件“C:\ProgramData\Anaconda 3\lib\site-packages\backtester\dataSource\data_source_utils.py”,第25行,在getCookieForYahoo返回cookie,crumb #返回crumb和cookie的元组
UnboundLocalError:在赋值前引用局部变量“crumb”
Complete article with code can be found here
任何帮助都是非常感谢的。谢谢你。

ql3eal8s

ql3eal8s1#

关于print函数的错误,您会遗漏括号。
例如,此错误:
错误消息:文件“",第9行打印“相关性:' + str(X_diverging.corr(Y_diverging))语法错误:无效语法
是由第9行引起的:

print 'Correlation: ' + str(X_diverging.corr(Y_diverging))

其应为:

print('Correlation: ' + str(X_diverging.corr(Y_diverging)))

相关问题