Statistics for MySQL  1.1
 All Classes Files Functions Variables Typedefs Macros Pages
stddevw_pop.c
Go to the documentation of this file.
1 /* stddevw_pop.c (weighted standard deviation of population) */
2 
3 /***********************************************************************
4 * This code is part of Statistics for MySQL.
5 *
6 * Copyright (C) 2015 Heinrich Schuchardt (xypron.glpk@gmx.de)
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 ***********************************************************************/
20 
32 #include "sqlstat.h"
33 
38  int argc;
39  double count;
40  double sumX;
41  double sumXX;
42 };
43 
56 my_bool stddevw_pop_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
57  struct stddevw_pop_storage * data;
58 
59  if (args->arg_count < 1 || args->arg_count > 2) {
60  strcpy(message,"stddevw_pop() requires one or two arguments");
61  return 1;
62  }
63  args->arg_type[0] = REAL_RESULT;
64  if (args->arg_count > 1) {
65  args->arg_type[1] = REAL_RESULT;
66  }
67 
68  data = (struct stddevw_pop_storage *) malloc( sizeof(struct stddevw_pop_storage));
69  if (data == NULL) {
70  strcpy(message,"Couldn't allocate memory");
71  return 1;
72  }
73  data->argc = args->arg_count;
74 
75  initid->maybe_null = 1;
76  initid->decimals = NOT_FIXED_DEC;
77  initid->max_length = 13 + initid->decimals;
78  initid->ptr = (char *) data;
79  initid->const_item = 0;
80 
81  return 0;
82 }
83 
95 void stddevw_pop_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
96  stddevw_pop_clear(initid, is_null, error);
97  stddevw_pop_add(initid, args, is_null, error);
98 }
99 
109 void stddevw_pop_clear(UDF_INIT *initid, char *is_null, char *error) {
110  struct stddevw_pop_storage *data;
111  data = (struct stddevw_pop_storage *) initid->ptr;
112  data->count = 0;
113  data->sumX = 0;
114  data->sumXX = 0;
115 }
116 
127 void stddevw_pop_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
128  struct stddevw_pop_storage * data;
129  double x;
130  double w;
131 
132  if (!args->args[0]) {
133  return;
134  }
135  data = (struct stddevw_pop_storage *) initid->ptr;
136  if (data->argc > 1) {
137  if (!args->args[1]) {
138  return;
139  }
140  w = *((double*) args->args[1]);
141  } else {
142  w = 1.;
143  }
144  x = *((double*) args->args[0]);
145 
146  data->count += w;
147  data->sumX += w * x;
148  data->sumXX += w * x * x;
149 }
150 
160 double stddevw_pop(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
161  struct stddevw_pop_storage * data;
162  double m2;
163  double mean;
164 
165  double ret;
166 
167  data = (struct stddevw_pop_storage *) initid->ptr;
168 
169  if (data->count <= 0) {
170  *is_null = 1;
171  return 0;
172  }
173 
174  mean = data->sumX / data->count;
175 
176  m2 = (data->sumXX - mean * data->sumX) / data->count;
177 
178  ret = sqrt(m2);
179 
180  return ret;
181 }
182 
190 void stddevw_pop_deinit(UDF_INIT *initid) {
191  if (initid->ptr) {
192  free(initid->ptr);
193  }
194 }
Definition of functions for UDFs and plugins.
Storage for weighted standard deviation of population.
Definition: stddevw_pop.c:37
#define NOT_FIXED_DEC
Maximum number of digits in double As defined in mysql/sql_string.h.
Definition: sqlstat.h:77
int argc
number of arguments
Definition: stddevw_pop.c:38
void stddevw_pop_clear(UDF_INIT *initid, char *is_null, char *error)
Called at start of group.
Definition: stddevw_pop.c:109
void stddevw_pop_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Reset function and add first group member Calls clear and add.
Definition: stddevw_pop.c:95
void stddevw_pop_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Add a member of the group.
Definition: stddevw_pop.c:127
void stddevw_pop_deinit(UDF_INIT *initid)
Called after last access to function.
Definition: stddevw_pop.c:190
my_bool stddevw_pop_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
Definition: stddevw_pop.c:56
double sumX
Sum of x.
Definition: stddevw_pop.c:40
double stddevw_pop(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve weighted standard deviation of population. Called at end of group.
Definition: stddevw_pop.c:160
double count
Counter.
Definition: stddevw_pop.c:39
double sumXX
Sum of x^2.
Definition: stddevw_pop.c:41