1 #include <iostream>
2 #include <string>
3 #include <fstream>
4 #include <map>
5 #include <cmath>
6 #include <vector>
7 #include <iomanip>
8 #include <boost/algorithm/string.hpp>
9 #include <boost/math/statistics/linear_regression.hpp>
10 
11 
main(int argc,char ** argv)12 int main(int argc, char** argv)
13 {
14     if (argc != 2)
15     {
16         std::cout << "Usage: ./regress_accuracy.x foo.csv\n";
17         return 1;
18     }
19     std::string filename = std::string(argv[1]);
20     std::ifstream ifs(filename.c_str());
21     if (!ifs.good())
22     {
23         std::cerr << "Couldn't find file " << filename << "\n";
24         return 1;
25     }
26     std::map<std::string, std::vector<double>> m;
27 
28     std::string header_line;
29     std::getline(ifs, header_line);
30     std::cout << "Header line = " << header_line << "\n";
31     std::vector<std::string> header_strs;
32     boost::split(header_strs, header_line, boost::is_any_of(","));
33     for (auto & s : header_strs) {
34         boost::algorithm::trim(s);
35     }
36 
37     std::string line;
38     std::vector<double> r;
39     std::vector<double> matched_holder;
40     std::vector<double> linear;
41     std::vector<double> quadratic_b_spline;
42     std::vector<double> cubic_b_spline;
43     std::vector<double> quintic_b_spline;
44     std::vector<double> cubic_hermite;
45     std::vector<double> pchip;
46     std::vector<double> makima;
47     std::vector<double> fotaylor;
48     std::vector<double> quintic_hermite;
49     std::vector<double> sotaylor;
50     std::vector<double> totaylor;
51     std::vector<double> septic_hermite;
52     while(std::getline(ifs, line))
53     {
54         std::vector<std::string> strs;
55         boost::split(strs, line, boost::is_any_of(","));
56         for (auto & s : strs)
57         {
58             boost::algorithm::trim(s);
59         }
60         std::vector<double> v(strs.size(), std::numeric_limits<double>::quiet_NaN());
61         for (size_t i = 0; i < v.size(); ++i)
62         {
63             v[i] = std::stod(strs[i]);
64         }
65         r.push_back(v[0]);
66         matched_holder.push_back(std::log2(v[1]));
67         linear.push_back(std::log2(v[2]));
68         quadratic_b_spline.push_back(std::log2(v[3]));
69         cubic_b_spline.push_back(std::log2(v[4]));
70         quintic_b_spline.push_back(std::log2(v[5]));
71         cubic_hermite.push_back(std::log2(v[6]));
72         pchip.push_back(std::log2(v[7]));
73         makima.push_back(std::log2(v[8]));
74         fotaylor.push_back(std::log2(v[9]));
75         if (v.size() > 10) {
76             quintic_hermite.push_back(std::log2(v[10]));
77             sotaylor.push_back(std::log2(v[11]));
78         }
79         if (v.size() > 12) {
80             totaylor.push_back(std::log2(v[12]));
81             septic_hermite.push_back(std::log2(v[13]));
82         }
83     }
84 
85     std::cout << std::fixed << std::setprecision(16);
86     auto q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, matched_holder);
87     assert(std::get<1>(q) < 0);
88     std::cout << "Matched Holder    : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
89 
90     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, linear);
91     assert(std::get<1>(q) < 0);
92     std::cout << "Linear            : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
93 
94     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, quadratic_b_spline);
95     assert(std::get<1>(q) < 0);
96     std::cout << "Quadratic B-spline: " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
97 
98     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, cubic_b_spline);
99     assert(std::get<1>(q) < 0);
100     std::cout << "Cubic B-spline    : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
101 
102     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, quintic_b_spline);
103     assert(std::get<1>(q) < 0);
104     std::cout << "Quintic B-spline  : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
105 
106     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, cubic_hermite);
107     assert(std::get<1>(q) < 0);
108     std::cout << "Cubic Hermite     : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
109 
110     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, pchip);
111     assert(std::get<1>(q) < 0);
112     std::cout << "PCHIP             : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
113 
114     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, makima);
115     assert(std::get<1>(q) < 0);
116     std::cout << "Makima            : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
117 
118     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, fotaylor);
119     assert(std::get<1>(q) < 0);
120     std::cout << "First-order Taylor: " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
121 
122     if (sotaylor.size() > 0)
123     {
124     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, quintic_hermite);
125     assert(std::get<1>(q) < 0);
126     std::cout << "Quintic Hermite   : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
127 
128     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, sotaylor);
129     assert(std::get<1>(q) < 0);
130     std::cout << "2nd order Taylor  : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
131 
132     }
133 
134     if (totaylor.size() > 0)
135     {
136     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, totaylor);
137     assert(std::get<1>(q) < 0);
138     std::cout << "3rd order Taylor  : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
139 
140     q  = boost::math::statistics::simple_ordinary_least_squares_with_R_squared(r, septic_hermite);
141     assert(std::get<1>(q) < 0);
142     std::cout << "Septic Hermite    : " << std::get<0>(q) << " - " << std::abs(std::get<1>(q)) << "r, R^2 = " << std::get<2>(q) << "\n";
143 
144     }
145 
146 }