2023-07-16 19:00:30 +00:00
|
|
|
#ifndef SQUIGGLEC
|
|
|
|
#define SQUIGGLEC
|
2023-06-26 17:44:41 +00:00
|
|
|
|
2023-07-23 10:47:47 +00:00
|
|
|
// uint64_t header
|
2023-07-16 19:00:30 +00:00
|
|
|
#include <stdint.h>
|
2023-06-26 17:44:41 +00:00
|
|
|
|
|
|
|
// Pseudo Random number generator
|
2023-07-23 10:47:47 +00:00
|
|
|
uint64_t xorshift64(uint64_t* seed);
|
2023-06-26 17:44:41 +00:00
|
|
|
|
2023-07-16 20:32:03 +00:00
|
|
|
// Basic distribution sampling functions
|
2023-07-23 11:02:56 +00:00
|
|
|
double sample_unit_uniform(uint64_t* seed);
|
|
|
|
double sample_unit_normal(uint64_t* seed);
|
2023-07-16 20:32:03 +00:00
|
|
|
|
|
|
|
// Composite distribution sampling functions
|
2023-07-23 11:02:56 +00:00
|
|
|
double sample_uniform(double start, double end, uint64_t* seed);
|
|
|
|
double sample_normal(double mean, double sigma, uint64_t* seed);
|
|
|
|
double sample_lognormal(double logmean, double logsigma, uint64_t* seed);
|
|
|
|
double sample_to(double low, double high, uint64_t* seed);
|
2023-06-26 17:44:41 +00:00
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
double sample_gamma(double alpha, uint64_t* seed);
|
|
|
|
double sample_beta(double a, double b, uint64_t* seed);
|
2023-08-11 12:01:02 +00:00
|
|
|
double sample_laplace(double successes, double failures, uint64_t* seed);
|
2023-07-23 08:09:34 +00:00
|
|
|
|
2023-06-26 17:44:41 +00:00
|
|
|
// Array helpers
|
2023-07-23 11:02:56 +00:00
|
|
|
double array_sum(double* array, int length);
|
|
|
|
void array_cumsum(double* array_to_sum, double* array_cumsummed, int length);
|
|
|
|
double array_mean(double* array, int length);
|
|
|
|
double array_std(double* array, int length);
|
2023-06-26 17:44:41 +00:00
|
|
|
|
|
|
|
// Mixture function
|
2023-07-23 11:02:56 +00:00
|
|
|
double sample_mixture(double (*samplers[])(uint64_t*), double* weights, int n_dists, uint64_t* seed);
|
2023-06-26 17:44:41 +00:00
|
|
|
|
2023-07-16 19:26:33 +00:00
|
|
|
// Box
|
|
|
|
struct box {
|
|
|
|
int empty;
|
2023-07-23 11:02:56 +00:00
|
|
|
double content;
|
2023-07-16 19:26:33 +00:00
|
|
|
char* error_msg;
|
|
|
|
};
|
|
|
|
|
2023-07-16 20:32:03 +00:00
|
|
|
// Macros to handle errors
|
|
|
|
#define MAX_ERROR_LENGTH 500
|
2023-07-16 20:58:20 +00:00
|
|
|
#define EXIT_ON_ERROR 0
|
|
|
|
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
|
|
|
|
struct box process_error(const char* error_msg, int should_exit, char* file, int line);
|
2023-07-16 20:32:03 +00:00
|
|
|
|
2023-07-16 19:26:33 +00:00
|
|
|
// Inverse cdf
|
2023-07-23 11:02:56 +00:00
|
|
|
struct box inverse_cdf_double(double cdf(double), double p);
|
|
|
|
struct box inverse_cdf_box(struct box cdf_box(double), double p);
|
2023-07-16 19:26:33 +00:00
|
|
|
|
|
|
|
// Samplers from cdf
|
2023-07-23 11:02:56 +00:00
|
|
|
struct box sampler_cdf_double(double cdf(double), uint64_t* seed);
|
|
|
|
struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed);
|
2023-07-16 19:26:33 +00:00
|
|
|
|
2023-07-23 17:11:25 +00:00
|
|
|
// Get 90% confidence interval
|
2023-09-23 22:08:51 +00:00
|
|
|
typedef struct ci_t {
|
2023-08-01 12:08:19 +00:00
|
|
|
float low;
|
|
|
|
float high;
|
2023-09-23 22:08:51 +00:00
|
|
|
} ci;
|
|
|
|
ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed);
|
2023-07-23 17:11:25 +00:00
|
|
|
|
2023-09-23 21:15:48 +00:00
|
|
|
// small algebra manipulations
|
2023-09-23 20:53:45 +00:00
|
|
|
|
|
|
|
typedef struct normal_params_t {
|
|
|
|
double mean;
|
|
|
|
double std;
|
|
|
|
} normal_params;
|
2023-09-23 21:15:48 +00:00
|
|
|
normal_params algebra_sum_normals(normal_params a, normal_params b);
|
2023-09-23 20:53:45 +00:00
|
|
|
|
|
|
|
typedef struct lognormal_params_t {
|
|
|
|
double logmean;
|
|
|
|
double logstd;
|
|
|
|
} lognormal_params;
|
|
|
|
lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params b);
|
|
|
|
|
2023-09-23 21:33:25 +00:00
|
|
|
|
2023-09-23 22:08:51 +00:00
|
|
|
lognormal_params convert_ci_to_lognormal_params(ci x);
|
2023-09-23 21:33:25 +00:00
|
|
|
|
|
|
|
|
2023-09-23 22:08:51 +00:00
|
|
|
ci convert_lognormal_params_to_ci(lognormal_params y);
|
2023-09-23 21:33:25 +00:00
|
|
|
|
2023-07-16 19:00:30 +00:00
|
|
|
#endif
|