Rejection Sampling
gyak2_rejection_sampling.c
Go to the documentation of this file.
00001 /*      gyak2_rejection_sampling.c
00002  *
00003  *      created by: racze
00004  *      date: 2011-09-25
00005  *
00006  *      This program is an example for rejection sampling.
00007  */
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <math.h>
00012 
00013 #include <gsl/gsl_rng.h>
00047 void Generate_Corrected_Pareto(double* pointer, size_t length, double alpha, double delta, double xmin, const gsl_rng *rng);
00048 
00052 int Hi2Lo(const void* a, const void* b);
00053 
00054 int main(void){
00055         size_t N = 10000;
00056         size_t i;
00057         double data[N];
00058 
00059         /*Initialize for the GSL random number generator.*/
00060         const gsl_rng_type * T;
00061         gsl_rng * r;
00062      
00063         gsl_rng_env_setup();
00064         /*Choose rng type.*/
00065         T = gsl_rng_default;
00066         /*Allocate memory to the rng.*/
00067         r = gsl_rng_alloc (T);
00068         /*Set the seed of the random number generator.*/
00069         gsl_rng_set(r, 123456);
00070 
00071         Generate_Corrected_Pareto(data, N, 2.0, 1.0, 10.0, r);
00072 
00073 /*      for(i = 0; i < N; ++i){
00074                 fprintf(stdout, "%g\n" , data[i]);
00075         }*/
00076         
00077         /*Free the memory allocated to the rng.*/
00078         gsl_rng_free(r);
00079 
00080         /*
00081  *      HOMEWORK: Check whether rejection sampling actually works:
00082  *      1) analytically
00083  *      and/or
00084  *      2) by constructing the empirical distribution function of a larger sample (say N=1000)
00085  *      and comparing it to the analytic function
00086  */
00087 
00088         qsort(data, N, sizeof(double), Hi2Lo);
00089 
00090         for(i = 0; i < N; ++i){
00091                 fprintf(stdout, "%g\t%g\n", data[i], ((double) i+1)/((double) N));
00092         }
00093 
00094 
00095         return 0;       
00096 }
00097 
00098 
00099 void Generate_Corrected_Pareto(double* data, size_t length, double alpha, double delta, double xmin, const gsl_rng *rng){
00100         size_t index = 0;
00101         short accepted;
00102         double x, u;
00103         while(index < length){
00104                 accepted = 0;
00105                 fprintf(stderr, "------------- index = %zu -------------", index);
00106                 while(!accepted){
00107                         /*1) Generate a random number distributed according to (x/xmin)^(-alpha)*/
00108                         u = gsl_rng_uniform(rng);
00109                         x = xmin*pow(u, -1/alpha);
00110                         /*2) Generate another random number to decide whether 
00111  *                         to accept or reject the previously generated value*/
00112                         u = gsl_rng_uniform(rng);
00113                         accepted = (u <  1.0 - pow(x/xmin, -delta));
00114                         /*accepted = 1;*/
00115                         fprintf(stderr, "accepted = %d\n", accepted);
00116                 }
00117                 data[index++] = x;
00118         }
00119 }
00120 
00121 int Hi2Lo(const void* a, const void* b){
00122         double vala = *(double*) a;
00123         double valb = *(double*) b;
00124 
00125         if(vala > valb){
00126                 return -1;
00127         }else if(vala < valb){
00128                 return 1;
00129         }else{
00130                 return 0;
00131         }
00132 }
00133 
 All Files Functions