example.c (4442B)
1 #include "../../../squiggle.h" 2 #include <math.h> 3 #include <stdint.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 double sample_fermi_logspace(uint64_t * seed) 8 { 9 // Replicate <https://arxiv.org/pdf/1806.02404.pdf>, and in particular the red line in page 11. 10 // You can see a simple version of this function in naive.c in this same folder 11 double log_rate_of_star_formation = sample_uniform(log(1), log(100), seed); 12 double log_fraction_of_stars_with_planets = sample_uniform(log(0.1), log(1), seed); 13 double log_number_of_habitable_planets_per_star_system = sample_uniform(log(0.1), log(1), seed); 14 15 double log_rate_of_life_formation_in_habitable_planets = sample_normal(1, 50, seed); 16 double log_fraction_of_habitable_planets_in_which_any_life_appears; 17 /* 18 Consider: 19 a = underlying normal 20 b = rate_of_life_formation_in_habitable_planets = exp(underlying normal) = exp(a) 21 c = 1 - exp(-b) = fraction_of_habitable_planets_in_which_any_life_appears 22 d = log(c) 23 24 Looking at the Taylor expansion for c = 1 - exp(-b), it's 25 b - b^2/2 + b^3/6 - x^b/24, etc. 26 <https://www.wolframalpha.com/input?i=1-exp%28-x%29> 27 When b ~ 0 (as is often the case), this is close to b. 28 29 But now, if b ~ 0, c ~ b 30 and d = log(c) ~ log(b) = log(exp(a)) = a 31 32 Now, we could play around with estimating errors, 33 and indeed if we want b^2/2 = exp(a)^2/2 < 10^(-n), i.e., to have n decimal digits of precision, 34 we could compute this as e.g., a < (nlog(10) + log(2))/2 35 so for example if we want ten digits of precision, that's a < -11 36 37 Empirically, the two numbers as calculated in C do become really close around 11 or so, 38 and at 38 that calculation results in a -inf (so probably a floating point error or similar.) 39 So we should be using that formula for somewhere between -38 << a < -11 40 41 I chose -16 as a happy medium after playing around with 42 double invert(double x){ 43 return log(1-exp(-exp(-x))); 44 } 45 for(int i=0; i<64; i++){ 46 double j = i; 47 printf("for %lf, log(1-exp(-exp(-x))) is calculated as... %lf\n", j, invert(j)); 48 } 49 and <https://www.wolframalpha.com/input?i=log%281-exp%28-exp%28-16%29%29%29> 50 */ 51 if (log_rate_of_life_formation_in_habitable_planets < -16) { 52 log_fraction_of_habitable_planets_in_which_any_life_appears = log_rate_of_life_formation_in_habitable_planets; 53 } else { 54 double rate_of_life_formation_in_habitable_planets = exp(log_rate_of_life_formation_in_habitable_planets); 55 double fraction_of_habitable_planets_in_which_any_life_appears = -expm1(-rate_of_life_formation_in_habitable_planets); 56 log_fraction_of_habitable_planets_in_which_any_life_appears = log(fraction_of_habitable_planets_in_which_any_life_appears); 57 } 58 59 double log_fraction_of_planets_with_life_in_which_intelligent_life_appears = sample_uniform(log(0.001), log(1), seed); 60 double log_fraction_of_intelligent_planets_which_are_detectable_as_such = sample_uniform(log(0.01), log(1), seed); 61 double log_longevity_of_detectable_civilizations = sample_uniform(log(100), log(10000000000), seed); 62 63 double log_n = 64 log_rate_of_star_formation + 65 log_fraction_of_stars_with_planets + 66 log_number_of_habitable_planets_per_star_system + 67 log_fraction_of_habitable_planets_in_which_any_life_appears + 68 log_fraction_of_planets_with_life_in_which_intelligent_life_appears + 69 log_fraction_of_intelligent_planets_which_are_detectable_as_such + 70 log_longevity_of_detectable_civilizations; 71 return log_n; 72 } 73 74 double sample_are_we_alone_logspace(uint64_t * seed) 75 { 76 double log_n = sample_fermi_logspace(seed); 77 return ((log_n > 0) ? 1 : 0); 78 // log_n > 0 => n > 1 79 } 80 81 82 int main() 83 { 84 85 // set randomness seed 86 uint64_t* seed = malloc(sizeof(uint64_t)); 87 *seed = 1001; // xorshift can't start with a seed of 0 88 89 double logspace_fermi_proportion = 0; 90 int n_samples = 1000 * 1000; 91 for (int i = 0; i < n_samples; i++) { 92 double result = sample_are_we_alone_logspace(seed); 93 logspace_fermi_proportion += result; 94 } 95 double p_not_alone = logspace_fermi_proportion / n_samples; 96 printf("Probability that we are not alone: %lf (%.lf%%)\n", p_not_alone, p_not_alone * 100); 97 98 free(seed); 99 }