2023-07-16 19:37:43 +00:00
|
|
|
#include <float.h>
|
|
|
|
#include <limits.h>
|
2023-07-16 19:00:30 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
2023-07-16 19:26:33 +00:00
|
|
|
#include <stdio.h>
|
2023-07-16 19:00:30 +00:00
|
|
|
#include <stdlib.h>
|
2023-07-22 20:24:22 +00:00
|
|
|
#include <sys/types.h>
|
2023-07-16 19:26:33 +00:00
|
|
|
#include <time.h>
|
2023-07-16 19:52:24 +00:00
|
|
|
|
2023-07-16 19:26:33 +00:00
|
|
|
#define MAX_ERROR_LENGTH 500
|
2023-07-16 20:58:20 +00:00
|
|
|
#define EXIT_ON_ERROR 0
|
|
|
|
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
|
2023-07-16 19:00:30 +00:00
|
|
|
|
2023-07-16 20:32:03 +00:00
|
|
|
const float PI = 3.14159265358979323846; // M_PI in gcc gnu99
|
2023-07-16 19:00:30 +00:00
|
|
|
|
|
|
|
// Pseudo Random number generator
|
2023-07-16 19:08:05 +00:00
|
|
|
uint32_t xorshift32(uint32_t* seed)
|
2023-07-16 19:00:30 +00:00
|
|
|
{
|
2023-07-16 19:08:05 +00:00
|
|
|
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
|
|
|
|
// See <https://stackoverflow.com/questions/53886131/how-does-xorshift32-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/>
|
|
|
|
|
|
|
|
uint32_t x = *seed;
|
|
|
|
x ^= x << 13;
|
|
|
|
x ^= x >> 17;
|
|
|
|
x ^= x << 5;
|
|
|
|
return *seed = x;
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Distribution & sampling functions
|
2023-07-22 17:24:00 +00:00
|
|
|
// Unit distributions
|
2023-07-22 17:21:20 +00:00
|
|
|
float sample_unit_uniform(uint32_t* seed)
|
2023-07-16 19:08:05 +00:00
|
|
|
{
|
2023-07-22 17:25:36 +00:00
|
|
|
// samples uniform from [0,1] interval.
|
2023-07-16 19:08:05 +00:00
|
|
|
return ((float)xorshift32(seed)) / ((float)UINT32_MAX);
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 17:21:20 +00:00
|
|
|
float sample_unit_normal(uint32_t* seed)
|
2023-07-16 19:00:30 +00:00
|
|
|
{
|
2023-07-16 21:33:46 +00:00
|
|
|
// See: <https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform>
|
2023-07-22 17:21:20 +00:00
|
|
|
float u1 = sample_unit_uniform(seed);
|
|
|
|
float u2 = sample_unit_uniform(seed);
|
2023-07-16 19:00:30 +00:00
|
|
|
float z = sqrtf(-2.0 * log(u1)) * sin(2 * PI * u2);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2023-07-22 17:24:00 +00:00
|
|
|
// Composite distributions
|
2023-07-22 17:21:20 +00:00
|
|
|
float sample_uniform(float from, float to, uint32_t* seed)
|
2023-07-16 19:00:30 +00:00
|
|
|
{
|
2023-07-22 17:21:20 +00:00
|
|
|
return sample_unit_uniform(seed) * (to - from) + from;
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 17:21:20 +00:00
|
|
|
float sample_normal(float mean, float sigma, uint32_t* seed)
|
2023-07-16 19:00:30 +00:00
|
|
|
{
|
2023-07-22 17:21:20 +00:00
|
|
|
return (mean + sigma * sample_unit_normal(seed));
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 17:21:20 +00:00
|
|
|
float sample_lognormal(float logmean, float logsigma, uint32_t* seed)
|
2023-07-16 19:00:30 +00:00
|
|
|
{
|
2023-07-22 17:21:20 +00:00
|
|
|
return expf(sample_normal(logmean, logsigma, seed));
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 17:21:20 +00:00
|
|
|
float sample_to(float low, float high, uint32_t* seed)
|
2023-07-16 19:00:30 +00:00
|
|
|
{
|
2023-07-22 17:25:36 +00:00
|
|
|
// Given a (positive) 90% confidence interval,
|
|
|
|
// returns a sample from a lognormal
|
|
|
|
// with a matching 90% c.i.
|
2023-07-16 19:00:30 +00:00
|
|
|
const float NORMAL95CONFIDENCE = 1.6448536269514722;
|
|
|
|
float loglow = logf(low);
|
|
|
|
float loghigh = logf(high);
|
|
|
|
float logmean = (loglow + loghigh) / 2;
|
|
|
|
float logsigma = (loghigh - loglow) / (2.0 * NORMAL95CONFIDENCE);
|
2023-07-22 17:21:20 +00:00
|
|
|
return sample_lognormal(logmean, logsigma, seed);
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 19:40:35 +00:00
|
|
|
float sample_gamma(float alpha, uint32_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
|
|
|
|
if(alpha >=1){
|
|
|
|
float 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 = pow(v, 3);
|
|
|
|
u = sample_unit_uniform(seed);
|
|
|
|
if( u < 1.0 - 0.0331 * pow(x, 4)){ // 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*pow(x,2) + 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:24:22 +00:00
|
|
|
float sample_beta(float a, float b, uint32_t* seed){
|
|
|
|
float gamma_a = sample_gamma(a, seed);
|
|
|
|
float gamma_b = sample_gamma(b, seed);
|
|
|
|
return a / (a + b);
|
|
|
|
}
|
|
|
|
|
2023-07-16 19:00:30 +00:00
|
|
|
// Array helpers
|
|
|
|
float array_sum(float* array, int length)
|
|
|
|
{
|
2023-07-22 17:36:43 +00:00
|
|
|
float sum = 0.0;
|
2023-07-16 19:00:30 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2023-07-22 17:36:43 +00:00
|
|
|
sum += array[i];
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
2023-07-22 17:36:43 +00:00
|
|
|
return sum;
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void array_cumsum(float* array_to_sum, float* 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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 17:36:43 +00:00
|
|
|
float array_mean(float* array, int length){
|
|
|
|
float sum = array_sum(array, length);
|
|
|
|
return sum / length;
|
|
|
|
}
|
|
|
|
|
|
|
|
float array_std(float* array, int length){
|
|
|
|
float mean = array_mean(array, length);
|
|
|
|
float std = 0.0;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
std += pow(array[i] - mean, 2.0);
|
|
|
|
}
|
|
|
|
std=sqrt(std/length);
|
|
|
|
return std;
|
|
|
|
}
|
|
|
|
|
2023-07-16 19:00:30 +00:00
|
|
|
// Mixture function
|
2023-07-22 17:21:20 +00:00
|
|
|
float sample_mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint32_t* seed)
|
2023-07-16 19:00:30 +00:00
|
|
|
{
|
|
|
|
// You can see a simpler version of this function in the git history
|
|
|
|
// or in C-02-better-algorithm-one-thread/
|
|
|
|
float sum_weights = array_sum(weights, n_dists);
|
2023-07-16 19:08:05 +00:00
|
|
|
float* cumsummed_normalized_weights = (float*)malloc(n_dists * sizeof(float));
|
|
|
|
cumsummed_normalized_weights[0] = weights[0] / sum_weights;
|
2023-07-16 19:00:30 +00:00
|
|
|
for (int i = 1; i < n_dists; i++) {
|
2023-07-16 19:08:05 +00:00
|
|
|
cumsummed_normalized_weights[i] = cumsummed_normalized_weights[i - 1] + weights[i] / sum_weights;
|
|
|
|
}
|
|
|
|
|
|
|
|
float result;
|
|
|
|
int result_set_flag = 0;
|
2023-07-22 17:21:20 +00:00
|
|
|
float p = sample_uniform(0, 1, seed);
|
2023-07-16 19:08:05 +00:00
|
|
|
for (int k = 0; k < n_dists; k++) {
|
|
|
|
if (p < cumsummed_normalized_weights[k]) {
|
|
|
|
result = samplers[k](seed);
|
|
|
|
result_set_flag = 1;
|
|
|
|
break;
|
|
|
|
}
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
2023-07-16 19:08:05 +00:00
|
|
|
if (result_set_flag == 0)
|
|
|
|
result = samplers[n_dists - 1](seed);
|
2023-07-16 19:00:30 +00:00
|
|
|
|
2023-07-16 19:08:05 +00:00
|
|
|
free(cumsummed_normalized_weights);
|
|
|
|
return result;
|
2023-07-16 19:00:30 +00:00
|
|
|
}
|
2023-07-16 19:26:33 +00:00
|
|
|
|
2023-07-16 19:37:43 +00:00
|
|
|
// Sample from an arbitrary cdf
|
2023-07-16 19:26:33 +00:00
|
|
|
struct box {
|
|
|
|
int empty;
|
|
|
|
float content;
|
|
|
|
char* error_msg;
|
|
|
|
};
|
|
|
|
|
2023-07-16 20:59:47 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-07-16 20:58:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 19:26:33 +00:00
|
|
|
// Inverse cdf at point
|
|
|
|
// Two versions of this function:
|
|
|
|
// - raw, dealing with cdfs that return floats
|
|
|
|
// - input: cdf: float => float, p
|
|
|
|
// - output: Box(number|error)
|
|
|
|
// - box, dealing with cdfs that return a box.
|
|
|
|
// - input: cdf: float => Box(number|error), p
|
|
|
|
// - output: Box(number|error)
|
|
|
|
struct box inverse_cdf_float(float cdf(float), float 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
|
|
|
|
|
|
|
|
float low = -1.0;
|
|
|
|
float 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) {
|
2023-07-16 20:58:20 +00:00
|
|
|
return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf");
|
2023-07-16 19:26:33 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
int convergence_condition = 0;
|
|
|
|
int count = 0;
|
|
|
|
while (!convergence_condition && (count < (INT_MAX / 2))) {
|
|
|
|
float mid = (high + low) / 2;
|
|
|
|
int mid_not_new = (mid == low) || (mid == high);
|
|
|
|
// float width = high - low;
|
|
|
|
// if ((width < 1e-8) || mid_not_new){
|
|
|
|
if (mid_not_new) {
|
|
|
|
convergence_condition = 1;
|
|
|
|
} else {
|
|
|
|
float 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 {
|
2023-07-16 20:58:20 +00:00
|
|
|
return PROCESS_ERROR("Search process did not converge, in function inverse_cdf");
|
2023-07-16 19:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct box inverse_cdf_box(struct box cdf_box(float), float 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
|
|
|
|
|
|
|
|
float low = -1.0;
|
|
|
|
float 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) {
|
2023-07-16 20:58:20 +00:00
|
|
|
return PROCESS_ERROR(cdf_low.error_msg);
|
2023-07-16 19:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct box cdf_high = cdf_box(high);
|
|
|
|
if (cdf_high.empty) {
|
2023-07-16 20:58:20 +00:00
|
|
|
return PROCESS_ERROR(cdf_low.error_msg);
|
2023-07-16 19:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-07-16 20:58:20 +00:00
|
|
|
return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf");
|
2023-07-16 19:26:33 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
int convergence_condition = 0;
|
|
|
|
int count = 0;
|
|
|
|
while (!convergence_condition && (count < (INT_MAX / 2))) {
|
|
|
|
float mid = (high + low) / 2;
|
|
|
|
int mid_not_new = (mid == low) || (mid == high);
|
|
|
|
// float 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) {
|
2023-07-16 20:58:20 +00:00
|
|
|
return PROCESS_ERROR(cdf_mid.error_msg);
|
2023-07-16 19:26:33 +00:00
|
|
|
}
|
|
|
|
float 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 {
|
2023-07-16 20:58:20 +00:00
|
|
|
return PROCESS_ERROR("Search process did not converge, in function inverse_cdf");
|
2023-07-16 19:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sampler based on inverse cdf and randomness function
|
2023-07-16 20:58:20 +00:00
|
|
|
struct box sampler_cdf_box(struct box cdf(float), uint32_t* seed)
|
2023-07-16 19:26:33 +00:00
|
|
|
{
|
2023-07-22 17:21:20 +00:00
|
|
|
float p = sample_unit_uniform(seed);
|
2023-07-16 19:26:33 +00:00
|
|
|
struct box result = inverse_cdf_box(cdf, p);
|
|
|
|
return result;
|
|
|
|
}
|
2023-07-16 20:58:20 +00:00
|
|
|
struct box sampler_cdf_float(float cdf(float), uint32_t* seed)
|
2023-07-16 19:26:33 +00:00
|
|
|
{
|
2023-07-22 17:21:20 +00:00
|
|
|
float p = sample_unit_uniform(seed);
|
2023-07-16 19:26:33 +00:00
|
|
|
struct box result = inverse_cdf_float(cdf, p);
|
|
|
|
return result;
|
|
|
|
}
|
2023-07-16 21:33:46 +00:00
|
|
|
|
|
|
|
/* Could also define other variations, e.g.,
|
|
|
|
float sampler_danger(struct box cdf(float), uint32_t* seed)
|
|
|
|
{
|
2023-07-22 17:21:20 +00:00
|
|
|
float p = sample_unit_uniform(seed);
|
2023-07-16 21:33:46 +00:00
|
|
|
struct box result = inverse_cdf_box(cdf, p);
|
|
|
|
if(result.empty){
|
|
|
|
exit(1);
|
|
|
|
}else{
|
|
|
|
return result.content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|