Merge pull request #620 from macs3-project/misc/macs3/reproduce_debian_med_testing_re...
[MACS.git] / test / test_online.py
blobc0acb9bc6c55d1e4a551ac8cf40451baf5e9faf8
1 #!/usr/bin/env python
2 """Module Description: Test functions for online algorithm functions.
4 This code is free software; you can redistribute it and/or modify it
5 under the terms of the BSD License (see the file LICENSE included with
6 the distribution).
7 """
9 import unittest
10 import numpy as np
11 from math import sqrt
12 # ------------------------------------
13 # Main function
14 # ------------------------------------
16 def update_m_s(x, c, m, s):
17 c += 1
18 delta = x - m
19 m += delta / c
20 s += delta * (x - m)
21 return(c, m, s)
23 class Test_Mean(unittest.TestCase):
24 def setUp( self ):
25 # np.random.randint(1,1000,size=10)
26 self.data = [410, 710, 891, 312, 453, 460, 237, 622, 268]
27 # [np.mean(self.data[0:x]) for x in range(0,len(self.data)+1)]
28 self.correct_mean = [410.0, 560.0, 670.3333333333334, 580.75, 555.2, 539.3333333333334, 496.14285714285717, 511.875, 484.77777777777777]
29 # [np.std(self.data[0:x]) for x in range(0,len(self.data)+1)]
30 self.correct_stddev = [0.0, 150.0, 198.36050234078579, 231.48582569997671, 213.25984150795946, 197.8852080261573, 211.55845431425504, 202.2247743848414, 205.48737608036913]
32 def test_mean( self ):
33 c = 1
34 m = self.data[ 0 ]
35 s = 0
36 for i in range( 1, len( self.data ) ):
37 (c, m, s) = update_m_s( self.data[ i ], c, m, s )
38 mean = m
39 std = sqrt( s/c )
40 self.assertAlmostEqual( self.correct_mean[i], m, 2 )
41 self.assertAlmostEqual( self.correct_stddev[i], std, 2 )