fix: reorder headers to fix compilation error

This commit is contained in:
NunoSempere 2023-11-18 21:10:21 +00:00
parent 61851a321a
commit 6387c0df70
23 changed files with 132 additions and 122 deletions

View File

@ -1,7 +1,7 @@
#include "../../squiggle.h" #include "../../squiggle.h"
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
// Estimate functions // Estimate functions
double sample_0(uint64_t* seed) double sample_0(uint64_t* seed)
@ -24,10 +24,11 @@ double sample_many(uint64_t* seed)
return sample_to(2, 10, seed); return sample_to(2, 10, seed);
} }
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 0 *seed = 1000; // xorshift can't start with 0
double p_a = 0.8; double p_a = 0.8;
double p_b = 0.5; double p_b = 0.5;
@ -38,6 +39,6 @@ int main(){
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many }; double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
double result_one = sample_mixture(samplers, weights, n_dists, seed); double result_one = sample_mixture(samplers, weights, n_dists, seed);
printf("result_one: %f\n", result_one); printf("result_one: %f\n", result_one);
free(seed); free(seed);
} }

View File

@ -1,38 +1,38 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "../../squiggle.h" #include "../../squiggle.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
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 0 *seed = 1000; // xorshift can't start with 0
double p_a = 0.8; double p_a = 0.8;
double p_b = 0.5; double p_b = 0.5;
double p_c = p_a * p_b; double p_c = p_a * p_b;
int n_dists = 4; int n_dists = 4;
double sample_0(uint64_t* seed){ return 0; } double sample_0(uint64_t * seed) { return 0; }
double sample_1(uint64_t* seed) { return 1; } double sample_1(uint64_t * seed) { return 1; }
double sample_few(uint64_t* seed){ return sample_to(1, 3, seed); } double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
double sample_many(uint64_t* seed){ return sample_to(2, 10, seed); } double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many }; double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 }; double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
int n_samples = 1000000; int n_samples = 1000000;
double* result_many = (double *) malloc(n_samples * sizeof(double)); double* result_many = (double*)malloc(n_samples * sizeof(double));
for(int i=0; i<n_samples; i++){ for (int i = 0; i < n_samples; i++) {
result_many[i] = sample_mixture(samplers, weights, n_dists, seed); result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
} }
printf("result_many: ["); printf("result_many: [");
for(int i=0; i<100; i++){ for (int i = 0; i < 100; i++) {
printf("%.2f, ", result_many[i]); printf("%.2f, ", result_many[i]);
} }
printf("]\n"); printf("]\n");
free(seed); free(seed);
} }

View File

