update squiggle.c

This commit is contained in:
NunoSempere 2023-11-30 00:02:02 +00:00
parent 825336ef0a
commit 546a9ee0b5
4 changed files with 253 additions and 180 deletions

View File

@ -9,21 +9,22 @@ int main()
double p_b = 0.5; double p_b = 0.5;
double p_c = p_a * p_b; double p_c = p_a * p_b;
double sample_0(uint64_t* seed){ return 0; } double sample_0(uint64_t * seed) { return 0; }
double sample_1(uint64_t* seed) { return 1; } double sample_1(uint64_t * 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); }
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) { double sampler_result(uint64_t * seed)
{
return sample_mixture(samplers, weights, n_dists, seed); return sample_mixture(samplers, weights, n_dists, seed);
} }
int n_samples = 1000 * 1000, n_threads = 16; int n_samples = 1000 * 1000, n_threads = 16;
double* results = malloc(n_samples * sizeof(double)); double* results = malloc(n_samples * sizeof(double));
parallel_sampler(sampler_result, results, n_threads, n_samples); sampler_parallel(sampler_result, results, n_threads, n_samples);
printf("Avg: %f\n", array_sum(results, n_samples)/n_samples); printf("Avg: %f\n", array_sum(results, n_samples) / n_samples);
free(results); free(results);
} }

View File

