forked from personal/squiggle.c
formatting pass
This commit is contained in:
parent
4deb2044d6
commit
5ef5c6847a
|
@ -1,9 +1,9 @@
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <float.h> // FLT_MAX, FLT_MIN
|
#include <float.h> // FLT_MAX, FLT_MIN
|
||||||
#include <limits.h> // INT_MAX
|
#include <limits.h> // INT_MAX
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#define VERBOSE 1
|
#define VERBOSE 1
|
||||||
|
|
||||||
// Errors
|
// Errors
|
||||||
|
@ -22,7 +22,8 @@ struct box {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Example cdf
|
// Example cdf
|
||||||
float cdf_uniform_0_1(float x){
|
float cdf_uniform_0_1(float x)
|
||||||
|
{
|
||||||
if (x < 0) {
|
if (x < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (x > 1) {
|
} else if (x > 1) {
|
||||||
|
@ -32,7 +33,8 @@ float cdf_uniform_0_1(float x){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float cdf_squared_0_1(float x){
|
float cdf_squared_0_1(float x)
|
||||||
|
{
|
||||||
if (x < 0) {
|
if (x < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (x > 1) {
|
} else if (x > 1) {
|
||||||
|
@ -42,14 +44,16 @@ float cdf_squared_0_1(float x){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float cdf_normal_0_1(float x){
|
float cdf_normal_0_1(float x)
|
||||||
|
{
|
||||||
float mean = 0;
|
float mean = 0;
|
||||||
float std = 1;
|
float std = 1;
|
||||||
return 0.5 * (1 + erf((x - mean) / (std * sqrt(2))));
|
return 0.5 * (1 + erf((x - mean) / (std * sqrt(2))));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inverse cdf
|
// Inverse cdf
|
||||||
struct box inverse_cdf(float cdf(float), float p){
|
struct box inverse_cdf(float cdf(float), float p)
|
||||||
|
{
|
||||||
// given a cdf: [-Inf, Inf] => [0,1]
|
// given a cdf: [-Inf, Inf] => [0,1]
|
||||||
// returns x such that cdf(x) = p
|
// returns x such that cdf(x) = p
|
||||||
// to do: add bounds, add error checking
|
// to do: add bounds, add error checking
|
||||||
|
@ -113,7 +117,6 @@ struct box inverse_cdf(float cdf(float), float p){
|
||||||
high = mid;
|
high = mid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (convergence_condition) {
|
if (convergence_condition) {
|
||||||
|
@ -125,12 +128,10 @@ struct box inverse_cdf(float cdf(float), float p){
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get random number between 0 and 1
|
// Get random number between 0 and 1
|
||||||
uint32_t xorshift32
|
uint32_t xorshift32(uint32_t* seed)
|
||||||
(uint32_t* seed)
|
|
||||||
{
|
{
|
||||||
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
|
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
|
||||||
// See <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works>
|
// See <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works>
|
||||||
|
@ -146,12 +147,14 @@ uint32_t xorshift32
|
||||||
|
|
||||||
// Distribution & sampling functions
|
// Distribution & sampling functions
|
||||||
|
|
||||||
float rand_0_to_1(uint32_t* seed){
|
float rand_0_to_1(uint32_t* seed)
|
||||||
|
{
|
||||||
return ((float)xorshift32(seed)) / ((float)UINT32_MAX);
|
return ((float)xorshift32(seed)) / ((float)UINT32_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
// sampler based on inverse cdf
|
// sampler based on inverse cdf
|
||||||
struct box sampler(float cdf(float), uint32_t* seed){
|
struct box sampler(float cdf(float), uint32_t* seed)
|
||||||
|
{
|
||||||
struct box result;
|
struct box result;
|
||||||
float p = rand_0_to_1(seed);
|
float p = rand_0_to_1(seed);
|
||||||
result = inverse_cdf(cdf, p);
|
result = inverse_cdf(cdf, p);
|
||||||
|
@ -164,7 +167,8 @@ struct box sampler(float cdf(float), uint32_t* seed){
|
||||||
// <https://github.com/codeplea/incbeta>
|
// <https://github.com/codeplea/incbeta>
|
||||||
|
|
||||||
// main with an example
|
// main with an example
|
||||||
int main(){
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
// Uniform:
|
// Uniform:
|
||||||
struct box result_1 = inverse_cdf(cdf_uniform_0_1, 0.5);
|
struct box result_1 = inverse_cdf(cdf_uniform_0_1, 0.5);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user