forked from personal/squiggle.c
formatting pass, upkeep
This commit is contained in:
parent
199e76bdfb
commit
1d89eb6231
|
@ -15,8 +15,16 @@ int main()
|
||||||
int n_dists = 4;
|
int n_dists = 4;
|
||||||
|
|
||||||
// These are nested functions. They will not compile without gcc.
|
// These are nested functions. They will not compile without gcc.
|
||||||
double sample_0(uint64_t * seed) { UNUSED(seed); return 0; }
|
double sample_0(uint64_t * seed)
|
||||||
double sample_1(uint64_t * seed) { UNUSED(seed); return 1; }
|
{
|
||||||
|
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_few(uint64_t * seed) { return sample_to(1, 3, seed); }
|
||||||
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }
|
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }
|
||||||
|
|
||||||
|
|
BIN
examples/core/06_dissolving_fermi_paradox/example
Executable file
BIN
examples/core/06_dissolving_fermi_paradox/example
Executable file
Binary file not shown.
|
@ -1,11 +1,11 @@
|
||||||
#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>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
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));
|
return exp(sample_uniform(log(a), log(b), seed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,8 @@ int main()
|
||||||
uint64_t* seed = malloc(sizeof(uint64_t));
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
||||||
*seed = UINT64_MAX / 64; // xorshift can't start with a seed of 0
|
*seed = UINT64_MAX / 64; // xorshift can't start with a seed of 0
|
||||||
|
|
||||||
double sample_fermi_naive(uint64_t* seed){
|
double sample_fermi_naive(uint64_t * seed)
|
||||||
|
{
|
||||||
double rate_of_star_formation = sample_loguniform(1, 100, seed);
|
double rate_of_star_formation = sample_loguniform(1, 100, seed);
|
||||||
double fraction_of_stars_with_planets = sample_loguniform(0.1, 1, 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 number_of_habitable_planets_per_star_system = sample_loguniform(0.1, 1, seed);
|
||||||
|
@ -41,18 +42,13 @@ int main()
|
||||||
|
|
||||||
// Expected number of civilizations in the Milky way;
|
// Expected number of civilizations in the Milky way;
|
||||||
// see footnote 3 (p. 5)
|
// see footnote 3 (p. 5)
|
||||||
double n = rate_of_star_formation *
|
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;
|
||||||
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;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
double sample_fermi_paradox_naive(uint64_t* seed){
|
double sample_fermi_paradox_naive(uint64_t * seed)
|
||||||
|
{
|
||||||
double n = sample_fermi_naive(seed);
|
double n = sample_fermi_naive(seed);
|
||||||
return ((n > 1) ? 1 : 0);
|
return ((n > 1) ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
@ -66,9 +62,9 @@ int main()
|
||||||
}
|
}
|
||||||
printf("Naïve %% that we are not alone: %lf\n", naive_fermi_proportion / n);
|
printf("Naïve %% that we are not alone: %lf\n", naive_fermi_proportion / n);
|
||||||
|
|
||||||
|
|
||||||
// Thinking in log space
|
// Thinking in log space
|
||||||
double sample_fermi_logspace(uint64_t* seed){
|
double sample_fermi_logspace(uint64_t * seed)
|
||||||
|
{
|
||||||
double log_rate_of_star_formation = sample_uniform(log(1), log(100), seed);
|
double log_rate_of_star_formation = sample_uniform(log(1), log(100), seed);
|
||||||
double log_fraction_of_stars_with_planets = sample_uniform(log(0.1), log(1), seed);
|
double log_fraction_of_stars_with_planets = sample_uniform(log(0.1), log(1), seed);
|
||||||
double log_number_of_habitable_planets_per_star_system = sample_uniform(log(0.1), log(1), seed);
|
double log_number_of_habitable_planets_per_star_system = sample_uniform(log(0.1), log(1), seed);
|
||||||
|
@ -83,13 +79,7 @@ int main()
|
||||||
// printf(" log_fraction_of_intelligent_planets_which_are_detectable_as_such = %lf\n", log_fraction_of_intelligent_planets_which_are_detectable_as_such);
|
// printf(" log_fraction_of_intelligent_planets_which_are_detectable_as_such = %lf\n", log_fraction_of_intelligent_planets_which_are_detectable_as_such);
|
||||||
// printf(" log_longevity_of_detectable_civilizations = %lf\n", log_longevity_of_detectable_civilizations);
|
// printf(" log_longevity_of_detectable_civilizations = %lf\n", log_longevity_of_detectable_civilizations);
|
||||||
|
|
||||||
double log_n1 =
|
double log_n1 = log_rate_of_star_formation + log_fraction_of_stars_with_planets + log_number_of_habitable_planets_per_star_system + log_fraction_of_planets_with_life_in_which_intelligent_life_appears + log_fraction_of_intelligent_planets_which_are_detectable_as_such + log_longevity_of_detectable_civilizations;
|
||||||
log_rate_of_star_formation +
|
|
||||||
log_fraction_of_stars_with_planets +
|
|
||||||
log_number_of_habitable_planets_per_star_system +
|
|
||||||
log_fraction_of_planets_with_life_in_which_intelligent_life_appears +
|
|
||||||
log_fraction_of_intelligent_planets_which_are_detectable_as_such +
|
|
||||||
log_longevity_of_detectable_civilizations;
|
|
||||||
// printf("first part of calculation: %lf\n", log_n1);
|
// printf("first part of calculation: %lf\n", log_n1);
|
||||||
|
|
||||||
/* Consider fraction_of_habitable_planets_in_which_any_life_appears separately.
|
/* Consider fraction_of_habitable_planets_in_which_any_life_appears separately.
|
||||||
|
@ -139,7 +129,8 @@ int main()
|
||||||
return log_n;
|
return log_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
double sample_fermi_paradox_logspace(uint64_t* seed){
|
double sample_fermi_paradox_logspace(uint64_t * seed)
|
||||||
|
{
|
||||||
double n = sample_fermi_logspace(seed);
|
double n = sample_fermi_logspace(seed);
|
||||||
return ((n > 0) ? 1 : 0);
|
return ((n > 0) ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
@ -151,7 +142,6 @@ int main()
|
||||||
logspace_fermi_proportion += result;
|
logspace_fermi_proportion += result;
|
||||||
}
|
}
|
||||||
printf("Using more accurate logspace computations, %% that we are not alone: %lf\n", logspace_fermi_proportion / n);
|
printf("Using more accurate logspace computations, %% that we are not alone: %lf\n", logspace_fermi_proportion / n);
|
||||||
double result2;
|
|
||||||
|
|
||||||
free(seed);
|
free(seed);
|
||||||
}
|
}
|
Binary file not shown.
|
@ -1,5 +1,5 @@
|
||||||
#include "../squiggle.h"
|
#include "../squiggle.h"
|
||||||
#include "../squiggle_more.h"
|
// #include "../squiggle_more.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -7,16 +7,18 @@
|
||||||
|
|
||||||
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));
|
return exp(sample_uniform(log(a), log(b), seed));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
// Replicate <https://arxiv.org/pdf/1806.02404.pdf>, and in particular the red line in page 11.
|
||||||
|
// Could also be interesting to just produce and save many samples.
|
||||||
|
|
||||||
// set randomness seed
|
// set randomness seed
|
||||||
uint64_t* seed = malloc(sizeof(uint64_t));
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
||||||
*seed = UINT64_MAX/64; // xorshift can't start with a seed of 0
|
*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 rate_of_star_formation = sample_loguniform(1,100, seed);
|
||||||
double fraction_of_stars_with_planets = sample_loguniform(0.1, 1, 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 number_of_habitable_planets_per_star_system = sample_loguniform(0.1, 1, seed);
|
||||||
|
@ -50,21 +52,23 @@ int main()
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
double fermi_paradox_naive(uint64_t* seed){
|
double sample_fermi_paradox_naive(uint64_t* seed){
|
||||||
double n = fermi_naive(seed);
|
double n = sample_fermi_naive(seed);
|
||||||
return (n > 1 ? 1 : 0);
|
return ((n > 1) ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
double result;
|
double n = 1000000;
|
||||||
for(int i=0; i<1000; i++){
|
double naive_fermi_proportion = 0;
|
||||||
result = fermi_naive(seed);
|
for(int i=0; i<n; i++){
|
||||||
printf("result from fermi_naive: %lf\n", result);
|
double result = sample_fermi_paradox_naive(seed);
|
||||||
printf("\n\n");
|
// printf("result: %lf\n", result);
|
||||||
|
naive_fermi_proportion+=result;
|
||||||
}
|
}
|
||||||
printf("result from naïve implementation: %lf\n", result);
|
printf("Naïve %% that we are not alone: %lf\n", naive_fermi_proportion/n);
|
||||||
|
|
||||||
|
|
||||||
// Thinking in log space
|
// Thinking in log space
|
||||||
double fermi_logspace(uint64_t* seed){
|
double sample_fermi_logspace(uint64_t* seed){
|
||||||
double log_rate_of_star_formation = sample_uniform(log(1), log(100), seed);
|
double log_rate_of_star_formation = sample_uniform(log(1), log(100), seed);
|
||||||
double log_fraction_of_stars_with_planets = sample_uniform(log(0.1), log(1), seed);
|
double log_fraction_of_stars_with_planets = sample_uniform(log(0.1), log(1), seed);
|
||||||
double log_number_of_habitable_planets_per_star_system = sample_uniform(log(0.1), log(1), seed);
|
double log_number_of_habitable_planets_per_star_system = sample_uniform(log(0.1), log(1), seed);
|
||||||
|
@ -86,7 +90,7 @@ int main()
|
||||||
log_fraction_of_planets_with_life_in_which_intelligent_life_appears +
|
log_fraction_of_planets_with_life_in_which_intelligent_life_appears +
|
||||||
log_fraction_of_intelligent_planets_which_are_detectable_as_such +
|
log_fraction_of_intelligent_planets_which_are_detectable_as_such +
|
||||||
log_longevity_of_detectable_civilizations;
|
log_longevity_of_detectable_civilizations;
|
||||||
printf("first part of calculation: %lf\n", log_n1);
|
// printf("first part of calculation: %lf\n", log_n1);
|
||||||
|
|
||||||
/* Consider fraction_of_habitable_planets_in_which_any_life_appears separately.
|
/* Consider fraction_of_habitable_planets_in_which_any_life_appears separately.
|
||||||
Imprecisely, we could do:
|
Imprecisely, we could do:
|
||||||
|
@ -110,14 +114,15 @@ int main()
|
||||||
Turns out there is!
|
Turns out there is!
|
||||||
|
|
||||||
Looking at the Taylor expansion for c = 1 - exp(-b), it's b - b^2/2 + b^3/6 - x^b/24, etc.
|
Looking at the Taylor expansion for c = 1 - exp(-b), it's b - b^2/2 + b^3/6 - x^b/24, etc.
|
||||||
When b ~ 0 (as it is), this is close to b.
|
// https://www.wolframalpha.com/input?i=1-exp%28-x%29
|
||||||
|
When b ~ 0 (as is often the case), this is close to b.
|
||||||
|
|
||||||
But now, if b ~ 0
|
But now, if b ~ 0
|
||||||
c ~ b
|
c ~ b
|
||||||
and d = log(c) ~ log(b) = log(exp(a)) = a
|
and d = log(c) ~ log(b) = log(exp(a)) = a
|
||||||
*/
|
*/
|
||||||
double log_rate_of_life_formation_in_habitable_planets = sample_normal(1, 50, seed);
|
double log_rate_of_life_formation_in_habitable_planets = sample_normal(1, 50, seed);
|
||||||
printf("log_rate_of_life_formation_in_habitable_planets: %lf\n", log_rate_of_life_formation_in_habitable_planets);
|
// printf("log_rate_of_life_formation_in_habitable_planets: %lf\n", log_rate_of_life_formation_in_habitable_planets);
|
||||||
|
|
||||||
double log_fraction_of_habitable_planets_in_which_any_life_appears;
|
double log_fraction_of_habitable_planets_in_which_any_life_appears;
|
||||||
if(log_rate_of_life_formation_in_habitable_planets < -32){
|
if(log_rate_of_life_formation_in_habitable_planets < -32){
|
||||||
|
@ -127,22 +132,26 @@ int main()
|
||||||
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 = -expm1(-rate_of_life_formation_in_habitable_planets);
|
||||||
log_fraction_of_habitable_planets_in_which_any_life_appears = log(fraction_of_habitable_planets_in_which_any_life_appears);
|
log_fraction_of_habitable_planets_in_which_any_life_appears = log(fraction_of_habitable_planets_in_which_any_life_appears);
|
||||||
}
|
}
|
||||||
printf(" log_fraction_of_habitable_planets_in_which_any_life_appears: %lf\n", log_fraction_of_habitable_planets_in_which_any_life_appears);
|
// printf(" log_fraction_of_habitable_planets_in_which_any_life_appears: %lf\n", log_fraction_of_habitable_planets_in_which_any_life_appears);
|
||||||
|
|
||||||
double log_n = log_n1 + log_fraction_of_habitable_planets_in_which_any_life_appears;
|
double log_n = log_n1 + log_fraction_of_habitable_planets_in_which_any_life_appears;
|
||||||
|
|
||||||
return log_n;
|
return log_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
double result2;
|
double sample_fermi_paradox_logspace(uint64_t* seed){
|
||||||
|
double n = sample_fermi_logspace(seed);
|
||||||
/*
|
return ((n > 0) ? 1 : 0);
|
||||||
for(int i=0; i<1000; i++){
|
|
||||||
result2 = fermi_logspace(seed);
|
|
||||||
printf("result from logspace implementation: %lf.2\n", result2);
|
|
||||||
printf("\n\n");
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
double logspace_fermi_proportion = 0;
|
||||||
|
for(int i=0; i<n; i++){
|
||||||
|
double result = sample_fermi_paradox_logspace(seed);
|
||||||
|
// printf("result: %lf\n", result);
|
||||||
|
logspace_fermi_proportion+=result;
|
||||||
|
}
|
||||||
|
printf("Using more accurate logspace computations, %% that we are not alone: %lf\n", logspace_fermi_proportion/n);
|
||||||
|
double result2;
|
||||||
|
|
||||||
free(seed);
|
free(seed);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user