From 3e2eb69e3a01028d1d0796970b1c81e261b03427 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Wed, 31 Jan 2024 15:26:20 +0100 Subject: [PATCH] display # of values above and below 90% confidence interval --- squiggle_more.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/squiggle_more.c b/squiggle_more.c index 6d42eee..8badbc7 100644 --- a/squiggle_more.c +++ b/squiggle_more.c @@ -334,8 +334,13 @@ void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins){ double bin_width = range / n_bins; // Fill the bins with sample counts + int below_min = 0, above_max = 0; for (int i = 0; i < n_samples; i++) { - if((x[i] > min_value) && (x[i] < max_value)){ + if(x[i] < min_value){ + below_min++; + }else if (x[i] > max_value){ + above_max++; + }else{ int bin_index = (int)((xs[i] - min_value) / bin_width); if (bin_index == n_bins) { bin_index--; // Last bin includes max_value @@ -355,16 +360,17 @@ void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins){ double scale = max_bin_count > MAX_WIDTH ? (double)MAX_WIDTH / max_bin_count : 1.0; // Print the histogram + int decimalPlaces = 1; + if((0 < bin_width) && (bin_width < 1)){ + int magnitude = (int) floor(log10(bin_width)); + decimalPlaces = -magnitude; + decimalPlaces = decimalPlaces > 10 ? 10 : decimalPlaces; + } + printf( " (-∞, %*.*f): %d\n", 4+decimalPlaces, decimalPlaces, min_value, below_min); for (int i = 0; i < n_bins; i++) { double bin_start = min_value + i * bin_width; double bin_end = bin_start + bin_width; - int decimalPlaces = 1; - if((0 < bin_width) && (bin_width < 1)){ - int magnitude = (int) floor(log10(bin_width)); - decimalPlaces = -magnitude; - decimalPlaces = decimalPlaces > 10 ? 10 : decimalPlaces; - } printf(" [%*.*f, %*.*f", 4+decimalPlaces, decimalPlaces, bin_start, 4+decimalPlaces, decimalPlaces, bin_end); char interval_delimiter = ')'; if(i == (n_bins-1)){ @@ -378,6 +384,7 @@ void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins){ } printf(" %d\n", bins[i]); } + printf( " (%*.*f, +∞): %d\n", 4+decimalPlaces, decimalPlaces, max_value, above_max); // Free the allocated memory for bins free(bins);