This function builds the cdf F of a random variable Z from a set of outcomes of Z, contained in range [first,last). Cdf F is a function defined by a set of thresholds zi and the associated probabilities F(zi), i = 1,..., n.
In version 1, function argument nb_of_thresholds indicates the number n of thresholds to be used to define the cdf. The n values retained are n equally-spaced quantiles of the distribution. For example, if n = 5, z1 is the smallest value in range [first,last), z2 is the first quartile, z3 is the median, z4 is the upper quartile, and z5 is the highest value in range [first,last). With this version of the algorithm, F(z1) = Prob(Z < z1) = 0 and F(zn) = Prob(Z < zn) = 1.
Version 2 of the algorithm assumes that the cdf new_cdf is already initialized: it already contains the values zi, i = 1,..., n, and function build_cdf computes the corresponding probabilities F(zi). This version gives a complete flexibility in the choice of the thresholds values zi.
Note that range [first,last) will be modified by build_cdf (it will be sorted). If the range must not be modified, a copy should be made first.
int main()
{
gaussian_cdf normal_cdf(0,1);
vector<double> gaussian_values;
for(int i=1; i<=100; i++)
gaussian_values.push_back( normal_cdf.inverse(drand48()) );
non_parametric_cdf new_cdf;
build_cdf(gaussian_values.begin(), gaussian_values.end(),
new_cdf);
}
new_cdf now contains a discrete representation of a standard normal cdf, and the elements of gaussian_values are sorted.