-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats_utils.c
308 lines (239 loc) · 8.2 KB
/
stats_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Copyright (C) 2013
Fabien Gaud <fgaud@sfu.ca>, Baptiste Lepers <baptiste.lepers@inria.fr>,
Fabien Mottet <fabien.mottet@inria.fr>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 or later, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "stats_utils.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define NO_YET_IMPLEMENTED printf("%s, l.%d: not yet implemented.\n", __FUNCTION__, __LINE__); exit(EXIT_FAILURE)
/******************************************************************************************/
void init_stats_sample_Lf(stats_sample_Lf_t * sample, int size_max, char * name){
assert(sample !=NULL);
sample->s = calloc(size_max, sizeof(long double));
assert(sample->s !=NULL);
sample->nb_elem = 0;
sample->size_max = size_max;
sample->name = name;
}
/******************************************************************************************/
void destroy_stats_sample_Lf(stats_sample_Lf_t * sample){
assert(sample->s !=NULL);
free(sample->s);
sample->s = NULL;
sample->nb_elem = 0;
sample->size_max = 0;
sample->name = NULL;
}
/******************************************************************************************/
void insert_Lf(stats_sample_Lf_t *sample, long double val){
assert(sample->nb_elem < sample->size_max);
sample->s[sample->nb_elem] = val;
sample->nb_elem++;
}
/******************************************************************************************/
void dump_Lf(stats_sample_Lf_t *sample){
unsigned long i;
for (i=0; i<sample->nb_elem; i++){
printf("DUMP [%s]->s[%lu]: %Lf\n", sample->name, i, sample->s[i]);
}
}
/******************************************************************************************/
long double _helper_average_Lf(long double *tab, unsigned long start_i, unsigned long end_i){
long double avg = 0;
unsigned long i;
for (i=start_i; i<=end_i; i++){
avg += tab[i];
}
avg = avg / (long double)(end_i-start_i+1);
return avg;
}
/******************************************************************************************/
long double _helper_stddev_Lf(long double *tab, unsigned long start_i, unsigned long end_i){
long double avg = 0;
long double stddev=0;
unsigned long i;
avg = _helper_average_Lf(tab, start_i, end_i);
for (i=start_i; i<=end_i; i++) {
stddev += powl(tab[i]-avg, 2);
}
stddev = sqrtl(stddev / (long double)(end_i-start_i+1));
return stddev;
}
/******************************************************************************************/
long double average_Lf(stats_sample_Lf_t *sample) {
assert(sample !=NULL);
if (sample->nb_elem == 0){
return 0;
}
return _helper_average_Lf(sample->s, 0, sample->nb_elem-1);
}
/******************************************************************************************/
long double stddev_Lf(stats_sample_Lf_t *sample){
assert(sample !=NULL);
if (sample->nb_elem == 0){
return 0;
}
return _helper_stddev_Lf(sample->s, 0, sample->nb_elem-1);
}
/******************************************************************************************/
long double interval_average_Lf(stats_sample_Lf_t *sample, int percent){
assert(sample !=NULL);
assert(percent>0 && percent<=100);
if (sample->nb_elem == 0){
return 0;
}
float side_percent_to_cut = ((100.0-(float)percent)/2.0)/100.0;
unsigned long int start_i = (unsigned long)floor(sample->nb_elem * side_percent_to_cut);
unsigned long int end_i = sample->nb_elem - start_i;
assert(end_i > start_i);
return _helper_average_Lf(sample->s, start_i, end_i-1);
}
/******************************************************************************************/
long double interval_stddev_Lf(stats_sample_Lf_t *sample, int percent){
assert(sample !=NULL);
assert(percent>0 && percent<=100);
if (sample->nb_elem == 0){
return 0;
}
float side_percent_to_cut = ((100.0-(float)percent)/2.0)/100.0;
unsigned long int start_i = (unsigned long)floor(sample->nb_elem * side_percent_to_cut);
unsigned long int end_i = sample->nb_elem - start_i;
assert(end_i > start_i);
return _helper_stddev_Lf(sample->s, start_i, end_i-1);
}
/******************************************************************************************/
int compare_elem(const void *a,const void *b){
if((*(long double *) a) < (*(long double *) b)){
return -1;
}
else if((*(long double *) a) > (*(long double *) b)){
return 1;
}
else{
return 0;
}
}
/******************************************************************************************/
long double percentil_average_Lf(stats_sample_Lf_t *sample, int percentil){
assert(sample->s !=NULL);
assert(percentil>0 && percentil<=100);
if (sample->nb_elem == 0){
return 0;
}
float side_percent_to_cut = ((100.0-(float)percentil)/2.0)/100.0;
unsigned long int start_i = (unsigned long)floor(sample->nb_elem * side_percent_to_cut);
unsigned long int end_i = sample->nb_elem - start_i;
assert(end_i > start_i);
long double * temp = (long double *)calloc(sample->nb_elem, sizeof(long double));
assert(temp != NULL);
memcpy(temp, sample->s, sample->nb_elem*sizeof(long double));
qsort(temp , sample->nb_elem, sizeof(long double), compare_elem);
long double avg = _helper_average_Lf(temp, start_i, end_i-1);
free(temp);
return avg;
}
/******************************************************************************************/
long double percentil_stddev_Lf(stats_sample_Lf_t *sample, int percentil) {
assert(sample->s !=NULL);
assert(percentil>0 && percentil<=100);
if (sample->nb_elem == 0) {
return 0;
}
float side_percent_to_cut = ((100.0-(float)percentil)/2.0)/100.0;
unsigned long int start_i = (unsigned long)floor(sample->nb_elem * side_percent_to_cut);
unsigned long int end_i = sample->nb_elem - start_i;
assert(end_i > start_i);
long double * temp = (long double *)calloc(sample->nb_elem, sizeof(long double));
assert(temp != NULL);
memcpy(temp, sample->s, sample->nb_elem*sizeof(long double));
qsort(temp, sample->nb_elem, sizeof(long double), compare_elem);
long double avg = _helper_stddev_Lf(temp, start_i, end_i-1);
free(temp);
return avg;
}
/******************************************************************************************/
long double min_Lf(stats_sample_Lf_t *sample){
assert(sample !=NULL);
assert(sample->nb_elem > 0);
unsigned long i;
long double min = sample->s[0];
for (i=0; i<sample->nb_elem; i++){
if(min > sample->s[i]){
min = sample->s[i];
}
}
return min;
}
/******************************************************************************************/
long double max_Lf(stats_sample_Lf_t *sample){
assert(sample !=NULL);
assert(sample->nb_elem > 0);
unsigned long i;
long double max = sample->s[0];
for (i=0; i<sample->nb_elem; i++){
if(max < sample->s[i]){
max = sample->s[i];
}
}
return max;
}
/******************************************************************************************/
/**********************Table functions*************************************/
double t_average_f(double *table,int n) {
assert(table!=NULL || n>0);
double moy = 0;
int i;
//average computation
for (i=0; i<n; i++){
moy += table[i];
}
moy /= n;
return moy;
}
double t_average_ul(unsigned long *table,int n) {
assert(table!=NULL || n>0);
double moy = 0;
int i;
//average computation
for (i=0; i<n; i++){
moy += table[i];
}
moy /= n;
return moy;
}
unsigned long t_minSearch_ul(unsigned long *table,int n){
assert(table!=NULL || n>0);
int i;
unsigned long min = table[0];
for( i=1 ; i<n ; i++){
if(min > table[i]){
min = table[i];
}
}
return min;
}
unsigned long t_maxSearch_ul(unsigned long *table,int n){
assert(table!=NULL || n>0);
int i;
unsigned long max = table[0];
for( i=1 ; i<n ; i++){
if(max < table[i]){
max = table[i];
}
}
return max;
}