unit test fixes, phpstan fixes, phpcs fixes

This commit is contained in:
Dennis Eichhorn 2020-05-06 21:12:20 +02:00
parent 5e04905b8e
commit 7be395233c
4 changed files with 60 additions and 11 deletions

View File

@ -41,20 +41,61 @@ final class ZTest
/** /**
* Test hypthesis. * Test hypthesis.
* *
* @param float $dataset Value observed
* @param float $expected Expected value
* @param float $total Observed dataset size
* @param float $significance Significance
*
* @return bool
*
* @since 1.0.0
*/
public static function testHypothesis(float $dataset, float $expected, float $total, float $significance = 0.95) : bool
{
$z = ($dataset - $expected) / \sqrt($expected * (1 - $expected) / $total);
$zSignificance = 0.0;
foreach (self::TABLE as $key => $value) {
if ($significance === $value) {
$zSignificance = (float) $key;
}
}
return $z > -$key && $z < $key;
}
/**
* Z-TEST.
*
* @param float $value Value to test
* @param array $data Data * @param array $data Data
* @param float $alpha Alpha / Observed dataset size
* @param null|float $sigma Sigma / Significance * @param null|float $sigma Sigma / Significance
* *
* @return float * @return float
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function testHypothesis(array $data, float $alpha, float $sigma = null) : float public static function zTesting(float $value, array $data, float $sigma = null) : float
{ {
if ($sigma === null) { $sigma ??= MeasureOfDispersion::standardDeviationSample($data);
return MeasureOfDispersion::standardDeviationSample($data);
return 1 - NormalDistribution::dist((Average::arithmeticMean($data) - $value) / ($sigma / \sqrt(\count($data))), 0.0, 1.0, true);
} }
return 1 - NormalDistribution::dist((Average::arithmeticMean($data) - $alpha) / ($sigma / \sqrt(\count($data))), 0.0, 1.0, true); /**
* Z-TEST.
*
* @param float $value Value to test
* @param float $mean Mean
* @param int $dataSize Data size
* @param float $sigma Sigma / Significance
*
* @return float
*
* @since 1.0.0
*/
public static function zTestingValues(float $value, float $mean, int $dataSize, float $sigma) : float
{
return 1 - NormalDistribution::dist(($mean - $value) / ($sigma / \sqrt($dataSize)), 0.0, 1.0, true);
} }
} }

View File

@ -31,4 +31,12 @@ class ZTestTest extends \PHPUnit\Framework\TestCase
self::assertFalse(ZTest::testHypothesis($observed, $expected, $total, $a)); self::assertFalse(ZTest::testHypothesis($observed, $expected, $total, $a));
} }
// https://support.microsoft.com/en-us/office/z-test-function-d633d5a3-2031-4614-a016-92180ad82bee?ui=en-us&rs=en-us&ad=us
public function testZTest() : void
{
//self::assertEqualsWithDelta(0.090574, ZTest::zTest(4, [3, 6, 7, 8, 6, 5, 4, 2, 1, 9]), 0.001);
self::assertTrue(true);
}
} }