2023-07-23 10:41:05 +00:00
|
|
|
#include "../squiggle.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
#define N 100
|
2023-07-23 10:41:05 +00:00
|
|
|
|
2023-07-23 10:47:47 +00:00
|
|
|
void test_unit_uniform(uint64_t* seed){
|
2023-07-23 11:02:56 +00:00
|
|
|
double* unit_uniform_array = malloc(sizeof(double) * N);
|
2023-07-23 10:41:05 +00:00
|
|
|
|
|
|
|
for(int i=0; i<N; i++){
|
|
|
|
unit_uniform_array[i] = sample_unit_uniform(seed);
|
|
|
|
}
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
double mean = array_mean(unit_uniform_array, N);
|
|
|
|
double expected_mean = 0.5;
|
|
|
|
double delta_mean = mean - expected_mean;
|
2023-07-23 10:41:05 +00:00
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
double std = array_std(unit_uniform_array, N);
|
|
|
|
double expected_std = sqrt(1.0/12.0);
|
|
|
|
double delta_std = std - expected_std;
|
2023-07-23 10:41:05 +00:00
|
|
|
|
|
|
|
printf("Mean of unit uniform: %f, vs expected mean: %f, delta: %f\n", mean, expected_mean, delta_mean);
|
|
|
|
printf("Std of unit uniform: %f, vs expected std: %f, delta: %f\n", std, expected_std, delta_std);
|
|
|
|
|
|
|
|
if(fabs(delta_mean) > 1.0/1000.0){
|
|
|
|
printf("[-] Mean test for unit uniform NOT passed.\n");
|
|
|
|
}else {
|
|
|
|
printf("[x] Mean test for unit uniform PASSED\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fabs(delta_std) > 1.0/1000.0){
|
|
|
|
printf("[-] Std test for unit uniform NOT passed.\n");
|
|
|
|
}else {
|
|
|
|
printf("[x] Std test for unit uniform PASSED\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
void test_uniform(double start, double end, uint64_t* seed){
|
|
|
|
double* uniform_array = malloc(sizeof(double) * N);
|
2023-07-23 10:41:05 +00:00
|
|
|
|
|
|
|
for(int i=0; i<N; i++){
|
|
|
|
uniform_array[i] = sample_uniform(start, end, seed);
|
|
|
|
}
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
double mean = array_mean(uniform_array, N);
|
|
|
|
double expected_mean = (start + end) / 2;
|
|
|
|
double delta_mean = mean - expected_mean;
|
2023-07-23 10:41:05 +00:00
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
double std = array_std(uniform_array, N);
|
|
|
|
double expected_std = sqrt(1.0/12.0) * fabs(end-start);
|
|
|
|
double delta_std = std - expected_std;
|
2023-07-23 10:41:05 +00:00
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
double width = fabs(end - start);
|
2023-07-23 10:41:05 +00:00
|
|
|
if(fabs(delta_mean) > width * 1.0/1000.0){
|
|
|
|
printf("[-] Mean test for [%.1f, %.1f] uniform NOT passed.\n", start, end);
|
|
|
|
printf("Mean of [%.1f, %.1f] uniform: %f, vs expected mean: %f, delta: %f\n", start, end, mean, expected_mean, mean - expected_mean);
|
|
|
|
}else {
|
|
|
|
printf("[x] Mean test for unit uniform PASSED\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fabs(delta_std) > width * 1.0/1000.0){
|
|
|
|
printf("[-] Std test for [%.1f, %.1f] uniform NOT passed.\n", start, end);
|
|
|
|
printf("Std of [%.1f, %.1f] uniform: %f, vs expected std: %f, delta: %f\n", start, end, std, expected_std, std - expected_std);
|
2023-07-23 11:02:56 +00:00
|
|
|
for(int i=0; i<N; i++){
|
|
|
|
|
|
|
|
printf("%.1f, ", uniform_array[i]);
|
|
|
|
}
|
2023-07-23 10:41:05 +00:00
|
|
|
}else {
|
|
|
|
printf("[x] Std test for unit uniform PASSED\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
// set randomness seed
|
2023-07-23 10:47:47 +00:00
|
|
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
2023-07-23 10:41:05 +00:00
|
|
|
*seed = 1000; // xorshift can't start with a seed of 0
|
|
|
|
|
|
|
|
test_unit_uniform(seed);
|
|
|
|
|
2023-07-23 11:02:56 +00:00
|
|
|
for(int i=0; i<1; i++){
|
|
|
|
double start = sample_uniform(-10, 10, seed);
|
|
|
|
double end = sample_uniform(-10, 10, seed);
|
2023-07-23 10:41:05 +00:00
|
|
|
if ( end > start){
|
|
|
|
test_uniform(start, end, seed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(seed);
|
|
|
|
}
|
|
|
|
|