Skip to content

Commit b6bb0be

Browse files
Merge pull request #1 from devinstevenson/vectorize_drawdown
Vectorize drawdown
2 parents 035093a + 977069f commit b6bb0be

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

pyfolio/bayesian.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,7 @@ def model_returns_t_alpha_beta(data, bmark, samples=2000):
106106

107107

108108
def model_returns_normal(data, samples=500):
109-
"""Run Bayesian model assuming returns are Student-T distributed.
110-
111-
Compared with the normal model, this model assumes returns be
112-
T-distributed and thus has a 3rd parameter (nu) that controls the
113-
mass in the tails.
109+
"""Run Bayesian model assuming returns are normally distributed.
114110
115111
Parameters
116112
----------
@@ -149,7 +145,11 @@ def model_returns_normal(data, samples=500):
149145

150146

151147
def model_returns_t(data, samples=500):
152-
"""Run Bayesian model assuming returns are normally distributed.
148+
"""Run Bayesian model assuming returns are Student-T distributed.
149+
150+
Compared with the normal model, this model assumes returns are
151+
T-distributed and thus have a 3rd parameter (nu) that controls the
152+
mass in the tails.
153153
154154
Parameters
155155
----------

pyfolio/timeseries.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,9 @@ def max_drawdown(returns):
7676
return np.nan
7777

7878
df_cum_rets = cum_returns(returns, starting_value=100)
79+
cum_max_return = df_cum_rets.cummax()
7980

80-
MDD = 0
81-
DD = 0
82-
peak = -99999
83-
for value in df_cum_rets:
84-
if (value > peak):
85-
peak = value
86-
else:
87-
DD = (peak - value) / peak
88-
if (DD > MDD):
89-
MDD = DD
90-
return -1 * MDD
81+
return df_cum_rets.sub(cum_max_return).div(cum_max_return).min()
9182

9283

9384
def annual_return(returns, period=DAILY):
@@ -925,7 +916,7 @@ def calc_distribution_stats(x):
925916

926917

927918
def get_max_drawdown_underwater(underwater):
928-
"""Determines peak, valley, and recovery dates given and 'underwater'
919+
"""Determines peak, valley, and recovery dates given an 'underwater'
929920
DataFrame.
930921
931922
An underwater DataFrame is a DataFrame that has precomputed

0 commit comments

Comments
 (0)