#include #include #include #include #include #include #include #include #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 float PI = 3.14159265358979323846; // M_PI in gcc gnu99 // Pseudo Random number generator uint32_t xorshift32(uint32_t* seed) { // Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" // See // https://en.wikipedia.org/wiki/Xorshift // Also some drama: , uint32_t x = *seed; x ^= x << 13; x ^= x >> 17; x ^= x << 5; return *seed = x; } // Distribution & sampling functions // Unit distributions float sample_unit_uniform(uint32_t* seed) { // samples uniform from [0,1] interval. return ((float)xorshift32(seed)) / ((float)UINT32_MAX); } float sample_unit_normal(uint32_t* seed) { // See: float u1 = sample_unit_uniform(seed); float u2 = sample_unit_uniform(seed); float z = sqrtf(-2.0 * log(u1)) * sin(2 * PI * u2); return z; } // Composite distributions float sample_uniform(float from, float to, uint32_t* seed) { return sample_unit_uniform(seed) * (to - from) + from; } float sample_normal(float mean, float sigma, uint32_t* seed) { return (mean + sigma * sample_unit_normal(seed)); } float sample_lognormal(float logmean, float logsigma, uint32_t* seed) { return expf(sample_normal(logmean, logsigma, seed)); } float sample_to(float low, float high, uint32_t* seed) { // Given a (positive) 90% confidence interval, // returns a sample from a lognormal // with a matching 90% c.i. 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); return sample_lognormal(logmean, logsigma, seed); } 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 } } 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 gamma_a / (gamma_a + gamma_b); } // Array helpers float array_sum(float* array, int length) { float sum = 0.0; for (int i = 0; i < length; i++) { sum += array[i]; } return sum; } 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]; } } 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; } // Mixture function float sample_mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint32_t* seed) { // 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); float* cumsummed_normalized_weights = (float*)malloc(n_dists * sizeof(float)); 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; } float result; int result_set_flag = 0; float 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; float 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 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) { 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))) { 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 { return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); } } } 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) { 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))) { 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) { return PROCESS_ERROR(cdf_mid.error_msg); } 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 { 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(float), uint32_t* seed) { float p = sample_unit_uniform(seed); struct box result = inverse_cdf_box(cdf, p); return result; } struct box sampler_cdf_float(float cdf(float), uint32_t* seed) { float p = sample_unit_uniform(seed); struct box result = inverse_cdf_float(cdf, p); return result; } /* Could also define other variations, e.g., float sampler_danger(struct box cdf(float), uint32_t* seed) { float p = sample_unit_uniform(seed); struct box result = inverse_cdf_box(cdf, p); if(result.empty){ exit(1); }else{ return result.content; } } */