mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
add namespace - statistics
This commit is contained in:
parent
579620271f
commit
1125c85b3f
@ -11,94 +11,105 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* continuous mean and variance computance using
|
* \namespace statistics
|
||||||
* first value as an approximation for the mean.
|
* \brief Statistical algorithms
|
||||||
* If the first number is much far form the mean, the algorithm becomes very
|
|
||||||
* inaccurate to compute variance and standard deviation.
|
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
namespace statistics {
|
||||||
class stats_computer1 {
|
|
||||||
public:
|
/**
|
||||||
/** Constructor
|
* continuous mean and variance computance using
|
||||||
* \param[in] x new data sample
|
* first value as an approximation for the mean.
|
||||||
|
* If the first number is much far form the mean, the algorithm becomes very
|
||||||
|
* inaccurate to compute variance and standard deviation.
|
||||||
*/
|
*/
|
||||||
void new_val(T x) {
|
template <typename T>
|
||||||
if (n == 0)
|
class stats_computer1 {
|
||||||
K = x;
|
public:
|
||||||
n++;
|
/** Constructor
|
||||||
T tmp = x - K;
|
* \param[in] x new data sample
|
||||||
Ex += tmp;
|
*/
|
||||||
Ex2 += tmp * tmp;
|
void new_val(T x) {
|
||||||
}
|
if (n == 0)
|
||||||
|
K = x;
|
||||||
|
n++;
|
||||||
|
T tmp = x - K;
|
||||||
|
Ex += tmp;
|
||||||
|
Ex2 += tmp * tmp;
|
||||||
|
}
|
||||||
|
|
||||||
/** return sample mean computed till last sample */
|
/** return sample mean computed till last sample */
|
||||||
double mean() const { return K + Ex / n; }
|
double mean() const { return K + Ex / n; }
|
||||||
|
|
||||||
/** return data variance computed till last sample */
|
/** return data variance computed till last sample */
|
||||||
double variance() const { return (Ex2 - (Ex * Ex) / n) / (n - 1); }
|
double variance() const { return (Ex2 - (Ex * Ex) / n) / (n - 1); }
|
||||||
|
|
||||||
/** return sample standard deviation computed till last sample */
|
/** return sample standard deviation computed till last sample */
|
||||||
double std() const { return std::sqrt(this->variance()); }
|
double std() const { return std::sqrt(this->variance()); }
|
||||||
|
|
||||||
/** short-hand operator to read new sample from input stream
|
/** short-hand operator to read new sample from input stream
|
||||||
* \n e.g.: `std::cin >> stats1;`
|
* \n e.g.: `std::cin >> stats1;`
|
||||||
|
*/
|
||||||
|
friend std::istream &operator>>(std::istream &input,
|
||||||
|
stats_computer1 &stat) {
|
||||||
|
T val;
|
||||||
|
input >> val;
|
||||||
|
stat.new_val(val);
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
unsigned int n = 0;
|
||||||
|
double Ex, Ex2;
|
||||||
|
T K;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* continuous mean and variance computance using
|
||||||
|
* Welford's algorithm (very accurate)
|
||||||
*/
|
*/
|
||||||
friend std::istream &operator>>(std::istream &input,
|
template <typename T>
|
||||||
stats_computer1 &stat) {
|
class stats_computer2 {
|
||||||
T val;
|
public:
|
||||||
input >> val;
|
/** Constructor
|
||||||
stat.new_val(val);
|
* \param[in] x new data sample
|
||||||
return input;
|
*/
|
||||||
}
|
void new_val(T x) {
|
||||||
|
n++;
|
||||||
|
double delta = x - mu;
|
||||||
|
mu += delta / n;
|
||||||
|
double delta2 = x - mu;
|
||||||
|
M += delta * delta2;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
/** return sample mean computed till last sample */
|
||||||
unsigned int n = 0;
|
double mean() const { return mu; }
|
||||||
double Ex, Ex2;
|
|
||||||
T K;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/** return data variance computed till last sample */
|
||||||
* continuous mean and variance computance using
|
double variance() const { return M / n; }
|
||||||
* Welford's algorithm (very accurate)
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
class stats_computer2 {
|
|
||||||
public:
|
|
||||||
/** Constructor
|
|
||||||
* \param[in] x new data sample
|
|
||||||
*/
|
|
||||||
void new_val(T x) {
|
|
||||||
n++;
|
|
||||||
double delta = x - mu;
|
|
||||||
mu += delta / n;
|
|
||||||
double delta2 = x - mu;
|
|
||||||
M += delta * delta2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** return sample mean computed till last sample */
|
/** return sample standard deviation computed till last sample */
|
||||||
double mean() const { return mu; }
|
double std() const { return std::sqrt(this->variance()); }
|
||||||
|
|
||||||
/** return data variance computed till last sample */
|
/** short-hand operator to read new sample from input stream
|
||||||
double variance() const { return M / n; }
|
* \n e.g.: `std::cin >> stats1;`
|
||||||
|
*/
|
||||||
|
friend std::istream &operator>>(std::istream &input,
|
||||||
|
stats_computer2 &stat) {
|
||||||
|
T val;
|
||||||
|
input >> val;
|
||||||
|
stat.new_val(val);
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
/** return sample standard deviation computed till last sample */
|
private:
|
||||||
double std() const { return std::sqrt(this->variance()); }
|
unsigned int n = 0;
|
||||||
|
double mu = 0, var = 0, M = 0;
|
||||||
|
};
|
||||||
|
|
||||||
/** short-hand operator to read new sample from input stream
|
} // namespace statistics
|
||||||
* \n e.g.: `std::cin >> stats1;`
|
|
||||||
*/
|
|
||||||
friend std::istream &operator>>(std::istream &input,
|
|
||||||
stats_computer2 &stat) {
|
|
||||||
T val;
|
|
||||||
input >> val;
|
|
||||||
stat.new_val(val);
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
using statistics::stats_computer1;
|
||||||
unsigned int n = 0;
|
using statistics::stats_computer2;
|
||||||
double mu = 0, var = 0, M = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Test the algorithm implementation
|
/** Test the algorithm implementation
|
||||||
* \param[in] test_data array of data to test the algorithms
|
* \param[in] test_data array of data to test the algorithms
|
||||||
|
Loading…
x
Reference in New Issue
Block a user