-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.py
298 lines (227 loc) · 11.6 KB
/
stats.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import numpy as np
import pandas as pd
from typing import Union
def calculate_regime_durations(regimes_df: pd.DataFrame) -> pd.DataFrame:
"""
Calculate regime durations.
Args:
regimes_df (DataFrame): DateFrame that includes columns (`'DATE'`, `'regime'`). Other columns will be ignored.
Returns:
regime_durations (DataFrame): DateFrame only with columns (`'regime'`, `'period'`, `'duration'`).
"""
regimes_df = regimes_df[['DATE', 'regime']]
regimes_df['regime_if_new'] = regimes_df['regime'] != regimes_df['regime'].shift(1)
regimes_df['period'] = regimes_df['regime_if_new'].cumsum()
regime_durations = regimes_df.groupby(by=['regime', 'period']).size().reset_index(name='duration')
return regime_durations
def calculate_regime_durations_statistics(regime_durations: pd.DataFrame) -> pd.DataFrame:
"""
Calculate mean, std, 25, 50, 70 quantiles for regime durations and the total days and the corresponding percentage for each regime and all regime periods.
Args:
regimes_durations (DataFrame): dataframe that includes columns (`'regime'`, `'duration'`). Other columns will be ignored.
Note, the 'duration'` is the duration of the period when the regime keeps the same. Not the total duration of a regime.
Returns:
statistics (DataFrame): dataframe with columns (`'regime'`, `'mean'`, `'std'`, `'count'`, `'25 quantile'`, `'median'`, `'75 quantile'`, `'percent'`).
"""
total_dates = regime_durations['duration'].sum()
# for each regime
statistics = {
regime:
np.concatenate(
(
[dura['duration'].mean(), dura['duration'].std(), dura['duration'].sum()],
np.quantile(dura['duration'], [0.25,0.5,0.75])
)
)
for regime, dura in regime_durations.groupby(by='regime')
}
# for the whole period
statistics['all'] = np.concatenate(
(
[regime_durations['duration'].mean(), regime_durations['duration'].std(), regime_durations['duration'].sum()],
np.quantile(regime_durations['duration'], [0.25,0.5,0.75])
)
)
# to DateFrame
statistics = pd.DataFrame.from_dict(statistics, orient='index', columns=['mean','std','count', '25 quantile','median','75 quantile'])
# add 'percent'
statistics['percent'] = statistics['count'] / total_dates
statistics = statistics.reset_index(names='regime')
return statistics
def calculate_return_metrics(
df: pd.DataFrame,
freq_alias: str | None = None,
freq_length: int | None = 1
):
"""
Calculate 5 metrics for returns - average return, std, annualized return, annualized std, sharpe ratio. Note, the return period used for
the average return and std are givin by `freq_alias` and `freq_length` (and alias and length should be constant). For example,
alias `'W'` and length 5 for average weekly return.
Args:
df (DataFrame): the data only with columns (`'DATE'`, ...) (the other columns are returns).
freq_alias (str | None): alias for the length of the return period for average return and std. This can be `'W'`, `'2W'`, `'ME'`.
Default is `None` for 1 day. Full list see https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases.
freq_length (int): length of the return period (in days). Default to `1`.
Returns:
metrics (DataFrame): dataframe with indices as indexes and metrics as columns.
"""
# group by frequency
if freq_alias is not None:
# not to use sum() here because there may be some missing data during the workdaying during the return period
df_returns = df.groupby(pd.Grouper(key='DATE',freq=freq_alias),sort=False).mean()*freq_length
else:
df_returns = df.drop(columns='DATE')
# calculate metrics
average_return = df_returns.mean()
std = df_returns.std()
ann_average_return = (252/freq_length) * average_return
ann_std = np.sqrt(252/freq_length) * std
sharpe_ratio = ann_average_return / ann_std
metrics = {
'average_return' : average_return,
'std' : std,
'ann_return' : ann_average_return,
'ann_std' : ann_std,
'sharpe_ratio' : sharpe_ratio
}
return pd.DataFrame(metrics)
def calculate_return_metrics_within_regime(
df: pd.DataFrame,
freq_alias: str | None = None,
freq_length: int | None = 1
) ->pd.DataFrame:
"""
Calculate 5 metrics for returns - average return, std, annualized return, annualized std, sharpe ratio within each regime.
Note, the return period used for the average return and std are givin by `freq_alias` and `freq_length` (and alias and length should be constant).
For example, alias `'W'` and length 5 for average weekly return.
Args:
df: the data only with columns (`'DATE'`, `'regime'`, ...) (the other columns are returns).
freq_alias (str | None): alias for the length of the return period for average return and std. This can be `'W'`, `'2W'`, `'ME'`.
Default is `None` for 1 day. Full list see https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases.
freq_length (int): length of the return period (in days). Default to `1`.
Returns:
results (dict): dict with regimes as keys as dataframe as values. The dataframe has indices as indexes and metrics as columns.
"""
results = df.groupby('regime').apply(
calculate_return_metrics,
freq_alias = freq_alias,
freq_length = freq_length,
include_groups=False
).reset_index(names=['regime', 'index'])
return results
def calculate_return_metrics_last_date_of_period(
df: pd.DataFrame,
freq_alias: str | None = None,
freq_length: int | None = 1
) -> pd.DataFrame:
"""
Calculate average return, std, annualized return, annualized std, sharpe ratio for the last day of a period within each regime.
Args:
df (DataFrame): the data only with columns (`'DATE'`, `'regime'`, ...) (the other columns are returns).
freq_alias (str | None): alias for the length of the return period for average return and std. This can be `'W'`, `'2W'`, `'ME'`.
Default is `None` for 1 day. Full list see https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases.
freq_length (int): length of the return period (in days). Default to `1`.
Returns:
results (dict): dict with regimes as keys and dataframe as values. The dataframe has indices as indexes and metrics as columns
"""
# get the last day of each period
df = df[df['regime'] != df['regime'].shift(-1)]
results = df.groupby('regime').apply(
calculate_return_metrics,
freq_alias = freq_alias,
freq_length = freq_length,
include_groups=False
).reset_index(names=['regime', 'index'])
return results
def calculate_returns_forward(
df: pd.DataFrame,
period: int
):
"""
Calculate returns in the following `period` days for each regime.
Args:
df (DataFrame): the data with columns (`'DATE'`, `'regime'`, ...) (the other columns are returns).
period (int): length of the period.
Returns:
results: dataframe with indices as indexes, regime as columns and returns as rows.
"""
returns_cum = df.sort_values(by='DATE', ascending=False).drop(columns=['DATE', 'regime']).rolling(period).sum().shift(1)
returns_cum = pd.merge(df[['DATE','regime']], returns_cum, left_index=True, right_index=True).dropna().sort_values(by='DATE', ascending=True)
returns_forward_regimes_grouped = returns_cum.drop(columns='DATE').groupby('regime')
returns_forward_regimes_stats = pd.DataFrame({
'mean': returns_forward_regimes_grouped.mean().stack(),
'std' : returns_forward_regimes_grouped.std().stack()
}).reset_index(names=['regime', 'index'])
return returns_forward_regimes_stats
def smooth_regimes(regimes: np.ndarray, window: int | None = 10):
"""
Smooth the regimes, by assigning the most frequent regime within the sliding window.
Args:
regimes (ndarray): the regime identification results.
window (int): length of the sliding window.
Returns:
regimes_series (Series): Series of the smoothed regimes.
"""
regimes_series = pd.Series(regimes)
regimes_series = regimes_series.rolling(window, min_periods=1).apply(lambda x : x.mode()[0])
regimes_series = regimes_series.round()
return regimes_series
def calculate_transition_matrix(regimes_df: pd.DataFrame, at_regimes_level: bool | None = True) -> pd.DataFrame:
"""
Calculate transition matrix.
Args:
regimes_df (DataFrame): DateFrame of the regimes which must include columns(`'DATE'`, `'regime'`)
at_regimes_level (bool): If to calculate regime at regime level. If `'True'`, the probability between the same regime would be zero.
If `'False'`, the transition matrix is at the data level.
"""
# ensure the regimes are in ascending order of the dates
regimes_df = regimes_df.sort_values(by='DATE', ascending=True)
regimes_df = regimes_df[['DATE', 'regime']]
if at_regimes_level:
# keep the rows whose 'regime' is a new one, compared with the previous one
regimes_df = regimes_df[regimes_df['regime'] != regimes_df['regime'].shift(1)]
# calculate trainsition matix
regimes_df['regime_next'] = regimes_df['regime'].shift(-1)
regimes_df = regimes_df.dropna() # droup the last row
transition_counts = pd.crosstab(regimes_df['regime'], regimes_df['regime_next'])
transition_matrix = transition_counts.div(transition_counts.sum(axis=1),axis=0)
return transition_matrix
def calculate_rwo_entropy(row: Union[np.ndarray, pd.Series]) -> float:
"""
Calculate entropy for the row of probabilities.
Args:
row (ndarray | Series): row of probabilities.
Returns:
entropy (float): entropy of the row.
"""
row = row[row>0]
return -np.sum(row * np.log(row))
def calculate_transition_matrix_entropy(matrix: pd.DataFrame, apply_norm: bool | None = False) -> float:
"""
Calculate the entropy for this trainsition matrix.
Args:
matrix (DataFrame): the transition matrix, with regimes as indexes and regimes as columns.
Value at row i column j means the probability of regime i switching to regime j.
apply_norm: If `Ture`, normalize the entropy by number_of_rows x log(number_of_rows).
Returns:
entropy (float): entropy of the matrix.
"""
entropies_rows = np.apply_along_axis(calculate_rwo_entropy, 1, matrix)
if apply_norm:
return np.sum(entropies_rows) / np.log(len(entropies_rows)-1) / len(entropies_rows)
else:
return np.sum(entropies_rows)
def analyse_regimes_durations(regimes_df: pd.DataFrame) -> pd.DataFrame:
"""
Calculate mean, std, 25, 50, 70 quantiles for regime durations and the total days and
the corresponding percentage for each regime and all regime periods.
Args:
regimes_df (DataFrame): DataFrame with columns (`'DATE'`, `'regime'`). Other columns will be ignored.
Returns:
regimes_durations_stats (DataFrame): DataFrame with columns
(`'regime'`, `'mean'`, `'std'`, `'count'`, `'25 quantile'`, `'median'`, `'75 quantile'`, `'percent'`).
"""
regimes_df = regimes_df[['DATE','regime']]
regimes_durations = calculate_regime_durations(regimes_df)
regimes_durations_stats = calculate_regime_durations_statistics(regimes_durations)
return regimes_durations_stats