def test_std(self): """Should produce a standard deviation of 1.0 for a std normal dist""" expected = 1.58113883008 self.assertFloatEqual(std(array([1,2,3,4,5])), expected) expected_a = array([expected, expected, expected, expected, expected]) a = array([[1,2,3,4,5],[5,1,2,3,4],[4,5,1,2,3],[3,4,5,1,2],[2,3,4,5,1]]) self.assertFloatEqual(std(a,axis=0), expected_a) self.assertFloatEqual(std(a,axis=1), expected_a) self.assertRaises(ValueError, std, a, 5)
def test_std(self): """Should produce a standard deviation of 1.0 for a std normal dist""" expected = 1.58113883008 self.assertFloatEqual(std(array([1, 2, 3, 4, 5])), expected) expected_a = array([expected, expected, expected, expected, expected]) a = array([[1, 2, 3, 4, 5], [5, 1, 2, 3, 4], [4, 5, 1, 2, 3], [3, 4, 5, 1, 2], [2, 3, 4, 5, 1]]) self.assertFloatEqual(std(a, axis=0), expected_a) self.assertFloatEqual(std(a, axis=1), expected_a) self.assertRaises(ValueError, std, a, 5)
def test_std_2d(self): """Should produce from 2darray the same stdevs as scipy.stats.std""" inp = array([[1, 2, 3], [4, 5, 6]]) exps = ( #tuple(scipy_std(inp, ax) for ax in [None, 0, 1]) 1.8708286933869707, array([2.12132034, 2.12132034, 2.12132034]), array([1., 1.])) results = tuple(std(inp, ax) for ax in [None, 0, 1]) for obs, exp in zip(results, exps): testing.assert_almost_equal(obs, exp)
def test_std_2d(self): """Should produce from 2darray the same stdevs as scipy.stats.std""" inp = array([[1,2,3],[4,5,6]]) exps = ( #tuple(scipy_std(inp, ax) for ax in [None, 0, 1]) 1.8708286933869707, array([ 2.12132034, 2.12132034, 2.12132034]), array([ 1., 1.])) results = tuple(std(inp, ax) for ax in [None, 0, 1]) for obs, exp in zip(results, exps): testing.assert_almost_equal(obs, exp)
def test_std_3d(self): """Should produce from 3darray the same std devs as scipy.stats.std""" inp3d = array( #2,2,3 [[[0, 2, 2], [3, 4, 5]], [[1, 9, 0], [9, 10, 1]]]) exp3d = ( #for axis None, 0, 1, 2: calc from scipy.stats.std 3.63901418552, array([[0.70710678, 4.94974747, 1.41421356], [4.24264069, 4.24264069, 2.82842712]]), array([[2.12132034, 1.41421356, 2.12132034], [5.65685425, 0.70710678, 0.70710678]]), array([[1.15470054, 1.], [4.93288286, 4.93288286]])) res = tuple(std(inp3d, ax) for ax in [None, 0, 1, 2]) for obs, exp in zip(res, exp3d): testing.assert_almost_equal(obs, exp)
def test_std_3d(self): """Should produce from 3darray the same std devs as scipy.stats.std""" inp3d = array(#2,2,3 [[[ 0, 2, 2], [ 3, 4, 5]], [[ 1, 9, 0], [ 9, 10, 1]]]) exp3d = (#for axis None, 0, 1, 2: calc from scipy.stats.std 3.63901418552, array([[ 0.70710678, 4.94974747, 1.41421356], [ 4.24264069, 4.24264069, 2.82842712]]), array([[ 2.12132034, 1.41421356, 2.12132034], [ 5.65685425, 0.70710678, 0.70710678]]), array([[ 1.15470054, 1. ], [ 4.93288286, 4.93288286]])) res = tuple(std(inp3d, ax) for ax in [None, 0, 1, 2]) for obs, exp in zip(res, exp3d): testing.assert_almost_equal(obs, exp)
def change_length_analysis(n, branch_lengths=\ [0.05, 0.1, 0.15, 0.2, 0.25,0.3, 0.35, 0.4], leaves=16, slen=1000): header = ['mean_three_m','mean_three_sd','var_three_m', 'var_three_sd', \ 'ratio_three_m', 'ratio_three_sd'] conditions = {'one_q':balanced_one_q_tree, 'two_q':balanced_two_q_tree, \ 'multi_q':balanced_multi_q_tree} condition_names = ['one_q', 'two_q', 'multi_q'] result_f = mean_var_eigen full_header = ['length'] + \ [name+i for name in condition_names for i in header] print '\t'.join(full_header) for b in lengths: result = [b] for c in condition_names: tree_f = conditions[c] make_tree = lambda:tree_f(n=leaves,seq_length=slen,length=b,\ perturb=False) samples = list(tree_stats(n, make_tree, result_f)) means = average(samples) stdevs = std(samples) for i in zip(means, stdevs): result.extend(i) print '\t'.join(map(str, result))
def var(x): return std(x)**2