-
Notifications
You must be signed in to change notification settings - Fork 20
/
multivagraddec.php
271 lines (231 loc) · 5.88 KB
/
multivagraddec.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
<?php
class MVGradient {
protected $data;
protected $learning_rate = 0.1;
/**
* Set the data for the function.
* @param array - 0 => (x1, x2, x3, x4), 1 => y
*/
public function set_data($data) {
$this->data = $this->scale_data($data);
}
/**
* Set the rate at which the algorithm updates.
* Normal values are 0.1 - 0.001
*
* @param float $rate
* @return void
*/
public function set_learning_rate($rate) {
$this->learning_rate = $rate;
}
/**
* Normalise variance and scale data to:
* xi - avg(xi) / range(max-min)
* so we get in a -0.5 - 0.5 range with an
* avg of 0
* - this is a bit of clunky method!
*/
protected function scale_data($data) {
$minmax = array();
$rows = count($data);
foreach($data as $key => $row) {
foreach($row[0] as $id => $val) {
/* Initialise Arrays */
if(!isset($minmax[$id])) {
$minmax[$id] = array();
$minmax[$id]['min'] = false;
$minmax[$id]['max'] = false;
$minmax[$id]['total'] = 0;
}
/* Get stats */
if( $minmax[$id]['min'] == false ||
$minmax[$id]['min'] > $val) {
$minmax[$id]['min'] = $val;
}
if( $minmax[$id]['max'] == false ||
$minmax[$id]['max'] < $val) {
$minmax[$id]['max'] = $val;
}
$minmax[$id]['total'] += $val;
}
}
/* Compute average and variance */
foreach($minmax as $id => $row) {
$minmax[$id]['var'] = $row['max'] - $row['min'];
$minmax[$id]['avg'] = $row['total'] / $rows;
}
foreach($data as $key => $row) {
foreach($row[0] as $id => $val) {
$data[$key][0][$id] = ( $val - $minmax[$id]['avg'] )
/ $minmax[$id]['var'];
}
}
return $data;
}
/**
* Update the parameters, including using a dummy row value
* of 1 for the first parameter.
*
* @param array $params
* @return array
*/
protected function learn($params) {
$data_rate = 1/count($this->data);
foreach($params as $id => $p) {
foreach($this->data as $row) {
$score = $this->mv_hypothesis($row[0], $params) - $row[1];
// Update parameters
$params[$id] -= $this->learning_rate *
($data_rate *
( $score * ($id == 0 ? 1 : $row[0][$id-1]) )
);
}
}
return $params;
}
/**
* Generate a score based on the data and passed parameters
*
* @param array $params
* @return int
*/
protected function mv_hypothesis($rowdata, $params) {
$score = $params[0];
foreach($rowdata as $id => $value) {
$score += $value * $params[$id+1];
}
return $score;
}
/**
* Return the sum of squared error score
*
* @param array $params
* @return int
*/
public function score($params) {
$score = 0;
foreach($this->data as $row) {
$score += pow($this->mv_hypothesis($row[0], $params) - $row[1], 2);
}
return $score;
}
/**
* Update parameters
*
* @param string $data
* @param string $parameters
* @return array parameters
*/
function mv_gradient($parameters) {
$score = $this->score($parameters);
// Create a new hypothesis to test our score
$parameters = $this->learn($parameters);
if($score < $this->score($parameters)) {
return false;
}
return $parameters;
}
/**
* Find the parameters that best fit the data
*
* @param int $iterations - max iterations to run
* @param array $defaults - optional starting params
* @return array - best fit parameters
*/
public function find_params($iterations = 5000, $defaults = null) {
if(!$defaults) {
$defaults = array_fill(0, count($this->data[0][0]) + 1, 0);
}
$parameters = $defaults;
$iters = 0;
do {
$last_parameters = $parameters;
$parameters = $this->mv_gradient($parameters);
} while($parameters != false && $iters++ < $iterations);
return $parameters ? $parameters : $last_parameters;
}
}
/* Nice regular data for testing */
$data = array(
array(array(2, 4000, 0.5), 2+2+(2*4)+(3*5)),
array(array(2, 4000, 0.4), 2+2+(2*4)+(3*4)),
array(array(2, 4000, 0.6), 2+2+(2*4)+(3*6)),
array(array(1, 5000, 0.5), 2+1+(2*5)+(3*5)),
array(array(2, 5000, 0.1), 2+2+(2*5)+(3*1)),
);
class PolyMV extends MVGradient {
/**
* Skip scaling just for the example
*/
protected function scale_data($data) {
return $data;
}
/**
* Generate a score based on the data and passed parameters
*
* @param array $params
* @return int
*/
protected function mv_hypothesis($rowdata, $params) {
$score = $params[0];
foreach($rowdata as $id => $value) {
$score += pow($value, $id+2) * $params[$id+1];
}
return $score;
}
/**
* Update the parameters, including using a dummy row value
* of 1 for the first parameter.
*
* @param array $params
* @return array
*/
protected function learn($params) {
$data_rate = 1/count($this->data);
foreach($params as $id => $p) {
foreach($this->data as $row) {
$score = $this->mv_hypothesis($row[0], $params) - $row[1];
// Update parameters
// We have to multiply by an appropriate power as part of the
// partial derivative
$params[$id] -= $this->learning_rate *
($data_rate *
( $score * ($id == 0 ? 1 : pow($row[0][$id-1], $id+1)) )
);
}
}
return $params;
}
}
/*
$iterations = array(10, 100, 500, 1000, 2000, 5000, 10000);
$mvg = new MVGradient();
$mvg->set_data($data);
foreach(array(0.1, 0.01, 0.001, 0.001) as $rate) {
$mvg->set_learning_rate($rate);
foreach($iterations as $i) {
$params = $mvg->find_params($i);
echo $mvg->score($params), "\n";
}
echo "\n";
}
die();
// We have a polynomial example here
$data = array(
array(array(2, 2), 1+(3*pow(2, 2))+(2*pow(2, 3))),
array(array(3, 3), 1+(3*pow(3, 2))+(2*pow(3, 3))),
array(array(4, 4), 1+(3*pow(4, 2))+(2*pow(4, 3))),
array(array(5, 5), 1+(3*pow(5, 2))+(2*pow(5, 3))),
);
$iterations = array(10000);
$mvg = new PolyMV();
$mvg->set_data($data);
$mvg->set_learning_rate(0.001);
foreach($iterations as $i) {
$params = $mvg->find_params($i);
echo $mvg->score($params), "\n";
var_dump($params);
}
echo "\n";
*/