Theoretica
A C++ numerical and automatic mathematical library
theoretica::histogram Class Reference

Histogram class with running statistics, can be constructed from the parameters of the bins or from a dataset. More...

#include <histogram.h>

Public Member Functions

 histogram (unsigned int bin_count, real range_min, real range_max)
 Construct the histogram from the number of bins and the range. More...
 
template<typename Dataset , enable_vector< Dataset > = true>
 histogram (const Dataset &data, unsigned int bin_count=0)
 Construct the histogram from a set of data points, with the given number of bins. More...
 
void insert (real x)
 Insert a new data point inside the histogram, updating the running statistics and the corresponding bin. More...
 
unsigned int index (real x) const
 Find the bin index corresponding to a given data point. More...
 
unsigned int number () const
 Get the number of data points inside the histogram. More...
 
std::vector< unsigned int > bins () const
 Get a vector containing the bin counts of each bin. More...
 
real max () const
 Get the biggest data point of the histogram. More...
 
real min () const
 Get the smallest data point of the histogram. More...
 
real mean () const
 Get the mean value of the histogram data. More...
 
real tss () const
 Get the total sum of squares (TSS) computed using Welford's one-pass method. More...
 
real operator() (real x)
 Evaluate the histogram like a step function which is zero outside the range of the histogram. More...
 
unsigned int operator[] (unsigned int i) const
 Get the number of elements in the i-th bin. More...
 
std::string to_string (const std::string &separator=" ", bool normalized=true, bool lower_extreme=false) const
 TO-DO Cumulative Distribution Function. More...
 
 operator std::string ()
 Convert the histogram to string representation.
 

Friends

std::ostream & operator<< (std::ostream &out, const histogram &obj)
 Stream the histogram in string representation to an output stream (std::ostream)
 

Detailed Description

Histogram class with running statistics, can be constructed from the parameters of the bins or from a dataset.

Elements are inserted one by one, updating the running statistics for the TSS, mean, maximum and minimum on each step.

Constructor & Destructor Documentation

◆ histogram() [1/2]

theoretica::histogram::histogram ( unsigned int  bin_count,
real  range_min,
real  range_max 
)
inline

Construct the histogram from the number of bins and the range.

The histogram is initialized from the arguments, without specifying any data points, which need to be added with insert().

Parameters
bin_countThe number of bins
range_minThe lower bound of the range
range_maxThe upper bound of the range

◆ histogram() [2/2]

template<typename Dataset , enable_vector< Dataset > = true>
theoretica::histogram::histogram ( const Dataset &  data,
unsigned int  bin_count = 0 
)
inline

Construct the histogram from a set of data points, with the given number of bins.

If the number of bins is not specified, it defaults to \([\sqrt{N}]\).

Parameters
dataThe set of data points
bin_countThe number of bins (defaults to the square root of the number of points)

Member Function Documentation

◆ bins()

std::vector<unsigned int> theoretica::histogram::bins ( ) const
inline

Get a vector containing the bin counts of each bin.

Note
The bins cannot be directly modified, new elements must be added using insert().
Returns
A vector containing the number of elements in each bin.

◆ index()

unsigned int theoretica::histogram::index ( real  x) const
inline

Find the bin index corresponding to a given data point.

Parameters
xThe value to find the bin index of (must be between range_min and range_max)
Note
This function does not check whether the value is between range_min and range_max, so care should be taken to use only valid inputs.

◆ insert()

void theoretica::histogram::insert ( real  x)
inline

Insert a new data point inside the histogram, updating the running statistics and the corresponding bin.

Parameters
xThe value to insert

◆ max()

real theoretica::histogram::max ( ) const
inline

Get the biggest data point of the histogram.

Returns
The maximum value of all elements.

◆ mean()

real theoretica::histogram::mean ( ) const
inline

Get the mean value of the histogram data.

Returns
The running mean of all elements of the histogram.

◆ min()

real theoretica::histogram::min ( ) const
inline

Get the smallest data point of the histogram.

Returns
The minimum value of all elements.

◆ number()

unsigned int theoretica::histogram::number ( ) const
inline

Get the number of data points inside the histogram.

Returns
The number of data points which have been added to the histogram.

◆ operator()()

real theoretica::histogram::operator() ( real  x)
inline

Evaluate the histogram like a step function which is zero outside the range of the histogram.

Parameters
xThe point to evaluate the histogram function at
Returns
The value of the histogram function at x

◆ operator[]()

unsigned int theoretica::histogram::operator[] ( unsigned int  i) const
inline

Get the number of elements in the i-th bin.

Parameters
iThe index of the bin
Returns
The number of elements in the i-th bin

◆ to_string()

std::string theoretica::histogram::to_string ( const std::string &  separator = " ",
bool  normalized = true,
bool  lower_extreme = false 
) const
inline

TO-DO Cumulative Distribution Function.

Convert the histogram to string representation

Parameters
separatorThe string to print between row elements
normalizedWhether to normalize the bin counts as a frequency (defaults to true).
lower_extremeWhether to print the lower extreme of the bins or use the mid point (defaults to false, using mid points).
Returns
A string representing the histogram, ready to plot.

◆ tss()

real theoretica::histogram::tss ( ) const
inline

Get the total sum of squares (TSS) computed using Welford's one-pass method.

Returns
The total sum of squares of all elements of the histogram.

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