@ -8,7 +8,7 @@
#define NORMAL90CONFIDENCE 1.6448536269514727 #define NORMAL90CONFIDENCE 1.6448536269514727
// Pseudo Random number generator // Pseudo Random number generator
uint64_t xorshift32(uint32_t* seed) static uint64_t xorshift32(uint32_t* seed)
{ {
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" // Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
// See: // See:
@ -24,7 +24,7 @@ uint64_t xorshift32(uint32_t* seed)
return *seed = x; return *seed = x;
} }
uint64_t xorshift64(uint64_t* seed) static uint64_t xorshift64(uint64_t* seed)
{ {
// same as above, but for generating doubles instead of floats // same as above, but for generating doubles instead of floats
uint64_t x = *seed; uint64_t x = *seed;

View File

@ -1,67 +1,226 @@
#include "squiggle.h"
#include <float.h> #include <float.h>
#include <math.h>
#include <limits.h> #include <limits.h>
#include <math.h>
#include <omp.h> #include <omp.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "squiggle.h"
/* Math constants */ /* Parallel sampler */
#define PI 3.14159265358979323846 // M_PI in gcc gnu99 void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples)
#define NORMAL90CONFIDENCE 1.6448536269514727 {
/* Some error niceties */ // Division terminology:
// These won't be used until later // a = b * quotient + reminder
#define MAX_ERROR_LENGTH 500 // a = (a/b)*b + (a%b)
#define EXIT_ON_ERROR 0 // dividend: a
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) // divisor: b
// quotient = a / b
// reminder = a % b
// "divisor's multiple" := (a/b)*b
// now, we have n_samples and n_threads
// to make our life easy, each thread will have a number of samples of: a/b (quotient)
// and we'll compute the remainder of samples separately
// to possibly do by Jorge: improve so that the remainder is included in the threads
int quotient = n_samples / n_threads;
/* int remainder = n_samples % n_threads; // not used, comment to avoid lint warning */
int divisor_multiple = quotient * n_threads;
uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*));
// printf("UINT64_MAX: %lu\n", UINT64_MAX);
srand(1);
for (uint64_t i = 0; i < n_threads; i++) {
seeds[i] = malloc(sizeof(uint64_t));
// Constraints:
// - xorshift can't start with 0
// - the seeds should be reasonably separated and not correlated
*seeds[i] = (uint64_t)rand() * (UINT64_MAX / RAND_MAX);
// printf("#%ld: %lu\n",i, *seeds[i]);
// Other initializations tried:
// *seeds[i] = 1 + i;
// *seeds[i] = (i + 0.5)*(UINT64_MAX/n_threads);
// *seeds[i] = (i + 0.5)*(UINT64_MAX/n_threads) + constant * i;
}
int i;
#pragma omp parallel private(i)
{
#pragma omp for
for (i = 0; i < n_threads; i++) {
int lower_bound_inclusive = i * quotient;
int upper_bound_not_inclusive = ((i + 1) * quotient); // note the < in the for loop below,
// printf("Lower bound: %d, upper bound: %d\n", lower_bound, upper_bound);
for (int j = lower_bound_inclusive; j < upper_bound_not_inclusive; j++) {
results[j] = sampler(seeds[i]);
}
}
}
for (int j = divisor_multiple; j < n_samples; j++) {
results[j] = sampler(seeds[0]);
// we can just reuse a seed, this isn't problematic because we are not doing multithreading
}
for (uint64_t i = 0; i < n_threads; i++) {
free(seeds[i]);
}
free(seeds);
}
/* Get confidence intervals, given a sampler */ /* Get confidence intervals, given a sampler */
// Not in core yet because I'm not sure how much I like the struct // Not in core yet because I'm not sure how much I like the struct
// and the built-in 100k samples // and the built-in 100k samples
// to do: add n to function parameters and document // to do: add n to function parameters and document
typedef struct ci_t { typedef struct ci_t {
float low; double low;
float high; double high;
} ci; } ci;
int compare_doubles(const void* p, const void* q)
static void swp(int i, int j, double xs[])
{ {
// https://wikiless.esmailelbob.xyz/wiki/Qsort?lang=en double tmp = xs[i];
double x = *(const double*)p; xs[i] = xs[j];
double y = *(const double*)q; xs[j] = tmp;
/* Avoid returning x - y, which can cause undefined behaviour
because of signed integer overflow. */
if (x < y)
return -1; // Return -1 if you want ascending, 1 if you want descending order.
else if (x > y)
return 1; // Return 1 if you want ascending, -1 if you want descending order.
return 0;
} }
ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed)
static int partition(int low, int high, double xs[], int length)
{ {
int n = 100 * 1000; // To understand this function:
double* samples_array = malloc(n * sizeof(double)); // - see the note after gt variable definition
for (int i = 0; i < n; i++) { // - go to commit 578bfa27 and the scratchpad/ folder in it, which has printfs sprinkled throughout
samples_array[i] = sampler(seed); int pivot = low + floor((high - low) / 2);
double pivot_value = xs[pivot];
swp(pivot, high, xs);
int gt = low; /* This pointer will iterate until finding an element which is greater than the pivot. Then it will move elements that are smaller before it--more specifically, it will move elements to its position and then increment. As a result all elements between gt and i will be greater than the pivot. */
for (int i = low; i < high; i++) {
if (xs[i] < pivot_value) {
swp(gt, i, xs);
gt++;
} }
qsort(samples_array, n, sizeof(double), compare_doubles); }
swp(high, gt, xs);
return gt;
}
static double quickselect(int k, double xs[], int n)
{
// https://en.wikipedia.org/wiki/Quickselect
int low = 0;
int high = n - 1;
for (;;) {
if (low == high) {
return xs[low];
}
int pivot = partition(low, high, xs, n);
if (pivot == k) {
return xs[pivot];
} else if (k < pivot) {
high = pivot - 1;
} else {
low = pivot + 1;
}
}
}
ci array_get_ci(ci interval, double* xs, int n)
{
int low_k = floor(interval.low * n);
int high_k = ceil(interval.high * n);
ci result = { ci result = {
.low = samples_array[5000], .low = quickselect(low_k, xs, n),
.high = samples_array[94999], .high = quickselect(high_k, xs, n),
}; };
free(samples_array); return result;
}
ci array_get_90_ci(double xs[], int n)
{
return array_get_ci((ci) { .low = 0.05, .high = 0.95 }, xs, n);
}
ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed)
{
double* xs = malloc(n * sizeof(double));
/*for (int i = 0; i < n; i++) {
xs[i] = sampler(seed);
}*/
sampler_parallel(sampler, xs, 16, n);
ci result = array_get_ci(interval, xs, n);
free(xs);
return result;
}
ci sampler_get_90_ci(double (*sampler)(uint64_t*), int n, uint64_t* seed)
{
return sampler_get_ci((ci) { .low = 0.05, .high = 0.95 }, sampler, n, seed);
}
/* Algebra manipulations */
// here I discover named structs,
// which mean that I don't have to be typing
// struct blah all the time.
#define NORMAL90CONFIDENCE 1.6448536269514727
typedef struct normal_params_t {
double mean;
double std;
} normal_params;
normal_params algebra_sum_normals(normal_params a, normal_params b)
{
normal_params result = {
.mean = a.mean + b.mean,
.std = sqrt((a.std * a.std) + (b.std * b.std)),
};
return result;
}
typedef struct lognormal_params_t {
double logmean;
double logstd;
} lognormal_params;
lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params b)
{
lognormal_params result = {
.logmean = a.logmean + b.logmean,
.logstd = sqrt((a.logstd * a.logstd) + (b.logstd * b.logstd)),
};
return result;
}
lognormal_params convert_ci_to_lognormal_params(ci x)
{
double loghigh = logf(x.high);
double loglow = logf(x.low);
double logmean = (loghigh + loglow) / 2.0;
double logstd = (loghigh - loglow) / (2.0 * NORMAL90CONFIDENCE);
lognormal_params result = { .logmean = logmean, .logstd = logstd };
return result;
}
ci convert_lognormal_params_to_ci(lognormal_params y)
{
double h = y.logstd * NORMAL90CONFIDENCE;
double loghigh = y.logmean + h;
double loglow = y.logmean - h;
ci result = { .low = exp(loglow), .high = exp(loghigh) };
return result; return result;
} }
/* Scaffolding to handle errors */ /* Scaffolding to handle errors */
// We are building towards sample from an arbitrary cdf // We will sample from an arbitrary cdf
// and that operation might fail // and that operation might fail
// so we build some scaffolding here // so we build some scaffolding here
#define MAX_ERROR_LENGTH 500
#define EXIT_ON_ERROR 0
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
struct box { struct box {
int empty; int empty;
double content; double content;
@ -246,111 +405,21 @@ double sampler_cdf_danger(struct box cdf(double), uint64_t* seed)
{ {
double p = sample_unit_uniform(seed); double p = sample_unit_uniform(seed);
struct box result = inverse_cdf_box(cdf, p); struct box result = inverse_cdf_box(cdf, p);
if(result.empty){ if (result.empty) {
exit(1); exit(1);
}else{ } else {
return result.content; return result.content;
} }
} }
/* Algebra manipulations */ /* array print: potentially useful for debugging */
// here I discover named structs,
// which mean that I don't have to be typing
// struct blah all the time.
typedef struct normal_params_t {
double mean;
double std;
} normal_params;
normal_params algebra_sum_normals(normal_params a, normal_params b) void array_print(double xs[], int n)
{ {
normal_params result = { printf("[");
.mean = a.mean + b.mean, for (int i = 0; i < n - 1; i++) {
.std = sqrt((a.std * a.std) + (b.std * b.std)), printf("%f, ", xs[i]);
}; }
return result; printf("%f", xs[n - 1]);
} printf("]\n");
typedef struct lognormal_params_t {
double logmean;
double logstd;
} lognormal_params;
lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params b)
{
lognormal_params result = {
.logmean = a.logmean + b.logmean,
.logstd = sqrt((a.logstd * a.logstd) + (b.logstd * b.logstd)),
};
return result;
}
lognormal_params convert_ci_to_lognormal_params(ci x)
{
double loghigh = logf(x.high);
double loglow = logf(x.low);
double logmean = (loghigh + loglow) / 2.0;
double logstd = (loghigh - loglow) / (2.0 * NORMAL90CONFIDENCE);
lognormal_params result = { .logmean = logmean, .logstd = logstd };
return result;
}
ci convert_lognormal_params_to_ci(lognormal_params y)
{
double h = y.logstd * NORMAL90CONFIDENCE;
double loghigh = y.logmean + h;
double loglow = y.logmean - h;
ci result = { .low = exp(loglow), .high = exp(loghigh) };
return result;
}
/* Parallel sampler */
void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples){
// Division terminology:
// a = b * quotient + reminder
// a = (a/b)*b + (a%b)
// dividend: a
// divisor: b
// quotient = a / b
// reminder = a % b
// "divisor's multiple" := (a/b)*b
// now, we have n_samples and n_threads
// to make our life easy, each thread will have a number of samples of: a/b (quotient)
// and we'll compute the remainder of samples separately
// to possibly do by Jorge: improve so that the remainder is included in the threads
int quotient = n_samples / n_threads;
int remainder = n_samples % n_threads;
int divisor_multiple = quotient * n_threads;
uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*));
for (uint64_t i = 0; i < n_threads; i++) {
seeds[i] = malloc(sizeof(uint64_t));
*seeds[i] = i + 1; // xorshift can't start with 0
}
int i;
#pragma omp parallel private(i)
{
#pragma omp for
for (i = 0; i < n_threads; i++) {
int lower_bound_inclusive = i * quotient;
int upper_bound_not_inclusive = ((i+1) * quotient); // note the < in the for loop below,
// printf("Lower bound: %d, upper bound: %d\n", lower_bound, upper_bound);
for (int j = lower_bound_inclusive; j < upper_bound_not_inclusive; j++) {
results[j] = sampler(seeds[i]);
}
}
}
for(int j=divisor_multiple; j<n_samples; j++){
results[j] = sampler(seeds[0]);
// we can just reuse a seed, this isn't problematic because we are not doing multithreading
}
for (uint64_t i = 0; i < n_threads; i++) {
free(seeds[i]);
}
free(seeds);
} }

