forked from personal/squiggle.c
formatting pass.
This commit is contained in:
parent
6b2349132b
commit
88e998edce
114
test/test.c
114
test/test.c
|
@ -1,11 +1,11 @@
|
||||||
#include "../squiggle.h"
|
#include "../squiggle.h"
|
||||||
#include <stdint.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define PERCENTAGE_TOLERANCE 1.0/1000.0
|
#define PERCENTAGE_TOLERANCE 1.0 / 1000.0
|
||||||
#define PERCENTAGE_TOLERANCE_LOGNORMAL 5.0/1000.0
|
#define PERCENTAGE_TOLERANCE_LOGNORMAL 5.0 / 1000.0
|
||||||
#define MAX_NAME_LENGTH 500
|
#define MAX_NAME_LENGTH 500
|
||||||
|
|
||||||
// Structs
|
// Structs
|
||||||
|
@ -19,40 +19,40 @@ struct array_expectations {
|
||||||
double tolerance;
|
double tolerance;
|
||||||
};
|
};
|
||||||
|
|
||||||
void test_array_expectations(struct array_expectations e){
|
void test_array_expectations(struct array_expectations e)
|
||||||
|
{
|
||||||
double mean = array_mean(e.array, e.n);
|
double mean = array_mean(e.array, e.n);
|
||||||
double delta_mean = mean - e.expected_mean;
|
double delta_mean = mean - e.expected_mean;
|
||||||
|
|
||||||
double std = array_std(e.array, e.n);
|
double std = array_std(e.array, e.n);
|
||||||
double delta_std = std - e.expected_std;
|
double delta_std = std - e.expected_std;
|
||||||
|
|
||||||
|
if (fabs(delta_mean) / fabs(mean) > e.tolerance) {
|
||||||
if(fabs(delta_mean)/fabs(mean) > e.tolerance){
|
|
||||||
printf("[-] Mean test for %s NOT passed.\n", e.name);
|
printf("[-] Mean test for %s NOT passed.\n", e.name);
|
||||||
printf("Mean of %s: %f, vs expected mean: %f\n", e.name, mean, e.expected_mean);
|
printf("Mean of %s: %f, vs expected mean: %f\n", e.name, mean, e.expected_mean);
|
||||||
printf("delta: %f, relative delta: %f\n", delta_mean, delta_mean/fabs(mean));
|
printf("delta: %f, relative delta: %f\n", delta_mean, delta_mean / fabs(mean));
|
||||||
}else {
|
} else {
|
||||||
printf("[x] Mean test for %s PASSED\n", e.name);
|
printf("[x] Mean test for %s PASSED\n", e.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fabs(delta_std)/fabs(std) > e.tolerance){
|
if (fabs(delta_std) / fabs(std) > e.tolerance) {
|
||||||
printf("[-] Std test for %s NOT passed.\n", e.name);
|
printf("[-] Std test for %s NOT passed.\n", e.name);
|
||||||
printf("Std of %s: %f, vs expected std: %f\n", e.name, std, e.expected_std);
|
printf("Std of %s: %f, vs expected std: %f\n", e.name, std, e.expected_std);
|
||||||
printf("delta: %f, relative delta: %f\n", delta_std, delta_std/fabs(std));
|
printf("delta: %f, relative delta: %f\n", delta_std, delta_std / fabs(std));
|
||||||
}else {
|
} else {
|
||||||
printf("[x] Std test for %s PASSED\n", e.name);
|
printf("[x] Std test for %s PASSED\n", e.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test unit uniform
|
// Test unit uniform
|
||||||
void test_unit_uniform(uint64_t* seed){
|
void test_unit_uniform(uint64_t* seed)
|
||||||
|
{
|
||||||
int n = 1000 * 1000;
|
int n = 1000 * 1000;
|
||||||
double* unit_uniform_array = malloc(sizeof(double) * n);
|
double* unit_uniform_array = malloc(sizeof(double) * n);
|
||||||
|
|
||||||
for(int i=0; i<n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
unit_uniform_array[i] = sample_unit_uniform(seed);
|
unit_uniform_array[i] = sample_unit_uniform(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ void test_unit_uniform(uint64_t* seed){
|
||||||
.n = n,
|
.n = n,
|
||||||
.name = "unit uniform",
|
.name = "unit uniform",
|
||||||
.expected_mean = 0.5,
|
.expected_mean = 0.5,
|
||||||
.expected_std = sqrt(1.0/12.0),
|
.expected_std = sqrt(1.0 / 12.0),
|
||||||
.tolerance = 1 * PERCENTAGE_TOLERANCE,
|
.tolerance = 1 * PERCENTAGE_TOLERANCE,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -70,11 +70,12 @@ void test_unit_uniform(uint64_t* seed){
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test uniforms
|
// Test uniforms
|
||||||
void test_uniform(double start, double end, uint64_t* seed){
|
void test_uniform(double start, double end, uint64_t* seed)
|
||||||
|
{
|
||||||
int n = 1000 * 1000;
|
int n = 1000 * 1000;
|
||||||
double* uniform_array = malloc(sizeof(double) * n);
|
double* uniform_array = malloc(sizeof(double) * n);
|
||||||
|
|
||||||
for(int i=0; i<n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
uniform_array[i] = sample_uniform(start, end, seed);
|
uniform_array[i] = sample_uniform(start, end, seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,9 +85,9 @@ void test_uniform(double start, double end, uint64_t* seed){
|
||||||
.array = uniform_array,
|
.array = uniform_array,
|
||||||
.n = n,
|
.n = n,
|
||||||
.name = name,
|
.name = name,
|
||||||
.expected_mean = (start + end)/2,
|
.expected_mean = (start + end) / 2,
|
||||||
.expected_std = sqrt(1.0/12.0) * fabs(end-start),
|
.expected_std = sqrt(1.0 / 12.0) * fabs(end - start),
|
||||||
.tolerance = fabs(end -start) * PERCENTAGE_TOLERANCE,
|
.tolerance = fabs(end - start) * PERCENTAGE_TOLERANCE,
|
||||||
};
|
};
|
||||||
|
|
||||||
test_array_expectations(expectations);
|
test_array_expectations(expectations);
|
||||||
|
@ -95,11 +96,12 @@ void test_uniform(double start, double end, uint64_t* seed){
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test unit normal
|
// Test unit normal
|
||||||
void test_unit_normal(uint64_t* seed){
|
void test_unit_normal(uint64_t* seed)
|
||||||
|
{
|
||||||
int n = 1000 * 1000;
|
int n = 1000 * 1000;
|
||||||
double* unit_normal_array = malloc(sizeof(double) * n);
|
double* unit_normal_array = malloc(sizeof(double) * n);
|
||||||
|
|
||||||
for(int i=0; i<n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
unit_normal_array[i] = sample_unit_normal(seed);
|
unit_normal_array[i] = sample_unit_normal(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,11 +119,12 @@ void test_unit_normal(uint64_t* seed){
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test normal
|
// Test normal
|
||||||
void test_normal(double mean, double std, uint64_t* seed){
|
void test_normal(double mean, double std, uint64_t* seed)
|
||||||
|
{
|
||||||
int n = 10 * 1000 * 1000;
|
int n = 10 * 1000 * 1000;
|
||||||
double* normal_array = malloc(sizeof(double) * n);
|
double* normal_array = malloc(sizeof(double) * n);
|
||||||
|
|
||||||
for(int i=0; i<n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
normal_array[i] = sample_normal(mean, std, seed);
|
normal_array[i] = sample_normal(mean, std, seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,11 +145,12 @@ void test_normal(double mean, double std, uint64_t* seed){
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test lognormal
|
// Test lognormal
|
||||||
void test_lognormal(double logmean, double logstd, uint64_t* seed){
|
void test_lognormal(double logmean, double logstd, uint64_t* seed)
|
||||||
|
{
|
||||||
int n = 10 * 1000 * 1000;
|
int n = 10 * 1000 * 1000;
|
||||||
double* lognormal_array = malloc(sizeof(double) * n);
|
double* lognormal_array = malloc(sizeof(double) * n);
|
||||||
|
|
||||||
for(int i=0; i<n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
lognormal_array[i] = sample_lognormal(logmean, logstd, seed);
|
lognormal_array[i] = sample_lognormal(logmean, logstd, seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,8 +160,8 @@ void test_lognormal(double logmean, double logstd, uint64_t* seed){
|
||||||
.array = lognormal_array,
|
.array = lognormal_array,
|
||||||
.n = n,
|
.n = n,
|
||||||
.name = name,
|
.name = name,
|
||||||
.expected_mean = exp(logmean + pow(logstd, 2)/2),
|
.expected_mean = exp(logmean + pow(logstd, 2) / 2),
|
||||||
.expected_std = sqrt((exp(pow(logstd, 2)) - 1) * exp(2*logmean + pow(logstd, 2))),
|
.expected_std = sqrt((exp(pow(logstd, 2)) - 1) * exp(2 * logmean + pow(logstd, 2))),
|
||||||
.tolerance = exp(logstd) * PERCENTAGE_TOLERANCE_LOGNORMAL,
|
.tolerance = exp(logstd) * PERCENTAGE_TOLERANCE_LOGNORMAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -168,11 +172,12 @@ void test_lognormal(double logmean, double logstd, uint64_t* seed){
|
||||||
|
|
||||||
// Test beta
|
// Test beta
|
||||||
|
|
||||||
void test_beta(double a, double b, uint64_t* seed){
|
void test_beta(double a, double b, uint64_t* seed)
|
||||||
|
{
|
||||||
int n = 10 * 1000 * 1000;
|
int n = 10 * 1000 * 1000;
|
||||||
double* beta_array = malloc(sizeof(double) * n);
|
double* beta_array = malloc(sizeof(double) * n);
|
||||||
|
|
||||||
for(int i=0; i<n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
beta_array[i] = sample_beta(a, b, seed);
|
beta_array[i] = sample_beta(a, b, seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,8 +187,8 @@ void test_beta(double a, double b, uint64_t* seed){
|
||||||
.array = beta_array,
|
.array = beta_array,
|
||||||
.n = n,
|
.n = n,
|
||||||
.name = name,
|
.name = name,
|
||||||
.expected_mean = a/(a+b),
|
.expected_mean = a / (a + b),
|
||||||
.expected_std = sqrt((a*b)/(pow(a+b, 2) * (a + b + 1))),
|
.expected_std = sqrt((a * b) / (pow(a + b, 2) * (a + b + 1))),
|
||||||
.tolerance = PERCENTAGE_TOLERANCE,
|
.tolerance = PERCENTAGE_TOLERANCE,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -191,28 +196,29 @@ void test_beta(double a, double b, uint64_t* seed){
|
||||||
free(name);
|
free(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(){
|
int main()
|
||||||
|
{
|
||||||
// set randomness seed
|
// set randomness seed
|
||||||
uint64_t* seed = malloc(sizeof(uint64_t));
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
||||||
*seed = 1000; // xorshift can't start with a seed of 0
|
*seed = 1000; // xorshift can't start with a seed of 0
|
||||||
/*
|
|
||||||
printf("Testing unit uniform\n");
|
printf("Testing unit uniform\n");
|
||||||
test_unit_uniform(seed);
|
test_unit_uniform(seed);
|
||||||
|
|
||||||
printf("Testing small uniforms\n");
|
printf("Testing small uniforms\n");
|
||||||
for(int i=0; i<100; i++){
|
for (int i = 0; i < 100; i++) {
|
||||||
double start = sample_uniform(-10, 10, seed);
|
double start = sample_uniform(-10, 10, seed);
|
||||||
double end = sample_uniform(-10, 10, seed);
|
double end = sample_uniform(-10, 10, seed);
|
||||||
if ( end > start){
|
if (end > start) {
|
||||||
test_uniform(start, end, seed);
|
test_uniform(start, end, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Testing wide uniforms\n");
|
printf("Testing wide uniforms\n");
|
||||||
for(int i=0; i<100; i++){
|
for (int i = 0; i < 100; i++) {
|
||||||
double start = sample_uniform(-1000 * 1000, 1000 * 1000, seed);
|
double start = sample_uniform(-1000 * 1000, 1000 * 1000, seed);
|
||||||
double end = sample_uniform(-1000 * 1000, 1000 * 1000, seed);
|
double end = sample_uniform(-1000 * 1000, 1000 * 1000, seed);
|
||||||
if ( end > start){
|
if (end > start) {
|
||||||
test_uniform(start, end, seed);
|
test_uniform(start, end, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,60 +227,58 @@ int main(){
|
||||||
test_unit_normal(seed);
|
test_unit_normal(seed);
|
||||||
|
|
||||||
printf("Testing small normals\n");
|
printf("Testing small normals\n");
|
||||||
for(int i=0; i<100; i++){
|
for (int i = 0; i < 100; i++) {
|
||||||
double mean = sample_uniform(-10, 10, seed);
|
double mean = sample_uniform(-10, 10, seed);
|
||||||
double std = sample_uniform(0, 10, seed);
|
double std = sample_uniform(0, 10, seed);
|
||||||
if ( std > 0){
|
if (std > 0) {
|
||||||
test_normal(mean, std, seed);
|
test_normal(mean, std, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Testing larger normals\n");
|
printf("Testing larger normals\n");
|
||||||
for(int i=0; i<100; i++){
|
for (int i = 0; i < 100; i++) {
|
||||||
double mean = sample_uniform(-1000 * 1000, 1000 * 1000, seed);
|
double mean = sample_uniform(-1000 * 1000, 1000 * 1000, seed);
|
||||||
double std = sample_uniform(0, 1000 * 1000, seed);
|
double std = sample_uniform(0, 1000 * 1000, seed);
|
||||||
if ( std > 0){
|
if (std > 0) {
|
||||||
test_normal(mean, std, seed);
|
test_normal(mean, std, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
printf("Testing very small lognormals\n");
|
printf("Testing very small lognormals\n");
|
||||||
for(int i=0; i<10; i++){
|
for (int i = 0; i < 10; i++) {
|
||||||
double mean = sample_uniform(-1, 1, seed);
|
double mean = sample_uniform(-1, 1, seed);
|
||||||
double std = sample_uniform(0, 1, seed);
|
double std = sample_uniform(0, 1, seed);
|
||||||
if ( std > 0){
|
if (std > 0) {
|
||||||
test_lognormal(mean, std, seed);
|
test_lognormal(mean, std, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Testing small lognormals\n");
|
printf("Testing small lognormals\n");
|
||||||
for(int i=0; i<10; i++){
|
for (int i = 0; i < 10; i++) {
|
||||||
double mean = sample_uniform(-1, 5, seed);
|
double mean = sample_uniform(-1, 5, seed);
|
||||||
double std = sample_uniform(0, 5, seed);
|
double std = sample_uniform(0, 5, seed);
|
||||||
if ( std > 0){
|
if (std > 0) {
|
||||||
test_lognormal(mean, std, seed);
|
test_lognormal(mean, std, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
printf("Testing beta distribution\n");
|
printf("Testing beta distribution\n");
|
||||||
for(int i=0; i<100; i++){
|
for (int i = 0; i < 100; i++) {
|
||||||
double a = sample_uniform(0, 1000, seed);
|
double a = sample_uniform(0, 1000, seed);
|
||||||
double b = sample_uniform(0, 1000, seed);
|
double b = sample_uniform(0, 1000, seed);
|
||||||
if ( (a > 0) && ( b >0)){
|
if ((a > 0) && (b > 0)) {
|
||||||
test_beta(a, b, seed);
|
test_beta(a, b, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Testing larger beta distributions\n");
|
printf("Testing larger beta distributions\n");
|
||||||
for(int i=0; i<100; i++){
|
for (int i = 0; i < 100; i++) {
|
||||||
double a = sample_uniform(0, 1000 * 1000, seed);
|
double a = sample_uniform(0, 1000 * 1000, seed);
|
||||||
double b = sample_uniform(0, 1000 * 1000, seed);
|
double b = sample_uniform(0, 1000 * 1000, seed);
|
||||||
if ( (a > 0) && ( b >0)){
|
if ((a > 0) && (b > 0)) {
|
||||||
test_beta(a, b, seed);
|
test_beta(a, b, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(seed);
|
free(seed);
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user