DirichletTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Multivariate;
  3. use MathPHP\Probability\Distribution\Multivariate\Dirichlet;
  4. use MathPHP\Exception;
  5. class DirichletTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test pdf works as expected
  9. * @dataProvider dataProviderForPDF
  10. * @param array $xs
  11. * @param array $αs
  12. * @param float $expected
  13. * @throws \Exception
  14. */
  15. public function testPdf(array $xs, array $αs, $expected)
  16. {
  17. // Given
  18. $dirichlet = new Dirichlet($αs);
  19. // When
  20. $pdf = $dirichlet->pdf($xs);
  21. // Then
  22. $this->assertEqualsWithDelta($expected, $pdf, 0.00001);
  23. }
  24. /**
  25. * Test data made with scipy.stats.dirichlet.pdf
  26. * @return array
  27. */
  28. public function dataProviderForPdf(): array
  29. {
  30. return [
  31. [
  32. [0.07255081, 0.27811903, 0.64933016],
  33. [1, 2, 3],
  34. 7.03579378,
  35. ],
  36. [
  37. [0.48061647, 0.26403299, 0.25535054],
  38. [1, 2, 3],
  39. 1.0329588,
  40. ],
  41. [
  42. [0.1075077, 0.68975829, 0.20273401],
  43. [1, 2, 3],
  44. 1.70098867,
  45. ],
  46. [
  47. [0.14909711, 0.20747317, 0.64342972],
  48. [1, 2, 3],
  49. 5.15365594,
  50. ],
  51. [
  52. [0.19010044, 0.26225231, 0.54764725],
  53. [1, 2, 3],
  54. 4.71924363,
  55. ],
  56. [
  57. [0.06652357, 0.53396899, 0.39950744],
  58. [1, 2, 3],
  59. 5.11348541,
  60. ],
  61. [
  62. [0.16362941, 0.4521069, 0.38426368],
  63. [1, 2, 3],
  64. 4.00544774,
  65. ],
  66. [
  67. [0.07255081, 0.27811903, 0.64933016],
  68. [0.1, 0.3, 0.7],
  69. 0.76125217,
  70. ],
  71. [
  72. [0.48061647, 0.26403299, 0.25535054],
  73. [0.1, 0.3, 0.7],
  74. 0.19049659,
  75. ],
  76. [
  77. [0.1075077, 0.68975829, 0.20273401],
  78. [0.1, 0.3, 0.7],
  79. 0.40118652,
  80. ],
  81. [
  82. [0.14909711, 0.20747317, 0.64342972],
  83. [0.1, 0.3, 0.7],
  84. 0.49007413,
  85. ],
  86. [
  87. [0.19010044, 0.26225231, 0.54764725],
  88. [0.1, 0.3, 0.7],
  89. 0.35080774,
  90. ],
  91. [
  92. [0.06652357, 0.53396899, 0.39950744],
  93. [0.1, 0.3, 0.7],
  94. 0.6031302,
  95. ],
  96. [
  97. [0.16362941, 0.4521069, 0.38426368],
  98. [0.1, 0.3, 0.7],
  99. 0.30498251,
  100. ],
  101. ];
  102. }
  103. /**
  104. * @test pdf throws a BadDataException if the xs and αs do not have the same number of elements
  105. * @throws \Exception
  106. */
  107. public function testPdfArraysNotSameLengthException()
  108. {
  109. // Given
  110. $xs = [0.1, 0.2];
  111. $αs = [1, 2, 3];
  112. $dirichlet = new Dirichlet($αs);
  113. // Then
  114. $this->expectException(Exception\BadDataException::class);
  115. // When
  116. $pdf = $dirichlet->pdf($xs);
  117. }
  118. }