1
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【忘備録】Minimum Covariance Determinantを使用したマーケットの異常探知

Last updated at Posted at 2019-12-30

Minimum Covariance Determinant or MLEで求めた共分散行列による異常探知比較

飛んだ値に思いっ切り引きずられるのを避けるためにも、マーケットデータと相性良いメソッドかと


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
from sklearn.covariance import EmpiricalCovariance, MinCovDet
from sklearn.covariance import EmpiricalCovariance

warnings.filterwarnings('ignore')

plt.style.use('seaborn-darkgrid')
plt.rcParams['axes.xmargin'] = 0.01
plt.rcParams['axes.ymargin'] = 0.01

ReadDFに週次の各種マーケットリターン(為替、株、債券etc)

# ルックバック過去50週間リターンを用いて算出

ts_out = pd.DataFrame()
for date in ReadDF.dropna(axis=0)[50:].index:
    x = ReadDF[:date][-50:]
    x = (x/x.std()).dropna(axis=0)
    
    mcd.fit(x[:-1])
    anomaly_score_mcd = mcd.mahalanobis(x[-1:])
    
    mle.fit(x[:-1])
    anomaly_score_mle = mle.mahalanobis(x[-1:])
    
    out = pd.DataFrame([anomaly_score_mcd, anomaly_score_mle]).T
    
    out.columns = ['mcd', 'mle']
    out.index = [date]
    
    ts_out = pd.concat([ts_out, out], axis=0)
fig = plt.figure(figsize=(15, 10), dpi=80)
out = pd.DataFrame(ts_out/ts_out.std())  # 規格化
sns.set_palette("hls", len(out.columns))
ax1 = fig.add_subplot(1, 1, 1)
ax1.plot(out, alpha=0.6)
plt.legend(out.columns)
plt.show()

Anomaly.png

1
6
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
1
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?