Compare commits

..

No commits in common. "5d28295a15c8e9a7eadbb54e5963b9abd26b7115" and "b352cdf3bae52dc901ea986dd258cef87efb75f3" have entirely different histories.

30 changed files with 180 additions and 161 deletions

View File

@ -381,9 +381,7 @@ Overall, I'd describe the error handling capabilities of this library as pretty
### To do ### To do
- [ ] Drive in a few more real-life applications - [ ] Drive in a few more real-life applications
- [ ] US election modelling?
- [ ] Look into using size_t instead of int for sample numbers - [ ] Look into using size_t instead of int for sample numbers
- [ ] Reorganize code a little bit to reduce usage of gcc's nested functions
### Done ### Done

Binary file not shown.

View File

@ -3,24 +3,26 @@
#include <stdlib.h> #include <stdlib.h>
// Estimate functions // Estimate functions
double sample_0(uint64_t* seed)
{
UNUSED(seed);
return 0;
}
double sample_0(uint64_t* seed) { UNUSED(seed); return 0; } double sample_1(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); } UNUSED(seed);
double sample_many(uint64_t* seed) { return sample_to(2, 10, seed); } return 1;
}
double sample_model(uint64_t* seed){ double sample_few(uint64_t* seed)
{
return sample_to(1, 3, seed);
}
double p_a = 0.8; double sample_many(uint64_t* seed)
double p_b = 0.5; {
double p_c = p_a * p_b; 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 };
double result = sample_mixture(samplers, weights, n_dists, seed);
return result;
} }
int main() int main()
@ -29,6 +31,15 @@ int main()
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
printf("result_one: %f\n", sample_model(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_one = sample_mixture(samplers, weights, n_dists, seed);
printf("result_one: %f\n", result_one);
free(seed); free(seed);
} }

View File

@ -2,35 +2,37 @@
#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_model(seed); result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
} }
printf("Mean: %f\n", array_mean(result_many, n_samples)); printf("Mean: %f\n", array_mean(result_many, n_samples));

View File

@ -2,36 +2,39 @@
#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_model(seed); result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
} }
printf("result_many: ["); printf("result_many: [");
@ -39,6 +42,5 @@ int main()
printf("%.2f, ", result_many[i]); printf("%.2f, ", result_many[i]);
} }
printf("]\n"); printf("]\n");
free(seed); free(seed);
} }

Binary file not shown.

View File

@ -2,6 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// Estimate functions
int main() int main()
{ {
// set randomness seed // set randomness seed
@ -9,21 +11,33 @@ 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++) {
gamma_array[i] = sample_gamma(1.0, seed); double gamma_0 = sample_gamma(0.0, seed);
// printf("sample_gamma(0.0): %f\n", gamma_0);
} }
printf("gamma(1) summary statistics = mean: %f, std: %f\n", array_mean(gamma_array, n), array_std(gamma_array, n)); printf("\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_array = malloc(sizeof(double) * (size_t)n); double* beta_1_2_array = malloc(sizeof(double) * (size_t)n);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
beta_array[i] = sample_beta(1, 2.0, seed); double beta_1_2 = 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_array, n), array_std(beta_array, n)); 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));
free(beta_1_2_array);
printf("\n"); printf("\n");
free(gamma_array);
free(beta_array);
free(seed); free(seed);
} }

View File

@ -4,9 +4,16 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
double sample_fermi_logspace(uint64_t * seed) int main()
{ {
// 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);
@ -69,22 +76,14 @@ double sample_fermi_logspace(uint64_t * seed)
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;

View File

@ -3,10 +3,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
double sample_model(uint64_t* seed){
return sample_to(1, 10, seed);
}
int main() int main()
{ {
// set randomness seed // set randomness seed

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,10 +3,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
double sample_model(uint64_t * seed)
{
return sample_lognormal(0, 10, seed);
}
// Estimate functions // Estimate functions
int main() int main()
{ {
@ -17,9 +13,13 @@ int main()
int n_samples = 1000 * 1000 * 1000; int n_samples = 1000 * 1000 * 1000;
int n_threads = 16; int n_threads = 16;
double sampler(uint64_t * seed)
{
return sample_lognormal(0, 10, seed);
}
double* results = malloc((size_t)n_samples * sizeof(double)); double* results = malloc((size_t)n_samples * sizeof(double));
sampler_parallel(sample_model, results, n_threads, n_samples); sampler_parallel(sampler, results, n_threads, n_samples);
double avg = array_sum(results, n_samples) / n_samples; double avg = array_sum(results, n_samples) / n_samples;
printf("Average of 1B lognormal(0,10): %f\n", avg); printf("Average of 1B lognormal(0,10): %f\n", avg);

View File

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
double sampler_result(uint64_t * seed) int main()
{ {
double p_a = 0.8; double p_a = 0.8;
double p_b = 0.5; double p_b = 0.5;
@ -17,11 +17,10 @@ double sampler_result(uint64_t * seed)
int n_dists = 4; int n_dists = 4;
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 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 (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
double sampler_result(uint64_t * seed)
{
return sample_mixture(samplers, weights, n_dists, seed); return sample_mixture(samplers, weights, n_dists, seed);
} }
int main()
{
int n_samples = 1000 * 1000, n_threads = 16; int n_samples = 1000 * 1000, n_threads = 16;
double* results = malloc((size_t)n_samples * sizeof(double)); double* results = malloc((size_t)n_samples * sizeof(double));

View File

@ -63,8 +63,6 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_
{ {
#pragma omp for #pragma omp for
for (i = 0; i < n_threads; i++) { for (i = 0; i < n_threads; i++) {
// It's possible I don't need the for, and could instead call omp
// in some different way and get the thread number with omp_get_thread_num()
int lower_bound_inclusive = i * quotient; int lower_bound_inclusive = i * quotient;
int upper_bound_not_inclusive = ((i + 1) * quotient); // note the < in the for loop below, int upper_bound_not_inclusive = ((i + 1) * quotient); // note the < in the for loop below,