-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrepareRawData.cpp
133 lines (123 loc) · 4.62 KB
/
PrepareRawData.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
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
#include "PrepareRawData.h"
namespace MeanVarianceFrontier
{
PrepareRawData::PrepareRawData(std::vector<RawData> rawDataVector) :
m_dailyReturnsData{ }
{
for (auto& returnsVector : rawDataVector)
{
m_dailyReturnsData.emplace_back(returnsVector.financialDataName, std::move(returnsVector.rawData));
}
}
void PrepareRawData::limitDataByDate()
{
auto limitDate = findMaxEarliestDateAmongReturnsData();
for (auto& data : m_dailyReturnsData)
{
auto& rawReturnsData = data.getReturnsData();
auto removeAllElementsBeforeOrAfter = std::find_if(rawReturnsData.begin(), rawReturnsData.end(),
[&limitDate](const std::pair<boost::gregorian::date, double>& x)
{ return x.first == limitDate; });
if (rawReturnsData.front().first < rawReturnsData.back().first)
{
rawReturnsData.erase(rawReturnsData.begin(), removeAllElementsBeforeOrAfter);
}
else
{
rawReturnsData.erase(removeAllElementsBeforeOrAfter, rawReturnsData.end());
}
}
}
boost::gregorian::date PrepareRawData::findMaxEarliestDateAmongReturnsData()
{
std::vector<boost::gregorian::date> earliestDates;
for (auto& data : m_dailyReturnsData)
{
auto rawReturnsData = data.getReturnsData();
if (rawReturnsData.front().first < rawReturnsData.back().first)
earliestDates.emplace_back(rawReturnsData.front().first);
else
earliestDates.emplace_back(rawReturnsData.back().first);
}
return *(std::max_element(earliestDates.begin(), earliestDates.end()));
}
void PrepareRawData::removeAllMissingValues()
{
size_t repeat = m_dailyReturnsData.size();
for (size_t i = 0; i < repeat; i++)
{
removeMissingValues();
if (allReturnDataSizesMatch())
break;
}
}
bool PrepareRawData::allReturnDataSizesMatch()
{
std::vector<size_t> returnDataSizes;
for (auto& dailyReturns : m_dailyReturnsData)
{
returnDataSizes.push_back(dailyReturns.getReturnsData().size());
}
if (std::adjacent_find(returnDataSizes.begin(), returnDataSizes.end(), std::not_equal_to<>()) ==
returnDataSizes.end())
return true;
return false;
}
void PrepareRawData::removeMissingValues()
{
MeanVarianceFrontier::ComputeDailyReturns* smallestDailyReturn = nullptr;
size_t smallestVectorSize = SIZE_MAX;
for (auto& dailyReturnsData : m_dailyReturnsData)
{
size_t dataSize = dailyReturnsData.getReturnsData().size();
if (smallestVectorSize > dataSize)
{
smallestVectorSize = dailyReturnsData.getReturnsData().size();
smallestDailyReturn = &dailyReturnsData;
}
}
if (smallestDailyReturn != nullptr)
{
auto& smallestDailyReturnVector = smallestDailyReturn->getReturnsData();
for (auto& dailyReturnsData : m_dailyReturnsData)
{
auto& otherVec = dailyReturnsData.getReturnsData();
auto temp = getIntersectionOfDateReturns(smallestDailyReturnVector, otherVec);
std::swap(temp, otherVec);
}
}
}
std::vector<MeanVarianceFrontier::ComputeDailyReturns> PrepareRawData::getDailyReturns()
{
return m_dailyReturnsData;
}
std::vector<std::pair<boost::gregorian::date, double>> PrepareRawData::getIntersectionOfDateReturns(
const std::vector<std::pair<boost::gregorian::date, double>>& reference,
const std::vector<std::pair<boost::gregorian::date, double>>& toModify)
{
std::vector<std::pair<boost::gregorian::date, double>> temp;
auto refIter = reference.begin();
auto toModifyIter = toModify.begin();
while (refIter != reference.end() || toModifyIter != toModify.end())
{
if (refIter->first == toModifyIter->first)
{
temp.push_back(*toModifyIter);
++refIter;
if (refIter == reference.end())
break;
}
while (refIter->first < toModifyIter->first)
{
++refIter;
}
if (refIter->first > toModifyIter->first)
{
++toModifyIter;
if (toModifyIter == toModify.end())
break;
}
}
return temp;
}
}