Skip to content

Commit ca8567e

Browse files
committed
BUG Refactor stability measure. Before was using cumulative returns where cumulative log-returns should have been used. This leads to different values so also fix the test value.
1 parent 27581fc commit ca8567e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pyfolio/tests/test_timeseries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def test_sharpe_2(self, returns, rolling_sharpe_window, expected):
289289
returns, rolling_sharpe_window).values.tolist()), expected)
290290

291291
@parameterized.expand([
292-
(simple_rets, 0.010766923838471554)
292+
(simple_rets, 0.10376378866671222)
293293
])
294294
def test_stability_of_timeseries(self, returns, expected):
295295
self.assertAlmostEqual(

pyfolio/timeseries.py

+24
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,30 @@ def beta(returns, factor_returns):
474474

475475
return alpha_beta(returns, factor_returns)[1]
476476

477+
def stability_of_timeseries(returns):
478+
"""Determines R-squared of a linear fit to the cumulative
479+
log returns. Computes an ordinary least squares linear fit,
480+
and returns R-squared.
481+
482+
Parameters
483+
----------
484+
returns : pd.Series
485+
Daily returns of the strategy, noncumulative.
486+
- See full explanation in tears.create_full_tear_sheet.
487+
488+
Returns
489+
-------
490+
float
491+
R-squared.
492+
493+
"""
494+
495+
cum_log_returns = np.log(1 + returns).cumsum()
496+
rhat = stats.linregress(np.arange(len(cum_log_returns)),
497+
cum_log_returns.values)[2]
498+
499+
return rhat
500+
477501

478502
SIMPLE_STAT_FUNCS = [
479503
annual_return,

0 commit comments

Comments
 (0)