@ -88,7 +88,7 @@ int main()
printf("\nGetting some samples from sample_unit_normal\n"); printf("\nGetting some samples from sample_unit_normal\n");
clock_t begin_2 = clock(); clock_t begin_2 = clock();
double* normal_samples = malloc(NUM_SAMPLES * sizeof(double)); double* normal_samples = malloc(NUM_SAMPLES * sizeof(double));
for (int i = 0; i < NUM_SAMPLES; i++) { for (int i = 0; i < NUM_SAMPLES; i++) {
normal_samples[i] = sample_unit_normal(seed); normal_samples[i] = sample_unit_normal(seed);
// printf("%f\n", normal_sample); // printf("%f\n", normal_sample);

View File

@ -11,8 +11,8 @@ int main()
uint64_t* seed = malloc(sizeof(uint64_t)); uint64_t* seed = malloc(sizeof(uint64_t));
*seed = 1000; // xorshift can't start with 0 *seed = 1000; // xorshift can't start with 0
int n = 1000 * 1000; int n = 1000 * 1000;
/* /*
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
double gamma_0 = sample_gamma(0.0, seed); double gamma_0 = sample_gamma(0.0, seed);
// printf("sample_gamma(0.0): %f\n", gamma_0); // printf("sample_gamma(0.0): %f\n", gamma_0);
@ -20,26 +20,25 @@ int main()
printf("\n"); printf("\n");
*/ */
double* gamma_1_array = malloc(sizeof(double) * n); double* gamma_1_array = malloc(sizeof(double) * n);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
double gamma_1 = sample_gamma(1.0, seed); double gamma_1 = sample_gamma(1.0, seed);
// printf("sample_gamma(1.0): %f\n", gamma_1); // printf("sample_gamma(1.0): %f\n", gamma_1);
gamma_1_array[i] = gamma_1; gamma_1_array[i] = gamma_1;
} }
printf("gamma(1) summary statistics = mean: %f, std: %f\n", array_mean(gamma_1_array, n), array_std(gamma_1_array, n)); printf("gamma(1) summary statistics = mean: %f, std: %f\n", array_mean(gamma_1_array, n), array_std(gamma_1_array, n));
free(gamma_1_array); free(gamma_1_array);
printf("\n"); printf("\n");
double* beta_1_2_array = malloc(sizeof(double) * n); double* beta_1_2_array = malloc(sizeof(double) * n);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
double beta_1_2 = sample_beta(1, 2.0, seed); double beta_1_2 = sample_beta(1, 2.0, seed);
// printf("sample_beta(1.0, 2.0): %f\n", beta_1_2); // printf("sample_beta(1.0, 2.0): %f\n", beta_1_2);
beta_1_2_array[i] = beta_1_2; beta_1_2_array[i] = beta_1_2;
} }
printf("beta(1,2) summary statistics: mean: %f, std: %f\n", array_mean(beta_1_2_array, n), array_std(beta_1_2_array, n)); printf("beta(1,2) summary statistics: mean: %f, std: %f\n", array_mean(beta_1_2_array, n), array_std(beta_1_2_array, n));
free(beta_1_2_array); free(beta_1_2_array);
printf("\n"); printf("\n");
free(seed);
}
free(seed);
}

Binary file not shown.

View File

@ -5,8 +5,9 @@
#include <stdlib.h> #include <stdlib.h>
// Estimate functions // Estimate functions
double beta_1_2_sampler(uint64_t* seed){ double beta_1_2_sampler(uint64_t* seed)
return sample_beta(1, 2.0, seed); {
return sample_beta(1, 2.0, seed);
} }
int main() int main()
@ -15,8 +16,8 @@ int main()
uint64_t* seed = malloc(sizeof(uint64_t)); uint64_t* seed = malloc(sizeof(uint64_t));
*seed = 1000; // xorshift can't start with 0 *seed = 1000; // xorshift can't start with 0
ci beta_1_2_ci_90 = get_90_confidence_interval(beta_1_2_sampler, seed); ci beta_1_2_ci_90 = get_90_confidence_interval(beta_1_2_sampler, seed);
printf("90%% confidence interval of beta(1,2) is [%f, %f]\n", beta_1_2_ci_90.low, beta_1_2_ci_90.high); printf("90%% confidence interval of beta(1,2) is [%f, %f]\n", beta_1_2_ci_90.low, beta_1_2_ci_90.high);
free(seed); free(seed);
} }

Binary file not shown.

Binary file not shown.

View File

@ -45,7 +45,5 @@ int main()
ci ci_90 = get_90_confidence_interval(sample_minutes_per_day_jumping_rope_needed_to_burn_10kg, seed); ci ci_90 = get_90_confidence_interval(sample_minutes_per_day_jumping_rope_needed_to_burn_10kg, seed);
printf("90%% confidence interval: [%f, %f]\n", ci_90.low, ci_90.high); printf("90%% confidence interval: [%f, %f]\n", ci_90.low, ci_90.high);
free(seed); free(seed);
} }

Binary file not shown.

View File

