USDJPYの動きを予測する2

やること

USDJPYの動きを予測する。

間違えていたこと

T時点での各種特徴量をもとにT+1時点での動き(上がるor下がる)を予測するはずなのに、T時点での特徴量からT時点での動きを予測していた、、

正しい方法でやってみた

data.head(5)
 UpDown  MA5 MA20    RSI MA5-MA20
date                    
2002-05-03  1   -0.126412   -0.168527   45.147580   0.042114
2002-05-06  0   -0.113726   -0.165258   54.949230   0.051532
2002-05-07  1   -0.064440   -0.100299   55.895686   0.035859
2002-05-08  1   0.236344    -0.062955   56.392661   0.299300
2002-05-09  1   0.075608    -0.112981   46.391520   0.188589
#  random forest
features = data.drop('UpDown', axis = 1).values
label = data['UpDown']

X_train, X_test, y_train, y_test = train_test_split(features, label, test_size = 0.3, random_state = 1234)

clf = RandomForestClassifier(random_state = 1234)
clf.fit(X_train, y_train)

#  評価
print('Test score: {}'.format(clf.score(X_test, y_test)))
print('Train score: {}'.format(clf.score(X_train, y_train)))
print('f1 score: {:.3f}'.format(f1_score(y_test, clf.predict(X_test))))
Test score: 0.5743626062322946
Train score: 0.9744990892531876
f1 score: 0.550

RSIについて

RSIの分布は変わらないけど、範囲区切ったときの上がるor下がるは変わった

#  RSIの検証
rsi = data['RSI']

# ax = sns.distplot(rsi, kde = False, rug = False, bins = 20)
# fig = ax.get_figure()
# fig.savefig('output/fig1.png')


adj_rsi = rsi.apply(lambda x:1 if x > 55 else (-1 if x < 45 else 0))
adj_rsi.name = 'adjRSI'

view = pd.concat([data['UpDown'], adj_rsi], axis = 1)
ax = sns.countplot(x = 'adjRSI', hue = 'UpDown', data = view)
fig = ax.get_figure()
fig.savefig('output/fig1.png')

f:id:iiiiikamirin:20200907073854p:plain

全然予測に役に立ってなさそう。。

RSIをラベルとする

前回みたいにRSIについて55以上なら1, -45以下なら-1, その他は0というラベルとする

adj_rsi = rsi.apply(lambda x:1 if x > 55 else (-1 if x < 45 else 0))
adj_rsi.name = 'adjRSI'

#  RSI to adjRSI
data = data.drop('RSI', axis = 1)
data = pd.concat([data, adj_rsi], axis = 1)
data.head(5)
 UpDown  MA5 MA20    MA5-MA20    adjRSI
date                    
2002-05-03  1   -0.126412   -0.168527   0.042114    0
2002-05-06  0   -0.113726   -0.165258   0.051532    0
2002-05-07  1   -0.064440   -0.100299   0.035859    1
2002-05-08  1   0.236344    -0.062955   0.299300    1
2002-05-09  1   0.075608    -0.112981   0.188589    0
#  random forest
features = data.drop('UpDown', axis = 1).values
label = data['UpDown']

X_train, X_test, y_train, y_test = train_test_split(features, label, test_size = 0.3, random_state = 1234)

clf = RandomForestClassifier(random_state = 1234)
clf.fit(X_train, y_train)

#  評価
print('Test score: {}'.format(clf.score(X_test, y_test)))
print('Train score: {}'.format(clf.score(X_train, y_train)))
print('f1 score: {:.3f}'.format(f1_score(y_test, clf.predict(X_test))))
Test score: 0.5630311614730878
Train score: 0.9681238615664846
f1 score: 0.547

まあなんかほぼ変わんない結果にやっぱりなった