2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file testbed/testbed_api_sd.c
21 * @brief functions to calculate standard deviation
22 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
26 #include "gnunet_util_lib.h"
27 #include "testbed_api_sd.h"
30 * An entry to hold data which will be used to calculate SD
52 * Opaque handle for calculating SD
57 * DLL head for storing entries
62 * DLL tail for storing entries
67 * Squared sum of data values
69 unsigned long long sqsum;
72 * Sum of the data values
77 * The average of data amounts
87 * Number of data values; also the length of DLL containing SDEntries
92 * max number of entries we can have in the DLL
99 * Initialize standard deviation calculation handle
101 * @param max_cnt the maximum number of readings to keep
102 * @return the initialized handle
105 GNUNET_TESTBED_SD_init_ (unsigned int max_cnt)
109 GNUNET_assert (1 < max_cnt);
110 h = GNUNET_new (struct SDHandle);
111 h->max_cnt = max_cnt;
117 * Frees the memory allocated to the SD handle
119 * @param h the SD handle
122 GNUNET_TESTBED_SD_destroy_ (struct SDHandle *h)
124 struct SDEntry *entry;
126 while (NULL != (entry = h->head))
128 GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
136 * Add a reading to SD
138 * @param h the SD handle
139 * @param amount the reading value
142 GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
144 struct SDEntry *entry;
149 if (h->cnt == h->max_cnt)
152 GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
153 h->sum -= entry->amount;
155 ((unsigned long) entry->amount) * ((unsigned long) entry->amount);
158 GNUNET_assert (h->cnt < h->max_cnt);
160 entry = GNUNET_new (struct SDEntry);
161 entry->amount = amount;
162 GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, entry);
165 h->avg = ((float) h->sum) / ((float) h->cnt);
166 h->sqsum += ((unsigned long) amount) * ((unsigned long) amount);
167 sqsum_avg = ((double) h->sqsum) / ((double) h->cnt);
168 sqavg = ((double) h->avg) * ((double) h->avg);
169 h->vr = sqsum_avg - sqavg;
174 * Calculates the factor by which the given amount differs
176 * @param h the SDhandle
177 * @param amount the value for which the deviation is returned
178 * @param factor the factor by which the given amont differs
179 * @return GNUNET_SYSERR if the deviation cannot
180 * be calculated; GNUNET_OK if the deviation is returned through factor
183 GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount,
191 return GNUNET_SYSERR;
192 if (((float) amount) > h->avg)
194 diff = ((float) amount) - h->avg;
199 diff = h->avg - ((float) amount);
203 for (n = 1; n < 4; n++)
204 if (diff < (((double) (n * n)) * h->vr))
210 /* end of testbed_api_sd.c */