ACT-CV - Machine Vision for Cognitive Modeling
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
general
statistiken.h
Go to the documentation of this file.
1
// -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2
3
/*
4
ACT-CV - Machine Vision for Cognitive Modeling
5
Copyright (c) 2008 Marc Halbruegge
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
*/
21
22
34
#ifndef _STATISTIKEN_H
35
#define _STATISTIKEN_H
36
#include <stdexcept>
37
#include <vector>
38
#include <algorithm>
39
#include <cmath>
40
42
class
statistics_exn
:
public
std::runtime_error{
43
public
:
44
statistics_exn
() : std::runtime_error(
"Statistics"
) {
45
}
46
};
47
53
template
<
class
_t>
54
void
InitZero
(_t &x) {
55
x =
static_cast<
_t
>
(0);
56
};
57
58
59
61
template
<
class
_t>
class
Mean
{
62
protected
:
63
public
:
64
_t
summe
;
65
int
n
;
66
67
Mean
() {
68
init
();
69
}
70
void
init
() {
71
InitZero
(
summe
);
72
n
=0;
73
}
74
void
add
(_t d){
75
summe
+=d;
76
n
++;
77
}
78
int
get_num_cases
()
const
{
79
return
n
;
80
}
81
_t
get_mean
()
const
{
82
if
(
n
< 1)
throw
statistics_exn
();
83
return
summe
/
n
;
84
}
85
};
86
88
template
<
class
_t>
class
MeanMinMax
{
89
protected
:
90
public
:
91
_t
summe
;
92
_t
minimum
,
maximum
;
93
int
n
;
94
95
MeanMinMax
() {
96
init
();
97
}
98
void
init
() {
99
InitZero
(
summe
);
100
n
=0;
101
}
102
void
add
(_t d){
103
summe
+=d;
104
if
(
n
==0) {
105
minimum
=d;
106
maximum
=d;
107
}
else
{
108
if
(d<
minimum
)
minimum
=d;
109
if
(d>
maximum
)
maximum
=d;
110
}
111
n
++;
112
}
113
int
get_num_cases
()
const
{
114
return
n
;
115
}
116
_t
get_mean
()
const
{
117
if
(
n
< 1)
throw
statistics_exn
();
118
return
summe
/
n
;
119
}
120
_t
get_min
()
const
{
121
return
minimum
;
122
}
123
_t
get_max
()
const
{
124
return
maximum
;
125
}
126
};
127
134
template
<
class
_t>
class
MeanZero
:
public
Mean
<_t> {
135
};
136
142
template
<
class
_t>
143
class
SquareOperation
{
144
public
:
145
typedef
_t
sq_t
;
146
147
static
sq_t
square
(
const
_t &x) {
148
return
x*x;
149
}
150
};
151
152
153
162
template
<
class
_t>
class
Var
:
public
Mean
<_t>,
public
SquareOperation
<_t> {
163
protected
:
164
public
:
165
typename
SquareOperation<_t>::sq_t
summeQ
;
166
167
Var
() {
168
init
();
169
}
170
171
void
init
() {
172
Mean<_t>::init
();
173
InitZero
(
summeQ
);
174
}
175
176
void
add
(_t d) {
177
Mean<_t>::add
(d);
178
summeQ
+=
square
(d);
179
}
180
181
typename
SquareOperation<_t>::sq_t
get_var
()
const
{
182
#if 0
183
if
((
Mean<_t>::n
<=1)||((
summeQ
-
Mean<_t>::summe
*
Mean<_t>::summe
/
Mean<_t>::n
)<(_t)0))
184
throw
statistics_exn
();
185
#else
186
if
(
Mean<_t>::n
<=1)
throw
statistics_exn
();
187
#endif
188
return
(
summeQ
-
square
(
Mean<_t>::summe
)/
Mean<_t>::n
)/(
Mean<_t>::n
-1);
189
}
190
};
191
195
template
<
class
_t>
class
Corr
{
196
protected
:
197
Var<_t>
x
,
y
;
198
_t
summeXY
;
199
int
n
;
200
public
:
201
Corr
() {
202
init
();
203
}
204
void
init
() {
205
InitZero
(
summeXY
);
206
x
.init();
207
y
.init();
208
n
=0;
209
}
210
void
add
(_t a, _t b){
211
summeXY
+=a*b;
212
x
.add(a);
213
y
.add(b);
214
n
++;
215
}
216
int
get_num_cases
()
const
{
217
return
n
;
218
}
219
_t
get_squared_corr
()
const
{
220
if
( (
x
.n < 2) ||
221
(
x
.get_var() <= (_t)0) ||
222
(
y
.get_var() <= (_t)0)) {
223
throw
statistics_exn
();
224
}
225
226
return
(
summeXY
-
x
.summe*
y
.summe/
x
.n)*(
summeXY
-
x
.summe*
y
.summe/
x
.n)/
227
((
x
.summeQ-
x
.summe*
x
.summe/
x
.n)*(
y
.summeQ-
y
.summe*
y
.summe/
y
.n));
228
}
229
};
230
232
template
<
class
_t>
class
Regr
:
public
Corr
<_t> {
233
public
:
234
_t
get_y
(_t xw) {
235
return
xw*
get_b_yx
()+
get_a_yx
();
236
}
237
_t
get_a_yx
() {
238
//Y-Achsenabschnitt
239
return
Corr<_t>::y
.get_mean()-
get_b_yx
()*
Corr<_t>::x
.get_mean();
240
}
241
_t
get_b_yx
() {
242
//Steigung
243
if
(
Corr<_t>::x
.get_var()<=0.0)
throw
statistics_exn
();
244
return
(
Corr<_t>::summeXY
-
Corr<_t>::x
.summe*
Corr<_t>::y
.summe/
Corr<_t>::x
.
n
)/
245
(
Corr<_t>::x
.summeQ-
Corr<_t>::x
.summe*
Corr<_t>::x
.summe/
Corr<_t>::x
.
n
);
246
}
247
_t
get_b
(
int
i) {
248
switch
(i) {
249
case
0:
return
get_a_yx
();
250
case
1:
return
get_b_yx
();
251
default
:
throw
statistics_exn
();
252
}
253
}
254
};
255
256
260
class
TrimmedMean
{
261
std::vector<double>
data
;
262
Var<double>
avg
;
263
public
:
264
void
Init
() {
265
data
.clear();
266
}
267
void
Add
(
double
d) {
268
data
.push_back(d);
269
}
271
void
Compute
(
double
margin=.2) {
272
std::sort(
data
.begin(),
data
.end());
273
avg
.
init
();
274
for
(
unsigned
int
i=static_cast<unsigned int>(
data
.size()*margin);
275
i < static_cast<unsigned int>(
data
.size()*(1.0-margin));i++) {
276
avg
.
add
(
data
[i]);
277
}
278
}
280
double
GetMean
()
const
{
281
return
avg
.
get_mean
();
282
}
284
double
GetVar
()
const
{
285
return
avg
.
get_var
();
286
}
288
double
GetStDev
()
const
{
289
return
sqrt(
GetVar
());
290
}
291
unsigned
int
GetNumCases
()
const
{
292
return
avg
.
get_num_cases
();
293
}
294
};
295
296
297
298
#endif //_STATISTIKEN_H
299
300
301
302
303
304
305
306
307
308
309
310
311
ACT-CV - Machine Vision for Cognitive Modeling
© 2015
Marc Halbruegge
(actcvlibrary@googlemail.com)