Skip to content

[ENH] Switch regressions to use the pandas implementation #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pyfolio/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ def plot_rolling_risk_factors(
rolling_risk_multifactor = timeseries.rolling_multifactor_beta(
returns,
risk_factors.loc[:, ['SMB', 'HML', 'UMD']],
rolling_window=rolling_beta_window)
window=rolling_beta_window)

rolling_beta_SMB = timeseries.rolling_beta(
returns,
risk_factors['SMB'],
rolling_window=rolling_beta_window)
window=rolling_beta_window)
rolling_beta_HML = timeseries.rolling_beta(
returns,
risk_factors['HML'],
rolling_window=rolling_beta_window)
window=rolling_beta_window)
rolling_beta_UMD = timeseries.rolling_beta(
returns,
risk_factors['UMD'],
rolling_window=rolling_beta_window)
window=rolling_beta_window)

rolling_beta_SMB.plot(color='steelblue', alpha=0.7, ax=ax, **kwargs)
rolling_beta_HML.plot(color='orangered', alpha=0.7, ax=ax, **kwargs)
Expand Down Expand Up @@ -608,10 +608,10 @@ def plot_rolling_beta(returns, benchmark_rets,
ax.set_title("Rolling Portfolio Beta to S&P 500")
ax.set_ylabel('Beta')
rb_1 = timeseries.rolling_beta(
returns, benchmark_rets, rolling_window=rolling_beta_window * 2)
returns, benchmark_rets, window=rolling_beta_window * 2)['x']
rb_1.plot(color='steelblue', lw=3, alpha=0.6, ax=ax, **kwargs)
rb_2 = timeseries.rolling_beta(
returns, benchmark_rets, rolling_window=rolling_beta_window * 3)
returns, benchmark_rets, window=rolling_beta_window * 3)['x']
rb_2.plot(color='grey', lw=3, alpha=0.4, ax=ax, **kwargs)
ax.set_ylim((-2.5, 2.5))
ax.axhline(rb_1.mean(), color='steelblue', linestyle='--', lw=3)
Expand Down
43 changes: 22 additions & 21 deletions pyfolio/tests/test_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest import TestCase
import unittest
from nose_parameterized import parameterized

import numpy as np
Expand All @@ -7,8 +7,9 @@

from .. import timeseries

DECIMAL_PLACES = 9

class TestDrawdown(TestCase):
class TestDrawdown(unittest.TestCase):
px_list_1 = np.array(
[100, 120, 100, 80, 70, 110, 180, 150]) / 100. # Simple
px_list_2 = np.array(
Expand Down Expand Up @@ -106,13 +107,13 @@ def test_gen_drawdown_table(
(pd.Series(px_list_1 - 1, index=dt), -0.44000000000000011)
])
def test_max_drawdown(self, df_rets, expected):
self.assertEqual(timeseries.max_drawdown(df_rets), expected)
self.assertAlmostEqual(timeseries.max_drawdown(df_rets), expected, DECIMAL_PLACES)

@parameterized.expand([
(pd.Series(px_list_1 - 1, index=dt), -0.44000000000000011)
])
def test_max_drawdown_underwater(self, underwater, expected):
self.assertEqual(timeseries.max_drawdown(underwater), expected)
self.assertAlmostEqual(timeseries.max_drawdown(underwater), expected, DECIMAL_PLACES)

