-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoost.cpp
86 lines (78 loc) · 2.38 KB
/
Boost.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
#include "Boost.h"
#include <algorithm>
#include <random>
#include <chrono>
Vector<double, long> Boost::GetAAR_CAAR(long row, long col) {
return AAR_CAAR(row, col);
}
std::list< Vector<double, long> > Boost::GetAARlist() {
std::list<Vector<double, long> > aar_list;
for (int r = AAR_CAAR.MinRowIndex(); r <= AAR_CAAR.MaxRowIndex(); ++r) {
aar_list.push_back(AAR_CAAR(r, 1));
}
return aar_list;
}
std::list< Vector<double, long> > Boost::GetCAARlist() {
std::list<Vector<double, long> > caar_list;
for (int r = AAR_CAAR.MinRowIndex(); r <= AAR_CAAR.MaxRowIndex(); ++r) {
caar_list.push_back(AAR_CAAR(r, 2));
}
return caar_list;
}
const double Boost::GetThreshold() {
return threshold;
}
const int Boost::GetResampleTimes() {
return times;
}
void Boost::SetThreshold(double thre) {
threshold = thre;
}
void Boost::SetResampleTimes(int t) {
times = t;
}
const Group& Boost::GetGroup(int index) {
return ThreeGroups[index];
}
Group Boost::Sampling(const Group& group) {
Group sampled;
std::vector<std::string> keys = group.Gettickers();
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(keys.begin(), keys.end(), std::default_random_engine(seed));
for (int i = 0; i < 60; ++i) {
sampled.addnew(group.GetStock(keys[i]));
}
return sampled;
}
void Boost::WriteGroups(const std::map<std::string, Stock>& allstocks_) {
for (auto i = allstocks_.begin(); i != allstocks_.end(); i++) {
if ((i->second).GetSurprise() > threshold) {
ThreeGroups[0].addnew(i->second);
}
else if (i->second.GetSurprise() < -threshold) {
ThreeGroups[2].addnew(i->second);
}
else {
ThreeGroups[1].addnew(i->second);
}
}
}
void Boost::Bootstrap(const Share& SPY) {
cout << "Bootstraping initialized"<<endl
<< "Resampling might be called if outlier AR detected, which might takes a longer time." << endl;
for (int i = 0; i < times; ++i) {
for (int j = 1; j <= 3; ++j) {
Group sampled = Sampling(ThreeGroups[j - 1]);
while (!sampled.Computing(SPY)) {
sampled = Sampling(ThreeGroups[j - 1]);
}
std::vector<double> AAR = sampled.GetAAR();
std::vector<double> CAAR = sampled.GetCAAR();
for (int k = 1; k <= 90; k++) {
AAR_CAAR(j, 1)[k] = (AAR_CAAR(j, 1)[k] * i + AAR[k - 1]) / (i + 1);
AAR_CAAR(j, 2)[k] = (AAR_CAAR(j, 2)[k] * i + CAAR[k - 1]) / (i + 1);
}
}
std::cout << "Bootstrap Time " << i + 1 << " Completed" << std::endl;
}
}