-
Notifications
You must be signed in to change notification settings - Fork 2
/
regression.php
378 lines (328 loc) · 10.3 KB
/
regression.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
if (!class_exists('zc_math')) {
require_once('math.php');
}
/**
* Linear class
*
* The data format is array in array.
* Array containing number of coordinates and the coordinates contain an array of x and y.
*
* Example usage
* <code>
* $data = array(array($x1, $y1), array($x2, $y2));
* $linear = new zc_math_linear($data);
* $linear->calculate();
* // y = k*x + m
* echo $linear->k;
* echo $linear->m;
* </code>
*
* Linear regression formula found at {@link http://en.wikipedia.org/wiki/Numerical_methods_for_linear_least_squares#Computation }
*
* @author Han Lin Yap < http://zencodez.net/ >
* @copyright 2010 zencodez.net
* @license http://creativecommons.org/licenses/by-sa/3.0/
* @package math
* @version 1.0 - 2010-07-29
*/
class zc_math_linear {
/**
* Contains the coordinates
* @var array
*/
public $data;
// needed for linear regression formula
public $sum_x;
public $sum_x2;
public $sum_y;
public $sum_xy;
public $number_of_coords;
// y = k*x + m
/**
* @var float
*/
public $k;
/**
* @var float
*/
public $m;
/**
* @param array $data
*/
function __construct($data) {
$this->data = $data;
}
/**
* Calculate and make formula
* To understand better, {@link http://en.wikipedia.org/wiki/Numerical_methods_for_linear_least_squares#Computation see formula}
* @since 1.0
*/
public function calculate() {
$this->number_of_coords = count($this->data);
$this->sum_x = $this->sum_by_formula($this->data, '$x[0]');
$this->sum_x2 = $this->sum_by_formula($this->data, 'pow($x[0],2)');
$this->sum_y = $this->sum_by_formula($this->data, '$x[1]');
$this->sum_xy = $this->sum_by_formula($this->data, '$x[0] * $x[1]');
$this->k = ($this->number_of_coords * $this->sum_xy - $this->sum_y * $this->sum_x) / ($this->number_of_coords * $this->sum_x2 - pow($this->sum_x, 2));
$this->m = ($this->sum_y * $this->sum_x2 - $this->sum_x * $this->sum_xy) / ($this->number_of_coords * $this->sum_x2 - pow($this->sum_x, 2));
}
/**
* Gets all by a formula
* Turn a multidimention array to a ordinary array with values
* @param array $data Multidimention array
* @param string $formula How to get what
* @return array Get values in multidimention by formula
* @since 1.0
*/
protected function _get_values_by_formula($data, $formula) {
return array_map(create_function('$x', 'return ' . $formula . ';'), $data);
}
/**
* Sum all by a formula
* @param array $data Data to sum
* @param string $formula Formula to sum
* @return float sum of $data by formula
* @since 1.0
*/
public function sum_by_formula($data, $formula) {
return array_sum($this->_get_values_by_formula($data, $formula));
}
}
/**
* Polynomial regression
*
* Example usage
* <code>
* $polynomial = new zc_math_polynomial($data);
* echo print_r($polynomial->calculate_coefficients());
* echo "<img src='" . $polynomial->render_formula() . "' />";
* echo "<a href='" . $polynomial->render_formula('wolframalpha') . "'>" . $polynomial->render_formula('') . "</a>";
* echo $polynomial->get_y(1200);
* </code>
* {@link http://www.trentfguidry.net/post/2009/06/30/Matrix-Crout-LU-decomposition.aspx }
* {@link http://www.trentfguidry.net/post/2009/07/19/Linear-multiple-regression.aspx }
* {@link http://www.trentfguidry.net/post/2009/08/01/Linear-regression-polynomial-coefficients.aspx }
* @author Han Lin Yap < http://zencodez.net/ >
* @copyright 2010 zencodez.net
* @license http://creativecommons.org/licenses/by-sa/3.0/
* @package math
* @version 1.0 - 2010-07-30
*/
class zc_math_polynomial extends zc_math_linear {
/**
* The calculated formula in array
* @var array
*/
public $result;
/**
* Order to calculate
* @var integer
*/
public $order;
/**
* Initialize
* @param array $data Format array(array($x1, $y2), array($x2, $y2))
* @param integer $order optional What order the formula should be
* @since 1.0
*/
function __construct($data, $order=4) {
$this->data = $data;
$this->set_order($order);
}
/**
* Change order
* @param integer $order What order the formula should be
* @since 1.0
*/
public function set_order($order) {
$this->order = $order;
}
/**
* Find the formula
* @param integer $order optional What order the formula should be
* @return array The found formula in array
* @since 1.0
*/
public function calculate_coefficients($order=false) {
if ($order)
$this->set_order($order);
$x = $this->_get_values_by_formula($this->data, '$x[0]');
$y = $this->_get_values_by_formula($this->data, '$x[1]');
$z = array_fill(0, count($y), array());
for ($i = 0 ; $i < count($y) ; $i++) {
for ($j = 0 ; $j <= $this->order ; $j++) {
$z[$i][$j] = pow($x[$i],$j);
}
}
$this->result = $this->_polynomial_regress($z, $y);
return $this->result;
}
/**
* Render formula for wolframalpha, google chart, url or plain math text
*
* NOTE! {@link zc_math_polynomial::calculate_coefficients()} before calling this method
* Example usage
* <code>
* render_formula()
* render_formula('wolframalpha')
* render_formula('')
* render_formula('text');
* </code>
*
* @param string $type optional Available types wolframalpha, text, google or ''
* @param string|boolean $replace_x optional If you want to customize x output
* @return string URL or text, depends on $type
* @uses zc_math_polynomial::calculate_coefficients() Call it before to have something to render
* @since 1.0
*/
public function render_formula($type='google', $replace_x=false) {
$formula = array();
$text = '';
if ($type=='text')
$text = '*';
if (!$replace_x)
$x = 'x';
else
$x = $replace_x;
foreach($this->result AS $k => $v) {
if ($k == 0)
$formula[] = $v[0];
elseif ($k == 1)
$formula[] = $v[0] . $text . $x;
elseif ($type!='google')
$formula[] = $v[0] . $text . $x . "^". $k;
else
$formula[] = $v[0] . "x^{". $k . "}";
}
if ($type=='wolframalpha')
return 'http://www.wolframalpha.com/input/?i=' . implode("+%2b+", $formula);
elseif ($type=='text')
return implode("%2b", $formula);
elseif ($type!='google')
return 'y = ' . implode(" + ", $formula);
else
return 'http://chart.apis.google.com/chart?cht=tx&chl=y+=+' . implode("%2b", $formula);
}
/**
* Get y value from x coordinate
*
* NOTE! {@link zc_math_polynomial::calculate_coefficients()} before calling this method
*
* @param integer $x X-coordinate
* @return integer Y-coordinate
* @uses zc_math_polynomial::calculate_coefficients() Call it before to have something to check on
* @since 1.0
*/
public function get_y($x) {
$sum = 0;
foreach($this->result AS $k => $v) {
if ($k == 0)
$sum += $v[0];
else
$sum += $v[0] * pow($x,$k);
}
return $sum;
}
private function _polynomial_regress($z, $y) {
$z_transpose = zc_math::matrix_transpose($z);
$l = zc_math::matrix_multiply($z_transpose, $z);
$r = zc_math::matrix_multiply($z_transpose, zc_math::matrix_transpose(array($y)));
return $this->_solve_for($l, $r);
}
private function _solve_for($l, $r) {
$resultMatrix = array_fill(0, count($l[0]), array());
$resDecomp = $this->_lu_decompose($l);
$nP = $resDecomp['PivotArray'];
$lMatrix = $resDecomp['L'];
$uMatrix = $resDecomp['U'];
for ($k = 0 ; $k < count($r[0]) ; $k++) {
$sum = 0.0;
$dMatrix = array_fill(0, count($l), array());
$dMatrix[0][0] = $r[$nP[0]][$k] / $lMatrix[0][0];
for ($i = 1; $i < count($l); $i++) {
$sum = 0.0;
for ($j = 0; $j < $i; $j++) {
$sum += $lMatrix[$i][$j] * $dMatrix[$j][0];
}
$dMatrix[$i][0] = ($r[$nP[$i]][$k] - $sum) / $lMatrix[$i][$i];
}
$resultMatrix[count($l) - 1][$k] = $dMatrix[count($l) - 1][0];
for ($i = count($l) - 2; $i >= 0; $i--) {
$sum = 0.0;
for ($j = $i + 1; $j < count($l); $j++) {
$sum += $uMatrix[$i][$j] * $resultMatrix[$j][$k];
}
$resultMatrix[$i][$k] = $dMatrix[$i][0] - $sum;
}
}
return $resultMatrix;
}
private function _lu_decompose($l) {
if (!defined('PHP_INT_MIN')) {
define('PHP_INT_MIN', ~PHP_INT_MAX);
}
$_rowCount = count($l);
$_columnCount = count($l[0]);
$uMatrix = array_fill(0, $_rowCount, array());
$lMatrix = array_fill(0, $_rowCount, array());
$workingUMatrix = $l;
$workingLMatrix = array_fill(0, $_rowCount, array());
$pivotArray = range(0, $_rowCount-1);
for ($i = 0; $i < $_rowCount; $i++) {
$maxRowRatio = PHP_INT_MIN;
$maxRow = -1;
$maxPosition = -1;
for ($j = $i; $j < $_rowCount; $j++) {
$rowSum = 0.0;
for ($k = $i; $k < $_columnCount; $k++) {
$rowSum += abs($workingUMatrix[$pivotArray[$j]][$k]);
}
$dCurrentRatio = abs($workingUMatrix[$pivotArray[$j]][$i]) / $rowSum;
if ($dCurrentRatio > $maxRowRatio) {
$maxRowRatio = abs($workingUMatrix[$pivotArray[$j]][$i] / $rowSum);
$maxRow = $pivotArray[$j];
$maxPosition = $j;
}
}
if ($maxRow != $pivotArray[$i]) {
$hold = $pivotArray[$i];
$pivotArray[$i] = $maxRow;
$pivotArray[$maxPosition] = $hold;
}
$rowFirstElementValue = $workingUMatrix[$pivotArray[$i]][$i];
for ($j = 0; $j < $_columnCount; $j++) {
if ($j < $i) {
$workingUMatrix[$pivotArray[$i]][$j] = 0.0;
} elseif ($j == $i) {
$workingLMatrix[$pivotArray[$i]][$j] = $rowFirstElementValue;
$workingUMatrix[$pivotArray[$i]][$j] = 1.0;
} else {
$workingUMatrix[$pivotArray[$i]][$j] /= $rowFirstElementValue;
$workingLMatrix[$pivotArray[$i]][$j] = 0.0;
}
}
for ($k = $i + 1; $k < $_rowCount; $k++) {
$rowFirstElementValue = $workingUMatrix[$pivotArray[$k]][$i];
for ($j = 0; $j < $_rowCount; $j++) {
if ($j < $i) {
$workingUMatrix[$pivotArray[$k]][$j] = 0.0;
} elseif ($j == $i) {
$workingLMatrix[$pivotArray[$k]][$j] = $rowFirstElementValue;
$workingUMatrix[$pivotArray[$k]][$j] = 0.0;
} else {
$workingUMatrix[$pivotArray[$k]][$j] = $workingUMatrix[$pivotArray[$k]][$j] - $rowFirstElementValue * $workingUMatrix[$pivotArray[$i]][$j];
}
}
}
}
for ($i = 0; $i < $_rowCount; $i++) {
for ($j = 0; $j < $_rowCount; $j++) {
$uMatrix[$i][$j] = $workingUMatrix[$pivotArray[$i]][$j];
$lMatrix[$i][$j] = $workingLMatrix[$pivotArray[$i]][$j];
}
}
return array('U' => $uMatrix, 'L' => $lMatrix, 'PivotArray' => $pivotArray);
}
}
?>