@parameterized.expand([
(pd.Series(px_list_1,
Expand All @@ -130,7 +131,7 @@ def test_top_drawdowns(self, df_rets, top, expected):
expected)


class TestCumReturns(TestCase):
class TestCumReturns(unittest.TestCase):
dt = pd.date_range('2000-1-3', periods=3, freq='D')

@parameterized.expand([
Expand All @@ -144,7 +145,7 @@ def test_expected_result(self, input, expected, starting_value):
pdt.assert_series_equal(output, expected)


class TestVariance(TestCase):
class TestVariance(unittest.TestCase):

@parameterized.expand([
(1e7, 0.5, 1, 1, -10000000.0)
Expand All @@ -159,7 +160,7 @@ def test_var_cov_var_normal(self, P, c, mu, sigma, expected):
expected)


class TestNormalize(TestCase):
class TestNormalize(unittest.TestCase):
dt = pd.date_range('2000-1-3', periods=8, freq='D')
px_list = [1.0, 1.2, 1.0, 0.8, 0.7, 0.8, 0.8, 0.8]

Expand All @@ -171,7 +172,7 @@ def test_normalize(self, df, expected):
self.assertTrue(timeseries.normalize(df).equals(expected))


class TestAggregateReturns(TestCase):
class TestAggregateReturns(unittest.TestCase):
simple_rets = pd.Series(
[0.1] * 3 + [0] * 497,
pd.date_range(
Expand All @@ -192,7 +193,7 @@ def test_aggregate_rets(self, df_rets, convert_to, expected):
expected)


class TestStats(TestCase):
class TestStats(unittest.TestCase):
simple_rets = pd.Series(
[0.1] * 3 + [0] * 497,
pd.date_range(
Expand Down Expand Up @@ -224,18 +225,18 @@ def test_annual_ret(self, df_rets, style, expected):
(simple_rets, 0.12271674212427248)
])
def test_annual_volatility(self, df_rets, expected):
self.assertEqual(timeseries.annual_volatility(df_rets), expected)
self.assertAlmostEqual(timeseries.annual_volatility(df_rets), expected, DECIMAL_PLACES)

@parameterized.expand([
(simple_rets, 'calendar', 1.7112579454508172),
(simple_rets, 'compound', 1.3297007080039505)
])
def test_sharpe(self, df_rets, returns_style, expected):
self.assertEqual(
self.assertAlmostEqual(
timeseries.sharpe_ratio(
df_rets,
returns_style=returns_style),
expected)
expected, DECIMAL_PLACES)

@parameterized.expand([
(simple_rets[:5], 2, '[nan, inf, inf, 11.224972160321828, inf]')
Expand All @@ -248,25 +249,25 @@ def test_sharpe_2(self, df_rets, rolling_sharpe_window, expected):
(simple_rets, True, 0.010766923838471554)
])
def test_stability_of_timeseries(self, df_rets, logValue, expected):
self.assertEqual(
self.assertAlmostEqual(
timeseries.stability_of_timeseries(
df_rets,
logValue=logValue),
expected)
expected, DECIMAL_PLACES)

@parameterized.expand([
(simple_rets[:5], simple_benchmark[:5], 2, 8.024708101613483e-32)
])
def test_beta(self, df_rets, benchmark_rets, rolling_window, expected):
def test_beta(self, df_rets, benchmark_rets, window, expected):
self.assertEqual(
timeseries.rolling_beta(
df_rets,
benchmark_rets,
rolling_window=rolling_window).values.tolist()[2],
window=window).values.tolist()[2],
expected)


class TestMultifactor(TestCase):
class TestMultifactor(unittest.TestCase):
simple_rets = pd.Series(
[0.1] * 3 + [0] * 497,
pd.date_range(
Expand Down Expand Up @@ -300,16 +301,16 @@ def test_calc_multifactor(self, df_rets, factors, expected):
0.002997302427814967])
])
def test_multifactor_beta(
self, df_rets, benchmark_df, rolling_window, expected):
self, df_rets, benchmark_df, window, expected):
self.assertEqual(
timeseries.rolling_multifactor_beta(
df_rets,
benchmark_df,
rolling_window=rolling_window).values.tolist()[2],
window=window).values.tolist()[2],
expected)


class TestPerfStats(TestCase):
class TestPerfStats(unittest.TestCase):
simple_rets = pd.Series(
[0.1] * 3 + [0] * 497,
pd.date_range(
Expand All @@ -335,4 +336,4 @@ def test_perf_stats(
self.assertEqual(timeseries.perf_stats(df_rets,
returns_style=returns_style,
return_as_dict=return_as_dict).values.tolist()[-2:],
expected)
expected)
Loading