example.c (1089B)
1 #include "../../squiggle.h" 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 // Estimate functions 7 double sample_0(uint64_t* seed) 8 { 9 return 0; 10 } 11 12 double sample_1(uint64_t* seed) 13 { 14 return 1; 15 } 16 17 double sample_few(uint64_t* seed) 18 { 19 return sample_to(1, 3, seed); 20 } 21 22 double sample_many(uint64_t* seed) 23 { 24 return sample_to(2, 10, seed); 25 } 26 27 int main() 28 { 29 // set randomness seed 30 uint64_t* seed = malloc(sizeof(uint64_t)); 31 *seed = 1000; // xorshift can't start with 0 32 33 double p_a = 0.8; 34 double p_b = 0.5; 35 double p_c = p_a * p_b; 36 37 int n_dists = 4; 38 double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 }; 39 double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many }; 40 41 int n_samples = 1000000; 42 double* result_many = (double*)malloc(n_samples * sizeof(double)); 43 for (int i = 0; i < n_samples; i++) { 44 result_many[i] = sample_mixture(samplers, weights, n_dists, seed); 45 printf("%f\n", result_many[i]); 46 } 47 // printf("Mean: %f\n", array_mean(result_many, n_samples)); 48 49 free(seed); 50 }