@ -7,34 +7,38 @@
double yearly_probability_nuclear_collapse(double year, uint64_t* seed) double yearly_probability_nuclear_collapse(double year, uint64_t* seed)
{ {
double successes = 0; double successes = 0;
double failures = (year - 1960); double failures = (year - 1960);
return sample_laplace(successes, failures, seed); return sample_laplace(successes, failures, seed);
// ^ can change to (successes + 1)/(trials + 2) // ^ can change to (successes + 1)/(trials + 2)
// to get a probability, // to get a probability,
// rather than sampling from a distribution over probabilities. // rather than sampling from a distribution over probabilities.
} }
double yearly_probability_nuclear_collapse_2023(uint64_t* seed){ double yearly_probability_nuclear_collapse_2023(uint64_t* seed)
return yearly_probability_nuclear_collapse(2023, seed); {
return yearly_probability_nuclear_collapse(2023, seed);
} }
double yearly_probability_nuclear_collapse_after_recovery(double year, double rebuilding_period_length_years, uint64_t* seed){ double yearly_probability_nuclear_collapse_after_recovery(double year, double rebuilding_period_length_years, uint64_t* seed)
// assumption: nuclear {
double successes = 1.0; // assumption: nuclear
double failures = (year - rebuilding_period_length_years - 1960 - 1); double successes = 1.0;
return sample_laplace(successes, failures, seed); double failures = (year - rebuilding_period_length_years - 1960 - 1);
return sample_laplace(successes, failures, seed);
} }
double yearly_probability_nuclear_collapse_after_recovery_example(uint64_t* seed){ double yearly_probability_nuclear_collapse_after_recovery_example(uint64_t* seed)
double year = 2070; {
double rebuilding_period_length_years =30; double year = 2070;
// So, there was a nuclear collapse in 2040, double rebuilding_period_length_years = 30;
// then a recovery period of 30 years // So, there was a nuclear collapse in 2040,
// and it's now 2070 // then a recovery period of 30 years
return yearly_probability_nuclear_collapse_after_recovery(year, rebuilding_period_length_years, seed); // and it's now 2070
return yearly_probability_nuclear_collapse_after_recovery(year, rebuilding_period_length_years, seed);
} }
double yearly_probability_nuclear_collapse_after_recovery_antiinductive(uint64_t* seed){ double yearly_probability_nuclear_collapse_after_recovery_antiinductive(uint64_t* seed)
return yearly_probability_nuclear_collapse(2023, seed)/2; {
return yearly_probability_nuclear_collapse(2023, seed) / 2;
} }
int main() int main()
@ -45,8 +49,8 @@ int main()
int num_samples = 1000000; int num_samples = 1000000;
// Before a first nuclear collapse // Before a first nuclear collapse
printf("## Before the first nuclear collapse\n"); printf("## Before the first nuclear collapse\n");
ci ci_90_2023 = get_90_confidence_interval(yearly_probability_nuclear_collapse_2023, seed); ci ci_90_2023 = get_90_confidence_interval(yearly_probability_nuclear_collapse_2023, seed);
printf("90%% confidence interval: [%f, %f]\n", ci_90_2023.low, ci_90_2023.high); printf("90%% confidence interval: [%f, %f]\n", ci_90_2023.low, ci_90_2023.high);
@ -54,10 +58,10 @@ int main()
for (int i = 0; i < num_samples; i++) { for (int i = 0; i < num_samples; i++) {
yearly_probability_nuclear_collapse_2023_samples[i] = yearly_probability_nuclear_collapse_2023(seed); yearly_probability_nuclear_collapse_2023_samples[i] = yearly_probability_nuclear_collapse_2023(seed);
} }
printf("mean: %f\n", array_mean(yearly_probability_nuclear_collapse_2023_samples, num_samples)); printf("mean: %f\n", array_mean(yearly_probability_nuclear_collapse_2023_samples, num_samples));
// After the first nuclear collapse // After the first nuclear collapse
printf("\n## After the first nuclear collapse\n"); printf("\n## After the first nuclear collapse\n");
ci ci_90_2070 = get_90_confidence_interval(yearly_probability_nuclear_collapse_after_recovery_example, seed); ci ci_90_2070 = get_90_confidence_interval(yearly_probability_nuclear_collapse_after_recovery_example, seed);
printf("90%% confidence interval: [%f, %f]\n", ci_90_2070.low, ci_90_2070.high); printf("90%% confidence interval: [%f, %f]\n", ci_90_2070.low, ci_90_2070.high);
@ -65,10 +69,10 @@ int main()
for (int i = 0; i < num_samples; i++) { for (int i = 0; i < num_samples; i++) {
yearly_probability_nuclear_collapse_after_recovery_samples[i] = yearly_probability_nuclear_collapse_after_recovery_example(seed); yearly_probability_nuclear_collapse_after_recovery_samples[i] = yearly_probability_nuclear_collapse_after_recovery_example(seed);
} }
printf("mean: %f\n", array_mean(yearly_probability_nuclear_collapse_after_recovery_samples, num_samples)); printf("mean: %f\n", array_mean(yearly_probability_nuclear_collapse_after_recovery_samples, num_samples));
// After the first nuclear collapse (antiinductive) // After the first nuclear collapse (antiinductive)
printf("\n## After the first nuclear collapse (antiinductive)\n"); printf("\n## After the first nuclear collapse (antiinductive)\n");
ci ci_90_antiinductive = get_90_confidence_interval(yearly_probability_nuclear_collapse_after_recovery_antiinductive, seed); ci ci_90_antiinductive = get_90_confidence_interval(yearly_probability_nuclear_collapse_after_recovery_antiinductive, seed);
printf("90%% confidence interval: [%f, %f]\n", ci_90_antiinductive.low, ci_90_antiinductive.high); printf("90%% confidence interval: [%f, %f]\n", ci_90_antiinductive.low, ci_90_antiinductive.high);
@ -76,7 +80,7 @@ int main()
for (int i = 0; i < num_samples; i++) { for (int i = 0; i < num_samples; i++) {
yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples[i] = yearly_probability_nuclear_collapse_after_recovery_antiinductive(seed); yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples[i] = yearly_probability_nuclear_collapse_after_recovery_antiinductive(seed);
} }
printf("mean: %f\n", array_mean(yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples, num_samples)); printf("mean: %f\n", array_mean(yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples, num_samples));
free(seed); free(seed);
} }

