diff --git a/README.md b/README.md index 9a37ce7..51c0c67 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,6 @@ This library provides some basic building blocks. The recommended strategy is to ### Cdf auxiliary functions -To help with the above core strategy, this library provides convenience functions, which take a cdf, and return a sample from the distribution produced by that cdf. This might make it easier to program models, at the cost of a 20x to 60x slowdown in the parts of the code that use it. ### Nested functions and compilation with tcc. @@ -60,29 +59,6 @@ GCC has an extension which allows a program to define a function inside another My recommendation would be to use tcc while drawing a small number of samples for fast iteration, and then using gcc for the final version with lots of samples, and possibly with nested functions for ease of reading by others. -### Error propagation vs exiting on error - -The process of taking a cdf and returning a sample might fail, e.g., it's a Newton method which might fail to converge because of cdf artifacts. The cdf itself might also fail, e.g., if a distribution only accepts a range of parameters, but is fed parameters outside that range. - -This library provides two approaches: - -1. Print the line and function in which the error occured, then exit on error -2. In situations where there might be an error, return a struct containing either the correct value or an error message: - -```C -struct box { - int empty; - double content; - char* error_msg; -}; -``` - -The first approach produces terser programs but might not scale. The second approach seems like it could lead to more robust programmes, but is more verbose. - -Behaviour on error can be toggled by the `EXIT_ON_ERROR` variable. This library also provides a convenient macro, `PROCESS_ERROR`, to make error handling in either case much terser—see the usage in example 4 in the examples/ folder. - -Overall, I'd describe the error handling capabilities of this library as pretty rudimentary. For example, this program might fail in surprising ways if you ask for a lognormal with negative standard deviation, because I haven't added error checking for that case yet. - ### Guarantees and licensing - I offer no guarantees about stability, correctness, performance, etc. I might, for instance, abandon the version in C and rewrite it in Zig, Nim or Rust. @@ -272,6 +248,49 @@ make tidy It emits one warning about something I already took care of, so by default I've suppressed it. I think this is good news in terms of making me more confident that this simple library is correct :). +### Division between core functions and extraneous expansions + +This library differentiates between core functions, which are pretty tightly scoped, and expansions and convenience functions, which are more meandering. Expansions are in `extra.c` and `extra.h`. To use them, take care to link them: + +``` +// In your C source file +#include "extra.h" +``` + +``` +# When compiling: +gcc -std=c99 -Wall -O3 example.c squiggle.c extra.c -lm -o ./example + +``` + +#### Extra: Cdf auxiliary functions + +I provide some Take a cdf, and return a sample from the distribution produced by that cdf. This might make it easier to program models, at the cost of a 20x to 60x slowdown in the parts of the code that use it. + +#### Extra: Error propagation vs exiting on error + +The process of taking a cdf and returning a sample might fail, e.g., it's a Newton method which might fail to converge because of cdf artifacts. The cdf itself might also fail, e.g., if a distribution only accepts a range of parameters, but is fed parameters outside that range. + +This library provides two approaches: + +1. Print the line and function in which the error occured, then exit on error +2. In situations where there might be an error, return a struct containing either the correct value or an error message: + +```C +struct box { + int empty; + double content; + char* error_msg; +}; +``` + +The first approach produces terser programs but might not scale. The second approach seems like it could lead to more robust programmes, but is more verbose. + +Behaviour on error can be toggled by the `EXIT_ON_ERROR` variable. This library also provides a convenient macro, `PROCESS_ERROR`, to make error handling in either case much terser—see the usage in example 4 in the examples/ folder. + +Overall, I'd describe the error handling capabilities of this library as pretty rudimentary. For example, this program might fail in surprising ways if you ask for a lognormal with negative standard deviation, because I haven't added error checking for that case yet. + + ## Related projects - [Squiggle](https://www.squiggle-language.com/) diff --git a/examples/01_one_sample/example b/examples/01_one_sample/example index 9c9a4ae..d787733 100755 Binary files a/examples/01_one_sample/example and b/examples/01_one_sample/example differ diff --git a/examples/02_many_samples_time_to_botec/example b/examples/02_many_samples_time_to_botec/example index bb4ba8f..93a714d 100755 Binary files a/examples/02_many_samples_time_to_botec/example and b/examples/02_many_samples_time_to_botec/example differ diff --git a/examples/03_gcc_nested_function/example b/examples/03_gcc_nested_function/example index 62d1419..a513887 100755 Binary files a/examples/03_gcc_nested_function/example and b/examples/03_gcc_nested_function/example differ diff --git a/examples/04_sample_from_cdf_simple/example b/examples/04_sample_from_cdf_simple/example index 40d44cf..a8080e2 100755 Binary files a/examples/04_sample_from_cdf_simple/example and b/examples/04_sample_from_cdf_simple/example differ diff --git a/examples/04_sample_from_cdf_simple/example.c b/examples/04_sample_from_cdf_simple/example.c index fc8e219..ee24d34 100644 --- a/examples/04_sample_from_cdf_simple/example.c +++ b/examples/04_sample_from_cdf_simple/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/04_sample_from_cdf_simple/makefile b/examples/04_sample_from_cdf_simple/makefile index 16b466d..8b512cd 100644 --- a/examples/04_sample_from_cdf_simple/makefile +++ b/examples/04_sample_from_cdf_simple/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c +SRC=example.c ../../squiggle.c ../../extra.c OUTPUT=./example ## Dependencies diff --git a/examples/05_sample_from_cdf_beta/example b/examples/05_sample_from_cdf_beta/example index 4d13845..0c0a0ca 100755 Binary files a/examples/05_sample_from_cdf_beta/example and b/examples/05_sample_from_cdf_beta/example differ diff --git a/examples/05_sample_from_cdf_beta/example.c b/examples/05_sample_from_cdf_beta/example.c index bd7d28a..3ee1ab3 100644 --- a/examples/05_sample_from_cdf_beta/example.c +++ b/examples/05_sample_from_cdf_beta/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/05_sample_from_cdf_beta/makefile b/examples/05_sample_from_cdf_beta/makefile index ab7ceae..a41df3c 100644 --- 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 +SRC=example.c ../../squiggle.c ../../extra.c OUTPUT=./example ## Dependencies diff --git a/examples/06_gamma_beta/example b/examples/06_gamma_beta/example index 5ea6a71..0aa2d72 100755 Binary files a/examples/06_gamma_beta/example and b/examples/06_gamma_beta/example differ diff --git a/examples/07_ci_beta/example b/examples/07_ci_beta/example index ae75202..6dfa4c9 100755 Binary files a/examples/07_ci_beta/example and b/examples/07_ci_beta/example differ diff --git a/examples/07_ci_beta/example.c b/examples/07_ci_beta/example.c index 7b372ae..c143602 100644 --- a/examples/07_ci_beta/example.c +++ b/examples/07_ci_beta/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/07_ci_beta/makefile b/examples/07_ci_beta/makefile index ef385f7..09f8b3c 100644 --- 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 +SRC=example.c ../../squiggle.c ../../extra.c OUTPUT=example ## Dependencies diff --git a/examples/08_nuclear_war/example.c b/examples/08_nuclear_war/example.c index a17b44f..df5abcb 100644 --- a/examples/08_nuclear_war/example.c +++ b/examples/08_nuclear_war/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/08_nuclear_war/makefile b/examples/08_nuclear_war/makefile index ef385f7..09f8b3c 100644 --- 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 +SRC=example.c ../../squiggle.c ../../extra.c OUTPUT=example ## Dependencies diff --git a/examples/09_burn_10kg_fat/example.c b/examples/09_burn_10kg_fat/example.c index b6dd555..a2b1d18 100644 --- a/examples/09_burn_10kg_fat/example.c +++ b/examples/09_burn_10kg_fat/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/09_burn_10kg_fat/makefile b/examples/09_burn_10kg_fat/makefile index ef385f7..8b512cd 100644 --- a/examples/09_burn_10kg_fat/makefile +++ b/examples/09_burn_10kg_fat/makefile @@ -5,22 +5,25 @@ # make run # Compiler -CC=gcc +CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c -OUTPUT=example +SRC=example.c ../../squiggle.c ../../extra.c +OUTPUT=./example ## Dependencies MATH=-lm +DEPENDENCIES=$(MATH) +# OPENMP=-fopenmp ## Flags DEBUG= #'-g' -STANDARD=-std=c99 +STANDARD=-std=c99 ## gnu99 allows for nested functions. +EXTENSIONS= #-fnested-functions WARNINGS=-Wall -OPTIMIZED=-O3 #-Ofast -# OPENMP=-fopenmp +OPTIMIZED=-O3#-Ofast +CFLAGS=$(DEBUG) $(STANDARD) $(EXTENSIONS) $(WARNINGS) $(OPTIMIZED) ## Formatter STYLE_BLUEPRINT=webkit @@ -28,13 +31,14 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) ## make build build: $(SRC) - $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + # gcc -std=gnu99 example.c -lm -o example + $(CC) $(CFLAGS) $(SRC) $(DEPENDENCIES) -o $(OUTPUT) format: $(SRC) $(FORMATTER) $(SRC) run: $(SRC) $(OUTPUT) - OMP_NUM_THREADS=1 ./$(OUTPUT) && echo + ./$(OUTPUT) && echo time-linux: @echo "Requires /bin/time, found on GNU/Linux systems" && echo diff --git a/examples/10_nuclear_recovery/example.c b/examples/10_nuclear_recovery/example.c index 3891cd7..76de52d 100644 --- a/examples/10_nuclear_recovery/example.c +++ b/examples/10_nuclear_recovery/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/10_nuclear_recovery/makefile b/examples/10_nuclear_recovery/makefile index ef385f7..8b512cd 100644 --- a/examples/10_nuclear_recovery/makefile +++ b/examples/10_nuclear_recovery/makefile @@ -5,22 +5,25 @@ # make run # Compiler -CC=gcc +CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c -OUTPUT=example +SRC=example.c ../../squiggle.c ../../extra.c +OUTPUT=./example ## Dependencies MATH=-lm +DEPENDENCIES=$(MATH) +# OPENMP=-fopenmp ## Flags DEBUG= #'-g' -STANDARD=-std=c99 +STANDARD=-std=c99 ## gnu99 allows for nested functions. +EXTENSIONS= #-fnested-functions WARNINGS=-Wall -OPTIMIZED=-O3 #-Ofast -# OPENMP=-fopenmp +OPTIMIZED=-O3#-Ofast +CFLAGS=$(DEBUG) $(STANDARD) $(EXTENSIONS) $(WARNINGS) $(OPTIMIZED) ## Formatter STYLE_BLUEPRINT=webkit @@ -28,13 +31,14 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) ## make build build: $(SRC) - $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + # gcc -std=gnu99 example.c -lm -o example + $(CC) $(CFLAGS) $(SRC) $(DEPENDENCIES) -o $(OUTPUT) format: $(SRC) $(FORMATTER) $(SRC) run: $(SRC) $(OUTPUT) - OMP_NUM_THREADS=1 ./$(OUTPUT) && echo + ./$(OUTPUT) && echo time-linux: @echo "Requires /bin/time, found on GNU/Linux systems" && echo diff --git a/examples/11_algebra/example.c b/examples/11_algebra/example.c index b26d0ad..2fde7ff 100644 --- a/examples/11_algebra/example.c +++ b/examples/11_algebra/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/11_algebra/makefile b/examples/11_algebra/makefile index ef385f7..8b512cd 100644 --- a/examples/11_algebra/makefile +++ b/examples/11_algebra/makefile @@ -5,22 +5,25 @@ # make run # Compiler -CC=gcc +CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c -OUTPUT=example +SRC=example.c ../../squiggle.c ../../extra.c +OUTPUT=./example ## Dependencies MATH=-lm +DEPENDENCIES=$(MATH) +# OPENMP=-fopenmp ## Flags DEBUG= #'-g' -STANDARD=-std=c99 +STANDARD=-std=c99 ## gnu99 allows for nested functions. +EXTENSIONS= #-fnested-functions WARNINGS=-Wall -OPTIMIZED=-O3 #-Ofast -# OPENMP=-fopenmp +OPTIMIZED=-O3#-Ofast +CFLAGS=$(DEBUG) $(STANDARD) $(EXTENSIONS) $(WARNINGS) $(OPTIMIZED) ## Formatter STYLE_BLUEPRINT=webkit @@ -28,13 +31,14 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) ## make build build: $(SRC) - $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + # gcc -std=gnu99 example.c -lm -o example + $(CC) $(CFLAGS) $(SRC) $(DEPENDENCIES) -o $(OUTPUT) format: $(SRC) $(FORMATTER) $(SRC) run: $(SRC) $(OUTPUT) - OMP_NUM_THREADS=1 ./$(OUTPUT) && echo + ./$(OUTPUT) && echo time-linux: @echo "Requires /bin/time, found on GNU/Linux systems" && echo diff --git a/examples/12_algebra_and_conversion/example.c b/examples/12_algebra_and_conversion/example.c index a93860b..7386270 100644 --- a/examples/12_algebra_and_conversion/example.c +++ b/examples/12_algebra_and_conversion/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/12_algebra_and_conversion/makefile b/examples/12_algebra_and_conversion/makefile index ef385f7..8b512cd 100644 --- a/examples/12_algebra_and_conversion/makefile +++ b/examples/12_algebra_and_conversion/makefile @@ -5,22 +5,25 @@ # make run # Compiler -CC=gcc +CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c -OUTPUT=example +SRC=example.c ../../squiggle.c ../../extra.c +OUTPUT=./example ## Dependencies MATH=-lm +DEPENDENCIES=$(MATH) +# OPENMP=-fopenmp ## Flags DEBUG= #'-g' -STANDARD=-std=c99 +STANDARD=-std=c99 ## gnu99 allows for nested functions. +EXTENSIONS= #-fnested-functions WARNINGS=-Wall -OPTIMIZED=-O3 #-Ofast -# OPENMP=-fopenmp +OPTIMIZED=-O3#-Ofast +CFLAGS=$(DEBUG) $(STANDARD) $(EXTENSIONS) $(WARNINGS) $(OPTIMIZED) ## Formatter STYLE_BLUEPRINT=webkit @@ -28,13 +31,14 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) ## make build build: $(SRC) - $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + # gcc -std=gnu99 example.c -lm -o example + $(CC) $(CFLAGS) $(SRC) $(DEPENDENCIES) -o $(OUTPUT) format: $(SRC) $(FORMATTER) $(SRC) run: $(SRC) $(OUTPUT) - OMP_NUM_THREADS=1 ./$(OUTPUT) && echo + ./$(OUTPUT) && echo time-linux: @echo "Requires /bin/time, found on GNU/Linux systems" && echo diff --git a/examples/13_ergonomic_algebra/example.c b/examples/13_ergonomic_algebra/example.c index 250a334..6decb76 100644 --- a/examples/13_ergonomic_algebra/example.c +++ b/examples/13_ergonomic_algebra/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/13_ergonomic_algebra/makefile b/examples/13_ergonomic_algebra/makefile index ef385f7..8b512cd 100644 --- a/examples/13_ergonomic_algebra/makefile +++ b/examples/13_ergonomic_algebra/makefile @@ -5,22 +5,25 @@ # make run # Compiler -CC=gcc +CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c -OUTPUT=example +SRC=example.c ../../squiggle.c ../../extra.c +OUTPUT=./example ## Dependencies MATH=-lm +DEPENDENCIES=$(MATH) +# OPENMP=-fopenmp ## Flags DEBUG= #'-g' -STANDARD=-std=c99 +STANDARD=-std=c99 ## gnu99 allows for nested functions. +EXTENSIONS= #-fnested-functions WARNINGS=-Wall -OPTIMIZED=-O3 #-Ofast -# OPENMP=-fopenmp +OPTIMIZED=-O3#-Ofast +CFLAGS=$(DEBUG) $(STANDARD) $(EXTENSIONS) $(WARNINGS) $(OPTIMIZED) ## Formatter STYLE_BLUEPRINT=webkit @@ -28,13 +31,14 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) ## make build build: $(SRC) - $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + # gcc -std=gnu99 example.c -lm -o example + $(CC) $(CFLAGS) $(SRC) $(DEPENDENCIES) -o $(OUTPUT) format: $(SRC) $(FORMATTER) $(SRC) run: $(SRC) $(OUTPUT) - OMP_NUM_THREADS=1 ./$(OUTPUT) && echo + ./$(OUTPUT) && echo time-linux: @echo "Requires /bin/time, found on GNU/Linux systems" && echo diff --git a/examples/14_twitter_thread_example/example.c b/examples/14_twitter_thread_example/example.c index d5a4d41..81e836d 100644 --- a/examples/14_twitter_thread_example/example.c +++ b/examples/14_twitter_thread_example/example.c @@ -1,4 +1,5 @@ #include "../../squiggle.h" +#include "../../extra.h" #include #include #include diff --git a/examples/14_twitter_thread_example/makefile b/examples/14_twitter_thread_example/makefile index 2d251e0..8b512cd 100644 --- a/examples/14_twitter_thread_example/makefile +++ b/examples/14_twitter_thread_example/makefile @@ -5,22 +5,25 @@ # make run # Compiler -CC=gcc +CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c -OUTPUT=example +SRC=example.c ../../squiggle.c ../../extra.c +OUTPUT=./example ## Dependencies MATH=-lm +DEPENDENCIES=$(MATH) +# OPENMP=-fopenmp ## Flags DEBUG= #'-g' -STANDARD=-std=c99 +STANDARD=-std=c99 ## gnu99 allows for nested functions. +EXTENSIONS= #-fnested-functions WARNINGS=-Wall -OPTIMIZED=-O3 #-Ofast -# OPENMP=-fopenmp +OPTIMIZED=-O3#-Ofast +CFLAGS=$(DEBUG) $(STANDARD) $(EXTENSIONS) $(WARNINGS) $(OPTIMIZED) ## Formatter STYLE_BLUEPRINT=webkit @@ -28,7 +31,8 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) ## make build build: $(SRC) - $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + # gcc -std=gnu99 example.c -lm -o example + $(CC) $(CFLAGS) $(SRC) $(DEPENDENCIES) -o $(OUTPUT) format: $(SRC) $(FORMATTER) $(SRC) @@ -40,7 +44,7 @@ time-linux: @echo "Requires /bin/time, found on GNU/Linux systems" && echo @echo "Running 100x and taking avg time $(OUTPUT)" - @t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do ./$(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 1 thread: |" | sed 's|$$|ms|' && echo + @t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 1 thread: |" | sed 's|$$|ms|' && echo ## Profiling diff --git a/examples/16_100_lognormal_samples/example b/examples/16_100_lognormal_samples/example index 3773262..5aae3a0 100755 Binary files a/examples/16_100_lognormal_samples/example and b/examples/16_100_lognormal_samples/example differ diff --git a/extra.c b/extra.c index 8d134ee..4d7e412 100644 --- a/extra.c +++ b/extra.c @@ -6,15 +6,58 @@ #include #include #include +#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__) -// # More cool stuff -// This is no longer necessary to do basic estimation, -// but is still cool +// 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 return 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 { @@ -210,45 +253,6 @@ double sampler_danger(struct box cdf(double), uint64_t* seed) } */ -// Get confidence intervals, given a sampler - -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 return 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; -} - // # Small algebra manipulations // here I discover named structs, diff --git a/extra.h b/extra.h index becf562..b71ae4f 100644 --- a/extra.h +++ b/extra.h @@ -43,10 +43,7 @@ typedef struct lognormal_params_t { } lognormal_params; lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params b); - lognormal_params convert_ci_to_lognormal_params(ci x); - - -ci convert_lognormal_params_to_ci(lognormal_params y); +ci convert_lognormal_params_to_ci(lognormal_params y); #endif diff --git a/scratchpad/scratchpad b/scratchpad/scratchpad index 2c89a47..434102f 100755 Binary files a/scratchpad/scratchpad and b/scratchpad/scratchpad differ diff --git a/squiggle.c b/squiggle.c index 23e87fc..9177e97 100644 --- a/squiggle.c +++ b/squiggle.c @@ -7,13 +7,14 @@ #include #include -#define PI 3.14159265358979323846 // M_PI in gcc gnu99 -#define NORMAL90CONFIDENCE 1.6448536269514727 - // # 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 +#define NORMAL90CONFIDENCE 1.6448536269514727 + // Pseudo Random number generator uint64_t xorshift32(uint32_t* seed) { diff --git a/test/test b/test/test index 38c2336..594b53f 100755 Binary files a/test/test and b/test/test differ