Blame doc/histogram.rst

Packit 67cb25
.. index::
Packit 67cb25
   single: histograms
Packit 67cb25
   single: binning data
Packit 67cb25
Packit 67cb25
**********
Packit 67cb25
Histograms
Packit 67cb25
**********
Packit 67cb25
Packit 67cb25
This chapter describes functions for creating histograms.  Histograms
Packit 67cb25
provide a convenient way of summarizing the distribution of a set of
Packit 67cb25
data. A histogram consists of a set of *bins* which count the number
Packit 67cb25
of events falling into a given range of a continuous variable :math:`x`.
Packit 67cb25
In GSL the bins of a histogram contain floating-point numbers, so they
Packit 67cb25
can be used to record both integer and non-integer distributions.  The
Packit 67cb25
bins can use arbitrary sets of ranges (uniformly spaced bins are the
Packit 67cb25
default).  Both one and two-dimensional histograms are supported.
Packit 67cb25
Packit 67cb25
Once a histogram has been created it can also be converted into a
Packit 67cb25
probability distribution function.  The library provides efficient
Packit 67cb25
routines for selecting random samples from probability distributions.
Packit 67cb25
This can be useful for generating simulations based on real data.
Packit 67cb25
Packit 67cb25
The functions are declared in the header files :file:`gsl_histogram.h`
Packit 67cb25
and :file:`gsl_histogram2d.h`.
Packit 67cb25
Packit 67cb25
The histogram struct
Packit 67cb25
====================
Packit 67cb25
Packit 67cb25
A histogram is defined by the following struct,
Packit 67cb25
Packit 67cb25
.. type:: gsl_histogram
Packit 67cb25
Packit 67cb25
   ============================= ============================================================================
Packit 67cb25
   size_t n                      This is the number of histogram bins
Packit 67cb25
   double * range                The ranges of the bins are stored in an array of :code:`n+1` elements
Packit 67cb25
                                 pointed to by range.
Packit 67cb25
   double * bin                  The counts for each bin are stored in an array of :data:`n` elements
Packit 67cb25
                                 pointed to by :data:`bin`.  The bins are floating-point numbers, so you can
Packit 67cb25
                                 increment them by non-integer values if necessary.
Packit 67cb25
   ============================= ============================================================================
Packit 67cb25
Packit 67cb25
   The range for :code:`bin[i]` is given by :code:`range[i]` to
Packit 67cb25
   :code:`range[i+1]`.  For :math:`n` bins there are :code:`n+1` entries in the
Packit 67cb25
   array :data:`range`.  Each bin is inclusive at the lower end and exclusive
Packit 67cb25
   at the upper end.  Mathematically this means that the bins are defined by
Packit 67cb25
   the following inequality,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: \hbox{bin[i] corresponds to range[i]} \le x < \hbox{range[i+1]}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         bin[i] corresponds to range[i] <= x < range[i+1]
Packit 67cb25
Packit 67cb25
   Here is a diagram of the correspondence between ranges and bins on the
Packit 67cb25
   number-line for :math:`x`::
Packit 67cb25
Packit 67cb25
          [ bin[0] )[ bin[1] )[ bin[2] )[ bin[3] )[ bin[4] )
Packit 67cb25
       ---|---------|---------|---------|---------|---------|---  x
Packit 67cb25
        r[0]      r[1]      r[2]      r[3]      r[4]      r[5]
Packit 67cb25
Packit 67cb25
   In this picture the values of the :data:`range` array are denoted by
Packit 67cb25
   :math:`r`.  On the left-hand side of each bin the square bracket
Packit 67cb25
   :code:`[` denotes an inclusive lower bound 
Packit 67cb25
   (:math:`r \le x`),
Packit 67cb25
   and the round parentheses :code:`)` on the right-hand
Packit 67cb25
   side denote an exclusive upper bound (:math:`x < r`).  Thus any samples
Packit 67cb25
   which fall on the upper end of the histogram are excluded.  If you want
Packit 67cb25
   to include this value for the last bin you will need to add an extra bin
Packit 67cb25
   to your histogram.
Packit 67cb25
Packit 67cb25
   The :type:`gsl_histogram` struct and its associated functions are defined
Packit 67cb25
   in the header file :file:`gsl_histogram.h`.
Packit 67cb25
Packit 67cb25
Histogram allocation
Packit 67cb25
====================
Packit 67cb25
Packit 67cb25
The functions for allocating memory to a histogram follow the style of
Packit 67cb25
:func:`malloc` and :func:`free`.  In addition they also perform their own
Packit 67cb25
error checking.  If there is insufficient memory available to allocate a
Packit 67cb25
histogram then the functions call the error handler (with an error
Packit 67cb25
number of :macro:`GSL_ENOMEM`) in addition to returning a null pointer.
Packit 67cb25
Thus if you use the library error handler to abort your program then it
Packit 67cb25
isn't necessary to check every histogram :code:`alloc`.
Packit 67cb25
Packit 67cb25
.. function:: gsl_histogram * gsl_histogram_alloc (size_t n)
Packit 67cb25
Packit 67cb25
   This function allocates memory for a histogram with :data:`n` bins, and
Packit 67cb25
   returns a pointer to a newly created :type:`gsl_histogram` struct.  If
Packit 67cb25
   insufficient memory is available a null pointer is returned and the
Packit 67cb25
   error handler is invoked with an error code of :macro:`GSL_ENOMEM`. The
Packit 67cb25
   bins and ranges are not initialized, and should be prepared using one of
Packit 67cb25
   the range-setting functions below in order to make the histogram ready
Packit 67cb25
   for use.
Packit 67cb25
Packit 67cb25
.. @deftypefun {gsl_histogram *} gsl_histogram_calloc (size_t n)
Packit 67cb25
.. This function allocates memory for a histogram with :data:`n` bins, and
Packit 67cb25
.. returns a pointer to its newly initialized :type:`gsl_histogram` struct.
Packit 67cb25
.. The bins are uniformly spaced with a total range of 
Packit 67cb25
.. @c{$0 \le  x < n$}
Packit 67cb25
.. @math{0 <=  x < n},
Packit 67cb25
.. as shown in the table below.
Packit 67cb25
Packit 67cb25
.. @tex
Packit 67cb25
.. \beforedisplay
Packit 67cb25
.. $$
Packit 67cb25
.. \matrix{
Packit 67cb25
.. \hbox{bin[0]}&\hbox{corresponds to}& 0 \le x < 1\cr
Packit 67cb25
.. \hbox{bin[1]}&\hbox{corresponds to}& 1 \le x < 2\cr
Packit 67cb25
.. \dots&\dots&\dots\cr
Packit 67cb25
.. \hbox{bin[n-1]}&\hbox{corresponds to}&n-1 \le x < n}
Packit 67cb25
.. $$
Packit 67cb25
.. \afterdisplay
Packit 67cb25
.. @end tex
Packit 67cb25
.. @ifinfo
Packit 67cb25
.. @display
Packit 67cb25
.. bin[0] corresponds to 0 <= x < 1
Packit 67cb25
.. bin[1] corresponds to 1 <= x < 2
Packit 67cb25
.. @dots{}
Packit 67cb25
.. bin[n-1] corresponds to n-1 <= x < n
Packit 67cb25
.. @end display
Packit 67cb25
.. @end ifinfo
Packit 67cb25
.. @noindent
Packit 67cb25
.. The bins are initialized to zero so the histogram is ready for use.
Packit 67cb25
Packit 67cb25
.. If insufficient memory is available a null pointer is returned and the
Packit 67cb25
.. error handler is invoked with an error code of :macro:`GSL_ENOMEM`.
Packit 67cb25
.. @end deftypefun
Packit 67cb25
Packit 67cb25
.. @deftypefun {gsl_histogram *} gsl_histogram_calloc_uniform (size_t n, double xmin, double xmax)
Packit 67cb25
.. This function allocates memory for a histogram with :data:`n` uniformly
Packit 67cb25
.. spaced bins from :data:`xmin` to :data:`xmax`, and returns a pointer to the
Packit 67cb25
.. newly initialized :type:`gsl_histogram` struct. 
Packit 67cb25
.. If insufficient memory is available a null pointer is returned and the
Packit 67cb25
.. error handler is invoked with an error code of :macro:`GSL_ENOMEM`.
Packit 67cb25
.. @end deftypefun
Packit 67cb25
Packit 67cb25
.. @deftypefun {gsl_histogram *} gsl_histogram_calloc_range (size_t n, double * range)
Packit 67cb25
.. This function allocates a histogram of size :data:`n` using the @math{n+1}
Packit 67cb25
.. bin ranges specified by the array :data:`range`.
Packit 67cb25
.. @end deftypefun
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_set_ranges (gsl_histogram * h, const double range[], size_t size)
Packit 67cb25
Packit 67cb25
   This function sets the ranges of the existing histogram :data:`h` using
