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 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.
17 * @file testbed/testbed_api_sd.c
18 * @brief functions to calculate standard deviation
19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23 #include "gnunet_util_lib.h"
24 #include "testbed_api_sd.h"
27 * An entry to hold data which will be used to calculate SD
49 * Opaque handle for calculating SD
54 * DLL head for storing entries
59 * DLL tail for storing entries
64 * Squared sum of data values
66 unsigned long long sqsum;
69 * Sum of the data values
74 * The average of data amounts
84 * Number of data values; also the length of DLL containing SDEntries
89 * max number of entries we can have in the DLL
96 * Initialize standard deviation calculation handle
98 * @param max_cnt the maximum number of readings to keep
99 * @return the initialized handle
102 GNUNET_TESTBED_SD_init_ (unsigned int max_cnt)
106 GNUNET_assert (1 < max_cnt);
107 h = GNUNET_new (struct SDHandle);
108 h->max_cnt = max_cnt;
114 * Frees the memory allocated to the SD handle
116 * @param h the SD handle
119 GNUNET_TESTBED_SD_destroy_ (struct SDHandle *h)
121 struct SDEntry *entry;
123 while (NULL != (entry = h->head))
125 GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
133 * Add a reading to SD
135 * @param h the SD handle
136 * @param amount the reading value
139 GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
141 struct SDEntry *entry;
146 if (h->cnt == h->max_cnt)
149 GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
150 h->sum -= entry->amount;
152 ((unsigned long) entry->amount) * ((unsigned long) entry->amount);
155 GNUNET_assert (h->cnt < h->max_cnt);
157 entry = GNUNET_new (struct SDEntry);
158 entry->amount = amount;
159 GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, entry);
162 h->avg = ((float) h->sum) / ((float) h->cnt);
163 h->sqsum += ((unsigned long) amount) * ((unsigned long) amount);
164 sqsum_avg = ((double) h->sqsum) / ((double) h->cnt);
165 sqavg = ((double) h->avg) * ((double) h->avg);
166 h->vr = sqsum_avg - sqavg;
171 * Calculates the factor by which the given amount differs
173 * @param h the SDhandle
174 * @param amount the value for which the deviation is returned
175 * @param factor the factor by which the given amont differs
176 * @return GNUNET_SYSERR if the deviation cannot
177 * be calculated; GNUNET_OK if the deviation is returned through factor
180 GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount,
188 return GNUNET_SYSERR;
189 if (((float) amount) > h->avg)
191 diff = ((float) amount) - h->avg;
196 diff = h->avg - ((float) amount);
200 for (n = 1; n < 4; n++)
201 if (diff < (((double) (n * n)) * h->vr))
207 /* end of testbed_api_sd.c */