[PYTHON] [Note] Market anomaly detection using Minimum Covariance Determinant

Anomaly detection comparison by covariance matrix obtained by Minimum Covariance Determinant or MLE

Is it a method that is compatible with market data in order to avoid being dragged by the value that flew?


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

Weekly market returns on ReadDF (forex, stocks, bonds, etc.)

#Lookback Calculated using returns for the last 50 weeks

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())  #Standardization
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

Recommended Posts

[Note] Market anomaly detection using Minimum Covariance Determinant
Anomaly detection using MNIST by Autoencoder (PyTorch)