test_boxcox.py 2.6 KB

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