kspread
stats.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "stats.h"
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031
00032 KStats::KStats(){
00033
00034 error_flag = FALSE;
00035 data.setAutoDelete(TRUE);
00036
00037 }
00038
00039 KStats::~KStats(){
00040
00041 data.clear();
00042
00043 }
00044
00045 void KStats::clearAll(){
00046
00047 data.clear();
00048
00049 }
00050
00051 void KStats::enterData(CALCAMNT _data){
00052
00053 CALCAMNT *newdata;
00054 newdata = new CALCAMNT;
00055 *newdata = _data;
00056 data.append(newdata);
00057
00058 #ifdef DEBUG_STATS
00059 printf("Added %Lg\n",*newdata);
00060 printf("count %d\n",data.count());
00061 #endif
00062
00063 }
00064
00065
00066 void KStats::clearLast(){
00067
00068
00069 data.removeLast();
00070 #ifdef DEBUG_STATS
00071 printf("count %d\n",data.count());
00072 #endif
00073
00074
00075 }
00076
00077 CALCAMNT KStats::sum(){
00078
00079 CALCAMNT result = 0.0;
00080 CALCAMNT *dp;
00081 for ( dp=data.first(); dp != 0; dp=data.next() ){
00082
00083 result += *dp;
00084
00085 }
00086
00087 #ifdef DEBUG_STATS
00088 printf("Sum %Lg\n",result);
00089 #endif
00090
00091 return result;
00092 }
00093
00094 CALCAMNT KStats::min()
00095 {
00096 printf("MIIINNNN\n");
00097
00098 if ( data.count() == 0 )
00099 return 0.0;
00100
00101 printf("1\n");
00102
00103 CALCAMNT result = *(data.first());
00104 printf("result=%f\n",result);
00105
00106 CALCAMNT *dp = data.next();
00107 for ( ; dp != 0; dp=data.next() )
00108 if ( *dp < result )
00109 result = *dp;
00110
00111 printf("Return\n");
00112
00113 return result;
00114 }
00115
00116 CALCAMNT KStats::max()
00117 {
00118 if ( data.count() == 0 )
00119 return 0.0;
00120
00121 CALCAMNT result = *(data.first());
00122 CALCAMNT *dp = data.next();
00123 for ( ; dp != 0; dp=data.next() )
00124 if ( *dp > result )
00125 result = *dp;
00126
00127 return result;
00128 }
00129
00130 CALCAMNT KStats::mul(){
00131
00132 CALCAMNT result = 1.0;
00133 CALCAMNT *dp;
00134 for ( dp=data.first(); dp != 0; dp=data.next() ){
00135
00136 result *= *dp;
00137
00138 }
00139
00140 return result;
00141 }
00142
00143 CALCAMNT KStats::median(){
00144
00145 int index;
00146 CALCAMNT result;
00147 CALCAMNT *dp;
00148 int bound = 0;
00149
00150 MyList list;
00151
00152 for ( dp=data.first(); dp != 0; dp=data.next() ){
00153 list.inSort(dp);
00154 }
00155
00156 #ifdef DEBUG_STATS
00157 for(int l = 0; l < (int)list.count();l++){
00158
00159 printf("Sorted %Lg\n",*list.at(l));
00160
00161 }
00162 #endif
00163
00164 bound = list.count();
00165
00166 if (bound == 0){
00167 error_flag = TRUE;
00168 return 0.0;
00169 }
00170
00171 if ( bound == 1)
00172 return *list.at(0);
00173
00174 if( bound % 2){
00175
00176 index = (bound - 1 ) / 2 + 1;
00177 result = *list.at(index - 1 );
00178 }
00179 else {
00180
00181 index = bound / 2;
00182 result = ((*list.at(index - 1)) + (*list.at(index)))/2;
00183 }
00184
00185 return result;
00186
00187 }
00188
00189
00190
00191 CALCAMNT KStats::std_kernel(){
00192
00193 CALCAMNT result = 0.0;
00194 CALCAMNT _mean;
00195
00196 _mean = mean();
00197
00198 CALCAMNT *dp;
00199 for ( dp=data.first(); dp != 0; dp=data.next() ){
00200
00201 result += (*dp - _mean) * (*dp - _mean);
00202
00203 }
00204
00205 #ifdef DEBUG_STATS
00206 printf("std_kernel %Lg\n",result);
00207 #endif
00208
00209 return result;
00210
00211 }
00212
00213
00214 CALCAMNT KStats::sum_of_squares(){
00215
00216 CALCAMNT result = 0.0;
00217 CALCAMNT *dp;
00218 for ( dp=data.first(); dp != 0; dp=data.next() ){
00219
00220 result += (*dp) * (*dp);
00221
00222 }
00223
00224 #ifdef DEBUG_STATS
00225 printf("Sum of Squares %Lg\n",result);
00226 #endif
00227
00228 return result;
00229
00230 }
00231
00232 CALCAMNT KStats::mean(){
00233
00234 CALCAMNT result = 0.0;
00235
00236 if(data.count() == 0){
00237 error_flag = TRUE;
00238 return 0.0;
00239 }
00240
00241 result = sum()/data.count();
00242
00243 #ifdef DEBUG_STATS
00244 printf("mean: %Lg\n",result);
00245 #endif
00246
00247 return result;
00248
00249 }
00250
00251 CALCAMNT KStats::std(){
00252
00253 CALCAMNT result = 0.0;
00254
00255 if(data.count() == 0){
00256 error_flag = TRUE;
00257
00258 #ifdef DEBUG_STATS
00259 printf("set stats error\n");
00260 #endif
00261
00262 return 0.0;
00263 }
00264
00265 result = SQRT(std_kernel());
00266
00267 #ifdef DEBUG_STATS
00268 printf ("data.count %d\n",data.count());
00269 #endif
00270
00271 result = result/data.count();
00272
00273 #ifdef DEBUG_STATS
00274 printf("std: %Lg\n",result);
00275 #endif
00276
00277 return result;
00278
00279 }
00280
00281 CALCAMNT KStats::sample_std(){
00282
00283 CALCAMNT result = 0.0;
00284
00285 if(data.count() < 2 ){
00286 error_flag = TRUE;
00287 return 0.0;
00288 }
00289
00290 result = SQRT(std_kernel());
00291 result = result/(data.count() - 1);
00292 #ifdef DEBUG_STATS
00293 printf("sample std: %Lg\n",result);
00294 #endif
00295 return result;
00296
00297 }
00298
00299 int KStats::count(){
00300
00301 return data.count();
00302
00303 }
00304
00305 bool KStats::error(){
00306
00307 bool value;
00308 value = error_flag;
00309 error_flag = FALSE;
00310
00311 return value;
00312
00313 }
00314
00315 int MyList::compareItems(Item item_1, Item item_2){
00316
00317 CALCAMNT *item1;
00318 CALCAMNT *item2;
00319
00320 item1 = (CALCAMNT*) item_1;
00321 item2 = (CALCAMNT*) item_2;
00322
00323 if(*item1 > *item2)
00324 return 1;
00325
00326 if(*item2 > *item1)
00327 return -1;
00328
00329 return 0;
00330
00331 }
00332
00333
00334
|