Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
335 lines (243 loc) · 10.7 KB
/
Copy pathmain.cpp
File metadata and controls
335 lines (243 loc) · 10.7 KB
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
#include <iostream>
#include <Eigen/Dense>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
double computeMean(const Eigen::VectorXd& data) {
return data.sum() / data.size();
}
double computeVariance(const Eigen::VectorXd& data) {
double mean = computeMean(data);
double sumSquaredDiffs = 0.0;
for (int i = 0; i < data.size(); ++i) {
double diff = data(i) - mean;
sumSquaredDiffs += diff * diff;
}
return sumSquaredDiffs / data.size();
}
double computeStdDev(const Eigen::VectorXd& data) {
return std::sqrt(computeVariance(data));
}
void demoMatrixOps() {
Eigen::Matrix2d M;
M << 4, 7,
2, 6;
std::cout << "\nOriginal Matrix:: \n" << M << "\n";
std::cout << " Transpose of M:: \n" << M.transpose() << "\n";
std::cout << " Inverse of M:: \n" << M.inverse() << "\n";
std::cout << "M * M_inverse (should be identity):\n" << M * M.inverse() << "\n";
std::cout << "Ambatukam!!!!!!!!!!!" << "\n";
}
void linearRegressionDemo() {
Eigen::MatrixXd X(5, 2);
Eigen::VectorXd y(5);
X << 1, 1,
1, 2,
1, 3,
1, 4,
1, 5;
y << 2, 4, 5, 4, 5;
Eigen::MatrixXd Xt = X.transpose();
Eigen::MatrixXd beta = (Xt * X).inverse() * Xt * y;
std::cout << "\nLinear Regression Results:\n";
std::cout << "Intercept (b0): " << beta(0) << "\n";
std::cout << "Slope (b1): " << beta(1) << "\n";
std::cout << "Predicted y for x=6: " << beta(0) + beta(1) * 6 << "\n";
}
Eigen::MatrixXd loadCSV(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "ERROR: Could not open file: " << filename << "\n";
return Eigen::MatrixXd();
}
std::string line;
std::vector<std::vector<double>> rows;
std::getline(file, line);
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string cell;
std::vector<double> row;
while(std::getline(ss, cell, ',')) {
row.push_back(std::stod(cell));
}
rows.push_back(row);
}
Eigen::MatrixXd mat(rows.size(), rows[0].size());
for (size_t i = 0; i < rows.size(); ++i) {
for (size_t j = 0; j < rows[i].size(); ++j) {
mat(i, j) = rows[i][j];
}
}
return mat;
}
void writeResultsCSV(const std::string& fileName, const Eigen::MatrixXd& data, const std::vector<std::string> headers) {
std::ofstream file(fileName);
if (!file.is_open()) {
std::cerr << "ERROR: Could not write file to: " << fileName << "\n" ;
return;
}
for (size_t i = 0; i < headers.size(); ++i) {
file << headers[i];
if (i < headers.size() - 1) file << ",";
}
file << "\n";
for (int i = 0; i < data.rows(); ++i) {
for (int j = 0; j < data.cols(); ++j) {
file << data(i, j);
if (j < data.cols() - 1) file << ",";
}
file << "\n";
}
std::cout << "Results written to: " << fileName << "\n";
}
Eigen::VectorXd sigmoid(const Eigen::VectorXd& z) {
Eigen::ArrayXd result = 1.0 / (1.0 + (-z.array()).exp());
return result.matrix();
}
Eigen::VectorXd trainLogisticRegression(const Eigen::MatrixXd& X, const Eigen::VectorXd& y, int epochs = 1000, double learningRate = 0.0001) {
Eigen::VectorXd weights = Eigen::VectorXd::Zero(X.cols());
for (int epoch = 0; epoch < epochs; ++epoch) {
Eigen::VectorXd predictions = sigmoid(X * weights);
Eigen::VectorXd errors = y - predictions;
Eigen::VectorXd gradient = X.transpose() * errors;
weights += learningRate * gradient;
if (epoch % 1000 == 0) {
double loss = -(y.array() * predictions.array().log() + (1 - y.array()) * (1 - predictions.array()).log()).mean();
std::cout << "Epoch " << epoch << " Loss: " << loss << "\n";
}
}
return weights;
}
void evaluateClassifier(const Eigen::MatrixXd& X, const Eigen::VectorXd& y, const Eigen::VectorXd& weights) {
Eigen::VectorXd predictions = sigmoid(X * weights);
int correct = 0;
std::cout << "\nPredictions vs Actual\n" ;
std::cout << "Index | Actual | Predicted | Probability\n" ;
std::cout << "------+--------+-----------+------------\n";
for (int i = 0; i < predictions.size(); ++i) {
int predicted = predictions(i) >= 0.5 ? 1 : 0;
if (predicted == (int)y(i)) correct++;
std::cout << " " << i << " | " << (int)y(i) << " | " << predicted << " | " << predictions(i) << "\n";
}
double accuracy = (double)correct / y.size() * 100.0;
std::cout << "Accuracy: " << accuracy << "\n";
}
Eigen::MatrixXd computeCovarianceMatrix(const Eigen::MatrixXd& data) {
Eigen::VectorXd mean = data.colwise().mean();
Eigen::MatrixXd centered = data.rowwise() - mean.transpose();
Eigen::MatrixXd cov = (centered.transpose() * centered) / (data.rows() - 1);
return cov;
}
Eigen::VectorXd computeMahalanobisVariance(const Eigen::MatrixXd& data){
Eigen::VectorXd mean = data.colwise().mean();
Eigen::MatrixXd cov = computeCovarianceMatrix(data);
Eigen::MatrixXd covInv = cov.inverse();
Eigen::VectorXd distances(data.rows());
for(int i = 0; i < data.rows(); ++i) {
Eigen::VectorXd diff = data.row(i).transpose() - mean;
double scalar = (diff.transpose() * covInv * diff).value();
double dist = std::sqrt(scalar);
distances(i) = dist;
}
return distances;
}
void detectAnomalies(const Eigen::VectorXd& distances, const Eigen::VectorXd& labels, double threshold = 2.0) {
std::cout << "\nAnomaly Scores (Mahalanobis Distance):\n" ;
std::cout << "Index | Score | Flag | Actual\n" ;
std::cout << "------+----------+------------+-------\n" ;
int truePositives = 0;
int falsePositives = 0;
int trueNegatives = 0;
int falseNegatives = 0;
for (int i = 0; i < distances.size(); ++i) {
bool flagged = distances(i) > threshold;
bool actual = labels(i) == 1;
std::string flag = flagged ? "ANOMALY " : "normal ";
if (flagged && actual) truePositives++;
if (flagged && !actual) falsePositives++;
if (!flagged && !actual) trueNegatives++;
if (!flagged && actual) falseNegatives++;
std::cout << " " << std::setw(3) << i <<" | " << std::setw(8) << std::fixed << std::setprecision(4) << distances(i) << " | " << flag << " | " << (int)labels(i) << "\n";
}
std::cout << "\nDetection Summary: \n";
std::cout << "True Positives (Caught suspicious): " << truePositives << "\n";
std::cout << "False Positives (Wrongly flagged): " << falsePositives << "\n";
std::cout << "True Negatives (Correctly cleared): " << trueNegatives << "\n";
std::cout << "False Negatives (Missed suspicious): " << falseNegatives << "\n";
double precision = truePositives / (double)(truePositives + falsePositives);
double recall = truePositives / (double)(truePositives + falseNegatives);
double f1 = 2 * (precision * recall) / (precision + recall);
std::cout << std::fixed << std::setprecision(1);
std::cout << "\nPrecision: " << precision * 100 << "%" << " (of flagged files, how many were truly suspicious)\n";
std::cout << "Recall: " << recall * 100 << "%" << " (of suspicious files, how many did we catch)\n";
std::cout << "F1 Score: " << f1 * 100 << "%" << " (balance between precision and recall)\n";
}
int main() {
// ------ Week 2 ------
Eigen::VectorXd data(5);
data << 2, 4, 4, 4, 5;
std::cout << "Data: " << data.transpose() << "\n";
std::cout << "Mean: " << computeMean(data) << "\n";
std::cout << "Variance: " << computeVariance(data) << "\n";
std::cout << "Std Dev: " << computeStdDev(data) << "\n";
demoMatrixOps();
linearRegressionDemo();
// ----- Week 3 -----
Eigen::MatrixXd forensicsData = loadCSV("csv/data.csv");
if (forensicsData.size() == 0) {
std::cerr << "Stopping: data failed to load.\n";
return 1;
}
std::cout << "\n Loaded Forensics Data (" << forensicsData.rows() << " rows, " << forensicsData.cols() << " cols): \n" << forensicsData << "\n";
Eigen::VectorXd fileSize = forensicsData.col(0);
Eigen::VectorXd accessCount = forensicsData.col(1);
std::cout << "\nFile Size stats:\n";
std::cout << "Mean: " << computeMean(fileSize) << "\n";
std::cout << "Std Dev: " << computeStdDev(fileSize) << "\n";
std::cout << "\nAccess Count stats:\n";
std::cout << "Mean: " << computeMean(accessCount) << "\n";
std::cout << "Std Dev: " << computeStdDev(accessCount) << "\n";
std::vector<std::string> headers = { "file_size_kb", "access_counts", "time_since_modified_hrs", "hash_mismatch" };
writeResultsCSV("csv/output.csv", forensicsData, headers);
// ----- Week 4 ------
std::cout << "\n--- Forensics Classifier (Logistic Regression) ---\n" ;
Eigen::MatrixXd X_raw = forensicsData.leftCols(3);
Eigen::VectorXd y_label = forensicsData.col(3);
Eigen::MatrixXd X_clf(X_raw.rows(), X_raw.cols() + 1);
X_clf.col(0) = Eigen::VectorXd::Ones(X_raw.rows());
X_clf.rightCols(X_raw.cols()) = X_raw;
for (int j = 1; j < X_clf.cols(); ++j) {
double mean = X_clf.col(j).mean();
double std = std::sqrt((X_clf.col(j).array() - mean).square().mean());
if (std > 0) X_clf.col(j) = (X_clf.col(j).array() - mean) / std;
}
Eigen::VectorXd weights = trainLogisticRegression(X_clf, y_label, 5000, 0.1);
evaluateClassifier(X_clf, y_label, weights);
// ----- Week Extension -----
std::cout << "\n---Anomaly Detection (Mahalanobis Distance)---" ;
Eigen::MatrixXd features = forensicsData.leftCols(3);
Eigen::VectorXd labels = forensicsData.col(3);
Eigen::MatrixXd covMatrix = computeCovarianceMatrix(features);
std::cout << "\nCovariance Matrix\n" << covMatrix << "\n" ;
Eigen::VectorXd distances = computeMahalanobisVariance(features);
detectAnomalies(distances, labels, 1.0);
std::cout << "\n--- Threshold Sensitivity -----\n";
std::cout << " Threshold | Flagged | TP | FP\n";
std::cout << "-----------+----------+----+----\n";
for (double t = 1.0; t <= 3.0; t += 0.5) {
int tp = 0, fp = 0, flagged = 0;
for (int i = 0; i < distances.size(); ++i) {
if (distances(i) > t) {
flagged++;
if (labels(i) == 1) tp++;
else fp++;
}
}
std::cout << std::fixed << std::setprecision(1)
<< " " << t << " | " << flagged
<< " | " << tp << " | " << fp << "\n";
}
return 0;
}