From e579ddd33d6ccf612e7ce53cbb1c242978d726e2 Mon Sep 17 00:00:00 2001 From: devin stevenson Date: Sun, 31 Jan 2016 23:32:42 -0800 Subject: [PATCH 1/2] ENH: vectorize max_drawdown approx 4X speedup --- pyfolio/timeseries.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/pyfolio/timeseries.py b/pyfolio/timeseries.py index aafcdfef..dec1f041 100644 --- a/pyfolio/timeseries.py +++ b/pyfolio/timeseries.py @@ -76,18 +76,9 @@ def max_drawdown(returns): return np.nan df_cum_rets = cum_returns(returns, starting_value=100) + cum_max_return = df_cum_rets.cummax() - MDD = 0 - DD = 0 - peak = -99999 - for value in df_cum_rets: - if (value > peak): - peak = value - else: - DD = (peak - value) / peak - if (DD > MDD): - MDD = DD - return -1 * MDD + return df_cum_rets.sub(cum_max_return).div(cum_max_return).min() def annual_return(returns, period=DAILY): @@ -925,7 +916,7 @@ def calc_distribution_stats(x): def get_max_drawdown_underwater(underwater): - """Determines peak, valley, and recovery dates given and 'underwater' + """Determines peak, valley, and recovery dates given an 'underwater' DataFrame. An underwater DataFrame is a DataFrame that has precomputed From 977069f19b327e31c8cb10b525afe574403c771b Mon Sep 17 00:00:00 2001 From: devin stevenson Date: Sun, 31 Jan 2016 23:33:13 -0800 Subject: [PATCH 2/2] BUG: fix docs on model_returns[t|normal] --- pyfolio/bayesian.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pyfolio/bayesian.py b/pyfolio/bayesian.py index 549d8f03..23a25d73 100644 --- a/pyfolio/bayesian.py +++ b/pyfolio/bayesian.py @@ -106,11 +106,7 @@ def model_returns_t_alpha_beta(data, bmark, samples=2000): def model_returns_normal(data, samples=500): - """Run Bayesian model assuming returns are Student-T distributed. - - Compared with the normal model, this model assumes returns be - T-distributed and thus has a 3rd parameter (nu) that controls the - mass in the tails. + """Run Bayesian model assuming returns are normally distributed. Parameters ---------- @@ -149,7 +145,11 @@ def model_returns_normal(data, samples=500): def model_returns_t(data, samples=500): - """Run Bayesian model assuming returns are normally distributed. + """Run Bayesian model assuming returns are Student-T distributed. + + Compared with the normal model, this model assumes returns are + T-distributed and thus have a 3rd parameter (nu) that controls the + mass in the tails. Parameters ----------