-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (172 loc) · 7.24 KB
/
main.cpp
File metadata and controls
189 lines (172 loc) · 7.24 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
#include <algorithm>
#include <cassert>
#include <iostream>
#include <cmath>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <boost/units/systems/si.hpp>
#include "simplelinearregression.h"
#include "drawcanvas.h"
#pragma GCC diagnostic pop
int main()
{
//Let Boost.Units check for compiling
{
typedef boost::units::quantity<boost::units::si::time> Time;
typedef boost::units::quantity<boost::units::si::length> Distance;
typedef boost::units::quantity<boost::units::si::velocity> Velocity;
const std::vector<Time> xs
=
{
Time(1.0 * boost::units::si::second),
Time(2.0 * boost::units::si::second),
Time(3.0 * boost::units::si::second)
};
const std::vector<Distance> ys
=
{
Distance(2.0 * boost::units::si::meter),
Distance(3.0 * boost::units::si::meter),
Distance(4.0 * boost::units::si::meter)
};
const Velocity expected_slope(1.0 * boost::units::si::meter / boost::units::si::second);
const Distance expected_offset(1.0 * boost::units::si::meter);
const std::pair<Velocity,Distance> p
= ribi::SimpleLinearRegression::CalculateBestFit(xs,ys);
const Velocity slope(p.first);
const Distance offset(p.second);
assert(std::abs( Velocity(slope - expected_slope).value() )
< Velocity(0.0001 * boost::units::si::meter / boost::units::si::second).value() );
assert(std::abs( Distance(offset - expected_offset).value() )
< Distance(0.0001 * boost::units::si::meter).value() );
}
for (int i=1; i!=5; ++i) //Human-based counting, following the Ansombe's Quartet indices
{
const std::vector<double> xs
= ribi::SimpleLinearRegression::GetAnscombesQuartetX(i);
const std::vector<double> ys
= ribi::SimpleLinearRegression::GetAnscombesQuartetY(i);
const std::pair<double,double> p
= ribi::SimpleLinearRegression::CalculateBestFit(xs,ys);
const double expected_regression_line_a = 0.500; //to 3 decimal places
const double expected_regression_line_b = 3.00; //to 2 decimal places
const double e = 0.01;
assert(expected_regression_line_a - e < p.first && expected_regression_line_a + e > p.first);
assert(expected_regression_line_b - e < p.second && expected_regression_line_b + e > p.second);
//Plot the values
const int max_x = 78;
const int max_y = 20;
ribi::DrawCanvas c(max_x,max_y,ribi::CanvasColorSystem::invert,ribi::CanvasCoordinatSystem::graph);
c.DrawLine(0.0,0.0,max_x,0.0);
c.DrawLine(0.0,0.0,0.0,max_y);
assert(xs.size() == ys.size());
const double ray = 1.0;
const double max_x_co = ray + *std::max_element(xs.begin(),xs.end());
const double stretch_x = static_cast<double>(max_x) / max_x_co;
const double max_y_co = ray + *std::max_element(ys.begin(),ys.end());
const double stretch_y = static_cast<double>(max_y) / max_y_co;
const std::size_t n_values = xs.size();
for (std::size_t j=0; j!=n_values; ++j)
{
//c.DrawDot(xs[i] * stretch_x,ys[i] * stretch_y);
c.DrawCircle(xs[j] * stretch_x,ys[j] * stretch_y,ray);
}
c.DrawLine(
stretch_x * 0.0,
stretch_y * p.second,
stretch_x * static_cast<double>(max_x),
stretch_y * (p.second + static_cast<double>(max_x) * p.first)
);
std::cout << c << std::endl;
}
}
/* Screen output
TRACE '"Starting SimpleLinearRegression::Test"' line 155 in fi
le '../ToolTestSimpleLinearRegression/tooltestsimplelinearregressionmaindialog.c
pp': 'Starting SimpleLinearRegression::Test'
TRACE '"Finished SimpleLinearRegression::Test successfully"' l
ine 200 in file '../ToolTestSimpleLinearRegression/tooltestsimplelinearregressio
nmaindialog.cpp': 'Finished SimpleLinearRegression::Test succe
ssfully'
. -o.
: s-+ ` `
: +o: /hmooso
: . ` `-:+osmdm:`
: :os` .- .-:+os+/:.` :-
: :+s` `os:-+MMN+/-.` .
: -/` ` //:/hNNy//ho: /oo
: `o/: +hd/oso+:--o+` `. +:s
: `oo- `.:/osMhN-` `/.
: `oo.`-/ooso/:-` `+.
: `-smNdo/:.` `/
: .-/omdo//so` s/+
: .-/+so+/-o+s` oo/
: `.:/+so+:-. -oo` .
hoso+:-` `
o`
:
:
:
s::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::.
` `` `- . `.:/o+/-.`
- -. .++. `o/: +o+-:+do:-`
- . :/o` .++. `oo+-/Mdd:.o/o `
- /+o :+o` -/-:/sy/-.-/` //o -++`
- `: /:o ..-/+o+:-` - ::o`
- o:/ /::/+o/:.` :-
- `. oos:+o+/-.
- `sso/+os:-`
- `.-/sMds.`
- `-:/shy:.``-
- .-/+o+:-`-+/.
- `.:/+o/:.` .++.
- `-:+o+/-. -` ``
o/+o+:-` :/o`
s.` :+o`
- .`
-
-
-
o----------------------------------------------------------------------------`
. -s/
: +.o
: .s:
:
: `.-:/
: `.-:+oho/:-.
: `.:o+os+/:-./+o`
: .-.:/dNy+/dyo :/o`
: . -ys/+hMNo::y/: s:+ :.
: . -o-.-ymMoodyd:`-++- +o. `/
: ` `//` `syo:/MdN+/hoy -o+ --
: -+o``/yhsoyMdy-.oo: /`
: `.-ydMy+odh:` .: `
: `.-/+oo+/::+-
: `.:/+oo+/:-.
do+/:.`
:
:
:
s::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::.
.-``.
` ``-/:-``
` ``.-..``..
` `..-..`
` `....``
` ` ``....``
` //. ``....`
` `hy/ `..-.``
` `dm+``..-.``
` `-MMy.``
` ``.-..-NN/
` `..-.`` `hy/
` ``....`` `oo:
` ``.-..` `.
` `..-..`
:.``
`
`
`
-````````````````````````````````````````````````````````````````````````````
Press <RETURN> to close this window...
*/