"Fossies" - the Fresh Open Source Software Archive

Member "dlib-19.18/docs/running_stats_ex.cpp.html" (22 Sep 2019, 6313 Bytes) of package /linux/misc/dlib-19.18.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - running_stats_ex.cpp</title></head><body bgcolor='white'><pre>
    2 <font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
    3 </font><font color='#009900'>/*
    4     This is an example illustrating the use of the running_stats object from the dlib C++
    5     Library.  It is a simple tool for computing basic statistics on a stream of numbers.
    6     In this example, we sample 100 points from the sinc function and then then compute the
    7     unbiased sample mean, variance, skewness, and excess kurtosis.
    8 
    9 */</font>    
   10 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>
   11 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>vector<font color='#5555FF'>&gt;</font>
   12 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>statistics.h<font color='#5555FF'>&gt;</font>
   13 
   14 <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
   15 <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
   16 
   17 <font color='#009900'>// Here we define the sinc function so that we may generate sample data. We compute the mean,
   18 </font><font color='#009900'>// variance, skewness, and excess kurtosis of this sample data.
   19 </font>
   20 <font color='#0000FF'><u>double</u></font> <b><a name='sinc'></a>sinc</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> x<font face='Lucida Console'>)</font>
   21 <b>{</b>
   22     <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>x <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
   23         <font color='#0000FF'>return</font> <font color='#979000'>1</font>;
   24     <font color='#0000FF'>return</font> <font color='#BB00BB'>sin</font><font face='Lucida Console'>(</font>x<font face='Lucida Console'>)</font><font color='#5555FF'>/</font>x;
   25 <b>}</b>
   26 
   27 <font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
   28 <b>{</b>
   29     running_stats<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font> rs;
   30 
   31     <font color='#0000FF'><u>double</u></font> tp1 <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
   32     <font color='#0000FF'><u>double</u></font> tp2 <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
   33 
   34     <font color='#009900'>// We first generate the data and add it sequentially to our running_stats object.  We
   35 </font>    <font color='#009900'>// then print every fifth data point.
   36 </font>    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> x <font color='#5555FF'>=</font> <font color='#979000'>1</font>; x <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>100</font>; x<font color='#5555FF'>+</font><font color='#5555FF'>+</font><font face='Lucida Console'>)</font>
   37     <b>{</b>
   38         tp1 <font color='#5555FF'>=</font> x<font color='#5555FF'>/</font><font color='#979000'>100.0</font>;
   39         tp2 <font color='#5555FF'>=</font> <font color='#BB00BB'>sinc</font><font face='Lucida Console'>(</font>pi<font color='#5555FF'>*</font>x<font color='#5555FF'>/</font><font color='#979000'>100.0</font><font face='Lucida Console'>)</font>;
   40         rs.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>tp2<font face='Lucida Console'>)</font>;
   41 
   42         <font color='#0000FF'>if</font><font face='Lucida Console'>(</font>x <font color='#5555FF'>%</font> <font color='#979000'>5</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
   43         <b>{</b>
   44             cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> x = </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> tp1 <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> sinc(x) = </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> tp2 <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   45         <b>}</b>
   46     <b>}</b>
   47 
   48     <font color='#009900'>// Finally, we compute and print the mean, variance, skewness, and excess kurtosis of
   49 </font>    <font color='#009900'>// our data.
   50 </font>
   51     cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   52     cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Mean:           </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> rs.<font color='#BB00BB'>mean</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   53     cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Variance:       </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> rs.<font color='#BB00BB'>variance</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   54     cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Skewness:       </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> rs.<font color='#BB00BB'>skewness</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   55     cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Excess Kurtosis </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> rs.<font color='#BB00BB'>ex_kurtosis</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   56 
   57     <font color='#0000FF'>return</font> <font color='#979000'>0</font>;
   58 <b>}</b>
   59 
   60 
   61 </pre></body></html>