Blame histogram/test2d_resample.c

Packit 67cb25
/* histogram/test2d_resample.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <math.h>
Packit 67cb25
#include <gsl/gsl_histogram2d.h>
Packit 67cb25
#include <gsl/gsl_test.h>
Packit 67cb25
#include <gsl/gsl_ieee_utils.h>
Packit 67cb25
Packit 67cb25
#include "urand.c"
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
test2d_resample (void)
Packit 67cb25
{
Packit 67cb25
  size_t i, j;
Packit 67cb25
  int status = 0;
Packit 67cb25
  double total = 0;
Packit 67cb25
  size_t N = 200000;
Packit 67cb25
Packit 67cb25
  gsl_histogram2d *h;
Packit 67cb25
Packit 67cb25
  gsl_ieee_env_setup ();
Packit 67cb25
Packit 67cb25
  h = gsl_histogram2d_calloc_uniform (10, 10, 0.0, 1.0, 0.0, 1.0);
Packit 67cb25
Packit 67cb25
  for (i = 0; i < 10; i++)
Packit 67cb25
    {
Packit 67cb25
      for (j = 0; j < 10; j++)
Packit 67cb25
        {
Packit 67cb25
          double w = 10.0 * i + j;
Packit 67cb25
          total += w;
Packit 67cb25
          gsl_histogram2d_accumulate (h, 0.1 * i, 0.1 * i, w);
Packit 67cb25
        }
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  {
Packit 67cb25
    gsl_histogram2d_pdf *p = gsl_histogram2d_pdf_alloc (10,10);
Packit 67cb25
Packit 67cb25
    gsl_histogram2d *hh = gsl_histogram2d_calloc_uniform (20, 20,
Packit 67cb25
                                                          0.0, 1.0,
Packit 67cb25
                                                          0.0, 1.0);
Packit 67cb25
Packit 67cb25
    gsl_histogram2d_pdf_init (p, h);
Packit 67cb25
Packit 67cb25
    for (i = 0; i < N; i++)
Packit 67cb25
      {
Packit 67cb25
        double u = urand();
Packit 67cb25
        double v = urand();
Packit 67cb25
        double x, y;
Packit 67cb25
        status = gsl_histogram2d_pdf_sample (p, u, v, &x, &y);
Packit 67cb25
        status = gsl_histogram2d_increment (hh, x, y);
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
    status = 0;
Packit 67cb25
    for (i = 0; i < 20; i++)
Packit 67cb25
      {
Packit 67cb25
        for (j = 0; j < 20; j++)
Packit 67cb25
          {
Packit 67cb25
            double z = 4 * total * gsl_histogram2d_get (hh, i, j) / (double) N;
Packit 67cb25
            size_t k1, k2;
Packit 67cb25
            double ya;
Packit 67cb25
            double x, xmax, y, ymax;
Packit 67cb25
Packit 67cb25
            gsl_histogram2d_get_xrange (hh, i, &x, &xmax;;
Packit 67cb25
            gsl_histogram2d_get_yrange (hh, j, &y, &ymax);
Packit 67cb25
Packit 67cb25
            gsl_histogram2d_find (h, x, y, &k1, &k2;;
Packit 67cb25
            ya = gsl_histogram2d_get (h, k1, k2);
Packit 67cb25
Packit 67cb25
            if (ya == 0)
Packit 67cb25
              {
Packit 67cb25
                if (z != 0)
Packit 67cb25
                  {
Packit 67cb25
                    status = 1;
Packit 67cb25
                    printf ("(%d,%d): %g vs %g\n", (int)i, (int)j, z, ya);
Packit 67cb25
                  }
Packit 67cb25
              }
Packit 67cb25
            else
Packit 67cb25
              {
Packit 67cb25
                double err = 1 / sqrt (gsl_histogram2d_get (hh, i, j));
Packit 67cb25
                double sigma = fabs ((z - ya) / (ya * err));
Packit 67cb25
                if (sigma > 3)
Packit 67cb25
                  {
Packit 67cb25
                    status = 1;
Packit 67cb25
                    printf ("%g vs %g err=%g sigma=%g\n", z, ya, err, sigma);
Packit 67cb25
                  }
Packit 67cb25
              }
Packit 67cb25
          }
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
    gsl_histogram2d_pdf_free (p) ;
Packit 67cb25
    gsl_histogram2d_free (hh) ;
Packit 67cb25
    
Packit 67cb25
    gsl_test (status, "gsl_histogram2d_pdf_sample within statistical errors");
Packit 67cb25
  }
Packit 67cb25
Packit 67cb25
  gsl_histogram2d_free (h) ;
Packit 67cb25
}