ChiSquaredTableTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Table;
  3. use MathPHP\Probability\Distribution\Table\ChiSquared;
  4. use MathPHP\Exception;
  5. class ChiSquaredTableTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test values from constant
  9. * @dataProvider dataProviderForTable
  10. * @param int $df
  11. * @param float $p
  12. * @param float $χ²
  13. */
  14. public function testChiSquaredValuesFromConstant(int $df, float $p, float $χ²)
  15. {
  16. // Given
  17. $p = \sprintf('%1.3f', $p);
  18. // When
  19. $value = ChiSquared::CHI_SQUARED_SCORES[$df][$p];
  20. // Then
  21. $this->assertEquals($χ², $value);
  22. }
  23. /**
  24. * @test values from function
  25. * @dataProvider dataProviderForTable
  26. */
  27. public function testChiSquaredValuesFromFunction(int $df, float $p, float $χ²)
  28. {
  29. // When
  30. $value = ChiSquared::getChiSquareValue($df, $p);
  31. // Then
  32. $this->assertEquals($χ², $value);
  33. }
  34. public function dataProviderForTable(): array
  35. {
  36. return [
  37. [1, 0.995, 0.0000393],
  38. [1, 0.05, 3.841],
  39. [1, 0.050, 3.841],
  40. [1, 0.01, 6.635],
  41. [5, 0.05, 11.070],
  42. [5, 0.01, 15.086],
  43. ];
  44. }
  45. /**
  46. * @test exception
  47. * @throws Exception\BadDataException
  48. */
  49. public function testChiSquaredException()
  50. {
  51. // Given
  52. $df = 88474;
  53. $p = 0.44;
  54. // Then
  55. $this->expectException(Exception\BadDataException::class);
  56. // When
  57. ChiSquared::getChiSquareValue($df, $p);
  58. }
  59. }