home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / lib / statistics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-27  |  2.1 KB  |  80 lines

  1. /* statistics.c: find various statistics.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  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 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include "statistics.h"
  22.  
  23.  
  24. /* The mean.  The array A might have been filtered (for example), in
  25.    which case it will have many zeros in it that shouldn't be counted
  26.    when computing the statistics.  PERTINENT is the number of elements
  27.    that are actually valid.  COUNT, on the other hand, is the number of
  28.    elements in the array.  */
  29.  
  30. real
  31. mean (real *a, unsigned count, unsigned pertinent)
  32. {
  33.   unsigned this_variable;
  34.   real sum = 0.0;
  35.  
  36.   assert (pertinent > 0);
  37.  
  38.   for (this_variable = 0; this_variable < count; this_variable++)
  39.     sum += a[this_variable];
  40.  
  41.   return sum / pertinent;
  42. }
  43.  
  44.  
  45. /* The standard deviation.  We have to subtract one from the number of
  46.    samples, because we lost a degree of freedom because of the mean.  */
  47.  
  48. real
  49. standard_deviation (real *a, real mean, unsigned count, unsigned pertinent)
  50. {
  51.   real sum = 0.0;
  52.   int this_variable;
  53.  
  54.   assert (pertinent > 0);
  55.  
  56.   for (this_variable = 0; this_variable < count; this_variable++)
  57.     {
  58.       real x = a[this_variable] - mean;
  59.       sum += x * x;
  60.     }
  61.  
  62.   return sqrt (sum / (pertinent > 1 ? (pertinent - 1) : 1));
  63. }
  64.  
  65.  
  66. /* Here's a routine to fill up the statistics structure.  */
  67.  
  68. statistics_type
  69. statistics (real *data, unsigned data_length, unsigned pertinent)
  70. {
  71.   statistics_type s;
  72.  
  73.   s.mean = mean (data, data_length, pertinent);
  74.  
  75.   s.variance = standard_deviation (data, s.mean, data_length, pertinent);
  76.   s.variance *= s.variance;
  77.  
  78.   return s;
  79. }
  80.