Skip to content

Commit

Permalink
Modified tests and added annualized return calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
julius-datajunkie committed Jan 17, 2014
1 parent 00e5eb7 commit b427d9d
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 200 deletions.
30 changes: 6 additions & 24 deletions PerformanceAnalytics/frequency.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
class frequency:
second = 0
minutely = 1
hourly = 2
weekly = 3
monthly = 4
quarterly = 5
yearly = 6
class Frequency:
def __init__(self, scale=None, label=None):
self.label = label
self.scale = scale

def to_str(self, d):
if d is frequency.second:
name = "S"
if d is frequency.minutely:
name = "T"
if d is frequency.hourly:
name = "H"
if d is frequency.weekly:
name = "W"
if d is frequency.monthly:
name = "M"
if d is frequency.quarterly:
name = "Q"
if d is frequency.yearly:
name = "A"
return name
def __str__(self):
return self.label
14 changes: 14 additions & 0 deletions PerformanceAnalytics/returns.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pandas as pd
import numpy as np
import math
import utils


def cumulative(R, geometric=True):
Expand Down Expand Up @@ -58,3 +60,15 @@ def rebalancing(R, weights):
)
portfolio_return.index.name = 'Date'
return portfolio_return


def annualized(R, scale=None, geometric=True):
n = len(R)
result = 0
if scale is None:
scale = utils.periodicity(R).scale
if geometric:
result = math.pow((1 + R).prod(), scale / n) - 1
else:
result = R.mean() * scale
return result
36 changes: 36 additions & 0 deletions PerformanceAnalytics/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import numpy as np
from frequency import Frequency


def periodicity(df):
"""
Frequencies higher than daily will not be available
"""
med = np.median(np.diff(df.index.values))
seconds = int(med.astype('timedelta64[s]').item().total_seconds())
freq = Frequency()
if seconds < 60:
freq.label = "S"
freq.scale = None
elif seconds < 3600:
freq.label = "T"
freq.scale = None
elif seconds < 86400:
freq.label = "H"
freq.scale = None
elif seconds == 86400:
freq.label = "D"
freq.scale = 252
elif seconds <= 604800:
freq.label = "W"
freq.scale = 52
elif seconds <= 2678400:
freq.label = "M"
freq.scale = 12
elif seconds <= 7948800:
freq.label = "Q"
freq.scale = 4
else: # anything lower than year is deemed as yearly
freq.label = "A"
freq.scale = 1
return freq
5 changes: 5 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os
import sys


sys.path.append(os.path.dirname(__file__))
153 changes: 0 additions & 153 deletions tests/edhec.csv

This file was deleted.

7 changes: 1 addition & 6 deletions tests/test_ratios.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from PerformanceAnalytics import ratios
import pandas as pd


class TestRatios:
def test_kelly_ratio(self):
edhec = pd.read_csv("edhec.csv", parse_dates=[0])
edhec.index = edhec.ix[:, 0]
edhec = edhec.ix[:, 1:]
print ratios.returns(edhec.ix[:,0])
pass
12 changes: 4 additions & 8 deletions tests/test_returns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@


class TestReturns:
def test_rebalancing(self):
weights = pd.read_csv("weights.csv", parse_dates=[0])
edhec = pd.read_csv("edhec.csv", parse_dates=[0])
edhec.index = edhec.ix[:, 0]
weights.index = weights.ix[:, 0]
weights = weights.ix[:, 1:]
edhec = edhec.ix[:, 1:]
assert len(returns.rebalancing(edhec, weights)) == 116
def test_annualized(self):
dates = pd.date_range("1/1/2010", periods=12, freq="M")
df = pd.DataFrame([0.1 for i in range(12)], index=dates)
assert abs(returns.annualized(df) - 2.138428) < 0.0001
10 changes: 10 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pandas as pd
from PerformanceAnalytics import utils


def test_utils():
dates = pd.date_range('1/1/2010', periods=12, freq="M")
df = pd.DataFrame([0 for i in range(12)], index=dates)
freq = utils.periodicity(df)
assert freq.scale == 12
assert freq.label == "M"
9 changes: 0 additions & 9 deletions tests/weights.csv

This file was deleted.

0 comments on commit b427d9d

Please sign in to comment.