Compare commits

...

6 Commits

30 changed files with 160 additions and 179 deletions

View File

@ -381,7 +381,9 @@ Overall, I'd describe the error handling capabilities of this library as pretty
### To do
- [ ] Drive in a few more real-life applications
- [ ] US election modelling?
- [ ] 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

Binary file not shown.

View File

@ -3,33 +3,13 @@
#include <stdlib.h>
// Estimate functions
double sample_0(uint64_t* seed)
{
UNUSED(seed);
return 0;
}
double sample_1(uint64_t* seed)
{
UNUSED(seed);
return 1;
}
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_few(uint64_t* seed)
{
return sample_to(1, 3, seed);
}
double sample_many(uint64_t* seed)
{
return sample_to(2, 10, seed);
}
int main()
{
// set randomness seed
uint64_t* seed = malloc(sizeof(uint64_t));
*seed = 1000; // xorshift can't start with 0
double sample_model(uint64_t* seed){
double p_a = 0.8;
double p_b = 0.5;
@ -38,8 +18,17 @@ int main()
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);
double result_one = sample_mixture(samplers, weights, n_dists, seed);
printf("result_one: %f\n", result_one);
return result;
}
int main()
{
// set randomness seed
uint64_t* seed = malloc(sizeof(uint64_t));
*seed = 1000; // xorshift can't start with 0
printf("result_one: %f\n", sample_model(seed));
free(seed);
}

View File

@ -2,37 +2,35 @@
#include <stdio.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()
{
// set randomness seed
uint64_t* seed = malloc(sizeof(uint64_t));
*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;
double* result_many = (double*)malloc((size_t)n_samples * sizeof(double));
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));

View File

@ -2,39 +2,36 @@
#include <stdio.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()
{
// set randomness seed
uint64_t* seed = malloc(sizeof(uint64_t));
*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;
double* result_many = (double*)malloc((size_t)n_samples * sizeof(double));
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: [");
@ -42,5 +39,6 @@ int main()
printf("%.2f, ", result_many[i]);
}
printf("]\n");
free(seed);
}

Binary file not shown.

View File

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

View File

@ -4,16 +4,9 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
// 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)
{
// Replicate <https://arxiv.org/pdf/1806.02404.pdf>, and in particular the red line in page 11.
// 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_fraction_of_stars_with_planets = sample_uniform(log(0.1), log(1), seed);
@ -85,6 +78,14 @@ int main()
// 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;
int n_samples = 1000 * 1000;
for (int i = 0; i < n_samples; i++) {

View File

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

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

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

View File

@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
int main()
double sampler_result(uint64_t * seed)
{
double p_a = 0.8;
double p_b = 0.5;
@ -17,11 +17,12 @@ int main()
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 sampler_result(uint64_t * seed)
{
return sample_mixture(samplers, weights, n_dists, seed);
}
int main()
{
int n_samples = 1000 * 1000, n_threads = 16;
double* results = malloc((size_t)n_samples * sizeof(double));
sampler_parallel(sampler_result, results, n_threads, n_samples);

View File

@ -63,6 +63,8 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_
{
#pragma omp for
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 upper_bound_not_inclusive = ((i + 1) * quotient); // note the < in the for loop below,