Completed
Push — master ( c2654a...8b49d6 )
by Alec
21:15 queued 13:44
created

TDistribution::tValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 2
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools;
4
5
class TDistribution
6
{
7
    protected const T_VALUES =
8
        [
9
            1 => [12.71, 31.82, 63.66, 127.3, 318.3, 636.6],
10
            2 => [4.303, 6.965, 9.925, 14.09, 22.33, 31.6],
11
            3 => [3.182, 4.541, 5.841, 7.453, 10.21, 12.92],
12
            4 => [2.776, 3.747, 4.604, 5.598, 7.173, 8.61],
13
            5 => [2.571, 3.365, 4.032, 4.773, 5.893, 6.869],
14
            6 => [2.447, 3.143, 3.707, 4.317, 5.208, 5.959],
15
            7 => [2.365, 2.998, 3.499, 4.029, 4.785, 5.408],
16
            8 => [2.306, 2.896, 3.355, 3.833, 4.501, 5.041],
17
            9 => [2.262, 2.821, 3.25, 3.69, 4.297, 4.781],
18
            10 => [2.228, 2.764, 3.169, 3.581, 4.144, 4.587],
19
            11 => [2.201, 2.718, 3.106, 3.497, 4.025, 4.437],
20
            12 => [2.179, 2.681, 3.055, 3.428, 3.93, 4.318],
21
            13 => [2.16, 2.65, 3.012, 3.372, 3.852, 4.221],
22
            14 => [2.145, 2.624, 2.977, 3.326, 3.787, 4.14],
23
            15 => [2.131, 2.602, 2.947, 3.286, 3.733, 4.073],
24
            16 => [2.12, 2.583, 2.921, 3.252, 3.686, 4.015],
25
            17 => [2.11, 2.567, 2.898, 3.222, 3.646, 3.965],
26
            18 => [2.101, 2.552, 2.878, 3.197, 3.61, 3.922],
27
            19 => [2.093, 2.539, 2.861, 3.174, 3.579, 3.883],
28
            20 => [2.086, 2.528, 2.845, 3.153, 3.552, 3.85],
29
            21 => [2.08, 2.518, 2.831, 3.135, 3.527, 3.819],
30
            22 => [2.074, 2.508, 2.819, 3.119, 3.505, 3.792],
31
            23 => [2.069, 2.5, 2.807, 3.104, 3.485, 3.767],
32
            24 => [2.064, 2.492, 2.797, 3.091, 3.467, 3.745],
33
            25 => [2.06, 2.485, 2.787, 3.078, 3.45, 3.725],
34
            26 => [2.056, 2.479, 2.779, 3.067, 3.435, 3.707],
35
            27 => [2.052, 2.473, 2.771, 3.057, 3.421, 3.69],
36
            28 => [2.048, 2.467, 2.763, 3.047, 3.408, 3.674],
37
            29 => [2.045, 2.462, 2.756, 3.038, 3.396, 3.659],
38
            30 => [2.042, 2.457, 2.75, 3.03, 3.385, 3.646],
39
            40 => [2.021, 2.423, 2.704, 2.971, 3.307, 3.551],
40
            50 => [2.009, 2.403, 2.678, 2.937, 3.261, 3.496],
41
            60 => [2, 2.39, 2.66, 2.915, 3.232, 3.46],
42
            80 => [1.99, 2.374, 2.639, 2.887, 3.195, 3.416],
43
            100 => [1.984, 2.364, 2.626, 2.871, 3.174, 3.39],
44
            120 => [1.98, 2.358, 2.617, 2.86, 3.16, 3.373],
45
            10000 => [1.96, 2.326, 2.576, 2.807, 3.09, 3.291],
46
        ];
47
48
    protected const P_VALUES = [
49
        0 => 0.95,
50
        1 => 0.98,
51
        2 => 0.99,
52
        3 => 0.995,
53
        4 => 0.998,
54
        5 => 0.999,
55
    ];
56
57
    /**
58
     * @param int $numOfMeasurements
59
     * @param float $p
60
     * @return float
61
     */
62
    public static function tValue(int $numOfMeasurements, float $p = 0.95): float
63
    {
64
        $x = array_search($p, self::P_VALUES, true);
65
        $y = self::getClosest($numOfMeasurements);
66
        return self::T_VALUES[$y][$x];
67
    }
68
69
    /**
70
     * @param int $search
71
     * @return int
72
     */
73
    protected static function getClosest(int $search): int
74
    {
75
        $closest = null;
76
        foreach (self::T_VALUES as $key => $item) {
77
            if ($closest === null || abs($search - $closest) > abs($key - $search)) {
78
                $closest = $key;
79
            }
80
        }
81
        return (int)$closest;
82
    }
83
}
84