gsl_histogram__stat.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* gsl_histogram_stat.c
  2. * Copyright (C) 2000 Simone Piccardi
  3. *
  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.
  8. *
  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.
  13. *
  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.
  18. */
  19. /***************************************************************
  20. *
  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
  27. *
  28. * Author: S. Piccardi
  29. * Jan. 2000
  30. *
  31. ***************************************************************/
  32. #include "gsl__config.h"
  33. #include <stdlib.h>
  34. #include <math.h>
  35. #include "gsl_errno.h"
  36. #include "gsl_histogram.h"
  37. /* FIXME: We skip negative values in the histogram h->bin[i] < 0,
  38. since those correspond to negative weights (BJG) */
  39. double
  40. gsl_histogram_mean (const gsl_histogram * h)
  41. {
  42. const size_t n = h->n;
  43. size_t i;
  44. /* Compute the bin-weighted arithmetic mean M of a histogram using the
  45. recurrence relation
  46. M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
  47. W(n) = W(n-1) + w(n)
  48. */
  49. long double wmean = 0;
  50. long double W = 0;
  51. for (i = 0; i < n; i++)
  52. {
  53. double xi = (h->range[i + 1] + h->range[i]) / 2;
  54. double wi = h->bin[i];
  55. if (wi > 0)
  56. {
  57. W += wi;
  58. wmean += (xi - wmean) * (wi / W);
  59. }
  60. }
  61. return wmean;
  62. }
  63. double
  64. gsl_histogram_sigma (const gsl_histogram * h)
  65. {
  66. const size_t n = h->n;
  67. size_t i;
  68. long double wvariance = 0 ;
  69. long double wmean = 0;
  70. long double W = 0;
  71. /* FIXME: should use a single pass formula here, as given in
  72. N.J.Higham 'Accuracy and Stability of Numerical Methods', p.12 */
  73. /* Compute the mean */
  74. for (i = 0; i < n; i++)
  75. {
  76. double xi = (h->range[i + 1] + h->range[i]) / 2;
  77. double wi = h->bin[i];
  78. if (wi > 0)
  79. {
  80. W += wi;
  81. wmean += (xi - wmean) * (wi / W);
  82. }
  83. }
  84. /* Compute the variance */
  85. W = 0.0;
  86. for (i = 0; i < n; i++)
  87. {
  88. double xi = ((h->range[i + 1]) + (h->range[i])) / 2;
  89. double wi = h->bin[i];
  90. if (wi > 0) {
  91. const long double delta = (xi - wmean);
  92. W += wi ;
  93. wvariance += (delta * delta - wvariance) * (wi / W);
  94. }
  95. }
  96. {
  97. double sigma = sqrt (wvariance) ;
  98. return sigma;
  99. }
  100. }
  101. /*
  102. sum up all bins of histogram
  103. */
  104. double
  105. gsl_histogram_sum(const gsl_histogram * h)
  106. {
  107. double sum=0;
  108. size_t i=0;
  109. size_t n;
  110. n=h->n;
  111. while(i < n)
  112. sum += h->bin[i++];
  113. return sum;
  114. }