forked from hosseinmoein/DataFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_world.cc
371 lines (303 loc) · 18.3 KB
/
hello_world.cc
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
/*
Copyright (c) 2019-2026, Hossein Moein
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Hossein Moein and/or the DataFrame nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL Hossein Moein BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
#include <DataFrame/DataFrame.h> // Main DataFrame header
#include <DataFrame/DataFrameFinancialVisitors.h> // Financial algorithms
#include <DataFrame/DataFrameMLVisitors.h> // Machine-learning algorithms
#include <DataFrame/DataFrameStatsVisitors.h> // Statistical algorithms
#include <DataFrame/Utils/DateTime.h> // Cool and handy date-time object
#include <iostream>
// -----------------------------------------------------------------------------
// DataFrame library is entirely under hmdf name-space
//
using namespace hmdf;
// A DataFrame with ulong index type
//
using ULDataFrame = StdDataFrame<unsigned long>;
// A DataFrame with string index type
//
using StrDataFrame = StdDataFrame<std::string>;
// A DataFrame with DateTime index type
//
using DTDataFrame = StdDataFrame<DateTime>;
// This is just some arbitrary type to show how any type, including the DataFrame itself, could be in DataFrame
//
struct MyData {
int i { 10 };
double d { 5.5 };
std::string s { "Some Arbitrary String" };
MyData() = default;
};
// -----------------------------------------------------------------------------
// The main purpose of this file is to introduce the basic operations of DataFrame.
// For more advanced operations and a complete list of features with code samples, see documentation at:
// https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/DataFrame.html
//
int main(int, char *[]) {
// If you want to fully take advantage of DataFrame parallel computing logic, it is recommended to call the following
// at the beginning of your program.
//
// NOTE: make sure you read and understand the Multithreading section in the documentations (threads could
// potentially hinder performance). This program (hello world) is a perfect example. Since I know this program
// doesn’t deal with large datasets to trigger multithreaded algorithms, populating the thread-pool with threads
// (i.e. calling set_optimum_thread_level()) is a waste of resources.
//
ThreadGranularity::set_optimum_thread_level();
std::vector<unsigned long> idx_col1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector<MyData> mydata_col (10);
std::vector<int> int_col1 = { 1, 2, -3, -4, 5, 6, 7, 8, 9, -10 };
std::vector<double> dbl_col1 = { 0.01, 0.02, 0.03, 0.03, 0.05, 0.06, 0.03, 0.08, 0.09, 0.03 };
ULDataFrame ul_df1;
// One way to load data into the DataFrame is one column at a time. A DataFrame column could be at most as long as its
// index column. So, you must load the index first before loading any column.
//
// Once you load a column or index, the data is moved to DataFrame. The original vectors are now empty. There are other
// ways of loading data without the move.
//
ul_df1.load_index(std::move(idx_col1));
ul_df1.load_column("dbl_col", std::move(dbl_col1));
ul_df1.load_column("my_data_col", std::move(mydata_col));
ul_df1.load_column("integers", std::move(int_col1));
std::vector<unsigned long> idx_col2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector<std::string> str_col = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
std::vector<std::string> cool_col =
{ "Azadi", "Hello", " World", "!", "Hype", "cubic spline", "Shawshank", "Silverado", "Arash", "Pardis" };
std::vector<double> dbl_col2 = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0 };
ULDataFrame ul_df2;
// Also, you can load data into a DataFrame all at once. In this case again the data is moved to the DataFrame.
//
ul_df2.load_data(std::move(idx_col2),
std::make_pair("string col", str_col),
std::make_pair("Cool Column", cool_col),
std::make_pair("numbers", dbl_col2));
StrDataFrame ibm_df;
// Also, you can load data into a DataFrame from a file, supporting a few different formats. If the file cannot be found,
// an exception will be thrown. If the DataFrame data directory is your current directory when running this, it should
// work fine.
//
ibm_df.read("IBM.csv", io_format::csv2);
// To access a column, you must know its name (or index) and its type. In case of a "standard" DataFrame (not a view),
// the columns are returned as a reference to a std::vector of type of that column.
//
// get_column() involves 1 or sometimes 2 hash-table lookups. So, you should not call it repeatedly in a loop. Instead
// get a reference to it and use the reference.
//
const auto &cool_col_ref = ul_df2.get_column<std::string>("Cool Column");
const auto &str_col_ref = ul_df2.get_column<std::string>("string col");
std::cout << cool_col_ref[1] << cool_col_ref[2] << cool_col_ref[3] << std::endl;
std::cout << "Str Column = ";
for (const auto &str : str_col_ref)
std::cout << str << ", ";
std::cout << std::endl;
std::cout << "There are " << ibm_df.get_column<double>("IBM_Close").size() << " IBM close prices" << std::endl;
std::cout << "There are " << ibm_df.get_index().size() << " IBM indices" << std::endl;
// You can write the data to a file or stdout in a few formats. You must specify all the column types, but only once.
// When writing to a file, the file name/path must be create-able.
//
ul_df2.write<std::ostream, std::string, double>(std::cout, io_format::csv2);
ibm_df.write<double, long>("/tmp/test.json", io_format::json);
// You can serialize and deserialize the DataFrame both in string and binary formats.
// This could be used to transmit a DataFrame from one node to another or store a DataFrame in databases, caches, ...
//
const std::string ibm_df_serialized = ibm_df.serialize<double, long>();
StrDataFrame ibm_df_2;
ibm_df_2.deserialize(ibm_df_serialized);
using ul_idx_t = ULDataFrame::IndexType; // This is just unsigned long.
// You can sort by one or multiple columns. You must specify all the column types, but only once.
// Sort first by the index column in ascending order then by "string col" column in descending order.
//
ul_df2.sort<ul_idx_t, std::string, double, std::string>(DF_INDEX_COL_NAME, sort_spec::ascen,
"string col", sort_spec::desce);
// You could get another DataFrame by selecting on one or multiple columns.
// You must specify all the column types, but only once.
//
auto above_150_fn = [](const std::string &, const double &val)-> bool { return (val > 150.0); };
auto above_150_df = ibm_df.get_data_by_sel<double, decltype(above_150_fn), double, long>("IBM_Close", above_150_fn);
// Or, you could choose to get a view. See docs for views.
//
auto above_150_view =
ibm_df.get_view_by_sel<double, decltype(above_150_fn), double, long>("IBM_Close", above_150_fn);
// You can get another DataFrame by group-bying on one or multiple columns.
// You must specify only the type(s) of column(s), you are group-bying.
//
// Group-by column dbl_col, and I am specifying how to summarize the index column and each of the other columns.
//
auto gb_df = ul_df1.groupby1<double>("dbl_col",
LastVisitor<ul_idx_t, ul_idx_t>(),
std::make_tuple("integers", "sum_int", SumVisitor<int>()),
std::make_tuple("my_data_col", "last_my_data", LastVisitor<MyData>()));
// You can run statistical, financial, ML, ... algorithms on one or multiple columns by using visitors. You must specify
// the column(s) type(s). The visitor's data column is of type double and its index column is of type std::string.
//
StdVisitor<double, std::string> stdev_v;
ibm_df.visit<double>("IBM_Close", stdev_v);
std::cout << "Standard deviation of IBM close prices: " << stdev_v.get_result() << std::endl;
// Now let’s declare two DataFrames with index type of DateTime which is a handy object for date/time manipulations.
//
DTDataFrame ibm_dt_df;
DTDataFrame aapl_dt_df;
// Let’s read the AAPL and IBM market data from their files. The data for these two stocks start and end at different
// dates. But there is overlapping data between them.
//
ibm_dt_df.read("DT_IBM.csv", io_format::csv2);
aapl_dt_df.read("DT_AAPL.csv", io_format::csv2);
// First let’s make sure if there are missing data in our important columns, we fill them up.
//
ibm_dt_df.fill_missing<double>({ "IBM_Close", "IBM_Open", "IBM_High", "IBM_Low" }, fill_policy::linear_interpolate);
// Now we join the AAPL and IBM DataFrames using their indices and applying inner-join policy.
//
DTDataFrame aapl_ibm = ibm_dt_df.join_by_index<DTDataFrame, double, long>(aapl_dt_df, join_policy::inner_join);
// Now we calculate the Pearson correlation coefficient between AAPL and IBM close prices. The visitor's data columns are
// of type double and its index column is of type DateTime.
//
CorrVisitor<double, DateTime> corrl_v;
std::cout << "Correlation between AAPL and IBM close prices: "
<< aapl_ibm.visit<double, double>("AAPL_Close", "IBM_Close", corrl_v).get_result()
<< std::endl;
// Now let’s do something more sophisticated and calculate rolling exponentially weighted correlations between IBM and
// Apple close prices. Since this is a rolling -- moving -- analysis the result is a vector of exponentially weighted
// correlations for each date in the data stream.
//
ewm_corr_v<double> ewmcorr { exponential_decay_spec::span, 3 };
const auto &ewmcorr_result =
aapl_ibm.single_act_visit<double, double>("AAPL_Close", "IBM_Close", ewmcorr).get_result();
std::cout << "The last exponentailly weighted correlation between AAPL and IBM close prices: "
<< ewmcorr_result.back() << std::endl;
using dt_idx_t = DTDataFrame::IndexType; // This is just DateTime.
// Appel data are daily. Let’s create 10-day OHLC (plus a bunch of other stats) for close prices.
//
DTDataFrame aapl_ohlc =
aapl_dt_df.bucketize(
bucket_type::by_count,
10,
LastVisitor<dt_idx_t, dt_idx_t>(), // How to bucketize the index column
std::make_tuple("AAPL_Close", "Open", FirstVisitor<double, dt_idx_t>()),
std::make_tuple("AAPL_Close", "High", MaxVisitor<double, dt_idx_t>()),
std::make_tuple("AAPL_Close", "Low", MinVisitor<double, dt_idx_t>()),
std::make_tuple("AAPL_Close", "Close", LastVisitor<double, dt_idx_t>()),
std::make_tuple("AAPL_Close", "Mean", MeanVisitor<double, dt_idx_t>()),
std::make_tuple("AAPL_Close", "Median", MedianVisitor<double, dt_idx_t>()),
std::make_tuple("AAPL_Close", "25% Quantile", QuantileVisitor<double, dt_idx_t>(0.25)),
std::make_tuple("AAPL_Close", "Std", StdVisitor<double, dt_idx_t>()),
// "Mode" column is a column of std::array<ModeVisitor::DataItem, 2>'s
std::make_tuple("AAPL_Close", "Mode", ModeVisitor<2, double, dt_idx_t>()),
std::make_tuple("AAPL_Close", "MAD", MADVisitor<double, dt_idx_t>(mad_type::mean_abs_dev_around_mean)),
// "Z Score" column is a column of std::vector<double>'s
std::make_tuple("AAPL_Close", "Z Score", ZScoreVisitor<double, dt_idx_t>()),
// "Return Vector" column is a column of std::vector<double>'s
std::make_tuple("AAPL_Close", "Return Vector", ReturnVisitor<double, dt_idx_t>(return_policy::log)),
std::make_tuple("AAPL_Volume", "Volume", SumVisitor<long, dt_idx_t>()));
// Big output
//
// aapl_ohlc.write<std::ostream, double, long, std::vector<double>>(std::cout, io_format::csv2);
// Now let's get a view of a random sample of appel data. We randomly sample 35% of the data.
//
auto random_view = aapl_dt_df.get_view_by_rand<double, long>(random_policy::frac_rows_no_seed, 0.35);
// ---------------------------------------------------
//
// Now let’s do some stuff that are a little more involved (multi steps). There are a lot of theories,
// math, and procedures that I am skipping to explain here. See docs for more details.
//
// NOTE: I am applying the following analysis to financial data but it equally applies to other scientific fields.
//
// ---------------------------------------------------
// Let’s calculate IBM daily returns and then try to find clusters of similar patterns in those returns.
// We will use k-means clustering to do that.
//
ReturnVisitor<double> return_v { return_policy::log };
// Calculate the returns and load them as a column.
//
ibm_dt_df.load_result_as_column<double>("IBM_Close", std::move(return_v), "IBM_Return");
ibm_dt_df.get_column<double>("IBM_Return")[0] = 0; // Remove the NaN. It messes things up.
// Let's try to find 4 clusters.
//
KMeansVisitor<4, double, DateTime> kmeans_v { 1000 }; // Iterate at most 1000 times.
ibm_dt_df.single_act_visit<double>("IBM_Return", kmeans_v);
const auto &cluster_means = kmeans_v.get_result();
std::cout << "Means of clusters are: ";
for (const auto &mean : cluster_means)
std::cout << mean << ", ";
std::cout << std::endl;
/*
// This produces a very large output.
//
std::cout << "\nClusters are: ";
for (const auto &mean1 : kmeans_v.get_clusters()) {
for (const auto &mean2 : mean1)
std::cout << mean2 << ", ";
std::cout << '\n' << std::endl;
}
*/
// We want to find a few quantiles of IBM returns
//
QuantileVisitor<double, DateTime> qt50 { 0.5, quantile_policy::mid_point }; // 50%
QuantileVisitor<double, DateTime> qt75 { 0.75, quantile_policy::mid_point }; // 75%
QuantileVisitor<double, DateTime> qt95 { 0.95, quantile_policy::mid_point }; // 95%
ibm_dt_df.single_act_visit<double>("IBM_Return", qt50);
ibm_dt_df.single_act_visit<double>("IBM_Return", qt75);
ibm_dt_df.single_act_visit<double>("IBM_Return", qt95);
std::cout << "IBM returns 50% quantile: " << qt50.get_result() << ", "
<< "75% quantile: " << qt75.get_result() << ", "
<< "95% quantile: " << qt95.get_result() << std::endl;
// Now let’s do another interesting thing. Let’s take the IBM returns curve and split it into 3 different curves; Trend,
// Seasonal, and Idiocentric or Residual or Random. For the sake of this exercise, we assume IBM business goes through
// 170-day seasonal cycles.
//
DecomposeVisitor<double, DateTime> decom { 170, 0.6, 0.01 };
// After this call, the 3 curves will be in decom visitor instance. See docs how to get them and analyze them.
//
ibm_dt_df.single_act_visit<double>("IBM_Return", decom);
// But what if you don’t know the seasonality of IBM returns which would be most of the time. No worries,
// Mr. Joseph Fourier comes to the rescue.
//
FastFourierTransVisitor<double, DateTime> fft;
ibm_dt_df.single_act_visit<double>("IBM_Return", fft);
const auto &magnitudes = fft.get_magnitude();
double max_val = 0;
// The following analysis and conclusion are over simplified and naive. It is more involved which is behind the scope of
// Hello World. But this is the basic idea.
//
for (std::size_t i = 1; i < magnitudes.size(); ++i) {
const double val = 1.0 / magnitudes[i];
if (val > max_val)
max_val = val;
}
std::cout << "The seasonality of IBM returns is " << std::size_t(max_val) << " days.\n"
<< "So use this instead of 170 days in decomposition analysis"
<< std::endl;
// Use lagged auto-correlation to verify your finding.
//
FixedAutoCorrVisitor<double, DateTime> facorr { 170, roll_policy::blocks };
FixedAutoCorrVisitor<double, DateTime> facorr2 { std::size_t(max_val), roll_policy::blocks };
ibm_dt_df.single_act_visit<double>("IBM_Return", facorr);
ibm_dt_df.single_act_visit<double>("IBM_Return", facorr2);
std::cout << "Auto correlations of 170 days lag: ";
for (std::size_t i = facorr.get_result().size() - 1; i > facorr.get_result().size() - 10; --i)
std::cout << facorr.get_result()[i] << ", ";
std::cout << std::endl;
std::cout << "Auto correlations of " << std::size_t(max_val) << " days lag: ";
for (std::size_t i = facorr2.get_result().size() - 1; i > facorr2.get_result().size() - 10; --i)
std::cout << facorr2.get_result()[i] << ", ";
std::cout << std::endl;
return (0);
}
// Local Variables:
// mode:C++
// tab-width:4
// c-basic-offset:4
// End: