コード例 #1
0
ファイル: PearsonChiSquare.php プロジェクト: BGCX067/fajr-git
 /**
  * Compute chiSquare test value from array of observations
  * and expected probabilities of slots.
  *
  * @param array(int) $observedCnt observed counts
  * @param array(double) $expectedProb expected probabilities
  *
  * @returns double chi squared
  */
 static function chiSquare(array $observedCnt, array $expectedProb)
 {
     Preconditions::check(count($observedCnt) == count($expectedProb), "number of slots for observations and expectations should match.");
     foreach ($observedCnt as $cnt) {
         Preconditions::check(is_int($cnt), "Observed count should be integer");
     }
     foreach ($expectedProb as $prob) {
         Preconditions::check(is_double($prob), "Probability should be double");
         Preconditions::check(0.0 < $prob && 1.0 > $prob, "Expected probabilities shoud be in range (0,1).");
     }
     Preconditions::check(abs(array_sum($expectedProb) - 1) < 1.0E-6, "Probabilities does not sum up to 1");
     $samples = array_sum($observedCnt);
     foreach ($expectedProb as $prob) {
         if ($samples * $prob <= 5) {
             // @see http://en.wikipedia.org/wiki/Pearson's_chi-square_test#Problems
             throw new RuntimeException("You should have more samples for " . "computing Pearson test with specified probabilities");
         }
     }
     $chisqr = 0;
     for ($i = 0; $i < count($observedCnt); $i++) {
         $expected = $expectedProb[$i] * $samples;
         $chisqr += Math::sqr($observedCnt[$i] - $expected) / $expected;
     }
     return $chisqr;
 }
コード例 #2
0
ファイル: MathTest.php プロジェクト: BGCX067/fajr-git
 public function testSqr()
 {
     $this->assertSame(0, Math::sqr(0));
     $this->assertSame(1, Math::sqr(-1));
     $this->assertSame(1, Math::sqr(1));
     $this->assertSame(4, Math::sqr(2));
     $this->assertSame(4.0, Math::sqr(2.0));
     $this->assertEquals(1.7689, Math::sqr(1.33), '', 1.0E-10);
 }