forked from personal/squiggle.c
437 lines
13 KiB
C
437 lines
13 KiB
C
#include <float.h>
|
|
#include <limits.h>
|
|
#include <math.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <time.h>
|
|
|
|
#define MAX_ERROR_LENGTH 500
|
|
#define EXIT_ON_ERROR 0
|
|
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
|
|
|
|
const double PI = 3.14159265358979323846; // M_PI in gcc gnu99
|
|
|
|
// Pseudo Random number generator
|
|
uint64_t xorshift32(uint32_t* seed)
|
|
{
|
|
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
|
|
// See <https://stackoverflow.com/questions/53886131/how-does-xorshift64-works>
|
|
// https://en.wikipedia.org/wiki/Xorshift
|
|
// Also some drama: <https://www.pcg-random.org/posts/on-vignas-pcg-critique.html>, <https://prng.di.unimi.it/>
|
|
|
|
uint64_t x = *seed;
|
|
x ^= x << 13;
|
|
x ^= x >> 17;
|
|
x ^= x << 5;
|
|
return *seed = x;
|
|
}
|
|
|
|
uint64_t xorshift64(uint64_t* seed)
|
|
{
|
|
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
|
|
// See <https://stackoverflow.com/questions/53886131/how-does-xorshift64-works>
|
|
// https://en.wikipedia.org/wiki/Xorshift
|
|
// Also some drama: <https://www.pcg-random.org/posts/on-vignas-pcg-critique.html>, <https://prng.di.unimi.it/>
|
|
|
|
uint64_t x = *seed;
|
|
x ^= x << 13;
|
|
x ^= x >> 7;
|
|
x ^= x << 17;
|
|
return *seed = x;
|
|
}
|
|
|
|
// Distribution & sampling functions
|
|
// Unit distributions
|
|
double sample_unit_uniform(uint64_t* seed)
|
|
{
|
|
// samples uniform from [0,1] interval.
|
|
return ((double)xorshift64(seed)) / ((double)UINT64_MAX);
|
|
}
|
|
|
|
double sample_unit_normal(uint64_t* seed)
|
|
{
|
|
// See: <https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform>
|
|
double u1 = sample_unit_uniform(seed);
|
|
double u2 = sample_unit_uniform(seed);
|
|
double z = sqrtf(-2.0 * log(u1)) * sin(2 * PI * u2);
|
|
return z;
|
|
}
|
|
|
|
// Composite distributions
|
|
double sample_uniform(double start, double end, uint64_t* seed)
|
|
{
|
|
return sample_unit_uniform(seed) * (end - start) + start;
|
|
}
|
|
|
|
double sample_normal(double mean, double sigma, uint64_t* seed)
|
|
{
|
|
return (mean + sigma * sample_unit_normal(seed));
|
|
}
|
|
|
|
double sample_lognormal(double logmean, double logstd, uint64_t* seed)
|
|
{
|
|
return exp(sample_normal(logmean, logstd, seed));
|
|
}
|
|
|
|
double sample_to(double low, double high, uint64_t* seed)
|
|
{
|
|
// Given a (positive) 90% confidence interval,
|
|
// returns a sample from a lognormal
|
|
// with a matching 90% c.i.
|
|
const double NORMAL95CONFIDENCE = 1.6448536269514722;
|
|
double loglow = logf(low);
|
|
double loghigh = logf(high);
|
|
double logmean = (loglow + loghigh) / 2;
|
|
double logstd = (loghigh - loglow) / (2.0 * NORMAL95CONFIDENCE);
|
|
return sample_lognormal(logmean, logstd, seed);
|
|
}
|
|
|
|
double sample_gamma(double alpha, uint64_t* seed)
|
|
{
|
|
|
|
// A Simple Method for Generating Gamma Variables, Marsaglia and Wan Tsang, 2001
|
|
// https://dl.acm.org/doi/pdf/10.1145/358407.358414
|
|
// see also the references/ folder
|
|
// Note that the Wikipedia page for the gamma distribution includes a scaling parameter
|
|
// k or beta
|
|
// https://en.wikipedia.org/wiki/Gamma_distribution
|
|
// such that gamma_k(alpha, k) = k * gamma(alpha)
|
|
// or gamma_beta(alpha, beta) = gamma(alpha) / beta
|
|
// So far I have not needed to use this, and thus the second parameter is by default 1.
|
|
if (alpha >= 1) {
|
|
double d, c, x, v, u;
|
|
d = alpha - 1.0 / 3.0;
|
|
c = 1.0 / sqrt(9.0 * d);
|
|
while (1) {
|
|
|
|
do {
|
|
x = sample_unit_normal(seed);
|
|
v = 1.0 + c * x;
|
|
} while (v <= 0.0);
|
|
|
|
v = v * v * v;
|
|
u = sample_unit_uniform(seed);
|
|
if (u < 1.0 - 0.0331 * (x * x * x * x)) { // Condition 1
|
|
// the 0.0331 doesn't inspire much confidence
|
|
// however, this isn't the whole story
|
|
// by knowing that Condition 1 implies condition 2
|
|
// we realize that this is just a way of making the algorithm faster
|
|
// i.e., of not using the logarithms
|
|
return d * v;
|
|
}
|
|
if (log(u) < 0.5 * (x * x) + d * (1.0 - v + log(v))) { // Condition 2
|
|
return d * v;
|
|
}
|
|
}
|
|
} else {
|
|
return sample_gamma(1 + alpha, seed) * pow(sample_unit_uniform(seed), 1 / alpha);
|
|
// see note in p. 371 of https://dl.acm.org/doi/pdf/10.1145/358407.358414
|
|
}
|
|
}
|
|
|
|
double sample_beta(double a, double b, uint64_t* seed)
|
|
{
|
|
double gamma_a = sample_gamma(a, seed);
|
|
double gamma_b = sample_gamma(b, seed);
|
|
return gamma_a / (gamma_a + gamma_b);
|
|
}
|
|
|
|
// Array helpers
|
|
double array_sum(double* array, int length)
|
|
{
|
|
double sum = 0.0;
|
|
for (int i = 0; i < length; i++) {
|
|
sum += array[i];
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
void array_cumsum(double* array_to_sum, double* array_cumsummed, int length)
|
|
{
|
|
array_cumsummed[0] = array_to_sum[0];
|
|
for (int i = 1; i < length; i++) {
|
|
array_cumsummed[i] = array_cumsummed[i - 1] + array_to_sum[i];
|
|
}
|
|
}
|
|
|
|
double array_mean(double* array, int length)
|
|
{
|
|
double sum = array_sum(array, length);
|
|
return sum / length;
|
|
}
|
|
|
|
double array_std(double* array, int length)
|
|
{
|
|
double mean = array_mean(array, length);
|
|
double std = 0.0;
|
|
for (int i = 0; i < length; i++) {
|
|
std += (array[i] - mean) * (array[i] - mean);
|
|
}
|
|
std = sqrt(std / length);
|
|
return std;
|
|
}
|
|
|
|
// Mixture function
|
|
double sample_mixture(double (*samplers[])(uint64_t*), double* weights, int n_dists, uint64_t* seed)
|
|
{
|
|
// You can see a simpler version of this function in the git history
|
|
// or in C-02-better-algorithm-one-thread/
|
|
double sum_weights = array_sum(weights, n_dists);
|
|
double* cumsummed_normalized_weights = (double*)malloc(n_dists * sizeof(double));
|
|
cumsummed_normalized_weights[0] = weights[0] / sum_weights;
|
|
for (int i = 1; i < n_dists; i++) {
|
|
cumsummed_normalized_weights[i] = cumsummed_normalized_weights[i - 1] + weights[i] / sum_weights;
|
|
}
|
|
|
|
double result;
|
|
int result_set_flag = 0;
|
|
double p = sample_uniform(0, 1, seed);
|
|
for (int k = 0; k < n_dists; k++) {
|
|
if (p < cumsummed_normalized_weights[k]) {
|
|
result = samplers[k](seed);
|
|
result_set_flag = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (result_set_flag == 0)
|
|
result = samplers[n_dists - 1](seed);
|
|
|
|
free(cumsummed_normalized_weights);
|
|
return result;
|
|
}
|
|
|
|
// Sample from an arbitrary cdf
|
|
struct box {
|
|
int empty;
|
|
double content;
|
|
char* error_msg;
|
|
};
|
|
|
|
struct box process_error(const char* error_msg, int should_exit, char* file, int line)
|
|
{
|
|
if (should_exit) {
|
|
printf("@, in %s (%d)", file, line);
|
|
exit(1);
|
|
} else {
|
|
char error_msg[MAX_ERROR_LENGTH];
|
|
snprintf(error_msg, MAX_ERROR_LENGTH, "@, in %s (%d)", file, line);
|
|
struct box error = { .empty = 1, .error_msg = error_msg };
|
|
return error;
|
|
}
|
|
}
|
|
|
|
// Inverse cdf at point
|
|
// Two versions of this function:
|
|
// - raw, dealing with cdfs that return doubles
|
|
// - input: cdf: double => double, p
|
|
// - output: Box(number|error)
|
|
// - box, dealing with cdfs that return a box.
|
|
// - input: cdf: double => Box(number|error), p
|
|
// - output: Box(number|error)
|
|
struct box inverse_cdf_double(double cdf(double), double p)
|
|
{
|
|
// given a cdf: [-Inf, Inf] => [0,1]
|
|
// returns a box with either
|
|
// x such that cdf(x) = p
|
|
// or an error
|
|
// if EXIT_ON_ERROR is set to 1, it exits instead of providing an error
|
|
|
|
double low = -1.0;
|
|
double high = 1.0;
|
|
|
|
// 1. Make sure that cdf(low) < p < cdf(high)
|
|
int interval_found = 0;
|
|
while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) {
|
|
// ^ Using FLT_MIN and FLT_MAX is overkill
|
|
// but it's also the *correct* thing to do.
|
|
|
|
int low_condition = (cdf(low) < p);
|
|
int high_condition = (p < cdf(high));
|
|
if (low_condition && high_condition) {
|
|
interval_found = 1;
|
|
} else if (!low_condition) {
|
|
low = low * 2;
|
|
} else if (!high_condition) {
|
|
high = high * 2;
|
|
}
|
|
}
|
|
|
|
if (!interval_found) {
|
|
return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf");
|
|
} else {
|
|
|
|
int convergence_condition = 0;
|
|
int count = 0;
|
|
while (!convergence_condition && (count < (INT_MAX / 2))) {
|
|
double mid = (high + low) / 2;
|
|
int mid_not_new = (mid == low) || (mid == high);
|
|
// double width = high - low;
|
|
// if ((width < 1e-8) || mid_not_new){
|
|
if (mid_not_new) {
|
|
convergence_condition = 1;
|
|
} else {
|
|
double mid_sign = cdf(mid) - p;
|
|
if (mid_sign < 0) {
|
|
low = mid;
|
|
} else if (mid_sign > 0) {
|
|
high = mid;
|
|
} else if (mid_sign == 0) {
|
|
low = mid;
|
|
high = mid;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (convergence_condition) {
|
|
struct box result = { .empty = 0, .content = low };
|
|
return result;
|
|
} else {
|
|
return PROCESS_ERROR("Search process did not converge, in function inverse_cdf");
|
|
}
|
|
}
|
|
}
|
|
|
|
struct box inverse_cdf_box(struct box cdf_box(double), double p)
|
|
{
|
|
// given a cdf: [-Inf, Inf] => Box([0,1])
|
|
// returns a box with either
|
|
// x such that cdf(x) = p
|
|
// or an error
|
|
// if EXIT_ON_ERROR is set to 1, it exits instead of providing an error
|
|
|
|
double low = -1.0;
|
|
double high = 1.0;
|
|
|
|
// 1. Make sure that cdf(low) < p < cdf(high)
|
|
int interval_found = 0;
|
|
while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) {
|
|
// ^ Using FLT_MIN and FLT_MAX is overkill
|
|
// but it's also the *correct* thing to do.
|
|
struct box cdf_low = cdf_box(low);
|
|
if (cdf_low.empty) {
|
|
return PROCESS_ERROR(cdf_low.error_msg);
|
|
}
|
|
|
|
struct box cdf_high = cdf_box(high);
|
|
if (cdf_high.empty) {
|
|
return PROCESS_ERROR(cdf_low.error_msg);
|
|
}
|
|
|
|
int low_condition = (cdf_low.content < p);
|
|
int high_condition = (p < cdf_high.content);
|
|
if (low_condition && high_condition) {
|
|
interval_found = 1;
|
|
} else if (!low_condition) {
|
|
low = low * 2;
|
|
} else if (!high_condition) {
|
|
high = high * 2;
|
|
}
|
|
}
|
|
|
|
if (!interval_found) {
|
|
return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf");
|
|
} else {
|
|
|
|
int convergence_condition = 0;
|
|
int count = 0;
|
|
while (!convergence_condition && (count < (INT_MAX / 2))) {
|
|
double mid = (high + low) / 2;
|
|
int mid_not_new = (mid == low) || (mid == high);
|
|
// double width = high - low;
|
|
if (mid_not_new) {
|
|
// if ((width < 1e-8) || mid_not_new){
|
|
convergence_condition = 1;
|
|
} else {
|
|
struct box cdf_mid = cdf_box(mid);
|
|
if (cdf_mid.empty) {
|
|
return PROCESS_ERROR(cdf_mid.error_msg);
|
|
}
|
|
double mid_sign = cdf_mid.content - p;
|
|
if (mid_sign < 0) {
|
|
low = mid;
|
|
} else if (mid_sign > 0) {
|
|
high = mid;
|
|
} else if (mid_sign == 0) {
|
|
low = mid;
|
|
high = mid;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (convergence_condition) {
|
|
struct box result = { .empty = 0, .content = low };
|
|
return result;
|
|
} else {
|
|
return PROCESS_ERROR("Search process did not converge, in function inverse_cdf");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sampler based on inverse cdf and randomness function
|
|
struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed)
|
|
{
|
|
double p = sample_unit_uniform(seed);
|
|
struct box result = inverse_cdf_box(cdf, p);
|
|
return result;
|
|
}
|
|
struct box sampler_cdf_double(double cdf(double), uint64_t* seed)
|
|
{
|
|
double p = sample_unit_uniform(seed);
|
|
struct box result = inverse_cdf_double(cdf, p);
|
|
return result;
|
|
}
|
|
|
|
// Get confidence intervals, given a sampler
|
|
|
|
struct c_i {
|
|
float low;
|
|
float high;
|
|
};
|
|
int compare_doubles(const void* p, const void* q)
|
|
{
|
|
// https://wikiless.esmailelbob.xyz/wiki/Qsort?lang=en
|
|
double x = *(const double*)p;
|
|
double y = *(const double*)q;
|
|
|
|
/* Avoid return 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;
|
|
}
|
|
struct c_i get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed)
|
|
{
|
|
int n = 100 * 1000;
|
|
double* samples_array = malloc(n * sizeof(double));
|
|
for (int i = 0; i < n; i++) {
|
|
samples_array[i] = sampler(seed);
|
|
}
|
|
qsort(samples_array, n, sizeof(double), compare_doubles);
|
|
|
|
struct c_i result = {
|
|
.low = samples_array[5000],
|
|
.high = samples_array[94999],
|
|
};
|
|
free(samples_array);
|
|
|
|
return result;
|
|
}
|
|
|
|
/* Could also define other variations, e.g.,
|
|
double sampler_danger(struct box cdf(double), uint64_t* seed)
|
|
{
|
|
double p = sample_unit_uniform(seed);
|
|
struct box result = inverse_cdf_box(cdf, p);
|
|
if(result.empty){
|
|
exit(1);
|
|
}else{
|
|
return result.content;
|
|
}
|
|
}
|
|
*/
|