forked from personal/squiggle.c
		
	refactor & recompile for function definitions
This commit is contained in:
		
							parent
							
								
									023c9f28ac
								
							
						
					
					
						commit
						fb110a35f3
					
				
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -15,7 +15,7 @@ int main()
 | 
			
		|||
    uint64_t* seed = malloc(sizeof(uint64_t));
 | 
			
		||||
    *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 = sampler_get_90_ci(beta_1_2_sampler, 1000000, 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);
 | 
			
		||||
 | 
			
		||||
    free(seed);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -60,7 +60,7 @@ int main()
 | 
			
		|||
    }
 | 
			
		||||
    printf("... ]\n");
 | 
			
		||||
 | 
			
		||||
    ci ci_90 = get_90_confidence_interval(mixture, seed);
 | 
			
		||||
    ci ci_90 = sampler_get_90_ci(mixture, 1000000, seed);
 | 
			
		||||
    printf("mean: %f\n", array_mean(mixture_result, n));
 | 
			
		||||
    printf("90%% confidence interval: [%f, %f]\n", ci_90.low, ci_90.high);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -41,7 +41,7 @@ int main()
 | 
			
		|||
    }
 | 
			
		||||
    printf("... ]\n");
 | 
			
		||||
 | 
			
		||||
    ci ci_90 = get_90_confidence_interval(sample_minutes_per_day_jumping_rope_needed_to_burn_10kg, seed);
 | 
			
		||||
    ci ci_90 = sampler_get_90_ci(sample_minutes_per_day_jumping_rope_needed_to_burn_10kg, 1000000, seed);
 | 
			
		||||
    printf("90%% confidence interval: [%f, %f]\n", ci_90.low, ci_90.high);
 | 
			
		||||
 | 
			
		||||
    free(seed);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -50,7 +50,7 @@ int main()
 | 
			
		|||
 | 
			
		||||
    // Before a first nuclear collapse
 | 
			
		||||
    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 = 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) * num_samples);
 | 
			
		||||
| 
						 | 
				
			
			@ -61,7 +61,7 @@ int main()
 | 
			
		|||
 | 
			
		||||
    // After the first nuclear collapse
 | 
			
		||||
    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 = 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) * num_samples);
 | 
			
		||||
| 
						 | 
				
			
			@ -72,7 +72,7 @@ int main()
 | 
			
		|||
 | 
			
		||||
    // After the first nuclear collapse (antiinductive)
 | 
			
		||||
    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 = 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) * num_samples);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -9,7 +9,7 @@ int main()
 | 
			
		|||
    // set randomness seed
 | 
			
		||||
    // uint64_t* seed = malloc(sizeof(uint64_t));
 | 
			
		||||
    // *seed = 1000; // xorshift can't start with 0
 | 
			
		||||
    // ^ not necessary, because parallel_sampler takes care of the seed.
 | 
			
		||||
    // ^ not necessary, because sampler_parallel takes care of the seed.
 | 
			
		||||
 | 
			
		||||
    int n_samples = 1000 * 1000 * 1000;
 | 
			
		||||
    int n_threads = 16;
 | 
			
		||||
