PERF: Use empyrical with ndarrays instead of Series

This commit is contained in:
Richard Frank
2016-09-08 09:10:08 -04:00
parent 73a61caaeb
commit ef04fe3a9b
4 changed files with 67 additions and 46 deletions
+38 -4
View File
@@ -1,11 +1,30 @@
package:
name: empyrical
version: "0.1.11"
version: "0.2.0"
source:
fn: empyrical-0.1.11.tar.gz
url: https://pypi.python.org/packages/df/82/c4050b0fe341db97430b2de7ae736d89a2ddb78636d3e0235aef1b7499d5/empyrical-0.1.11.tar.gz
md5: fbd543416d204688146f13f325ca8731
fn: empyrical-0.2.0.tar.gz
url: https://pypi.python.org/packages/e0/e7/e6bf883c009993677f2f283d93c3efcf910c7e42ce0dcd4cf8abd465b070/empyrical-0.2.0.tar.gz
md5: 32a360c6a151b32872e5b6f56dfbe3e4
# patches:
# List any patch files here
# - fix.patch
# build:
# noarch_python: True
# preserve_egg_dir: True
# entry_points:
# Put any entry points (scripts to be generated automatically) here. The
# syntax is module:function. For example
#
# - empyrical = empyrical:main
#
# Would create an entry point called empyrical that calls empyrical.main()
# If this is a new build for the same version, increment the build
# number. If you do not include this key, it defaults to 0.
# number: 1
requirements:
build:
@@ -23,6 +42,21 @@ requirements:
- scipy >=0.15.1
- bottleneck >=1.0.0
test:
# Python imports
imports:
- empyrical
- empyrical.tests
# commands:
# - nosetests
# You can also put a file called run_test.py in the recipe that will be run
# at test time.
requires:
- nose >=1.3.7
# - nose_parameterized >=0.5.0
about:
home: https://github.com/quantopian/empyrical
+1 -1
View File
@@ -67,4 +67,4 @@ intervaltree==2.1.0
lru-dict==1.1.4
# For financial risk calculations
empyrical==0.1.11
empyrical==0.2.0
+18 -25
View File
@@ -28,15 +28,14 @@ from . risk import (
)
from empyrical import (
alpha,
alpha_beta_aligned,
annual_volatility,
beta,
cum_returns,
downside_risk,
information_ratio,
max_drawdown,
sharpe_ratio,
sortino_ratio
sortino_ratio,
)
log = logbook.Logger('Risk Cumulative')
@@ -152,7 +151,6 @@ class RiskMetricsCumulative(object):
self.algorithm_returns_cont[dt_loc] = algorithm_returns
self.algorithm_returns = self.algorithm_returns_cont[:dt_loc + 1]
algorithm_returns_series = pd.Series(self.algorithm_returns)
self.num_trading_days = len(self.algorithm_returns)
@@ -161,8 +159,8 @@ class RiskMetricsCumulative(object):
self.algorithm_returns = np.append(0.0, self.algorithm_returns)
self.algorithm_cumulative_returns[dt_loc] = cum_returns(
algorithm_returns_series
).iloc[-1]
self.algorithm_returns
)[-1]
algo_cumulative_returns_to_date = \
self.algorithm_cumulative_returns[:dt_loc + 1]
@@ -186,14 +184,14 @@ class RiskMetricsCumulative(object):
self.benchmark_returns_cont[dt_loc] = benchmark_returns
self.benchmark_returns = self.benchmark_returns_cont[:dt_loc + 1]
benchmark_returns_series = pd.Series(self.benchmark_returns)
if self.create_first_day_stats:
if len(self.benchmark_returns) == 1:
self.benchmark_returns = np.append(0.0, self.benchmark_returns)
self.benchmark_cumulative_returns[dt_loc] = cum_returns(
benchmark_returns_series
).iloc[-1]
self.benchmark_returns
)[-1]
benchmark_cumulative_returns_to_date = \
self.benchmark_cumulative_returns[:dt_loc + 1]
@@ -234,10 +232,10 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}"
self.update_current_max()
self.benchmark_volatility[dt_loc] = annual_volatility(
benchmark_returns_series
self.benchmark_returns
)
self.algorithm_volatility[dt_loc] = annual_volatility(
algorithm_returns_series
self.algorithm_returns
)
# caching the treasury rates for the minutely case is a
@@ -258,31 +256,26 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}"
self.algorithm_cumulative_returns[dt_loc] -
self.treasury_period_return)
self.beta[dt_loc] = beta(
algorithm_returns_series,
benchmark_returns_series
)
self.alpha[dt_loc] = alpha(
algorithm_returns_series,
benchmark_returns_series,
_beta=self.beta[dt_loc]
self.alpha[dt_loc], self.beta[dt_loc] = alpha_beta_aligned(
self.algorithm_returns,
self.benchmark_returns,
)
self.sharpe[dt_loc] = sharpe_ratio(
algorithm_returns_series
self.algorithm_returns,
)
self.downside_risk[dt_loc] = downside_risk(
algorithm_returns_series
self.algorithm_returns
)
self.sortino[dt_loc] = sortino_ratio(
algorithm_returns_series,
self.algorithm_returns,
_downside_risk=self.downside_risk[dt_loc]
)
self.information[dt_loc] = information_ratio(
algorithm_returns_series,
benchmark_returns_series
self.algorithm_returns,
self.benchmark_returns,
)
self.max_drawdown = max_drawdown(
algorithm_returns_series
self.algorithm_returns
)
self.max_drawdowns[dt_loc] = self.max_drawdown
self.max_leverage = self.calculate_max_leverage()
+10 -16
View File
@@ -25,9 +25,8 @@ from . import risk
from . risk import check_entry
from empyrical import (
alpha,
alpha_beta_aligned,
annual_volatility,
beta,
cum_returns,
downside_risk,
information_ratio,
@@ -124,28 +123,23 @@ class RiskMetricsPeriod(object):
if pd.isnull(self.sharpe):
self.sharpe = 0.0
self.downside_risk = downside_risk(
self.algorithm_returns
self.algorithm_returns.values
)
self.sortino = sortino_ratio(
self.algorithm_returns,
_downside_risk=self.downside_risk
self.algorithm_returns.values,
_downside_risk=self.downside_risk,
)
self.information = information_ratio(
self.algorithm_returns,
self.benchmark_returns
self.algorithm_returns.values,
self.benchmark_returns.values,
)
self.beta = beta(
self.algorithm_returns,
self.benchmark_returns
)
self.alpha = alpha(
self.algorithm_returns,
self.benchmark_returns,
_beta=self.beta
self.alpha, self.beta = alpha_beta_aligned(
self.algorithm_returns.values,
self.benchmark_returns.values,
)
self.excess_return = self.algorithm_period_returns - \
self.treasury_period_return
self.max_drawdown = max_drawdown(self.algorithm_returns)
self.max_drawdown = max_drawdown(self.algorithm_returns.values)
self.max_leverage = self.calculate_max_leverage()
def to_dict(self):