squiggle.c

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

naive.c (3956B)


      1 #include "../../../squiggle.h"
      2 #include <math.h>
      3 #include <stdint.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 
      7 #define VERBOSE 0
      8 
      9 double sample_loguniform(double a, double b, uint64_t* seed)
     10 {
     11     return exp(sample_uniform(log(a), log(b), seed));
     12 }
     13 
     14 int main()
     15 {
     16     // Replicate <https://arxiv.org/pdf/1806.02404.pdf>, and in particular the red line in page 11.
     17     // Could also be interesting to just produce and save many samples.
     18 
     19     // set randomness seed
     20     uint64_t* seed = malloc(sizeof(uint64_t));
     21     *seed = UINT64_MAX / 64; // xorshift can't start with a seed of 0
     22 
     23     // Do this naïvely, without worrying that much about numerical precision
     24     double sample_fermi_naive(uint64_t * seed)
     25     {
     26         double rate_of_star_formation = sample_loguniform(1, 100, seed);
     27         double fraction_of_stars_with_planets = sample_loguniform(0.1, 1, seed);
     28         double number_of_habitable_planets_per_star_system = sample_loguniform(0.1, 1, seed);
     29         double rate_of_life_formation_in_habitable_planets = sample_lognormal(1, 50, seed);
     30         double fraction_of_habitable_planets_in_which_any_life_appears = -expm1(-rate_of_life_formation_in_habitable_planets);
     31         // double fraction_of_habitable_planets_in_which_any_life_appears = 1-exp(-rate_of_life_formation_in_habitable_planets);
     32         // but with more precision
     33         double fraction_of_planets_with_life_in_which_intelligent_life_appears = sample_loguniform(0.001, 1, seed);
     34         double fraction_of_intelligent_planets_which_are_detectable_as_such = sample_loguniform(0.01, 1, seed);
     35         double longevity_of_detectable_civilizations = sample_loguniform(100, 10000000000, seed);
     36 
     37         if(VERBOSE) printf(" rate_of_star_formation = %lf\n", rate_of_star_formation);
     38         if(VERBOSE) printf(" fraction_of_stars_with_planets = %lf\n", fraction_of_stars_with_planets);
     39         if(VERBOSE) printf(" number_of_habitable_planets_per_star_system = %lf\n", number_of_habitable_planets_per_star_system);
     40         if(VERBOSE) printf(" rate_of_life_formation_in_habitable_planets = %.16lf\n", rate_of_life_formation_in_habitable_planets);
     41         if(VERBOSE) printf(" fraction_of_habitable_planets_in_which_any_life_appears = %lf\n", fraction_of_habitable_planets_in_which_any_life_appears);
     42         if(VERBOSE) printf(" fraction_of_planets_with_life_in_which_intelligent_life_appears = %lf\n", fraction_of_planets_with_life_in_which_intelligent_life_appears);
     43         if(VERBOSE) printf(" fraction_of_intelligent_planets_which_are_detectable_as_such = %lf\n", fraction_of_intelligent_planets_which_are_detectable_as_such);
     44         if(VERBOSE) printf(" longevity_of_detectable_civilizations = %lf\n", longevity_of_detectable_civilizations);
     45 
     46         // Expected number of civilizations in the Milky way;
     47         // see footnote 3 (p. 5)
     48         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;
     49 
     50         return n;
     51     }
     52 
     53     double sample_are_we_alone_naive(uint64_t * seed)
     54     {
     55         double n = sample_fermi_naive(seed);
     56         return ((n > 1) ? 1 : 0);
     57     }
     58 
     59     double n = 1000000;
     60     double naive_fermi_proportion = 0;
     61     for (int i = 0; i < n; i++) {
     62         double result = sample_are_we_alone_naive(seed);
     63         if(VERBOSE) printf("result: %lf\n", result);
     64         naive_fermi_proportion += result;
     65     }
     66     printf("Naïve %% that we are not alone: %lf\n", naive_fermi_proportion / n);
     67 
     68     free(seed);
     69 
     70     /* 
     71         double invert(double x){
     72             return log(1-exp(-exp(-x)));
     73         }
     74         for(int i=0; i<64; i++){
     75             double j = i;
     76             printf("for %lf, log(1-exp(-exp(-x))) is calculated as... %lf\n", j, invert(j));
     77         }
     78     */
     79 }