Packit 67cb25
   the array :data:`range` of size :data:`size`.  The values of the histogram
Packit 67cb25
   bins are reset to zero.  The :data:`range` array should contain the
Packit 67cb25
   desired bin limits.  The ranges can be arbitrary, subject to the
Packit 67cb25
   restriction that they are monotonically increasing.
Packit 67cb25
Packit 67cb25
   The following example shows how to create a histogram with logarithmic
Packit 67cb25
   bins with ranges [1,10), [10,100) and [100,1000)::
Packit 67cb25
Packit 67cb25
      gsl_histogram * h = gsl_histogram_alloc (3);
Packit 67cb25
Packit 67cb25
      /* bin[0] covers the range 1 <= x < 10 */
Packit 67cb25
      /* bin[1] covers the range 10 <= x < 100 */
Packit 67cb25
      /* bin[2] covers the range 100 <= x < 1000 */
Packit 67cb25
Packit 67cb25
      double range[4] = { 1.0, 10.0, 100.0, 1000.0 };
Packit 67cb25
Packit 67cb25
      gsl_histogram_set_ranges (h, range, 4);
Packit 67cb25
Packit 67cb25
   Note that the size of the :data:`range` array should be defined to be one
Packit 67cb25
   element bigger than the number of bins.  The additional element is
Packit 67cb25
   required for the upper value of the final bin.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_set_ranges_uniform (gsl_histogram * h, double xmin, double xmax)
Packit 67cb25
Packit 67cb25
   This function sets the ranges of the existing histogram :data:`h` to cover
Packit 67cb25
   the range :data:`xmin` to :data:`xmax` uniformly.  The values of the
Packit 67cb25
   histogram bins are reset to zero.  The bin ranges are shown in the table
Packit 67cb25
   below,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math::
Packit 67cb25
Packit 67cb25
         \begin{array}{ccc}
Packit 67cb25
           \hbox{bin[0]}&\hbox{corresponds to}& xmin \le  x < xmin + d \\
Packit 67cb25
           \hbox{bin[1]} &\hbox{corresponds to}& xmin + d \le  x < xmin + 2 d \\
Packit 67cb25
           \dots&\dots&\dots \\
Packit 67cb25
           \hbox{bin[n-1]} & \hbox{corresponds to}& xmin + (n-1)d \le  x < xmax
Packit 67cb25
         \end{array}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         bin[0] corresponds to xmin <= x < xmin + d
Packit 67cb25
         bin[1] corresponds to xmin + d <= x < xmin + 2 d
Packit 67cb25
         ......
Packit 67cb25
         bin[n-1] corresponds to xmin + (n-1)d <= x < xmax
