Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ZTesting
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 testHypothesis
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 zTest
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 zTestValues
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Math\Stochastic\Distribution
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Math\Stochastic\Distribution;
16
17use phpOMS\Math\Statistic\Average;
18use phpOMS\Math\Statistic\MeasureOfDispersion;
19
20/**
21 * ZTest
22 *
23 * Test if the mean is the same as the population mean
24 *
25 * @package phpOMS\Math\Stochastic\Distribution
26 * @license OMS License 2.0
27 * @link    https://jingga.app
28 * @since   1.0.0
29 *
30 * @internal
31 */
32final class ZTesting
33{
34    public const TABLE = [
35        '2.58' => 0.99,
36        '2.33' => 0.98,
37        '1.96' => 0.95,
38        '1.64' => 0.90,
39        '1.44' => 0.85,
40        '1.28' => 0.80,
41    ];
42
43    /**
44     * Test hypthesis.
45     *
46     * @param float $dataset      Value observed
47     * @param float $expected     Expected value
48     * @param float $total        Observed dataset size
49     * @param float $significance Significance
50     *
51     * @return bool
52     *
53     * @since 1.0.0
54     */
55    public static function testHypothesis(float $dataset, float $expected, float $total, float $significance = 0.95) : bool
56    {
57        $z = ($dataset - $expected) / \sqrt($expected * (1 - $expected) / $total);
58
59        $zSignificance = 0.0;
60        foreach (self::TABLE as $key => $value) {
61            if ($significance === $value) {
62                $zSignificance = (float) $key;
63            }
64        }
65
66        return $z > -$zSignificance && $z < $zSignificance;
67    }
68
69    /**
70     * Z-TEST.
71     *
72     * @param float      $value Value to test
73     * @param array      $data  Data
74     * @param null|float $sigma Sigma / Significance
75     *
76     * @return float
77     *
78     * @since 1.0.0
79     */
80    public static function zTest(float $value, array $data, float $sigma = null) : float
81    {
82        $sigma ??= MeasureOfDispersion::standardDeviationSample($data);
83
84        return 1 - NormalDistribution::getCdf((Average::arithmeticMean($data) - $value) / ($sigma / \sqrt(\count($data))), 0.0, 1.0);
85    }
86
87    /**
88     * Z-TEST.
89     *
90     * @param float $value    Value to test
91     * @param float $mean     Mean
92     * @param int   $dataSize Data size
93     * @param float $sigma    Sigma / Significance
94     *
95     * @return float
96     *
97     * @since 1.0.0
98     */
99    public static function zTestValues(float $value, float $mean, int $dataSize, float $sigma) : float
100    {
101        return 1 - NormalDistribution::getCdf(($mean - $value) / ($sigma / \sqrt($dataSize)), 0.0, 1.0);
102    }
103}