def testPowFunction(self): ma5min = MovingAverage(5, 'close') >> Minimum holder = Pow(ma5min, 3) for i, close in enumerate(self.sampleClose): data = {'close': close} ma5min.push(data) holder.push(data) expected = math.pow(ma5min.result(), 3) calculated = holder.result() self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated))
def testArithmeticFunctionsDeepcopy(self): data = {'x': 1} test = Exp('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.exp(data['x']), copied.value) test = Log('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.log(data['x']), copied.value) test = Sqrt('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.sqrt(data['x']), copied.value) data['x'] = -1. test = Pow('x', 2) test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(data['x']**2, copied.value) test = Abs('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(abs(data['x']), copied.value) test = Sign('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(-1., copied.value) data['x'] = 1. test = Acos('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.acos(data['x']), copied.value) test = Asin('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.asin(data['x']), copied.value) test = Acosh('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.acosh(data['x']), copied.value) test = Asinh('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.asinh(data['x']), copied.value)
def testPowFunction(self): ma5min = MovingAverage(5, 'close') >> Minimum holder = Pow(ma5min, 3) holder2 = Pow(ma5min ^ ma5min, 3) for i, close in enumerate(self.sampleClose): data = {'close': close} ma5min.push(data) holder.push(data) holder2.push(data) expected = math.pow(ma5min.result(), 3) calculated = holder.result() self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated)) calculated = holder2.result() for cal in calculated: self.assertAlmostEqual(cal, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, cal))
def testArithmeticFunctionsPickle(self): data = {'x': 1} test = Exp('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Log('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Sqrt('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) data['x'] = -1. test = Pow('x', 2) test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Abs('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Sign('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) data['x'] = 1. test = Acos('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Asin('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Acosh('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Asinh('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name)
def __init__(self, dependency='x'): super(Kurtosis, self).__init__(dependency) self._std4 = Pow(Variance(dependency, isPopulation=True), 2) self._moment4 = CenterMoment(4, dependency) self._kurtosis = self._moment4 / self._std4
def __init__(self, dependency='x'): super(Skewness, self).__init__(dependency) self._std3 = Pow(Variance(dependency, isPopulation=True), 1.5) self._moment3 = CenterMoment(3, dependency) self._skewness = self._moment3 / self._std3
def testPowStr(self): s = Pow('roe', 3) self.assertEqual("''\\text{roe}'' ^ {3.0}", str(s))
def __init__(self, window, dependency='x'): super(MovingKurtosis, self).__init__(window, dependency) self._runningStd4 = Pow( MovingVariance(window, dependency, isPopulation=True), 2) self._runningMoment4 = MovingCenterMoment(window, 4, dependency) self._runningKurtosis = self._runningMoment4 / self._runningStd4
def __init__(self, window, dependency='x'): super(MovingSkewness, self).__init__(window, dependency) self._runningStd3 = Pow( MovingVariance(window, dependency, isPopulation=True), 1.5) self._runningMoment3 = MovingCenterMoment(window, 3, dependency) self._runningSkewness = self._runningMoment3 / self._runningStd3