From d140d585c27dcdf5d34aba271798877624475efe Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Mon, 28 Nov 2016 12:58:18 -0500 Subject: [PATCH] MAINT: Put exponential_weights where it's used. `math_utils` is mostly a shim around bottleneck imports. If we need this somewhere else, it probably belongs in `numpy_utils`. --- zipline/pipeline/factors/technical.py | 24 +++++++++++++++++++++++- zipline/utils/math_utils.py | 10 +--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/zipline/pipeline/factors/technical.py b/zipline/pipeline/factors/technical.py index ef495aea..947b7495 100644 --- a/zipline/pipeline/factors/technical.py +++ b/zipline/pipeline/factors/technical.py @@ -14,6 +14,7 @@ from numpy import ( dstack, exp, fmax, + full, inf, isnan, log, @@ -35,7 +36,6 @@ from zipline.utils.math_utils import ( nanstd, nansum, nanmin, - exponential_weights, ) from zipline.utils.numpy_utils import rolling_window from .factor import CustomFactor @@ -161,6 +161,28 @@ class AverageDollarVolume(CustomFactor): out[:] = nansum(close * volume, axis=0) / len(close) +def exponential_weights(length, decay_rate): + """ + Build a weight vector for an exponentially-weighted statistic. + + The resulting ndarray is of the form:: + + [decay_rate ** length, ..., decay_rate ** 2, decay_rate] + + Parameters + ---------- + length : int + The length of the desired weight vector. + decay_rate : float + The rate at which entries in the weight vector increase or decrease. + + Returns + ------- + weights : ndarray[float64] + """ + return full(length, decay_rate, float64_dtype) ** arange(length + 1, 1, -1) + + class _ExponentialWeightedFactor(SingleInputMixin, CustomFactor): """ Base class for factors implementing exponential-weighted operations. diff --git a/zipline/utils/math_utils.py b/zipline/utils/math_utils.py index 1bcc93c5..da99900d 100644 --- a/zipline/utils/math_utils.py +++ b/zipline/utils/math_utils.py @@ -14,7 +14,7 @@ # limitations under the License. import math -from numpy import isnan, full, arange +from numpy import isnan def tolerant_equals(a, b, atol=10e-7, rtol=10e-7, equal_nan=False): @@ -77,11 +77,3 @@ def round_if_near_integer(a, epsilon=1e-4): return round(a) else: return a - - -def exponential_weights(length, decay_rate): - """ - Return weighting vector for an exponential moving statistic on `length` - rows with a decay rate of `decay_rate`. - """ - return full(length, decay_rate, float) ** arange(length + 1, 1, -1)