-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegression.cc
More file actions
48 lines (44 loc) · 1.19 KB
/
Regression.cc
File metadata and controls
48 lines (44 loc) · 1.19 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
/*
* Regression.cc - create an object that fits a line to a list of real numbers
*
* Copyright (C) 2023
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
#include <stdio.h>
#include "Regression.h"
/* ---------------------------------------------------------------------- */
Regression::Regression(std::vector<float> input) {
float sumX = 0.0;
float sumY = 0.0;
float sumXY = 0.0;
float sumX2 = 0.0;
int count = 0;
minCentroid = 0.0;
maxCentroid = 0.0;
if (input.size() > 1) {
minCentroid = input[0];
maxCentroid = input[0];
}
// produce regression terms
for (auto entry : input) {
listCopy.push_back(entry);
sumY += entry;
sumX += count;
sumXY += entry * count;
sumX2 += count * count;
count++;
if (entry > maxCentroid) {
maxCentroid = entry;
} else if (entry < minCentroid) {
minCentroid = entry;
}
}
slope = (count * sumXY - sumX * sumY) / (count * sumX2 - sumX * sumX);
yIntercept = sumY / count - slope * sumX / count;
}
/* ---------------------------------------------------------------------- */
Regression::~Regression(void) {
listCopy.clear();
}