summaryrefslogtreecommitdiffstats
path: root/ml/dlib/examples/running_stats_ex.cpp
blob: d94faf35b83af2559d58703096a4044bc9756956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
    This is an example illustrating the use of the running_stats object from the dlib C++
    Library.  It is a simple tool for computing basic statistics on a stream of numbers.
    In this example, we sample 100 points from the sinc function and then then compute the
    unbiased sample mean, variance, skewness, and excess kurtosis.

*/    
#include <iostream>
#include <vector>
#include <dlib/statistics.h>

using namespace std;
using namespace dlib;

// Here we define the sinc function so that we may generate sample data. We compute the mean,
// variance, skewness, and excess kurtosis of this sample data.

double sinc(double x)
{
    if (x == 0)
        return 1;
    return sin(x)/x;
}

int main()
{
    running_stats<double> rs;

    double tp1 = 0;
    double tp2 = 0;

    // We first generate the data and add it sequentially to our running_stats object.  We
    // then print every fifth data point.
    for (int x = 1; x <= 100; x++)
    {
        tp1 = x/100.0;
        tp2 = sinc(pi*x/100.0);
        rs.add(tp2);

        if(x % 5 == 0)
        {
            cout << " x = " << tp1 << " sinc(x) = " << tp2 << endl;
        }
    }

    // Finally, we compute and print the mean, variance, skewness, and excess kurtosis of
    // our data.

    cout << endl;
    cout << "Mean:           " << rs.mean() << endl;
    cout << "Variance:       " << rs.variance() << endl;
    cout << "Skewness:       " << rs.skewness() << endl;
    cout << "Excess Kurtosis " << rs.ex_kurtosis() << endl;

    return 0;
}