Statistics for MySQL  1.1
 All Classes Files Functions Variables Typedefs Macros Pages
stddevw_samp.c
Go to the documentation of this file.
1 /* stddevw_samp.c (weighted standard deviation of sample) */
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 
21 
33 #include "sqlstat.h"
34 
39  int argc;
40  double count;
41  double sumX;
42  double sumXX;
43 };
44 
57 my_bool stddevw_samp_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
58  struct stddevw_samp_storage * data;
59 
60  if (args->arg_count < 1 || args->arg_count > 2) {
61  strcpy(message,"stddevw_coor() requires one or two arguments");
62  return 1;
63  }
64  args->arg_type[0] = REAL_RESULT;
65  if (args->arg_count > 1) {
66  args->arg_type[1] = REAL_RESULT;
67  }
68 
69  data = (struct stddevw_samp_storage *) malloc( sizeof(struct stddevw_samp_storage));
70  if (data == NULL) {
71  strcpy(message,"Couldn't allocate memory");
72  return 1;
73  }
74  data->argc = args->arg_count;
75 
76  initid->maybe_null = 1;
77  initid->decimals = NOT_FIXED_DEC;
78  initid->max_length = 13 + initid->decimals;
79  initid->ptr = (char *) data;
80  initid->const_item = 0;
81 
82  return 0;
83 }
84 
96 void stddevw_samp_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
97  stddevw_samp_clear(initid, is_null, error);
98  stddevw_samp_add(initid, args, is_null, error);
99 }
100 
110 void stddevw_samp_clear(UDF_INIT *initid, char *is_null, char *error) {
111  struct stddevw_samp_storage *data;
112  data = (struct stddevw_samp_storage *) initid->ptr;
113  data->count = 0;
114  data->sumX = 0;
115  data->sumXX = 0;
116 }
117 
128 void stddevw_samp_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
129  struct stddevw_samp_storage * data;
130  double x;
131  double w;
132 
133  if (!args->args[0]) {
134  return;
135  }
136  data = (struct stddevw_samp_storage *) initid->ptr;
137  if (data->argc > 1) {
138  if (!args->args[1]) {
139  return;
140  }
141  w = *((double*) args->args[1]);
142  } else {
143  w = 1.;
144  }
145  x = *((double*) args->args[0]);
146 
147  data->count += w;
148  data->sumX += w * x;
149  data->sumXX += w * x * x;
150 }
151 
161 double stddevw_samp(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
162  struct stddevw_samp_storage * data;
163  double m2;
164  double mean;
165 
166  double ret;
167 
168  data = (struct stddevw_samp_storage *) initid->ptr;
169 
170  if (data->count <= 2) {
171  *is_null = 1;
172  return 0;
173  }
174 
175  mean = data->sumX / data->count;
176 
177  m2 = (data->sumXX - mean * data->sumX) / (data->count - 1);
178 
179  ret = sqrt(m2);
180 
181  return ret;
182 }
183 
191 void stddevw_samp_deinit(UDF_INIT *initid) {
192  if (initid->ptr) {
193  free(initid->ptr);
194  }
195 }
Definition of functions for UDFs and plugins.
double stddevw_samp(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve weighted standard deviation of sample. Called at end of group.
Definition: stddevw_samp.c:161
my_bool stddevw_samp_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
Definition: stddevw_samp.c:57
void stddevw_samp_clear(UDF_INIT *initid, char *is_null, char *error)
Called at start of group.
Definition: stddevw_samp.c:110
void stddevw_samp_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_samp.c:96
double sumX
Sum of x.
Definition: stddevw_samp.c:41
double count
Counter.
Definition: stddevw_samp.c:40
#define NOT_FIXED_DEC
Maximum number of digits in double As defined in mysql/sql_string.h.
Definition: sqlstat.h:77
Storage for weighted standard deviation of sample.
Definition: stddevw_samp.c:38
double sumXX
Sum of x^2.
Definition: stddevw_samp.c:42
int argc
number of arguments
Definition: stddevw_samp.c:39
void stddevw_samp_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Add a member of the group.
Definition: stddevw_samp.c:128
void stddevw_samp_deinit(UDF_INIT *initid)
Called after last access to function.
Definition: stddevw_samp.c:191