2 * Copyright (C) 2002 Achim Gaedke
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.
20 /***************************************************************
22 * File histogram/stat2d.c:
23 * Routine to return statistical values of the content of a 2D hisogram.
25 * Contains the routines:
26 * gsl_histogram2d_sum sum up all bin values
27 * gsl_histogram2d_xmean determine mean of x values
28 * gsl_histogram2d_ymean determine mean of y values
30 * Author: Achim Gaedke Achim.Gaedke@zpr.uni-koeln.de
33 ***************************************************************/
37 #include <gsl/gsl_errno.h>
38 #include <gsl/gsl_histogram2d.h>
41 sum up all bins of histogram2d
45 gsl_histogram2d_sum (const gsl_histogram2d * h)
47 const size_t n = h->nx * h->ny;
58 gsl_histogram2d_xmean (const gsl_histogram2d * h)
60 const size_t nx = h->nx;
61 const size_t ny = h->ny;
65 /* Compute the bin-weighted arithmetic mean M of a histogram using the
68 M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
73 long double wmean = 0;
76 for (i = 0; i < nx; i++)
78 double xi = (h->xrange[i + 1] + h->xrange[i]) / 2.0;
81 for (j = 0; j < ny; j++)
83 double wij = h->bin[i * ny + j];
90 wmean += (xi - wmean) * (wi / W);
98 gsl_histogram2d_ymean (const gsl_histogram2d * h)
100 const size_t nx = h->nx;
101 const size_t ny = h->ny;
105 /* Compute the bin-weighted arithmetic mean M of a histogram using the
108 M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
113 long double wmean = 0;
116 for (j = 0; j < ny; j++)
118 double yj = (h->yrange[j + 1] + h->yrange[j]) / 2.0;
121 for (i = 0; i < nx; i++)
123 double wij = h->bin[i * ny + j];
131 wmean += (yj - wmean) * (wj / W);
139 gsl_histogram2d_xsigma (const gsl_histogram2d * h)
141 const double xmean = gsl_histogram2d_xmean (h);
142 const size_t nx = h->nx;
143 const size_t ny = h->ny;
147 /* Compute the bin-weighted arithmetic mean M of a histogram using the
150 M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
155 long double wvariance = 0;
158 for (i = 0; i < nx; i++)
160 double xi = (h->xrange[i + 1] + h->xrange[i]) / 2 - xmean;
163 for (j = 0; j < ny; j++)
165 double wij = h->bin[i * ny + j];
173 wvariance += ((xi * xi) - wvariance) * (wi / W);
178 double xsigma = sqrt (wvariance);
184 gsl_histogram2d_ysigma (const gsl_histogram2d * h)
186 const double ymean = gsl_histogram2d_ymean (h);
187 const size_t nx = h->nx;
188 const size_t ny = h->ny;
192 /* Compute the bin-weighted arithmetic mean M of a histogram using the
195 M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
200 long double wvariance = 0;
203 for (j = 0; j < ny; j++)
205 double yj = (h->yrange[j + 1] + h->yrange[j]) / 2.0 - ymean;
208 for (i = 0; i < nx; i++)
210 double wij = h->bin[i * ny + j];
217 wvariance += ((yj * yj) - wvariance) * (wj / W);
222 double ysigma = sqrt (wvariance);
228 gsl_histogram2d_cov (const gsl_histogram2d * h)
230 const double xmean = gsl_histogram2d_xmean (h);
231 const double ymean = gsl_histogram2d_ymean (h);
232 const size_t nx = h->nx;
233 const size_t ny = h->ny;
237 /* Compute the bin-weighted arithmetic mean M of a histogram using the
240 M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
245 long double wcovariance = 0;
248 for (j = 0; j < ny; j++)
250 for (i = 0; i < nx; i++)
252 double xi = (h->xrange[i + 1] + h->xrange[i]) / 2.0 - xmean;
253 double yj = (h->yrange[j + 1] + h->yrange[j]) / 2.0 - ymean;
254 double wij = h->bin[i * ny + j];
259 wcovariance += ((xi * yj) - wcovariance) * (wij / W);