Statistics for MySQL  1.1
 All Classes Files Functions Variables Typedefs Macros Pages
skewness_samp.c
Go to the documentation of this file.
1 /* skewness_samp.c (sample 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 
21 
33 #include "sqlstat.h"
34 
39  int argc;
40  double count;
41  double sumX;
42  double sumXX;
43  double sumXXX;
44 };
45 
58 my_bool skewness_samp_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
59  struct skewness_samp_storage * data;
60 
61  if (args->arg_count < 1 || args->arg_count > 2) {
62  strcpy(message,"skewness_coor() requires one or two arguments");
63  return 1;
64  }
65  args->arg_type[0] = REAL_RESULT;
66  if (args->arg_count > 1) {
67  args->arg_type[1] = REAL_RESULT;
68  }
69 
70  data = (struct skewness_samp_storage *) malloc( sizeof(struct skewness_samp_storage));
71  if (data == NULL) {
72  strcpy(message,"Couldn't allocate memory");
73  return 1;
74  }
75  data->argc = args->arg_count;
76 
77  initid->maybe_null = 1;
78  initid->decimals = NOT_FIXED_DEC;
79  initid->max_length = 13 + initid->decimals;
80  initid->ptr = (char *) data;
81  initid->const_item = 0;
82 
83  return 0;
84 }
85 
97 void skewness_samp_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
98  skewness_samp_clear(initid, is_null, error);
99  skewness_samp_add(initid, args, is_null, error);
100 }
101 
111 void skewness_samp_clear(UDF_INIT *initid, char *is_null, char *error) {
112  struct skewness_samp_storage *data;
113  data = (struct skewness_samp_storage *) initid->ptr;
114  data->count = 0;
115  data->sumX = 0;
116  data->sumXX = 0;
117  data->sumXXX = 0;
118 }
119 
130 void skewness_samp_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
131  struct skewness_samp_storage * data;
132  double x;
133  double w;
134 
135  if (!args->args[0]) {
136  return;
137  }
138  data = (struct skewness_samp_storage *) initid->ptr;
139  if (data->argc > 1) {
140  if (!args->args[1]) {
141  return;
142  }
143  w = *((double*) args->args[1]);
144  } else {
145  w = 1.;
146  }
147  x = *((double*) args->args[0]);
148 
149  data->count += w;
150  data->sumX += w * x;
151  data->sumXX += w * x*x;
152  data->sumXXX += w * x*x*x;
153 }
154 
164 double skewness_samp(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
165  struct skewness_samp_storage * data;
166  double m3;
167  double m2;
168  double mean;
169 
170  double ret;
171 
172  data = (struct skewness_samp_storage *) initid->ptr;
173 
174  if (data->count <= 2) {
175  *is_null = 1;
176  return 0;
177  }
178 
179  mean = data->sumX / data->count;
180 
181  m3 = (data->sumXXX - 3 * mean * data->sumXX + 2 * mean * mean * data->sumX) / data->count;
182  m2 = (data->sumXX - mean * data->sumX) / data->count;
183 
184  if (m2 <= 0) {
185  *is_null = 1;
186  return 0;
187  }
188 
189  ret = sqrt(data->count * (data->count - 1) ) / (data->count - 2) * m3 / sqrt(m2 * m2 * m2);
190 
191  return ret;
192 }
193 
201 void skewness_samp_deinit(UDF_INIT *initid) {
202  if (initid->ptr) {
203  free(initid->ptr);
204  }
205 }
void skewness_samp_clear(UDF_INIT *initid, char *is_null, char *error)
Called at start of group.
Definition of functions for UDFs and plugins.
#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: skewness_samp.c:39
Storage for sample moment coefficient of skewness.
Definition: skewness_samp.c:38
double count
Counter.
Definition: skewness_samp.c:40
void skewness_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: skewness_samp.c:97
void skewness_samp_deinit(UDF_INIT *initid)
Called after last access to function.
my_bool skewness_samp_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
Definition: skewness_samp.c:58
double sumXX
Sum of x^2.
Definition: skewness_samp.c:42
void skewness_samp_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Add a member of the group.
double sumX
Sum of x.
Definition: skewness_samp.c:41
double skewness_samp(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve sample moment coefficient of skewness. Called at end of group.
double sumXXX
Sum of x^3.
Definition: skewness_samp.c:43