gsl_statistics__wvariance_source.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* statistics/wvariance_source.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Jim Davies, Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. static double
  20. FUNCTION(compute,wvariance) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n, const double wmean);
  21. static double
  22. FUNCTION(compute,factor) (const BASE w[], const size_t wstride, const size_t n);
  23. static double
  24. FUNCTION(compute,wvariance) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n, const double wmean)
  25. {
  26. /* takes a dataset and finds the weighted variance */
  27. long double wvariance = 0 ;
  28. long double W = 0;
  29. size_t i;
  30. /* find the sum of the squares */
  31. for (i = 0; i < n; i++)
  32. {
  33. BASE wi = w[i * wstride];
  34. if (wi > 0) {
  35. const long double delta = (data[i * stride] - wmean);
  36. W += wi ;
  37. wvariance += (delta * delta - wvariance) * (wi / W);
  38. }
  39. }
  40. return wvariance ;
  41. }
  42. static double
  43. FUNCTION(compute,factor) (const BASE w[], const size_t wstride, const size_t n)
  44. {
  45. /* Find the factor ``N/(N-1)'' which multiplies the raw std dev */
  46. long double a = 0 ;
  47. long double b = 0;
  48. long double factor;
  49. size_t i;
  50. /* find the sum of the squares */
  51. for (i = 0; i < n; i++)
  52. {
  53. BASE wi = w[i * wstride];
  54. if (wi > 0)
  55. {
  56. a += wi ;
  57. b += wi * wi ;
  58. }
  59. }
  60. factor = (a*a) / ((a*a) - b);
  61. return factor ;
  62. }
  63. double
  64. FUNCTION(gsl_stats,wvariance_with_fixed_mean) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n, const double wmean)
  65. {
  66. const double wvariance = FUNCTION(compute,wvariance) (w, wstride, data, stride, n, wmean);
  67. return wvariance;
  68. }
  69. double
  70. FUNCTION(gsl_stats,wsd_with_fixed_mean) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n, const double wmean)
  71. {
  72. const double wvariance = FUNCTION(compute,wvariance) (w, wstride, data, stride, n, wmean);
  73. const double wsd = sqrt (wvariance);
  74. return wsd;
  75. }
  76. double
  77. FUNCTION(gsl_stats,wvariance_m) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n, const double wmean)
  78. {
  79. const double variance = FUNCTION(compute,wvariance) (w, wstride, data, stride, n, wmean);
  80. const double scale = FUNCTION(compute,factor)(w, wstride, n);
  81. return scale * variance;
  82. }
  83. double
  84. FUNCTION(gsl_stats,wsd_m) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n, const double wmean)
  85. {
  86. const double variance = FUNCTION(compute,wvariance) (w, wstride, data, stride, n, wmean);
  87. const double scale = FUNCTION(compute,factor)(w, wstride, n);
  88. const double wsd = sqrt(scale * variance) ;
  89. return wsd;
  90. }
  91. double
  92. FUNCTION(gsl_stats,wsd) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n)
  93. {
  94. const double wmean = FUNCTION(gsl_stats,wmean) (w, wstride, data, stride, n);
  95. return FUNCTION(gsl_stats,wsd_m) (w, wstride, data, stride, n, wmean) ;
  96. }
  97. double
  98. FUNCTION(gsl_stats,wvariance) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n)
  99. {
  100. const double wmean = FUNCTION(gsl_stats,wmean) (w, wstride, data, stride, n);
  101. return FUNCTION(gsl_stats,wvariance_m)(w, wstride, data, stride, n, wmean);
  102. }