-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathStatistics.py
59 lines (57 loc) · 2.04 KB
/
Statistics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pandas as pd
import numpy as np
import scipy.stats
class Statistics:
def __init__(self,series):
self.series = np.array(series)
self.n = len(series)
def mean(self):
return np.mean(self.series)
def std(self):
return np.std(self.series)
def stderr(self):
return scipy.stats.sem(self.series)
def percentiles(self,p=[.25,.5,.75]):
return pd.Series(self.series).describe(percentiles=p)
def pos_perc(self):
return 100*sum(self.series>0)/self.n
def skewness(self):
return scipy.stats.skew(self.series)
def kurtosis(self):
return scipy.stats.kurtosis(self.series)
def VaR(self,confidence):
indx = int(confidence*self.n/100)
return sorted(self.series)[indx-1]
def CVaR(self,confidence):
indx = int(confidence*self.n/100)
return sum(sorted(self.series)[:indx])/indx
def MDD(self):
money = np.cumprod(1+self.series/100)
maximums = np.maximum.accumulate(money)
drawdowns = 1 - money/maximums
return np.max(drawdowns)
def sharpe(self,risk_free_rate = 0.0003):
mu = self.mean()
sig = self.std()
sharpe_d = (mu-risk_free_rate)/sig
return (252**0.5)*sharpe_d
def shortreport(self):
print('Mean \t\t',self.mean())
print('Standard dev \t',self.std())
print('Sharpe ratio \t',self.sharpe())
def report(self):
print('Mean \t\t',self.mean())
print('Standard dev \t',self.std())
print('Sharpe ratio \t',self.sharpe())
print('Standard Error \t',self.stderr())
print('Share>0 \t',self.pos_perc())
print('Skewness \t',self.skewness())
print('Kurtosis \t',self.kurtosis())
print('VaR_1 \t\t',self.VaR(1))
print('VaR_2 \t\t',self.VaR(2))
print('VaR_5 \t\t',self.VaR(5))
print('CVaR_1 \t\t',self.CVaR(1))
print('CVaR_2 \t\t',self.CVaR(2))
print('CVaR_5 \t\t',self.CVaR(5))
print('MDD \t\t',self.MDD())
print(self.percentiles())