squiggle.c

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

commit 186b10cddf1ebb9f68e13be602ab29218ee83069
parent fb110a35f3f6e50dc655e7502c00c360b90b6211
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Wed, 29 Nov 2023 23:08:36 +0000

more refactors; add another example

Diffstat:
Mexamples/more/03_ci_beta/example | 0
Mexamples/more/03_ci_beta/example.c | 1+
Mexamples/more/04_nuclear_war/example | 0
Mexamples/more/05_burn_10kg_fat/example | 0
Mexamples/more/06_nuclear_recovery/example | 0
Mexamples/more/08_algebra_and_conversion/example | 0
Mexamples/more/09_ergonomic_algebra/example | 0
Mexamples/more/11_billion_lognormals_paralell/example | 0
Mexamples/more/12_time_to_botec_parallel/example | 0
Mexamples/more/13_parallelize_min/example | 0
Mexamples/more/13_parallelize_min/example.c | 4++--
Aexamples/more/14_check_confidence_interval/example | 0
Aexamples/more/14_check_confidence_interval/example.c | 21+++++++++++++++++++++
Mexamples/more/makefile | 6++++++
Mscratchpad/makefile | 2+-
Mscratchpad/scratchpad | 0
Mscratchpad/scratchpad.c | 17+++++++++--------
Msquiggle_more.h | 7+++++--
18 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/examples/more/03_ci_beta/example b/examples/more/03_ci_beta/example Binary files differ. diff --git a/examples/more/03_ci_beta/example.c b/examples/more/03_ci_beta/example.c @@ -17,6 +17,7 @@ int main() ci beta_1_2_ci_90 = sampler_get_90_ci(beta_1_2_sampler, 1000000, seed); printf("90%% confidence interval of beta(1,2) is [%f, %f]\n", beta_1_2_ci_90.low, beta_1_2_ci_90.high); + printf("You can check this in <https://nunosempere.com/blog/2023/03/15/fit-beta/>\n"); free(seed); } diff --git a/examples/more/04_nuclear_war/example b/examples/more/04_nuclear_war/example Binary files differ. diff --git a/examples/more/05_burn_10kg_fat/example b/examples/more/05_burn_10kg_fat/example Binary files differ. diff --git a/examples/more/06_nuclear_recovery/example b/examples/more/06_nuclear_recovery/example Binary files differ. diff --git a/examples/more/08_algebra_and_conversion/example b/examples/more/08_algebra_and_conversion/example Binary files differ. diff --git a/examples/more/09_ergonomic_algebra/example b/examples/more/09_ergonomic_algebra/example Binary files differ. diff --git a/examples/more/11_billion_lognormals_paralell/example b/examples/more/11_billion_lognormals_paralell/example Binary files differ. diff --git a/examples/more/12_time_to_botec_parallel/example b/examples/more/12_time_to_botec_parallel/example Binary files differ. diff --git a/examples/more/13_parallelize_min/example b/examples/more/13_parallelize_min/example Binary files differ. diff --git a/examples/more/13_parallelize_min/example.c b/examples/more/13_parallelize_min/example.c @@ -29,7 +29,7 @@ int main() int n_samples = 1000000, n_threads = 16; double* results = malloc(n_samples * sizeof(double)); - sampler_parallel(sampler_result, results, n_threads, n_samples); + sampler_parallel(sample_min_of_1000, results, n_threads, n_samples); printf("Mean of the distribution of (taking the min of 1000 samples of a normal(5,2)): %f\n", array_mean(results, n_samples)); free(results); @@ -57,7 +57,7 @@ int main() } } if(min > result_remainder){ - min = results_remainder; + min = result_remainder; } free(results_quotient); return min; diff --git a/examples/more/14_check_confidence_interval/example b/examples/more/14_check_confidence_interval/example Binary files differ. diff --git a/examples/more/14_check_confidence_interval/example.c b/examples/more/14_check_confidence_interval/example.c @@ -0,0 +1,21 @@ +#include "../../../squiggle.h" +#include "../../../squiggle_more.h" +#include <stdio.h> +#include <stdlib.h> + +int main() +{ + // set randomness seed + uint64_t* seed = malloc(sizeof(uint64_t)); + *seed = 1000; // xorshift can't start with a seed of 0 + + int n = 1000000; + double* xs = malloc(sizeof(double) * n); + for (int i = 0; i < n; i++) { + xs[i] = sample_to(10, 100, seed); + } + ci ci_90 = array_get_90_ci(xs, n); + printf("Recovering confidence interval of sample_to(10, 100):\n low: %f, high: %f\n", ci_90.low, ci_90.high); + + free(seed); +} diff --git a/examples/more/makefile b/examples/more/makefile @@ -48,6 +48,8 @@ all: $(CC) $(OPTIMIZED) $(DEBUG) 10_twitter_thread_example/$(SRC) $(DEPS) -o 10_twitter_thread_example/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) 11_billion_lognormals_paralell/$(SRC) $(DEPS) -o 11_billion_lognormals_paralell/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) 12_time_to_botec_parallel/$(SRC) $(DEPS) -o 12_time_to_botec_parallel/$(OUTPUT) + $(CC) $(OPTIMIZED) $(DEBUG) 13_parallelize_min/$(SRC) $(DEPS) -o 13_parallelize_min/$(OUTPUT) + $(CC) $(OPTIMIZED) $(DEBUG) 14_check_confidence_interval/$(SRC) $(DEPS) -o 14_check_confidence_interval/$(OUTPUT) format-all: $(FORMATTER) 00_example_template/$(SRC) @@ -63,6 +65,8 @@ format-all: $(FORMATTER) 10_twitter_thread_example/$(SRC) $(FORMATTER) 11_billion_lognormals_paralell/$(SRC) $(FORMATTER) 12_time_to_botec_parallel/$(SRC) + $(FORMATTER) 13_parallelize_min/$(SRC) + $(FORMATTER) 14_check_confidence_interval/$(SRC) run-all: 00_example_template/$(OUTPUT) @@ -78,6 +82,8 @@ run-all: 10_twitter_thread_example/$(OUTPUT) 11_billion_lognormals_paralell/$(OUTPUT) 12_time_to_botec_parallel/$(OUTPUT) + 13_parallelize_min/$(OUTPUT) + 14_check_confidence_interval/$(OUTPUT) ## make one DIR=06_nuclear_recovery one: $(DIR)/$(SRC) diff --git a/scratchpad/makefile b/scratchpad/makefile @@ -9,7 +9,7 @@ CC=gcc # CC=tcc # <= faster compilation # Main file -SRC=scratchpad.c ../squiggle.c +SRC=scratchpad.c ../squiggle.c ../squiggle_more.c OUTPUT=scratchpad ## Dependencies diff --git a/scratchpad/scratchpad b/scratchpad/scratchpad Binary files differ. diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c @@ -1,4 +1,5 @@ #include "../squiggle.h" +#include "../squiggle_more.h" #include <math.h> #include <stdint.h> #include <stdio.h> @@ -9,14 +10,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); + + int n = 1000000; + double* xs = malloc(sizeof(double) * n); + for (int i = 0; i < n; i++) { + xs[i] = sample_to(10, 100, seed); + } + ci ci_90 = array_get_90_ci(xs, n); + printf("Recovering confidence interval of sample_to(10, 100):\n low: %f, high: %f\n", ci_90.low, ci_90.high); free(seed); } diff --git a/squiggle_more.h b/squiggle_more.h @@ -6,9 +6,12 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_ /* Get 90% confidence interval */ typedef struct ci_t { - float low; - float high; + double low; + double high; } ci; +ci array_get_ci(ci interval, double* xs, int n); +ci array_get_90_ci(double xs[], int n); +ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed); ci sampler_get_90_ci(double (*sampler)(uint64_t*), int n, uint64_t* seed); /* Algebra manipulations */