Binary file not shown.

View File

@ -10,20 +10,20 @@ 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 0 *seed = 1000; // xorshift can't start with 0
// Convert to 90% confidence interval form and back // Convert to 90% confidence interval form and back
lognormal_params ln1 = { .logmean = 1.0, .logstd = 3.0 }; lognormal_params ln1 = { .logmean = 1.0, .logstd = 3.0 };
ci ln1_ci = convert_lognormal_params_to_ci(ln1); ci ln1_ci = convert_lognormal_params_to_ci(ln1);
printf("The 90%% confidence interval of Lognormal(%f, %f) is [%f, %f]\n", printf("The 90%% confidence interval of Lognormal(%f, %f) is [%f, %f]\n",
ln1.logmean, ln1.logstd, ln1.logmean, ln1.logstd,
ln1_ci.low, ln1_ci.high); ln1_ci.low, ln1_ci.high);
lognormal_params ln1_ci_paramas = convert_ci_to_lognormal_params(ln1_ci); lognormal_params ln1_ci_paramas = convert_ci_to_lognormal_params(ln1_ci);
printf("The lognormal which has 90%% confidence interval [%f, %f] is Lognormal(%f, %f)\n", printf("The lognormal which has 90%% confidence interval [%f, %f] is Lognormal(%f, %f)\n",
ln1_ci.low, ln1_ci.high, ln1_ci.low, ln1_ci.high,
ln1.logmean, ln1.logstd); ln1.logmean, ln1.logstd);
lognormal_params ln2 = convert_ci_to_lognormal_params((ci){.low = 1, .high = 10}); lognormal_params ln2 = convert_ci_to_lognormal_params((ci) { .low = 1, .high = 10 });
lognormal_params ln3 = convert_ci_to_lognormal_params((ci){.low = 5, .high = 50}); lognormal_params ln3 = convert_ci_to_lognormal_params((ci) { .low = 5, .high = 50 });
lognormal_params sln = algebra_product_lognormals(ln2, ln3); lognormal_params sln = algebra_product_lognormals(ln2, ln3);
ci sln_ci = convert_lognormal_params_to_ci(sln); ci sln_ci = convert_lognormal_params_to_ci(sln);

