more refactors; add another example

This commit is contained in:
NunoSempere 2023-11-29 23:08:36 +00:00
parent fb110a35f3
commit 186b10cddf
18 changed files with 45 additions and 13 deletions

Binary file not shown.

View File

@ -17,6 +17,7 @@ int main()
ci beta_1_2_ci_90 = sampler_get_90_ci(beta_1_2_sampler, 1000000, seed); 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("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); free(seed);
} }

Binary file not shown.

View File

@ -29,7 +29,7 @@ int main()
int n_samples = 1000000, n_threads = 16; int n_samples = 1000000, n_threads = 16;
double* results = malloc(n_samples * sizeof(double)); 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)); 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); free(results);
@ -57,7 +57,7 @@ int main()
} }
} }
if(min > result_remainder){ if(min > result_remainder){
min = results_remainder; min = result_remainder;
} }
free(results_quotient); free(results_quotient);
return min; return min;

Binary file not shown.

View File

@ -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);
}

View File

@ -48,6 +48,8 @@ all:
$(CC) $(OPTIMIZED) $(DEBUG) 10_twitter_thread_example/$(SRC) $(DEPS) -o 10_twitter_thread_example/$(OUTPUT) $(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) 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) 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: format-all:
$(FORMATTER) 00_example_template/$(SRC) $(FORMATTER) 00_example_template/$(SRC)
@ -63,6 +65,8 @@ format-all:
$(FORMATTER) 10_twitter_thread_example/$(SRC) $(FORMATTER) 10_twitter_thread_example/$(SRC)
$(FORMATTER) 11_billion_lognormals_paralell/$(SRC) $(FORMATTER) 11_billion_lognormals_paralell/$(SRC)
$(FORMATTER) 12_time_to_botec_parallel/$(SRC) $(FORMATTER) 12_time_to_botec_parallel/$(SRC)
$(FORMATTER) 13_parallelize_min/$(SRC)
$(FORMATTER) 14_check_confidence_interval/$(SRC)
run-all: run-all:
00_example_template/$(OUTPUT) 00_example_template/$(OUTPUT)
@ -78,6 +82,8 @@ run-all:
10_twitter_thread_example/$(OUTPUT) 10_twitter_thread_example/$(OUTPUT)
11_billion_lognormals_paralell/$(OUTPUT) 11_billion_lognormals_paralell/$(OUTPUT)
12_time_to_botec_parallel/$(OUTPUT) 12_time_to_botec_parallel/$(OUTPUT)
13_parallelize_min/$(OUTPUT)
14_check_confidence_interval/$(OUTPUT)
## make one DIR=06_nuclear_recovery ## make one DIR=06_nuclear_recovery
one: $(DIR)/$(SRC) one: $(DIR)/$(SRC)

View File

@ -9,7 +9,7 @@ CC=gcc
# CC=tcc # <= faster compilation # CC=tcc # <= faster compilation
# Main file # Main file
SRC=scratchpad.c ../squiggle.c SRC=scratchpad.c ../squiggle.c ../squiggle_more.c
OUTPUT=scratchpad OUTPUT=scratchpad
## Dependencies ## Dependencies

Binary file not shown.

View File

@ -1,4 +1,5 @@
#include "../squiggle.h" #include "../squiggle.h"
#include "../squiggle_more.h"
#include <math.h> #include <math.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -9,14 +10,14 @@ int main()
// set randomness seed // set randomness seed
uint64_t* seed = malloc(sizeof(uint64_t)); uint64_t* seed = malloc(sizeof(uint64_t));
*seed = 1000; // xorshift can't start with a seed of 0 *seed = 1000; // xorshift can't start with a seed of 0
/*
for (int i = 0; i < 100; i++) { int n = 1000000;
double draw = sample_unit_uniform(seed); double* xs = malloc(sizeof(double) * n);
printf("%f\n", draw); for (int i = 0; i < n; i++) {
xs[i] = sample_to(10, 100, seed);
}*/ }
// Test division ci ci_90 = array_get_90_ci(xs, n);
printf("\n%d\n", 10 % 3); printf("Recovering confidence interval of sample_to(10, 100):\n low: %f, high: %f\n", ci_90.low, ci_90.high);
free(seed); free(seed);
} }

View File

@ -6,9 +6,12 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_
/* Get 90% confidence interval */ /* Get 90% confidence interval */
typedef struct ci_t { typedef struct ci_t {
float low; double low;
float high; double high;
} ci; } 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); ci sampler_get_90_ci(double (*sampler)(uint64_t*), int n, uint64_t* seed);
/* Algebra manipulations */ /* Algebra manipulations */