misc_stat.cpp
Go to the documentation of this file.
#include <itpp/base/algebra/svd.h>
#include <itpp/stat/misc_stat.h>
namespace itpp
{
double mean(const vec &v)
{
return sum(v) / v.length();
}
std::complex<double> mean(const cvec &v)
{
return sum(v) / double(v.size());
}
double mean(const svec &v)
{
return (double)sum(v) / v.length();
}
double mean(const ivec &v)
{
return (double)sum(v) / v.length();
}
double mean(const mat &m)
{
return sum(sum(m)) / (m.rows()*m.cols());
}
std::complex<double> mean(const cmat &m)
{
return sum(sum(m)) / static_cast<std::complex<double> >(m.rows()*m.cols());
}
double mean(const smat &m)
{
return static_cast<double>(sum(sum(m))) / (m.rows()*m.cols());
}
double mean(const imat &m)
{
return static_cast<double>(sum(sum(m))) / (m.rows()*m.cols());
}
double norm(const cvec &v)
{
double E = 0.0;
for (int i = 0; i < v.length(); i++)
E += std::norm(v[i]);
return std::sqrt(E);
}
double norm(const cvec &v, int p)
{
double E = 0.0;
for (int i = 0; i < v.size(); i++)
E += std::pow(std::norm(v[i]), p / 2.0); // Yes, 2.0 is correct!
return std::pow(E, 1.0 / p);
}
double norm(const cvec &v, const std::string &)
{
return norm(v, 2);
}
/*
* Calculate the p-norm of a real matrix
* p = 1: max(svd(m))
* p = 2: max(sum(abs(X)))
*/
double norm(const mat &m, int p)
{
it_assert((p == 1) || (p == 2),
"norm(): Can only calculate a matrix norm of order 1 or 2");
if (p == 1)
return max(sum(abs(m)));
else
return max(svd(m));
}
/*
* Calculate the p-norm of a complex matrix
* p = 1: max(svd(m))
* p = 2: max(sum(abs(X)))
*/
double norm(const cmat &m, int p)
{
it_assert((p == 1) || (p == 2),
"norm(): Can only calculate a matrix norm of order 1 or 2");
if (p == 1)
return max(sum(abs(m)));
else
return max(svd(m));
}
// Calculate the Frobenius norm of matrix m for s = "fro"
double norm(const mat &m, const std::string &s)
{
it_assert(s == "fro", "norm(): Unrecognised norm");
double E = 0.0;
for (int r = 0; r < m.rows(); ++r) {
for (int c = 0; c < m.cols(); ++c) {
E += m(r, c) * m(r, c);
}
}
return std::sqrt(E);
}
// Calculate the Frobenius norm of matrix m for s = "fro"
double norm(const cmat &m, const std::string &s)
{
it_assert(s == "fro", "norm(): Unrecognised norm");
double E = 0.0;
for (int r = 0; r < m.rows(); ++r) {
for (int c = 0; c < m.cols(); ++c) {
E += std::norm(m(r, c));
}
}
return std::sqrt(E);
}
double variance(const cvec &v)
{
int len = v.size();
double sq_sum = 0.0;
std::complex<double> sum = 0.0;
const std::complex<double> *p = v._data();
for (int i = 0; i < len; i++, p++) {
sum += *p;
sq_sum += std::norm(*p);
}
return (double)(sq_sum - std::norm(sum) / len) / (len - 1);
}
double moment(const vec &x, const int r)
{
double m = mean(x), mr = 0;
int n = x.size();
double temp;
switch (r) {
case 1:
for (int j = 0; j < n; j++)
mr += (x(j) - m);
break;
case 2:
for (int j = 0; j < n; j++)
mr += (x(j) - m) * (x(j) - m);
break;
case 3:
for (int j = 0; j < n; j++)
mr += (x(j) - m) * (x(j) - m) * (x(j) - m);
break;
case 4:
for (int j = 0; j < n; j++) {
temp = (x(j) - m) * (x(j) - m);
temp *= temp;
mr += temp;
}
break;
default:
for (int j = 0; j < n; j++)
mr += std::pow(x(j) - m, double(r));
break;
}
return mr / n;
}
double skewness(const vec &x)
{
int n = x.size();
double k2 = variance(x) * n / (n - 1); // 2nd k-statistic
double k3 = moment(x, 3) * n * n / (n - 1) / (n - 2); //3rd k-statistic
return k3 / std::pow(k2, 3.0 / 2.0);
}
double kurtosisexcess(const vec &x)
{
int n = x.size();
double m2 = variance(x);
double m4 = moment(x, 4);
double k2 = m2 * n / (n - 1); // 2nd k-statistic
double k4 = (m4 * (n + 1) - 3 * (n - 1) * m2 * m2) * n * n / (n - 1) / (n - 2) / (n - 3); //4th k-statistic
return k4 / (k2*k2);
}
} // namespace itpp
itpp::Array
General array class.
Definition array.h:105
itpp::Array::size
int size() const
Returns the number of data elements in the array object.
Definition array.h:155
itpp::Array::length
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
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 on Mon Jun 10 2024 11:49:25 for IT++ by Doxygen 1.9.8