1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/* rstat/rstat.c
*
* Copyright (C) 2015 Patrick Alken
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_rstat.h>
gsl_rstat_workspace *
gsl_rstat_alloc(void)
{
gsl_rstat_workspace *w;
w = calloc(1, sizeof(gsl_rstat_workspace));
if (w == 0)
{
GSL_ERROR_NULL ("failed to allocate space for workspace", GSL_ENOMEM);
}
w->median_workspace_p = gsl_rstat_quantile_alloc(0.5);
if (w->median_workspace_p == 0)
{
GSL_ERROR_NULL ("failed to allocate space for median workspace",
GSL_ENOMEM);
}
gsl_rstat_reset(w);
return w;
} /* gsl_rstat_alloc() */
void
gsl_rstat_free(gsl_rstat_workspace *w)
{
if (w->median_workspace_p)
gsl_rstat_quantile_free(w->median_workspace_p);
free(w);
} /* gsl_rstat_free() */
size_t
gsl_rstat_n(gsl_rstat_workspace *w)
{
return w->n;
} /* gsl_rstat_n() */
/* add a data point to the running totals */
int
gsl_rstat_add(const double x, gsl_rstat_workspace *w)
{
double delta = x - w->mean;
double delta_n, delta_nsq, term1, n;
/* update min and max */
if (w->n == 0)
{
w->min = x;
w->max = x;
}
else
{
if (x < w->min)
w->min = x;
if (x > w->max)
w->max = x;
}
/* update mean and variance */
n = (double) ++(w->n);
delta_n = delta / n;
delta_nsq = delta_n * delta_n;
term1 = delta * delta_n * (n - 1.0);
w->mean += delta_n;
w->M4 += term1 * delta_nsq * (n * n - 3.0 * n + 3.0) +
6.0 * delta_nsq * w->M2 - 4.0 * delta_n * w->M3;
w->M3 += term1 * delta_n * (n - 2.0) - 3.0 * delta_n * w->M2;
w->M2 += term1;
/* update median */
gsl_rstat_quantile_add(x, w->median_workspace_p);
return GSL_SUCCESS;
} /* gsl_rstat_add() */
double
gsl_rstat_min(gsl_rstat_workspace *w)
{
return w->min;
} /* gsl_rstat_min() */
double
gsl_rstat_max(gsl_rstat_workspace *w)
{
return w->max;
} /* gsl_rstat_max() */
double
gsl_rstat_mean(gsl_rstat_workspace *w)
{
return w->mean;
} /* gsl_rstat_mean() */
double
gsl_rstat_variance(gsl_rstat_workspace *w)
{
if (w->n > 1)
{
double n = (double) w->n;
return (w->M2 / (n - 1.0));
}
else
return 0.0;
} /* gsl_rstat_variance() */
double
gsl_rstat_sd(gsl_rstat_workspace *w)
{
double var = gsl_rstat_variance(w);
return (sqrt(var));
} /* gsl_rstat_sd() */
double
gsl_rstat_rms(gsl_rstat_workspace *w)
{
double rms = 0.0;
if (w->n > 0)
{
double mean = gsl_rstat_mean(w);
double sigma = gsl_rstat_sd(w);
double n = (double) w->n;
double a = sqrt((n - 1.0) / n);
rms = gsl_hypot(mean, a * sigma);
}
return rms;
}
/* standard deviation of the mean: sigma / sqrt(n) */
double
gsl_rstat_sd_mean(gsl_rstat_workspace *w)
{
if (w->n > 0)
{
double sd = gsl_rstat_sd(w);
return (sd / sqrt((double) w->n));
}
else
return 0.0;
} /* gsl_rstat_sd_mean() */
double
gsl_rstat_median(gsl_rstat_workspace *w)
{
return gsl_rstat_quantile_get(w->median_workspace_p);
}
double
gsl_rstat_skew(gsl_rstat_workspace *w)
{
if (w->n > 0)
{
double n = (double) w->n;
double fac = pow(n - 1.0, 1.5) / n;
return ((fac * w->M3) / pow(w->M2, 1.5));
}
else
return 0.0;
} /* gsl_rstat_skew() */
double
gsl_rstat_kurtosis(gsl_rstat_workspace *w)
{
if (w->n > 0)
{
double n = (double) w->n;
double fac = ((n - 1.0) / n) * (n - 1.0);
return ((fac * w->M4) / (w->M2 * w->M2) - 3.0);
}
else
return 0.0;
} /* gsl_rstat_kurtosis() */
int
gsl_rstat_reset(gsl_rstat_workspace *w)
{
int status;
w->min = 0.0;
w->max = 0.0;
w->mean = 0.0;
w->M2 = 0.0;
w->M3 = 0.0;
w->M4 = 0.0;
w->n = 0;
status = gsl_rstat_quantile_reset(w->median_workspace_p);
return status;
} /* gsl_rstat_reset() */
|