神刀安全网

Kolmogorov-Smirnov test as regime switcher

Kolmogorov-Smirnov test is a nonparametric test of the equality of continuous distribution that can be used to compare a sample with a reference probability distribution. KS-test is usually referred as goodness of fit test, but also it’s test for "normality" (if our reference distribution is normal).

Below you’ll find same strategy as previous one using Shapiro-Wilk test for normality . We compare our month of close to close returns to normal distribution, get probability of null hypothesis and 2 months later we buy when it is below some threshold. Meaning we bet on abnormal positive returns. What it looks like at first sight, p-values (scaled) are high for environment before and during 2008 crisis:

Kolmogorov-Smirnov test as regime switcher

And return of the strategy:

Kolmogorov-Smirnov test as regime switcher

It seems that higher return is due to avoiding the biggest drawdowns.

FullPython code:

import pymongo from pymongoimport MongoClient import pandasas pd import numpyas np import datetime import matplotlib.pyplotas plt import seabornas sns; sns.set(color_codes=True) from scipyimport stats   dbase = "lean" client = MongoClient() db = client[dbase]   def read_mongo(dbase, collection, query={}):     # Connect     client = MongoClient()     db = client[dbase]     #query if needed     cursor = db[collection].find(query).sort('DATE_TIME')     #expand cursor and create dataframe          df =  pd.DataFrame(list(cursor))          if len(df) > 0:         df.DATE_TIME = pd.to_datetime(df.DATE_TIME, format='%Y-%m-%d %H:%M:%S')         # Delete the _id         del df['_id']         df = df.set_index('DATE_TIME')          return df   #variables symbols = ['S&P5001440'] threshold = 0.02   #script vals = [] prices = [] for symin symbols:     print sym     #construct dates     y = 0     yclean = 0     for perin range(1, 12*10):         yclean = int(per/12)         y = int(per/12) + 2006         m = per - yclean*12 + 1         m1 = per - yclean*12 + 2                  if m < 10:             mStr = "0"+str(m)         else:             mStr = str(m)                  if m1 < 10:             mStr1 = "0"+str(m1)         else:             mStr1 = str(m1)                  yStr = str(y)         yStr1 = str(y)                      if m1 == 13:             mStr1 = "01"             yStr1 = str(y+1)                  #get data         dta = read_mongo(dbase, sym, query={'DATE_TIME': {'$gte': ''+yStr+'-'+mStr+'-01', /                                                           '$lt': ''+yStr1+'-'+mStr1+'-01'}})         #kolmogorov-smirnof         __, kolmogorov_test = stats.kstest(rvs=dta.CLOSE.pct_change().dropna().as_matrix(), cdf='norm')                  #get price at the time of shapiro wilk value         prices.append(dta.CLOSE.dropna().as_matrix()[-1])                  #add kolmogorov-smirnofvalues to list         vals.append(kolmogorov_test)                   #signals part     ret = pd.DataFrame(prices).pct_change()     ks = pd.DataFrame(vals).shift(2) * 1000.0     returns = np.where(ks < threshold, ret, 0)       #scatter relationship     plt.scatter(x=ret[1:], y=ks[1:])     plt.axhline(threshold, color = 'r', xmax=5)     plt.show()       #return curve     plt.plot(returns.cumsum())     plt.show()       #curves relationship     plt.plot(ret, color='g')     plt.plot(ks)     plt.axhline(threshold, color = 'r', xmax=5)     plt.show() 

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Kolmogorov-Smirnov test as regime switcher

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址