twoDimensionalStatistics.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
twoDimensionalStatistics.cc
Go to the documentation of this file.
1 /* TwoDimensionalStatistics.cc
2 */
3 #include "osl/stat/twoDimensionalStatistics.h"
4 #include "osl/stat/average.h"
5 #include <cmath>
6
7 class osl::stat::TwoDimensionalStatistics::Data
8 {
9 public:
10 Average m_x, m_y, m_x2, m_y2, m_xy;
11
12 // MANIPULATORS
13 void add(const double& x, const double& y)
14 {
15 m_x.add(x);
16 m_y.add(y);
17 m_x2.add(x*x);
18 m_y2.add(y*y);
19 m_xy.add(x*y);
20 }
21 void merge(const Data& r)
22 {
23 m_x.merge(r.m_x);
24 m_y.merge(r.m_y);
25 m_x2.merge(r.m_x2);
26 m_y2.merge(r.m_y2);
27 m_xy.merge(r.m_xy);
28 }
29 void clear()
30 {
31 m_x.clear();
32 m_y.clear();
33 m_x2.clear();
34 m_y2.clear();
35 m_xy.clear();
36 }
37 };
38
39 // CREATORS
40 osl::stat::TwoDimensionalStatistics::
41 TwoDimensionalStatistics() : m_data(new Data())
42 {
43 }
44
45 osl::stat::TwoDimensionalStatistics::
46 ~TwoDimensionalStatistics()
47 {
48 }
49
50 // MANIPULATORS
51 void osl::stat::TwoDimensionalStatistics::
52 add(const double& x, const double& y)
53 {
54 m_data->add(x,y);
55 }
56
57 void osl::stat::TwoDimensionalStatistics::
58 merge(const TwoDimensionalStatistics& r)
59 {
60 m_data->merge(*r.m_data);
61 }
62
63 void osl::stat::TwoDimensionalStatistics::
64 clear()
65 {
66 m_data->clear();
67 }
68
69
70 // ACCESSORS
71 size_t osl::stat::TwoDimensionalStatistics::
72 size() const
73 {
74 return m_data->m_x.numElements();
75 }
76
77 double osl::stat::TwoDimensionalStatistics::
78 averageX() const
79 {
80 return m_data->m_x.getAverage();
81 }
82
83 double osl::stat::TwoDimensionalStatistics::
84 averageY() const
85 {
86 return m_data->m_y.getAverage();
87 }
88
89 double osl::stat::TwoDimensionalStatistics::
90 averageX2() const
91 {
92 return m_data->m_x2.getAverage();
93 }
94
95 double osl::stat::TwoDimensionalStatistics::
96 averageY2() const
97 {
98 return m_data->m_y2.getAverage();
99 }
100
101 double osl::stat::TwoDimensionalStatistics::
102 averageXY() const
103 {
104 return m_data->m_xy.getAverage();
105 }
106
107 double osl::stat::TwoDimensionalStatistics::
108 meanSquaredErrors() const
109 {
110 return averageX2() - 2*averageXY() + averageY2();
111 }
112
113 double osl::stat::TwoDimensionalStatistics::
114 meanSquaredErrorsAdjustConstant() const
115 {
116 return averageX2() - averageX()*averageX()
117 -2*(averageXY() - averageX()*averageY())
118 + averageY2() - averageY()*averageY();
119 }
120
121 double osl::stat::TwoDimensionalStatistics::
122 correlation() const
123 {
124 return (averageXY() - averageX()*averageY())
125 / sqrt((averageX2() - averageX()*averageX())
126 * (averageY2() - averageY()*averageY()));
127 }
128
129 void osl::stat::TwoDimensionalStatistics::
130 fitting(double &a, double &b, double &residual) const
131 {
132 double d = averageX2() - averageX()*averageX();
133 double n = averageXY() - averageX()*averageY();
134 if (std::abs(d) < 1e-8) {
135 a = 0.0;
136 b = averageY();
137 residual = averageY2() - averageY()*averageY();
138 return;
139 }
140 a = n / d;
141 b = (averageX2()*averageY() - averageXY()*averageX()) / d;
142 residual = averageY2()-averageY()*averageY()
143 - n * n / d;
144 }
145
146 /* ------------------------------------------------------------------------- */
147 // ;;; Local Variables:
148 // ;;; mode:c++
149 // ;;; c-basic-offset:2
150 // ;;; End: