1 /* gsl_histogram_stat.c
2 * Copyright (C) 2000 Simone Piccardi
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 /***************************************************************
21 * File gsl_histogram_stat.c:
22 * Routines for statisticalcomputations on histograms.
23 * Need GSL library and header.
24 * Contains the routines:
25 * gsl_histogram_mean compute histogram mean
26 * gsl_histogram_sigma compute histogram sigma
31 ***************************************************************/
35 #include <gsl/gsl_errno.h>
36 #include <gsl/gsl_histogram.h>
38 /* FIXME: We skip negative values in the histogram h->bin[i] < 0,
39 since those correspond to negative weights (BJG) */
42 gsl_histogram_mean (const gsl_histogram * h)
44 const size_t n = h->n;
47 /* Compute the bin-weighted arithmetic mean M of a histogram using the
50 M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
55 long double wmean = 0;
58 for (i = 0; i < n; i++)
60 double xi = (h->range[i + 1] + h->range[i]) / 2;
61 double wi = h->bin[i];
66 wmean += (xi - wmean) * (wi / W);
74 gsl_histogram_sigma (const gsl_histogram * h)
76 const size_t n = h->n;
79 long double wvariance = 0 ;
80 long double wmean = 0;
83 /* FIXME: should use a single pass formula here, as given in
84 N.J.Higham 'Accuracy and Stability of Numerical Methods', p.12 */
86 /* Compute the mean */
88 for (i = 0; i < n; i++)
90 double xi = (h->range[i + 1] + h->range[i]) / 2;
91 double wi = h->bin[i];
96 wmean += (xi - wmean) * (wi / W);
100 /* Compute the variance */
104 for (i = 0; i < n; i++)
106 double xi = ((h->range[i + 1]) + (h->range[i])) / 2;
107 double wi = h->bin[i];
110 const long double delta = (xi - wmean);
112 wvariance += (delta * delta - wvariance) * (wi / W);
117 double sigma = sqrt (wvariance) ;
124 sum up all bins of histogram
128 gsl_histogram_sum(const gsl_histogram * h)