-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatisticalMethods.c
More file actions
1106 lines (711 loc) · 26.1 KB
/
StatisticalMethods.c
File metadata and controls
1106 lines (711 loc) · 26.1 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// StatisticalMethods.c
// Automated_CSV_Data_Analysis
// DavidRichardson02
#include "StatisticalMethods.h"
#include "CommonDefinitions.h"
#include "GeneralUtilities.h"
#include "StringUtilities.h"
#include "FileUtilities.h"
#include "Integrators.h"
/**
* uniform_samples
*
* Generates a uniform distribution of samples between xmin and xmax with a step size.
* Given a range [xmin, xmax], this function generates 'n' equally spaced points within the range
* where the spacing between each point is 'step'. This is primarily used for the Gaussian function
* sampling.
*
*/
double* get_uniform_samples(int *n, double xmin, double xmax, double step)
{
//*n = (int)((xmax - xmin) / step) + 1;
*n = random_in_range(xmin, xmax);
double *x = allocate_memory_double_ptr(*n);
for (int i = 0; i < *n; i++)
{
x[i] = xmin + i * step;
}
return x;
}
/**
* compute_mean
*
* This function calculates the mean value of an array of data.
* The mean is calculated as the sum of all elements divided by the number of elements.
*/
double compute_mean(double* data, int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += data[i];
}
return sum / (double)n;
}
/**
* compute_standard_deviation
*
* This function calculates the standard deviation of an array of data.
* The standard deviation is a measure of the amount of variation or dispersion in a set of values.
*a measure of how dispersed the data is in relation to the mean
* The formula for the sample standard deviation is:
* s = sqrt((1/n) * Σ(xi - mean)^2)
*/
double compute_standard_deviation(double* data, int n, double mean)
{
double variance = 0.0;
for (int i = 0; i < n; i++)
{
variance += (data[i] - mean) * (data[i] - mean);
}
variance /= (double)n;
return sqrt(variance);
}
/**
* compute_skewness
*
* Pearson's moment coefficient of skewness, which is a measure of the asymmetry of the probability distribution of a real-valued random variable about its mean.
* This function calculates Pearson's moment coefficient of skewness of an array of data.
* This skewness is a measure of the asymmetry of the probability distribution of a real-valued
* random variable about its mean.
* The formula for the sample skewness is:
* s = (n / (n−1)(n−2)) * Σ(xi - mean / std_dev)^3)
*/
double compute_skewness(double *data, int n)
{
double m = compute_mean(data, n);
double sd = compute_standard_deviation(data, n, m);
double sum = 0.0;
for(int i = 0; i < n; i++)
{
sum += pow((data[i] - m) / sd, 3);
}
double skewness = ((double)n / ((n - 1) * (n - 2))) * sum;
return skewness;
}
/**
* compute_normal_cdf
*
* This function calculates the cumulative distribution function (CDF)
* for a normal distribution at a given value 'x', with given mean 'mu'
* and standard deviation 'sigma'.
*/
double compute_normal_cdf(double x, double mu, double sigma)
{
return 0.5 * (1.0 + erf((x - mu) / (sigma * sqrt(2.0))));
}
/**
* compute_gaussian
*
* Computes the Gaussian function values for given x-values.
* Gaussian function is given by: f(x) = (2/sqrt(pi)) * exp(-x^2).
* This function returns an array of Gaussian values for a given array of x-values.
*/
double* compute_gaussian(int n, double *x)
{
double mean = compute_mean(x, n);
double stdDev = compute_standard_deviation(x, n, mean);
double *f = (double*)malloc(n * sizeof(double));//allocate_memory_double_ptr(n);
for (int i = 0; i < n; i++)
{
//f(x) = (1 / (σ * √(2π))) * exp(-( (x - μ)² / (2σ²) ))
f[i] = (1 / (stdDev*sqrt(2.0*M_PI))) * exp(-((x[i] - mean) * (x[i] - mean)) / (2.0*stdDev*stdDev));
}
return f;
}
/**
* gaussian_riemann_sum_integration
*
* Integrates the function f(x) using Riemann approximation.
* This function takes in arrays of x-values and their corresponding function values f(x) and
* computes samples of the function F(x), the integral, using a Riemann sum approximation.
*/
double* gaussian_riemann_sum_integration(int n, double *x, double *f)
{
double *F = (double*)malloc(n * sizeof(double));//allocate_memory_double_ptr(n);
F[0] = 0; // Starting value
for (int i = 1; i < n; i++)
{
// Make sure i > 0 before accessing x[i-1] and f[i-1]
if (i > 0)
{
double dx = x[i] - x[i-1];
// Using the trapezoidal rule to approximate area
//F[i] = F[i-1] + 0.5 * (f[i] + f[i-1]) * dx;
F[i] = F[i-1] + f[i] * dx; // Rectangle approximation for area
}
}
return F;
}
/**
* anderson_darling_normality_test
*
* Performs the Anderson-Darling test for normality.
*
* The test statistic A-squared is a measure of how well the data follow
* a particular distribution. For the normal distribution, if the A-squared
* statistic is large, it indicates that the data does not follow a normal distribution.
*tfr1e
* The test rejects the hypothesis of normality when the p-value is less than or equal to 0.05.
* Failing the normality test allows you to state with 95% confidence the data does not fit the normal distribution.
* Passing the normality test only allows you to state no significant departure from normality was found.
*
* The Anderson-Darling test, while having excellent theoretical properties, has a serious flaw when applied to real world data.
* The Anderson-Darling test is severely affected by ties in the data due to poor precision.
* When a significant number of ties exist, the Anderson-Darling will frequently reject the data as non-normal,
* regardless of how well the data fits the normal distribution. A tie is when identical values occur more than once.
*/
double anderson_darling_normality_test(double *data, int n)
{
// The test requires sorted data
double *sortedData = allocate_memory_double_ptr(n);
for (int i = 0; i < n; i++)
{
sortedData[i] = data[i];
}
radix_sort_doubles(sortedData, n);
double mean = compute_mean(data, n);
double std_dev = compute_standard_deviation(data, n, mean);
double A_squared = 0.0;
for (int i = 0; i < n; i++)
{
double Fi = compute_normal_cdf(sortedData[i], mean, std_dev);
A_squared += (2 * (i + 1) - 1) * (log(Fi) + log(1 - Fi));
}
A_squared = -n - A_squared * (1.0 / n);
free(sortedData);
return A_squared;
}
/**
* compute_IQR
*
* Computes the Interquartile Range (IQR) of a dataset.
* The IQR is a measure of statistical dispersion and is calculated as the difference
* between the 75th (Q3) and 25th (Q1) percentiles of the dataset. The function sorts
* the data internally before computing the quartiles. Handles both even and
* odd-sized datasets. If the computed IQR is zero (indicating all values are the same),
* the function issues a warning and recalculates the IQR based on the 25th and 75th indices.
* Assumes that the dataset is large enough to provide meaningful percentile values.
*/
double compute_IQR(double* data, int n)
{
// Allocate memory for a copy of the data to be sorted
double *sortedData = allocate_memory_double_ptr(n);
// Copy data to the new array for sorting, to preserve the original data order
for (int i = 0; i < n; i++)
{
sortedData[i] = data[i];
}
//memcpy(sortedData, data, n);
memcpy(sortedData, data, n);
// Sort the data to prepare for IQR calculation
radix_sort_doubles(sortedData, n); // // Note: Sorting is required for IQR calculation. merge sort is a comparison sort algorithm(good for medium size lists and O(nlogn) in the worst case)
//merge_sort(data, n);
// Calculating the Interquartile Range (IQR).
double iqr = 0.0;
double q25, q75;
// Calculate the 25th and 75th percentiles based on sorted data
// If the number of data points is even, calculate the quartiles using the middle two points
int mid = (n / 2);
if (n % 2 == 0)
{
q25 = (sortedData[n / 4 - 1] + sortedData[n / 4]) / 2.0;
q75 = (sortedData[3 * n / 4 - 1] + sortedData[3 * n / 4]) / 2.0;
//q25 = (sortedData[mid / 2 - 1] + sortedData[mid / 2]) / 2.0;
//q75 = (sortedData[mid + mid / 2 - 1] + sortedData[mid + mid / 2]) / 2.0;
}
else // If the number of data points is odd
{
q25 = sortedData[(n + 1) / 4]; // Adjusted for interpolation
q75 = sortedData[(3 * n + 1) / 4]; // Adjusted for interpolation
}
// Compute the IQR as the difference between the 75th and 25th quartiles
iqr = q75 - q25;
// Handle special case where IQR is zero, which can skew the bin width calculation
if (iqr == 0.0)
{
//perror("\n\nError: Interquartile range is zero. Choose a different binning method, in 'compute_IQR'.");
printf("\n\nError: Interquartile range is zero. Choose a different binning method, in 'compute_IQR'.");
//printf("\nq75: %lf", q75);
//printf("\nq25: %lf", q25);
//printf("\n iqr: %lf \n\n\n\n", iqr);
//free(sortedData);
int q25_index = n / 4;
int q75_index = 3 * n / 4;
double q25 = sortedData[q25_index];
double q75 = sortedData[q75_index];
return q75 - q25;
}
// Return the computed IQR
return iqr;
}
/**
* compute_bin_width
*
* Calculates optimal bin width for histogram using the Freedman-Diaconis rule.
* The Freedman-Diaconis rule provides a robust bin-width calculation based on the
* Interquartile Range (IQR) of the data set.
*
* Formula: binWidth = 2 * (IQR) * n^(-1/3)
*/
double compute_bin_width(double* data, int n)
{
// Check if the data set is large enough to calculate IQR
if (n < 2)
{
perror("\n\nError: Insufficient data to calculate bin width in 'compute_bin_width'.\n");
exit(1);
}
double iqr = compute_IQR(data, n);
//
/*
// Calculating the Interquartile Range (IQR).
double iqr = 0.0;
double q25, q75;
double *sortedData = allocate_memory_double_ptr(n);
for (int i = 0; i < n; i++)
{
sortedData[i] = data[i];
}
memcpy(sortedData, data, n);
radix_sort_doubles(sortedData, n); // Sorting is required for IQR calculation. merge sort is a comparison sort algorithm(good for medium size lists and O(nlogn) in the worst case)
for (int i = 1; i < n; i++)
{
//printf("\n\nsortedData: %.17g", sortedData[i]);
//printf("\n\ndata: %.17g", (*data)[i]);
//sortedData[i-1] = (*data)[i-1];
}
// Calculate the 25th and 75th percentiles based on sorted data
// Check if data size is even
if (n % 2 == 0)
{
int mid = n / 2;
q25 = (sortedData[mid / 2 - 1] + sortedData[mid / 2]) / 2.0;
q75 = (sortedData[mid + mid / 2 - 1] + sortedData[mid + mid / 2]) / 2.0;
}
else // If data size is odd
{
int mid = (n - 1) / 2;
q25 = sortedData[mid / 2];
q75 = sortedData[mid + mid / 2];
}
// Compute the Interquartile Range (IQR)
iqr = q75 - q25;
// Handle cases where IQR is zero (all numbers are the same)
if (iqr == 0.0)
{
perror("\n\nError: Interquartile range is zero. Choose a different binning method, in 'compute_bin_width'.");
exit(1);
//printf("\nq75: %lf", q75);
//printf("\nq25: %lf", q25);
//printf("\n iqr: %lf \n\n\n\n", iqr);
//free(sortedData);
//return 0.0;
}
//*/
// Calculate the optimal bin width using the Freedman-Diaconis rule
double binWidth = 2.0 * iqr / pow((double)(n), 1.0 / 3.0);
//data = allocate_memory_double_ptr_ptr(n);
//for (int i = 0; i < n; i++)
//{
// (*data)[i] = sortedData[i];
//printf("\n\nsorted data ???: %.17g", (*data)[i]);
//}
//memcpy(data, sortedData, n);
//free(sortedData);// Cleanup allocated memory
return binWidth;
}
/**
* calculateOptimalNumBins
*
* Calculates the optimal number of bins using a Bayesian approach.
* This function calculates Bayesian Information Criterion (BIC) based on the bin width
* to find the optimal number of bins for a histogram.
*
* BIC Formula: BIC = n * log(binWidth) + 0.5 * log(n) * numBins
* The formula is also adjusted for small sample sizes.
*/
int compute_optimal_num_bins(double* data, int n)
{
double binWidth = compute_bin_width(data, n);
if (binWidth == 0.0)
{
return n;
}
//merge_sort(data, n);
//for (int i = 1; i < n; i++)
//{
// printf("\n\nsorted data ??? : %.17g", (*data)[i]);
//sortedData[i-1] = (*data)[i-1];
//}
// Calculate the range of the data
//double range = data[n - 1] - data[0]; // The data has already been sorted
// Estimate the number of bins based on the range and optimal bin width
//int numBins = ((int)(range / binWidth));
// Estimate the number of bins based on the range and optimal bin width
//int numBins = (int)ceil(range / binWidth);
// Calculate the range of the data
double range = max_element(data, n) - min_element(data, n);
// Estimate the number of bins based on the range and optimal bin width
int numBins = (int)ceil(range / binWidth);
// Bayesian Information Criterion (BIC) to find the optimal number of bins
double bic = n * log(binWidth) + 0.5 * log(n) * numBins;
// Adjusting for small sample sizes
bic += (numBins - 1) * log((double)(n)) / 2.0;
return numBins;
//return (int)(bic);
}
/**
* compute_data_set_binning
*
* Computes the optimal binning for a histogram of a dataset.
* This function determines the number of bins and their width for constructing
* a histogram, using the Freedman-Diaconis rule which is based on the IQR and
* the size of the dataset. The function checks for sufficient data size and
* handles cases with small datasets. Utilizes the IQR and range of the data to
* calculate bin width and number. Populates the bins based on the calculated binning strategy.
*
* @note For very small datasets, the Freedman-Diaconis rule may not be optimal.
* The method assumes a reasonably symmetric distribution of data.
* Does not consider the Bayesian Information Criterion (BIC) for bin number optimization.
*/
Histogram compute_data_set_binning(double *data, int n)
{
// Calculate the optimal number of bins
// Verify that the data set is large enough to calculate IQR
if (n < 2)
{
perror("\n\nError: Insufficient data to calculate bin width in 'compute_bin_width'.\n");
}
// Calculating the Interquartile Range (IQR).
//double iqr = compute_IQR(data, n);
double iqr = compute_IQR_robust(data, n);
// Calculate bin width using the Freedman-Diaconis rule, which reduces the effect of outliers
double binWidth = 2.0 * iqr / pow((double)(n), 1.0 / 3.0);
// If binWidth is too small, adjust it to a reasonable minimum value
if(binWidth < 1)
{
binWidth = n;
}
//merge_sort(data, n);
// Calculate the range of the data
double range = 0;
range = max_element(data, n) - min_element(data, n);
// Estimate the number of bins based on the range and optimal bin width
int numBins = 0;
numBins = ((int)(range / binWidth));
// Bayesian Information Criterion (BIC) to find the optimal number of bins
//double bic = n * log(binWidth) + 0.5 * log(n) * numBins;
// Adjusting for small sample sizes
//bic -= (numBins - 1) * log((double)(n)) / 2.0;
//print_array(n, *data, "Histogram Data");
// Calculate the optimal number of bins, ensuring it's a positive integer
int optimalNumBins = numBins > 0 ? numBins : 1;
if (optimalNumBins <= 0)
{
perror("\n\nError: Unable to calculate the optimal number of bins in 'compute_data_set_binning'.\n");
//exit(1);
optimalNumBins = n;
}
// Calculate the minimum and maximum values in the data set
double min_value = min_element(data, n);
double max_value = max_element(data, n);
//printf("\n\nminValue: %lf, maxValue: %lf\n", min_value, max_value);
// Calculate the width of each bin
double bin_width = (max_value - min_value) / optimalNumBins;
// Initialize bins, holds frequency count
int* dataSetBins = allocate_memory_int_ptr(optimalNumBins); //A pointer to an array of histogram bins is initialized with a size equal to the optimal number of bins.
//Each bin is initialized to have a frequency count of 0.
for(int i=0; i<optimalNumBins;i++)
{
dataSetBins[i] = 0;
}
// Populate the bins with the frequency count of each bin
for (int i = 0; i < n; i++)
{
int bin_index = (int)((data[i] - min_value) / bin_width);
if (bin_index == optimalNumBins)
{
bin_index--; // Handle the edge case when data[i] == max_value
}
dataSetBins[bin_index]++;
}
// Update the data array to hold histogram values for visualization
double *binData = allocate_memory_double_ptr(optimalNumBins);
// Output bins
for (int i = 0; i < optimalNumBins; i++)
{
double bin_start = min_value + i * bin_width;
double bin_end = bin_start + bin_width;
//printf("\nBin %d: [%.17gf, %.17g): %d", i, bin_start, bin_end, dataSetBins[i]);
binData[i] = (bin_start + bin_end) * 0.5;
//(*data)[i] = (bin_start + bin_end) * 0.5;
}
//free(*(data));
//data = allocate_memory_double_ptr_ptr(optimalNumBins);
//memcpy(data, binData, optimalNumBins);
// Return the computed histogram structure
Histogram data_histogram;
data_histogram.bins = dataSetBins;
data_histogram.num_bins = optimalNumBins;
data_histogram.bin_width = bin_width;
data_histogram.min_value = min_value;
data_histogram.max_value = max_value;
return data_histogram;
}
/**
* compute_histogram
*
* Computes the histogram of the data using optimal bin width and number of bins.
* This function uses the Bayesian Information Criterion (BIC) to find the optimal number of bins,
* and then constructs the histogram accordingly.
*/
void compute_histogram(double* data, int n)
{
// Check if the data set is large enough to calculate IQR
if (n < 2)
{
perror("\n\nError: Insufficient data to calculate bin width in 'compute_bin_width'.\n");
}
// Calculating the Interquartile Range (IQR).
double iqr = 0.0;
double q25 = 0, q75 = 0;
double* sortedData = (double*)malloc(n * sizeof(double));
for (int i = 0; i < n; i++)
{
sortedData[i] = data[i];
}
radix_sort_doubles(sortedData, n); // Sorting is required for IQR calculation. merge sort is a comparison sort algorithm(good for medium size lists and O(nlogn) in the worst case)
// Calculate the 25th and 75th percentiles based on sorted data
// Check if data size is even
if (n % 2 == 0)
{
int mid = n / 2;
q25 = (sortedData[mid / 2 - 1] + sortedData[mid / 2]) / 2.0;
q75 = (sortedData[mid + mid / 2 - 1] + sortedData[mid + mid / 2]) / 2.0;
}
else // If data size is odd
{
int mid = (n - 1) / 2;
q25 = sortedData[mid / 2];
q75 = sortedData[mid + mid / 2];
}
// Compute the Interquartile Range (IQR)
iqr = q75 - q25;
// Handle cases where IQR is zero (all numbers are the same)
if (iqr == 0.0)
{
printf("\n\nError: Interquartile range is zero. Choose a different binning method, in 'compute_bin_width'.");
printf("\nq75: %lf", q75);
printf("\nq25: %lf", q25);
printf("\n iqr: %lf \n\n\n\n", iqr);
free(sortedData);
}
// Calculate the optimal bin width using the Freedman-Diaconis rule
double binWidth = 2.0 * iqr / pow((double)(n), 1.0 / 3.0);
if(binWidth < 1)
{
binWidth = n;
}
free(sortedData);// Cleanup allocated memory
merge_sort(sortedData, n);
// Calculate the range of the data
double range = 0;
range = max_element(data, n) - min_element(data, n);
// Estimate the number of bins based on the range and optimal bin width
int numBins = 0;
numBins = ((int)(range / binWidth));
// Bayesian Information Criterion (BIC) to find the optimal number of bins
//double bic = n * log(binWidth) + 0.5 * log(n) * numBins;
// Adjusting for small sample sizes
//bic -= (numBins - 1) * log((double)(n)) / 2.0;
//print_array(n, *data, "Histogram Data");
//printf("\n\nq25: %.17g", q25);
//printf("\n\nq75: %.17g", q75);
//printf("\n\nbinWidth: %.17g", binWidth);
//printf("\n\nrange: %.17g", range);
//printf("\n\nnumBins: %d", numBins);
//printf("\n\nbic: %.17g", bic);
// Calculate the optimal number of bins
int optimalNumBins = numBins;
if (optimalNumBins <= 1)
{
perror("\n\nError: Unable to calculate the optimal number of bins in 'compute_histogram'.\n");
printf("\n\noptimalNumBins: %d", optimalNumBins);
//exit(1);
optimalNumBins = n;
}
// Determine the range of the data
double min_value = min_element(data, n);
double max_value = max_element(data, n);
//printf("\n\nminValue: %lf, maxValue: %lf\n", min_value, max_value);
// Calculate the width of each bin
double bin_width = (max_value - min_value) / optimalNumBins;
// Initialize bins, holds frequency count
int* histogramBins = allocate_memory_int_ptr(optimalNumBins); //A pointer to an array of histogram bins is initialized with a size equal to the optimal number of bins.
//Each bin is initialized to have a frequency count of 0.
for(int i=0; i<optimalNumBins;i++)
{
histogramBins[i] = 0;
}
// Populate bins
for (int i = 0; i < n; i++)
{
int bin_index = (int)((data[i] - min_value) / bin_width);
if (bin_index == optimalNumBins)
{
bin_index--; // Handle the edge case when data[i] == max_value
}
histogramBins[bin_index]++;
}
// Update the data array to hold histogram values for visualization
data = (double*)malloc(optimalNumBins * sizeof(double)); // Allocate memory for n pointers to double
if (data == NULL)
{
perror("\n\nError: Failed to allocate memory for data in 'compute_histogram'.");
exit(1);
}
// Output bins
for (int i = 0; i < optimalNumBins; i++)
{
double bin_start = min_value + i * bin_width;
double bin_end = bin_start + bin_width;
//printf("\nBin %d: [%lf, %lf): %d", i, bin_start, bin_end, histogramBins[i]);
data[i] = (bin_start + bin_end) * 0.5;
}
}
void print_histogram(Histogram histogram, char *label)
{
// Print all the details of the data structure
printf("\n\n\n\n\n\n\n\nprint_histogram %s ==============================================================================================\n", label);
printf("\n\n num_bins: %d", histogram.num_bins);
printf("\n bin_width: %.17g", histogram.bin_width);
printf("\n\n min_value: %.17g", histogram.min_value);
printf("\n max_value: %.17g", histogram.max_value);
printf("\n\n Histogram Bins: ");
for(int i = 0; i < histogram.num_bins; i++)
{
printf("\n bin %d: %d", i, histogram.bins[i]);
}
printf("\n\n\n==============================================================================================");
}
StatisticalReport analyze_plottable_data_file(const char *filePathName)
{
StatisticalReport report;
int n;
double *data = load_data_from_file_as_double(filePathName, &n);
report.mean = compute_mean(data, n);
report.std_dev = compute_standard_deviation(data, n, report.mean);
report.skewness = compute_skewness(data, n);
report.ad_stat = anderson_darling_normality_test(data, n);
// For a histogram, if numHistogramBins <= 0, automatically compute optimal:
report.histogram = compute_data_set_binning(data, n);
// p_value could be computed by comparing ad_stat to critical values (not implemented here)
//report.p_value = -1.0; // placeholder
free(data);
return report;
}
StatisticalReport analyze_numeric_data(double *data, int n, const char *outputDirectory, const char *fieldName)
{
StatisticalReport results;
double mean = compute_mean(data, n);
double std_dev = compute_standard_deviation(data, n, mean);
double skew = compute_skewness(data, n);
results.mean = mean;
results.std_dev = std_dev;
results.skewness = skew;
// Optionally, compute histogram:
Histogram hist = compute_data_set_binning(data, n);
// Write histogram to a file
{
char histFilePath[4096];
snprintf(histFilePath, sizeof(histFilePath), "%s/%s_histogram.txt", outputDirectory, fieldName);
FILE *histFile = fopen(histFilePath, "w+");
if (histFile) {
//fprintf(histFile, "# BinStart BinEnd Count\n");