| 
						 | 
				
			
			@ -18,12 +18,12 @@ int main()
 | 
			
		|||
    }
 | 
			
		||||
    double* results = malloc(n_samples * sizeof(double));
 | 
			
		||||
 | 
			
		||||
    parallel_sampler(sampler, results, n_threads, n_samples);
 | 
			
		||||
    sampler_parallel(sampler, results, n_threads, n_samples);
 | 
			
		||||
    double avg = array_sum(results, n_samples)/n_samples;
 | 
			
		||||
    printf("Average of 1B lognormal(0,10): %f", avg);
 | 
			
		||||
 | 
			
		||||
    free(results);
 | 
			
		||||
 | 
			
		||||
    // free(seed);
 | 
			
		||||
    // ^ not necessary, because parallel_sampler takes care of the seed.
 | 
			
		||||
    // ^ not necessary, because sampler_parallel takes care of the seed.
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -23,7 +23,7 @@ int main()
 | 
			
		|||
 | 
			
		||||
    int n_samples = 1000 * 1000, n_threads = 16;
 | 
			
		||||
    double* results = malloc(n_samples * sizeof(double));
 | 
			
		||||
    parallel_sampler(sampler_result, results, n_threads, n_samples);
 | 
			
		||||
    sampler_parallel(sampler_result, results, n_threads, n_samples);
 | 
			
		||||
    printf("Avg: %f\n", array_sum(results, n_samples)/n_samples);
 | 
			
		||||
    free(results);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -29,7 +29,7 @@ int main()
 | 
			
		|||
 | 
			
		||||
    int n_samples = 1000000, n_threads = 16;
 | 
			
		||||
    double* results = malloc(n_samples * sizeof(double));
 | 
			
		||||
    parallel_sampler(sampler_result, results, n_threads, n_samples);
 | 
			
		||||
    sampler_parallel(sampler_result, 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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -48,7 +48,7 @@ int main()
 | 
			
		|||
            return sample_min_of_n(seed, quotient);
 | 
			
		||||
        }
 | 
			
		||||
        double* results_quotient = malloc(quotient * sizeof(double));
 | 
			
		||||
        parallel_sampler(sample_min_of_quotient, results_quotient, n_threads, quotient);
 | 
			
		||||
        sampler_parallel(sample_min_of_quotient, results_quotient, n_threads, quotient);
 | 
			
		||||
 | 
			
		||||
        double min = results_quotient[0];
 | 
			
		||||
        for(int i=1; i<quotient; i++){
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -8,7 +8,7 @@
 | 
			
		|||
#include "squiggle.h"
 | 
			
		||||
 | 
			
		||||
/* Parallel sampler */
 | 
			
		||||
void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples){
 | 
			
		||||
void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples){
 | 
			
		||||
    if((n_samples % n_threads) != 0){
 | 
			
		||||
        fprintf(stderr, "Number of samples isn't divisible by number of threads, aborting\n");
 | 
			
		||||
        exit(1);
 | 
			
		||||
| 
						 | 
				
			
			@ -45,8 +45,8 @@ void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_
 | 
			
		|||
// to do: add n to function parameters and document
 | 
			
		||||
 | 
			
		||||
typedef struct ci_t {
 | 
			
		||||
    float low;
 | 
			
		||||
    float high;
 | 
			
		||||
    double low;
 | 
			
		||||
    double high;
 | 
			
		||||
} ci;
 | 
			
		||||
 | 
			
		||||
static void swp(int i, int j, double xs[])
 | 
			
		||||
| 
						 | 
				
			
			@ -75,16 +75,16 @@ static int partition(int low, int high, double xs[], int length)
 | 
			
		|||
    return gt;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static double quickselect(int k, double xs[], int length)
 | 
			
		||||
static double quickselect(int k, double xs[], int n)
 | 
			
		||||
{
 | 
			
		||||
    // https://en.wikipedia.org/wiki/Quickselect
 | 
			
		||||
    int low = 0;
 | 
			
		||||
    int high = length - 1;
 | 
			
		||||
    int high = n - 1;
 | 
			
		||||
    for (;;) {
 | 
			
		||||
        if (low == high) {
 | 
			
		||||
            return xs[low];
 | 
			
		||||
        }
 | 
			
		||||
        int pivot = partition(low, high, xs, length);
 | 
			
		||||
        int pivot = partition(low, high, xs, n);
 | 
			
		||||
        if (pivot == k) {
 | 
			
		||||
            return xs[pivot];
 | 
			
		||||
        } else if (k < pivot) {
 | 
			
		||||
| 
						 | 
				
			
			@ -95,27 +95,34 @@ static double quickselect(int k, double xs[], int length)
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ci sampler_get_ci(double (*sampler)(uint64_t*), ci interval, int n, uint64_t* seed){
 | 
			
		||||
    double* xs = malloc(n * sizeof(double));
 | 
			
		||||
    for (int i = 0; i < n; i++) {
 | 
			
		||||
        xs[i] = sampler(seed);
 | 
			
		||||
    }
 | 
			
		||||
ci array_get_ci(ci interval, double* xs, int n){
 | 
			
		||||
 | 
			
		||||
    int low_k = floor(interval.low * n);
 | 
			
		||||
    int high_k = ceil(interval.high * n);
 | 
			
		||||
 | 
			
		||||
    ci result = {
 | 
			
		||||
        .low = quickselect(low_k, xs, n),
 | 
			
		||||
        .high = quickselect(high_k, xs, n),
 | 
			
		||||
    };
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
ci array_get_90_ci(double xs[], int n)
 | 
			
		||||
{
 | 
			
		||||
    return array_get_ci((ci) {.low = 0.05, .high = 0.95}, xs, n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed){
 | 
			
		||||
    double* xs = malloc(n * sizeof(double));
 | 
			
		||||
    for (int i = 0; i < n; i++) {
 | 
			
		||||
        xs[i] = sampler(seed);
 | 
			
		||||
    }
 | 
			
		||||
    ci result = array_get_ci(interval, xs, n);
 | 
			
		||||
    free(xs);
 | 
			
		||||
    return result;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed)
 | 
			
		||||
ci sampler_get_90_ci(double (*sampler)(uint64_t*), int n, uint64_t* seed)
 | 
			
		||||
{
 | 
			
		||||
    return sampler_get_ci(sampler, (ci) {.low = 0.05, .high = 0.95}, 1000000, seed);
 | 
			
		||||
    return sampler_get_ci((ci) {.low = 0.05, .high = 0.95}, sampler, n, seed);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Algebra manipulations */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,14 +2,14 @@
 | 
			
		|||
#define SQUIGGLE_C_EXTRA 
 | 
			
		||||
 | 
			
		||||
/* Parallel sampling */
 | 
			
		||||
void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples);
 | 
			
		||||
void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples);
 | 
			
		||||
 | 
			
		||||
/* Get 90% confidence interval */
 | 
			
		||||
typedef struct ci_t {
 | 
			
		||||
    float low;
 | 
			
		||||
    float high;
 | 
			
		||||
} ci;
 | 
			
		||||
ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed);
 | 
			
		||||
ci sampler_get_90_ci(double (*sampler)(uint64_t*), int n, uint64_t* seed);
 | 
			
		||||
 | 
			
		||||
/* Algebra manipulations */
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user