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`.
This commit is contained in:
Scott Sanderson
2016-11-28 12:58:18 -05:00
parent c05635333e
commit d140d585c2
2 changed files with 24 additions and 10 deletions
+23 -1
View File
@@ -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.
+1 -9
View File
@@ -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)