![]() |
Statistics for MySQL
0.9
|
00001 /* skewness_samp.c (sample moment coefficient of skewness) */ 00002 00003 /*********************************************************************** 00004 * This code is part of Statistics for MySQL. 00005 * 00006 * Copyright (C) 2011 Heinrich Schuchardt (xypron.glpk@gmx.de) 00007 * 00008 * Licensed under the Apache License, Version 2.0 (the "License"); 00009 * you may not use this file except in compliance with the License. 00010 * You may obtain a copy of the License at 00011 * 00012 * http://www.apache.org/licenses/LICENSE-2.0 00013 * 00014 * Unless required by applicable law or agreed to in writing, software 00015 * distributed under the License is distributed on an "AS IS" BASIS, 00016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00017 * See the License for the specific language governing permissions and 00018 * limitations under the License. 00019 ***********************************************************************/ 00020 00021 00031 #include "sqlstat.h" 00032 00036 struct skewness_samp_storage { 00037 int argc; 00038 double count; 00039 double sumX; 00040 double sumXX; 00041 double sumXXX; 00042 }; 00043 00056 my_bool skewness_samp_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { 00057 struct skewness_samp_storage * data; 00058 00059 if (args->arg_count < 1 || args->arg_count > 2) { 00060 strcpy(message,"skewness_coor() requires one or two arguments"); 00061 return 1; 00062 } 00063 args->arg_type[0] = REAL_RESULT; 00064 if (args->arg_count > 1) { 00065 args->arg_type[1] = REAL_RESULT; 00066 } 00067 00068 data = (struct skewness_samp_storage *) malloc( sizeof(struct skewness_samp_storage)); 00069 if (data == NULL) { 00070 strcpy(message,"Couldn't allocate memory"); 00071 return 1; 00072 } 00073 data->argc = args->arg_count; 00074 00075 initid->maybe_null = 1; 00076 initid->decimals = NOT_FIXED_DEC; 00077 initid->max_length = 13 + initid->decimals; 00078 initid->ptr = (char *) data; 00079 initid->const_item = 0; 00080 00081 return 0; 00082 } 00083 00095 void skewness_samp_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { 00096 skewness_samp_clear(initid, is_null, error); 00097 skewness_samp_add(initid, args, is_null, error); 00098 } 00099 00109 void skewness_samp_clear(UDF_INIT *initid, char *is_null, char *error) { 00110 struct skewness_samp_storage *data; 00111 data = (struct skewness_samp_storage *) initid->ptr; 00112 data->count = 0; 00113 data->sumX = 0; 00114 data->sumXX = 0; 00115 data->sumXXX = 0; 00116 } 00117 00128 void skewness_samp_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { 00129 struct skewness_samp_storage * data; 00130 double x; 00131 double w; 00132 00133 if (!args->args[0]) { 00134 return; 00135 } 00136 data = (struct skewness_samp_storage *) initid->ptr; 00137 if (data->argc > 1) { 00138 if (!args->args[1]) { 00139 return; 00140 } 00141 w = *((double*) args->args[1]); 00142 } else { 00143 w = 1.; 00144 } 00145 x = *((double*) args->args[0]); 00146 00147 data->count += w; 00148 data->sumX += w * x; 00149 data->sumXX += w * x*x; 00150 data->sumXXX += w * x*x*x; 00151 } 00152 00162 double skewness_samp(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { 00163 struct skewness_samp_storage * data; 00164 double m3; 00165 double m2; 00166 double mean; 00167 00168 double ret; 00169 00170 data = (struct skewness_samp_storage *) initid->ptr; 00171 00172 if (data->count <= 2) { 00173 *is_null = 1; 00174 return 0; 00175 } 00176 00177 mean = data->sumX / data->count; 00178 00179 m3 = (data->sumXXX - 3 * mean * data->sumXX + 2 * mean * mean * data->sumX) / data->count; 00180 m2 = (data->sumXX - mean * data->sumX) / data->count; 00181 00182 if (m2 <= 0) { 00183 *is_null = 1; 00184 return 0; 00185 } 00186 00187 ret = sqrt(data->count * (data->count - 1) ) / (data->count - 2) * m3 / sqrt(m2 * m2 * m2); 00188 00189 return ret; 00190 } 00191 00199 void skewness_samp_deinit(UDF_INIT *initid) { 00200 if (initid->ptr) { 00201 free(initid->ptr); 00202 } 00203 }