- test config fixes for adaptive overlay connects
[oweals/gnunet.git] / src / testbed / standard_deviation.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 #include <gnunet/platform.h>
22 #include <gnunet/gnunet_common.h>
23
24 struct SDHandle
25 {
26   /**
27    * Squared sum of data values
28    */
29   unsigned long long sqsum;
30
31   /**
32    * Sum of the data values
33    */
34   unsigned long sum;
35
36   /**
37    * The average of data amounts
38    */
39   unsigned int avg;
40
41   /**
42    * The variance
43    */
44   unsigned int vr;
45
46   /**
47    * Number of data values
48    */
49   unsigned int cnt;
50 };
51
52
53 struct SDHandle *
54 GNUNET_TESTBED_SD_init ()
55 {
56   return GNUNET_malloc (sizeof (struct SDHandle));
57 }
58
59 void
60 GNUNET_TESTBED_SD_destroy (struct SDHandle *h)
61 {
62   GNUNET_free (h);
63 }
64
65 void
66 GNUNET_TESTBED_SD_add_data (struct SDHandle *h, unsigned int amount)
67 {
68   unsigned long sqavg;
69
70   h->sum += amount;
71   h->cnt++;
72   h->sqsum += ((unsigned long) amount) * ((unsigned long) amount);
73   h->avg = h->sum / h->cnt;
74   sqavg = h->avg * h->avg;
75   h->vr = (h->sqsum / h->cnt) - sqavg;
76 }
77
78
79 /**
80  * Returns the factor by which the given amount differs from the standard deviation
81  *
82  * @param h the SDhandle
83  * @param amount the value for which the deviation is returned
84  * @return the deviation from the average; GNUNET_SYSERR if the deviation cannot
85  *           be calculated
86  */
87 int
88 GNUNET_TESTBED_SD_deviation_factor (struct SDHandle *h, unsigned int amount)
89 {
90   unsigned long diff;
91   unsigned int n;
92
93   if (h->cnt < 2)
94     return GNUNET_SYSERR;
95   if (amount > h->avg)
96     diff = amount - h->avg;
97   else
98     diff = h->avg - amount;
99   diff *= diff;
100   for (n = 1; n < 4; n++)
101     if (diff < (n * n * h->vr))
102       break;
103   return n;
104 }
105
106
107 int
108 main ()
109 {
110   struct SDHandle * h = GNUNET_TESTBED_SD_init ();
111   
112   GNUNET_TESTBED_SD_add_data (h, 40);
113   GNUNET_TESTBED_SD_add_data (h, 30);
114   GNUNET_TESTBED_SD_add_data (h, 40);
115   GNUNET_TESTBED_SD_add_data (h, 10);
116   GNUNET_TESTBED_SD_add_data (h, 30);
117   printf ("Average: %d\n", h->avg);
118   printf ("Variance: %d\n", h->vr);
119   printf ("Standard Deviation: %d\n", (int) sqrt (h->vr));
120   printf ("Deviation factor: %d\n", GNUNET_TESTBED_SD_deviation (h, 40));
121   GNUNET_TESTBED_SD_destroy (h);
122   return 0;
123 }