gsl_histogram__stat2d.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* histogram/stat2d.c
  2. * Copyright (C) 2002 Achim Gaedke
  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 histogram/stat2d.c:
  22. * Routine to return statistical values of the content of a 2D hisogram.
  23. *
  24. * Contains the routines:
  25. * gsl_histogram2d_sum sum up all bin values
  26. * gsl_histogram2d_xmean determine mean of x values
  27. * gsl_histogram2d_ymean determine mean of y values
  28. *
  29. * Author: Achim Gaedke Achim.Gaedke@zpr.uni-koeln.de
  30. * Jan. 2002
  31. *
  32. ***************************************************************/
  33. #include "gsl__config.h"
  34. #include <math.h>
  35. #include "gsl_errno.h"
  36. #include "gsl_histogram2d.h"
  37. /*
  38. sum up all bins of histogram2d
  39. */
  40. double
  41. gsl_histogram2d_sum (const gsl_histogram2d * h)
  42. {
  43. const size_t n = h->nx * h->ny;
  44. double sum = 0;
  45. size_t i = 0;
  46. while (i < n)
  47. sum += h->bin[i++];
  48. return sum;
  49. }
  50. double
  51. gsl_histogram2d_xmean (const gsl_histogram2d * h)
  52. {
  53. const size_t nx = h->nx;
  54. const size_t ny = h->ny;
  55. size_t i;
  56. size_t j;
  57. /* Compute the bin-weighted arithmetic mean M of a histogram using the
  58. recurrence relation
  59. M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
  60. W(n) = W(n-1) + w(n)
  61. */
  62. long double wmean = 0;
  63. long double W = 0;
  64. for (i = 0; i < nx; i++)
  65. {
  66. double xi = (h->xrange[i + 1] + h->xrange[i]) / 2.0;
  67. double wi = 0;
  68. for (j = 0; j < ny; j++)
  69. {
  70. double wij = h->bin[i * ny + j];
  71. if (wij > 0)
  72. wi += wij;
  73. }
  74. if (wi > 0)
  75. {
  76. W += wi;
  77. wmean += (xi - wmean) * (wi / W);
  78. }
  79. }
  80. return wmean;
  81. }
  82. double
  83. gsl_histogram2d_ymean (const gsl_histogram2d * h)
  84. {
  85. const size_t nx = h->nx;
  86. const size_t ny = h->ny;
  87. size_t i;
  88. size_t j;
  89. /* Compute the bin-weighted arithmetic mean M of a histogram using the
  90. recurrence relation
  91. M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
  92. W(n) = W(n-1) + w(n)
  93. */
  94. long double wmean = 0;
  95. long double W = 0;
  96. for (j = 0; j < ny; j++)
  97. {
  98. double yj = (h->yrange[j + 1] + h->yrange[j]) / 2.0;
  99. double wj = 0;
  100. for (i = 0; i < nx; i++)
  101. {
  102. double wij = h->bin[i * ny + j];
  103. if (wij > 0)
  104. wj += wij;
  105. }
  106. if (wj > 0)
  107. {
  108. W += wj;
  109. wmean += (yj - wmean) * (wj / W);
  110. }
  111. }
  112. return wmean;
  113. }
  114. double
  115. gsl_histogram2d_xsigma (const gsl_histogram2d * h)
  116. {
  117. const double xmean = gsl_histogram2d_xmean (h);
  118. const size_t nx = h->nx;
  119. const size_t ny = h->ny;
  120. size_t i;
  121. size_t j;
  122. /* Compute the bin-weighted arithmetic mean M of a histogram using the
  123. recurrence relation
  124. M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
  125. W(n) = W(n-1) + w(n)
  126. */
  127. long double wvariance = 0;
  128. long double W = 0;
  129. for (i = 0; i < nx; i++)
  130. {
  131. double xi = (h->xrange[i + 1] + h->xrange[i]) / 2 - xmean;
  132. double wi = 0;
  133. for (j = 0; j < ny; j++)
  134. {
  135. double wij = h->bin[i * ny + j];
  136. if (wij > 0)
  137. wi += wij;
  138. }
  139. if (wi > 0)
  140. {
  141. W += wi;
  142. wvariance += ((xi * xi) - wvariance) * (wi / W);
  143. }
  144. }
  145. {
  146. double xsigma = sqrt (wvariance);
  147. return xsigma;
  148. }
  149. }
  150. double
  151. gsl_histogram2d_ysigma (const gsl_histogram2d * h)
  152. {
  153. const double ymean = gsl_histogram2d_ymean (h);
  154. const size_t nx = h->nx;
  155. const size_t ny = h->ny;
  156. size_t i;
  157. size_t j;
  158. /* Compute the bin-weighted arithmetic mean M of a histogram using the
  159. recurrence relation
  160. M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
  161. W(n) = W(n-1) + w(n)
  162. */
  163. long double wvariance = 0;
  164. long double W = 0;
  165. for (j = 0; j < ny; j++)
  166. {
  167. double yj = (h->yrange[j + 1] + h->yrange[j]) / 2.0 - ymean;
  168. double wj = 0;
  169. for (i = 0; i < nx; i++)
  170. {
  171. double wij = h->bin[i * ny + j];
  172. if (wij > 0)
  173. wj += wij;
  174. }
  175. if (wj > 0)
  176. {
  177. W += wj;
  178. wvariance += ((yj * yj) - wvariance) * (wj / W);
  179. }
  180. }
  181. {
  182. double ysigma = sqrt (wvariance);
  183. return ysigma;
  184. }
  185. }
  186. double
  187. gsl_histogram2d_cov (const gsl_histogram2d * h)
  188. {
  189. const double xmean = gsl_histogram2d_xmean (h);
  190. const double ymean = gsl_histogram2d_ymean (h);
  191. const size_t nx = h->nx;
  192. const size_t ny = h->ny;
  193. size_t i;
  194. size_t j;
  195. /* Compute the bin-weighted arithmetic mean M of a histogram using the
  196. recurrence relation
  197. M(n) = M(n-1) + (x[n] - M(n-1)) (w(n)/(W(n-1) + w(n)))
  198. W(n) = W(n-1) + w(n)
  199. */
  200. long double wcovariance = 0;
  201. long double W = 0;
  202. for (j = 0; j < ny; j++)
  203. {
  204. for (i = 0; i < nx; i++)
  205. {
  206. double xi = (h->xrange[i + 1] + h->xrange[i]) / 2.0 - xmean;
  207. double yj = (h->yrange[j + 1] + h->yrange[j]) / 2.0 - ymean;
  208. double wij = h->bin[i * ny + j];
  209. if (wij > 0)
  210. {
  211. W += wij;
  212. wcovariance += ((xi * yj) - wcovariance) * (wij / W);
  213. }
  214. }
  215. }
  216. return wcovariance;
  217. }