2 * This file is part of GNUnet
3 * Copyright (C) 2013 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 sensor/plugin_sensor_model_gaussian.c
23 * @brief Gaussian model for sensor analysis
24 * @author Omar Tarabai
28 #include "gnunet_sensor_model_plugin.h"
29 #include "gnunet_sensor_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-model-gaussian", __VA_ARGS__)
35 * Plugin state information
41 * Configuration handle
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
46 * Number of initial readings to be used for training only
51 * Number of standard deviations considered within "normal"
53 int confidence_interval;
56 * Increase in weight with each reading
63 * State of single model instance
69 * Pointer to the plugin state
71 struct Plugin *plugin;
79 * Number of readings so far
84 * Weight to be used for the next reading
91 * Update local sums of model with a new value.
93 * @param model Targe model
94 * @param val New value
97 update_sums (struct Model *model, double val)
101 for (i = 0; i < 3; i++)
102 model->s[i] += model->w * pow (val, (double) i);
103 model->w += model->plugin->weight_inc;
109 * Feed a new value to a model
111 * @param cls closure (model state)
112 * @param val value to be fed to the model
113 * @return #GNUNET_YES in case of a detected outlier, #GNUNET_NO otherwise
116 sensor_gaussian_model_feed (void *cls, double val)
118 struct Model *model = cls;
119 struct Plugin *plugin = model->plugin;
122 long double allowed_variance;
124 if (model->n < plugin->training_window)
126 update_sums (model, val);
129 if (model->n == plugin->training_window)
130 LOG (GNUNET_ERROR_TYPE_DEBUG, "Gaussian model out of training period.\n");
131 mean = model->s[1] / model->s[0];
133 (model->s[0] * model->s[2] -
134 model->s[1] * model->s[1]) / (model->s[0] * (model->s[0] - 1));
135 if (stddev < 0) /* Value can be slightly less than 0 due to rounding errors */
137 stddev = sqrt (stddev);
138 allowed_variance = (plugin->confidence_interval * stddev);
139 if ((val < (mean - allowed_variance)) || (val > (mean + allowed_variance)))
141 update_sums (model, val);
147 * Destroy a model instance
149 * @param cls closure (model state)
152 sensor_gaussian_model_destroy_model (void *cls)
154 struct Model *model = cls;
161 * Create a model instance
163 * @param cls closure (plugin state)
164 * @return model state to be used for later calls
167 sensor_gaussian_model_create_model (void *cls)
169 struct Plugin *plugin = cls;
172 model = GNUNET_new (struct Model);
174 model->plugin = plugin;
181 * Entry point for the plugin.
183 * @param cls The struct GNUNET_CONFIGURATION_Handle.
184 * @return NULL on error, otherwise the plugin context
187 libgnunet_plugin_sensor_model_gaussian_init (void *cls)
189 static struct Plugin plugin;
190 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
191 struct GNUNET_SENSOR_ModelFunctions *api;
192 unsigned long long num;
194 if (NULL != plugin.cfg)
195 return NULL; /* can only initialize once! */
196 memset (&plugin, 0, sizeof (struct Plugin));
199 GNUNET_CONFIGURATION_get_value_number (cfg, "sensor-model-gaussian",
200 "TRAINING_WINDOW", &num))
202 LOG (GNUNET_ERROR_TYPE_ERROR,
203 _("Missing `TRAINING_WINDOW' value in configuration.\n"));
208 LOG (GNUNET_ERROR_TYPE_WARNING,
209 "Minimum training window invalid (<1), setting to 1.\n");
210 plugin.training_window = 1;
214 plugin.training_window = (int) num;
217 GNUNET_CONFIGURATION_get_value_number (cfg, "sensor-model-gaussian",
218 "CONFIDENCE_INTERVAL", &num))
220 LOG (GNUNET_ERROR_TYPE_ERROR,
221 _("Missing `CONFIDENCE_INTERVAL' value in configuration.\n"));
225 GNUNET_CONFIGURATION_get_value_float (cfg, "sensor-model-gaussian",
226 "WEIGHT_INC", &plugin.weight_inc))
228 LOG (GNUNET_ERROR_TYPE_ERROR,
229 _("Missing `WEIGHT_INC' value in configuration.\n"));
232 plugin.confidence_interval = (int) num;
233 api = GNUNET_new (struct GNUNET_SENSOR_ModelFunctions);
236 api->create_model = &sensor_gaussian_model_create_model;
237 api->destroy_model = &sensor_gaussian_model_destroy_model;
238 api->feed_model = &sensor_gaussian_model_feed;
239 LOG (GNUNET_ERROR_TYPE_DEBUG, "Gaussian model plugin is running.\n");
245 * Exit point from the plugin.
247 * @param cls The plugin context (as returned by "init")
248 * @return Always NULL
251 libgnunet_plugin_sensor_model_gaussian_done (void *cls)
253 struct GNUNET_SENSOR_ModelFunctions *api = cls;
254 struct Plugin *plugin = api->cls;
258 LOG (GNUNET_ERROR_TYPE_DEBUG, "Guassian model plugin is finished\n");
263 /* end of plugin_sensor_model_gaussian.c */