Scroll to navigation

Math::GSL::Histogram(3pm) User Contributed Perl Documentation Math::GSL::Histogram(3pm)

NAME

Math::GSL::Histogram - Create and manipulate histograms of data

SYNOPSIS

    use Math::GSL::Histogram qw/:all/;
    my $H = gsl_histogram_alloc(100);
    gsl_histogram_set_ranges_uniform($H,0,101);
    gsl_histogram_increment($H, -50 );  # ignored
    gsl_histogram_increment($H, 70 );
    gsl_histogram_increment($H, 85.2 );
    my $G = gsl_histogram_clone($H);
    my $value = gsl_histogram_get($G, 70);
    my ($max,$min) = (gsl_histogram_min_val($H), gsl_histogram_max_val($H) );
    my $sum = gsl_histogram_sum($H);

DESCRIPTION

Here is a list of all the functions included in this module :

"gsl_histogram_calloc "
"gsl_histogram_calloc_uniform "
"gsl_histogram_increment($h, $x)" - This function updates the histogram $h by adding one (1.0) to the bin whose range contains the coordinate $x. If $x lies in the valid range of the histogram then the function returns zero to indicate success. If $x is less than the lower limit of the histogram then the function returns $GSL_EDOM, and none of bins are modified. Similarly, if the value of $x is greater than or equal to the upper limit of the histogram then the function returns $GSL_EDOM, and none of the bins are modified. The error handler is not called, however, since it is often necessary to compute histograms for a small range of a larger dataset, ignoring the values outside the range of interest.
"gsl_histogram_accumulate($h, $x, $weight)" - This function is similar to gsl_histogram_increment but increases the value of the appropriate bin in the histogram $h by the floating-point number weight.
"gsl_histogram_find($h, $x)" - This function finds the bin number which covers the coordinate $x in the histogram $h. The bin is located using a binary search. The search includes an optimization for histograms with uniform range, and will return the correct bin immediately in this case. If $x is found in the range of the histogram then the function returns the bin number and returns $GSL_SUCCESS. If $x lies outside the valid range of the histogram then the function returns $GSL_EDOM and the error handler is invoked.
"gsl_histogram_get($h, $i)" - This function returns the contents of the $i-th bin of the histogram $h. If $i lies outside the valid range of indices for the histogram then the error handler is called with an error code of GSL_EDOM and the function returns 0.
"gsl_histogram_get_range($h, $i)" - This function finds the upper and lower range limits of the $i-th bin of the histogram $h. If the index $i is valid then the corresponding range limits are returned after the 0 in this order : lower and then upper. The lower limit is inclusive (i.e. events with this coordinate are included in the bin) and the upper limit is exclusive (i.e. events with the coordinate of the upper limit are excluded and fall in the neighboring higher bin, if it exists). The function returns 0 to indicate success. If i lies outside the valid range of indices for the histogram then the error handler is called and the function returns an error code of $GSL_EDOM.
"gsl_histogram_calloc_range"
"gsl_histogram_set_ranges($h, $range, $size)" - This function sets the ranges of the existing histogram $h using the array $range of size $size. The values of the histogram bins are reset to zero. The $range array should contain the desired bin limits. The ranges can be arbitrary, subject to the restriction that they are monotonically increasing. Note that the size of the $range array should be defined to be one element bigger than the number of bins. The additional element is required for the upper value of the final bin.
"gsl_histogram_set_ranges_uniform($h, $xmin, $xmax)" - This function sets the ranges of the existing histogram $h to cover the range $xmin to $xmax uniformly. The values of the histogram bins are reset to zero. The bin ranges are shown in the table below,
......

where d is the bin spacing, d = (xmax-xmin)/n.

