def testCorrelation(self):
        mm = Correlation(dependency=['close', 'open'])

        for i, (openPrice, closePrice) in enumerate(zip(self.samplesOpen, self.samplesClose)):
            mm.push(dict(open=openPrice, close=closePrice))
            if i == 0:
                self.assertTrue(np.isnan(mm.result()))
            if i >= 1:
                expected = np.corrcoef(self.samplesOpen[:i + 1], self.samplesClose[:i + 1])[0, 1]
                calculated = mm.result()
                self.assertAlmostEqual(expected, calculated, 10, "at index {0:d}\n"
                                                                 "expected correlation:   {1:f}\n"
                                                                 "calculated correlation: {2:f}".format(i, expected,
                                                                                                        calculated))
    def testCompoundedOperator(self):
        ma5 = MovingAverage(5, 'x')
        maxer = Max('close')
        max5ma = Max('close') >> MovingAverage(5)
        max5ma2 = MovingAverage(5, Max('close'))
        average = Average('close')
        sumM = Sum('close')
        mvTest = Correlation(dependency=('x', 'y'))
        mvCorr = (Average('close') ^ Sum('close')) >> Correlation(dependency=('x', 'y'))

        for i, close in enumerate(self.sampleClose):
            data = {'close': close, 'open': open}
            maxer.push(data)
            data2 = {'x': maxer.result()}
            ma5.push(data2)
            max5ma.push(data)
            max5ma2.push(data)
            average.push(data)
            sumM.push(data)
            data3 = {'x': average.result(), 'y': sumM.result()}
            mvTest.push(data3)
            mvCorr.push(data)

            expected = ma5.result()
            calculated = max5ma.result()
            self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n"
                                                             "expected:   {1:f}\n"
                                                             "calculated: {2:f}".format(i, expected, calculated))

            calculated = max5ma2.result()
            self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n"
                                                             "expected:   {1:f}\n"
                                                             "calculated: {2:f}".format(i, expected, calculated))

            if i >= 1:
                expected = mvTest.result()
                calculated = mvCorr.result()
                self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n"
                                                                 "expected:   {1:f}\n"
                                                                 "calculated: {2:f}".format(i, expected, calculated))

        with self.assertRaises(ValueError):
            _ = Max('close') >> math.sqrt

        with self.assertRaises(ValueError):
            _ = (Max('close') ^ Minimum('close')) >> MovingCorrelation(20, dependency=('x', 'y', 'z'))

        (Max('close') ^ Minimum('close')) >> MovingCorrelation(20, dependency=('x', 'y'))
Пример #3
0
    def testCompoundedOperator(self):
        ma5 = MovingAverage(5, 'x')
        maxer = Max('close')
        max5ma = Max('close') >> MovingAverage(5)
        max5ma2 = MovingAverage(5, Max('close'))
        average = Average('close')
        sumM = Sum('close')
        mvTest = Correlation(dependency=('x', 'y'))
        mvCorr = (Average('close')
                  ^ Sum('close')) >> Correlation(dependency=('x', 'y'))

        for i, close in enumerate(self.sampleClose):
            data = {'close': close, 'open': 1.}
            maxer.push(data)
            data2 = {'x': maxer.result()}
            ma5.push(data2)
            max5ma.push(data)
            max5ma2.push(data)
            average.push(data)
            sumM.push(data)
            data3 = {'x': average.result(), 'y': sumM.result()}
            mvTest.push(data3)
            mvCorr.push(data)

            expected = ma5.result()
            calculated = max5ma.result()
            self.assertAlmostEqual(
                calculated, expected, 12, "at index {0:d}\n"
                "expected:   {1:f}\n"
                "calculated: {2:f}".format(i, expected, calculated))

            calculated = max5ma2.result()
            self.assertAlmostEqual(
                calculated, expected, 12, "at index {0:d}\n"
                "expected:   {1:f}\n"
                "calculated: {2:f}".format(i, expected, calculated))

            if i >= 1:
                expected = mvTest.result()
                calculated = mvCorr.result()
                self.assertAlmostEqual(
                    calculated, expected, 12, "at index {0:d}\n"
                    "expected:   {1:f}\n"
                    "calculated: {2:f}".format(i, expected, calculated))

        with self.assertRaises(ValueError):
            _ = Max('close') >> math.sqrt

        with self.assertRaises(ValueError):
            _ = (Max('close') ^ Min('close')) >> MovingCorrelation(
                20, dependency=('x', 'y', 'z'))

        (Max('close') ^ Min('close')) >> MovingCorrelation(
            20, dependency=('x', 'y'))
    def testCorrelation(self):
        mm = Correlation(dependency=['close', 'open'])

        for i, (openPrice, closePrice) in enumerate(
                zip(self.samplesOpen, self.samplesClose)):
            mm.push(dict(open=openPrice, close=closePrice))
            if i == 0:
                self.assertTrue(np.isnan(mm.result()))
            if i >= 1:
                expected = np.corrcoef(self.samplesOpen[:i + 1],
                                       self.samplesClose[:i + 1])[0, 1]
                calculated = mm.result()
                self.assertAlmostEqual(
                    expected, calculated, 10, "at index {0:d}\n"
                    "expected correlation:   {1:f}\n"
                    "calculated correlation: {2:f}".format(
                        i, expected, calculated))