2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file testbed/testbed_api_sd.c
23 * @brief functions to calculate standard deviation
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_util_lib.h"
29 #include "testbed_api_sd.h"
32 * An entry to hold data which will be used to calculate SD
54 * Opaque handle for calculating SD
59 * DLL head for storing entries
64 * DLL tail for storing entries
69 * Squared sum of data values
71 unsigned long long sqsum;
74 * Sum of the data values
79 * The average of data amounts
89 * Number of data values; also the length of DLL containing SDEntries
94 * max number of entries we can have in the DLL
101 * Initialize standard deviation calculation handle
103 * @param max_cnt the maximum number of readings to keep
104 * @return the initialized handle
107 GNUNET_TESTBED_SD_init_ (unsigned int max_cnt)
111 GNUNET_assert (1 < max_cnt);
112 h = GNUNET_malloc (sizeof (struct SDHandle));
113 h->max_cnt = max_cnt;
119 * Frees the memory allocated to the SD handle
121 * @param h the SD handle
124 GNUNET_TESTBED_SD_destroy_ (struct SDHandle *h)
126 struct SDEntry *entry;
128 while (NULL != (entry = h->head))
130 GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
138 * Add a reading to SD
140 * @param h the SD handle
141 * @param amount the reading value
144 GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
146 struct SDEntry *entry;
151 if (h->cnt == h->max_cnt)
154 GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
155 h->sum -= entry->amount;
157 ((unsigned long) entry->amount) * ((unsigned long) entry->amount);
160 GNUNET_assert (h->cnt < h->max_cnt);
162 entry = GNUNET_malloc (sizeof (struct SDEntry));
163 entry->amount = amount;
164 GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, entry);
167 h->avg = ((float) h->sum) / ((float) h->cnt);
168 h->sqsum += ((unsigned long) amount) * ((unsigned long) amount);
169 sqsum_avg = ((double) h->sqsum) / ((double) h->cnt);
170 sqavg = ((double) h->avg) * ((double) h->avg);
171 h->vr = sqsum_avg - sqavg;
176 * Returns the factor by which the given amount differs from the standard deviation
178 * @param h the SDhandle
179 * @param amount the value for which the deviation is returned
181 * @return the deviation from the average; GNUNET_SYSERR if the deviation cannot
182 * be calculated OR 0 if the deviation is less than the average; a
183 * maximum of 4 is returned for deviations equal to or larger than 4
186 GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount)
192 return GNUNET_SYSERR;
193 if (((float) amount) > h->avg)
194 diff = ((float) amount) - h->avg;
196 return 0; //diff = h->avg - ((float) amount);
198 for (n = 1; n < 4; n++)
199 if (diff < (((double) (n * n)) * h->vr))
204 /* end of testbed_api_sd.c */