Statistics for MySQL  1.1
 All Classes Files Functions Variables Typedefs Macros Pages
skewness_pop.c
Go to the documentation of this file.
1 /* skewness_pop.c (population moment coefficient of skewness) */
2 
3 /***********************************************************************
4 * This code is part of Statistics for MySQL.
5 *
6 * Copyright (C) 2011 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  double sumXXX;
43 };
44 
57 my_bool skewness_pop_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
58  struct skewness_pop_storage * data;
59 
60  if (args->arg_count < 1 || args->arg_count > 2) {
61  strcpy(message,"skewness_pop() 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 skewness_pop_storage *) malloc( sizeof(struct skewness_pop_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 skewness_pop_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
97  skewness_pop_clear(initid, is_null, error);
98  skewness_pop_add(initid, args, is_null, error);
99 }
100 
110 void skewness_pop_clear(UDF_INIT *initid, char *is_null, char *error) {
111  struct skewness_pop_storage *data;
112  data = (struct skewness_pop_storage *) initid->ptr;
113  data->count = 0;
114  data->sumX = 0;
115  data->sumXX = 0;
116  data->sumXXX = 0;
117 }
118 
129 void skewness_pop_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
130  struct skewness_pop_storage * data;
131  double x;
132  double w;
133 
134  if (!args->args[0]) {
135  return;
136  }
137  data = (struct skewness_pop_storage *) initid->ptr;
138  if (data->argc > 1) {
139  if (!args->args[1]) {
140  return;
141  }
142  w = *((double*) args->args[1]);
143  } else {
144  w = 1.;
145  }
146  x = *((double*) args->args[0]);
147 
148  data->count += w;
149  data->sumX += w * x;
150  data->sumXX += w * x*x;
151  data->sumXXX += w * x*x*x;
152 }
153 
163 double skewness_pop(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
164  struct skewness_pop_storage * data;
165  double m3;
166  double m2;
167  double mean;
168 
169  double ret;
170 
171  data = (struct skewness_pop_storage *) initid->ptr;
172 
173  if (data->count <= 0) {
174  *is_null = 1;
175  return 0;
176  }
177 
178  mean = data->sumX / data->count;
179 
180  m3 = (data->sumXXX - 3 * mean * data->sumXX + 2 * mean * mean * data->sumX) / data->count;
181  m2 = (data->sumXX - mean * data->sumX) / data->count;
182 
183  if (m2 <= 0) {
184  *is_null = 1;
185  return 0;
186  }
187 
188  ret = m3 / sqrt(m2 * m2 * m2);
189 
190  return ret;
191 }
192 
200 void skewness_pop_deinit(UDF_INIT *initid) {
201  if (initid->ptr) {
202  free(initid->ptr);
203  }
204 }
Definition of functions for UDFs and plugins.
int argc
number of arguments
Definition: skewness_pop.c:38
double sumX
Sum of x.
Definition: skewness_pop.c:40
double sumXX
Sum of x^2.
Definition: skewness_pop.c:41
my_bool skewness_pop_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
Definition: skewness_pop.c:57
void skewness_pop_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Add a member of the group.
Definition: skewness_pop.c:129
#define NOT_FIXED_DEC
Maximum number of digits in double As defined in mysql/sql_string.h.
Definition: sqlstat.h:77
void skewness_pop_deinit(UDF_INIT *initid)
Called after last access to function.
Definition: skewness_pop.c:200
void skewness_pop_clear(UDF_INIT *initid, char *is_null, char *error)
Called at start of group.
Definition: skewness_pop.c:110
Storage for population moment coefficient of skewness.
Definition: skewness_pop.c:37
double skewness_pop(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve population moment coefficient of skewness. Called at end of group.
Definition: skewness_pop.c:163
double sumXXX
Sum of x^3.
Definition: skewness_pop.c:42
void skewness_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: skewness_pop.c:96
double count
Counter.
Definition: skewness_pop.c:39