-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
797 lines (620 loc) · 18.9 KB
/
Copy pathSource.cpp
File metadata and controls
797 lines (620 loc) · 18.9 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <omp.h>
#include <ctime>
// defines
#define MIN_PATTERN_WIDTH 1
#define MIN_PATTERN_HEIGHT 3
#define MAX_PATTERN_WIDTH 1
#define MAX_PATTERN_HEIGHT 4
#define e 2.71828
#define TESTED_DRAW 100 // 0 will attempt to predict the next future draw. A positive number will test N predictions at the end of the dataset
#define EN_LOC_AMP false
#define EN_TIMEPLUS false
#define EN_STANDARDIZE_WEIGHTS false //weight standardisation enabled
#define EN_NORMALIZE_DATA true //normalize the data, per column values, to the value range [0,1]. If using relative dataset, do not enable
const double ACCURACY = 0.76; // this value indicates how similar a pattern must be to another, to consider it to be a match
using namespace std;
// struct used to store the patterns
typedef struct _pattern {
public:
int width, height;
double image[MAX_PATTERN_HEIGHT][MAX_PATTERN_WIDTH];
double score;
int indices[MAX_PATTERN_WIDTH]; // index linking to the row position where the pattern was found
double recent;
} pattern;
// score struct for keeping track of which scores belong long to which number
typedef struct _score {
public:
double weight;
int num;
vector<double> vals;
vector<double> wts;
bool cald = false;
void CalcWeight() {
weight = 0;
double wtMean = 0;
for (int i = 0; i < (int)vals.size(); i++) {
weight += (vals[i] * wts[i]);
wtMean += wts[i];
}
weight = weight / wtMean;
cald = true;
}
void Clear() {
vals.clear();
wts.clear();
}
} score;
//prototypes
int ListContains(const pattern &bar, vector<pattern> &p);
bool UpperMatches(const pattern &a, const pattern &b);
bool PatternMatches(const pattern &a, const pattern &b);
bool CompVals(double a, double b);
double GetAcc(double a, double b);
long long Sigmoid(long long x, long long mid, long long highest);
pattern PullFromDataset(int line, int offset, int width, int height, const vector<vector<double> > &data);
bool IndicesMatch(const pattern &foo, const pattern &bar);
//overloaded operators
bool operator == (const pattern& left, const pattern& right) {
return PatternMatches(left, right);
}
bool operator < (const pattern& left, const pattern& right) {
return left.score < right.score;
}
bool operator < (score left, score right) {
if (left.cald == false)
left.CalcWeight();
if (right.cald == false)
right.CalcWeight();
return left.weight < right.weight;
}
int main() {
std::cout.precision(6);
std::cout.setf(std::ios::fixed);
string out = "";
time_t before, after;
//data arrays
vector<vector<double> > dataset;
vector<double> row;
vector<double> min, max;
// getting time
before = time(NULL);
/*
FILE INPUT AND PARSING
*/
cout << "Reading File Data..." << endl;
string fileName = "allForexData.csv";
bool fileRead = true;
ifstream fileInput(fileName);
if (fileInput.bad()) {
cout << "Could not open file: " << fileName << endl;
fileRead = false;
}
//load data into a vector of strings
string lineS = "";
vector<string> preData;
if (fileRead) {
while (getline(fileInput, lineS)) {
if (!lineS.empty()) {
lineS += ','; //add a comma at the end of the string to facilitate parsing
preData.push_back(lineS);
}
}
}
cout << "Number of Lines in File: " << preData.size() << endl;
//parse data
int numCols = 0;
cout << "Parsing File Data..." << endl;
string preWord = "";
string sampDate = "";
int rst = 0, day;
tm timeIn;
double val;
for (int i = 0; i < (int)preData.size(); i++) {
rst = 0;
day = -1;
preWord = "";
//pre load foo with line
for (int j = 0; j < (int)preData[i].size(); j++) {
if (preData[i][j] != ',') {
preWord += preData[i][j];
}
else {
//parse date into a day of week for cleaning up (this is because the data include weekends in which the price stays the same)
if (rst == 0) {
rst++;
timeIn.tm_sec = 0;
timeIn.tm_min = 0;
timeIn.tm_hour = 0;
sampDate = preWord[0];
sampDate += preWord[1];
timeIn.tm_mday = stoi(sampDate);
sampDate = preWord[3];
sampDate += preWord[4];
timeIn.tm_mon = stoi(sampDate) - 1;
sampDate = preWord[6];
sampDate += preWord[7];
sampDate += preWord[8];
sampDate += preWord[9];
timeIn.tm_year = stoi(sampDate) - 1900;
time_t timeOut = mktime(&timeIn);
day = timeIn.tm_wday;
preWord = "";
}
else {
val = stod(preWord);
row.push_back(val);
//record min and max values for normalization on every column
if (EN_NORMALIZE_DATA) {
if (i > 0) {
if (min[rst - 1] > val)
min[rst - 1] = val;
if (max[rst - 1] < val)
max[rst - 1] = val;
}
else {
min.push_back(val);
max.push_back(val);
}
}
preWord = "";
rst++;
}
}
}
//check for the day of the week and dont add to dataset if its not friday or saturday
if (true) { //day != 5 && day != 6
if (i > 0 && (int)row.size() != numCols) {
cout << "Dimension error at line #: " << i << endl;
}
numCols = (int)row.size();
dataset.push_back(row);
}
row.clear();
}
int NUM_LINES = (int)dataset.size();
int NUM_NUMBERS = numCols;
vector<vector<double>> datasetRel(NUM_LINES, vector<double>(NUM_NUMBERS, 0));
cout << "Number of Lines in Parsed Dataset: " << dataset.size() << endl;
/*
Normalize Data
*/
if (EN_NORMALIZE_DATA) {
cout << "MIN: ";
for (int i = 0; i < (int)min.size(); i++) {
cout << min[i] << " ";
}
cout << endl << "MAX: ";
for (int i = 0; i < (int)max.size(); i++) {
cout << max[i] << " ";
}
cout << endl;
for (int i = 0; i < (int)dataset.size(); i++) {
//make sure width of the dataset and the min/max vectors are the same
if (dataset[i].size() == min.size() && dataset[i].size() == max.size()) {
for (int j = 0; j < (int)dataset[i].size(); j++) {
dataset[i][j] = abs(dataset[i][j] - min[j]) / (max[j] - min[j]);
}
}
else {
cout << "Error: Couldn't normalize data as dataset width doesnt match min or max size: " << dataset[i].size() << " " << min.size() << " " << max.size() << " at: " << i << endl;
break;
}
}
}
// Initialise relative dataset where the data is relative to the previous rows value
for (int i = 1; i < (int)dataset.size(); i++) {
for (int j = 0; j < (int)dataset[i].size(); j++) {
datasetRel[i][j] = dataset[i][j] - dataset[i - 1][j];
}
}
/*
PATTERN FINDING
*/
cout << "Pattern Finding..." << endl;
vector<pattern> patternList;
int numThreads;
#pragma omp parallel shared(patternList)
{
if (omp_get_thread_num() == 0)
numThreads = omp_get_num_threads();
pattern foo;
vector<pattern> _patternList;
int bip = 0;
// first two for loops determine pattern limits, the smallest of which is 1x2
for (int width = MIN_PATTERN_WIDTH; width <= MAX_PATTERN_WIDTH; width++) {
if (omp_get_thread_num() == 0)
cout << width << " of " << MAX_PATTERN_WIDTH << "... ";
for (int height = MIN_PATTERN_HEIGHT; height <= MAX_PATTERN_HEIGHT; height++) {
foo.width = width;
foo.height = height;
#pragma omp for
//next go through, line by line, column by column with the pattern
for (int line = 1; line <= ((NUM_LINES - height) - TESTED_DRAW); line++) {//line += MAX_PATTERN_SIZE
for (int col = 0; col <= NUM_NUMBERS - width; col++) { //col += MAX_PATTERN_SIZE
foo.score = 1;
foo.recent = (double)line;
//pull in the pattern from the dataset
for (int i = 0; i < width; i++) {
//keep track of original row position...
foo.indices[i] = col + i;
for (int j = 0; j < height; j++) {
//load up foo
foo.image[j][i] = datasetRel[line + j][col + i];
}
}
// if the pattern is not a match or the vector is empty, add it to the vector
bip = 0;
bip = ListContains(foo, _patternList);
if (bip == -1) {
_patternList.push_back(foo);
}
else { // the list has a match and returns the index of the pattern in the vector, so increment its score
_patternList[bip].score += 1;
}
}
}
}
}
#pragma omp critical
{
patternList.insert(patternList.end(), _patternList.begin(), _patternList.end());
}
}
cout << endl << "Number of Threads: " << numThreads << endl;
// sort the patternlist before printing
sort(patternList.begin(), patternList.end());
/*
Standardize the Scores
*/
if (EN_STANDARDIZE_WEIGHTS) {
double mean = 0, sqr = 0, msqrd = 0;
for (int i = 0; i < (int)patternList.size(); i++) {
mean += (double)patternList[i].score;
}
mean = mean / patternList.size();
for (int i = 0; i < (int)patternList.size(); i++) {
sqr = patternList[i].score - mean;
msqrd += pow(sqr, 2.0);
}
msqrd = msqrd / patternList.size();
double standDist = sqrt(msqrd);
for (int i = 0; i < (int)patternList.size(); i++) {
patternList[i].score = (double)(patternList[i].score - mean) / standDist;
}
}
cout << endl;
cout << "Number of Patterns Found: " << patternList.size() << endl << endl;
pattern foo;
ofstream output;
//write the pattern data to file for checking
out = "";
output.open("MottledPatternsFound.txt");
for (int i = 0; i < (int)patternList.size(); i++) {
foo = patternList[i];
for (int j = 0; j < foo.height; j++) {
for (int k = 0; k < foo.width; k++) {
out += to_string(foo.image[j][k]) + " ";
}
out += "\n";
}
out += to_string(foo.score) + "\n\n";
}
output << out;
output.close();
//print the last few values in the dataset
double disp;
for (int h = NUM_LINES - 10; h < NUM_LINES; ++h) {
for (int i = 0; i < NUM_NUMBERS; i++) {
//Denormalize first
if (EN_NORMALIZE_DATA) {
disp = (dataset[h][i] * (max[i] - min[i])) + min[i];
}
else {
disp = dataset[h][i];
}
//then print
cout << disp << " ";
}
cout << endl;
}
cout << endl;
/*
PREDICTION
*/
vector<score> prediction;
string testS = "";
score blegh;
blegh.Clear();
for (int i = 0; i < NUM_NUMBERS; i++) {
blegh.num = i;
blegh.weight = 0;
prediction.push_back(blegh);
}
pattern bar;
int line;
int posMatches, avPosMat = 0, minPosMat = 100000, maxPosMat = 0, failUp = 0, failDown = 0;
double dispBack = 0, pCtr = 0, tpCtr = 0;
double acc = 0, totalAcc = 0, extra = 1, timePlus = 1;
vector<double> grAcc;
for (int tests = (NUM_LINES - TESTED_DRAW); tests <= NUM_LINES; tests++) {
for (int clr = 0; clr < (int)prediction.size(); clr++) {
prediction[clr].Clear(); //initially clear
// to prevent nan output predictions, always start with a baseline of the previous rows information
prediction[clr].vals.push_back(dataset[tests - 1][clr]); // and re initialize with the previous rows value
prediction[clr].wts.push_back(1); // and reinitialize with a weight of 1
}
posMatches = 0;
for (int i = 0; i < (int)patternList.size(); i++) {
foo = patternList[i];
bar.width = foo.width;
bar.height = foo.height;
//next go through column by column with the pattern, testing for a match at the end of the dataset
for (int col = 0; col <= NUM_NUMBERS - foo.width; col++) {
line = (tests - foo.height) + 1;
//pull in the pattern from the dataset
for (int i = 0; i < foo.width; i++) {
//pull in the indices as well
bar.indices[i] = col + i;
for (int j = 0; j < (foo.height - 1); j++) {
bar.image[j][i] = datasetRel[line + j][col + i];
}
}
// if the upper portion of the pattern from the pattern list matches whats on the dataset, put the last row of the pattern into the prediction val and the respective weight of the pattern into the wts
if (UpperMatches(foo, bar)) {
posMatches++;
//testing out if original pattern has a match in an original column spot, give it a little more score
if (EN_LOC_AMP) {
if (IndicesMatch(bar, foo)) {
extra = 2;
}
else {
extra = 1;
}
}
else {
extra = 1;
}
//giving slightly more weight to newer patterns
if (EN_TIMEPLUS) {
timePlus = 1 + (foo.recent / (double)NUM_LINES);
}
else {
timePlus = 1;
}
for (int k = 0; k < foo.width; k++) {
prediction[col + k].vals.push_back(dataset[tests - 1][col + k] + foo.image[foo.height - 1][k]);
prediction[col + k].wts.push_back(foo.score * extra * timePlus);
}
}
}
}
/*
Calculate the Prediction Weights and DeNormalize the Prediction if needed
*/
for (int i = 0; i < (int)prediction.size(); i++) {
//make sure width of the prediction and the min/max vectors are the same (this should always check out)
prediction[i].CalcWeight();
//Denormalize
if (EN_NORMALIZE_DATA) {
prediction[i].weight = (prediction[i].weight * (max[i] - min[i])) + min[i];
}
}
/*
DISPLAY RESULTS
*/
cout << endl << "Positive Matches for Prediction Found: " << posMatches << endl;
avPosMat += posMatches;
if (posMatches < minPosMat)
minPosMat = posMatches;
if (posMatches > maxPosMat)
maxPosMat = posMatches;
string pOrF;
if (tests < NUM_LINES) {
testS = "Test #" + to_string(NUM_LINES - tests) + " ";
}
else {
testS = ">>>>>>> Future ";
}
cout << testS << "Prediction: " << endl;
for (int i = 0; i < (int)prediction.size(); i++) {
cout << prediction[i].weight << " ";
}
cout << endl;
//calculate accuracy
if (tests < NUM_LINES && TESTED_DRAW > 0) {
pOrF = "";
pCtr = 0;
cout << "Actual:" << endl;
for (int i = 0; i < NUM_NUMBERS; i++) {
if (EN_NORMALIZE_DATA) {
disp = (dataset[tests][i] * (max[i] - min[i])) + min[i];
dispBack = (dataset[tests - 1][i] * (max[i] - min[i])) + min[i];
}
else {
disp = dataset[tests][i];
dispBack = dataset[tests - 1][i];
}
cout << disp << " ";
//determine basic up vs down pass or fail...
if (prediction[i].weight <= dispBack) {
if (disp <= dispBack) {
pOrF += "D-Pass-- ";
pCtr += 1;
}
else {
pOrF += "D-Fail-- ";
failDown++;
}
}
else {
if (disp > dispBack) {
pOrF += "U-Pass-- ";
pCtr += 1;
}
else {
pOrF += "U-Fail-- ";
failUp++;
}
}
}
cout << endl;
cout << pOrF << endl;
acc = 0;
for (int i = 0; i < (int)prediction.size(); i++) {
if (EN_NORMALIZE_DATA) {
disp = (dataset[tests][i] * (max[i] - min[i])) + min[i];
}
else {
disp = dataset[tests][i];
}
acc += GetAcc(prediction[i].weight, disp);
}
disp = (double)prediction.size();
acc = acc / disp;
pCtr = pCtr / disp;
cout << "Row Numeric Accuracy: " << acc << endl;
cout << "Row Pass/Fail Accuracy: " << pCtr << endl;
grAcc.push_back(pCtr);
tpCtr += pCtr;
totalAcc += acc;
}
}
if (TESTED_DRAW > 0) {
totalAcc = totalAcc / (double)TESTED_DRAW;
tpCtr = tpCtr / (double)TESTED_DRAW;
cout << endl << "Total Numeric Accuracy for " << TESTED_DRAW << " Tests: " << totalAcc << endl;
cout << "Total Pass/Fail Accuracy: " << tpCtr << endl;
}
avPosMat = avPosMat / TESTED_DRAW;
cout << "Average Positive Matches: " << avPosMat << endl;
cout << "Fewest Positive Matches: " << minPosMat << endl;
cout << "Most Positive Matches: " << maxPosMat << endl;
cout << "Number of Up Fails: " << failUp << endl;
cout << "Number of Down Fails: " << failDown << endl << endl;
after = time(NULL);
cout << endl << "Time taken in seconds: " << (after - before) << endl << endl;
out = "";
output.open("PassFailCtrs.csv");
for (int i = 0; i < (int)grAcc.size(); i++) {
out += to_string(grAcc[i]) + '\n';
}
output << out;
output.close();
system("pause");
return 0;
}
int ListContains(const pattern &bar, vector<pattern> &p) {
int result = -1; // returns -1 on not contained
if (p.empty())
return result;
for (int i = 0; i < (int)p.size(); i++) {
if (bar == p[i]) {
result = i;
break;
}
}
return result;
}
bool UpperMatches(const pattern &a, const pattern &b) {
int ctr = 0;
if(a.width != b.width || a.height != b.height)
return false;
for(int i = 0; i < b.width; i++) {
for(int j = 0; j < (b.height - 1); j++) {
if(CompVals(a.image[j][i], b.image[j][i]))
ctr++;
}
}
return ctr == (a.width * (a.height - 1));
}
bool PatternMatches(const pattern& a, const pattern& b) {
int ctr = 0;
if (a.width != b.width || a.height != b.height)
return false;
//CompVals(a.image[j][i], b.image[j][i])
for (int i = 0; i < b.width; i++) {
for (int j = 0; j < b.height; j++) {
if (CompVals(a.image[j][i], b.image[j][i]))
ctr++;
}
}
//double result = ctr / (double)(b.width * (b.height - 1));
return ctr == (a.width * a.height);
}
bool CompVals(double a, double b) {
//cout << a << " " << b << " | ";
double error;
if (a == 0 && b == 0)
return true;
error = abs(a - b);
error = error / a;
/*
if (b != 0)
equ = a / b;
else if (a != 0)
equ = b / a;
if (abs(equ) > 1.000)
equ = 1.0000 / equ;
return equ >= ACCURACY;
*/
return abs(error) < (1 - ACCURACY);
}
double GetAcc(double a, double b) {
double error;
error = abs(a - b);
error = error / a;
/*
if (b != 0)
equ = a / b;
else if (a != 0)
equ = b / a;
if (abs(equ) > 1.000)
equ = 1.0000 / equ;
return equ >= ACCURACY;
*/
return (1 - abs(error));
}
long long Sigmoid(long long x, long long mid, long long highest) {
double L = (double)highest;
double x0 = (double)mid;
double k = 0.008;
double exp = k * (x - x0);
double val = L / (1.0 + (1.0 / pow(e, exp)));
//rescale the value and cast back to int
return (long long)val;
}
pattern PullFromDataset(int line, int offset, int width, int height, const vector<vector<double> > &data) {
pattern result;
result.width = width;
result.height = height;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if(line + j < (int)data.size())
result.image[j][i] = data[line + j][offset + i];
else
result.image[j][i] = 0;
}
}
return result;
}
bool IndicesMatch(const pattern &foo, const pattern &bar) {
bool result = true;
if (foo.width != bar.width)
return false;
for (int x = 0; x < foo.width; x++) {
if (foo.indices[x] != bar.indices[x])
return false;
}
return result;
}