/
Index.php
302 lines (282 loc) · 10.1 KB
/
Index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
// http://mathworld.wolfram.com/IndexNumber.html
namespace Math\StatIndex;
/**
* Class Index
* @package Math\StatIndex
*/
/**
* Class Index
* @package Math\StatIndex
*/
class Index {
/**
* @var
*/
private $_currList;
/**
* @var
*/
private $_refList;
/**
* @var
*/
private $_indexes;
/**
* @var
*/
private $_itemNames;
/**
* @var bool
*/
private $_validData = false;
/**
* StatIndex constructor.
*
* @param \Math\StatIndex\ItemList $current
* @param \Math\StatIndex\ItemList $reference
*/
public function __construct(ItemList $current,
ItemList $reference) {
$this->setCurrentAndReferenceData($current, $reference);
}
/**
* @param \Math\StatIndex\ItemList $current
* @param \Math\StatIndex\ItemList $reference
*
* @return bool
* @throws \Math\StatIndex\StatIndexException
*/
public function setData(ItemList $current,
ItemList $reference) {
return $this->setCurrentAndReferenceData($current, $reference);
}
/**
* @param \Math\StatIndex\ItemList $current
* @param \Math\StatIndex\ItemList $reference
*
* @return bool
* @throws \Math\StatIndex\StatIndexException
*/
public function setCurrentAndReferenceData(ItemList $current,
ItemList $reference) {
// validate lists
$sizeCheck = ($current->size() === $reference->size());
$diff = array_diff($current->getItemsNames(), $reference->getItemsNames());
$namesCheck = empty($diff);
$this->_validData = $sizeCheck && $namesCheck;
$this->validData(); // raise an exception if something is amiss
$this->_indexes = array();
$this->_currList = $current;
$this->_itemNames = $current->getItemsNames();
$this->_refList = $reference;
return $this->_validData;
}
/**
* @throws \Math\StatIndex\StatIndexException
*/
private function validData() {
if (!$this->_validData) {
throw new StatIndexException('Inconsistent current and reference data, cannot compute index');
}
}
/**
* @return \Math\StatIndex\float
* @throws \DivisionByError
* @throws \Math\StatIndex\StatIndexException
*/
function mitchellIndex():float {
$this->validData();
// estimate the index if it was not calculated previously
if (!isset($this->_indexes['mitchell'])) {
$numeratorSum = 0;
$denominatorSum = 0;
foreach ($this->_itemNames as $name) {
$currItem = $this->_currList->getItem($name);
$refItem = $this->_refList->getItem($name);
$numeratorSum += $currItem->getValue() * $refItem->getImportance();
$denominatorSum += $refItem->getValue() * $refItem->getImportance();
}
if ($denominatorSum == 0) {
throw new \DivisionByError('Division by zero while estimating Mitchell\'s Index');
}
$this->_indexes['mitchell'] = ($numeratorSum / $denominatorSum);
}
return $this->_indexes['mitchell'];
}
/**
* @return \Math\StatIndex\float|mixed
* @throws \DivisionByError
* @throws \Math\StatIndex\StatIndexException
*/
public function paascheIndex():float {
$this->validData();
if (!isset($this->_indexes['paasche'])) {
$numeratorSum = 0;
$denominatorSum = 0;
foreach ($this->_itemNames as $name) {
$currItem = $this->_currList->getItem($name);
$refItem = $this->_refList->getItem($name);
$numeratorSum += $currItem->getValue() * $currItem->getQuantity();
$denominatorSum += $refItem->getValue() * $currItem->getQuantity();
}
if ($denominatorSum == 0) {
throw new \DivisionByError('Division by zero while estimating Paasche\'s Index');
}
$this->_indexes['paasche'] = ($numeratorSum / $denominatorSum);
}
return $this->_indexes['paasche'];
}
/**
* @return \Math\StatIndex\float|mixed
* @throws \DivisionByError
*/
function laspeyresIndex():float {
$this->validData();
if (!isset($this->_indexes['laspeyres'])) {
$numeratorSum = 0;
$denominatorSum = 0;
foreach ($this->_itemNames as $name) {
$currItem = $this->_currList->getItem($name);
$refItem = $this->_refList->getItem($name);
$numeratorSum += $currItem->getValue() * $refItem->getQuantity();
$denominatorSum += $refItem->getValue() * $refItem->getQuantity();
}
if ($denominatorSum == 0) {
throw new \DivisionByError('Division by zero while estimating Laspeyres\' Index');
}
$this->_indexes['laspeyres'] = ($numeratorSum / $denominatorSum);
}
return $this->_indexes['laspeyres'];
}
/**
* @return mixed
*/
function bowleyIndex():float {
$this->validData();
if (!isset($this->_indexes['bowley'])) {
$this->_indexes['bowley'] = 0.5 * ($this->paascheIndex() + $this->laspeyresIndex());
}
return $this->_indexes['bowley'];
}
/**
* @return mixed
*/
function fisherIndex():float {
$this->validData();
if (!isset($this->_indexes['fisher'])) {
$this->_indexes['fisher'] = sqrt($this->paascheIndex() * $this->laspeyresIndex());
}
return $this->_indexes['fisher'];
}
/**
* @return mixed
* @throws \DivisionByError
*/
function marshallEdgeworthIndex() {
$this->validData();
if (!isset($this->_indexes['marshall-edgeworth'])) {
$numeratorSum = 0;
$denominatorSum = 0;
foreach ($this->_itemNames as $name) {
$currItem = $this->_currList->getItem($name);
$refItem = $this->_refList->getItem($name);
$numeratorSum += $currItem->getValue() * ($currItem->getQuantity() + $refItem->getQuantity());
$denominatorSum += ($refItem->getValue() * $refItem->getQuantity()) +
($currItem->getValue() * $currItem->getQuantity());
}
if ($denominatorSum == 0) {
throw new \DivisionByError('Division by zero while estimating Marshall-Edgeworth\'s Index');
}
$this->_indexes['marshall-edgeworth'] = ($numeratorSum / $denominatorSum);
}
return $this->_indexes['marshall-edgeworth'];
}
/**
* @return mixed
* @throws \DivisionByError
*/
function walshIndex() {
$this->validData();
if (!isset($this->_indexes['walsh'])) {
$numeratorSum = 0;
$denominatorSum = 0;
foreach ($this->_itemNames as $name) {
$currItem = $this->_currList->getItem($name);
$refItem = $this->_refList->getItem($name);
$factor = sqrt($currItem->getQuantity() * $refItem->getQuantity());
$numeratorSum += $currItem->getValue() * $factor;
$denominatorSum += $refItem->getValue() * $factor;
}
if ($denominatorSum == 0) {
throw new \DivisionByError('Division by zero while estimating Walsh\'s Index');
}
$this->_indexes['walsh'] = ($numeratorSum / $denominatorSum);
}
return $this->_indexes['walsh'];
}
/**
* @return mixed
* @throws \DivisionByError
*/
function harmonicMeanIndex() {
$this->validData();
if (!isset($this->_indexes['harmonic-mean'])) {
$numeratorSum = 0;
$denominatorSum = 0;
foreach ($this->_itemNames as $name) {
$currItem = $this->_currList->getItem($name);
$refItem = $this->_refList->getItem($name);
$refValue = $refItem->getValue() * $refItem->getQuantity();
$numeratorSum += $refValue;
$denominatorSum += ($refItem->getValue() / $currItem->getValue()) * $refValue;
}
if ($denominatorSum == 0) {
throw new \DivisionByError('Division by zero while estimating the Harmonic Mean Index');
}
$this->_indexes['harmonic-mean'] = ($numeratorSum / $denominatorSum);
}
return $this->_indexes['harmonic-mean'];
}
/**
* @return mixed
*/
function geometricMeanIndex() {
$this->validData();
if (!isset($this->_indexes['geometric-mean'])) {
$product = 1;
$inverseExponent = 0;
foreach ($this->_itemNames as $name) {
$currItem = $this->_currList->getItem($name);
$refItem = $this->_refList->getItem($name);
$refTotal = $refItem->getValue() * $refItem->getQuantity();
$product *= pow(($currItem->getValue() / $refItem->getValue()), $refTotal);
if (is_infinite($product)) {
throw new \OverflowException('Math overflow when computing geometric mean index');
}
$inverseExponent += $refTotal;
}
$this->_indexes['geometric-mean'] = pow($product, 1 / $inverseExponent);
}
return $this->_indexes['geometric-mean'];
}
/**
* Convenience method to calculate all the indexes from the current and reference data
*
* @return array An associative array with all the indexes
* @throws StatIndexException if the the data is inconsistent
*/
function allIndexes() {
// calculate all indexes
$this->mitchellIndex();
$this->paascheIndex();
$this->laspeyresIndex();
$this->bowleyIndex();
$this->fisherIndex();
$this->marshallEdgeworthIndex();
$this->walshIndex();
$this->harmonicMeanIndex();
$this->geometricMeanIndex();
return $this->_indexes;
}
}