-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.h
328 lines (316 loc) · 11.4 KB
/
stats.h
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
================================================================================
Name : data.h
Author : Randu Karisa
Version :
Copyright : Your copyright notice
Description : Set of functions to compute data on a data structure
================================================================================
*/
#ifndef STATS_H
#define STATS_H
#include <stdlib.h>
#include <stdio.h>
#include "array.h"
#include "data.h"
#include <math.h>
/******************************************************************************************
* FUNCTIONS FOR INTEGER ARRAY
******************************************************************************************/
/**
*addIntegerArrays function, Adds two integer arrays
*@param array1, The array to be added to the other
*@param array2, The array to be added to the other
*@return sum, An array, the sum of the two arrays
*Note: if arrays are of different size, The longer one will be trimmed
*/
struct IntegerArray* addIntegerArrays(const struct IntegerArray* array1,const struct IntegerArray* array2)
{
struct IntegerArray* ints = newIntegerArray();
int diff;
if(*getIntegerArraySize(array1) < *getIntegerArraySize(array2))
{
diff = (*getIntegerArraySize(array2)) - (*getIntegerArraySize(array1));
for (int i = 0; i < array1->size; ++i)
addToIntegerArray(ints, (*getIntegerValue(array1, i))+(*getIntegerValue(array2, i+diff)));
}
else
{
diff = (*getIntegerArraySize(array1)) - (*getIntegerArraySize(array2));
for (int i = 0; i < (array2)->size; ++i)
addToIntegerArray(ints,(* getIntegerValue(array1, i+diff))+(*getIntegerValue(array2, i)));
}
return ints;
}
/**
*diffIntegerArrays function, Finds the difference of two arrays
*@param array1, The array to find the difference from
*@param array2, The array to find the difference to
*@return array, An array the difference of the two arrays
*Note: if arrays are of different size, The longer one will be trimmed
*/
struct IntegerArray* diffIntegerArrays(const struct IntegerArray* array1,const struct IntegerArray* array2)
{
struct IntegerArray* ints = newIntegerArray();
int diff;
if(*getIntegerArraySize(array1) < *getIntegerArraySize(array2))
{
diff = *getIntegerArraySize(array2) - *getIntegerArraySize(array1);
for (int i = 0; i < (array1)->size; ++i)
addToIntegerArray(ints, *getIntegerValue(array1, i)- *getIntegerValue(array2, i+diff));
}
else
{
diff = *getIntegerArraySize(array1) - *getIntegerArraySize(array2);
for (int i = 0; i < (array2)->size; ++i)
addToIntegerArray(ints, *getIntegerValue(array1, i+diff)- *getIntegerValue(array2, i));
}
return ints;
}
/**
*prodIntegerArrays function, calculate the product of two arrays
*@param array1, Array to calculate the product
*@param array2, Array to calculate the product
*@return array, Array, The product of the two
*Note: if arrays are of different size, The longer one will be trimmed
*/
struct IntegerArray* prodIntegerArrays(const struct IntegerArray* array1,const struct IntegerArray* array2)
{
struct IntegerArray* ints = newIntegerArray();
int diff;
if(*getIntegerArraySize(array1) < *getIntegerArraySize(array2))
{
diff = *getIntegerArraySize(array2) - *getIntegerArraySize(array1);
for (int i = 0; i < (array1)->size; ++i)
addToIntegerArray(ints, (*getIntegerValue(array1, i))*(*getIntegerValue(array2, i+diff)));
}
else
{
diff = *getIntegerArraySize(array1) - *getIntegerArraySize(array2);
for (int i = 0; i < (array2)->size; ++i)
addToIntegerArray(ints, (*getIntegerValue(array1, i+diff))*(*getIntegerValue(array2, i)));
}
return ints;
}
/**
*sumIntegerArray function, Calculated the sum of all the values in an array
*@param array, The array holding the values
*@return sum, Integer the sum of all the values
*/
const int sumIntegerArray(const struct IntegerArray* array)
{
int sum = 0;
for (int i = 0; i < array->size; ++i)
sum = sum + (*getIntegerValue(array, i));
return sum;
}
/**
*sumAbsIntegerArray function, Calculate the absolute sum of all the values in the array
*@param array, The array holding the values
*@return sum
*/
int sumAbsIntegerArray(const struct IntegerArray* array)
{
int sum = 0;
for (int i = 0; i < array->size; ++i)
sum = sum + abs(*getIntegerValue(array, i));
return sum;
}
/**
*meanIntegerArray function, Calculates the mean of the array
*@return mean of the array
*/
double meanIntegerArray(const struct IntegerArray* array)
{
return (double) sumIntegerArray(array)/array->size;
}
/**
*scalarDiffIntegerArrays function, Calculate the difference of the array using a scalar value
*@param array, The array to find its difference
*@param scalar, The value to subtract
*@return array, The resulting array
*/
struct IntegerArray* scalarDiffIntegerArrays(const struct IntegerArray* array1,const int scalar)
{
struct IntegerArray* doubles = newIntegerArray();
for(int i = 0; i < (array1)->size; ++i)
addToIntegerArray(doubles, *getIntegerValue(array1,i) - scalar);
return doubles;
}
/**
*corrIntegerArray function, Calculates the correlation between two arrays
*@param array1, The X array
*@param array3, The Y array
*@return double, The correlation coefficient
*Note: if arrays are of different size, The longer one will be trimmed
*/
double corrIntegerArray(const struct IntegerArray* array1,const struct IntegerArray* array2)
{
struct IntegerArray* diffx = (scalarDiffIntegerArrays(array1, meanIntegerArray(array1)));
struct IntegerArray* diffy = (scalarDiffIntegerArrays(array2, meanIntegerArray(array2)));
struct IntegerArray* prod = prodIntegerArrays(diffx,diffy);
int prodUpper = sumIntegerArray(prod);
struct IntegerArray* prodx = prodIntegerArrays(diffx,diffx);
struct IntegerArray* prody = prodIntegerArrays(diffy,diffy);
int sumx = sumIntegerArray(prodx);
int sumy = sumIntegerArray(prody);
freeIntegerArray(prod);
freeIntegerArray(prodx);
freeIntegerArray(prody);
freeIntegerArray(diffx);
freeIntegerArray(diffy);
double prodLower = sqrt(sumx*sumy);
return (prodUpper/prodLower);
}
/******************************************************************************************
* FUNCTIONS FOR DOUBLE ARRAY
******************************************************************************************/
/**
*addDoubleArrays function, adds two arrays
*@param array1, The first array
*@param array2, The second array
*@param array, The sum of the two arrays
*Note: if arrays are of different size, The longer one will be trimmed
*/
struct DoubleArray* addDoubleArrays(const struct DoubleArray* array1,const struct DoubleArray* array2)
{
struct DoubleArray* doubles = newDoubleArray();
double diff;
if(*getDoubleArraySize(array1) < *getDoubleArraySize(array2))
{
diff = *getDoubleArraySize(array2) - *getDoubleArraySize(array1);
for (int i = 0; i < *getDoubleArraySize(array1); ++i)
addToDoubleArray(doubles, (*getDoubleValue(array1, i))+(*getDoubleValue(array2, i+diff)));
}
else
{
diff = *getDoubleArraySize(array1) - *getDoubleArraySize(array2);
for (int i = 0; i < *getDoubleArraySize(array2); ++i)
addToDoubleArray(doubles, (*getDoubleValue(array1, i+diff))+(*getDoubleValue(array2, i)));
}
return doubles;
}
/**
*diffDoubleArrays function, Finds the difference between two arrays
*@param array1, The first array
*@param array2, The second array
*@return array, The difference between the two
*Note: if arrays are of different size, The longer one will be trimmed
*/
struct DoubleArray* diffDoubleArrays(const struct DoubleArray* array1,const struct DoubleArray* array2)
{
struct DoubleArray* doubles = newDoubleArray();
double diff;
if(*getDoubleArraySize(array1) < *getDoubleArraySize(array2))
{
diff = *getDoubleArraySize(array2) - *getDoubleArraySize(array1);
for (int i = 0; i < *getDoubleArraySize(array1); ++i)
addToDoubleArray(doubles, (*getDoubleValue(array1, i))-(*getDoubleValue(array2, i+diff)));
}
else
{
diff = *getDoubleArraySize(array1) - *getDoubleArraySize(array2);
for (int i = 0; i < *getDoubleArraySize(array2); ++i)
addToDoubleArray(doubles, (*getDoubleValue(array1, i+diff))-(*getDoubleValue(array2, i)));
}
return doubles;
}
/**
*prodDoubleArrays function, Finds the product of the two arrays
*@param array1, The first array
*@param array2, The second array
*@return array, The resulting array
*Note: if arrays are of different size, The longer one will be trimmed
*/
struct DoubleArray* prodDoubleArrays(const struct DoubleArray* array1,const struct DoubleArray* array2)
{
struct DoubleArray* doubles = newDoubleArray();
double diff;
if(*getDoubleArraySize(array1) < *getDoubleArraySize(array2))
{
diff = *getDoubleArraySize(array2) - *getDoubleArraySize(array1);
for (int i = 0; i < *getDoubleArraySize(array1); ++i)
addToDoubleArray(doubles, (*getDoubleValue(array1, i))*(*getDoubleValue(array2, i+diff)));
}
else
{
diff = *getDoubleArraySize(array1) - *getDoubleArraySize(array2);
for (int i = 0; i < *getDoubleArraySize(array2); ++i)
addToDoubleArray(doubles, (*getDoubleValue(array1, i+diff))*(*getDoubleValue(array2, i)));
}
return doubles;
}
/**
*sumDoubleArray function, Finds the sum of the array
*@param array, The array to find the sum of its values
*@return sum, Double value representing the sum
*/
double sumDoubleArray(const struct DoubleArray* array)
{
double sum = 0.0;
for (int i = 0; i < array->size; ++i)
sum = sum + (*getDoubleValue(array, i));
return sum;
}
/**
*sumAbsDoubleArray function, Finds the absolute sum of the array
*@param array, The array to find the sum of its values
*@return sum, Double value representing the absolute sum
*/
double sumAbsDoubleArray(const struct DoubleArray* array)
{
double sum = 0.0;
for (int i = 0; i < array->size; ++i)
sum = sum + fabs(*getDoubleValue(array, i));
return sum;
}
/**
*meanDoubleArray function, Finds the mean of the array
*@param array, The array to find the mean of its values
*@return mean, Double value representing the sum
*/
double meanDoubleArray(const struct DoubleArray* array)
{
if(*getDoubleArraySize(array) > 0)
return (double) sumDoubleArray(array)/(*getDoubleArraySize(array));
else
return 0.0;
}
/**
*scalarDiffDoubleArrays function, Finds the difference of the array with a scalar value
*@param array, The array to subtract the scalar
*@param scalar, The value to subtract from the array
*@return array, The resulting array
*/
struct DoubleArray* scalarDiffDoubleArrays(const struct DoubleArray* array,const double scalar)
{
struct DoubleArray* doubles = newDoubleArray();
for(int i = 0; i < *getDoubleArraySize(array); ++i)
addToDoubleArray(doubles, *getDoubleValue(array,i) - scalar);
return doubles;
}
/**
*corrDoubleArray function, Finds the correlation of the arrays
*@param array1, The X array
*@param array2, The Y array
*@return correlation, The correlation coefficient
*/
double corrDoubleArray(const struct DoubleArray* array1,const struct DoubleArray* array2)
{
struct DoubleArray* diffx = (scalarDiffDoubleArrays(array1, meanDoubleArray(array1)));
struct DoubleArray* diffy = (scalarDiffDoubleArrays(array2, meanDoubleArray(array2)));
struct DoubleArray* prod = prodDoubleArrays(diffx,diffy);
double prodUpper = sumDoubleArray(prod);
struct DoubleArray* prodx = prodDoubleArrays(diffx,diffx);
struct DoubleArray* prody = prodDoubleArrays(diffy,diffy);
double sumx = sumDoubleArray(prodx);
double sumy = sumDoubleArray(prody);
freeDoubleArray(prod);
freeDoubleArray(prodx);
freeDoubleArray(prody);
freeDoubleArray(diffx);
freeDoubleArray(diffy);
double prodLower = sqrt(sumx*sumy);
return (prodUpper/prodLower);
}
#endif