View File

@ -1,35 +1,20 @@
#ifndef SQUIGGLE_C_EXTRA #ifndef SQUIGGLE_C_EXTRA
#define SQUIGGLE_C_EXTRA #define SQUIGGLE_C_EXTRA
// Box /* Parallel sampling */
struct box { void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples);
int empty;
double content;
char* error_msg;
};
// Macros to handle errors /* Get 90% confidence interval */
#define MAX_ERROR_LENGTH 500
#define EXIT_ON_ERROR 0
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
struct box process_error(const char* error_msg, int should_exit, char* file, int line);
// Inverse cdf
struct box inverse_cdf_double(double cdf(double), double p);
struct box inverse_cdf_box(struct box cdf_box(double), double p);
// Samplers from cdf
struct box sampler_cdf_double(double cdf(double), uint64_t* seed);
struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed);
// Get 90% confidence interval
typedef struct ci_t { typedef struct ci_t {
float low; double low;
float high; double high;
} ci; } ci;
ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed); ci array_get_ci(ci interval, double* xs, int n);
ci array_get_90_ci(double xs[], int n);
ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed);
ci sampler_get_90_ci(double (*sampler)(uint64_t*), int n, uint64_t* seed);
// small algebra manipulations /* Algebra manipulations */
typedef struct normal_params_t { typedef struct normal_params_t {
double mean; double mean;
@ -46,6 +31,24 @@ lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params
lognormal_params convert_ci_to_lognormal_params(ci x); lognormal_params convert_ci_to_lognormal_params(ci x);
ci convert_lognormal_params_to_ci(lognormal_params y); ci convert_lognormal_params_to_ci(lognormal_params y);
void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples); /* Error handling */
struct box {
int empty;
double content;
char* error_msg;
};
#define MAX_ERROR_LENGTH 500
#define EXIT_ON_ERROR 0
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
struct box process_error(const char* error_msg, int should_exit, char* file, int line);
void array_print(double* array, int length);
/* Inverse cdf */
struct box inverse_cdf_double(double cdf(double), double p);
struct box inverse_cdf_box(struct box cdf_box(double), double p);
/* Samplers from cdf */
struct box sampler_cdf_double(double cdf(double), uint64_t* seed);
struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed);
#endif #endif