Packit 67cb25
Packit 67cb25
   where :math:`d` is the bin spacing, :math:`d = (xmax-xmin)/n`.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_histogram_free (gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function frees the histogram :data:`h` and all of the memory
Packit 67cb25
   associated with it.
Packit 67cb25
Packit 67cb25
Copying Histograms
Packit 67cb25
==================
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_memcpy (gsl_histogram * dest, const gsl_histogram * src)
Packit 67cb25
Packit 67cb25
   This function copies the histogram :data:`src` into the pre-existing
Packit 67cb25
   histogram :data:`dest`, making :data:`dest` into an exact copy of :data:`src`.
Packit 67cb25
   The two histograms must be of the same size.
Packit 67cb25
Packit 67cb25
.. function:: gsl_histogram * gsl_histogram_clone (const gsl_histogram * src)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to a newly created histogram which is an
Packit 67cb25
   exact copy of the histogram :data:`src`.
Packit 67cb25
Packit 67cb25
Updating and accessing histogram elements
Packit 67cb25
=========================================
Packit 67cb25
Packit 67cb25
There are two ways to access histogram bins, either by specifying an
Packit 67cb25
:math:`x` coordinate or by using the bin-index directly.  The functions
Packit 67cb25
for accessing the histogram through :math:`x` coordinates use a binary
Packit 67cb25
search to identify the bin which covers the appropriate range.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_increment (gsl_histogram * h, double x)
Packit 67cb25
Packit 67cb25
   This function updates the histogram :data:`h` by adding one (1.0) to the
Packit 67cb25
   bin whose range contains the coordinate :data:`x`. 
Packit 67cb25
Packit 67cb25
   If :data:`x` lies in the valid range of the histogram then the function
Packit 67cb25
   returns zero to indicate success.  If :data:`x` is less than the lower
Packit 67cb25
   limit of the histogram then the function returns :macro:`GSL_EDOM`, and
Packit 67cb25
   none of bins are modified.  Similarly, if the value of :data:`x` is greater
Packit 67cb25
   than or equal to the upper limit of the histogram then the function
Packit 67cb25
   returns :macro:`GSL_EDOM`, and none of the bins are modified.  The error
Packit 67cb25
   handler is not called, however, since it is often necessary to compute
Packit 67cb25
   histograms for a small range of a larger dataset, ignoring the values
Packit 67cb25
   outside the range of interest.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_accumulate (gsl_histogram * h, double x, double weight)
Packit 67cb25
Packit 67cb25
   This function is similar to :func:`gsl_histogram_increment` but increases
Packit 67cb25
   the value of the appropriate bin in the histogram :data:`h` by the
Packit 67cb25
   floating-point number :data:`weight`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram_get (const gsl_histogram * h, size_t i)
Packit 67cb25
Packit 67cb25
   This function returns the contents of the :data:`i`-th bin of the histogram
Packit 67cb25
   :data:`h`.  If :data:`i` lies outside the valid range of indices for the
Packit 67cb25
   histogram then the error handler is called with an error code of
Packit 67cb25
   :macro:`GSL_EDOM` and the function returns 0.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_get_range (const gsl_histogram * h, size_t i, double * lower, double * upper)
Packit 67cb25
Packit 67cb25
   This function finds the upper and lower range limits of the :data:`i`-th
Packit 67cb25
   bin of the histogram :data:`h`.  If the index :data:`i` is valid then the
Packit 67cb25
   corresponding range limits are stored in :data:`lower` and :data:`upper`.
Packit 67cb25
   The lower limit is inclusive (i.e. events with this coordinate are
Packit 67cb25
   included in the bin) and the upper limit is exclusive (i.e. events with
Packit 67cb25
   the coordinate of the upper limit are excluded and fall in the
Packit 67cb25
   neighboring higher bin, if it exists).  The function returns 0 to
Packit 67cb25
   indicate success.  If :data:`i` lies outside the valid range of indices for
Packit 67cb25
   the histogram then the error handler is called and the function returns
Packit 67cb25
   an error code of :macro:`GSL_EDOM`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram_max (const gsl_histogram * h)
Packit 67cb25
              double gsl_histogram_min (const gsl_histogram * h)
Packit 67cb25
              size_t gsl_histogram_bins (const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   These functions return the maximum upper and minimum lower range limits
Packit 67cb25
   and the number of bins of the histogram :data:`h`.  They provide a way of
Packit 67cb25
   determining these values without accessing the :type:`gsl_histogram`
Packit 67cb25
   struct directly.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_histogram_reset (gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function resets all the bins in the histogram :data:`h` to zero.
Packit 67cb25
Packit 67cb25
Searching histogram ranges
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
The following functions are used by the access and update routines to
Packit 67cb25
locate the bin which corresponds to a given :math:`x` coordinate.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_find (const gsl_histogram * h, double x, size_t * i)
Packit 67cb25
Packit 67cb25
   This function finds and sets the index :data:`i` to the bin number which
Packit 67cb25
   covers the coordinate :data:`x` in the histogram :data:`h`.  The bin is
Packit 67cb25
   located using a binary search. The search includes an optimization for
Packit 67cb25
   histograms with uniform range, and will return the correct bin
Packit 67cb25
   immediately in this case.  If :data:`x` is found in the range of the
Packit 67cb25
   histogram then the function sets the index :data:`i` and returns
Packit 67cb25
   :macro:`GSL_SUCCESS`.  If :data:`x` lies outside the valid range of the
Packit 67cb25
   histogram then the function returns :macro:`GSL_EDOM` and the error
Packit 67cb25
   handler is invoked.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: histogram statistics
Packit 67cb25
   single: statistics, from histogram
Packit 67cb25
   single: maximum value, from histogram
Packit 67cb25
   single: minimum value, from histogram
Packit 67cb25
Packit 67cb25
Histogram Statistics
Packit 67cb25
====================
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram_max_val (const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function returns the maximum value contained in the histogram bins.
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_histogram_max_bin (const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function returns the index of the bin containing the maximum
Packit 67cb25
   value. In the case where several bins contain the same maximum value the
Packit 67cb25
   smallest index is returned.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram_min_val (const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function returns the minimum value contained in the histogram bins.
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_histogram_min_bin (const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function returns the index of the bin containing the minimum
Packit 67cb25
   value. In the case where several bins contain the same maximum value the
Packit 67cb25
   smallest index is returned.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: mean value, from histogram
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram_mean (const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function returns the mean of the histogrammed variable, where the
Packit 67cb25
   histogram is regarded as a probability distribution. Negative bin values
Packit 67cb25
   are ignored for the purposes of this calculation.  The accuracy of the
Packit 67cb25
   result is limited by the bin width.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: standard deviation, from histogram
Packit 67cb25
   single: variance, from histogram
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram_sigma (const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function returns the standard deviation of the histogrammed
Packit 67cb25
   variable, where the histogram is regarded as a probability
Packit 67cb25
   distribution. Negative bin values are ignored for the purposes of this
Packit 67cb25
   calculation. The accuracy of the result is limited by the bin width.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram_sum (const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function returns the sum of all bin values. Negative bin values
Packit 67cb25
   are included in the sum.
Packit 67cb25
Packit 67cb25
Histogram Operations
Packit 67cb25
====================
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_equal_bins_p (const gsl_histogram * h1, const gsl_histogram * h2)
Packit 67cb25
Packit 67cb25
   This function returns 1 if the all of the individual bin
Packit 67cb25
   ranges of the two histograms are identical, and 0
Packit 67cb25
   otherwise.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_add (gsl_histogram * h1, const gsl_histogram * h2)
Packit 67cb25
Packit 67cb25
   This function adds the contents of the bins in histogram :data:`h2` to the
Packit 67cb25
   corresponding bins of histogram :data:`h1`,  i.e. :math:`h'_1(i) = h_1(i) + h_2(i)`.
Packit 67cb25
   The two histograms must have identical bin ranges.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_sub (gsl_histogram * h1, const gsl_histogram * h2)
Packit 67cb25
Packit 67cb25
   This function subtracts the contents of the bins in histogram :data:`h2`
Packit 67cb25
   from the corresponding bins of histogram :data:`h1`, i.e. :math:`h'_1(i) = h_1(i) - h_2(i)`.
Packit 67cb25
   The two histograms must have identical bin ranges.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_mul (gsl_histogram * h1, const gsl_histogram * h2)
Packit 67cb25
Packit 67cb25
   This function multiplies the contents of the bins of histogram :data:`h1`
Packit 67cb25
   by the contents of the corresponding bins in histogram :data:`h2`,
Packit 67cb25
   i.e. :math:`h'_1(i) = h_1(i) * h_2(i)`.  The two histograms must have
Packit 67cb25
   identical bin ranges.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_div (gsl_histogram * h1, const gsl_histogram * h2)
Packit 67cb25
Packit 67cb25
   This function divides the contents of the bins of histogram :data:`h1` by
Packit 67cb25
   the contents of the corresponding bins in histogram :data:`h2`,
Packit 67cb25
   i.e. :math:`h'_1(i) = h_1(i) / h_2(i)`.  The two histograms must have
Packit 67cb25
   identical bin ranges.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_scale (gsl_histogram * h, double scale)
Packit 67cb25
Packit 67cb25
   This function multiplies the contents of the bins of histogram :data:`h`
Packit 67cb25
   by the constant :data:`scale`, i.e.
Packit 67cb25
   
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
   
Packit 67cb25
      .. math:: h'_1(i) = h_1(i) * \hbox{\it scale}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         h'_1(i) = h_1(i) * scale
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_shift (gsl_histogram * h, double offset)
Packit 67cb25
Packit 67cb25
   This function shifts the contents of the bins of histogram :data:`h` by
Packit 67cb25
   the constant :data:`offset`, i.e.
Packit 67cb25
   
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
   
Packit 67cb25
      .. math:: h'_1(i) = h_1(i) + \hbox{\it offset}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         h'_1(i) = h_1(i) + offset
Packit 67cb25
Packit 67cb25
Reading and writing histograms
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
The library provides functions for reading and writing histograms to a file
Packit 67cb25
as binary data or formatted text.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_fwrite (FILE * stream, const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function writes the ranges and bins of the histogram :data:`h` to the
Packit 67cb25
   stream :data:`stream` in binary format.  The return value is 0 for success
Packit 67cb25
   and :macro:`GSL_EFAILED` if there was a problem writing to the file.  Since
Packit 67cb25
   the data is written in the native binary format it may not be portable
Packit 67cb25
   between different architectures.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_fread (FILE * stream, gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function reads into the histogram :data:`h` from the open stream
Packit 67cb25
   :data:`stream` in binary format.  The histogram :data:`h` must be
Packit 67cb25
   preallocated with the correct size since the function uses the number of
Packit 67cb25
   bins in :data:`h` to determine how many bytes to read.  The return value is
Packit 67cb25
   0 for success and :macro:`GSL_EFAILED` if there was a problem reading from
Packit 67cb25
   the file.  The data is assumed to have been written in the native binary
Packit 67cb25
   format on the same architecture.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_fprintf (FILE * stream, const gsl_histogram * h, const char * range_format, const char * bin_format)
Packit 67cb25
Packit 67cb25
   This function writes the ranges and bins of the histogram :data:`h`
Packit 67cb25
   line-by-line to the stream :data:`stream` using the format specifiers
Packit 67cb25
   :data:`range_format` and :data:`bin_format`.  These should be one of the
Packit 67cb25
   :code:`%g`, :code:`%e` or :code:`%f` formats for floating point
Packit 67cb25
   numbers.  The function returns 0 for success and :macro:`GSL_EFAILED` if
Packit 67cb25
   there was a problem writing to the file.  The histogram output is
Packit 67cb25
   formatted in three columns, and the columns are separated by spaces,
Packit 67cb25
   like this::
Packit 67cb25
Packit 67cb25
      range[0] range[1] bin[0]
Packit 67cb25
      range[1] range[2] bin[1]
Packit 67cb25
      range[2] range[3] bin[2]
Packit 67cb25
      ....
Packit 67cb25
      range[n-1] range[n] bin[n-1]
Packit 67cb25
Packit 67cb25
   The values of the ranges are formatted using :data:`range_format` and the
Packit 67cb25
   value of the bins are formatted using :data:`bin_format`.  Each line
Packit 67cb25
   contains the lower and upper limit of the range of the bins and the
Packit 67cb25
   value of the bin itself.  Since the upper limit of one bin is the lower
Packit 67cb25
   limit of the next there is duplication of these values between lines but
Packit 67cb25
   this allows the histogram to be manipulated with line-oriented tools.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_fscanf (FILE * stream, gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function reads formatted data from the stream :data:`stream` into the
Packit 67cb25
   histogram :data:`h`.  The data is assumed to be in the three-column format
Packit 67cb25
   used by :func:`gsl_histogram_fprintf`.  The histogram :data:`h` must be
Packit 67cb25
   preallocated with the correct length since the function uses the size of
Packit 67cb25
   :data:`h` to determine how many numbers to read.  The function returns 0
Packit 67cb25
   for success and :macro:`GSL_EFAILED` if there was a problem reading from
Packit 67cb25
   the file.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: resampling from histograms
Packit 67cb25
   single: sampling from histograms
Packit 67cb25
   single: probability distributions, from histograms
Packit 67cb25
Packit 67cb25
Resampling from histograms
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
A histogram made by counting events can be regarded as a measurement of
Packit 67cb25
a probability distribution.  Allowing for statistical error, the height
Packit 67cb25
of each bin represents the probability of an event where the value of
Packit 67cb25
:math:`x` falls in the range of that bin.  The probability distribution
Packit 67cb25
function has the one-dimensional form :math:`p(x)dx` where,
Packit 67cb25
Packit 67cb25
.. math:: p(x) = n_i / (N w_i)
Packit 67cb25
Packit 67cb25
In this equation :math:`n_i` is the number of events in the bin which
Packit 67cb25
contains :math:`x`, :math:`w_i` is the width of the bin and :math:`N` is
Packit 67cb25
the total number of events.  The distribution of events within each bin
Packit 67cb25
is assumed to be uniform.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: probability distribution, from histogram
Packit 67cb25
   single: sampling from histograms
Packit 67cb25
   single: random sampling from histograms
Packit 67cb25
   single: histograms, random sampling from
Packit 67cb25
Packit 67cb25
The histogram probability distribution struct
Packit 67cb25
=============================================
Packit 67cb25
Packit 67cb25
The probability distribution function for a histogram consists of a set
Packit 67cb25
of *bins* which measure the probability of an event falling into a
Packit 67cb25
given range of a continuous variable :math:`x`. A probability
Packit 67cb25
distribution function is defined by the following struct, which actually
Packit 67cb25
stores the cumulative probability distribution function.  This is the
Packit 67cb25
natural quantity for generating samples via the inverse transform
Packit 67cb25
method, because there is a one-to-one mapping between the cumulative
Packit 67cb25
probability distribution and the range [0,1].  It can be shown that by
Packit 67cb25
taking a uniform random number in this range and finding its
Packit 67cb25
corresponding coordinate in the cumulative probability distribution we
Packit 67cb25
obtain samples with the desired probability distribution.
Packit 67cb25
Packit 67cb25
.. type:: gsl_histogram_pdf
Packit 67cb25
Packit 67cb25
   ================================ =======================================================================
Packit 67cb25
   :code:`size_t n`                 This is the number of bins used to approximate the probability
Packit 67cb25
                                    distribution function. 
Packit 67cb25
   :code:`double * range`           The ranges of the bins are stored in an array of :math:`n + 1`
Packit 67cb25
                                    elements pointed to by :data:`range`.
Packit 67cb25
   :code:`double * sum`             The cumulative probability for the bins is stored in an array of
Packit 67cb25
                                    :data:`n` elements pointed to by :data:`sum`.
Packit 67cb25
   ================================ =======================================================================
Packit 67cb25
Packit 67cb25
The following functions allow you to create a :type:`gsl_histogram_pdf`
Packit 67cb25
struct which represents this probability distribution and generate
Packit 67cb25
random samples from it.
Packit 67cb25
Packit 67cb25
.. function:: gsl_histogram_pdf * gsl_histogram_pdf_alloc (size_t n)
Packit 67cb25
Packit 67cb25
   This function allocates memory for a probability distribution with
Packit 67cb25
   :data:`n` bins and returns a pointer to a newly initialized
Packit 67cb25
   :type:`gsl_histogram_pdf` struct. If insufficient memory is available a
Packit 67cb25
   null pointer is returned and the error handler is invoked with an error
Packit 67cb25
   code of :macro:`GSL_ENOMEM`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram_pdf_init (gsl_histogram_pdf * p, const gsl_histogram * h)
Packit 67cb25
Packit 67cb25
   This function initializes the probability distribution :data:`p` with
Packit 67cb25
   the contents of the histogram :data:`h`. If any of the bins of :data:`h` are
Packit 67cb25
   negative then the error handler is invoked with an error code of
Packit 67cb25
   :macro:`GSL_EDOM` because a probability distribution cannot contain
Packit 67cb25
   negative values.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_histogram_pdf_free (gsl_histogram_pdf * p)
Packit 67cb25
Packit 67cb25
   This function frees the probability distribution function :data:`p` and
Packit 67cb25
   all of the memory associated with it.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram_pdf_sample (const gsl_histogram_pdf * p, double r)
Packit 67cb25
Packit 67cb25
   This function uses :data:`r`, a uniform random number between zero and
Packit 67cb25
   one, to compute a single random sample from the probability distribution
Packit 67cb25
   :data:`p`.  The algorithm used to compute the sample :math:`s` is given by
Packit 67cb25
   the following formula,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: s = \hbox{range}[i] + \delta * (\hbox{range}[i+1] - \hbox{range}[i])
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         s = range[i] + delta * (range[i+1] - range[i])
Packit 67cb25
Packit 67cb25
   where :math:`i` is the index which satisfies 
Packit 67cb25
   :math:`sum[i] \le  r < sum[i+1]`
Packit 67cb25
   and :math:`delta` is 
Packit 67cb25
   :math:`(r - sum[i])/(sum[i+1] - sum[i])`.
Packit 67cb25
Packit 67cb25
Example programs for histograms
Packit 67cb25
===============================
Packit 67cb25
Packit 67cb25
The following program shows how to make a simple histogram of a column
Packit 67cb25
of numerical data supplied on :code:`stdin`.  The program takes three
Packit 67cb25
arguments, specifying the upper and lower bounds of the histogram and
Packit 67cb25
the number of bins.  It then reads numbers from :code:`stdin`, one line at
Packit 67cb25
a time, and adds them to the histogram.  When there is no more data to
Packit 67cb25
read it prints out the accumulated histogram using
Packit 67cb25
:func:`gsl_histogram_fprintf`.
Packit 67cb25
Packit 67cb25
.. include:: examples/histogram.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
Here is an example of the program in use.  We generate 10000 random
Packit 67cb25
samples from a Cauchy distribution with a width of 30 and histogram
Packit 67cb25
them over the range -100 to 100, using 200 bins::
Packit 67cb25
Packit 67cb25
  $ gsl-randist 0 10000 cauchy 30 
Packit 67cb25
     | gsl-histogram -- -100 100 200 > histogram.dat
Packit 67cb25
Packit 67cb25
:numref:`fig_histogram` shows the familiar shape of the
Packit 67cb25
Cauchy distribution and the fluctuations caused by the finite sample
Packit 67cb25
size.
Packit 67cb25
Packit 67cb25
.. _fig_histogram:
Packit 67cb25
Packit 67cb25
.. figure:: /images/histogram.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Histogram output from example program
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: two dimensional histograms
Packit 67cb25
   single: 2D histograms
Packit 67cb25
Packit 67cb25
Two dimensional histograms
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
A two dimensional histogram consists of a set of *bins* which count
Packit 67cb25
the number of events falling in a given area of the :math:`(x,y)`
Packit 67cb25
plane.  The simplest way to use a two dimensional histogram is to record
Packit 67cb25
two-dimensional position information, :math:`n(x,y)`.  Another possibility
Packit 67cb25
is to form a *joint distribution* by recording related
Packit 67cb25
variables.  For example a detector might record both the position of an
Packit 67cb25
event (:math:`x`) and the amount of energy it deposited :math:`E`.  These
Packit 67cb25
could be histogrammed as the joint distribution :math:`n(x,E)`.
Packit 67cb25
Packit 67cb25
The 2D histogram struct
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
Two dimensional histograms are defined by the following struct,
Packit 67cb25
Packit 67cb25
.. type:: gsl_histogram2d
Packit 67cb25
Packit 67cb25
   =========================== ============================================================================
Packit 67cb25
   :code:`size_t nx, ny`       This is the number of histogram bins in the x and y directions.
Packit 67cb25
   :code:`double * xrange`     The ranges of the bins in the x-direction are stored in an array of
Packit 67cb25
                               :code:`nx + 1` elements pointed to by :data:`xrange`.
Packit 67cb25
   :code:`double * yrange`     The ranges of the bins in the y-direction are stored in an array of
Packit 67cb25
                               :code:`ny + 1` elements pointed to by :data:`yrange`.
Packit 67cb25
   :code:`double * bin`        The counts for each bin are stored in an array pointed to by :data:`bin`.
Packit 67cb25
                               The bins are floating-point numbers, so you can increment them by
Packit 67cb25
                               non-integer values if necessary.  The array :data:`bin` stores the two
Packit 67cb25
                               dimensional array of bins in a single block of memory according to the
Packit 67cb25
                               mapping :code:`bin(i,j)` = :code:`bin[i * ny + j]`.
Packit 67cb25
   =========================== ============================================================================
Packit 67cb25
Packit 67cb25
The range for :code:`bin(i,j)` is given by :code:`xrange[i]` to
Packit 67cb25
:code:`xrange[i+1]` in the x-direction and :code:`yrange[j]` to
Packit 67cb25
:code:`yrange[j+1]` in the y-direction.  Each bin is inclusive at the lower
Packit 67cb25
end and exclusive at the upper end.  Mathematically this means that the
Packit 67cb25
bins are defined by the following inequality,
Packit 67cb25
Packit 67cb25
.. only:: not texinfo
Packit 67cb25
Packit 67cb25
   .. math::
Packit 67cb25
Packit 67cb25
      \begin{array}{cc}
Packit 67cb25
        \hbox{bin(i,j) corresponds to} & \hbox{\it xrange}[i] \le x < \hbox{\it xrange}[i+1] \\
Packit 67cb25
        \hbox{and} & \hbox{\it yrange}[j] \le y < \hbox{\it yrange}[j+1]
Packit 67cb25
      \end{array}
Packit 67cb25
Packit 67cb25
.. only:: texinfo
Packit 67cb25
Packit 67cb25
   ::
Packit 67cb25
Packit 67cb25
      bin(i,j) corresponds to xrange[i] <= x < xrange[i+1]
Packit 67cb25
                          and yrange[j] <= y < yrange[j+1]
Packit 67cb25
Packit 67cb25
Note that any samples which fall on the upper sides of the histogram are
Packit 67cb25
excluded.  If you want to include these values for the side bins you will
Packit 67cb25
need to add an extra row or column to your histogram.
Packit 67cb25
Packit 67cb25
The :type:`gsl_histogram2d` struct and its associated functions are
Packit 67cb25
defined in the header file :file:`gsl_histogram2d.h`.
Packit 67cb25
Packit 67cb25
2D Histogram allocation
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
The functions for allocating memory to a 2D histogram follow the style
Packit 67cb25
of :func:`malloc` and :func:`free`.  In addition they also perform their
Packit 67cb25
own error checking.  If there is insufficient memory available to
Packit 67cb25
allocate a histogram then the functions call the error handler (with
Packit 67cb25
an error number of :macro:`GSL_ENOMEM`) in addition to returning a null
Packit 67cb25
pointer.  Thus if you use the library error handler to abort your program
Packit 67cb25
then it isn't necessary to check every 2D histogram :code:`alloc`.
Packit 67cb25
Packit 67cb25
.. function:: gsl_histogram2d * gsl_histogram2d_alloc (size_t nx, size_t ny)
Packit 67cb25
Packit 67cb25
   This function allocates memory for a two-dimensional histogram with
Packit 67cb25
   :data:`nx` bins in the x direction and :data:`ny` bins in the y direction.
Packit 67cb25
   The function returns a pointer to a newly created :type:`gsl_histogram2d`
Packit 67cb25
   struct. If insufficient memory is available a null pointer is returned
Packit 67cb25
   and the error handler is invoked with an error code of
Packit 67cb25
   :macro:`GSL_ENOMEM`. The bins and ranges must be initialized with one of
Packit 67cb25
   the functions below before the histogram is ready for use.
Packit 67cb25
Packit 67cb25
.. @deftypefun {gsl_histogram2d *} gsl_histogram2d_calloc (size_t nx, size_t ny)
Packit 67cb25
.. This function allocates memory for a two-dimensional histogram with
Packit 67cb25
.. :data:`nx` bins in the x direction and :data:`ny` bins in the y
Packit 67cb25
.. direction.  The function returns a pointer to a newly initialized
Packit 67cb25
.. :type:`gsl_histogram2d` struct.  The bins are uniformly spaced with a
Packit 67cb25
.. total range of 
Packit 67cb25
.. @c{$0 \le  x < nx$}
Packit 67cb25
.. @math{0 <= x < nx} in the x-direction and 
Packit 67cb25
.. @c{$0 \le  y < ny$} 
Packit 67cb25
.. @math{0 <=  y < ny} in the y-direction, as shown in the table below.
Packit 67cb25
.. 
Packit 67cb25
.. The bins are initialized to zero so the histogram is ready for use.
Packit 67cb25
.. 
Packit 67cb25
.. If insufficient memory is available a null pointer is returned and the
Packit 67cb25
.. error handler is invoked with an error code of :macro:`GSL_ENOMEM`.
Packit 67cb25
.. @end deftypefun
Packit 67cb25
.. 
Packit 67cb25
.. @deftypefun {gsl_histogram2d *} gsl_histogram2d_calloc_uniform (size_t nx, size_t ny, double xmin, double xmax, double ymin, double ymax)
Packit 67cb25
.. This function allocates a histogram of size :data:`nx`-by-:data:`ny` which
Packit 67cb25
.. uniformly covers the ranges :data:`xmin` to :data:`xmax` and :data:`ymin` to
Packit 67cb25
.. :data:`ymax` in the :math:`x` and :math:`y` directions respectively.
Packit 67cb25
.. @end deftypefun
Packit 67cb25
.. 
Packit 67cb25
.. @deftypefun {gsl_histogram2d *} gsl_histogram2d_calloc_range (size_t nx, size_t ny, double * xrange, double * yrange)
Packit 67cb25
.. This function allocates a histogram of size :data:`nx`-by-:data:`ny` using
Packit 67cb25
.. the @math{nx+1} and @math{ny+1} bin ranges specified by the arrays
Packit 67cb25
.. :data:`xrange` and :data:`yrange`.
Packit 67cb25
.. @end deftypefun
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_set_ranges (gsl_histogram2d * h,  const double xrange[], size_t xsize, const double yrange[], size_t ysize)
Packit 67cb25
Packit 67cb25
   This function sets the ranges of the existing histogram :data:`h` using
Packit 67cb25
   the arrays :data:`xrange` and :data:`yrange` of size :data:`xsize` and
Packit 67cb25
   :data:`ysize` respectively.  The values of the histogram bins are reset to
Packit 67cb25
   zero.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_set_ranges_uniform (gsl_histogram2d * h, double xmin, double xmax, double ymin, double ymax)
Packit 67cb25
Packit 67cb25
   This function sets the ranges of the existing histogram :data:`h` to cover
Packit 67cb25
   the ranges :data:`xmin` to :data:`xmax` and :data:`ymin` to :data:`ymax`
Packit 67cb25
   uniformly.  The values of the histogram bins are reset to zero.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_histogram2d_free (gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function frees the 2D histogram :data:`h` and all of the memory
Packit 67cb25
   associated with it.
Packit 67cb25
Packit 67cb25
Copying 2D Histograms
Packit 67cb25
=====================
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_memcpy (gsl_histogram2d * dest, const gsl_histogram2d * src)
Packit 67cb25
Packit 67cb25
   This function copies the histogram :data:`src` into the pre-existing
Packit 67cb25
   histogram :data:`dest`, making :data:`dest` into an exact copy of :data:`src`.
Packit 67cb25
   The two histograms must be of the same size.
Packit 67cb25
Packit 67cb25
.. function:: gsl_histogram2d * gsl_histogram2d_clone (const gsl_histogram2d * src)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to a newly created histogram which is an
Packit 67cb25
   exact copy of the histogram :data:`src`.
Packit 67cb25
Packit 67cb25
Updating and accessing 2D histogram elements
Packit 67cb25
============================================
Packit 67cb25
Packit 67cb25
You can access the bins of a two-dimensional histogram either by
Packit 67cb25
specifying a pair of :math:`(x,y)` coordinates or by using the bin
Packit 67cb25
indices :math:`(i,j)` directly.  The functions for accessing the histogram
Packit 67cb25
through :math:`(x,y)` coordinates use binary searches in the x and y
Packit 67cb25
directions to identify the bin which covers the appropriate range.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_increment (gsl_histogram2d * h, double x, double y)
Packit 67cb25
Packit 67cb25
   This function updates the histogram :data:`h` by adding one (1.0) to the
Packit 67cb25
   bin whose x and y ranges contain the coordinates (:data:`x`, :data:`y`).
Packit 67cb25
Packit 67cb25
   If the point :math:`(x,y)` lies inside the valid ranges of the
Packit 67cb25
   histogram then the function returns zero to indicate success.  If
Packit 67cb25
   :math:`(x,y)` lies outside the limits of the histogram then the
Packit 67cb25
   function returns :macro:`GSL_EDOM`, and none of the bins are modified.  The
Packit 67cb25
   error handler is not called, since it is often necessary to compute
Packit 67cb25
   histograms for a small range of a larger dataset, ignoring any
Packit 67cb25
   coordinates outside the range of interest.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_accumulate (gsl_histogram2d * h, double x, double y, double weight)
Packit 67cb25
Packit 67cb25
   This function is similar to :func:`gsl_histogram2d_increment` but increases
Packit 67cb25
   the value of the appropriate bin in the histogram :data:`h` by the
Packit 67cb25
   floating-point number :data:`weight`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_get (const gsl_histogram2d * h, size_t i, size_t j)
Packit 67cb25
Packit 67cb25
   This function returns the contents of the (:data:`i`, :data:`j`)-th bin of the
Packit 67cb25
   histogram :data:`h`.  If (:data:`i`, :data:`j`) lies outside the valid range of
Packit 67cb25
   indices for the histogram then the error handler is called with an error
Packit 67cb25
   code of :macro:`GSL_EDOM` and the function returns 0.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_get_xrange (const gsl_histogram2d * h, size_t i, double * xlower, double * xupper)
Packit 67cb25
              int gsl_histogram2d_get_yrange (const gsl_histogram2d * h, size_t j, double * ylower, double * yupper)
Packit 67cb25
Packit 67cb25
   These functions find the upper and lower range limits of the :data:`i`-th
Packit 67cb25
   and :data:`j`-th bins in the x and y directions of the histogram :data:`h`.
Packit 67cb25
   The range limits are stored in :data:`xlower` and :data:`xupper` or
Packit 67cb25
   :data:`ylower` and :data:`yupper`.  The lower limits are inclusive
Packit 67cb25
   (i.e. events with these coordinates are included in the bin) and the
Packit 67cb25
   upper limits are exclusive (i.e. events with the value of the upper
Packit 67cb25
   limit are not included and fall in the neighboring higher bin, if it
Packit 67cb25
   exists).  The functions return 0 to indicate success.  If :data:`i` or
Packit 67cb25
   :data:`j` lies outside the valid range of indices for the histogram then
Packit 67cb25
   the error handler is called with an error code of :macro:`GSL_EDOM`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_xmax (const gsl_histogram2d * h)
Packit 67cb25
              double gsl_histogram2d_xmin (const gsl_histogram2d * h)
Packit 67cb25
              size_t gsl_histogram2d_nx (const gsl_histogram2d * h)
Packit 67cb25
              double gsl_histogram2d_ymax (const gsl_histogram2d * h)
Packit 67cb25
              double gsl_histogram2d_ymin (const gsl_histogram2d * h)
Packit 67cb25
              size_t gsl_histogram2d_ny (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   These functions return the maximum upper and minimum lower range limits
Packit 67cb25
   and the number of bins for the x and y directions of the histogram
Packit 67cb25
   :data:`h`.  They provide a way of determining these values without
Packit 67cb25
   accessing the :type:`gsl_histogram2d` struct directly.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_histogram2d_reset (gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function resets all the bins of the histogram :data:`h` to zero.
Packit 67cb25
Packit 67cb25
Searching 2D histogram ranges
Packit 67cb25
=============================
Packit 67cb25
Packit 67cb25
The following functions are used by the access and update routines to
Packit 67cb25
locate the bin which corresponds to a given :math:`(x,y)` coordinate.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_find (const gsl_histogram2d * h, double x, double y, size_t * i, size_t * j)
Packit 67cb25
Packit 67cb25
   This function finds and sets the indices :data:`i` and :data:`j` to
Packit 67cb25
   the bin which covers the coordinates (:data:`x`, :data:`y`). The bin is
Packit 67cb25
   located using a binary search.  The search includes an optimization for
Packit 67cb25
   histograms with uniform ranges, and will return the correct bin immediately
Packit 67cb25
   in this case. If :math:`(x,y)` is found then the function sets the
Packit 67cb25
   indices (:data:`i`, :data:`j`) and returns :macro:`GSL_SUCCESS`.  If
Packit 67cb25
   :math:`(x,y)` lies outside the valid range of the histogram then the
Packit 67cb25
   function returns :macro:`GSL_EDOM` and the error handler is invoked.
Packit 67cb25
Packit 67cb25
2D Histogram Statistics
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_max_val (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function returns the maximum value contained in the histogram bins.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_histogram2d_max_bin (const gsl_histogram2d * h, size_t * i, size_t * j)
Packit 67cb25
Packit 67cb25
   This function finds the indices of the bin containing the maximum value
Packit 67cb25
   in the histogram :data:`h` and stores the result in (:data:`i`, :data:`j`). In
Packit 67cb25
   the case where several bins contain the same maximum value the first bin
Packit 67cb25
   found is returned.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_min_val (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function returns the minimum value contained in the histogram bins.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_histogram2d_min_bin (const gsl_histogram2d * h, size_t * i, size_t * j)
Packit 67cb25
Packit 67cb25
   This function finds the indices of the bin containing the minimum value
Packit 67cb25
   in the histogram :data:`h` and stores the result in (:data:`i`, :data:`j`). In
Packit 67cb25
   the case where several bins contain the same maximum value the first bin
Packit 67cb25
   found is returned.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_xmean (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function returns the mean of the histogrammed x variable, where the
Packit 67cb25
   histogram is regarded as a probability distribution. Negative bin values
Packit 67cb25
   are ignored for the purposes of this calculation.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_ymean (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function returns the mean of the histogrammed y variable, where the
Packit 67cb25
   histogram is regarded as a probability distribution. Negative bin values
Packit 67cb25
   are ignored for the purposes of this calculation.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_xsigma (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function returns the standard deviation of the histogrammed
Packit 67cb25
   x variable, where the histogram is regarded as a probability
Packit 67cb25
   distribution. Negative bin values are ignored for the purposes of this
Packit 67cb25
   calculation.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_ysigma (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function returns the standard deviation of the histogrammed
Packit 67cb25
   y variable, where the histogram is regarded as a probability
Packit 67cb25
   distribution. Negative bin values are ignored for the purposes of this
Packit 67cb25
   calculation.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_cov (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function returns the covariance of the histogrammed x and y
Packit 67cb25
   variables, where the histogram is regarded as a probability
Packit 67cb25
   distribution. Negative bin values are ignored for the purposes of this
Packit 67cb25
   calculation.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_histogram2d_sum (const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function returns the sum of all bin values. Negative bin values
Packit 67cb25
   are included in the sum.
Packit 67cb25
Packit 67cb25
2D Histogram Operations
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_equal_bins_p (const gsl_histogram2d * h1, const gsl_histogram2d * h2)
Packit 67cb25
Packit 67cb25
   This function returns 1 if all the individual bin ranges of the two
Packit 67cb25
   histograms are identical, and 0 otherwise.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_add (gsl_histogram2d * h1, const gsl_histogram2d * h2)
Packit 67cb25
Packit 67cb25
   This function adds the contents of the bins in histogram :data:`h2` to the
Packit 67cb25
   corresponding bins of histogram :data:`h1`,
Packit 67cb25
   i.e. :math:`h'_1(i,j) = h_1(i,j) + h_2(i,j)`.
Packit 67cb25
   The two histograms must have identical bin ranges.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_sub (gsl_histogram2d * h1, const gsl_histogram2d * h2)
Packit 67cb25
Packit 67cb25
   This function subtracts the contents of the bins in histogram :data:`h2` from the
Packit 67cb25
   corresponding bins of histogram :data:`h1`,
Packit 67cb25
   i.e. :math:`h'_1(i,j) = h_1(i,j) - h_2(i,j)`.
Packit 67cb25
   The two histograms must have identical bin ranges.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_mul (gsl_histogram2d * h1, const gsl_histogram2d * h2)
Packit 67cb25
Packit 67cb25
   This function multiplies the contents of the bins of histogram :data:`h1`
Packit 67cb25
   by the contents of the corresponding bins in histogram :data:`h2`,
Packit 67cb25
   i.e. :math:`h'_1(i,j) = h_1(i,j) * h_2(i,j)`.
Packit 67cb25
   The two histograms must have identical bin ranges.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_div (gsl_histogram2d * h1, const gsl_histogram2d * h2)
Packit 67cb25
Packit 67cb25
   This function divides the contents of the bins of histogram :data:`h1`
Packit 67cb25
   by the contents of the corresponding bins in histogram :data:`h2`,
Packit 67cb25
   i.e. :math:`h'_1(i,j) = h_1(i,j) / h_2(i,j)`.
Packit 67cb25
   The two histograms must have identical bin ranges.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_scale (gsl_histogram2d * h, double scale)
Packit 67cb25
Packit 67cb25
   This function multiplies the contents of the bins of histogram :data:`h`
Packit 67cb25
   by the constant :data:`scale`, i.e.
Packit 67cb25
   
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
   
Packit 67cb25
      .. math:: h'_1(i,j) = h_1(i,j) * \hbox{\it scale}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         h'_1(i,j) = h_1(i,j) scale
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_shift (gsl_histogram2d * h, double offset)
Packit 67cb25
Packit 67cb25
   This function shifts the contents of the bins of histogram :data:`h`
Packit 67cb25
   by the constant :data:`offset`, i.e.
Packit 67cb25
   
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
   
Packit 67cb25
      .. math:: h'_1(i,j) = h_1(i,j) + \hbox{\it offset}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         h'_1(i,j) = h_1(i,j) + offset
Packit 67cb25
Packit 67cb25
Reading and writing 2D histograms
Packit 67cb25
=================================
Packit 67cb25
Packit 67cb25
The library provides functions for reading and writing two dimensional
Packit 67cb25
histograms to a file as binary data or formatted text.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_fwrite (FILE * stream, const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function writes the ranges and bins of the histogram :data:`h` to the
Packit 67cb25
   stream :data:`stream` in binary format.  The return value is 0 for success
Packit 67cb25
   and :macro:`GSL_EFAILED` if there was a problem writing to the file.  Since
Packit 67cb25
   the data is written in the native binary format it may not be portable
Packit 67cb25
   between different architectures.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_fread (FILE * stream, gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function reads into the histogram :data:`h` from the stream
Packit 67cb25
   :data:`stream` in binary format.  The histogram :data:`h` must be
Packit 67cb25
   preallocated with the correct size since the function uses the number of
Packit 67cb25
   x and y bins in :data:`h` to determine how many bytes to read.  The return
Packit 67cb25
   value is 0 for success and :macro:`GSL_EFAILED` if there was a problem
Packit 67cb25
   reading from the file.  The data is assumed to have been written in the
Packit 67cb25
   native binary format on the same architecture.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_fprintf (FILE * stream, const gsl_histogram2d * h, const char * range_format, const char * bin_format)
Packit 67cb25
Packit 67cb25
   This function writes the ranges and bins of the histogram :data:`h`
Packit 67cb25
   line-by-line to the stream :data:`stream` using the format specifiers
Packit 67cb25
   :data:`range_format` and :data:`bin_format`.  These should be one of the
Packit 67cb25
   :code:`%g`, :code:`%e` or :code:`%f` formats for floating point
Packit 67cb25
   numbers.  The function returns 0 for success and :macro:`GSL_EFAILED` if
Packit 67cb25
   there was a problem writing to the file.  The histogram output is
Packit 67cb25
   formatted in five columns, and the columns are separated by spaces,
Packit 67cb25
   like this::
Packit 67cb25
Packit 67cb25
      xrange[0] xrange[1] yrange[0] yrange[1] bin(0,0)
Packit 67cb25
      xrange[0] xrange[1] yrange[1] yrange[2] bin(0,1)
Packit 67cb25
      xrange[0] xrange[1] yrange[2] yrange[3] bin(0,2)
Packit 67cb25
      ....
Packit 67cb25
      xrange[0] xrange[1] yrange[ny-1] yrange[ny] bin(0,ny-1)
Packit 67cb25
Packit 67cb25
      xrange[1] xrange[2] yrange[0] yrange[1] bin(1,0)
Packit 67cb25
      xrange[1] xrange[2] yrange[1] yrange[2] bin(1,1)
Packit 67cb25
      xrange[1] xrange[2] yrange[1] yrange[2] bin(1,2)
Packit 67cb25
      ....
Packit 67cb25
      xrange[1] xrange[2] yrange[ny-1] yrange[ny] bin(1,ny-1)
Packit 67cb25
Packit 67cb25
      ....
Packit 67cb25
Packit 67cb25
      xrange[nx-1] xrange[nx] yrange[0] yrange[1] bin(nx-1,0)
Packit 67cb25
      xrange[nx-1] xrange[nx] yrange[1] yrange[2] bin(nx-1,1)
Packit 67cb25
      xrange[nx-1] xrange[nx] yrange[1] yrange[2] bin(nx-1,2)
Packit 67cb25
      ....
Packit 67cb25
      xrange[nx-1] xrange[nx] yrange[ny-1] yrange[ny] bin(nx-1,ny-1)
Packit 67cb25
Packit 67cb25
   Each line contains the lower and upper limits of the bin and the
Packit 67cb25
   contents of the bin.  Since the upper limits of the each bin are the
Packit 67cb25
   lower limits of the neighboring bins there is duplication of these
Packit 67cb25
   values but this allows the histogram to be manipulated with
Packit 67cb25
   line-oriented tools.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_fscanf (FILE * stream, gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function reads formatted data from the stream :data:`stream` into the
Packit 67cb25
   histogram :data:`h`.  The data is assumed to be in the five-column format
Packit 67cb25
   used by :func:`gsl_histogram2d_fprintf`.  The histogram :data:`h` must be
Packit 67cb25
   preallocated with the correct lengths since the function uses the sizes
Packit 67cb25
   of :data:`h` to determine how many numbers to read.  The function returns 0
Packit 67cb25
   for success and :macro:`GSL_EFAILED` if there was a problem reading from
Packit 67cb25
   the file.
Packit 67cb25
Packit 67cb25
Resampling from 2D histograms
Packit 67cb25
=============================
Packit 67cb25
Packit 67cb25
As in the one-dimensional case, a two-dimensional histogram made by
Packit 67cb25
counting events can be regarded as a measurement of a probability
Packit 67cb25
distribution.  Allowing for statistical error, the height of each bin
Packit 67cb25
represents the probability of an event where (:math:`x`,:math:`y`) falls in
Packit 67cb25
the range of that bin.  For a two-dimensional histogram the probability
Packit 67cb25
distribution takes the form :math:`p(x,y) dx dy` where,
Packit 67cb25
Packit 67cb25
.. math:: p(x,y) = n_{ij} / (N A_{ij})
Packit 67cb25
Packit 67cb25
In this equation :math:`n_{ij}`
Packit 67cb25
is the number of events in the bin which
Packit 67cb25
contains :math:`(x,y)`, :math:`A_{ij}`
Packit 67cb25
is the area of the bin and :math:`N` is
Packit 67cb25
the total number of events.  The distribution of events within each bin
Packit 67cb25
is assumed to be uniform.
Packit 67cb25
Packit 67cb25
.. type:: gsl_histogram2d_pdf
Packit 67cb25
Packit 67cb25
   ============================= ===========================================================================
Packit 67cb25
   :code:`size_t nx, ny`         This is the number of histogram bins used to approximate the probability
Packit 67cb25
                                 distribution function in the x and y directions.
Packit 67cb25
   :code:`double * xrange`       The ranges of the bins in the x-direction are stored in an array of
Packit 67cb25
                                 :code:`nx + 1` elements pointed to by :data:`xrange`.
Packit 67cb25
   :code:`double * yrange`       The ranges of the bins in the y-direction are stored in an array of
Packit 67cb25
                                 :code:`ny + 1` pointed to by :data:`yrange`.
Packit 67cb25
   :code:`double * sum`          The cumulative probability for the bins is stored in an array of
Packit 67cb25
                                 :data:`nx` * :data:`ny` elements pointed to by :data:`sum`.
Packit 67cb25
   ============================= ===========================================================================
Packit 67cb25
Packit 67cb25
The following functions allow you to create a :type:`gsl_histogram2d_pdf`
Packit 67cb25
struct which represents a two dimensional probability distribution and
Packit 67cb25
generate random samples from it.
Packit 67cb25
Packit 67cb25
.. function:: gsl_histogram2d_pdf * gsl_histogram2d_pdf_alloc (size_t nx, size_t ny)
Packit 67cb25
Packit 67cb25
   This function allocates memory for a two-dimensional probability
Packit 67cb25
   distribution of size :data:`nx`-by-:data:`ny` and returns a pointer to a
Packit 67cb25
   newly initialized :type:`gsl_histogram2d_pdf` struct. If insufficient
Packit 67cb25
   memory is available a null pointer is returned and the error handler is
Packit 67cb25
   invoked with an error code of :macro:`GSL_ENOMEM`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_pdf_init (gsl_histogram2d_pdf * p, const gsl_histogram2d * h)
Packit 67cb25
Packit 67cb25
   This function initializes the two-dimensional probability distribution
Packit 67cb25
   calculated :data:`p` from the histogram :data:`h`.  If any of the bins of
Packit 67cb25
   :data:`h` are negative then the error handler is invoked with an error
Packit 67cb25
   code of :macro:`GSL_EDOM` because a probability distribution cannot
Packit 67cb25
   contain negative values.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_histogram2d_pdf_free (gsl_histogram2d_pdf * p)
Packit 67cb25
Packit 67cb25
   This function frees the two-dimensional probability distribution
Packit 67cb25
   function :data:`p` and all of the memory associated with it.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_histogram2d_pdf_sample (const gsl_histogram2d_pdf * p, double r1, double r2, double * x, double * y)
Packit 67cb25
Packit 67cb25
   This function uses two uniform random numbers between zero and one,
Packit 67cb25
   :data:`r1` and :data:`r2`, to compute a single random sample from the
Packit 67cb25
   two-dimensional probability distribution :data:`p`.
Packit 67cb25
Packit 67cb25
Example programs for 2D histograms
Packit 67cb25
==================================
Packit 67cb25
Packit 67cb25
This program demonstrates two features of two-dimensional histograms.
Packit 67cb25
First a 10-by-10 two-dimensional histogram is created with x and y running
Packit 67cb25
from 0 to 1.  Then a few sample points are added to the histogram, at
Packit 67cb25
(0.3,0.3) with a height of 1, at (0.8,0.1) with a height of 5 and at
Packit 67cb25
(0.7,0.9) with a height of 0.5.  This histogram with three events is
Packit 67cb25
used to generate a random sample of 1000 simulated events, which are
Packit 67cb25
printed out.
Packit 67cb25
Packit 67cb25
.. include:: examples/histogram2d.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
The following plot shows the distribution of the simulated events.  Using
Packit 67cb25
a higher resolution grid we can see the original underlying histogram
Packit 67cb25
and also the statistical fluctuations caused by the events being
Packit 67cb25
uniformly distributed over the area of the original bins.
Packit 67cb25
Packit 67cb25
.. figure:: /images/histogram2d.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Distribution of simulated events from example program