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 <limits.h> // INT_MAX
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define VERBOSE 1
|
||||
|
||||
// Errors
|
||||
|
@ -22,7 +22,8 @@ struct box {
|
|||
};
|
||||
|
||||
// Example cdf
|
||||
float cdf_uniform_0_1(float x){
|
||||
float cdf_uniform_0_1(float x)
|
||||
{
|
||||
if (x < 0) {
|
||||
return 0;
|
||||
} 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) {
|
||||
return 0;
|
||||
} 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 std = 1;
|
||||
return 0.5 * (1 + erf((x - mean) / (std * sqrt(2))));
|
||||
}
|
||||
|
||||
// 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]
|
||||
// returns x such that cdf(x) = p
|
||||
// to do: add bounds, add error checking
|
||||
|
@ -113,7 +117,6 @@ struct box inverse_cdf(float cdf(float), float p){
|
|||
high = mid;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (convergence_condition) {
|
||||
|
@ -125,12 +128,10 @@ struct box inverse_cdf(float cdf(float), float p){
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Get random number between 0 and 1
|
||||
uint32_t xorshift32
|
||||
(uint32_t* seed)
|
||||
uint32_t xorshift32(uint32_t* seed)
|
||||
{
|
||||
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
|
||||
// See <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works>
|
||||
|
@ -146,12 +147,14 @@ uint32_t xorshift32
|
|||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
float p = rand_0_to_1(seed);
|
||||
result = inverse_cdf(cdf, p);
|
||||
|
@ -164,7 +167,8 @@ struct box sampler(float cdf(float), uint32_t* seed){
|
|||
// <https://github.com/codeplea/incbeta>
|
||||
|
||||
// main with an example
|
||||
int main(){
|
||||
int main()
|
||||
{
|
||||
|
||||
// Uniform:
|
||||
struct box result_1 = inverse_cdf(cdf_uniform_0_1, 0.5);
|
||||
|
|
Loading…
Reference in New Issue
Block a user