CHistogram:Calc


The Calc method computes the histogram of a data array. If you do not need to preserve the histogram object or results, the THist function may be a simpler way to make a histogram.

Syntax

Table = CHistogram:Calc( Data )

where

    tData is the input data table of sample points.

    tTable is a returned table containing the histogram frequencies.

  

This function returns an array containing the histogram frequencies for the Data array. The histogram is dependent on the binning parameters that control the size of each bin and how many bins are used over what range of values. Several class methods are provided for setting the binning strategy. If you do not configure the binning strategy, then the histogram is computed using the full range of sample data values and a bin count that is the lesser of the square root of the number of sample points and 100.

One reason to use this class and method rather than the THist function is that the class provides access to the actual binning parameters used after the histogram is calculated in "Auto" mode (the default or set using the SetBinAuto method).

Example

The following script computes and plots a histogram of 1 million Gaussian random numbers. The 4th line creates a nice x-axis that makes the distribution symmetrical about 0 and places the center of each bin at the x-coordinate of the bin. Since there are 1,000,000 values in the data sample and the histogram uses auto binning, the number of elements in the histogram array y will be 100 (see above).

H = CHistogram:new()

-- create a CHistogram object

v = TGaussDev( 1000000 )

-- create 1,000,000 values

y = H:Calc( v )

-- compute the histogram

x = TSeries( H:GetBinCount(), H:GetMin()+H:GetBinWidth()/2, H:GetBinWidth() )

PlotStep( x, y, "Sigma's", "Frequency" )

-- plot the histogram

H:delete()

 

This script created the plot shown below.

images/gaussian_histo_2.png

Since this example uses default histogram parameters, a similar "quick and dirty" result could be obtained using the non-class THist function, as follows:

h = THist( TGaussDev(1000000) )

-- compute it

PlotStep( TSeries(table.getn(h)), h )

-- plot h against 1, 2, 3, ...

In this second script, note that the number of points to plot is obtained from the returned histogram table using the built-in table.getn() function on the returned histogram array, h. The TSeries function creates a table of abscissa values in integer steps from 1 to table.getn(h).

Related Topics

SetBinAuto

CHistogram Class

TSeries function

TGaussDev function

PlotConnect function

THist function