| 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 |
|
| 20 |
static double |
| 21 |
FUNCTION(compute,wvariance) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n, const double wmean); |
| 22 |
|
| 23 |
static double |
| 24 |
FUNCTION(compute,factor) (const BASE w[], const size_t wstride, const size_t n); |
| 25 |
|
| 26 |
static double |
| 27 |
FUNCTION(compute,wvariance) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n, const double wmean) |
| 28 |
{ |
| 29 |
/* takes a dataset and finds the weighted variance */ |
| 30 |
|
| 31 |
long double wvariance = 0 ; |
| 32 |
long double W = 0; |
| 33 |
|
| 34 |
size_t i; |
| 35 |
|
| 36 |
/* find the sum of the squares */ |
| 37 |
for (i = 0; i < n; i++) |
| 38 |
{ |
| 39 |
BASE wi = w[i * wstride]; |
| 40 |
|
| 41 |
if (wi > 0) { |
| 42 |
const long double delta = (data[i * stride] - wmean); |
| 43 |
W += wi ; |
| 44 |
wvariance += (delta * delta - wvariance) * (wi / W); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return wvariance ; |
| 49 |
} |
| 50 |
|
| 51 |
static double |
| 52 |
FUNCTION(compute,factor) (const BASE w[], const size_t wstride, const size_t n) |
| 53 |
{ |
| 54 |
/* Find the factor ``N/(N-1)'' which multiplies the raw std dev */ |
| 55 |
|
| 56 |
long double a = 0 ; |
| 57 |
long double b = 0; |
| 58 |
long double factor; |
| 59 |
|
| 60 |
size_t i; |
| 61 |
|
| 62 |
/* find the sum of the squares */ |
| 63 |
for (i = 0; i < n; i++) |
| 64 |
{ |
| 65 |
BASE wi = w[i * wstride]; |
| 66 |
|
| 67 |
if (wi > 0) |
| 68 |
{ |
| 69 |
a += wi ; |
| 70 |
b += wi * wi ; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
factor = (a*a) / ((a*a) - b); |
| 75 |
|
| 76 |
return factor ; |
| 77 |
} |
| 78 |
|
| 79 |
double |
| 80 |
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) |
| 81 |
{ |
| 82 |
const double wvariance = FUNCTION(compute,wvariance) (w, wstride, data, stride, n, wmean); |
| 83 |
return wvariance; |
| 84 |
} |
| 85 |
|
| 86 |
double |
| 87 |
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) |
| 88 |
{ |
| 89 |
const double wvariance = FUNCTION(compute,wvariance) (w, wstride, data, stride, n, wmean); |
| 90 |
const double wsd = sqrt (wvariance); |
| 91 |
|
| 92 |
return wsd; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
double |
| 97 |
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) |
| 98 |
{ |
| 99 |
const double variance = FUNCTION(compute,wvariance) (w, wstride, data, stride, n, wmean); |
| 100 |
const double scale = FUNCTION(compute,factor)(w, wstride, n); |
| 101 |
|
| 102 |
return scale * variance; |
| 103 |
} |
| 104 |
|
| 105 |
double |
| 106 |
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) |
| 107 |
{ |
| 108 |
const double variance = FUNCTION(compute,wvariance) (w, wstride, data, stride, n, wmean); |
| 109 |
const double scale = FUNCTION(compute,factor)(w, wstride, n); |
| 110 |
const double wsd = sqrt(scale * variance) ; |
| 111 |
|
| 112 |
return wsd; |
| 113 |
} |
| 114 |
|
| 115 |
double |
| 116 |
FUNCTION(gsl_stats,wsd) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n) |
| 117 |
{ |
| 118 |
const double wmean = FUNCTION(gsl_stats,wmean) (w, wstride, data, stride, n); |
| 119 |
return FUNCTION(gsl_stats,wsd_m) (w, wstride, data, stride, n, wmean) ; |
| 120 |
} |
| 121 |
|
| 122 |
double |
| 123 |
FUNCTION(gsl_stats,wvariance) (const BASE w[], const size_t wstride, const BASE data[], const size_t stride, const size_t n) |
| 124 |
{ |
| 125 |
const double wmean = FUNCTION(gsl_stats,wmean) (w, wstride, data, stride, n); |
| 126 |
return FUNCTION(gsl_stats,wvariance_m)(w, wstride, data, stride, n, wmean); |
| 127 |
} |