diff --git a/examples/core/06_dissolving_fermi_paradox/fermi.pdf b/examples/core/06_dissolving_fermi_paradox/fermi.pdf new file mode 100644 index 0000000..df2e828 Binary files /dev/null and b/examples/core/06_dissolving_fermi_paradox/fermi.pdf differ diff --git a/examples/core/06_dissolving_fermi_paradox/makefile b/examples/core/06_dissolving_fermi_paradox/makefile new file mode 100644 index 0000000..d515383 --- /dev/null +++ b/examples/core/06_dissolving_fermi_paradox/makefile @@ -0,0 +1,56 @@ +# Interface: +# make +# make build +# make format +# make run + +# Compiler +CC=gcc +# CC=tcc # <= faster compilation + +# Main file +SRC=scratchpad.c ../squiggle.c ../squiggle_more.c +OUTPUT=scratchpad + +## Dependencies +MATH=-lm + +## Flags +DEBUG= #'-g' +STANDARD=-std=c99 +WARNINGS=-Wall +OPTIMIZED=-O3 #-Ofast +# OPENMP=-fopenmp + +## Formatter +STYLE_BLUEPRINT=webkit +FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) + +## make build +build: $(SRC) + $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + +format: $(SRC) + $(FORMATTER) $(SRC) + +run: $(SRC) $(OUTPUT) + ./$(OUTPUT) + +verify: $(SRC) $(OUTPUT) + ./$(OUTPUT) | grep "NOT passed" -A 2 --group-separator='' || true + +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 + +## Profiling + +profile-linux: + echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar" + echo "Must be run as sudo" + $(CC) $(SRC) $(MATH) -o $(OUTPUT) + sudo perf record ./$(OUTPUT) + sudo perf report + rm perf.data diff --git a/examples/core/06_dissolving_fermi_paradox/scratchpad b/examples/core/06_dissolving_fermi_paradox/scratchpad new file mode 100755 index 0000000..f3010c5 Binary files /dev/null and b/examples/core/06_dissolving_fermi_paradox/scratchpad differ diff --git a/examples/core/06_dissolving_fermi_paradox/scratchpad.c b/examples/core/06_dissolving_fermi_paradox/scratchpad.c new file mode 100644 index 0000000..da52936 --- /dev/null +++ b/examples/core/06_dissolving_fermi_paradox/scratchpad.c @@ -0,0 +1,157 @@ +#include "../squiggle.h" +// #include "../squiggle_more.h" +#include +#include +#include +#include + +double sample_loguniform(double a, double b, uint64_t* seed){ + return exp(sample_uniform(log(a), log(b), seed)); +} + +int main() +{ + // Replicate , and in particular the red line in page 11. + // Could also be interesting to just produce and save many samples. + + // set randomness seed + uint64_t* seed = malloc(sizeof(uint64_t)); + *seed = UINT64_MAX/64; // xorshift can't start with a seed of 0 + + double sample_fermi_naive(uint64_t* seed){ + double rate_of_star_formation = sample_loguniform(1,100, seed); + double fraction_of_stars_with_planets = sample_loguniform(0.1, 1, seed); + double number_of_habitable_planets_per_star_system = sample_loguniform(0.1, 1, seed); + double rate_of_life_formation_in_habitable_planets = sample_lognormal(1, 50, seed); + double fraction_of_habitable_planets_in_which_any_life_appears = -expm1(-rate_of_life_formation_in_habitable_planets); + // double fraction_of_habitable_planets_in_which_any_life_appears = 1-exp(-rate_of_life_formation_in_habitable_planets); + // but with more precision + double fraction_of_planets_with_life_in_which_intelligent_life_appears = sample_loguniform(0.001, 1, seed); + double fraction_of_intelligent_planets_which_are_detectable_as_such = sample_loguniform(0.01, 1, seed); + double longevity_of_detectable_civilizations = sample_loguniform(100, 10000000000, seed); + + // printf(" rate_of_star_formation = %lf\n", rate_of_star_formation); + // printf(" fraction_of_stars_with_planets = %lf\n", fraction_of_stars_with_planets); + // printf(" number_of_habitable_planets_per_star_system = %lf\n", number_of_habitable_planets_per_star_system); + // printf(" rate_of_life_formation_in_habitable_planets = %.16lf\n", rate_of_life_formation_in_habitable_planets); + // printf(" fraction_of_habitable_planets_in_which_any_life_appears = %lf\n", fraction_of_habitable_planets_in_which_any_life_appears); + // printf(" fraction_of_planets_with_life_in_which_intelligent_life_appears = %lf\n", fraction_of_planets_with_life_in_which_intelligent_life_appears); + // printf(" fraction_of_intelligent_planets_which_are_detectable_as_such = %lf\n", fraction_of_intelligent_planets_which_are_detectable_as_such); + // printf(" longevity_of_detectable_civilizations = %lf\n", longevity_of_detectable_civilizations); + + // Expected number of civilizations in the Milky way; + // see footnote 3 (p. 5) + double n = rate_of_star_formation * + fraction_of_stars_with_planets * + number_of_habitable_planets_per_star_system * + fraction_of_habitable_planets_in_which_any_life_appears * + fraction_of_planets_with_life_in_which_intelligent_life_appears * + fraction_of_intelligent_planets_which_are_detectable_as_such * + longevity_of_detectable_civilizations; + + return n; + } + + double sample_fermi_paradox_naive(uint64_t* seed){ + double n = sample_fermi_naive(seed); + return ((n > 1) ? 1 : 0); + } + + double n = 1000000; + double naive_fermi_proportion = 0; + for(int i=0; i 0) ? 1 : 0); + } + + double logspace_fermi_proportion = 0; + for(int i=0; i