Blame histogram/test1d_resample.c

Packit 67cb25
/* histogram/test1d_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_histogram.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
test1d_resample (void)
Packit 67cb25
{
Packit 67cb25
  size_t i;
Packit 67cb25
  int status = 0;
Packit 67cb25
Packit 67cb25
  gsl_histogram *h;
Packit 67cb25
Packit 67cb25
  gsl_ieee_env_setup ();
Packit 67cb25
Packit 67cb25
  h = gsl_histogram_calloc_uniform (10, 0.0, 1.0);
Packit 67cb25
Packit 67cb25
  gsl_histogram_increment (h, 0.1);
Packit 67cb25
  gsl_histogram_increment (h, 0.2);
Packit 67cb25
  gsl_histogram_increment (h, 0.2);
Packit 67cb25
  gsl_histogram_increment (h, 0.3);
Packit 67cb25
Packit 67cb25
  {
Packit 67cb25
    gsl_histogram_pdf *p = gsl_histogram_pdf_alloc (10);
Packit 67cb25
Packit 67cb25
    gsl_histogram *hh = gsl_histogram_calloc_uniform (100, 0.0, 1.0);
Packit 67cb25
Packit 67cb25
    gsl_histogram_pdf_init (p, h);
Packit 67cb25
Packit 67cb25
    for (i = 0; i < 100000; i++)
Packit 67cb25
      {
Packit 67cb25
        double u = urand();
Packit 67cb25
        double x = gsl_histogram_pdf_sample (p, u);
Packit 67cb25
        gsl_histogram_increment (hh, x);
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
    for (i = 0; i < 100; i++)
Packit 67cb25
      {
Packit 67cb25
        double y = gsl_histogram_get (hh, i) / 2500;
Packit 67cb25
        double x, xmax;
Packit 67cb25
        size_t k;
Packit 67cb25
        double ya;
Packit 67cb25
Packit 67cb25
        gsl_histogram_get_range (hh, i, &x, &xmax;;
Packit 67cb25
Packit 67cb25
        gsl_histogram_find (h, x, &k);
Packit 67cb25
        ya = gsl_histogram_get (h, k);
Packit 67cb25
Packit 67cb25
        if (ya == 0)
Packit 67cb25
          {
Packit 67cb25
            if (y != 0)
Packit 67cb25
              {
Packit 67cb25
                printf ("%d: %g vs %g\n", (int) i, y, ya);
Packit 67cb25
                status = 1;
Packit 67cb25
              }
Packit 67cb25
          }
Packit 67cb25
        else
Packit 67cb25
          {
Packit 67cb25
            double err = 1 / sqrt (gsl_histogram_get (hh, i));
Packit 67cb25
            double sigma = fabs ((y - 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", y, ya, err, sigma);
Packit 67cb25
              }
Packit 67cb25
          }
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
    gsl_histogram_pdf_free (p) ;
Packit 67cb25
    gsl_histogram_free (hh);
Packit 67cb25
Packit 67cb25
    gsl_test (status, "gsl_histogram_pdf_sample within statistical errors");
Packit 67cb25
  }
Packit 67cb25
Packit 67cb25
  gsl_histogram_free (h);
Packit 67cb25
}