diff --git a/Business/Marketing/Metrics.php b/Business/Marketing/Metrics.php index 2f3e28be7..f97ff12a3 100644 --- a/Business/Marketing/Metrics.php +++ b/Business/Marketing/Metrics.php @@ -142,7 +142,7 @@ final class Metrics */ public static function calclateMailingSuccess(float $discountRate, array $purchaseProbability, array $payoffs) : Matrix { - $count = \count($purchaseProbability); + $count = \count($purchaseProbability); $profit = new Matrix($count, $count); $G = Vector::fromArray($payoffs); @@ -233,8 +233,8 @@ final class Metrics $count = \count($purchaseProbability); for ($i = 0; $i < $count; ++$i) { - $matrix[$i] = []; - $matrix[$i][0] = $purchaseProbability[$i]; + $matrix[$i] = []; + $matrix[$i][0] = $purchaseProbability[$i]; $matrix[$i][$i + 1] = 1 - $purchaseProbability[$i]; } diff --git a/Math/Stochastic/Distribution/TDistribution.php b/Math/Stochastic/Distribution/TDistribution.php index 23ed88608..1cb1bbd76 100644 --- a/Math/Stochastic/Distribution/TDistribution.php +++ b/Math/Stochastic/Distribution/TDistribution.php @@ -188,10 +188,10 @@ final class TDistribution $sum = 0.0; if ($degrees % 2 === 1) { - $i = 3; + $i = 3; $term = $cos; } else { - $i = 2; + $i = 2; $term = 1; } diff --git a/Math/Stochastic/Distribution/ZTest.php b/Math/Stochastic/Distribution/ZTest.php index 288b043e8..c7f4464a0 100644 --- a/Math/Stochastic/Distribution/ZTest.php +++ b/Math/Stochastic/Distribution/ZTest.php @@ -41,20 +41,61 @@ final class ZTest /** * 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 float $alpha Alpha / Observed dataset size * @param null|float $sigma Sigma / Significance * * @return float * * @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) { - return MeasureOfDispersion::standardDeviationSample($data); - } + $sigma ??= MeasureOfDispersion::standardDeviationSample($data); - return 1 - NormalDistribution::dist((Average::arithmeticMean($data) - $alpha) / ($sigma / \sqrt(\count($data))), 0.0, 1.0, true); + return 1 - NormalDistribution::dist((Average::arithmeticMean($data) - $value) / ($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); } } diff --git a/tests/Math/Stochastic/Distribution/ZTestTest.php b/tests/Math/Stochastic/Distribution/ZTestTest.php index ad983171e..a6bcf5ac5 100644 --- a/tests/Math/Stochastic/Distribution/ZTestTest.php +++ b/tests/Math/Stochastic/Distribution/ZTestTest.php @@ -31,4 +31,12 @@ class ZTestTest extends \PHPUnit\Framework\TestCase 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); + } }