forked from tperamun2/Phillips-Hue-Lighting-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Predict.cpp
278 lines (222 loc) · 8.45 KB
/
Predict.cpp
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
/**
* @brief The Predict class is used to train a logistic regression algorithm using a specific lights state data to predict future scheduling state events.
* @brief If the algorithm does not perform at a high enough level of certainty, than the Predict class will opt to use a most-frequent algorithm to decide scheduling.
* @author Gurkiran Tatla
* @author Jake Schindler
* @author Justine Kim
* @author Paul Salvatore
* @author Timal Peramune
*/
#include "Predict.hpp"
/**
* Predict constructor.
* There are no parameters/variables to initalize for this class so the constructor does nothing.
*/
Predict::Predict(){
return;
}
/**
* Predict deconstructor.
* There are no class variables to destroy for this class so the deconstructor does nothing.
*/
Predict::~Predict(){
return;
}
/**
* Maps the features X1 and X2 polynomially for more complex fitting.
*
* @param X1: m x 1 vector where m is number of training examples (first feature column)
* @param X2: m x 1 vector where m is number of training examples (second feature column)
* @return a vector of size m x 21 representing polynomial mapped features X1 and X2
*/
arma::mat Predict::mapFeature(arma::mat X1, arma::mat X2){
arma::mat out(size(X1, 0), MAPPED_NUM);
int column = 0;
for(int i = 0; i <= DEGREE; i++){
for(int j = 0; j <= i; j++){
out.col(column++) = ( pow(X1, i-j) % pow(X2, j) );
}
}
return out;
}
/**
* Computes the sigmoid value of each training example in X parameterized with theta
*
* @param theta: (n + 1) x 1 vector
* @param X: m x (n+1) vector where m is number of training examples and n is the number of features
* @param m equivallent to the number of training examples in X
* @return a vector of size m x 1 representing the sigmoid function applied to each value of z
*/
arma::mat Predict::sigmoid(arma::mat *theta, arma::mat *X, unsigned long m){
arma::mat z = *theta * *X;
arma::mat s(m, 1);
for(int i = 0; i < m; i++){
s(i, 0) = ( 1.0/(1.0 + exp(-1.0 * z(i, 0))) );
}
return s;
}
/**
* Makes a prediction based on the sigmoid values for a weeks worth of x points.
*
* @param theta: (n + 1) x 1 vector
* @param X: m x (n+1) vector where m is number of predictions to make and n is the number of features
* @param threshold is a double representing the threshold from which to consider the output positive
* @return the prediction for each of the values in X parameterized with theta: m x 1 vector
*/
arma::mat Predict::predict(arma::mat *theta, arma::mat *X){
unsigned long m = arma::size(*X, 0);
arma::mat p = arma::zeros(m, 1);
arma::mat sigmoids = sigmoid(X, theta, m);
for(int i = 0; i < m; i++){
if( sigmoids(i, 0) >= THRESHOLD ){
p(i, 0) = 1;
}
}
return p;
}
/**
* Computes the theta values for the matrix using the normal equation.
*
* @param X: m x (n+1) matrix, where m is number of training examples and n is the number of features
* @param y: m x 1 vector
* @param lambda the regularization parameter
* @return the minimized values of theta: (n+1) x 1 vector
*/
arma::mat Predict::normalEquation(arma::mat *X, arma::mat *y){
arma::mat X_transpose = X->t();
unsigned long n = arma::size(*X, 1);
arma::mat normalMatrix = arma::eye<arma::mat>(n, n);
normalMatrix(0, 0) = 0;
return ( arma::pinv(X_transpose * *X + LAMBDA * normalMatrix) * X_transpose * *y );
}
/**
* Builds and returns a matrix representing the state information for a particular light.
*
* @param lightId: the id of the light for which we are getting all state information for
* @return an matrix representation where the rows represent each data point, col1: day, col2: hour, col3: state (1 = on, 0 = off)
*/
arma::mat Predict::generateLightMatrix(int lightId){
arma::mat data;
data.load("./lightData" + std::to_string(lightId) + ".txt");
return data;
}
/**
* Builds a matrix of x parameters to be used for predicting the next week of state shceduling.
*
* @return a matrix representing a week of x values for oru scheduling prediction algorithm
*/
arma::mat Predict::generateWeekOfX(){
arma::mat x_predict(24*7, 2);
int pos = 0;
for(int day = 1; day < 8; day++){
for(int hour = 0; hour < 24; hour++) {
x_predict(pos, 0) = day;
x_predict(pos, 1) = hour;
pos++;
}
}
return mapFeature(x_predict.col(0), x_predict.col(1));
}
/*
* Predicts a weeks worth of scheduling based ont he most frequent past states for each time.
* To be used when the logisitic regression algorithm is not confident enough.
*
* @param data: the matrix representation of the state data for the light which we want to predict
* @return a matrix representing a week of scheduling
*/
arma::mat Predict::predictByFrequency(arma::mat *data){
arma::mat positive = arma::zeros(24 * 7, 1);
arma::mat negative = arma::zeros(24 * 7, 1);
arma::mat prediction = arma::zeros(24 * 7, 1);
for(int i = 0; i < arma::size(*data, 0); i++){
if((*data).at(i, 2) == 1){
positive.at((((*data).at(i, 0)) - 1) * 24 + (*data).at(i, 1), 0) += 1;
} else {
negative.at((((*data).at(i, 0)) - 1) * 24 + (*data).at(i, 1), 0) += 1;
}
}
for(int i = 0; i < 24 * 7; i++){
if(positive.at(i, 0) > negative.at(i, 0)){
prediction.at(i, 0) = 1;
}
}
return prediction;
}
/**
* Decides which prediction algorithm to use and exeutes the prediction.
*
* @param lightId the id of a light to collect all data for, train theta, and predict a weeks worth of scheduling
* @return a matrix representing a week of scheduling
*/
arma::mat Predict::trainAndPredict(int lightId, int machineLearning){
// Data does not need to be randomized since we are using all of it
arma::mat data = generateLightMatrix(lightId);
// We predict using logistic regression dependant on our confidence in our predictions
if(machineLearning){
arma::mat x_predict = generateWeekOfX();
// train and predict and then schedule
arma::mat y = data.col(2);
arma::mat X = mapFeature(data.col(0), data.col(1));
arma::mat theta = normalEquation(&X, &y);
return predict(&theta, &x_predict);
} else {
// predict based on most frequent for that day
return predictByFrequency(&data);
}
}
/**
* Computes the F score for the test data to determine accuracy.
*
* @param the id of the light that we are checking the confidence for
* @return an integer 1 or 0, respectively representing if our f score surpasses our confidence threshold
*/
double Predict::fscore(arma::mat *y_predicted, arma::mat *y_test){
double true_postive = 0;
double pred_positive = 0;
double actual_positive = 0;
// Counting the number of true, predicted, and actual positive values
for(int i = 0; i < arma::size((*y_predicted), 0); i++){
if((*y_predicted).at(i, 0) == 1){
if((*y_test).at(i, 0) == 1){
true_postive++;
actual_positive++;
}
pred_positive++;
} else if ((*y_test).at(i, 0) == 1){
actual_positive++;
}
}
// Returning lowest possible f score to avoid dividing by 0 error
if(actual_positive == 0 || true_postive == 0){
return 0;
}
double recall = true_postive / actual_positive;
double precision = true_postive / pred_positive;
// F score calculation
return 2 * ( (precision * recall) / (precision + recall) );
}
/**
* Splits a lights data into a training and test set to compute the accuracy of our
* training algorithm with the available data.
*
* @param the id of the light that we are checking the confidence for
* @return an integer 1 or 0, respectively representing if our f score surpasses our confidence threshold
*/
int Predict::checkConfidence(int lightId){
arma::mat data = generateLightMatrix(lightId);
// Randomizing the data for training
data = arma::shuffle(data, 1);
// Dividing into train (70%) and test (30%) sets
int rowSize = arma::size(data, 0);
arma::mat y_train = data.col(2).rows(0, std::floor(rowSize * 0.7));
arma::mat y_test = data.col(2).rows(std::floor(rowSize * 0.7) + 1, rowSize - 1);
arma::mat X = mapFeature(data.col(0), data.col(1));
arma::mat X_train = X.rows(0, std::floor(rowSize * 0.7));
arma::mat X_test = X.rows(std::floor(rowSize * 0.7) + 1, rowSize - 1);
// Training theta with the training set
arma::mat theta = normalEquation(&X_train, &y_train);
// Predicting values for the test set
arma::mat predicted = predict(&theta, &X_test);
// Checking how confidence we are
return fscore(&predicted, &y_test) > CERTAINTY_THRESHOLD;
}