squiggle.c

Self-contained Monte Carlo estimation in C99
Log | Files | Refs | README

commit d3af874403141eda80e8d6da18f807a127ec4313
parent 6387c0df703c3039a72c51d1e2dfca3d68202b9d
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Sat, 18 Nov 2023 23:54:31 +0000

feat: start adding paralellism; recompile.

Diffstat:
MREADME.md | 9+++++----
Mexamples/00_example_template/example | 0
Mexamples/04_sample_from_cdf_simple/example | 0
Mexamples/04_sample_from_cdf_simple/example.c | 2+-
Mexamples/04_sample_from_cdf_simple/makefile | 5+++--
Mexamples/05_sample_from_cdf_beta/example | 0
Mexamples/05_sample_from_cdf_beta/example.c | 2+-
Mexamples/05_sample_from_cdf_beta/makefile | 2+-
Mexamples/07_ci_beta/example | 0
Mexamples/07_ci_beta/example.c | 2+-
Mexamples/07_ci_beta/makefile | 2+-
Mexamples/08_nuclear_war/example | 0
Mexamples/08_nuclear_war/example.c | 2+-
Mexamples/08_nuclear_war/makefile | 2+-
Mexamples/09_burn_10kg_fat/example | 0
Mexamples/09_burn_10kg_fat/example.c | 2+-
Mexamples/09_burn_10kg_fat/makefile | 2+-
Mexamples/10_nuclear_recovery/example | 0
Mexamples/10_nuclear_recovery/example.c | 2+-
Mexamples/10_nuclear_recovery/makefile | 2+-
Mexamples/11_algebra/example | 0
Mexamples/11_algebra/example.c | 2+-
Mexamples/11_algebra/makefile | 2+-
Mexamples/12_algebra_and_conversion/example | 0
Mexamples/12_algebra_and_conversion/example.c | 6+++---
Mexamples/12_algebra_and_conversion/makefile | 2+-
Mexamples/13_ergonomic_algebra/example | 0
Mexamples/13_ergonomic_algebra/example.c | 2+-
Mexamples/13_ergonomic_algebra/makefile | 2+-
Mexamples/14_twitter_thread_example/example | 0
Mexamples/14_twitter_thread_example/example.c | 2+-
Mexamples/14_twitter_thread_example/makefile | 2+-
Dextra.c | 305------------------------------------------------------------------------------
Mscratchpad/scratchpad | 0
Mscratchpad/scratchpad.c | 6++++--
Msquiggle.c | 8--------
Asquiggle_more.c | 339+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rextra.h -> squiggle_more.h | 0
38 files changed, 372 insertions(+), 342 deletions(-)

