TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
statistics::stats_computer1< T > Class Template Reference
Collaboration diagram for statistics::stats_computer1< T >:
[legend]

Public Member Functions

void new_val (T x)
 
double mean () const
 
double variance () const
 
double std () const
 

Private Attributes

unsigned int n = 0
 
double Ex
 
double Ex2
 
K
 

Friends

std::istream & operator>> (std::istream &input, stats_computer1 &stat)
 

Detailed Description

template<typename T>
class statistics::stats_computer1< T >

continuous mean and variance computance using 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.

Definition at line 27 of file realtime_stats.cpp.

Member Function Documentation

◆ mean()

template<typename T >
double statistics::stats_computer1< T >::mean ( ) const
inline

return sample mean computed till last sample

Definition at line 42 of file realtime_stats.cpp.

42{ return K + Ex / n; }

◆ new_val()

template<typename T >
void statistics::stats_computer1< T >::new_val ( T x)
inline

Constructor

Parameters
[in]xnew data sample

Definition at line 32 of file realtime_stats.cpp.

32 {
33 if (n == 0)
34 K = x;
35 n++;
36 T tmp = x - K;
37 Ex += tmp;
38 Ex2 += static_cast<double>(tmp) * tmp;
39 }

◆ std()

template<typename T >
double statistics::stats_computer1< T >::std ( ) const
inline

return sample standard deviation computed till last sample

Definition at line 48 of file realtime_stats.cpp.

48{ return std::sqrt(this->variance()); }

◆ variance()

template<typename T >
double statistics::stats_computer1< T >::variance ( ) const
inline

return data variance computed till last sample

Definition at line 45 of file realtime_stats.cpp.

45{ return (Ex2 - (Ex * Ex) / n) / (n - 1); }

Friends And Related Symbol Documentation

◆ operator>>

template<typename T >
std::istream & operator>> ( std::istream & input,
stats_computer1< T > & stat )
friend

short-hand operator to read new sample from input stream
e.g.: std::cin >> stats1;

Definition at line 53 of file realtime_stats.cpp.

54 {
55 T val;
56 input >> val;
57 stat.new_val(val);
58 return input;
59 }

Member Data Documentation

◆ Ex

template<typename T >
double statistics::stats_computer1< T >::Ex
private

Definition at line 63 of file realtime_stats.cpp.

◆ Ex2

template<typename T >
double statistics::stats_computer1< T >::Ex2
private

Definition at line 63 of file realtime_stats.cpp.

◆ K

template<typename T >
T statistics::stats_computer1< T >::K
private

Definition at line 64 of file realtime_stats.cpp.

◆ n

template<typename T >
unsigned int statistics::stats_computer1< T >::n = 0
private

Definition at line 62 of file realtime_stats.cpp.


The documentation for this class was generated from the following file: