Series::dataType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace devfym\IntelliPHP\Data;
4
5
use devfym\IntelliPHP\Math\StatisticInterface;
6
7
class Series implements StatisticInterface, DataManipulationInterface
8
{
9
    /**
10
     * @var array
11
     * Stores series of data in single array format.
12
     */
13
    protected $Sample = [];
14
15
    /**
16
     * @var int
17
     * Stores total number of $Sample.
18
     */
19
    protected $SampleCount = 0;
20
21
    /**
22
     * @var string
23
     * Stores if $Sample data is numeric or string value.
24
     */
25
    protected $DataType = 'Object';
26
27
    /**
28
     * @var int
29
     * Count number of NULL values in data.
30
     */
31
    protected $null_counter = 0;
32
33
    /**
34
     * BasicStatistics constructor.
35
     */
36
    public function __construct()
37
    {
38
        //
39
    }
40
41
    /**
42
     * @param array $Sample
43
     * Set $Sample value.
44
     */
45
    public function setSample($Sample = []) : void
46
    {
47
        $this->Sample = $Sample;
48
49
        $this->SampleCount = count($Sample);
50
51
        $this->null_counter = $this->nullCount();
52
53
        $this->DataType = $this->isNumeric();
54
    }
55
56
    public function nullCount() : int
57
    {
58
        $nullCount = 0;
59
60
        foreach ($this->Sample as $s) {
61
            if (is_null($s)) {
62
                $nullCount++;
63
            }
64
        }
65
66
        return $nullCount;
67
    }
68
69
    /**
70
     * @return string
71
     * Determine if $Sample data is 'Numeric' or 'Object' type.
72
     */
73
    public function isNumeric() : string
74
    {
75
        $DataType = 'Numeric';
76
77
        foreach ($this->Sample as $s) {
78
            if (!is_numeric($s)) {
79
                if (!is_null($s)) {
80
                    $DataType = 'Object';
81
                }
82
            }
83
        }
84
85
        return $DataType;
86
    }
87
88
    /**
89
     * @return string
90
     * Get DataType of Series.
91
     */
92
    public function dataType() : string
93
    {
94
        return $this->DataType;
95
    }
96
97
    /**
98
     * @param int $floatPoint
99
     * @return float
100
     * Compute mean value of $Sample.
101
     */
102
    public function mean($floatPoint = 2) : float
103
    {
104
        return round(array_sum($this->Sample) / ($this->SampleCount - $this->null_counter), $floatPoint);
105
    }
106
107
    /**
108
     * @param int $floatPoint
109
     * @return float
110
     * Determine max value in $Sample.
111
     */
112
    public function max($floatPoint = 2) : float
113
    {
114
        // Initialize max value with 1st index of $Sample.
115
        $max = $this->Sample[0];
116
117
        for ($i = 1; $i < $this->SampleCount; $i++) {
118
            if ($this->Sample[$i] > $max && !is_null($this->Sample[$i])) {
119
                $max = $this->Sample[$i];
120
            }
121
        }
122
123
        return round($max, $floatPoint);
124
    }
125
126
    /**
127
     * @param int $floatPoint
128
     * @return float
129
     * Determine min value in $Sample.
130
     */
131
    public function min($floatPoint = 2) : float
132
    {
133
        //Initialize min value with 1st index of $Sample.
134
        $min = $this->Sample[0];
135
136
        for ($i = 1; $i < $this->SampleCount; $i++) {
137
            if ($this->Sample[$i] < $min && !is_null($this->Sample[$i])) {
138
                $min = $this->Sample[$i];
139
            }
140
        }
141
142
        return round($min, $floatPoint);
143
    }
144
145
    /**
146
     * @param int $Q
147
     * @param int $floatPoint
148
     * @return float
149
     * Determine Q-th Quartile in $Sample.
150
     */
151
    public function quartile($Q = 1, $floatPoint = 2) : float
152
    {
153
        if ($this->dataType() == 'Object') {
154
            return 0;
155
        }
156
157
        //Sort Data into ascending order.
158
        $sorted_sample = $this->Sample;
159
160
        $sorted_sample = array_filter($sorted_sample);
161
162
        sort($sorted_sample);
163
164
        $quartile = ($Q / 4) * ((count($this->Sample) - $this->null_counter) + 1);
165
166
        return $sorted_sample[$quartile - 1];
167
    }
168
169
    /**
170
     * @param int $floatPoint
171
     * @return float
172
     * Determine 2nd Quartile in $sample.
173
     */
174
    public function median($floatPoint = 2) : float
175
    {
176
        return $this->quartile(2, $floatPoint);
177
    }
178
179
    /**
180
     * @param int $floatPoint
181
     * @return float
182
     * Determine Variance of $Sample.
183
     */
184
    public function variance($floatPoint = 4) : float
185
    {
186
        if ($this->dataType() == 'Object') {
187
            return 0;
188
        }
189
190
        $mean = $this->mean(4);
191
192
        $total_s = 0;
193
194
        foreach ($this->Sample as $s) {
195
            if (!is_null($s)) {
196
                $total_s += pow(($s - $mean), 2);
197
            }
198
        }
199
200
        $variance = $total_s / (count($this->Sample) - $this->null_counter);
201
202
        return round($variance, $floatPoint);
203
    }
204
205
    /**
206
     * @param int $floatPoint
207
     * @return float
208
     * Determine Standard Deviation of $Sample.
209
     */
210
    public function std($floatPoint = 2) : float
211
    {
212
        $std = sqrt($this->variance());
213
214
        return round($std, $floatPoint);
215
    }
216
217
    /**
218
     * @return array
219
     * Get $Sample value.
220
     */
221
    public function all() : array
222
    {
223
        return $this->Sample;
224
    }
225
226
    /**
227
     * @param int $from
228
     * @param int $to
229
     * @return array
230
     * Return all values in data within index $from $to.
231
     */
232
    public function withinIndexOf($from = 0, $to = 1) : array
233
    {
234
        // Initialize sample.
235
        $Sample = [];
236
237
        for ($i = $from; $i <= $to; $i++) {
238
            $Sample[] = $this->Sample[$i];
239
        }
240
241
        return $Sample;
242
    }
243
244
    public function get($index = 0)
245
    {
246
        return $this->Sample[$index];
247
    }
248
}
249