-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
185 lines (160 loc) · 6.87 KB
/
main.cpp
File metadata and controls
185 lines (160 loc) · 6.87 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
#include <iostream>
#include <string>
#include <vector>
#include "DecisionTree.h"
#include "LinearRegression.h"
#include "StockData.h"
using namespace std;
//Global variables for testing purposes
const int maxIterations = 1000;
const double learningRate = 0.01;
int main() {
string FolderInput;
string FileInput;
// First prompt: ETF or Stock selection
cout << "Would you like to load an ETF(1), or Stock(2): ";
cin >> FolderInput;
// Second prompt: Specific file selection
cout << "Which specific entry would you like to load (which .txt file): ";
cin >> FileInput;
string filePath;
if (FolderInput == "1") {
filePath = "../ETFs/" + FileInput;
} else if (FolderInput == "2") {
filePath = "../Stocks/" + FileInput;
} else {
cout << "Invalid input" << endl;
return 1;
}
dataLoader dataLoaderObj;
vector<stockDataPoint> allData = dataLoaderObj.readFile(filePath);
pair<vector<stockDataPoint>, vector<stockDataPoint>> splitResult = dataLoaderObj.splitData(allData);
vector<stockDataPoint> trainingData = splitResult.first; // 80% training data
vector<stockDataPoint> testData = splitResult.second; // 20% test data
dataLoaderObj.scaleData(trainingData, testData);
cout << "Data loaded successfully" << endl;
LinearRegression linearRegression;
DecisionTree decisionTree;
string menuInput;
while (true) {
cout << "===============================" << endl;
cout << "\033[31mStock Price Prediction System\033[0m" << endl;
cout << "===============================" << endl;
cout << "1. Train Linear Regression" << endl;
cout << "2. Train Decision Tree" << endl;
cout << "3. Compare Performance" << endl;
cout << "4. Predict using Linear Regression" << endl;
cout << "5. Predict using Decision Tree" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> menuInput;
if (menuInput == "6") {
cout << "Exiting program" << endl;
return 0;
}
else if (menuInput == "1") {
cout << "Training Linear Regression" << endl;
linearRegression.trainLinearRegression(trainingData, learningRate, maxIterations);
cout << "Linear Regression trained successfully" << endl;
}
else if (menuInput == "2") {
cout << "Training Decision Tree" << endl;
auto [trainFeatures, trainTargets] = dataLoaderObj.extractFeaturesAndTargets(trainingData);
decisionTree.train(trainFeatures, trainTargets);
cout << "Decision Tree trained successfully" << endl;
}
else if (menuInput == "3") {
cout << "Comparing Performance" << endl;
// Calculate Linear Regression performance using testData
vector<double> predictions;
vector<double> actual;
for (const auto& testPoint : testData) {
float prediction = linearRegression.predict(testPoint.stockInputCategories);
predictions.push_back((double)prediction);
actual.push_back((double)testPoint.target);
}
double linearRegressionMSE = linearRegression.MSE(predictions, actual);
cout << "Linear Regression Performance (MSE): " << linearRegressionMSE << endl;
// Test Decision Tree on test data
auto [testFeatures, testTargets] = dataLoaderObj.extractFeaturesAndTargets(testData);
vector<double> decisionTreePredictions = decisionTree.predictBatch(testFeatures);
// Calculate MSE for Decision Tree
double decisionTreeMSE = linearRegression.MSE(decisionTreePredictions, testTargets);
cout << "Decision Tree Performance (MSE): " << decisionTreeMSE << endl;
ofstream mseFile("mse_results.csv");
if (mseFile.is_open()) {
mseFile << "Model,MSE\n";
mseFile << "LinearRegression," << linearRegressionMSE << "\n";
mseFile << "DecisionTree," << decisionTreeMSE << "\n";
mseFile.close();
cout << "MSE results exported to mse_results.csv" << endl;
}
else{
cerr << "Failed to write MSE results file." << endl;
}
return 0;
}
else if (menuInput == "4") {
// Predict using Linear Regression
double open, high, low;
int volume, openInt;
cout << "Enter open price: ";
cin >> open;
cout << "Enter high price: ";
cin >> high;
cout << "Enter low price: ";
cin >> low;
cout << "Enter volume: ";
cin >> volume;
cout << "Enter openInt: ";
cin >> openInt;
// Construct feature vector with bias term first (same structure as training data)
vector<double> rawInput;
//features.push_back(1.0); // Bias term
rawInput.push_back(open);
rawInput.push_back(high);
rawInput.push_back(low);
rawInput.push_back((double)volume);
rawInput.push_back((double)openInt);
vector<double> scaledInput = dataLoaderObj.scaleSingleInput(rawInput);
vector<double> features;
features.push_back(1.0);
features.insert(features.end(), scaledInput.begin(), scaledInput.end());
double prediction = linearRegression.predict(features);
cout << "Predicted close price: " << prediction << endl;
}
else if (menuInput == "5") {
// Predict using Decision Tree
double open, high, low;
int volume, openInt;
cout << "Enter open price: ";
cin >> open;
cout << "Enter high price: ";
cin >> high;
cout << "Enter low price: ";
cin >> low;
cout << "Enter volume: ";
cin >> volume;
cout << "Enter openInt: ";
cin >> openInt;
// Construct feature vector with bias term first (same structure as training data)
vector<double> features;
features.push_back(1.0); // Bias term
features.push_back(open);
features.push_back(high);
features.push_back(low);
features.push_back((double)volume);
features.push_back((double)openInt);
double prediction = decisionTree.predict(features);
cout << "Predicted close price: " << prediction << endl;
}
else {
cout << "Invalid input" << endl;
return 0;
}
}
}
// TIP See CLion help at <a
// href="https://www.jetbrains.com/help/clion/">jetbrains.com/help/clion/</a>.
// Also, you can try interactive lessons for CLion by selecting
// 'Help | Learn IDE Features' from the main menu.