test_boxcox.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. from numpy.testing import assert_equal, assert_almost_equal, assert_allclose
  4. from scipy.special import boxcox, boxcox1p, inv_boxcox, inv_boxcox1p
  5. # There are more tests of boxcox and boxcox1p in test_mpmath.py.
  6. def test_boxcox_basic():
  7. x = np.array([0.5, 1, 2, 4])
  8. # lambda = 0 => y = log(x)
  9. y = boxcox(x, 0)
  10. assert_almost_equal(y, np.log(x))
  11. # lambda = 1 => y = x - 1
  12. y = boxcox(x, 1)
  13. assert_almost_equal(y, x - 1)
  14. # lambda = 2 => y = 0.5*(x**2 - 1)
  15. y = boxcox(x, 2)
  16. assert_almost_equal(y, 0.5*(x**2 - 1))
  17. # x = 0 and lambda > 0 => y = -1 / lambda
  18. lam = np.array([0.5, 1, 2])
  19. y = boxcox(0, lam)
  20. assert_almost_equal(y, -1.0 / lam)
  21. def test_boxcox_underflow():
  22. x = 1 + 1e-15
  23. lmbda = 1e-306
  24. y = boxcox(x, lmbda)
  25. assert_allclose(y, np.log(x), rtol=1e-14)
  26. def test_boxcox_nonfinite():
  27. # x < 0 => y = nan
  28. x = np.array([-1, -1, -0.5])
  29. y = boxcox(x, [0.5, 2.0, -1.5])
  30. assert_equal(y, np.array([np.nan, np.nan, np.nan]))
  31. # x = 0 and lambda <= 0 => y = -inf
  32. x = 0
  33. y = boxcox(x, [-2.5, 0])
  34. assert_equal(y, np.array([-np.inf, -np.inf]))
  35. def test_boxcox1p_basic():
  36. x = np.array([-0.25, -1e-20, 0, 1e-20, 0.25, 1, 3])
  37. # lambda = 0 => y = log(1+x)
  38. y = boxcox1p(x, 0)
  39. assert_almost_equal(y, np.log1p(x))
  40. # lambda = 1 => y = x
  41. y = boxcox1p(x, 1)
  42. assert_almost_equal(y, x)
  43. # lambda = 2 => y = 0.5*((1+x)**2 - 1) = 0.5*x*(2 + x)
  44. y = boxcox1p(x, 2)
  45. assert_almost_equal(y, 0.5*x*(2 + x))
  46. # x = -1 and lambda > 0 => y = -1 / lambda
  47. lam = np.array([0.5, 1, 2])
  48. y = boxcox1p(-1, lam)
  49. assert_almost_equal(y, -1.0 / lam)
  50. def test_boxcox1p_underflow():
  51. x = np.array([1e-15, 1e-306])
  52. lmbda = np.array([1e-306, 1e-18])
  53. y = boxcox1p(x, lmbda)
  54. assert_allclose(y, np.log1p(x), rtol=1e-14)
  55. def test_boxcox1p_nonfinite():
  56. # x < -1 => y = nan
  57. x = np.array([-2, -2, -1.5])
  58. y = boxcox1p(x, [0.5, 2.0, -1.5])
  59. assert_equal(y, np.array([np.nan, np.nan, np.nan]))
  60. # x = -1 and lambda <= 0 => y = -inf
  61. x = -1
  62. y = boxcox1p(x, [-2.5, 0])
  63. assert_equal(y, np.array([-np.inf, -np.inf]))
  64. def test_inv_boxcox():
  65. x = np.array([0., 1., 2.])
  66. lam = np.array([0., 1., 2.])
  67. y = boxcox(x, lam)
  68. x2 = inv_boxcox(y, lam)
  69. assert_almost_equal(x, x2)
  70. x = np.array([0., 1., 2.])
  71. lam = np.array([0., 1., 2.])
  72. y = boxcox1p(x, lam)
  73. x2 = inv_boxcox1p(y, lam)
  74. assert_almost_equal(x, x2)
  75. def test_inv_boxcox1p_underflow():
  76. x = 1e-15
  77. lam = 1e-306
  78. y = inv_boxcox1p(x, lam)
  79. assert_allclose(y, x, rtol=1e-14)