diff --git a/examples/core/03_gcc_nested_function/example.c b/examples/core/03_gcc_nested_function/example.c index 3b1ad0e..d00e983 100644 --- a/examples/core/03_gcc_nested_function/example.c +++ b/examples/core/03_gcc_nested_function/example.c @@ -15,8 +15,16 @@ int main() int n_dists = 4; // These are nested functions. They will not compile without gcc. - double sample_0(uint64_t * seed) { UNUSED(seed); return 0; } - double sample_1(uint64_t * seed) { UNUSED(seed); return 1; } + double sample_0(uint64_t * seed) + { + UNUSED(seed); + return 0; + } + double sample_1(uint64_t * seed) + { + UNUSED(seed); + return 1; + } double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); } double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); } diff --git a/examples/core/06_dissolving_fermi_paradox/example b/examples/core/06_dissolving_fermi_paradox/example new file mode 100755 index 0000000..f6ac909 Binary files /dev/null and b/examples/core/06_dissolving_fermi_paradox/example differ diff --git a/examples/core/06_dissolving_fermi_paradox/scratchpad.c b/examples/core/06_dissolving_fermi_paradox/example.c similarity index 81% rename from examples/core/06_dissolving_fermi_paradox/scratchpad.c rename to examples/core/06_dissolving_fermi_paradox/example.c index da52936..0e9a5e6 100644 --- a/examples/core/06_dissolving_fermi_paradox/scratchpad.c +++ b/examples/core/06_dissolving_fermi_paradox/example.c @@ -1,11 +1,11 @@ -#include "../squiggle.h" -// #include "../squiggle_more.h" +#include "../../../squiggle.h" #include #include #include #include -double sample_loguniform(double a, double b, uint64_t* seed){ +double sample_loguniform(double a, double b, uint64_t* seed) +{ return exp(sample_uniform(log(a), log(b), seed)); } @@ -16,10 +16,11 @@ int main() // 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); + *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); @@ -41,34 +42,29 @@ int main() // 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; + 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 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 #include #include @@ -7,16 +7,18 @@ 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 fermi_naive(uint64_t* seed){ + 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); @@ -50,21 +52,23 @@ int main() return n; } - double fermi_paradox_naive(uint64_t* seed){ - double n = fermi_naive(seed); - return (n > 1 ? 1 : 0); + double sample_fermi_paradox_naive(uint64_t* seed){ + double n = sample_fermi_naive(seed); + return ((n > 1) ? 1 : 0); } - double result; - for(int i=0; i<1000; i++){ - result = fermi_naive(seed); - printf("result from fermi_naive: %lf\n", result); - printf("\n\n"); + 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