IT++
4.3.1
Toggle main menu visibility
itpp
stat
misc_stat.cpp
Go to the documentation of this file.
1
28
29
#include <
itpp/base/algebra/svd.h
>
30
#include <
itpp/stat/misc_stat.h
>
31
32
33
namespace
itpp
34
{
35
36
double
mean
(
const
vec &v)
37
{
38
return
sum
(v) / v.length();
39
}
40
41
std::complex<double>
mean
(
const
cvec &v)
42
{
43
return
sum
(v) / double(v.size());
44
}
45
46
double
mean
(
const
svec &v)
47
{
48
return
(
double
)
sum
(v) / v.length();
49
}
50
51
double
mean
(
const
ivec &v)
52
{
53
return
(
double
)
sum
(v) / v.length();
54
}
55
56
double
mean
(
const
mat &m)
57
{
58
return
sum
(
sum
(m)) / (m.rows()*m.cols());
59
}
60
61
std::complex<double>
mean
(
const
cmat &m)
62
{
63
return
sum
(
sum
(m)) /
static_cast<
std::complex<double>
>
(m.rows()*m.cols());
64
}
65
66
double
mean
(
const
smat &m)
67
{
68
return
static_cast<
double
>
(
sum
(
sum
(m))) / (m.rows()*m.cols());
69
}
70
71
double
mean
(
const
imat &m)
72
{
73
return
static_cast<
double
>
(
sum
(
sum
(m))) / (m.rows()*m.cols());
74
}
75
76
77
double
norm
(
const
cvec &v)
78
{
79
double
E = 0.0;
80
for
(
int
i = 0; i < v.length(); i++)
81
E += std::norm(v[i]);
82
83
return
std::sqrt(E);
84
}
85
86
double
norm
(
const
cvec &v,
int
p)
87
{
88
double
E = 0.0;
89
for
(
int
i = 0; i < v.size(); i++)
90
E += std::pow(std::norm(v[i]), p / 2.0);
// Yes, 2.0 is correct!
91
92
return
std::pow(E, 1.0 / p);
93
}
94
95
double
norm
(
const
cvec &v,
const
std::string &)
96
{
97
return
norm
(v, 2);
98
}
99
100
/*
101
* Calculate the p-norm of a real matrix
102
* p = 1: max(svd(m))
103
* p = 2: max(sum(abs(X)))
104
*/
105
double
norm
(
const
mat &m,
int
p)
106
{
107
it_assert
((p == 1) || (p == 2),
108
"norm(): Can only calculate a matrix norm of order 1 or 2"
);
109
110
if
(p == 1)
111
return
max
(
sum
(
abs
(m)));
112
else
113
return
max
(
svd
(m));
114
}
115
116
/*
117
* Calculate the p-norm of a complex matrix
118
* p = 1: max(svd(m))
119
* p = 2: max(sum(abs(X)))
120
*/
121
double
norm
(
const
cmat &m,
int
p)
122
{
123
it_assert
((p == 1) || (p == 2),
124
"norm(): Can only calculate a matrix norm of order 1 or 2"
);
125
126
if
(p == 1)
127
return
max
(
sum
(
abs
(m)));
128
else
129
return
max
(
svd
(m));
130
}
131
132
// Calculate the Frobenius norm of matrix m for s = "fro"
133
double
norm
(
const
mat &m,
const
std::string &s)
134
{
135
it_assert
(s ==
"fro"
,
"norm(): Unrecognised norm"
);
136
double
E = 0.0;
137
for
(
int
r = 0; r < m.rows(); ++r) {
138
for
(
int
c = 0; c < m.cols(); ++c) {
139
E += m(r, c) * m(r, c);
140
}
141
}
142
return
std::sqrt(E);
143
}
144
145
// Calculate the Frobenius norm of matrix m for s = "fro"
146
double
norm
(
const
cmat &m,
const
std::string &s)
147
{
148
it_assert
(s ==
"fro"
,
"norm(): Unrecognised norm"
);
149
double
E = 0.0;
150
for
(
int
r = 0; r < m.rows(); ++r) {
151
for
(
int
c = 0; c < m.cols(); ++c) {
152
E += std::norm(m(r, c));
153
}
154
}
155
return
std::sqrt(E);
156
}
157
158
159
double
variance
(
const
cvec &v)
160
{
161
int
len = v.size();
162
double
sq_sum = 0.0;
163
std::complex<double>
sum
= 0.0;
164
const
std::complex<double> *p = v._data();
165
166
for
(
int
i = 0; i < len; i++, p++) {
167
sum
+= *p;
168
sq_sum += std::norm(*p);
169
}
170
171
return
(
double
)(sq_sum - std::norm(
sum
) / len) / (len - 1);
172
}
173
174
double
moment
(
const
vec &x,
const
int
r)
175
{
176
double
m =
mean
(x), mr = 0;
177
int
n = x.size();
178
double
temp;
179
180
switch
(r) {
181
case
1:
182
for
(
int
j = 0; j < n; j++)
183
mr += (x(j) - m);
184
break
;
185
case
2:
186
for
(
int
j = 0; j < n; j++)
187
mr += (x(j) - m) * (x(j) - m);
188
break
;
189
case
3:
190
for
(
int
j = 0; j < n; j++)
191
mr += (x(j) - m) * (x(j) - m) * (x(j) - m);
192
break
;
193
case
4:
194
for
(
int
j = 0; j < n; j++) {
195
temp = (x(j) - m) * (x(j) - m);
196
temp *= temp;
197
mr += temp;
198
}
199
break
;
200
default
:
201
for
(
int
j = 0; j < n; j++)
202
mr += std::pow(x(j) - m,
double
(r));
203
break
;
204
}
205
206
return
mr / n;
207
}
208
209
210
double
skewness
(
const
vec &x)
211
{
212
int
n = x.size();
213
214
double
k2 =
variance
(x) * n / (n - 1);
// 2nd k-statistic
215
double
k3 =
moment
(x, 3) * n * n / (n - 1) / (n - 2);
//3rd k-statistic
216
217
return
k3 / std::pow(k2, 3.0 / 2.0);
218
}
219
220
double
kurtosisexcess
(
const
vec &x)
221
{
222
int
n = x.size();
223
double
m2 =
variance
(x);
224
double
m4 =
moment
(x, 4);
225
226
double
k2 = m2 * n / (n - 1);
// 2nd k-statistic
227
double
k4 = (m4 * (n + 1) - 3 * (n - 1) * m2 * m2) * n * n / (n - 1) / (n - 2) / (n - 3);
//4th k-statistic
228
229
return
k4 / (k2*k2);
230
}
231
232
}
// namespace itpp
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition
itassert.h:94
itpp::sum
T sum(const Vec< T > &v)
Sum of all elements in the vector.
Definition
matfunc.h:59
itpp::svd
bool svd(const mat &A, vec &S)
Get singular values s of a real matrix A using SVD.
Definition
svd.cpp:185
itpp::max
T max(const Vec< T > &v)
Maximum value of vector.
Definition
min_max.h:45
itpp::moment
double moment(const vec &x, const int r)
Calculate the central moment of vector x.
Definition
misc_stat.cpp:174
itpp::skewness
double skewness(const vec &x)
Calculate the skewness excess of the input vector x.
Definition
misc_stat.cpp:210
itpp::kurtosisexcess
double kurtosisexcess(const vec &x)
Calculate the kurtosis excess of the input vector x.
Definition
misc_stat.cpp:220
itpp::norm
double norm(const cvec &v)
Calculate the 2-norm: norm(v)=sqrt(sum(abs(v).^2)).
Definition
misc_stat.cpp:77
itpp::variance
double variance(const cvec &v)
The variance of the elements in the vector. Normalized with N-1 to be unbiased.
Definition
misc_stat.cpp:159
itpp::mean
double mean(const vec &v)
The mean value.
Definition
misc_stat.cpp:36
misc_stat.h
Miscellaneous statistics functions and classes - header file.
itpp
itpp namespace
Definition
itmex.h:37
itpp::abs
bin abs(const bin &inbin)
absolute value of bin
Definition
binary.h:174
svd.h
Definitions of Singular Value Decompositions.
Generated by
1.17.0