From e3adf9d9793aa1302548a63a1e7d84a25e1420a8 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Sat, 8 Jan 2022 12:46:58 +0530 Subject: Add C code for experiments. * experiments/cube.c, experiments/integral.c, experiments/spheroid.c, experiments/volume.c: New files. --- experiments/cube.c | 46 ++++++++++++++++++++ experiments/integral.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ experiments/spheroid.c | 57 +++++++++++++++++++++++++ experiments/volume.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 322 insertions(+) create mode 100644 experiments/cube.c create mode 100644 experiments/integral.c create mode 100644 experiments/spheroid.c create mode 100644 experiments/volume.c diff --git a/experiments/cube.c b/experiments/cube.c new file mode 100644 index 0000000..845688c --- /dev/null +++ b/experiments/cube.c @@ -0,0 +1,46 @@ +/* How does the cube fare with unbiased extent sampling? + */ + +#include +#include +#include "oracles.h" +#include "extent-sampling.h" + +#define DIMENSION_START 5 +#define DIMENSION_STEP 5 +#define DIMENSION_STOP 50 + +#define TRIALS 100 + +#define RTOL 0.1 +#define EDGE 1.0 + +double cube_oracle (const gsl_vector* x) { + return cube_extent_oracle(x, EDGE); +} + +int main () +{ + gsl_rng_env_setup(); + gsl_rng* r = gsl_rng_alloc(gsl_rng_default); + + FILE* fp = fopen("cube-window.dat", "w"); + printf("dimension\tsamples\n"); + fprintf(fp, "dimension\tsamples\n"); + for (int dim=DIMENSION_START; dim<=DIMENSION_STOP; dim+=DIMENSION_STEP) { + unsigned int total_samples=0; + for (int i=0; i +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "extent-sampling.h" +#include "utils.h" + +#define DIMENSION_START 5 +#define DIMENSION_STEP 5 +#define DIMENSION_STOP 100 + +#define RTOLS 3 +#define RTOL1 0.2 +#define RTOL2 0.1 +#define RTOL3 0.05 + +#define MAX_EXTENT 1.0 + +#define POLYNOMIAL_DEGREE 3 +double poly_coeffs[POLYNOMIAL_DEGREE + 1] = {-0.09375, 0.6875, -1.5, 1}; + +typedef double (*true_integral_t) (unsigned int); +gsl_rng* r; + +double uniform_oracle (const gsl_vector* x) { + return gsl_ran_flat(r, 0, MAX_EXTENT); +} + +double polynomial_integrand (double r, const gsl_vector* x) { + return gsl_poly_eval(poly_coeffs, POLYNOMIAL_DEGREE + 1, r); +} + +double polynomial_true_integral (unsigned int n) { + double integrand[POLYNOMIAL_DEGREE + 1]; + for (int k=0; k<=POLYNOMIAL_DEGREE; k++) + integrand[k] = poly_coeffs[k]/(n + k)/(n + k + 1); + return surface_area_of_ball(n) + / MAX_EXTENT + * gsl_pow_uint(MAX_EXTENT, n + 1) + * gsl_poly_eval(integrand, POLYNOMIAL_DEGREE+1, MAX_EXTENT); +} + +double gaussian_integrand (double r, const gsl_vector* x) { + return exp(-gsl_pow_2(r)/2); +} + +double gaussian_true_integral (unsigned int n) { + return pow(2, 0.5*n - 1) + * surface_area_of_ball(n) + / MAX_EXTENT + * (MAX_EXTENT*lower_incomplete_gamma(0.5*n, 0.5*gsl_pow_2(MAX_EXTENT)) + - sqrt(2) * lower_incomplete_gamma(0.5*(n + 1), 0.5*gsl_pow_2(MAX_EXTENT))); +} + +double x_coordinate_integrand (double r, const gsl_vector* x) { + return fabs(r*gsl_vector_get(x, 0)); +} + +double x_coordinate_true_integral (unsigned int n) { + return surface_area_of_ball(n+3) / 2 / (n+2) / gsl_pow_2(M_PI) + * gsl_pow_uint(MAX_EXTENT, n+1) + / MAX_EXTENT; +} + +void run_experiment (integrand_t integrand, extent_oracle_t oracle, true_integral_t true_integral, const char* filename_prefix) +{ + gsl_rstat_workspace* stats = gsl_rstat_alloc(); + printf("dimension\trtol=%g\trtol=%g\trtol=%g\n", RTOL1, RTOL2, RTOL3); + double rtol[RTOLS] = {RTOL1, RTOL2, RTOL3}; + FILE *fp[RTOLS]; + for (int i=0; i +#include +#include +#include "extent-sampling.h" +#include "oracles.h" + +#define DIMENSION_START 5 +#define DIMENSION_STEP 5 +#define DIMENSION_STOP 100 + +#define RTOL 0.1 +#define SAMPLES_PER_CONE 1000 +#define SOLID_ANGLE_FACTOR 1.1 + +void run_experiment +(double eccentricity, double solid_angle_threshold_exponent_factor, FILE* fp) +{ + gsl_rng_env_setup(); + gsl_rng* r = gsl_rng_alloc(gsl_rng_default); + + spheroid_params params = {eccentricity}; + extent_oracle_t oracle = {spheroid_extent_oracle, ¶ms}; + + fprintf(fp, "# spheroid eccentricity = %g\n", eccentricity); + fprintf(fp, "# samples per cone = %d\n", SAMPLES_PER_CONE); + fprintf(fp, "# solid angle threshold = 2^(-%gn)\n", solid_angle_threshold_exponent_factor); + fprintf(fp, "# solid angle factor = %g\n", SOLID_ANGLE_FACTOR); + fprintf(fp, "dimension\tvolume\tsamples\ttime\n"); + for (int dim=DIMENSION_START; dim<=DIMENSION_STOP; dim+=DIMENSION_STEP) { + gsl_vector* mean = gsl_vector_alloc(dim); + gsl_vector_set_basis(mean, 0); + + unsigned int samples; + clock_t begin = clock(); + double volume = volume_importance(&oracle, r, mean, SAMPLES_PER_CONE, + SOLID_ANGLE_FACTOR, solid_angle_threshold_exponent_factor, &samples); + clock_t end = clock(); + fprintf(fp, "%d\t%g\t%d\t%g\n", dim, 2*volume / spheroid_true_volume(dim, ¶ms), + samples, (double)(end-begin)/CLOCKS_PER_SEC); + fflush(NULL); + + gsl_vector_free(mean); + } + gsl_rng_free(r); +} + +int main () +{ + FILE* fp = fopen("spheroid-eccentricity10.dat", "w"); + run_experiment(10, 2, fp); + fclose(fp); + + fp = fopen("spheroid-eccentricity100.dat", "w"); + run_experiment(100, 6, fp); + fclose(fp); + return 0; +} diff --git a/experiments/volume.c b/experiments/volume.c new file mode 100644 index 0000000..3a20d43 --- /dev/null +++ b/experiments/volume.c @@ -0,0 +1,106 @@ +#include +#include + +#include + +#include "extent-sampling.h" +#include "oracles.h" +#include "utils.h" + +#define RTOLS 3 +#define RTOL1 0.2 +#define RTOL2 0.1 +#define RTOL3 0.05 + +typedef double (*true_volume_t) (unsigned int); +gsl_rng* r; + +#define MAX_EXTENT 1.0 +double uniform_oracle (const gsl_vector* x) { + return gsl_ran_flat(r, 0, MAX_EXTENT); +} + +double uniform_true_volume_dim (unsigned int dim) +{ + return uniform_true_volume(0, MAX_EXTENT, dim); +} +#undef MAX_EXTENT + +#define ALPHA 2 +#define BETA 2 +double beta_oracle (const gsl_vector* x) { + return beta_extent_generator(r, ALPHA, BETA); +} + +double beta_true_volume_dim (unsigned int dim) +{ + return beta_true_volume(ALPHA, BETA, dim); +} +#undef ALPHA +#undef BETA + +double arcsine_oracle (const gsl_vector* x) { + return beta_extent_generator(r, 0.5, 0.5); +} + +double arcsine_true_volume_dim (unsigned int dim) +{ + return beta_true_volume(0.5, 0.5, dim); +} + +#define EDGE 1.0 +double cube_oracle (const gsl_vector* x) { + return cube_extent_oracle(x, EDGE); +} + +double cube_true_volume_dim (unsigned int dim) +{ + return cube_true_volume(EDGE, dim); +} +#undef EDGE + +void run_experiment (extent_oracle_t oracle, true_volume_t true_volume, const char* filename_prefix, + int dim_start, int dim_step, int dim_stop) +{ + gsl_rstat_workspace* stats = gsl_rstat_alloc(); + printf("dimension\trtol=%g\trtol=%g\trtol=%g\n", RTOL1, RTOL2, RTOL3); + double rtol[RTOLS] = {RTOL1, RTOL2, RTOL3}; + FILE *fp[RTOLS]; + for (int i=0; i