formatting pass.
This commit is contained in:
parent
607554f22b
commit
c487bdfaf5
|
@ -1,9 +1,9 @@
|
|||
#include <limits.h> // INT_MAX
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <float.h> // FLT_MAX, FLT_MIN
|
||||
#include <stdio.h>
|
||||
#include <limits.h> // INT_MAX
|
||||
#include <math.h> // erf, sqrt
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define EXIT_ON_ERROR 0
|
||||
|
@ -12,7 +12,7 @@
|
|||
struct box {
|
||||
int empty;
|
||||
float content;
|
||||
char * error_msg;
|
||||
char* error_msg;
|
||||
};
|
||||
|
||||
// Example cdf
|
||||
|
@ -76,10 +76,10 @@ struct box inverse_cdf(float cdf(float), float p)
|
|||
}
|
||||
|
||||
if (!interval_found) {
|
||||
if(EXIT_ON_ERROR){
|
||||
if (EXIT_ON_ERROR) {
|
||||
printf("Interval containing the target value not found, in function inverse_cdf, in %s (%d)", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}else{
|
||||
} else {
|
||||
char error_msg[200];
|
||||
snprintf(error_msg, 200, "Interval containing the target value not found in function inverse_cdf, in %s (%d)", __FILE__, __LINE__);
|
||||
result.empty = 1;
|
||||
|
@ -114,10 +114,10 @@ struct box inverse_cdf(float cdf(float), float p)
|
|||
result.content = low;
|
||||
result.empty = 0;
|
||||
} else {
|
||||
if(EXIT_ON_ERROR){
|
||||
if (EXIT_ON_ERROR) {
|
||||
printf("Search process did not converge, in function inverse_cdf, in %s (%d)", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}else{
|
||||
} else {
|
||||
char error_msg[200];
|
||||
snprintf(error_msg, 200, "Search process did not converge, in function inverse_cdf, in %s (%d)", __FILE__, __LINE__);
|
||||
result.empty = 1;
|
||||
|
@ -178,7 +178,8 @@ float sampler_normal_0_1(uint32_t* seed)
|
|||
#define STOP 1.0e-8
|
||||
#define TINY 1.0e-30
|
||||
|
||||
struct box incbeta(float a, float b, float x) {
|
||||
struct box incbeta(float a, float b, float x)
|
||||
{
|
||||
// Descended from <https://github.com/codeplea/incbeta/blob/master/incbeta.c>,
|
||||
// but modified to return a box struct and floats instead of doubles.
|
||||
// [x] to do: add attribution in README
|
||||
|
@ -209,11 +210,11 @@ struct box incbeta(float a, float b, float x) {
|
|||
*/
|
||||
struct box result;
|
||||
|
||||
if (x < 0.0 || x > 1.0){
|
||||
if(EXIT_ON_ERROR){
|
||||
if (x < 0.0 || x > 1.0) {
|
||||
if (EXIT_ON_ERROR) {
|
||||
printf("x = %f, x out of bounds [0, 1], in function incbeta, in %s (%d)", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}else{
|
||||
} else {
|
||||
char error_msg[200];
|
||||
snprintf(error_msg, 200, "x = %f, x out of bounds [0, 1], in function incbeta, in %s (%d)", x, __FILE__, __LINE__);
|
||||
result.empty = 1;
|
||||
|
@ -223,60 +224,62 @@ struct box incbeta(float a, float b, float x) {
|
|||
}
|
||||
|
||||
/*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){
|
||||
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{
|
||||
} else {
|
||||
result.empty = 0;
|
||||
result.content = 1-symmetric_incbeta.content;
|
||||
result.content = 1 - symmetric_incbeta.content;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/*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;
|
||||
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;
|
||||
|
||||
/*Use Lentz's algorithm to evaluate the continued fraction.*/
|
||||
float f = 1.0, c = 1.0, d = 0.0;
|
||||
|
||||
int i, m;
|
||||
for (i = 0; i <= 200; ++i) {
|
||||
m = i/2;
|
||||
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.*/
|
||||
numerator = (m * (b - m) * x) / ((a + 2.0 * m - 1.0) * (a + 2.0 * m)); /*Even term.*/
|
||||
} else {
|
||||
numerator = -((a+m)*(a+b+m)*x)/((a+2.0*m)*(a+2.0*m+1)); /*Odd term.*/
|
||||
numerator = -((a + m) * (a + b + m) * x) / ((a + 2.0 * m) * (a + 2.0 * m + 1)); /*Odd term.*/
|
||||
}
|
||||
|
||||
/*Do an iteration of Lentz's algorithm.*/
|
||||
d = 1.0 + numerator * d;
|
||||
if (fabs(d) < TINY) d = TINY;
|
||||
if (fabs(d) < TINY)
|
||||
d = TINY;
|
||||
d = 1.0 / d;
|
||||
|
||||
c = 1.0 + numerator / c;
|
||||
if (fabs(c) < TINY) c = TINY;
|
||||
if (fabs(c) < TINY)
|
||||
c = TINY;
|
||||
|
||||
const float cd = c*d;
|
||||
const float cd = c * d;
|
||||
f *= cd;
|
||||
|
||||
/*Check for stop.*/
|
||||
if (fabs(1.0-cd) < STOP) {
|
||||
result.content = front * (f-1.0);
|
||||
if (fabs(1.0 - cd) < STOP) {
|
||||
result.content = front * (f - 1.0);
|
||||
result.empty = 0;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
if(EXIT_ON_ERROR){
|
||||
if (EXIT_ON_ERROR) {
|
||||
printf("More loops needed, did not converge, in function incbeta, in %s (%d)", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}else{
|
||||
} else {
|
||||
char error_msg[200];
|
||||
snprintf(error_msg, 200, "More loops needed, did not converge, in function incbeta, in %s (%d)", __FILE__, __LINE__);
|
||||
result.empty = 1;
|
||||
|
@ -285,20 +288,22 @@ struct box incbeta(float a, float b, float x) {
|
|||
}
|
||||
}
|
||||
|
||||
struct box cdf_beta(float x){
|
||||
if(x < 0){
|
||||
struct box result = { .empty = 0, .content = 0};
|
||||
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};
|
||||
} else if (x > 1) {
|
||||
struct box result = { .empty = 0, .content = 1 };
|
||||
return result;
|
||||
} else {
|
||||
float successes = 1, failures = (2023-1945);
|
||||
float successes = 1, failures = (2023 - 1945);
|
||||
return incbeta(successes, failures, x);
|
||||
}
|
||||
}
|
||||
|
||||
float cdf_dangerous_beta(float x){
|
||||
float cdf_dangerous_beta(float x)
|
||||
{
|
||||
// So the thing is, this works
|
||||
// But it will propagate through the code
|
||||
// So it doesn't feel like a great architectural choice;
|
||||
|
@ -313,18 +318,18 @@ float cdf_dangerous_beta(float x){
|
|||
// b. clever & hacky go-to statements
|
||||
// i. They actually look fun to implement
|
||||
// ii. But they would be hard for others to use.
|
||||
if(x < 0){
|
||||
if (x < 0) {
|
||||
return 0;
|
||||
} else if(x > 1){
|
||||
} else if (x > 1) {
|
||||
return 1;
|
||||
} else {
|
||||
float successes = 100, failures = 100;
|
||||
struct box result = incbeta(successes, failures, x);
|
||||
if(result.empty){
|
||||
if (result.empty) {
|
||||
printf("%s\n", result.error_msg);
|
||||
exit(1);
|
||||
return 1;
|
||||
}else{
|
||||
} else {
|
||||
return result.content;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user