test_mstats_extras.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. import numpy.ma as ma
  4. import scipy.stats.mstats as ms
  5. from numpy.testing import (assert_equal, assert_almost_equal, assert_,
  6. assert_allclose)
  7. def test_compare_medians_ms():
  8. x = np.arange(7)
  9. y = x + 10
  10. assert_almost_equal(ms.compare_medians_ms(x, y), 0)
  11. y2 = np.linspace(0, 1, num=10)
  12. assert_almost_equal(ms.compare_medians_ms(x, y2), 0.017116406778)
  13. def test_hdmedian():
  14. # 1-D array
  15. x = ma.arange(11)
  16. assert_allclose(ms.hdmedian(x), 5, rtol=1e-14)
  17. x.mask = ma.make_mask(x)
  18. x.mask[:7] = False
  19. assert_allclose(ms.hdmedian(x), 3, rtol=1e-14)
  20. # Check that `var` keyword returns a value. TODO: check whether returned
  21. # value is actually correct.
  22. assert_(ms.hdmedian(x, var=True).size == 2)
  23. # 2-D array
  24. x2 = ma.arange(22).reshape((11, 2))
  25. assert_allclose(ms.hdmedian(x2, axis=0), [10, 11])
  26. x2.mask = ma.make_mask(x2)
  27. x2.mask[:7, :] = False
  28. assert_allclose(ms.hdmedian(x2, axis=0), [6, 7])
  29. def test_rsh():
  30. np.random.seed(132345)
  31. x = np.random.randn(100)
  32. res = ms.rsh(x)
  33. # Just a sanity check that the code runs and output shape is correct.
  34. # TODO: check that implementation is correct.
  35. assert_(res.shape == x.shape)
  36. # Check points keyword
  37. res = ms.rsh(x, points=[0, 1.])
  38. assert_(res.size == 2)
  39. def test_mjci():
  40. # Tests the Marits-Jarrett estimator
  41. data = ma.array([77, 87, 88,114,151,210,219,246,253,262,
  42. 296,299,306,376,428,515,666,1310,2611])
  43. assert_almost_equal(ms.mjci(data),[55.76819,45.84028,198.87875],5)
  44. def test_trimmed_mean_ci():
  45. # Tests the confidence intervals of the trimmed mean.
  46. data = ma.array([545,555,558,572,575,576,578,580,
  47. 594,605,635,651,653,661,666])
  48. assert_almost_equal(ms.trimmed_mean(data,0.2), 596.2, 1)
  49. assert_equal(np.round(ms.trimmed_mean_ci(data,(0.2,0.2)),1),
  50. [561.8, 630.6])
  51. def test_idealfourths():
  52. # Tests ideal-fourths
  53. test = np.arange(100)
  54. assert_almost_equal(np.asarray(ms.idealfourths(test)),
  55. [24.416667,74.583333],6)
  56. test_2D = test.repeat(3).reshape(-1,3)
  57. assert_almost_equal(ms.idealfourths(test_2D, axis=0),
  58. [[24.416667,24.416667,24.416667],
  59. [74.583333,74.583333,74.583333]],6)
  60. assert_almost_equal(ms.idealfourths(test_2D, axis=1),
  61. test.repeat(2).reshape(-1,2))
  62. test = [0, 0]
  63. _result = ms.idealfourths(test)
  64. assert_(np.isnan(_result).all())
  65. class TestQuantiles(object):
  66. data = [0.706560797,0.727229578,0.990399276,0.927065621,0.158953014,
  67. 0.887764025,0.239407086,0.349638551,0.972791145,0.149789972,
  68. 0.936947700,0.132359948,0.046041972,0.641675031,0.945530547,
  69. 0.224218684,0.771450991,0.820257774,0.336458052,0.589113496,
  70. 0.509736129,0.696838829,0.491323573,0.622767425,0.775189248,
  71. 0.641461450,0.118455200,0.773029450,0.319280007,0.752229111,
  72. 0.047841438,0.466295911,0.583850781,0.840581845,0.550086491,
  73. 0.466470062,0.504765074,0.226855960,0.362641207,0.891620942,
  74. 0.127898691,0.490094097,0.044882048,0.041441695,0.317976349,
  75. 0.504135618,0.567353033,0.434617473,0.636243375,0.231803616,
  76. 0.230154113,0.160011327,0.819464108,0.854706985,0.438809221,
  77. 0.487427267,0.786907310,0.408367937,0.405534192,0.250444460,
  78. 0.995309248,0.144389588,0.739947527,0.953543606,0.680051621,
  79. 0.388382017,0.863530727,0.006514031,0.118007779,0.924024803,
  80. 0.384236354,0.893687694,0.626534881,0.473051932,0.750134705,
  81. 0.241843555,0.432947602,0.689538104,0.136934797,0.150206859,
  82. 0.474335206,0.907775349,0.525869295,0.189184225,0.854284286,
  83. 0.831089744,0.251637345,0.587038213,0.254475554,0.237781276,
  84. 0.827928620,0.480283781,0.594514455,0.213641488,0.024194386,
  85. 0.536668589,0.699497811,0.892804071,0.093835427,0.731107772]
  86. def test_hdquantiles(self):
  87. data = self.data
  88. assert_almost_equal(ms.hdquantiles(data,[0., 1.]),
  89. [0.006514031, 0.995309248])
  90. hdq = ms.hdquantiles(data,[0.25, 0.5, 0.75])
  91. assert_almost_equal(hdq, [0.253210762, 0.512847491, 0.762232442,])
  92. hdq = ms.hdquantiles_sd(data,[0.25, 0.5, 0.75])
  93. assert_almost_equal(hdq, [0.03786954, 0.03805389, 0.03800152,], 4)
  94. data = np.array(data).reshape(10,10)
  95. hdq = ms.hdquantiles(data,[0.25,0.5,0.75],axis=0)
  96. assert_almost_equal(hdq[:,0], ms.hdquantiles(data[:,0],[0.25,0.5,0.75]))
  97. assert_almost_equal(hdq[:,-1], ms.hdquantiles(data[:,-1],[0.25,0.5,0.75]))
  98. hdq = ms.hdquantiles(data,[0.25,0.5,0.75],axis=0,var=True)
  99. assert_almost_equal(hdq[...,0],
  100. ms.hdquantiles(data[:,0],[0.25,0.5,0.75],var=True))
  101. assert_almost_equal(hdq[...,-1],
  102. ms.hdquantiles(data[:,-1],[0.25,0.5,0.75], var=True))
  103. def test_hdquantiles_sd(self):
  104. # Only test that code runs, implementation not checked for correctness
  105. res = ms.hdquantiles_sd(self.data)
  106. assert_(res.size == 3)
  107. def test_mquantiles_cimj(self):
  108. # Only test that code runs, implementation not checked for correctness
  109. ci_lower, ci_upper = ms.mquantiles_cimj(self.data)
  110. assert_(ci_lower.size == ci_upper.size == 3)