testbed_api_sd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file testbed/testbed_api_sd.c
  18. * @brief functions to calculate standard deviation
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "testbed_api_sd.h"
  24. /**
  25. * An entry to hold data which will be used to calculate SD
  26. */
  27. struct SDEntry
  28. {
  29. /**
  30. * DLL next pointer
  31. */
  32. struct SDEntry *next;
  33. /**
  34. * DLL prev pointer
  35. */
  36. struct SDEntry *prev;
  37. /**
  38. * The value to store
  39. */
  40. unsigned int amount;
  41. };
  42. /**
  43. * Opaque handle for calculating SD
  44. */
  45. struct SDHandle
  46. {
  47. /**
  48. * DLL head for storing entries
  49. */
  50. struct SDEntry *head;
  51. /**
  52. * DLL tail for storing entries
  53. */
  54. struct SDEntry *tail;
  55. /**
  56. * Squared sum of data values
  57. */
  58. unsigned long long sqsum;
  59. /**
  60. * Sum of the data values
  61. */
  62. unsigned long sum;
  63. /**
  64. * The average of data amounts
  65. */
  66. float avg;
  67. /**
  68. * The variance
  69. */
  70. double vr;
  71. /**
  72. * Number of data values; also the length of DLL containing SDEntries
  73. */
  74. unsigned int cnt;
  75. /**
  76. * max number of entries we can have in the DLL
  77. */
  78. unsigned int max_cnt;
  79. };
  80. /**
  81. * Initialize standard deviation calculation handle
  82. *
  83. * @param max_cnt the maximum number of readings to keep
  84. * @return the initialized handle
  85. */
  86. struct SDHandle *
  87. GNUNET_TESTBED_SD_init_ (unsigned int max_cnt)
  88. {
  89. struct SDHandle *h;
  90. GNUNET_assert (1 < max_cnt);
  91. h = GNUNET_new (struct SDHandle);
  92. h->max_cnt = max_cnt;
  93. return h;
  94. }
  95. /**
  96. * Frees the memory allocated to the SD handle
  97. *
  98. * @param h the SD handle
  99. */
  100. void
  101. GNUNET_TESTBED_SD_destroy_ (struct SDHandle *h)
  102. {
  103. struct SDEntry *entry;
  104. while (NULL != (entry = h->head))
  105. {
  106. GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
  107. GNUNET_free (entry);
  108. }
  109. GNUNET_free (h);
  110. }
  111. /**
  112. * Add a reading to SD
  113. *
  114. * @param h the SD handle
  115. * @param amount the reading value
  116. */
  117. void
  118. GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
  119. {
  120. struct SDEntry *entry;
  121. double sqavg;
  122. double sqsum_avg;
  123. entry = NULL;
  124. if (h->cnt == h->max_cnt)
  125. {
  126. entry = h->head;
  127. GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
  128. h->sum -= entry->amount;
  129. h->sqsum -=
  130. ((unsigned long) entry->amount) * ((unsigned long) entry->amount);
  131. h->cnt--;
  132. }
  133. GNUNET_assert (h->cnt < h->max_cnt);
  134. if (NULL == entry)
  135. entry = GNUNET_new (struct SDEntry);
  136. entry->amount = amount;
  137. GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, entry);
  138. h->sum += amount;
  139. h->cnt++;
  140. h->avg = ((float) h->sum) / ((float) h->cnt);
  141. h->sqsum += ((unsigned long) amount) * ((unsigned long) amount);
  142. sqsum_avg = ((double) h->sqsum) / ((double) h->cnt);
  143. sqavg = ((double) h->avg) * ((double) h->avg);
  144. h->vr = sqsum_avg - sqavg;
  145. }
  146. /**
  147. * Calculates the factor by which the given amount differs
  148. *
  149. * @param h the SDhandle
  150. * @param amount the value for which the deviation is returned
  151. * @param factor the factor by which the given amont differs
  152. * @return GNUNET_SYSERR if the deviation cannot
  153. * be calculated; GNUNET_OK if the deviation is returned through factor
  154. */
  155. int
  156. GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount,
  157. int *factor)
  158. {
  159. double diff;
  160. int f;
  161. int n;
  162. if (h->cnt < 2)
  163. return GNUNET_SYSERR;
  164. if (((float) amount) > h->avg)
  165. {
  166. diff = ((float) amount) - h->avg;
  167. f = 1;
  168. }
  169. else
  170. {
  171. diff = h->avg - ((float) amount);
  172. f = -1;
  173. }
  174. diff *= diff;
  175. for (n = 1; n < 4; n++)
  176. if (diff < (((double) (n * n)) * h->vr))
  177. break;
  178. *factor = f * n;
  179. return GNUNET_OK;
  180. }
  181. /* end of testbed_api_sd.c */