View File

@ -6,9 +6,9 @@
#include <stdlib.h> #include <stdlib.h>
#define ln lognormal_params #define ln lognormal_params
#define to(...) convert_ci_to_lognormal_params((ci) __VA_ARGS__) #define to(...) convert_ci_to_lognormal_params((ci)__VA_ARGS__)
#define from(...) convert_lognormal_params_to_ci((ln) __VA_ARGS__) #define from(...) convert_lognormal_params_to_ci((ln)__VA_ARGS__)
#define times(a,b) algebra_product_lognormals(a,b) #define times(a, b) algebra_product_lognormals(a, b)
int main() int main()
{ {
@ -16,8 +16,8 @@ int main()
uint64_t* seed = malloc(sizeof(uint64_t)); uint64_t* seed = malloc(sizeof(uint64_t));
*seed = 1000; // xorshift can't start with 0 *seed = 1000; // xorshift can't start with 0
ln a = to({.low = 1, .high = 10}); ln a = to({ .low = 1, .high = 10 });
ln b = to({.low = 5, .high = 500}); ln b = to({ .low = 5, .high = 500 });
ln c = times(a, b); ln c = times(a, b);
printf("Result: to(%f, %f)\n", from(c).low, from(c).high); printf("Result: to(%f, %f)\n", from(c).low, from(c).high);

View File

@ -4,19 +4,23 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
double sample_0(uint64_t* seed){ double sample_0(uint64_t* seed)
{
return 0; return 0;
} }
double sample_1(uint64_t* seed){ double sample_1(uint64_t* seed)
{
return 1; return 1;
} }
double sample_normal_mean_1_std_2(uint64_t* seed){ double sample_normal_mean_1_std_2(uint64_t* seed)
{
return sample_normal(1, 2, seed); return sample_normal(1, 2, seed);
} }
double sample_1_to_3(uint64_t* seed){ double sample_1_to_3(uint64_t* seed)
{
return sample_to(1, 3, seed); return sample_to(1, 3, seed);
} }
@ -28,11 +32,11 @@ int main()
int n_dists = 4; int n_dists = 4;
double weights[] = { 1, 2, 3, 4 }; double weights[] = { 1, 2, 3, 4 };
double (*samplers[])(uint64_t*) = { double (*samplers[])(uint64_t*) = {
sample_0, sample_0,
sample_1, sample_1,
sample_normal_mean_1_std_2, sample_normal_mean_1_std_2,
sample_1_to_3 sample_1_to_3
}; };
int n_samples = 10; int n_samples = 10;

View File

@ -0,0 +1,3 @@
build:
format:

View File

@ -1,17 +1,18 @@
#include "../../squiggle.h" #include "../../squiggle.h"
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
// Estimate functions // Estimate functions
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 0 *seed = 1000; // xorshift can't start with 0
for(int i=0; i<100; i++){ for (int i = 0; i < 100; i++) {
double sample = sample_lognormal(0,10, seed); double sample = sample_lognormal(0, 10, seed);
printf("%f\n", sample); printf("%f\n", sample);
} }
free(seed); free(seed);
} }

View File

@ -1,3 +1,4 @@
#include "squiggle.h"
#include <float.h> #include <float.h>
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
@ -6,7 +7,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
#include "squiggle.h"
// math constants // math constants
#define PI 3.14159265358979323846 // M_PI in gcc gnu99 #define PI 3.14159265358979323846 // M_PI in gcc gnu99
@ -58,7 +58,6 @@ ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed)
return result; return result;
} }
// ## Sample from an arbitrary cdf // ## Sample from an arbitrary cdf
struct box { struct box {
int empty; int empty;