LoginSignup
0
2

More than 3 years have passed since last update.

ML 為替予測の為のデータ分析-月毎のtick数比較分析-

Last updated at Posted at 2019-10-14

前提

本件は、為替による通貨変動を機械学習にて予測する為の事前準備として収集したデータの分析を実施する。
私自身は学歴として高卒ではあるが、数学は中学程度しか理解していない。
その為、分析方法・数式の書き方に不備が有るが了承頂きたい。

資料その他

本件にて利用する為替データは、Tickstoryより取得したGBPUSDの日足データを利用する。
分析環境は、google Colaboratoryを利用する。

趣旨

為替において月毎にボラリティに特色があると仮定し前回月毎のボラリティ比率をグラフ化したが、全体的に平坦で有り、10月−1月までの上昇傾向以外に特出する傾向は見受けあれなかった。
その為、今回は各月のtick数の比率をグラフ化し前回結果と比較し傾向を考察する。

※前回投稿した「ML 為替予測の為のデータ分析-月毎のボラリティ比較分析-」は「参考」項を参照

仮説

今回集計する各月の年次におけるtick数比率により、各月毎の活性度傾向を視覚化出来る。
tick数の変動は前回集計した、各月の年次におけるボラリティ比率に比例すると仮定する。
経緯としては、各月の活性度が高くなるにつれボラリティも上昇するとみる。

定義

今回利用する各項目を下記と置き
・日足の最大値:y
・日足の最小値:z
・Tick数:cnt
・月の日数:n
・月毎のボラリティ:A
 ・月毎のtick数:B

年次に於ける月毎のボラリティ比率

前回月のボラリティを下記の通り定義

A=\frac{\sum_{k=1}^{n}(\frac{|x_k-y_k|}{cnt})}{n}

上記を基に年次に於ける月毎のボラリティ比率を下記の通り定義

年間の各月のボラリティ比率=\frac{A_i}{\sum_{i=1}^{12}{A_i}}

年次に於ける月毎のtick数比率

今回も同様に月に於ける1日の平均tick数を計算する。

B=\frac{\sum_{k=1}^{n}(cnt_i)}{n}

年間に於ける各月のtick数比率は下記と定義

年間の各月のtick数比率=\frac{B_k}{\sum_{k=1}^{12}{B_k}}

ソース

①事前準備

ライブラリのインポート

import pandas as pd #表を用いた統計学的計算を実施する為のライブラリ
import matplotlib.pyplot as plt #グラフ描写の為に定義

日足データを格納しているgoogle driveをマウント

from google.colab import drive
drive.mount('/content/drive')

※ コマンドを実行し、google driveの認証処理を実行する。

日足データを取得し、dateカラムをインデックスに登録

csvT = pd.read_csv('/content/drive/My Drive/1day/GBPUSD2004_2018_1day.csv'  )
csvT.date = pd.to_datetime(csvT.date)
csvT.set_index('date',inplace=True)

読み込んだデータから必要情報を計算し、カラムとして追加

csvT['days'] = csvT.index.day # [days]カラムに対象レコードの日付を格納
csvT['month'] = (csvT.index.month) # [month]カラムに対象レコードの月を格納

#### グラフ表示の為の準備

fig = plt.figure(figsize=(30, 10), dpi=70)
ax = fig.add_subplot(1,1,1)
year_ary = set(csvT.index.year) # csvデータから収集されている年次情報を取得
```

表を下記の通り作成

df = pd.DataFrame(columns=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],index=year_ary)

②計算

年次毎にレコードを作成

for year_int,year_str in enumerate(year_ary):
  tmpstr = []

月毎に平均tick数を計算し一時変数へ配列として格納

  for moths in range(1,13):
    csvT_test = csvT[str(year_str) + '-' + str(moths)]
    cnt_CSV=len(csvT_test)
    tmpstr.append(csvT_test.tickCount.sum() / cnt_CSV)
  arry_total = sum(tmpstr)
  tmp_arry = []

年間のtick数比率を変数へ格納

  arry_total = sum(tmpstr)

年次における各月のボラリティの変動比率を計算し一時変数へ格納

 for cnt,i in enumerate(tmpstr):
    tmp_arry.append(i / arry_total)

グラフ表示用の表の各年次に紐づくレコードへ格納する

  df.loc[year_str] = tmp_arry

③表へ出力

年次毎に繰り返しグラフへ格納

year_ary = set(df.index)
for year_int,year_str in enumerate(year_ary):
  ax.plot(df.loc[year_str], label=str(year_str) )
plt.legend()
plt.show()

スクリーンショット 2019-10-14 15.04.10.png

結果

年次に於けるtick数の推移

スクリーンショット 2019-10-14 15.12.40.png

年次に於ける月毎のtick数比率

スクリーンショット 2019-10-14 15.04.10.png

前回収集した年次に於ける月毎のボラリティ比率

スクリーンショット 2019-10-14 15.36.31.png

ソース

今回利用したソースは下記となる

import pandas as pd
import matplotlib.pyplot as plt

from google.colab import drive
drive.mount('/content/drive')

csvT = pd.read_csv('/content/drive/My Drive/1day/GBPUSD2004_2018_1day.csv'  )
csvT.date = pd.to_datetime(csvT.date)
csvT.set_index('date',inplace=True)

csvT['days'] = csvT.index.day
csvT['month'] = (csvT.index.month)

ig = plt.figure(figsize=(30, 10), dpi=70)
ax = fig.add_subplot(1,1,1)
year_ary = set(csvT.index.year)

df = pd.DataFrame(columns=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],index=year_ary)

for year_int,year_str in enumerate(year_ary):
  tmpstr = []
  for moths in range(1,13):
    csvT_test = csvT[str(year_str) + '-' + str(moths)]
    cnt_CSV=len(csvT_test)
    tmpstr.append(csvT_test.tickCount.sum() / cnt_CSV)
  arry_total = sum(tmpstr)
  tmp_arry = []
  arry_total = sum(tmpstr)
 for cnt,i in enumerate(tmpstr):
    tmp_arry.append(i / arry_total)
  df.loc[year_str] = tmp_arry
year_ary = set(df.index)
for year_int,year_str in enumerate(year_ary):
  ax.plot(df.loc[year_str], label=str(year_str) )
plt.legend()
plt.show()

参考

ML 為替予測の為のデータ分析-月毎のボラリティ比較分析-

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2