squiggle.c/squiggle.c

226 lines
7.7 KiB
C
Raw Normal View History

2023-07-16 19:37:43 +00:00
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
// math constants
#define PI 3.14159265358979323846 // M_PI in gcc gnu99
#define NORMAL90CONFIDENCE 1.6448536269514727
// Pseudo Random number generator
2023-11-29 22:24:42 +00:00
static uint64_t xorshift32(uint32_t* seed)
{
2023-07-16 19:08:05 +00:00
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
2023-09-27 13:10:40 +00:00
// See:
// <https://en.wikipedia.org/wiki/Xorshift>
// <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works>,
2023-09-27 14:25:12 +00:00
// Also some drama:
// <https://www.pcg-random.org/posts/on-vignas-pcg-critique.html>,
2023-09-27 13:10:40 +00:00
// <https://prng.di.unimi.it/>
2023-07-23 10:47:47 +00:00
uint64_t x = *seed;
2023-07-16 19:08:05 +00:00
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return *seed = x;
}
2023-11-29 22:24:42 +00:00
static uint64_t xorshift64(uint64_t* seed)
2023-07-23 10:44:16 +00:00
{
2023-09-27 12:51:05 +00:00
// same as above, but for generating doubles instead of floats
2023-07-23 14:30:42 +00:00
uint64_t x = *seed;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return *seed = x;
2023-07-23 10:44:16 +00:00
}
// Distribution & sampling functions
// Unit distributions
double sample_unit_uniform(uint64_t* seed)
2023-07-16 19:08:05 +00:00
{
2023-07-22 17:25:36 +00:00
// samples uniform from [0,1] interval.
return ((double)xorshift64(seed)) / ((double)UINT64_MAX);
}
double sample_unit_normal(uint64_t* seed)
{
2023-09-27 13:10:40 +00:00
// // See: <https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform>
2023-09-27 14:25:12 +00:00
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)
{
2023-07-23 10:44:16 +00:00
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));
}
2023-07-23 14:30:42 +00:00
double sample_lognormal(double logmean, double logstd, uint64_t* seed)
{
2023-07-23 14:30:42 +00:00
return exp(sample_normal(logmean, logstd, seed));
}
2023-09-27 12:51:05 +00:00
inline double sample_normal_from_90_confidence_interval(double low, double high, uint64_t* seed)
{
// Explanation of key idea:
// 1. We know that the 90% confidence interval of the unit normal is
// [-1.6448536269514722, 1.6448536269514722]
// see e.g.: https://stackoverflow.com/questions/20626994/how-to-calculate-the-inverse-of-the-normal-cumulative-distribution-function-in-p
2023-11-02 21:49:35 +00:00
// or https://www.wolframalpha.com/input?i=N%5BInverseCDF%28normal%280%2C1%29%2C+0.05%29%2C%7B%E2%88%9E%2C100%7D%5D
// 2. So if we take a unit normal and multiply it by
// L / 1.6448536269514722, its new 90% confidence interval will be
// [-L, L], i.e., length 2 * L
// 3. Instead, if we want to get a confidence interval of length L,
// we should multiply the unit normal by
// L / (2 * 1.6448536269514722)
// Meaning that its standard deviation should be multiplied by that amount
// see: https://en.wikipedia.org/wiki/Normal_distribution?lang=en#Operations_on_a_single_normal_variable
// 4. So we have learnt that Normal(0, L / (2 * 1.6448536269514722))
// has a 90% confidence interval of length L
// 5. If we want a 90% confidence interval from high to low,
// we can set mean = (high + low)/2; the midpoint, and L = high-low,
// Normal([high + low]/2, [high - low]/(2 * 1.6448536269514722))
double mean = (high + low) / 2.0;
2023-09-27 13:10:40 +00:00
double std = (high - low) / (2.0 * NORMAL90CONFIDENCE);
return sample_normal(mean, std, seed);
}
double sample_to(double low, double high, uint64_t* seed)
{
2023-07-22 17:25:36 +00:00
// Given a (positive) 90% confidence interval,
// returns a sample from a lognorma with a matching 90% c.i.
// Key idea: If we want a lognormal with 90% confidence interval [a, b]
// we need but get a normal with 90% confidence interval [log(a), log(b)].
2023-09-27 14:25:12 +00:00
// Then see code for sample_normal_from_90_confidence_interval
double loglow = logf(low);
double loghigh = logf(high);
2023-09-27 12:51:05 +00:00
return exp(sample_normal_from_90_confidence_interval(loglow, loghigh, seed));
}
double sample_gamma(double alpha, uint64_t* seed)
2023-07-23 07:29:00 +00:00
{
// 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
2023-07-23 22:37:45 +00:00
// 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.
2023-07-23 07:29:00 +00:00
if (alpha >= 1) {
double d, c, x, v, u;
2023-07-23 07:29:00 +00:00
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);
2023-07-23 14:30:42 +00:00
v = v * v * v;
2023-07-23 07:29:00 +00:00
u = sample_unit_uniform(seed);
if (u < 1.0 - 0.0331 * (x * x * x * x)) { // Condition 1
2023-07-23 07:29:00 +00:00
// 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
2023-07-23 07:29:00 +00:00
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)
2023-07-23 07:29:00 +00:00
{
// See: https://en.wikipedia.org/wiki/Gamma_distribution#Related_distributions
double gamma_a = sample_gamma(a, seed);
double gamma_b = sample_gamma(b, seed);
2023-07-23 07:29:00 +00:00
return gamma_a / (gamma_a + gamma_b);
2023-07-22 20:24:22 +00:00
}
double sample_laplace(double successes, double failures, uint64_t* seed)
{
// see <https://en.wikipedia.org/wiki/Beta_distribution?lang=en#Rule_of_succession>
return sample_beta(successes + 1, failures + 1, seed);
2023-08-11 12:01:02 +00:00
}
// Array helpers
double array_sum(double* array, int length)
{
double sum = 0.0;
for (int i = 0; i < length; i++) {
2023-07-22 17:36:43 +00:00
sum += array[i];
}
2023-07-22 17:36:43 +00:00
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)
2023-07-23 07:29:00 +00:00
{
double sum = array_sum(array, length);
2023-07-23 07:29:00 +00:00
return sum / length;
2023-07-22 17:36:43 +00:00
}
double array_std(double* array, int length)
2023-07-23 07:29:00 +00:00
{
double mean = array_mean(array, length);
double std = 0.0;
2023-07-22 17:36:43 +00:00
for (int i = 0; i < length; i++) {
std += (array[i] - mean) * (array[i] - mean);
2023-07-22 17:36:43 +00:00
}
2023-07-23 07:29:00 +00:00
std = sqrt(std / length);
2023-07-22 17:36:43 +00:00
return std;
}
// Mixture function
double sample_mixture(double (*samplers[])(uint64_t*), double* weights, int n_dists, uint64_t* seed)
{
// Sample from samples with frequency proportional to their weights.
double sum_weights = array_sum(weights, n_dists);
double* cumsummed_normalized_weights = (double*)malloc(n_dists * sizeof(double));
2023-07-16 19:08:05 +00:00
cumsummed_normalized_weights[0] = weights[0] / sum_weights;
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;
}
double result;
2023-07-16 19:08:05 +00:00
int result_set_flag = 0;
double 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:08:05 +00:00
if (result_set_flag == 0)
result = samplers[n_dists - 1](seed);
2023-07-16 19:08:05 +00:00
free(cumsummed_normalized_weights);
return result;
}