2023-07-16 19:37:43 +00:00
|
|
|
#include "../../squiggle.h"
|
2023-11-18 21:00:02 +00:00
|
|
|
#include "../../extra.h"
|
2023-07-16 19:32:17 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2023-07-16 21:33:46 +00:00
|
|
|
#define NUM_SAMPLES 1000000
|
2023-07-16 19:32:17 +00:00
|
|
|
|
|
|
|
// Example cdf
|
2023-07-23 11:02:56 +00:00
|
|
|
double cdf_uniform_0_1(double x)
|
2023-07-16 19:32:17 +00:00
|
|
|
{
|
|
|
|
if (x < 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (x > 1) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
double cdf_squared_0_1(double x)
|
2023-07-16 19:32:17 +00:00
|
|
|
{
|
|
|
|
if (x < 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (x > 1) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return x * x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
double cdf_normal_0_1(double x)
|
2023-07-16 19:32:17 +00:00
|
|
|
{
|
2023-07-23 11:02:56 +00:00
|
|
|
double mean = 0;
|
|
|
|
double std = 1;
|
2023-07-16 19:32:17 +00:00
|
|
|
return 0.5 * (1 + erf((x - mean) / (std * sqrt(2)))); // erf from math.h
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some testers
|
2023-07-23 11:02:56 +00:00
|
|
|
void test_inverse_cdf_double(char* cdf_name, double cdf_double(double))
|
2023-07-16 19:32:17 +00:00
|
|
|
{
|
2023-07-23 11:02:56 +00:00
|
|
|
struct box result = inverse_cdf_double(cdf_double, 0.5);
|
2023-07-16 19:32:17 +00:00
|
|
|
if (result.empty) {
|
|
|
|
printf("Inverse for %s not calculated\n", cdf_name);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("Inverse of %s at %f is: %f\n", cdf_name, 0.5, result.content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
void test_and_time_sampler_double(char* cdf_name, double cdf_double(double), uint64_t* seed)
|
2023-07-16 19:32:17 +00:00
|
|
|
{
|
|
|
|
printf("\nGetting some samples from %s:\n", cdf_name);
|
|
|
|
clock_t begin = clock();
|
|
|
|
for (int i = 0; i < NUM_SAMPLES; i++) {
|
2023-07-23 11:02:56 +00:00
|
|
|
struct box sample = sampler_cdf_double(cdf_double, seed);
|
2023-07-16 19:32:17 +00:00
|
|
|
if (sample.empty) {
|
|
|
|
printf("Error in sampler function for %s", cdf_name);
|
|
|
|
} else {
|
|
|
|
// printf("%f\n", sample.content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clock_t end = clock();
|
2023-07-23 11:02:56 +00:00
|
|
|
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
|
2023-07-16 19:32:17 +00:00
|
|
|
printf("Time spent: %f\n", time_spent);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2023-07-23 11:02:56 +00:00
|
|
|
// Test inverse cdf double
|
|
|
|
test_inverse_cdf_double("cdf_uniform_0_1", cdf_uniform_0_1);
|
|
|
|
test_inverse_cdf_double("cdf_squared_0_1", cdf_squared_0_1);
|
|
|
|
test_inverse_cdf_double("cdf_normal_0_1", cdf_normal_0_1);
|
2023-07-16 19:32:17 +00:00
|
|
|
|
|
|
|
// Testing samplers
|
|
|
|
// set randomness seed
|
2023-07-23 10:47:47 +00:00
|
|
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
2023-07-16 19:32:17 +00:00
|
|
|
*seed = 1000; // xorshift can't start with 0
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
// Test double sampler
|
|
|
|
test_and_time_sampler_double("cdf_uniform_0_1", cdf_uniform_0_1, seed);
|
|
|
|
test_and_time_sampler_double("cdf_squared_0_1", cdf_squared_0_1, seed);
|
|
|
|
test_and_time_sampler_double("cdf_normal_0_1", cdf_normal_0_1, seed);
|
2023-07-16 19:32:17 +00:00
|
|
|
|
|
|
|
// Get some normal samples using a previous approach
|
2023-07-22 17:21:20 +00:00
|
|
|
printf("\nGetting some samples from sample_unit_normal\n");
|
2023-07-16 19:32:17 +00:00
|
|
|
|
|
|
|
clock_t begin_2 = clock();
|
2023-07-23 17:11:25 +00:00
|
|
|
double* normal_samples = malloc(NUM_SAMPLES * sizeof(double));
|
2023-07-16 19:32:17 +00:00
|
|
|
for (int i = 0; i < NUM_SAMPLES; i++) {
|
2023-07-23 17:11:25 +00:00
|
|
|
normal_samples[i] = sample_unit_normal(seed);
|
2023-07-16 19:32:17 +00:00
|
|
|
// printf("%f\n", normal_sample);
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_t end_2 = clock();
|
2023-07-23 11:02:56 +00:00
|
|
|
double time_spent_2 = (double)(end_2 - begin_2) / CLOCKS_PER_SEC;
|
2023-07-16 19:32:17 +00:00
|
|
|
printf("Time spent: %f\n", time_spent_2);
|
|
|
|
|
|
|
|
free(seed);
|
|
|
|
return 0;
|
|
|
|
}
|