2023-07-16 19:32:17 +00:00
|
|
|
#include <math.h>
|
2023-07-16 10:09:58 +00:00
|
|
|
#include <stdint.h>
|
2023-07-16 10:26:55 +00:00
|
|
|
#include <stdio.h>
|
2023-07-16 14:30:38 +00:00
|
|
|
#include <stdlib.h>
|
2023-07-16 11:02:11 +00:00
|
|
|
#include <time.h>
|
2023-07-16 19:28:03 +00:00
|
|
|
#include "../../squiggle.h"
|
2023-07-16 10:26:55 +00:00
|
|
|
|
2023-07-16 15:59:53 +00:00
|
|
|
#define NUM_SAMPLES 1000000
|
2023-07-16 19:26:33 +00:00
|
|
|
#define STOP_BETA 1.0e-8
|
|
|
|
#define TINY_BETA 1.0e-30
|
2023-07-15 22:23:59 +00:00
|
|
|
|
2023-07-16 19:32:17 +00:00
|
|
|
// Incomplete beta function
|
2023-07-16 15:10:36 +00:00
|
|
|
struct box incbeta(float a, float b, float x)
|
|
|
|
{
|
|
|
|
// Descended from <https://github.com/codeplea/incbeta/blob/master/incbeta.c>,
|
2023-07-16 19:26:33 +00:00
|
|
|
// <https://codeplea.com/incomplete-beta-function-c>
|
2023-07-16 15:10:36 +00:00
|
|
|
// but modified to return a box struct and floats instead of doubles.
|
|
|
|
// [ ] to do: add attribution in README
|
|
|
|
// Original code under this license:
|
|
|
|
/*
|
|
|
|
* zlib License
|
|
|
|
*
|
|
|
|
* Regularized Incomplete Beta Function
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016, 2017 Lewis Van Winkle
|
|
|
|
* http://CodePlea.com
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
|
|
* arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
* claim that you wrote the original software. If you use this software
|
|
|
|
* in a product, an acknowledgement in the product documentation would be
|
|
|
|
* appreciated but is not required.
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
if (x < 0.0 || x > 1.0) {
|
|
|
|
PROCESS_ERROR("x out of bounds [0, 1], in function incbeta");
|
|
|
|
}
|
2023-07-16 10:09:58 +00:00
|
|
|
|
2023-07-16 15:10:36 +00:00
|
|
|
/*The continued fraction converges nicely for x < (a+1)/(a+b+2)*/
|
|
|
|
if (x > (a + 1.0) / (a + b + 2.0)) {
|
|
|
|
struct box symmetric_incbeta = incbeta(b, a, 1.0 - x);
|
|
|
|
if (symmetric_incbeta.empty) {
|
|
|
|
return symmetric_incbeta; // propagate error
|
|
|
|
} else {
|
2023-07-16 15:33:58 +00:00
|
|
|
struct box result = {
|
|
|
|
.empty = 0,
|
|
|
|
.content = 1 - symmetric_incbeta.content
|
|
|
|
};
|
2023-07-16 15:10:36 +00:00
|
|
|
return result;
|
2023-07-16 10:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 15:10:36 +00:00
|
|
|
/*Find the first part before the continued fraction.*/
|
|
|
|
const float lbeta_ab = lgamma(a) + lgamma(b) - lgamma(a + b);
|
|
|
|
const float front = exp(log(x) * a + log(1.0 - x) * b - lbeta_ab) / a;
|
2023-07-16 10:09:58 +00:00
|
|
|
|
2023-07-16 15:10:36 +00:00
|
|
|
/*Use Lentz's algorithm to evaluate the continued fraction.*/
|
|
|
|
float f = 1.0, c = 1.0, d = 0.0;
|
2023-07-16 10:09:58 +00:00
|
|
|
|
2023-07-16 15:10:36 +00:00
|
|
|
int i, m;
|
|
|
|
for (i = 0; i <= 200; ++i) {
|
|
|
|
m = i / 2;
|
|
|
|
|
|
|
|
float numerator;
|
|
|
|
if (i == 0) {
|
|
|
|
numerator = 1.0; /*First numerator is 1.0.*/
|
|
|
|
} else if (i % 2 == 0) {
|
|
|
|
numerator = (m * (b - m) * x) / ((a + 2.0 * m - 1.0) * (a + 2.0 * m)); /*Even term.*/
|
2023-07-16 10:09:58 +00:00
|
|
|
} else {
|
2023-07-16 15:10:36 +00:00
|
|
|
numerator = -((a + m) * (a + b + m) * x) / ((a + 2.0 * m) * (a + 2.0 * m + 1)); /*Odd term.*/
|
2023-07-16 14:56:11 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 15:10:36 +00:00
|
|
|
/*Do an iteration of Lentz's algorithm.*/
|
|
|
|
d = 1.0 + numerator * d;
|
|
|
|
if (fabs(d) < TINY_BETA)
|
|
|
|
d = TINY_BETA;
|
|
|
|
d = 1.0 / d;
|
|
|
|
|
|
|
|
c = 1.0 + numerator / c;
|
|
|
|
if (fabs(c) < TINY_BETA)
|
|
|
|
c = TINY_BETA;
|
|
|
|
|
|
|
|
const float cd = c * d;
|
|
|
|
f *= cd;
|
|
|
|
|
|
|
|
/*Check for stop.*/
|
|
|
|
if (fabs(1.0 - cd) < STOP_BETA) {
|
2023-07-16 15:33:58 +00:00
|
|
|
struct box result = {
|
|
|
|
.empty = 0,
|
|
|
|
.content = front * (f - 1.0)
|
|
|
|
};
|
2023-07-16 15:10:36 +00:00
|
|
|
return result;
|
|
|
|
}
|
2023-07-16 14:56:11 +00:00
|
|
|
}
|
2023-07-16 15:10:36 +00:00
|
|
|
|
|
|
|
PROCESS_ERROR("More loops needed, did not converge, in function incbeta");
|
2023-07-16 14:56:11 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 15:10:36 +00:00
|
|
|
struct box cdf_beta(float x)
|
|
|
|
{
|
|
|
|
if (x < 0) {
|
|
|
|
struct box result = { .empty = 0, .content = 0 };
|
|
|
|
return result;
|
|
|
|
} else if (x > 1) {
|
|
|
|
struct box result = { .empty = 0, .content = 1 };
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
float successes = 1, failures = (2023 - 1945);
|
|
|
|
return incbeta(successes, failures, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 15:33:37 +00:00
|
|
|
// Some testers
|
2023-07-16 15:33:58 +00:00
|
|
|
void test_inverse_cdf_box(char* cdf_name, struct box cdf_box(float))
|
|
|
|
{
|
2023-07-16 15:33:37 +00:00
|
|
|
struct box result = inverse_cdf_box(cdf_box, 0.5);
|
|
|
|
if (result.empty) {
|
|
|
|
printf("Inverse for %s not calculated\n", cdf_name);
|
2023-07-16 10:09:58 +00:00
|
|
|
exit(1);
|
|
|
|
} else {
|
2023-07-16 15:33:37 +00:00
|
|
|
printf("Inverse of %s at %f is: %f\n", cdf_name, 0.5, result.content);
|
2023-07-16 10:09:58 +00:00
|
|
|
}
|
2023-07-16 15:33:37 +00:00
|
|
|
}
|
2023-07-16 10:09:58 +00:00
|
|
|
|
2023-07-16 15:33:58 +00:00
|
|
|
void test_and_time_sampler_box(char* cdf_name, struct box cdf_box(float), uint32_t* seed)
|
|
|
|
{
|
2023-07-16 15:33:37 +00:00
|
|
|
printf("\nGetting some samples from %s:\n", cdf_name);
|
2023-07-16 14:30:38 +00:00
|
|
|
clock_t begin = clock();
|
2023-07-16 15:33:37 +00:00
|
|
|
for (int i = 0; i < NUM_SAMPLES; i++) {
|
|
|
|
struct box sample = sampler_box_cdf(cdf_box, seed);
|
2023-07-16 10:09:58 +00:00
|
|
|
if (sample.empty) {
|
2023-07-16 15:33:37 +00:00
|
|
|
printf("Error in sampler function for %s", cdf_name);
|
2023-07-16 10:09:58 +00:00
|
|
|
} else {
|
2023-07-16 15:59:53 +00:00
|
|
|
// printf("%f\n", sample.content);
|
2023-07-16 10:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-16 14:30:38 +00:00
|
|
|
clock_t end = clock();
|
|
|
|
float time_spent = (float)(end - begin) / CLOCKS_PER_SEC;
|
2023-07-16 15:33:37 +00:00
|
|
|
printf("Time spent: %f\n", time_spent);
|
|
|
|
}
|
2023-07-16 11:02:11 +00:00
|
|
|
|
2023-07-16 15:33:37 +00:00
|
|
|
int main()
|
|
|
|
{
|
2023-07-16 15:33:58 +00:00
|
|
|
// Test inverse cdf box
|
|
|
|
test_inverse_cdf_box("cdf_beta", cdf_beta);
|
|
|
|
|
2023-07-16 19:32:17 +00:00
|
|
|
// Test box sampler
|
2023-07-16 15:33:37 +00:00
|
|
|
uint32_t* seed = malloc(sizeof(uint32_t));
|
|
|
|
*seed = 1000; // xorshift can't start with 0
|
2023-07-16 15:33:58 +00:00
|
|
|
test_and_time_sampler_box("cdf_beta", cdf_beta, seed);
|
2023-07-16 19:26:33 +00:00
|
|
|
// Ok, this is slower than python!!
|
|
|
|
// Partly this is because I am using a more general algorithm,
|
|
|
|
// which applies to any cdf
|
2023-07-16 19:32:17 +00:00
|
|
|
// But I am also using absurdly precise convergence conditions.
|
2023-07-16 19:26:33 +00:00
|
|
|
// This could be optimized.
|
2023-07-16 15:33:37 +00:00
|
|
|
|
2023-07-16 15:33:58 +00:00
|
|
|
free(seed);
|
2023-07-16 10:09:58 +00:00
|
|
|
return 0;
|
2023-07-15 21:26:48 +00:00
|
|
|
}
|