"gsl_histogram_memcpy($dest, $src)" - This function copies the histogram $src into the pre-existing histogram $dest, making $dest into an exact copy of $src. The two histograms must be of the same size.
"gsl_histogram_equal_bins_p($h1, $h2)" - This function returns 1 if the all of the individual bin ranges of the two histograms are identical, and 0 otherwise.
"gsl_histogram_add($h1, $h2)" - This function adds the contents of the bins in histogram $h2 to the corresponding bins of histogram $h1, i.e. h'_1(i) = h_1(i) + h_2(i). The two histograms must have identical bin ranges.
"gsl_histogram_sub($h1, $h2)" - This function subtracts the contents of the bins in histogram $h2 from the corresponding bins of histogram $h1, i.e. h'_1(i) = h_1(i) - h_2(i). The two histograms must have identical bin ranges.
"gsl_histogram_mul($h1, $h2)" - This function multiplies the contents of the bins of histogram $h1 by the contents of the corresponding bins in histogram $h2, i.e. h'_1(i) = h_1(i) * h_2(i). The two histograms must have identical bin ranges.
"gsl_histogram_div($h1, $h2)" - This function divides the contents of the bins of histogram $h1 by the contents of the corresponding bins in histogram $h2, i.e. h'_1(i) = h_1(i) / h_2(i). The two histograms must have identical bin ranges.
"gsl_histogram_scale($h, $scale)" - This function multiplies the contents of the bins of histogram $h by the constant $scale, i.e. h'_1(i) = h_1(i) * scale.
"gsl_histogram_shift($h, $offset)" - This function shifts the contents of the bins of histogram $h by the constant $offset, i.e. h'_1(i) = h_1(i) + offset.
"gsl_histogram_fwrite($stream, $h)" - This function writes the ranges and bins of the histogram $h to the stream $stream, which has been opened by the gsl_fopen function from the Math::GSL module, in binary format. The return value is 0 for success and $GSL_EFAILED if there was a problem writing to the file. Since the data is written in the native binary format it may not be portable between different architectures.
"gsl_histogram_fread($stream, $h)" - This function reads into the histogram $h from the open stream $stream, which has been opened by the gsl_fopen function from the Math::GSL module, in binary format. The histogram $h must be preallocated with the correct size since the function uses the number of bins in $h to determine how many bytes to read. The return value is 0 for success and $GSL_EFAILED if there was a problem reading from the file. The data is assumed to have been written in the native binary format on the same architecture.
"gsl_histogram_fprintf($stream, $h, $range_format, $bin_format)" - This function writes the ranges and bins of the histogram $h line-by-line to the stream $stream (from the gsl_fopen function from the Math::GSL module) using the format specifiers $range_format and $bin_format. These should be one of the %g, %e or %f formats for floating point numbers. The function returns 0 for success and $GSL_EFAILED if there was a problem writing to the file. The histogram output is formatted in three columns, and the columns are separated by spaces, like this,

The values of the ranges are formatted using range_format and the value of the bins are formatted using bin_format. Each line contains the lower and upper limit of the range of the bins and the value of the bin itself. Since the upper limit of one bin is the lower limit of the next there is duplication of these values between lines but this allows the histogram to be manipulated with line-oriented tools.

"gsl_histogram_fscanf($stream, $h)" - This function reads formatted data from the stream $stream, which has been opened by the gsl_fopen function from the Math::GSL module, into the histogram $h. The data is assumed to be in the three-column format used by gsl_histogram_fprintf. The histogram $h must be preallocated with the correct length since the function uses the size of $h to determine how many numbers to read. The function returns 0 for success and $GSL_EFAILED if there was a problem reading from the file.
"gsl_histogram_pdf_init($p, $h)" - This function initializes the probability distribution $p with the contents of the histogram $h. If any of the bins of $h are negative then the error handler is invoked with an error code of $GSL_EDOM because a probability distribution cannot contain negative values.
"gsl_histogram_pdf_sample($p, $r)" - This function uses $r, a uniform random number between zero and one, to compute a single random sample from the probability distribution $p. The algorithm used to compute the sample s is given by the following formula, s = range[i] + delta * (range[i+1] - range[i]) where i is the index which satisfies sum[i] <= r < sum[i+1] and delta is (r - sum[i])/(sum[i+1] - sum[i]).

EXAMPLES

 The following example shows how to create a histogram with logarithmic bins with ranges [1,10), [10,100) and [100,1000).
 $h = gsl_histogram_alloc (3);
 # bin[0] covers the range 1 <= x < 10
 # bin[1] covers the range 10 <= x < 100
 # bin[2] covers the range 100 <= x < 1000
 $range = [ 1.0, 10.0, 100.0, 1000.0 ];
 gsl_histogram_set_ranges($h, $range, 4);

AUTHORS

Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

2024-07-26 perl v5.38.2