forked from personal/squiggle.c
run formatting pass
This commit is contained in:
parent
d9e2ca04c5
commit
4662539c60
|
@ -12,8 +12,16 @@ int main()
|
|||
double p_b = 0.5;
|
||||
double p_c = p_a * p_b;
|
||||
|
||||
double sample_0(uint64_t * seed) { UNUSED(seed); return 0; }
|
||||
double sample_1(uint64_t * seed) { UNUSED(seed); return 1; }
|
||||
double sample_0(uint64_t * seed)
|
||||
{
|
||||
UNUSED(seed);
|
||||
return 0;
|
||||
}
|
||||
double sample_1(uint64_t * seed)
|
||||
{
|
||||
UNUSED(seed);
|
||||
return 1;
|
||||
}
|
||||
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
|
||||
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }
|
||||
|
||||
|
@ -22,7 +30,7 @@ int main()
|
|||
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
|
||||
|
||||
int n_samples = 1000000;
|
||||
double* result_many = (double*)malloc((size_t) n_samples * sizeof(double));
|
||||
double* result_many = (double*)malloc((size_t)n_samples * sizeof(double));
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
|
||||
}
|
||||
|
|
|
@ -15,8 +15,16 @@ int main()
|
|||
int n_dists = 4;
|
||||
|
||||
// These are nested functions. They will not compile without gcc.
|
||||
double sample_0(uint64_t * seed) { UNUSED(seed); return 0; }
|
||||
double sample_1(uint64_t * seed) { UNUSED(seed); return 1; }
|
||||
double sample_0(uint64_t * seed)
|
||||
{
|
||||
UNUSED(seed);
|
||||
return 0;
|
||||
}
|
||||
double sample_1(uint64_t * seed)
|
||||
{
|
||||
UNUSED(seed);
|
||||
return 1;
|
||||
}
|
||||
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
|
||||
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }
|
||||
|
||||
|
@ -24,7 +32,7 @@ int main()
|
|||
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
|
||||
|
||||
int n_samples = 1000000;
|
||||
double* result_many = (double*)malloc((size_t) n_samples * sizeof(double));
|
||||
double* result_many = (double*)malloc((size_t)n_samples * sizeof(double));
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ int main()
|
|||
printf("\n");
|
||||
*/
|
||||
|
||||
double* gamma_1_array = malloc(sizeof(double) * (size_t) n);
|
||||
double* gamma_1_array = malloc(sizeof(double) * (size_t)n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
double gamma_1 = sample_gamma(1.0, seed);
|
||||
// printf("sample_gamma(1.0): %f\n", gamma_1);
|
||||
|
@ -29,7 +29,7 @@ int main()
|
|||
free(gamma_1_array);
|
||||
printf("\n");
|
||||
|
||||
double* beta_1_2_array = malloc(sizeof(double) * (size_t) n);
|
||||
double* beta_1_2_array = malloc(sizeof(double) * (size_t)n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
double beta_1_2 = sample_beta(1, 2.0, seed);
|
||||
// printf("sample_beta(1.0, 2.0): %f\n", beta_1_2);
|
||||
|
|
|
@ -49,7 +49,7 @@ int main()
|
|||
|
||||
int n = 1000 * 1000;
|
||||
|
||||
double* mixture_result = malloc(sizeof(double) * (size_t) n);
|
||||
double* mixture_result = malloc(sizeof(double) * (size_t)n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
mixture_result[i] = mixture(seed);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ int main()
|
|||
|
||||
int n = 1000 * 1000;
|
||||
|
||||
double* result = malloc(sizeof(double) * (size_t) n);
|
||||
double* result = malloc(sizeof(double) * (size_t)n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
result[i] = sample_minutes_per_day_jumping_rope_needed_to_burn_10kg(seed);
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ int main()
|
|||
ci ci_90_2023 = sampler_get_90_ci(yearly_probability_nuclear_collapse_2023, 1000000, seed);
|
||||
printf("90%% confidence interval: [%f, %f]\n", ci_90_2023.low, ci_90_2023.high);
|
||||
|
||||
double* yearly_probability_nuclear_collapse_2023_samples = malloc(sizeof(double) * (size_t) num_samples);
|
||||
double* yearly_probability_nuclear_collapse_2023_samples = malloc(sizeof(double) * (size_t)num_samples);
|
||||
for (int i = 0; i < num_samples; i++) {
|
||||
yearly_probability_nuclear_collapse_2023_samples[i] = yearly_probability_nuclear_collapse_2023(seed);
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ int main()
|
|||
ci ci_90_2070 = sampler_get_90_ci(yearly_probability_nuclear_collapse_after_recovery_example, 1000000, seed);
|
||||
printf("90%% confidence interval: [%f, %f]\n", ci_90_2070.low, ci_90_2070.high);
|
||||
|
||||
double* yearly_probability_nuclear_collapse_after_recovery_samples = malloc(sizeof(double) * (size_t) num_samples);
|
||||
double* yearly_probability_nuclear_collapse_after_recovery_samples = malloc(sizeof(double) * (size_t)num_samples);
|
||||
for (int i = 0; i < num_samples; i++) {
|
||||
yearly_probability_nuclear_collapse_after_recovery_samples[i] = yearly_probability_nuclear_collapse_after_recovery_example(seed);
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ int main()
|
|||
ci ci_90_antiinductive = sampler_get_90_ci(yearly_probability_nuclear_collapse_after_recovery_antiinductive, 1000000, seed);
|
||||
printf("90%% confidence interval: [%f, %f]\n", ci_90_antiinductive.low, ci_90_antiinductive.high);
|
||||
|
||||
double* yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples = malloc(sizeof(double) * (size_t) num_samples);
|
||||
double* yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples = malloc(sizeof(double) * (size_t)num_samples);
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ int main()
|
|||
{
|
||||
return sample_lognormal(0, 10, seed);
|
||||
}
|
||||
double* results = malloc((size_t) n_samples * sizeof(double));
|
||||
double* results = malloc((size_t)n_samples * sizeof(double));
|
||||
|
||||
sampler_parallel(sampler, results, n_threads, n_samples);
|
||||
double avg = array_sum(results, n_samples) / n_samples;
|
||||
|
|
|
@ -9,8 +9,16 @@ int main()
|
|||
double p_b = 0.5;
|
||||
double p_c = p_a * p_b;
|
||||
|
||||
double sample_0(uint64_t * seed) { UNUSED(seed); return 0; }
|
||||
double sample_1(uint64_t * seed) { UNUSED(seed); return 1; }
|
||||
double sample_0(uint64_t * seed)
|
||||
{
|
||||
UNUSED(seed);
|
||||
return 0;
|
||||
}
|
||||
double sample_1(uint64_t * seed)
|
||||
{
|
||||
UNUSED(seed);
|
||||
return 1;
|
||||
}
|
||||
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
|
||||
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }
|
||||
|
||||
|
@ -23,7 +31,7 @@ int main()
|
|||
}
|
||||
|
||||
int n_samples = 1000 * 1000, n_threads = 16;
|
||||
double* results = malloc((size_t) n_samples * sizeof(double));
|
||||
double* results = malloc((size_t)n_samples * sizeof(double));
|
||||
sampler_parallel(sampler_result, results, n_threads, n_samples);
|
||||
printf("Avg: %f\n", array_sum(results, n_samples) / n_samples);
|
||||
free(results);
|
||||
|
|
|
@ -30,7 +30,7 @@ int main()
|
|||
}
|
||||
|
||||
int n_samples = 1000000, n_threads = 16;
|
||||
double* results = malloc((size_t) n_samples * sizeof(double));
|
||||
double* results = malloc((size_t)n_samples * sizeof(double));
|
||||
sampler_parallel(sample_min_of_1000, results, n_threads, n_samples);
|
||||
printf("Mean of the distribution of (taking the min of 1000 samples of a normal(5,2)): %f\n", array_mean(results, n_samples));
|
||||
free(results);
|
||||
|
@ -51,7 +51,7 @@ int main()
|
|||
{
|
||||
return sample_min_of_n(seed, quotient);
|
||||
}
|
||||
double* results_quotient = malloc((size_t) quotient * sizeof(double));
|
||||
double* results_quotient = malloc((size_t)quotient * sizeof(double));
|
||||
sampler_parallel(sample_min_of_quotient, results_quotient, n_threads, quotient);
|
||||
|
||||
double min = results_quotient[0];
|
||||
|
|
|
@ -10,7 +10,7 @@ int main()
|
|||
*seed = 1000; // xorshift can't start with a seed of 0
|
||||
|
||||
int n = 1000000;
|
||||
double* xs = malloc(sizeof(double) * (size_t) n);
|
||||
double* xs = malloc(sizeof(double) * (size_t)n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
xs[i] = sample_to(10, 100, seed);
|
||||
}
|
||||
|
|
|
@ -204,7 +204,7 @@ double sample_mixture(double (*samplers[])(uint64_t*), double* weights, int n_di
|
|||
{
|
||||
// Sample from samples with frequency proportional to their weights.
|
||||
double sum_weights = array_sum(weights, n_dists);
|
||||
double* cumsummed_normalized_weights = (double*)malloc((size_t) n_dists * sizeof(double));
|
||||
double* cumsummed_normalized_weights = (double*)malloc((size_t)n_dists * sizeof(double));
|
||||
cumsummed_normalized_weights[0] = weights[0] / sum_weights;
|
||||
for (int i = 1; i < n_dists; i++) {
|
||||
cumsummed_normalized_weights[i] = cumsummed_normalized_weights[i - 1] + weights[i] / sum_weights;
|
||||
|
|
|
@ -28,7 +28,7 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_
|
|||
int quotient = n_samples / n_threads;
|
||||
int divisor_multiple = quotient * n_threads;
|
||||
|
||||
uint64_t** seeds = malloc((size_t) n_threads * sizeof(uint64_t*));
|
||||
uint64_t** seeds = malloc((size_t)n_threads * sizeof(uint64_t*));
|
||||
srand(1);
|
||||
for (int i = 0; i < n_threads; i++) {
|
||||
seeds[i] = malloc(sizeof(uint64_t));
|
||||
|
@ -83,12 +83,12 @@ static void swp(int i, int j, double xs[])
|
|||
|
||||
static int partition(int low, int high, double xs[], int length)
|
||||
{
|
||||
if(low > high || high >= length){
|
||||
if (low > high || high >= length) {
|
||||
printf("Invariant violated for function partition in %s (%d)", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}
|
||||
// Note: the scratchpad/ folder in commit 578bfa27 has printfs sprinkled throughout
|
||||
int pivot = low + (int) floor((high - low) / 2);
|
||||
int pivot = low + (int)floor((high - low) / 2);
|
||||
double pivot_value = xs[pivot];
|
||||
swp(pivot, high, xs);
|
||||
int gt = low; /* This pointer will iterate until finding an element which is greater than the pivot. Then it will move elements that are smaller before it--more specifically, it will move elements to its position and then increment. As a result all elements between gt and i will be greater than the pivot. */
|
||||
|
@ -125,8 +125,8 @@ static double quickselect(int k, double xs[], int n)
|
|||
ci array_get_ci(ci interval, double* xs, int n)
|
||||
{
|
||||
|
||||
int low_k = (int) floor(interval.low * n);
|
||||
int high_k = (int) ceil(interval.high * n);
|
||||
int low_k = (int)floor(interval.low * n);
|
||||
int high_k = (int)ceil(interval.high * n);
|
||||
ci result = {
|
||||
.low = quickselect(low_k, xs, n),
|
||||
.high = quickselect(high_k, xs, n),
|
||||
|
@ -141,7 +141,7 @@ ci array_get_90_ci(double xs[], int n)
|
|||
ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed)
|
||||
{
|
||||
UNUSED(seed); // don't want to use it right now, but want to preserve ability to do so (e.g., remove parallelism from internals). Also nicer for consistency.
|
||||
double* xs = malloc((size_t) n * sizeof(double));
|
||||
double* xs = malloc((size_t)n * sizeof(double));
|
||||
sampler_parallel(sampler, xs, 16, n);
|
||||
ci result = array_get_ci(interval, xs, n);
|
||||
free(xs);
|
||||
|
|
Loading…
Reference in New Issue
Block a user