Skip to content

Commit ad10570

Browse files
Calvin DeBoertwiecki
authored andcommitted
added quarterly return aggregation, removed unused code.
1 parent 9949f00 commit ad10570

File tree

6 files changed

+15
-4
lines changed

6 files changed

+15
-4
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ before_install:
3030
- conda update -q conda
3131

3232
install:
33-
- conda create -n testenv --yes -c quantopian pip python=$TRAVIS_PYTHON_VERSION numpy=$NUMPY_VERSION pandas=$PANDAS_VERSION scipy=$SCIPY_VERSION libgfortran=$LIBGFORTRAN_VERSION
33+
- conda create -n testenv --yes -c conda-forge pip python=$TRAVIS_PYTHON_VERSION numpy=$NUMPY_VERSION pandas=$PANDAS_VERSION scipy=$SCIPY_VERSION libgfortran=$LIBGFORTRAN_VERSION
3434
- source activate testenv
3535
- pip install -e .[dev]
3636

empyrical/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
DAILY,
7070
WEEKLY,
7171
MONTHLY,
72+
QUARTERLY,
7273
YEARLY
7374
)
7475

empyrical/periods.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33

44
MONTHS_PER_YEAR = 12
55
WEEKS_PER_YEAR = 52
6+
QTRS_PER_YEAR = 4
67

78
DAILY = 'daily'
89
WEEKLY = 'weekly'
910
MONTHLY = 'monthly'
11+
QUARTERLY = 'quarterly'
1012
YEARLY = 'yearly'
1113

1214
ANNUALIZATION_FACTORS = {
1315
DAILY: APPROX_BDAYS_PER_YEAR,
1416
WEEKLY: WEEKS_PER_YEAR,
1517
MONTHLY: MONTHS_PER_YEAR,
18+
QUARTERLY: QTRS_PER_YEAR,
1619
YEARLY: 1
1720
}

empyrical/stats.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515

1616
from __future__ import division
1717

18+
import math
1819
import pandas as pd
1920
import numpy as np
2021
from scipy import stats
2122
from six import iteritems
2223

2324
from .utils import nanmean, nanstd, nanmin, up, down, roll, rolling_window
2425
from .periods import ANNUALIZATION_FACTORS, APPROX_BDAYS_PER_YEAR
25-
from .periods import DAILY, WEEKLY, MONTHLY, YEARLY
26+
from .periods import DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY
2627

2728

2829
def _create_unary_vectorized_roll_function(function):
@@ -330,6 +331,8 @@ def cumulate_returns(x):
330331
grouping = [lambda x: x.year, lambda x: x.isocalendar()[1]]
331332
elif convert_to == MONTHLY:
332333
grouping = [lambda x: x.year, lambda x: x.month]
334+
elif convert_to == QUARTERLY:
335+
grouping = [lambda x: x.year, lambda x: int(math.ceil(x.month/3.))]
333336
elif convert_to == YEARLY:
334337
grouping = [lambda x: x.year]
335338
else:

empyrical/tests/test_stats.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,15 @@ def test_cum_returns_final(self, returns, starting_value, expected):
216216
0.0]),
217217
(simple_benchmark, empyrical.MONTHLY, [0.01,
218218
0.03030099999999991]),
219+
(simple_benchmark, empyrical.QUARTERLY, [0.04060401]),
219220
(simple_benchmark, empyrical.YEARLY, [0.040604010000000024]),
220221
(weekly_returns, empyrical.MONTHLY, [0.0, 0.087891200000000058,
221222
-0.04500459999999995]),
222223
(weekly_returns, empyrical.YEARLY, [0.038931091700480147]),
223-
(monthly_returns, empyrical.YEARLY, [0.038931091700480147])
224+
(monthly_returns, empyrical.YEARLY, [0.038931091700480147]),
225+
(monthly_returns, empyrical.QUARTERLY, [0.11100000000000021,
226+
0.008575999999999917,
227+
-0.072819999999999996])
224228
])
225229
def test_aggregate_returns(self, returns, convert_to, expected):
226230
returns = self.empyrical(pandas_only=True).aggregate_returns(

empyrical/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from pandas.tseries.offsets import BDay
2626
try:
2727
from pandas_datareader import data as web
28-
except ImportError as e:
28+
except ImportError:
2929
msg = ("Unable to import pandas_datareader. Suppressing import error and "
3030
"continuing. All data reading functionality will raise errors; but "
3131
"has been deprecated and will be removed in a later version.")

0 commit comments

Comments
 (0)