Completed
Push — master ( eb3cd5...621598 )
by Yasunori
12s queued 11s
created

Measurement::quartile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace devfym\IntelliPHP\Math;
4
5
use devfym\IntelliPHP\Traits\IsNotNullTrait;
6
7
class Measurement
8
{
9
    protected $sample;
10
11
    protected $sum;
12
13
    protected $count;
14
15
    use IsNotNullTrait;
16
17
    public function __construct(array $inputs)
18
    {
19
        $this->sample = array_values(array_filter($inputs, array($this, 'is_not_null')));
20
        $this->sum = array_sum($this->sample);
21
        $this->count = count($this->sample);
22
    }
23
24
    final public function mean(int $decimal = 4) : float
25
    {
26
        return round($this->sum / $this->count, $decimal);
27
    }
28
29
    final public function max(int $decimal = 4) : float
30
    {
31
        $max = $this->sample[0];
32
33
        for ($i = 1; $i < $this->count; $i++) {
34
            $max = max($max, $this->sample[$i]);
35
        }
36
37
        return round($max, $decimal);
38
    }
39
40
    final public function min(int $decimal = 4) : float
41
    {
42
        $min = $this->sample[0];
43
44
        for ($i = 1; $i < $this->count; $i++) {
45
            $min = min($min, $this->sample[$i]);
46
        }
47
48
        return round($min, $decimal);
49
    }
50
51
    final public function quartile(int $Q, int $decimal = 4) : float
52
    {
53
        $sort_sample = $this->sample;
54
        sort($sort_sample);
55
56
        $quartile = ($Q / 4) * (($this->count) + 1);
57
58
        return round($sort_sample[$quartile - 1], $decimal);
59
    }
60
61
    final public function median(int $decimal = 4) : float
62
    {
63
        return $this->quartile(2, $decimal);
64
    }
65
66
    final public function variance(int $decimal = 4) : float
67
    {
68
        $mean = $this->mean(4);
69
        $total_s = 0;
70
71
        foreach ($this->sample as $s) {
72
            $total_s += pow(($s - $mean), 2);
73
        }
74
75
        $variance = $total_s / $this->count;
76
77
        return round($variance, $decimal);
78
    }
79
80
    final public function std(int $decimal = 4) : float
81
    {
82
        $std = sqrt($this->variance($decimal));
83
84
        return round($std, $decimal);
85
    }
86
}