forked from personal/squiggle.c
make format
This commit is contained in:
parent
19367b04cd
commit
4874f1fa9c
|
@ -189,7 +189,8 @@ ci array_get_90_ci(double xs[], int n)
|
||||||
return array_get_ci((ci) { .low = 0.05, .high = 0.95 }, xs, n);
|
return array_get_ci((ci) { .low = 0.05, .high = 0.95 }, xs, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
double array_get_median(double xs[], int n){
|
double array_get_median(double xs[], int n)
|
||||||
|
{
|
||||||
int median_k = (int)floor(0.5 * n);
|
int median_k = (int)floor(0.5 * n);
|
||||||
return quickselect(median_k, xs, n);
|
return quickselect(median_k, xs, n);
|
||||||
}
|
}
|
||||||
|
@ -205,7 +206,8 @@ void array_print(double xs[], int n)
|
||||||
printf("]\n");
|
printf("]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void array_print_stats(double xs[], int n){
|
void array_print_stats(double xs[], int n)
|
||||||
|
{
|
||||||
ci ci_90 = array_get_ci((ci) { .low = 0.05, .high = 0.95 }, xs, n);
|
ci ci_90 = array_get_ci((ci) { .low = 0.05, .high = 0.95 }, xs, n);
|
||||||
ci ci_80 = array_get_ci((ci) { .low = 0.1, .high = 0.9 }, xs, n);
|
ci ci_80 = array_get_ci((ci) { .low = 0.1, .high = 0.9 }, xs, n);
|
||||||
ci ci_50 = array_get_ci((ci) { .low = 0.25, .high = 0.75 }, xs, n);
|
ci ci_50 = array_get_ci((ci) { .low = 0.25, .high = 0.75 }, xs, n);
|
||||||
|
@ -224,8 +226,8 @@ void array_print_stats(double xs[], int n){
|
||||||
mean, std, ci_90.low, ci_80.low, ci_50.low, median, ci_50.high, ci_80.high, ci_90.high);
|
mean, std, ci_90.low, ci_80.low, ci_50.low, median, ci_50.high, ci_80.high, ci_90.high);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void array_print_histogram(double* xs, int n_samples, int n_bins)
|
||||||
void array_print_histogram(double* xs, int n_samples, int n_bins) {
|
{
|
||||||
// Interface inspired by <https://github.com/red-data-tools/YouPlot>
|
// Interface inspired by <https://github.com/red-data-tools/YouPlot>
|
||||||
// Generated with the help of an llm; there might be subtle off-by-one errors
|
// Generated with the help of an llm; there might be subtle off-by-one errors
|
||||||
if (n_bins <= 1) {
|
if (n_bins <= 1) {
|
||||||
|
@ -236,7 +238,7 @@ void array_print_histogram(double* xs, int n_samples, int n_bins) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int *bins = (int*) calloc((size_t)n_bins, sizeof(int));
|
int* bins = (int*)calloc((size_t)n_bins, sizeof(int));
|
||||||
if (bins == NULL) {
|
if (bins == NULL) {
|
||||||
fprintf(stderr, "Memory allocation for bins failed.\n");
|
fprintf(stderr, "Memory allocation for bins failed.\n");
|
||||||
return;
|
return;
|
||||||
|
@ -286,14 +288,14 @@ void array_print_histogram(double* xs, int n_samples, int n_bins) {
|
||||||
double bin_end = bin_start + bin_width;
|
double bin_end = bin_start + bin_width;
|
||||||
|
|
||||||
int decimalPlaces = 1;
|
int decimalPlaces = 1;
|
||||||
if((0 < bin_width) && (bin_width < 1)){
|
if ((0 < bin_width) && (bin_width < 1)) {
|
||||||
int magnitude = (int) floor(log10(bin_width));
|
int magnitude = (int)floor(log10(bin_width));
|
||||||
decimalPlaces = -magnitude;
|
decimalPlaces = -magnitude;
|
||||||
decimalPlaces = decimalPlaces > 10 ? 10 : decimalPlaces;
|
decimalPlaces = decimalPlaces > 10 ? 10 : decimalPlaces;
|
||||||
}
|
}
|
||||||
printf("[%*.*f, %*.*f", 4+decimalPlaces, decimalPlaces, bin_start, 4+decimalPlaces, decimalPlaces, bin_end);
|
printf("[%*.*f, %*.*f", 4 + decimalPlaces, decimalPlaces, bin_start, 4 + decimalPlaces, decimalPlaces, bin_end);
|
||||||
char interval_delimiter = ')';
|
char interval_delimiter = ')';
|
||||||
if(i == (n_bins-1)){
|
if (i == (n_bins - 1)) {
|
||||||
interval_delimiter = ']'; // last bucket is inclusive
|
interval_delimiter = ']'; // last bucket is inclusive
|
||||||
}
|
}
|
||||||
printf("%c: ", interval_delimiter);
|
printf("%c: ", interval_delimiter);
|
||||||
|
@ -309,7 +311,8 @@ void array_print_histogram(double* xs, int n_samples, int n_bins) {
|
||||||
free(bins);
|
free(bins);
|
||||||
}
|
}
|
||||||
|
|
||||||
void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins){
|
void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins)
|
||||||
|
{
|
||||||
// Code duplicated from previous function
|
// Code duplicated from previous function
|
||||||
// I'll consider simplifying it at some future point
|
// I'll consider simplifying it at some future point
|
||||||
// Possible ideas:
|
// Possible ideas:
|
||||||
|
@ -325,7 +328,7 @@ void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int *bins = (int*) calloc((size_t)n_bins, sizeof(int));
|
int* bins = (int*)calloc((size_t)n_bins, sizeof(int));
|
||||||
if (bins == NULL) {
|
if (bins == NULL) {
|
||||||
fprintf(stderr, "Memory allocation for bins failed.\n");
|
fprintf(stderr, "Memory allocation for bins failed.\n");
|
||||||
return;
|
return;
|
||||||
|
@ -342,11 +345,11 @@ void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins){
|
||||||
// Fill the bins with sample counts
|
// Fill the bins with sample counts
|
||||||
int below_min = 0, above_max = 0;
|
int below_min = 0, above_max = 0;
|
||||||
for (int i = 0; i < n_samples; i++) {
|
for (int i = 0; i < n_samples; i++) {
|
||||||
if(xs[i] < min_value){
|
if (xs[i] < min_value) {
|
||||||
below_min++;
|
below_min++;
|
||||||
}else if (xs[i] > max_value){
|
} else if (xs[i] > max_value) {
|
||||||
above_max++;
|
above_max++;
|
||||||
}else{
|
} else {
|
||||||
int bin_index = (int)((xs[i] - min_value) / bin_width);
|
int bin_index = (int)((xs[i] - min_value) / bin_width);
|
||||||
if (bin_index == n_bins) {
|
if (bin_index == n_bins) {
|
||||||
bin_index--; // Last bin includes max_value
|
bin_index--; // Last bin includes max_value
|
||||||
|
@ -367,19 +370,19 @@ void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins){
|
||||||
|
|
||||||
// Print the histogram
|
// Print the histogram
|
||||||
int decimalPlaces = 1;
|
int decimalPlaces = 1;
|
||||||
if((0 < bin_width) && (bin_width < 1)){
|
if ((0 < bin_width) && (bin_width < 1)) {
|
||||||
int magnitude = (int) floor(log10(bin_width));
|
int magnitude = (int)floor(log10(bin_width));
|
||||||
decimalPlaces = -magnitude;
|
decimalPlaces = -magnitude;
|
||||||
decimalPlaces = decimalPlaces > 10 ? 10 : decimalPlaces;
|
decimalPlaces = decimalPlaces > 10 ? 10 : decimalPlaces;
|
||||||
}
|
}
|
||||||
printf( "(-∞, %*.*f): %d\n", 4+decimalPlaces, decimalPlaces, min_value, below_min);
|
printf("(-∞, %*.*f): %d\n", 4 + decimalPlaces, decimalPlaces, min_value, below_min);
|
||||||
for (int i = 0; i < n_bins; i++) {
|
for (int i = 0; i < n_bins; i++) {
|
||||||
double bin_start = min_value + i * bin_width;
|
double bin_start = min_value + i * bin_width;
|
||||||
double bin_end = bin_start + bin_width;
|
double bin_end = bin_start + bin_width;
|
||||||
|
|
||||||
printf("[%*.*f, %*.*f", 4+decimalPlaces, decimalPlaces, bin_start, 4+decimalPlaces, decimalPlaces, bin_end);
|
printf("[%*.*f, %*.*f", 4 + decimalPlaces, decimalPlaces, bin_start, 4 + decimalPlaces, decimalPlaces, bin_end);
|
||||||
char interval_delimiter = ')';
|
char interval_delimiter = ')';
|
||||||
if(i == (n_bins-1)){
|
if (i == (n_bins - 1)) {
|
||||||
interval_delimiter = ']'; // last bucket is inclusive
|
interval_delimiter = ']'; // last bucket is inclusive
|
||||||
}
|
}
|
||||||
printf("%c: ", interval_delimiter);
|
printf("%c: ", interval_delimiter);
|
||||||
|
@ -390,7 +393,7 @@ void array_print_90_ci_histogram(double* xs, int n_samples, int n_bins){
|
||||||
}
|
}
|
||||||
printf(" %d\n", bins[i]);
|
printf(" %d\n", bins[i]);
|
||||||
}
|
}
|
||||||
printf( "(%*.*f, +∞): %d\n", 4+decimalPlaces, decimalPlaces, max_value, above_max);
|
printf("(%*.*f, +∞): %d\n", 4 + decimalPlaces, decimalPlaces, max_value, above_max);
|
||||||
|
|
||||||
// Free the allocated memory for bins
|
// Free the allocated memory for bins
|
||||||
free(bins);
|
free(bins);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user