ze-filter  (ze-filter-0.8.0-develop-180218)
zeKStats.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : janvier 2002
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 /* ************************************************************************ */
24 /* Kameleon - A Discrete Event Simulator */
25 /* */
26 /* Created by : Jose Marcio Martins da Cruz */
27 /* Ecole Nationale Superieure des Mines de Paris */
28 /* */
29 /* History : */
30 /* 15 May 1996 : Creation */
31 /* ************************************************************************ */
32 
33 
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 
38 #include <ze-sys.h>
39 
40 #include "zeKStats.h"
41 
42 double
44  kstats_T *s;
45 {
46  if ((s == NULL) || (s->n == 0))
47  return 0;
48 
49  return s->st / s->n;
50 }
51 
52 double
54  kstats_T *s;
55 {
56  if ((s == NULL) || (s->n < 2))
57  return 0;
58  return sqrt (fabs ((s->st2 - s->st * s->st / s->n) / (s->n - 1)));
59 }
60 
61 double
62 zeKMin (s)
63  kstats_T *s;
64 {
65  if ((s == NULL) || (s->n == 0))
66  return 0;
67 
68  return s->min;
69 }
70 
71 double
72 zeKMax (s)
73  kstats_T *s;
74 {
75  if ((s == NULL) || (s->n == 0))
76  return 0;
77 
78  return s->max;
79 }
80 
81 long
83  kstats_T *s;
84 {
85  if ((s == NULL) || (s->n == 0))
86  return 0;
87 
88  return s->n;
89 }
90 
91 void
93  kstats_T *s;
94 {
95  if (s == NULL)
96  return;
97  memset (s, 0, sizeof (*s));
98 }
99 
100 void
102  kstats_T *s;
103  double t;
104 {
105  if (s == NULL)
106  return;
107  if (s->n == 0)
108  s->min = s->max = t;
109  s->n++;
110  s->st += t;
111  s->st2 += t * t;
112  if (t < s->min)
113  s->min = t;
114  if (t > s->max)
115  s->max = t;
116 }
117 
118 #if 0
119 void
120 print_inference (s, p, c)
121  kstats_T *s;
122  double p;
123  char *c;
124 {
125  if ((s != NULL) && (s->n > 1)) {
126  printf ("--> ");
127  if (c != NULL)
128  printf ("%s <--", c);
129  printf ("\n");
130  printf (" Mean Value : %9.4f\n", zeKMean (s));
131  printf (" Std. Deviation : %9.4f\n", zeKStdDev (s));
132  printf (" Confidence (%4.2f) : %9.4f\n", p,
133  confidence_interval (p, zeKStdDev (s), s->n));
134  printf (" Notes : %9d\n", s->n);
135  }
136 }
137 
138 #endif
139 
140 /* --------------------------------------------- */
141 
142 #ifndef OK
143 #define OK 0
144 #define ERROR -1
145 #endif
146 
147 #ifndef M_PI
148 #define M_PI 3.14159265358979323846
149 #endif
150 
151 #define SQRT2PI 2.50662827479
152 
153 #define sqr(x) ((x)*(x))
154 
155 typedef struct {
156  double x;
157  double y;
158 } point;
159 
160 static point *p = NULL;
161 
162 /* --------------------------------------------- */
163 static double
164 fn (double x)
165 {
166  return (exp (-sqr (x) / 2) / SQRT2PI);
167 }
168 
169 static int
170 init_table (int nint, double nmax)
171 {
172  int i;
173  double x, y = 0.;
174  double dx = nmax / nint;
175 
176  if (p == NULL) {
177  if ((p = (point *) malloc ((nint + 1) * sizeof (point))) == NULL)
178  return ERROR;
179  }
180  memset (p, 0, (nint + 1) * sizeof (point));
181 
182  p[0].x = 0.;
183  p[0].y = 0.;
184  for (i = 1; i < nint + 1; i++) {
185  x = i * dx;
186  y += dx * (fn (x) + fn (x - dx)) / 2;
187  p[i].x = x;
188  p[i].y = y;
189  }
190  return OK;
191 }
192 
193 /* --------------------------------------------- */
194 static int NINT = 1000;
195 static double NMAX = 10.;
196 
197 #define sign(x) (((x) > 0.) ? 1 : (((x) < 0.) ? -1 : 0))
198 
199 static double
200 f (double x)
201 {
202  double dx = NMAX / NINT;
203  int q;
204  double r;
205 
206  int i, j;
207  int s;
208 
209  if ((p == NULL) && ((init_table (NINT, NMAX)) != OK)) {
210  printf ("Erreur pendant creation de matrice normale\n");
211  exit (1);
212  }
213 
214  s = sign (x);
215  x = fabs (x);
216 
217  if (x > NMAX)
218  return (s * p[NINT].y);
219 
220  q = (int) (x / dx);
221  r = x - q * dx;
222 
223  i = j = q;
224  switch (sign (r)) {
225  case -1:
226  i--;
227  break;
228  case 0:
229  break;
230  case 1:
231  j++;
232  break;
233  }
234 
235  if (i != j)
236  return (s *
237  (p[i].y + (x - p[i].x) * (p[j].y - p[i].y) / (p[j].x - p[i].x)));
238  else
239  return (s * p[i].y);
240 }
241 
242 double
243 FGauss (double x)
244 {
245  return (0.5 + f (x));
246 }
247 
248 #if !defined(HAVE_ERF)
249 double
250 erf (double x)
251 {
252  return (2 * f (x));
253 }
254 #endif
255 
256 #if !defined(HAVE_ERFC)
257 double
258 erfc (double x)
259 {
260  return (1 - 2 * f (x));
261 }
262 #endif
263 
264 /* --------------------------------------------- */
265 static int
266 luby (double y)
267 {
268  int i, j, k;
269 
270  i = 0;
271  j = NINT;
272  while (j - i > 1) {
273  k = (j + i) / 2;
274  if (p[k].y > y) {
275  j = k;
276  continue;
277  }
278  if (p[k].y < y) {
279  i = k;
280  continue;
281  }
282  i = j = k;
283  break;
284  }
285  return j;
286 }
287 
288 static int
289 glby (double y)
290 {
291  int i, j, k;
292 
293  i = 0;
294  j = NINT;
295  while (j - i > 1) {
296  k = (j + i) / 2;
297  if (p[k].y > y) {
298  j = k;
299  continue;
300  }
301  if (p[k].y < y) {
302  i = k;
303  continue;
304  }
305  i = j = k;
306 
307  break;
308  }
309  return i;
310 }
311 
312 static double
313 finv (double y)
314 {
315  int i, j;
316 
317  if ((p == NULL) && ((init_table (NINT, NMAX)) != OK)) {
318  printf ("Erreur pendant creation de matrice normale\n");
319  exit (1);
320  }
321 
322  i = glby (y);
323  j = luby (y);
324 
325  if (j - i > 1)
326  return 0.;
327 
328  if (i == j)
329  return p[i].x;
330 
331  return p[i].x + (y - p[i].y) * (p[j].x - p[i].x) / (p[j].y - p[i].y);
332 }
333 
334 double
335 FGaussI (double y)
336 {
337  if ((y > 1.) || (y < 0.))
338  return 0;
339 
340  if (y >= 0.5)
341  return (finv (y - 0.5));
342  return (-finv (0.5 - y));
343 }
344 
345 double
346 erfi (double y)
347 {
348  if ((y > 1.) || (y < -1.))
349  return 0;
350 
351  return (sign (y) * finv (fabs (y / 2)));
352 }
353 
354 double
355 erfci (double y)
356 {
357  if ((y > 2.) || (y < 0.))
358  return 0;
359 
360  return (erfi (1. - y));
361 }
362 
363 /* --------------------------------------------- */
364 double
365 confidence_interval (double p, double stddev, int n)
366 {
367  if ((p < 0.) || (p > 1.))
368  return 0.;
369 
370  return (stddev * erfi (p) / sqrt ((double) n));
371 }
double max
Definition: zeKStats.h:31
double min
Definition: zeKStats.h:30
long zeKCount(kstats_T *s)
Definition: zeKStats.c:82
#define sign(x)
Definition: zeKStats.c:197
double st
Definition: zeKStats.h:28
double confidence_interval(double p, double stddev, int n)
Definition: zeKStats.c:365
void zeKStatsUpdate(kstats_T *s, double t)
Definition: zeKStats.c:101
double zeKMean(kstats_T *s)
Definition: zeKStats.c:43
void print_inference(kstats_T *, double, char *)
double y
Definition: zeKStats.c:157
double st2
Definition: zeKStats.h:29
double erf(double x)
Definition: zeKStats.c:250
double x
Definition: zeKStats.c:156
double FGauss(double x)
Definition: zeKStats.c:243
double erfci(double y)
Definition: zeKStats.c:355
#define min(a, b)
Definition: macros.h:128
void zeKStatsReset(kstats_T *s)
Definition: zeKStats.c:92
#define sqr(x)
Definition: zeKStats.c:153
double FGaussI(double y)
Definition: zeKStats.c:335
#define OK
Definition: zeKStats.c:143
int n
Definition: zeKStats.h:27
double zeKMax(kstats_T *s)
Definition: zeKStats.c:72
double zeKStdDev(kstats_T *s)
Definition: zeKStats.c:53
double erfi(double y)
Definition: zeKStats.c:346
#define ERROR
Definition: zeKStats.c:144
double erfc(double x)
Definition: zeKStats.c:258
double zeKMin(kstats_T *s)
Definition: zeKStats.c:62
#define SQRT2PI
Definition: zeKStats.c:151