Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichalowicz committed Jun 9, 2016
1 parent 811c469 commit b9d4b2d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
6 changes: 2 additions & 4 deletions tests/pipeline/test_engine.py
Expand Up @@ -1253,8 +1253,6 @@ def test_correlation_factors(self, returns_length, correlation_length):
num_days = end_date_index - start_date_index + 1
num_assets = len(assets)

returns = Returns(window_length=returns_length)

cascading_mask = \
AssetIDPlusDay() < (sids[-1] + dates[start_date_index].day)
expected_cascading_mask_result = make_cascading_boolean_array(
Expand Down Expand Up @@ -1313,6 +1311,7 @@ def test_correlation_factors(self, returns_length, correlation_length):
# (correlation_length - 1) days prior to our start date. This is
# because we need (correlation_length - 1) extra days of returns to
# compute our expected correlations.
returns = Returns(window_length=returns_length)
results = self.engine.run_pipeline(
Pipeline(columns={'returns': returns}),
dates[start_date_index - (correlation_length - 1)],
Expand Down Expand Up @@ -1371,8 +1370,6 @@ def test_regression_of_returns_factor(self,
num_days = end_date_index - start_date_index + 1
num_assets = len(assets)

returns = Returns(window_length=returns_length)

cascading_mask = \
AssetIDPlusDay() < (sids[-1] + dates[start_date_index].day)
expected_cascading_mask_result = make_cascading_boolean_array(
Expand Down Expand Up @@ -1435,6 +1432,7 @@ def test_regression_of_returns_factor(self,
# prior to our start date. This is because we need
# (regression_length - 1) extra days of returns to compute our
# expected regressions.
returns = Returns(window_length=returns_length)
results = self.engine.run_pipeline(
Pipeline(columns={'returns': returns}),
dates[start_date_index - (regression_length - 1)],
Expand Down
17 changes: 15 additions & 2 deletions tests/pipeline/test_slice.py
Expand Up @@ -13,7 +13,7 @@
from zipline.pipeline import CustomFactor, Pipeline
from zipline.pipeline.data import USEquityPricing
from zipline.pipeline.engine import SimplePipelineEngine
from zipline.pipeline.factors import Returns
from zipline.pipeline.factors import FactorSlice, Returns
from zipline.pipeline.loaders.frame import DataFrameLoader
from zipline.testing import check_arrays
from zipline.testing.fixtures import WithTradingEnvironment, ZiplineTestCase
Expand Down Expand Up @@ -58,6 +58,10 @@ def init_class_fixtures(cls):
)

def test_slice(self):
"""
Test that slices can be created by indexing into a term, and that they
have the correct shape when used as inputs.
"""
my_asset_column = 0
start_date_index = 5
end_date_index = 9
Expand All @@ -68,6 +72,7 @@ def test_slice(self):

returns = Returns(window_length=2)
returns_slice = returns[my_asset]
self.assertIsInstance(returns_slice, FactorSlice)

class UsesSlicedInput(CustomFactor):
window_length = 3
Expand All @@ -92,7 +97,12 @@ def compute(self, today, assets, out, returns, returns_slice):
pipe, self.dates[start_date_index], self.dates[end_date_index],
)

# Test that slices cannot be added as a pipeline column.
def test_adding_slice_column(self):
"""
Test that slices cannot be added as a pipeline column.
"""
my_asset = self.asset_finder.retrieve_asset(self.sids[0])

with self.assertRaises(UnsupportedPipelineColumn):
pipe = Pipeline(columns={'open_slice': OpenPrice()[my_asset]})

Expand All @@ -101,6 +111,9 @@ def compute(self, today, assets, out, returns, returns_slice):
pipe.add(OpenPrice()[my_asset], 'open_slice')

def test_single_column_output(self):
"""
Tests for custom factors that compute a 1D out.
"""
start_date_index = 5
end_date_index = 9
alternating_mask = (AssetIDPlusDay() % 2).eq(0)
Expand Down
4 changes: 2 additions & 2 deletions zipline/errors.py
Expand Up @@ -639,6 +639,6 @@ class NonExistentAssetInTimeFrame(ZiplineError):

class UnsupportedPipelineColumn(ZiplineError):
msg = (
"Adding single-column terms as pipeline columns is not currently "
"supported."
"{termname} cannot be added as a column. Adding single-column terms "
"as pipeline columns is not currently supported."
)
33 changes: 26 additions & 7 deletions zipline/pipeline/factors/factor.py
Expand Up @@ -628,18 +628,37 @@ def rank(self, method='ordinal', ascending=True, mask=NotSpecified):
"""
return Rank(self, method=method, ascending=ascending, mask=mask)

def rolling_pearson(self,
target_slice,
correlation_length,
mask=NotSpecified):
return RollingPearson(self, target_slice, correlation_length, mask)

def rolling_spearman(self,
@expect_types(
target_slice=Slice,
correlation_length=int,
mask=(Filter, NotSpecifiedType),
)
def rolling_pearsonr(self,
target_slice,
correlation_length,
mask=NotSpecified):
"""
"""
return RollingPearson(self, target_slice, correlation_length, mask)

@expect_types(
target_slice=Slice,
correlation_length=int,
mask=(Filter, NotSpecifiedType),
)
def rolling_spearmanr(self,
target_slice,
correlation_length,
mask=NotSpecified):
"""
"""
return RollingSpearman(self, target_slice, correlation_length, mask)

@expect_types(
target_slice=Slice,
regression_length=int,
mask=(Filter, NotSpecifiedType),
)
def rolling_linear_regression(self,
target_slice,
regression_length,
Expand Down
4 changes: 2 additions & 2 deletions zipline/pipeline/pipeline.py
Expand Up @@ -41,7 +41,7 @@ def __init__(self, columns=None, screen=None):
columns = {}
for term in columns.values():
if term.ndim == 1:
raise UnsupportedPipelineColumn()
raise UnsupportedPipelineColumn(termname=type(term).__name__)
self._columns = columns
self._screen = screen

Expand Down Expand Up @@ -78,7 +78,7 @@ def add(self, term, name, overwrite=False):
named `name`.
"""
if term.ndim == 1:
raise UnsupportedPipelineColumn()
raise UnsupportedPipelineColumn(termname=type(term).__name__)

columns = self.columns
if name in columns:
Expand Down

0 comments on commit b9d4b2d

Please sign in to comment.