//====================================================== file = sample.c =====
//=  A program to demonstrate the Central Limit Theorm (CLT)                 =
//============================================================================
//=  Notes:                                                                  =
//=   1) CLT says, "Let X1, X2, X3, ..., Xn be a sequence of n independent   =
//=      and identically distributed random variables each having finite     =
//=      values of expectation mu and variance sigma^2. The CLT states that  =
//=      as the sample size n increases, the distribution of the sample      =
//=      mean of these random variables approaches the normal distribution   =
//=      with a mean mu and variance sigma^2 / n irrespective of the shape   =
//=      of the original distribution."                                      =
//=--------------------------------------------------------------------------=
//=  Execution: sample                                                       =
//=--------------------------------------------------------------------------=
//=  Build: Standard CSIM build                                              =
//=--------------------------------------------------------------------------=
//=  History: KJC (07/12/11) - Genesis                                       =
//============================================================================
//----- Includes -------------------------------------------------------------
#include "csim.h"               // Needed for CSIM stuff
#include <stdio.h>              // Needed for printf()
#include <stdlib.h>             // Needed for exit()

//----- Constants ------------------------------------------------------------
#define MAX     1000000        // Number of samples
#define SAMPLE       10        // Sample size
#define DIST          2        // 1 = uniform and 2 = exponential (mean = 5.0)

//============================================================================
//==  Main program                                                          ==
//============================================================================
void sim(void)
{
  TABLE   tb1, tb2;     // CSIM tables
  double  z;            // Random variable
  double  *x1, *x2;     // Arrays for random values and sampled values
  double  sum;          // Temporary sum variable for sampling
  int     i, j, k;      // Counter indexes

  // Malloc space from the heap for x1 and x2 arrays
  x1 = (double *) malloc(sizeof(double) * MAX);
  x2 = (double *) malloc(sizeof(double) * MAX);
  if ((x1 == NULL) || (x2 == NULL))
  {
    printf("*** ERROR - malloc() failed! \n");
    exit(1);
  }

  // Initialize CSIM tables (set-up for a mean of 5.0)
  tb1 = table("Table of original values");
  table_histogram(tb1, 40, 0.0, 20.0);
  tb2 = table("Table of sampled values");
  table_histogram(tb2, 40, 0.0, 20.0);

  // Generate values (with mean = 5.0) and put them into x1
  for (i=0; i<MAX; i++)
  {
     if (DIST == 1)
       z = uniform(0.0, 10.0);
     else if (DIST == 2)
       z = exponential(5.0);
     x1[i] = z;
  }

  // Sample the values into x2
  k = 0;
  for (i=0; i<MAX; i=i+SAMPLE)
  {
    sum = 0.0;
    for (j=i; j<(i+SAMPLE); j++)
      sum = sum + x1[j];

    x2[k] = sum / SAMPLE;
    k++;
  }

  // Tablulate the values in x1
  for (i=0; i<MAX; i++)
    record(x1[i], tb1);

  // Tablulate the sampled values in x2
  for (i=0; i<k; i++)
    record(x2[i], tb2);

  // Output results
  printf("================================================================ \n");
  if (DIST == 1)
    printf("= Distribution     = Uniform(0.0, 5.0) \n");
  if (DIST == 2)
    printf("= Distribution     = Exponential(5.0) \n");
  printf("=--------------------------------------------------------------- \n");
  printf("= Number of values = %d \n", MAX);
  printf("= Sample size      = %d \n", SAMPLE);
  printf("================================================================ \n");
  report();
}

