From 0f5f68f21f6a38697bd4fa863e5684b74cd5f80d Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Wed, 7 Feb 2024 19:44:02 +0100 Subject: [PATCH] add possible gotcha to folk wisdom file --- FOLK_WISDOM.md | 59 ++++++++++++++ scratchpad/scratchpad | Bin 26992 -> 26832 bytes scratchpad/scratchpad.c | 166 +++++++--------------------------------- 3 files changed, 86 insertions(+), 139 deletions(-) diff --git a/FOLK_WISDOM.md b/FOLK_WISDOM.md index 6a29f9f..2dc9537 100644 --- a/FOLK_WISDOM.md +++ b/FOLK_WISDOM.md @@ -269,6 +269,65 @@ The last commit that has code to sample from arbitrary cdfs is `8f6919fa2...`. Y - Even though the C standard is ambiguous about this, this code assumes that doubles are 64 bit precision (otherwise the xorshift code should be different). +### Consider this code + +Consider sampling from the unit uniform in this manner: + +``` +#include "../squiggle.h" +#include "../squiggle_more.h" +#include +#include +#include +#include + +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 + + int n_samples = 100*MILLION; + int p_sixteenth = 0; + int p_eighth = 0; + int p_quarter = 0; + int p_half = 0; + double sample; + for(int i=0; i 0.5\n"); + } + } + printf("p_16th: %lf; p_eighth; %lf; p_quarter: %lf; p_half: %lf", ((double)p_sixteenth)/n_samples, (double)p_eighth/n_samples, (double)p_quarter/n_samples, (double)p_half/n_samples); +} +``` + +What will be printed out? In particular, consider that not all floating points have the same density. + +
+Click on the arrow to see the answer +The p_eighth will be ~0.125, p_quarter will be ~0.25, p_half will be ~0.5. This is because these random numbers are produced by generating ints and then dividing by the maximum int. There may be additional gotchas here, but this at least ensures that intervals of the same length in [0,1] will have the same number of samples. It's just that those that are smaller will be represented with more precision. +
+ ## Related projects - [Squiggle](https://www.squiggle-language.com/) diff --git a/scratchpad/scratchpad b/scratchpad/scratchpad index f3010c5a74d179998e74509811bd7296b6a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{0=%emq$Z z#h)sw47@c@$Mao|<_ZJ9jh`L3?&!n9%Q`+=n2I%1NjC(%X-tswbv)nMX#Sp=9#0PM zHKLUMSxMz2EyDB-)sw?vLR!kUG<}RGRx}fUw|<9d?FN0`13Eo@7NAl9Jad75Y;UvTJTNHV;5iGmEAs`vfJ!i#>qO~>b8-BjK%@Q(Xj z{c8h1)i9*rz`Z&?T96I@x^zA~nR6vQl)_1cmGSzJ-_&O4NO=F&=jSm<{)xwm4G>$S z)BEcfgTB@9G0)f-OfmTT|Nn1jD5UH39zn*xf~c4b3y&}u^&3ItU&{mopO>c7`;W_M zz>`AO8XfPis|@-Yn4QY6pa_-u9Iv^a*w$iBSw`>b&-ZyAY#p;9p3&mk}05q40a0~e(8Tcug=}z~X3U&Uc%m?A6#Jp`{hVTUyfbHtDdF2cE^s*fKP$kG z379rlj#yf|xV6dEM)&FJ`r76Nt~QobS;T%>J!pu5Y-yk03)x$hlOyiAu~*mY%x+fBV)LtF*oLZc5&Y*8cR7t1K8k%%m6Pq~ zMS9I|SuWHzEpW|mtF3NrYpHLqb64ZdsJW#Lzo1ykQszCJtdkSAy?J2+1_Th+xt1=i zcD1#&w6V5%6&62BQV+TvpO+NqTE5WDGOHI!_bLf-Y-9CU7FsoU$HnUTrX5pjWs}Lq zmdqb#va{{;bHn{7l^^2B12)&e*aHp%GecGZba*NJuLiC8MI?WL$fK|zhA6V l_3>b~q&AuLE~qh`V)>1uL*%(Sei>VW-itMQFCNQE{$HXb(~kfE diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c index da52936..b173ad3 100644 --- a/scratchpad/scratchpad.c +++ b/scratchpad/scratchpad.c @@ -1,14 +1,10 @@ #include "../squiggle.h" -// #include "../squiggle_more.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. @@ -18,140 +14,32 @@ int main() 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.5\n"); } - // 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; - - return log_n; } - - double sample_fermi_paradox_logspace(uint64_t* seed){ - double n = sample_fermi_logspace(seed); - return ((n > 0) ? 1 : 0); - } - - double logspace_fermi_proportion = 0; - for(int i=0; i