misc_stat.h
Go to the documentation of this file.
#ifndef MISC_STAT_H
#define MISC_STAT_H
#include <itpp/base/math/min_max.h>
#include <itpp/base/mat.h>
#include <itpp/base/math/elem_math.h>
#include <itpp/base/matfunc.h>
#include <itpp/itexports.h>
namespace itpp
{
class ITPP_EXPORT Stat
{
public:
Stat() {clear();}
virtual ~Stat() {}
virtual void clear() {
_n_overflows = 0;
_n_samples = 0;
_n_zeros = 0;
_max = 0.0;
_min = 0.0;
_sqr_sum = 0.0;
_sum = 0.0;
}
virtual void sample(const double s, const bool overflow = false) {
_n_samples++;
_sum += s;
_sqr_sum += s * s;
if (s < _min) _min = s;
if (s > _max) _max = s;
if (overflow) _n_overflows++;
if (s == 0.0) _n_zeros++;
}
int n_overflows() const {return _n_overflows;}
int n_samples() const {return _n_samples;}
int n_zeros() const {return _n_zeros;}
double avg() const {return _sum / _n_samples;}
double max() const {return _max;}
double min() const {return _min;}
double sigma() const {
double sigma2 = _sqr_sum / _n_samples - avg() * avg();
return std::sqrt(sigma2 < 0 ? 0 : sigma2);
}
double sqr_sum() const {return _sqr_sum;}
double sum() const {return _sum;}
vec histogram() const {return vec(0);}
protected:
int _n_overflows;
int _n_samples;
int _n_zeros;
double _max;
double _min;
double _sqr_sum;
double _sum;
};
ITPP_EXPORT double mean(const vec &v);
ITPP_EXPORT std::complex<double> mean(const cvec &v);
ITPP_EXPORT double mean(const svec &v);
ITPP_EXPORT double mean(const ivec &v);
ITPP_EXPORT double mean(const mat &m);
ITPP_EXPORT std::complex<double> mean(const cmat &m);
ITPP_EXPORT double mean(const smat &m);
ITPP_EXPORT double mean(const imat &m);
template<class T>
double geometric_mean(const Vec<T> &v)
{
return std::exp(std::log(static_cast<double>(prod(v))) / v.length());
}
template<class T>
double geometric_mean(const Mat<T> &m)
{
return std::exp(std::log(static_cast<double>(prod(prod(m))))
/ (m.rows() * m.cols()));
}
template<class T>
double median(const Vec<T> &v)
{
Vec<T> invect(v);
sort(invect);
return (double)(invect[(invect.length()-1)/2] + invect[invect.length()/2]) / 2.0;
}
ITPP_EXPORT double norm(const cvec &v);
template<class T>
double norm(const Vec<T> &v)
{
double E = 0.0;
for (int i = 0; i < v.size(); i++)
E += sqr(static_cast<double>(v[i]));
return std::sqrt(E);
}
ITPP_EXPORT double norm(const cvec &v, int p);
template<class T>
double norm(const Vec<T> &v, int p)
{
double E = 0.0;
for (int i = 0; i < v.size(); i++)
E += std::pow(fabs(static_cast<double>(v[i])), static_cast<double>(p));
return std::pow(E, 1.0 / p);
}
ITPP_EXPORT double norm(const cvec &v, const std::string &s);
template<class T>
double norm(const Vec<T> &v, const std::string &s)
{
it_assert(s == "fro", "norm(): Unrecognised norm");
double E = 0.0;
for (int i = 0; i < v.size(); i++)
E += sqr(static_cast<double>(v[i]));
return std::sqrt(E);
}
ITPP_EXPORT double norm(const mat &m, int p = 2);
ITPP_EXPORT double norm(const cmat &m, int p = 2);
ITPP_EXPORT double norm(const mat &m, const std::string &s);
ITPP_EXPORT double norm(const cmat &m, const std::string &s);
ITPP_EXPORT double variance(const cvec &v);
template<class T>
double variance(const Vec<T> &v)
{
int len = v.size();
const T *p = v._data();
double sum = 0.0, sq_sum = 0.0;
for (int i = 0; i < len; i++, p++) {
sum += *p;
sq_sum += *p * *p;
}
return (double)(sq_sum - sum*sum / len) / (len - 1);
}
template<class T>
double energy(const Vec<T> &v)
{
return sqr(norm(v));
}
inline bool within_tolerance(double x, double xref, double tol = 1e-14)
{
return (fabs(x -xref) <= tol) ? true : false;
}
inline bool within_tolerance(std::complex<double> x, std::complex<double> xref, double tol = 1e-14)
{
return (abs(x -xref) <= tol) ? true : false;
}
inline bool within_tolerance(const vec &x, const vec &xref, double tol = 1e-14)
{
return (max(abs(x -xref)) <= tol) ? true : false;
}
inline bool within_tolerance(const cvec &x, const cvec &xref, double tol = 1e-14)
{
return (max(abs(x -xref)) <= tol) ? true : false;
}
inline bool within_tolerance(const mat &X, const mat &Xref, double tol = 1e-14)
{
return (max(max(abs(X -Xref))) <= tol) ? true : false;
}
inline bool within_tolerance(const cmat &X, const cmat &Xref, double tol = 1e-14)
{
return (max(max(abs(X -Xref))) <= tol) ? true : false;
}
ITPP_EXPORT double moment(const vec &x, const int r);
ITPP_EXPORT double skewness(const vec &x);
ITPP_EXPORT double kurtosisexcess(const vec &x);
inline double kurtosis(const vec &x) {return kurtosisexcess(x) + 3;}
} // namespace itpp
#endif // #ifndef MISC_STAT_H
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
itpp::Stat
A class for sampling a signal and calculating statistics.
Definition misc_stat.h:49
itpp::Stat::_n_zeros
int _n_zeros
Number of zero samples.
Definition misc_stat.h:108
itpp::Stat::_n_overflows
int _n_overflows
Number of reported overflows.
Definition misc_stat.h:104
itpp::Stat::sample
virtual void sample(const double s, const bool overflow=false)
Register a sample and flag for overflow.
Definition misc_stat.h:68
itpp::Stat::_max
double _max
Maximum sample.
Definition misc_stat.h:110
itpp::Stat::sqr_sum
double sqr_sum() const
Squared sum of all samples.
Definition misc_stat.h:96
itpp::Stat::_sqr_sum
double _sqr_sum
Squared sum of all samples.
Definition misc_stat.h:114
itpp::Stat::avg
double avg() const
Average over all samples.
Definition misc_stat.h:85
itpp::Stat::histogram
vec histogram() const
Histogram over all samples (not implemented yet)
Definition misc_stat.h:100
itpp::Stat::n_zeros
int n_zeros() const
Number of zero samples.
Definition misc_stat.h:83
itpp::Stat::~Stat
virtual ~Stat()
Destructor.
Definition misc_stat.h:54
itpp::Stat::n_samples
int n_samples() const
Number of samples.
Definition misc_stat.h:81
itpp::Stat::sigma
double sigma() const
Standard deviation of all samples.
Definition misc_stat.h:91
itpp::Stat::_n_samples
int _n_samples
Number of samples.
Definition misc_stat.h:106
itpp::Stat::_sum
double _sum
Sum of all samples.
Definition misc_stat.h:116
itpp::Stat::_min
double _min
Minimum sample.
Definition misc_stat.h:112
itpp::Stat::clear
virtual void clear()
Clear statistics.
Definition misc_stat.h:57
itpp::Stat::Stat
Stat()
Default constructor.
Definition misc_stat.h:52
itpp::Stat::min
double min() const
Minimum sample.
Definition misc_stat.h:89
itpp::Stat::n_overflows
int n_overflows() const
Number of reported overflows.
Definition misc_stat.h:79
itpp::Stat::sum
double sum() const
Sum of all samples.
Definition misc_stat.h:98
itpp::Stat::max
double max() const
Maximum sample.
Definition misc_stat.h:87
elem_math.h
Elementary mathematical functions - header file.
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::prod
T prod(const Vec< T > &v)
The product of all elements in the vector.
Definition matfunc.h:195
itpp::max
T max(const Vec< T > &v)
Maximum value of vector.
Definition min_max.h:45
itpp::sqr
vec sqr(const cvec &data)
Absolute square of elements.
Definition elem_math.cpp:36
itpp::moment
double moment(const vec &x, const int r)
Calculate the central moment of vector x.
Definition misc_stat.cpp:174
itpp::energy
double energy(const Vec< T > &v)
Calculate the energy: squared 2-norm. energy(v)=sum(abs(v).^2)
Definition misc_stat.h:253
itpp::within_tolerance
bool within_tolerance(double x, double xref, double tol=1e-14)
Return true if the input value x is within the tolerance tol of the reference value xref.
Definition misc_stat.h:260
itpp::kurtosis
double kurtosis(const vec &x)
Calculate the kurtosis of the input vector x.
Definition misc_stat.h:379
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::geometric_mean
double geometric_mean(const Vec< T > &v)
The geometric mean of a vector.
Definition misc_stat.h:139
itpp::norm
double norm(const cvec &v)
Calculate the 2-norm: norm(v)=sqrt(sum(abs(v).^2))
Definition misc_stat.cpp:77
itpp::median
double median(const Vec< T > &v)
The median.
Definition misc_stat.h:154
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
mat.h
Matrix Class Definitions.
matfunc.h
Various functions on vectors and matrices - header file.
min_max.h
Minimum and maximum functions on vectors and matrices.
itpp
itpp namespace
Definition itmex.h:37
itpp::abs
bin abs(const bin &inbin)
absolute value of bin
Definition binary.h:174

Generated on Mon Jun 10 2024 11:49:25 for IT++ by Doxygen 1.9.8