forked from personal/squiggle.c
use new pattern to reduce nested functions extension
This commit is contained in:
parent
279fb12dee
commit
fa832cbd17
|
@ -4,12 +4,12 @@
|
||||||
|
|
||||||
// Estimate functions
|
// Estimate functions
|
||||||
|
|
||||||
double sample_model(uint64_t* seed){
|
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); }
|
||||||
|
|
||||||
double sample_0(uint64_t* seed) { UNUSED(seed); return 0; }
|
double sample_model(uint64_t* seed){
|
||||||
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); }
|
|
||||||
|
|
||||||
double p_a = 0.8;
|
double p_a = 0.8;
|
||||||
double p_b = 0.5;
|
double p_b = 0.5;
|
||||||
|
|
|
@ -2,37 +2,35 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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); }
|
||||||
|
|
||||||
|
double sample_model(uint64_t* seed){
|
||||||
|
|
||||||
|
double p_a = 0.8;
|
||||||
|
double p_b = 0.5;
|
||||||
|
double p_c = p_a * p_b;
|
||||||
|
|
||||||
|
int n_dists = 4;
|
||||||
|
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
|
||||||
|
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
|
||||||
|
double result = sample_mixture(samplers, weights, n_dists, seed);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// set randomness seed
|
// set randomness seed
|
||||||
uint64_t* seed = malloc(sizeof(uint64_t));
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
||||||
*seed = 1000; // xorshift can't start with 0
|
*seed = 1000; // xorshift can't start with 0
|
||||||
|
|
||||||
double p_a = 0.8;
|
|
||||||
double p_b = 0.5;
|
|
||||||
double p_c = p_a * p_b;
|
|
||||||
|
|
||||||
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); }
|
|
||||||
|
|
||||||
int n_dists = 4;
|
|
||||||
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
|
|
||||||
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
|
|
||||||
|
|
||||||
int n_samples = 1000000;
|
int n_samples = 1000000;
|
||||||
double* result_many = (double*)malloc((size_t)n_samples * sizeof(double));
|
double* result_many = (double*)malloc((size_t)n_samples * sizeof(double));
|
||||||
for (int i = 0; i < n_samples; i++) {
|
for (int i = 0; i < n_samples; i++) {
|
||||||
result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
|
result_many[i] = sample_model(seed);
|
||||||
}
|
}
|
||||||
printf("Mean: %f\n", array_mean(result_many, n_samples));
|
printf("Mean: %f\n", array_mean(result_many, n_samples));
|
||||||
|
|
||||||
|
|
|
@ -2,39 +2,36 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
double sample_model(uint64_t* seed){
|
||||||
|
|
||||||
|
double sample_0(uint64_t* seed) { UNUSED(seed); return 0; }
|
||||||
|
// Using a gcc extension, you can define a function inside another function
|
||||||
|
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); }
|
||||||
|
|
||||||
|
double p_a = 0.8;
|
||||||
|
double p_b = 0.5;
|
||||||
|
double p_c = p_a * p_b;
|
||||||
|
|
||||||
|
int n_dists = 4;
|
||||||
|
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
|
||||||
|
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
|
||||||
|
double result = sample_mixture(samplers, weights, n_dists, seed);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// set randomness seed
|
// set randomness seed
|
||||||
uint64_t* seed = malloc(sizeof(uint64_t));
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
||||||
*seed = 1000; // xorshift can't start with 0
|
*seed = 1000; // xorshift can't start with 0
|
||||||
|
|
||||||
double p_a = 0.8;
|
|
||||||
double p_b = 0.5;
|
|
||||||
double p_c = p_a * p_b;
|
|
||||||
|
|
||||||
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_few(uint64_t * seed) { return sample_to(1, 3, seed); }
|
|
||||||
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }
|
|
||||||
|
|
||||||
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
|
|
||||||
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
|
|
||||||
|
|
||||||
int n_samples = 1000000;
|
int n_samples = 1000000;
|
||||||
double* result_many = (double*)malloc((size_t)n_samples * sizeof(double));
|
double* result_many = (double*)malloc((size_t)n_samples * sizeof(double));
|
||||||
for (int i = 0; i < n_samples; i++) {
|
for (int i = 0; i < n_samples; i++) {
|
||||||
result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
|
result_many[i] = sample_model(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("result_many: [");
|
printf("result_many: [");
|
||||||
|
@ -42,5 +39,6 @@ int main()
|
||||||
printf("%.2f, ", result_many[i]);
|
printf("%.2f, ", result_many[i]);
|
||||||
}
|
}
|
||||||
printf("]\n");
|
printf("]\n");
|
||||||
|
|
||||||
free(seed);
|
free(seed);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
// Estimate functions
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// set randomness seed
|
// set randomness seed
|
||||||
|
@ -11,33 +9,21 @@ int main()
|
||||||
*seed = 1000; // xorshift can't start with 0
|
*seed = 1000; // xorshift can't start with 0
|
||||||
|
|
||||||
int n = 1000 * 1000;
|
int n = 1000 * 1000;
|
||||||
/*
|
double* gamma_array = malloc(sizeof(double) * (size_t)n);
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
double gamma_0 = sample_gamma(0.0, seed);
|
gamma_array[i] = sample_gamma(1.0, seed);
|
||||||
// printf("sample_gamma(0.0): %f\n", gamma_0);
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("gamma(1) summary statistics = mean: %f, std: %f\n", array_mean(gamma_array, n), array_std(gamma_array, n));
|
||||||
*/
|
|
||||||
|
|
||||||
double* gamma_1_array = malloc(sizeof(double) * (size_t)n);
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
double gamma_1 = sample_gamma(1.0, seed);
|
|
||||||
// printf("sample_gamma(1.0): %f\n", gamma_1);
|
|
||||||
gamma_1_array[i] = gamma_1;
|
|
||||||
}
|
|
||||||
printf("gamma(1) summary statistics = mean: %f, std: %f\n", array_mean(gamma_1_array, n), array_std(gamma_1_array, n));
|
|
||||||
free(gamma_1_array);
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
double* beta_1_2_array = malloc(sizeof(double) * (size_t)n);
|
double* beta_array = malloc(sizeof(double) * (size_t)n);
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
double beta_1_2 = sample_beta(1, 2.0, seed);
|
beta_array[i] = sample_beta(1, 2.0, seed);
|
||||||
// printf("sample_beta(1.0, 2.0): %f\n", beta_1_2);
|
|
||||||
beta_1_2_array[i] = beta_1_2;
|
|
||||||
}
|
}
|
||||||
printf("beta(1,2) summary statistics: mean: %f, std: %f\n", array_mean(beta_1_2_array, n), array_std(beta_1_2_array, n));
|
printf("beta(1,2) summary statistics: mean: %f, std: %f\n", array_mean(beta_array, n), array_std(beta_array, n));
|
||||||
free(beta_1_2_array);
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
free(gamma_array);
|
||||||
|
free(beta_array);
|
||||||
free(seed);
|
free(seed);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,16 +4,9 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main()
|
double sample_fermi_logspace(uint64_t * seed)
|
||||||
{
|
{
|
||||||
// Replicate <https://arxiv.org/pdf/1806.02404.pdf>, and in particular the red line in page 11.
|
// Replicate <https://arxiv.org/pdf/1806.02404.pdf>, and in particular the red line in page 11.
|
||||||
|
|
||||||
// set randomness seed
|
|
||||||
uint64_t* seed = malloc(sizeof(uint64_t));
|
|
||||||
*seed = 1001; // xorshift can't start with a seed of 0
|
|
||||||
|
|
||||||
double sample_fermi_logspace(uint64_t * seed)
|
|
||||||
{
|
|
||||||
// You can see a simple version of this function in naive.c in this same folder
|
// You can see a simple version of this function in naive.c in this same folder
|
||||||
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);
|
||||||
|
@ -76,14 +69,22 @@ int main()
|
||||||
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;
|
||||||
return log_n;
|
return log_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
double sample_are_we_alone_logspace(uint64_t * seed)
|
double sample_are_we_alone_logspace(uint64_t * seed)
|
||||||
{
|
{
|
||||||
double log_n = sample_fermi_logspace(seed);
|
double log_n = sample_fermi_logspace(seed);
|
||||||
return ((log_n > 0) ? 1 : 0);
|
return ((log_n > 0) ? 1 : 0);
|
||||||
// log_n > 0 => n > 1
|
// log_n > 0 => n > 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
// set randomness seed
|
||||||
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
||||||
|
*seed = 1001; // xorshift can't start with a seed of 0
|
||||||
|
|
||||||
double logspace_fermi_proportion = 0;
|
double logspace_fermi_proportion = 0;
|
||||||
int n_samples = 1000 * 1000;
|
int n_samples = 1000 * 1000;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user