예제 #1
0
def task_4_2_1():
    """
    Execute exercise 4.2.1, which is basically just a test for the auto correlation.
    """
    # TODO Task 4.2.1: Your code goes here
    list1 = [
        1., -1., 1., -1., 1., -1., 1., -1., 1., -1., 1., -1., 1., -1., 1., -1.,
        1., -1., 1., -1., 1., -1., 1., -1.
    ]
    list2 = [
        1., 1., -1., 1., 1., -1., 1., 1., -1., 1., 1., -1., 1., 1., -1., 1.,
        1., -1., 1., 1., -1., 1., 1., -1., 1., 1., -1.
    ]
    TIAC1 = TimeIndependentAutocorrelationCounter("EX1")
    TIAC2 = TimeIndependentAutocorrelationCounter("EX2")
    for i in range(len(list1)):
        TIAC1.count(list1[i])
    for i in range(len(list2)):
        TIAC2.count(list2[i])
    print "The Autocorrelation for the example 1 with  (lag%2 = 0) : ", TIAC1.get_auto_cov(
        0)
    print "The Autocorrelation for the example 1 with  (lag%2 = 1) : ", TIAC1.get_auto_cov(
        1)
    print "The Autocorrelation for the example 2 with  (lag%3 = 0) : ", TIAC2.get_auto_cov(
        0)
    print "The Autocorrelation for the example 2 with  (lag%3 = 1) : ", TIAC2.get_auto_cov(
        1)
    print "The Autocorrelation for the example 2 with  (lag%3 = 2) : ", TIAC2.get_auto_cov(
        2)
    def test_auto_correlation(self):
        """
        Test the basic implementation of the auto covariance counter.
        """
        tiacc = TimeIndependentAutocorrelationCounter(max_lag=5)

        for i in range(5000):
            tiacc.count(i % 25)

        results_cov = [52.0, 40.0, 29.0, 19.0, 10.0]
        results_cor = [0.9998, 0.7691, 0.5576, 0.3653, 0.1923]

        for lag in range(5):
            self.assertAlmostEqual(
                tiacc.get_auto_cov(lag),
                results_cov[lag],
                delta=.05,
                msg=
                "Error in TimeIndependentAutocorrelationCounter. Covariance calculation is wrong."
            )
            self.assertAlmostEqual(
                tiacc.get_auto_cor(lag),
                results_cor[lag],
                delta=.05,
                msg=
                "Error in TimeIndependentAutocorrelationCounter. Correlation calculation is wrong."
            )
    def test_auto_correlation(self):
        """
        Test the basic implementation of the auto covariance counter.
        """
        tiacc = TimeIndependentAutocorrelationCounter(max_lag=5)

        tiacc.count(1)
        tiacc.count(2)
        tiacc.count(3)
        tiacc.count(1)
        tiacc.count(2)
        tiacc.count(3)

        results_cov = [2. / 3., -.2, -.5, 2. / 3., 0., -1.]
        results_cor = [5. / 6., -.25, -.625, 5. / 6., 0., -1.25]

        for lag in range(5):
            self.assertAlmostEqual(
                tiacc.get_auto_cov(lag),
                results_cov[lag],
                delta=.01,
                msg=
                "Error in TimeIndependentAutocorrelationCounter. Covariance calculation is wrong."
            )
            self.assertAlmostEqual(
                tiacc.get_auto_cor(lag),
                results_cor[lag],
                delta=.01,
                msg=
                "Error in TimeIndependentAutocorrelationCounter. Correlation calculation is wrong."
            )