2 * This file is part of GNUnet
3 * (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;
58 * State of single model instance
64 * Pointer to the plugin state
66 struct Plugin *plugin;
69 * Number of readings so far
79 * Sum square of readings
86 * Update local sums of model with a new value.
88 * @param model Targe model
89 * @param val New value
92 update_sums (struct Model *model, double val)
95 model->sumsq += val * val;
101 * Feed a new value to a model
103 * @param cls closure (model state)
104 * @param val value to be fed to the model
105 * @return #GNUNET_YES in case of a detected outlier, #GNUNET_NO otherwise
108 sensor_gaussian_model_feed (void *cls, double val)
110 struct Model *model = cls;
111 struct Plugin *plugin = model->plugin;
114 long double allowed_variance;
116 if (model->n < plugin->training_window)
118 update_sums (model, val);
121 if (model->n == plugin->training_window)
122 LOG (GNUNET_ERROR_TYPE_DEBUG, "Gaussian model out of training period.\n");
123 mean = model->sum / model->n;
125 sqrt ((model->sumsq - 2 * mean * model->sum +
126 model->n * mean * mean) / (model->n - 1));
127 allowed_variance = (plugin->confidence_interval * stddev);
128 if ((val < (mean - allowed_variance)) || (val > (mean + allowed_variance)))
130 update_sums (model, val);
136 * Destroy a model instance
138 * @param cls closure (model state)
141 sensor_gaussian_model_destroy_model (void *cls)
143 struct Model *model = cls;
150 * Create a model instance
152 * @param cls closure (plugin state)
153 * @return model state to be used for later calls
156 sensor_gaussian_model_create_model (void *cls)
158 struct Plugin *plugin = cls;
161 model = GNUNET_new (struct Model);
163 model->plugin = plugin;
169 * Entry point for the plugin.
171 * @param cls The struct GNUNET_CONFIGURATION_Handle.
172 * @return NULL on error, otherwise the plugin context
175 libgnunet_plugin_sensor_model_gaussian_init (void *cls)
177 static struct Plugin plugin;
178 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
179 struct GNUNET_SENSOR_ModelFunctions *api;
180 unsigned long long num;
182 if (NULL != plugin.cfg)
183 return NULL; /* can only initialize once! */
184 memset (&plugin, 0, sizeof (struct Plugin));
187 GNUNET_CONFIGURATION_get_value_number (cfg, "sensor-model-gaussian",
188 "TRAINING_WINDOW", &num))
190 LOG (GNUNET_ERROR_TYPE_ERROR,
191 _("Missing `TRAINING_WINDOW' value in configuration.\n"));
194 plugin.training_window = (int) num;
196 GNUNET_CONFIGURATION_get_value_number (cfg, "sensor-model-gaussian",
197 "CONFIDENCE_INTERVAL", &num))
199 LOG (GNUNET_ERROR_TYPE_ERROR,
200 _("Missing `CONFIDENCE_INTERVAL' value in configuration.\n"));
203 plugin.confidence_interval = (int) num;
204 api = GNUNET_new (struct GNUNET_SENSOR_ModelFunctions);
207 api->create_model = &sensor_gaussian_model_create_model;
208 api->destroy_model = &sensor_gaussian_model_destroy_model;
209 api->feed_model = &sensor_gaussian_model_feed;
210 LOG (GNUNET_ERROR_TYPE_DEBUG, "Gaussian model plugin is running.\n");
216 * Exit point from the plugin.
218 * @param cls The plugin context (as returned by "init")
219 * @return Always NULL
222 libgnunet_plugin_sensor_model_gaussian_done (void *cls)
224 struct GNUNET_SENSOR_ModelFunctions *api = cls;
225 struct Plugin *plugin = api->cls;
229 LOG (GNUNET_ERROR_TYPE_DEBUG, "Guassian model plugin is finished\n");
234 /* end of plugin_sensor_model_gaussian.c */