diff --git a/README.md b/README.md @@ -301,7 +301,8 @@ Overall, I'd describe the error handling capabilities of this library as pretty ## To do list -- [ ] Think about whether to write a simple version of this for [uxn](https://100r.co/site/uxn.html), a minimalistic portable programming stack which, sadly, doesn't have doubles (64 bit floats) +- [ ] Document paralellism +- [ ] Document confidence intervals - [ ] Point out that, even though the C standard is ambiguous about this, this code assumes that doubles are 64 bit precision (otherwise the xorshift should be different). - [ ] Document rudimentary algebra manipulations for normal/lognormal - [ ] Think through whether to delete cdf => samples function @@ -310,11 +311,11 @@ Overall, I'd describe the error handling capabilities of this library as pretty - complexify and use boxes for everything - leave as is - [ ] Systematize references -- [ ] Publish online - [ ] Support all distribution functions in <https://www.squiggle-language.com/docs/Api/Dist> - [ ] do so efficiently - [ ] Add more functions to do algebra and get the 90% c.i. of normals, lognormals, betas, etc. - Think through which of these make sense. +- [ ] Disambiguate sample_laplace--successes vs failures || successes vs total trials as two distinct and differently named functions ## Done @@ -373,5 +374,5 @@ Overall, I'd describe the error handling capabilities of this library as pretty - [ ] Consider desirability of defining shortcuts for those functions. Adds a level of magic, though. - [ ] Test results - [x] Move to own file? Or signpost in file? => signposted in file. -- [ ] Disambiguate sample_laplace--successes vs failures || successes vs total trials as two distinct and differently named functions -- [ ] Write twitter thread. +- [x] Write twitter thread: now [here](https://twitter.com/NunoSempere/status/1707041153210564959); retweets appreciated. +- [ ] ~~Think about whether to write a simple version of this for [uxn](https://100r.co/site/uxn.html), a minimalistic portable programming stack which, sadly, doesn't have doubles (64 bit floats)~~ diff --git a/examples/00_example_template/example b/examples/00_example_template/example Binary files differ. diff --git a/examples/04_sample_from_cdf_simple/example b/examples/04_sample_from_cdf_simple/example Binary files differ. diff --git a/examples/04_sample_from_cdf_simple/example.c b/examples/04_sample_from_cdf_simple/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> diff --git a/examples/04_sample_from_cdf_simple/makefile b/examples/04_sample_from_cdf_simple/makefile @@ -9,12 +9,13 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies +OPENMP=-fopenmp MATH=-lm -DEPENDENCIES=$(MATH) +DEPENDENCIES=$(MATH) $(OPENMP) # OPENMP=-fopenmp ## Flags diff --git a/examples/05_sample_from_cdf_beta/example b/examples/05_sample_from_cdf_beta/example Binary files differ. diff --git a/examples/05_sample_from_cdf_beta/example.c b/examples/05_sample_from_cdf_beta/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> diff --git a/examples/05_sample_from_cdf_beta/makefile b/examples/05_sample_from_cdf_beta/makefile @@ -9,7 +9,7 @@ # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/07_ci_beta/example b/examples/07_ci_beta/example Binary files differ. diff --git a/examples/07_ci_beta/example.c b/examples/07_ci_beta/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/examples/07_ci_beta/makefile b/examples/07_ci_beta/makefile @@ -9,7 +9,7 @@ CC=gcc # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=example ## Dependencies diff --git a/examples/08_nuclear_war/example b/examples/08_nuclear_war/example Binary files differ. diff --git a/examples/08_nuclear_war/example.c b/examples/08_nuclear_war/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> diff --git a/examples/08_nuclear_war/makefile b/examples/08_nuclear_war/makefile @@ -9,7 +9,7 @@ CC=gcc # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=example ## Dependencies diff --git a/examples/09_burn_10kg_fat/example b/examples/09_burn_10kg_fat/example Binary files differ. diff --git a/examples/09_burn_10kg_fat/example.c b/examples/09_burn_10kg_fat/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> diff --git a/examples/09_burn_10kg_fat/makefile b/examples/09_burn_10kg_fat/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/10_nuclear_recovery/example b/examples/10_nuclear_recovery/example Binary files differ. diff --git a/examples/10_nuclear_recovery/example.c b/examples/10_nuclear_recovery/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> diff --git a/examples/10_nuclear_recovery/makefile b/examples/10_nuclear_recovery/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/11_algebra/example b/examples/11_algebra/example Binary files differ. diff --git a/examples/11_algebra/example.c b/examples/11_algebra/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> diff --git a/examples/11_algebra/makefile b/examples/11_algebra/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/12_algebra_and_conversion/example b/examples/12_algebra_and_conversion/example Binary files differ. diff --git a/examples/12_algebra_and_conversion/example.c b/examples/12_algebra_and_conversion/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> @@ -17,10 +17,10 @@ int main() printf("The 90%% confidence interval of Lognormal(%f, %f) is [%f, %f]\n", ln1.logmean, ln1.logstd, ln1_ci.low, ln1_ci.high); - lognormal_params ln1_ci_paramas = convert_ci_to_lognormal_params(ln1_ci); + lognormal_params ln1_params2 = convert_ci_to_lognormal_params(ln1_ci); printf("The lognormal which has 90%% confidence interval [%f, %f] is Lognormal(%f, %f)\n", ln1_ci.low, ln1_ci.high, - ln1.logmean, ln1.logstd); + ln1_params2.logmean, ln1_params2.logstd); lognormal_params ln2 = convert_ci_to_lognormal_params((ci) { .low = 1, .high = 10 }); lognormal_params ln3 = convert_ci_to_lognormal_params((ci) { .low = 5, .high = 50 }); diff --git a/examples/12_algebra_and_conversion/makefile b/examples/12_algebra_and_conversion/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/13_ergonomic_algebra/example b/examples/13_ergonomic_algebra/example Binary files differ. diff --git a/examples/13_ergonomic_algebra/example.c b/examples/13_ergonomic_algebra/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> diff --git a/examples/13_ergonomic_algebra/makefile b/examples/13_ergonomic_algebra/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/14_twitter_thread_example/example b/examples/14_twitter_thread_example/example Binary files differ. diff --git a/examples/14_twitter_thread_example/example.c b/examples/14_twitter_thread_example/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/examples/14_twitter_thread_example/makefile b/examples/14_twitter_thread_example/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/extra.c b/extra.c @@ -1,305 +0,0 @@ -#include "squiggle.h" -#include <float.h> -#include <limits.h> -#include <math.h> -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> -#include <sys/types.h> -#include <time.h> - -// math constants -#define PI 3.14159265358979323846 // M_PI in gcc gnu99 -#define NORMAL90CONFIDENCE 1.6448536269514727 - -// Some error niceties; these won't be used until later -#define MAX_ERROR_LENGTH 500 -#define EXIT_ON_ERROR 0 -#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) - -// Get confidence intervals, given a sampler -// Not in core yet because I'm not sure how much I like the interface, -// to do: add n to function parameters and document - -typedef struct ci_t { - float low; - float high; -} ci; -int compare_doubles(const void* p, const void* q) -{ - // https://wikiless.esmailelbob.xyz/wiki/Qsort?lang=en - double x = *(const double*)p; - double y = *(const double*)q; - - /* Avoid returning x - y, which can cause undefined behaviour - because of signed integer overflow. */ - if (x < y) - return -1; // Return -1 if you want ascending, 1 if you want descending order. - else if (x > y) - return 1; // Return 1 if you want ascending, -1 if you want descending order. - - return 0; -} -ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed) -{ - int n = 100 * 1000; - double* samples_array = malloc(n * sizeof(double)); - for (int i = 0; i < n; i++) { - samples_array[i] = sampler(seed); - } - qsort(samples_array, n, sizeof(double), compare_doubles); - - ci result = { - .low = samples_array[5000], - .high = samples_array[94999], - }; - free(samples_array); - - return result; -} - -// ## Sample from an arbitrary cdf -struct box { - int empty; - double content; - char* error_msg; -}; - -struct box process_error(const char* error_msg, int should_exit, char* file, int line) -{ - if (should_exit) { - printf("@, in %s (%d)", file, line); - exit(1); - } else { - char error_msg[MAX_ERROR_LENGTH]; - snprintf(error_msg, MAX_ERROR_LENGTH, "@, in %s (%d)", file, line); // NOLINT: We are being carefull here by considering MAX_ERROR_LENGTH explicitly. - struct box error = { .empty = 1, .error_msg = error_msg }; - return error; - } -} - -// Inverse cdf at point -// Two versions of this function: -// - raw, dealing with cdfs that return doubles -// - input: cdf: double => double, p -// - output: Box(number|error) -// - box, dealing with cdfs that return a box. -// - input: cdf: double => Box(number|error), p -// - output: Box(number|error) -struct box inverse_cdf_double(double cdf(double), double p) -{ - // given a cdf: [-Inf, Inf] => [0,1] - // returns a box with either - // x such that cdf(x) = p - // or an error - // if EXIT_ON_ERROR is set to 1, it exits instead of providing an error - - double low = -1.0; - double high = 1.0; - - // 1. Make sure that cdf(low) < p < cdf(high) - int interval_found = 0; - while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) { - // ^ Using FLT_MIN and FLT_MAX is overkill - // but it's also the *correct* thing to do. - - int low_condition = (cdf(low) < p); - int high_condition = (p < cdf(high)); - if (low_condition && high_condition) { - interval_found = 1; - } else if (!low_condition) { - low = low * 2; - } else if (!high_condition) { - high = high * 2; - } - } - - if (!interval_found) { - return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf"); - } else { - - int convergence_condition = 0; - int count = 0; - while (!convergence_condition && (count < (INT_MAX / 2))) { - double mid = (high + low) / 2; - int mid_not_new = (mid == low) || (mid == high); - // double width = high - low; - // if ((width < 1e-8) || mid_not_new){ - if (mid_not_new) { - convergence_condition = 1; - } else { - double mid_sign = cdf(mid) - p; - if (mid_sign < 0) { - low = mid; - } else if (mid_sign > 0) { - high = mid; - } else if (mid_sign == 0) { - low = mid; - high = mid; - } - } - } - - if (convergence_condition) { - struct box result = { .empty = 0, .content = low }; - return result; - } else { - return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); - } - } -} - -struct box inverse_cdf_box(struct box cdf_box(double), double p) -{ - // given a cdf: [-Inf, Inf] => Box([0,1]) - // returns a box with either - // x such that cdf(x) = p - // or an error - // if EXIT_ON_ERROR is set to 1, it exits instead of providing an error - - double low = -1.0; - double high = 1.0; - - // 1. Make sure that cdf(low) < p < cdf(high) - int interval_found = 0; - while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) { - // ^ Using FLT_MIN and FLT_MAX is overkill - // but it's also the *correct* thing to do. - struct box cdf_low = cdf_box(low); - if (cdf_low.empty) { - return PROCESS_ERROR(cdf_low.error_msg); - } - - struct box cdf_high = cdf_box(high); - if (cdf_high.empty) { - return PROCESS_ERROR(cdf_low.error_msg); - } - - int low_condition = (cdf_low.content < p); - int high_condition = (p < cdf_high.content); - if (low_condition && high_condition) { - interval_found = 1; - } else if (!low_condition) { - low = low * 2; - } else if (!high_condition) { - high = high * 2; - } - } - - if (!interval_found) { - return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf"); - } else { - - int convergence_condition = 0; - int count = 0; - while (!convergence_condition && (count < (INT_MAX / 2))) { - double mid = (high + low) / 2; - int mid_not_new = (mid == low) || (mid == high); - // double width = high - low; - if (mid_not_new) { - // if ((width < 1e-8) || mid_not_new){ - convergence_condition = 1; - } else { - struct box cdf_mid = cdf_box(mid); - if (cdf_mid.empty) { - return PROCESS_ERROR(cdf_mid.error_msg); - } - double mid_sign = cdf_mid.content - p; - if (mid_sign < 0) { - low = mid; - } else if (mid_sign > 0) { - high = mid; - } else if (mid_sign == 0) { - low = mid; - high = mid; - } - } - } - - if (convergence_condition) { - struct box result = { .empty = 0, .content = low }; - return result; - } else { - return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); - } - } -} - -// Sampler based on inverse cdf and randomness function -struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed) -{ - double p = sample_unit_uniform(seed); - struct box result = inverse_cdf_box(cdf, p); - return result; -} -struct box sampler_cdf_double(double cdf(double), uint64_t* seed) -{ - double p = sample_unit_uniform(seed); - struct box result = inverse_cdf_double(cdf, p); - return result; -} - -/* Could also define other variations, e.g., -double sampler_danger(struct box cdf(double), uint64_t* seed) -{ - double p = sample_unit_uniform(seed); - struct box result = inverse_cdf_box(cdf, p); - if(result.empty){ - exit(1); - }else{ - return result.content; - } -} -*/ - -// # Small algebra manipulations - -// here I discover named structs, -// which mean that I don't have to be typing -// struct blah all the time. -typedef struct normal_params_t { - double mean; - double std; -} normal_params; - -normal_params algebra_sum_normals(normal_params a, normal_params b) -{ - normal_params result = { - .mean = a.mean + b.mean, - .std = sqrt((a.std * a.std) + (b.std * b.std)), - }; - return result; -} - -typedef struct lognormal_params_t { - double logmean; - double logstd; -} lognormal_params; - -lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params b) -{ - lognormal_params result = { - .logmean = a.logmean + b.logmean, - .logstd = sqrt((a.logstd * a.logstd) + (b.logstd * b.logstd)), - }; - return result; -} - -lognormal_params convert_ci_to_lognormal_params(ci x) -{ - double loghigh = logf(x.high); - double loglow = logf(x.low); - double logmean = (loghigh + loglow) / 2.0; - double logstd = (loghigh - loglow) / (2.0 * NORMAL90CONFIDENCE); - lognormal_params result = { .logmean = logmean, .logstd = logstd }; - return result; -} - -ci convert_lognormal_params_to_ci(lognormal_params y) -{ - double h = y.logstd * NORMAL90CONFIDENCE; - double loghigh = y.logmean + h; - double loglow = y.logmean - h; - ci result = { .low = exp(loglow), .high = exp(loghigh) }; - return result; -} diff --git a/scratchpad/scratchpad b/scratchpad/scratchpad Binary files differ. diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c @@ -9,12 +9,14 @@ int main() // set randomness seed uint64_t* seed = malloc(sizeof(uint64_t)); *seed = 1000; // xorshift can't start with a seed of 0 - + /* for (int i = 0; i < 100; i++) { double draw = sample_unit_uniform(seed); printf("%f\n", draw); - } + }*/ + // Test division + printf("\n%d\n", 10 % 3); free(seed); } diff --git a/squiggle.c b/squiggle.c @@ -1,15 +1,7 @@ -#include <float.h> #include <limits.h> #include <math.h> #include <stdint.h> -#include <stdio.h> #include <stdlib.h> -#include <sys/types.h> -#include <time.h> - -// # Key functionality -// Define the minimum number of functions needed to do simple estimation -// Starts here, ends until the end of the mixture function // math constants #define PI 3.14159265358979323846 // M_PI in gcc gnu99 diff --git a/squiggle_more.c b/squiggle_more.c @@ -0,0 +1,339 @@ +#include <float.h> +#include <math.h> +#include <limits.h> +#include <omp.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include "squiggle.h" + +// math constants +#define PI 3.14159265358979323846 // M_PI in gcc gnu99 +#define NORMAL90CONFIDENCE 1.6448536269514727 + +// Some error niceties; these won't be used until later +#define MAX_ERROR_LENGTH 500 +#define EXIT_ON_ERROR 0 +#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) + +// Get confidence intervals, given a sampler +// Not in core yet because I'm not sure how much I like the struct +// and the built-in 100k samples +// to do: add n to function parameters and document + +typedef struct ci_t { + float low; + float high; +} ci; +int compare_doubles(const void* p, const void* q) +{ + // https://wikiless.esmailelbob.xyz/wiki/Qsort?lang=en + double x = *(const double*)p; + double y = *(const double*)q; + + /* Avoid returning x - y, which can cause undefined behaviour + because of signed integer overflow. */ + if (x < y) + return -1; // Return -1 if you want ascending, 1 if you want descending order. + else if (x > y) + return 1; // Return 1 if you want ascending, -1 if you want descending order. + + return 0; +} +ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed) +{ + int n = 100 * 1000; + double* samples_array = malloc(n * sizeof(double)); + for (int i = 0; i < n; i++) { + samples_array[i] = sampler(seed); + } + qsort(samples_array, n, sizeof(double), compare_doubles); + + ci result = { + .low = samples_array[5000], + .high = samples_array[94999], + }; + free(samples_array); + + return result; +} + +// ## Sample from an arbitrary cdf +struct box { + int empty; + double content; + char* error_msg; +}; + +struct box process_error(const char* error_msg, int should_exit, char* file, int line) +{ + if (should_exit) { + printf("@, in %s (%d)", file, line); + exit(1); + } else { + char error_msg[MAX_ERROR_LENGTH]; + snprintf(error_msg, MAX_ERROR_LENGTH, "@, in %s (%d)", file, line); // NOLINT: We are being carefull here by considering MAX_ERROR_LENGTH explicitly. + struct box error = { .empty = 1, .error_msg = error_msg }; + return error; + } +} + +// Inverse cdf at point +// Two versions of this function: +// - raw, dealing with cdfs that return doubles +// - input: cdf: double => double, p +// - output: Box(number|error) +// - box, dealing with cdfs that return a box. +// - input: cdf: double => Box(number|error), p +// - output: Box(number|error) +struct box inverse_cdf_double(double cdf(double), double p) +{ + // given a cdf: [-Inf, Inf] => [0,1] + // returns a box with either + // x such that cdf(x) = p + // or an error + // if EXIT_ON_ERROR is set to 1, it exits instead of providing an error + + double low = -1.0; + double high = 1.0; + + // 1. Make sure that cdf(low) < p < cdf(high) + int interval_found = 0; + while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) { + // ^ Using FLT_MIN and FLT_MAX is overkill + // but it's also the *correct* thing to do. + + int low_condition = (cdf(low) < p); + int high_condition = (p < cdf(high)); + if (low_condition && high_condition) { + interval_found = 1; + } else if (!low_condition) { + low = low * 2; + } else if (!high_condition) { + high = high * 2; + } + } + + if (!interval_found) { + return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf"); + } else { + + int convergence_condition = 0; + int count = 0; + while (!convergence_condition && (count < (INT_MAX / 2))) { + double mid = (high + low) / 2; + int mid_not_new = (mid == low) || (mid == high); + // double width = high - low; + // if ((width < 1e-8) || mid_not_new){ + if (mid_not_new) { + convergence_condition = 1; + } else { + double mid_sign = cdf(mid) - p; + if (mid_sign < 0) { + low = mid; + } else if (mid_sign > 0) { + high = mid; + } else if (mid_sign == 0) { + low = mid; + high = mid; + } + } + } + + if (convergence_condition) { + struct box result = { .empty = 0, .content = low }; + return result; + } else { + return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); + } + } +} + +struct box inverse_cdf_box(struct box cdf_box(double), double p) +{ + // given a cdf: [-Inf, Inf] => Box([0,1]) + // returns a box with either + // x such that cdf(x) = p + // or an error + // if EXIT_ON_ERROR is set to 1, it exits instead of providing an error + + double low = -1.0; + double high = 1.0; + + // 1. Make sure that cdf(low) < p < cdf(high) + int interval_found = 0; + while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) { + // ^ Using FLT_MIN and FLT_MAX is overkill + // but it's also the *correct* thing to do. + struct box cdf_low = cdf_box(low); + if (cdf_low.empty) { + return PROCESS_ERROR(cdf_low.error_msg); + } + + struct box cdf_high = cdf_box(high); + if (cdf_high.empty) { + return PROCESS_ERROR(cdf_low.error_msg); + } + + int low_condition = (cdf_low.content < p); + int high_condition = (p < cdf_high.content); + if (low_condition && high_condition) { + interval_found = 1; + } else if (!low_condition) { + low = low * 2; + } else if (!high_condition) { + high = high * 2; + } + } + + if (!interval_found) { + return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf"); + } else { + + int convergence_condition = 0; + int count = 0; + while (!convergence_condition && (count < (INT_MAX / 2))) { + double mid = (high + low) / 2; + int mid_not_new = (mid == low) || (mid == high); + // double width = high - low; + if (mid_not_new) { + // if ((width < 1e-8) || mid_not_new){ + convergence_condition = 1; + } else { + struct box cdf_mid = cdf_box(mid); + if (cdf_mid.empty) { + return PROCESS_ERROR(cdf_mid.error_msg); + } + double mid_sign = cdf_mid.content - p; + if (mid_sign < 0) { + low = mid; + } else if (mid_sign > 0) { + high = mid; + } else if (mid_sign == 0) { + low = mid; + high = mid; + } + } + } + + if (convergence_condition) { + struct box result = { .empty = 0, .content = low }; + return result; + } else { + return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); + } + } +} + +// Sampler based on inverse cdf and randomness function +struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed) +{ + double p = sample_unit_uniform(seed); + struct box result = inverse_cdf_box(cdf, p); + return result; +} +struct box sampler_cdf_double(double cdf(double), uint64_t* seed) +{ + double p = sample_unit_uniform(seed); + struct box result = inverse_cdf_double(cdf, p); + return result; +} + +/* Could also define other variations, e.g., +double sampler_danger(struct box cdf(double), uint64_t* seed) +{ + double p = sample_unit_uniform(seed); + struct box result = inverse_cdf_box(cdf, p); + if(result.empty){ + exit(1); + }else{ + return result.content; + } +} +*/ + +// # Small algebra manipulations + +// here I discover named structs, +// which mean that I don't have to be typing +// struct blah all the time. +typedef struct normal_params_t { + double mean; + double std; +} normal_params; + +normal_params algebra_sum_normals(normal_params a, normal_params b) +{ + normal_params result = { + .mean = a.mean + b.mean, + .std = sqrt((a.std * a.std) + (b.std * b.std)), + }; + return result; +} + +typedef struct lognormal_params_t { + double logmean; + double logstd; +} lognormal_params; + +lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params b) +{ + lognormal_params result = { + .logmean = a.logmean + b.logmean, + .logstd = sqrt((a.logstd * a.logstd) + (b.logstd * b.logstd)), + }; + return result; +} + +lognormal_params convert_ci_to_lognormal_params(ci x) +{ + double loghigh = logf(x.high); + double loglow = logf(x.low); + double logmean = (loghigh + loglow) / 2.0; + double logstd = (loghigh - loglow) / (2.0 * NORMAL90CONFIDENCE); + lognormal_params result = { .logmean = logmean, .logstd = logstd }; + return result; +} + +ci convert_lognormal_params_to_ci(lognormal_params y) +{ + double h = y.logstd * NORMAL90CONFIDENCE; + double loghigh = y.logmean + h; + double loglow = y.logmean - h; + ci result = { .low = exp(loglow), .high = exp(loghigh) }; + return result; +} + +// Paralellism +/* +void paralellize(float (*sampler)(uint64_t* seed), float* results, int n_threads, int n_samples){ + if((n_samples % n_threads) != 0){ + fprintf(stderr, "Number of samples isn't divisible by number of threads, aborting\n"); + exit(1); + } + uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*)); + for (uint64_t i = 0; i < n_threads; i++) { + seeds[i] = malloc(sizeof(uint64_t)); + *seeds[i] = i + 1; // xorshift can't start with 0 + } + + int i; + #pragma omp parallel private(i) + { + #pragma omp for + for (i = 0; i < n_threads; i++) { + int lower_bound = i * (n_samples / n_threads); + int upper_bound = ((i+1) * (n_samples / n_threads)) - 1; + // printf("Lower bound: %d, upper bound: %d\n", lower_bound, upper_bound); + for (int j = lower_bound; j < upper_bound; j++) { + results[j] = sampler(seeds[i]); + } + } + } + + for (uint64_t i = 0; i < n_threads; i++) { + free(seeds[i]); + } + free(seeds); +} +*/ diff --git a/extra.h b/squiggle_more.h