Histograms
This chapter describes the functions for creating and using histograms provided by the PLT Scheme Science Collection. Histograms provide a convenient way of summarizing the distribution of a set of data. A histogram contains a vector of bins that count the number of events falling into a given range. The bins of a histogram can be used to record both integer and non-integer distributions.
The ranges of the bins can be either continuous or discrete over a range. For continuous ranges, the width of these ranges can be either fixed or arbitrary. Also, for continuous ranges, both one and two dimensional histograms are supported.
9.1 1D Histograms
The 1D histogram functions described in this section are defined in the histogram.ss file in the science collection and are made available using the following form:
(require (planet "histogram.ss" ("williams" "science.plt")))
Function:
(histogram? x) |
Contract: (-> any? boolean?) |
|
9.1.1 Creating 1D Histograms
Function:
(make-histogram n) |
Contract: (-> (integer-in 1 +inf.0) histogram?) |
|
make-histogram-with-ranges-uniform
Function:
(make-histogram-with-ranges-uniform n x-min x-max) |
Contract: (->r ((n (integer-in 1 +inf.0) (x-min real?) (x-max (>/c x-min))) histogram?) |
|
9.1.2 Updating and Accessing 1D Histogram Elements
Function:
(histogram-n h) |
Contract: (-> histogram? (integer-in 1 +inf.0)) |
|
Function:
(histogram-ranges h) |
Contract: (-> histogram? (vectorof real?)) |
|
Function:
(set-histogram-ranges! h ranges) |
Contract: (-> histogram? (vectorof real?) void?) |
|
Function:
(set-histogram-ranges-uniform! h x-min x-max) |
Contract: (->r ((h histogram?) (x-min real?) (x-max (>/c x-min))) void?) |
|
Function:
(histogram-bins h) |
Contract: (-> histogram? (vectorof real?)) |
|
Function:
(histogram-increment! h x) |
Contract: (-> histogram? real? void?) |
|
Function:
(histogram-accumulate! h x weight) |
Contract: (-> histogram? real (>-/c 0.0) void?) |
|
Function:
(histogram-get h i) |
Contract: (-> histogram? natural-number? (>=/c 0.0)) |
|
Function:
(histogram-get-range h i) |
Contract: (-> histogram? natural-number? (values real? real?)) |
|
9.1.3 1D Histogram Statistics
Function:
(histogram-max h) |
Contract: (-> histogram? (>=/c 0.0)) |
|
Function:
(histogram-min h) |
Contract: (-> histogram? (>=/c 0.0)) |
|
Function:
(histogram-mean h) |
Contract: (-> histogram? (>=/c 0.0)) |
|
Function:
(histogram-sigma h) |
Contract: (-> histogram? (>=/c 0.0)) |
|
Function:
(histogram-sum h) |
Contract: (-> histogram? (>=/c 0.0)) |
|
9.1.4 1D Histogram Graphics
The histogram graphics functions are defined in the file histogram-graphics.ss in the science collection and are made available using the following form:
(require (planet "histogram-graphics.ss" ("williams" "science.plt")))
Function:
(histogram-plot h title) (histogram-plot h) |
Contract: (case-> (-> histogram? string? any) (-> histogram? any)) |
|
Function:
(histogram-plot-scaled h title) (histogram-plot-scaled h) |
Contract: (case-> (-> histogram? string? any) (-> histogram? any)) |
|
9.1.5 Examples
Example: Plot of histogram of random variates from the unit Gaussian (normal) distribution.
(require (planet "gaussian.ss" ("williams" "science.plt") "random-distributions")) (require (planet "histogram-with-graphics.ss" ("williams" "science.plt"))) (let ((h (make-histogram-with-ranges-uniform 40 -3.0 3.0))) (do ((i 0 (+ i 1))) ((= i 10000) (void)) (histogram-increment! h (random-unit-gaussian))) (histogram-plot h "Histogram of Unit Gaussian Distribution"))
Figure 62 shows the resulting histogram.
|
Example: Scaled plot of histogram of random variates from the exponential distribution with mean 1.0.
(require (planet "exponential.ss" ("williams" "science.plt") "random-distributions")) (require (planet "histogram-with-graphics.ss" ("williams" "science.plt"))) (let ((h (make-histogram-with-ranges-uniform 10 0.0 8.0))) (do ((i 0 (+ i 1))) ((= i 10000) (void)) (histogram-increment! h (random-exponential 1.0))) (histogram-plot-scaled h "Scaled Histogram of Exponential Distribution"))
Figure 63 shows the resulting histogram.
|
9.2 2D Histograms
The 2D histogram functions described in this chapter are defined in the histogram-2d .ss file in the science collection and are made available using the following form:
(require (planet "histogram-2d.ss" ("williams" "science.plt")))
Function:
(histogram-2d? x) |
Contract: (-> any? boolean?) |
|
9.2.1 Creating 2D Histograms
Function:
(make-histogram-2d nx ny) |
Contract: (-> (integer-in 1 +inf.0) (integer-in 1 +inf.0) histogram-2d?) |
|
make-histogram-2d-with-ranges-uniform
Function:
(make-histogram-2d-with-ranges-uniform nx ny x-min x-max y-min y-max) |
Contract: (->r ((nx (integer-in 1 +inf.0)) (ny (integer-in 1 +inf.0)) (x-min real?) (x-max (>/c x-min)) (y-min real?) (y-max (>/c y-min))) histogram-2d?) |
|
9.2.2 Updating and Accessing 2D Histogram Elements
Function:
(histogram-2d-nx h) |
Contract: (-> histogram-2d? (integer-in 1 +inf.0)) |
|
Function:
(histogram-2d-ny h) |
Contract: (-> histogram-2d? (integer-in 1 +inf.0)) |
|
Function:
(histogram-2d-x-ranges h) |
Contract: (-> histogram-2d? (vectorof real?)) |
|
Function:
(histogram-2d-y-ranges h) |
Contract: (-> histogram-2d? (vectorof real?)) |
|
Function:
(set-histogram-2d-ranges! h x-ranges y-ranges) |
Contract: (-> histogram-2d? (vectorof real?) (vectorof real?) void?) |
|
set-histogram-2d-ranges-uniform!
Function:
(set-histogram-2d-ranges-uniform! h x-min x-max y-min y-max) |
Contract: (->r ((h histogram-2d?) (x-min real?) (x-max (>/c x-min)) (y-min real?) (y-max (>/c y-max))) void?) |
|
Function:
(histogram-bins h) |
Contract: (-> histogram-2d? (vectorof real?)) |
|
Function:
(histogram-increment! h x y) |
Contract: (-> histogram-2d? real? real? void?) |
|
Function:
(histogram-2daccumulate! h x y weight) |
Contract: (-> histogram-2d? real? real? (>-/c 0.0) void?) |
|
Function:
(histogram-2d-get h i j) |
Contract: (-> histogram-2d? natural-number? natural-number? (>=/c 0.0)) |
|
Function:
(histogram-2d-get-x-range h i j) |
Contract: (-> histogram-2d? natural-number? natural-number? (values real? real?)) |
|
Function:
(histogram-2d-get-y-range h i j) |
Contract: (-> histogram-2d? natural-number? natural-number? (values real? real?)) |
|
9.2.3 2D Histogram Statistics
Function:
(histogram-2d-max h) |
Contract: (-> histogram-2d? (>=/c 0.0)) |
|
Function:
(histogram-2d-min h) |
Contract: (-> histogram-2d? (>=/c 0.0)) |
|
Function:
(histogram-2d-sum h) |
Contract: (-> histogram-2d? (>=/c 0.0)) |
|
Function:
(histogram-2d-x-mean h) |
Contract: (-> histogram-2d? (>=/c 0.0)) |
|
Function:
(histogram-2d-y-mean h) |
Contract: (-> histogram-2d? (>=/c 0.0)) |
|
Function:
(histogram-2d-x-sigma h) |
Contract: (-> histogram-2d? (>=/c 0.0)) |
|
Function:
(histogram-2d-y-sigma h) |
Contract: (-> histogram-2d? (>=/c 0.0)) |
|
Function:
(histogram-2d-covariance h) |
Contract: (-> histogram-2d? (>=/c 0.0)) |
|
9.2.4 2D Histogram Graphics
The 2D histogram graphics functions are defined in the file histogram-2d-graphics.ss in the science collection and are made available using the following form:
(require (planet "histogram-2d-graphics.ss" ("williams" "science.plt" 2 0)))
Function:
(histogram-2d-plot h title) (histogram-2d-plot h) |
Contract: (case-> (-> histogram-2d? string? any) (-> histogram-2d? any)) |
|
9.2.5 Example
Example: Plot of 2D histogram of random variates from the bivariate Gaussian distribution standard deviations 1.0 and 1.0 in the x and y directions and correlation coefficient 0.0.
(require (planet "bivariate-gaussian.ss" ("williams" "science.plt") "random-distributions")) (require (planet "histogram-2d-with-graphics.ss" ("williams" "science.plt"))) (let ((h (make-histogram-2d-with-ranges-uniform 20 20 -3.0 3.0 -3.0 3.0))) (do ((i 0 (+ i 1))) ((= i 10000) (void)) (let-values (((x y) (random-bivariate-gaussian 1.0 1.0 0.0))) (histogram-2d-increment! h x y))) (histogram-2d-plot h "Histogram of Bivariate Gaussian Distribution"))
Figure 64 shows the resulting 2D histogram.
|
9.3 Discrete Histograms
The discrete histogram functions described in this section are defined in the discrete-histogram.ss file in the science collection and are made available using the following form:
(require (planet "discrete-histogram.ss" ("williams" "science.plt")))
Function:
(discrete-histogram? x) |
Contract: (-> any? boolean?) |
|
9.3.1 Creating Discrete Histograms
Function:
(make-discrete-histogram n1 n2 dynamic?) (make-discrete-histogram n1 n2) (make-discrete-histogram) |
Contract: (case-> (->r ((n1 integer?) (n2 (and/c integer? (>=/c n1))) (dynamic? boolean?)) discrete-histogram?) (->r ((n1 integer?) (n2 (and/c integer? (>=/c n1)))) discrete-histogram?) (->r discrete-histogram?))) |
|
9.3.2 Updating and Accessing Discrete Histogram Elements
Function:
(discrete-histogram-n1 h) |
Contract: (-> discrete-histogram? integer?) |
|
Function:
(discrete-histogram-n2 h) |
Contract: (-> discrete-histogram? integer?) |
|
Function:
(discrete-histogram-dynamic? h) |
Contract: (-> discrete-histogram? boolean?) |
|
Function:
(discrete-histogram-bins h) |
Contract: (-> discrete-histogram? (vectorof real?)) |
|
Function:
(discrete-histogram-increment! h i) |
Contract: (-> discrete-histogram? integer? void?) |
|
discrete-histogram-accumulate!
Function:
(discrete-histogram-accumulate! h i weight) |
Contract: (-> discrete-histogram? integer? (>-/c 0.0) void?) |
|
Function:
(discrete-histogram-get h i) |
Contract: (-> discrete-histogram? integer? (>=/c 0.0)) |
|
9.3.3 Discrete Histogram Statistics
Function:
(discrete-histogram-max h) |
Contract: (-> discrete-histogram? (>=/c 0.0)) |
|
Function:
(discrete-histogram-min h) |
Contract: (-> discrete-histogram? (>=/c 0.0)) |
|
Function:
(discrete-histogram-sum h) |
Contract: (-> discrete-histogram? (>=/c 0.0)) |
|
9.3.4 Discrete Histogram Graphics
The discrete histogram graphics functions are defined in the file discrete-histogram-graphics.ss in the science collection and are made available using the following form:
(require (planet "discrete-histogram-graphics.ss" ("williams" "science.plt" 2 0)))
Function:
(discrete-histogram-plot h title) (discrete-histogram-plot h) |
Contract: (case-> (-> discrete-histogram? string? any) (-> discrete-histogram? any)) |
|
discrete-histogram-plot-scaled
Function:
(discrete-histogram-plot-scaled h title) (discrete-histogram-plot-scaled h) |
Contract: (case-> (-> discrete-histogram? string? any) (-> discrete-histogram? any)) |
|
9.3.5 Examples
Example: Plot of discrete histogram of random variates from the Poisson distribution with mean 10.0.
(require (planet "poisson.ss" ("williams" "science.plt") "random-distributions")) (require (planet "discrete-histogram-with-graphics.ss" ("williams" "science.plt"))) (let ((h (make-discrete-histogram))) (do ((i 0 (+ i 1))) ((= i 10000) (void)) (discrete-histogram-increment! h (random-poisson 10.0))) (discrete-histogram-plot h "Histogram of Poisson Distribution"))
Figure 65 shows the resulting histogram.
|
Example: Scaled plot of discrete histogram of random variates from the logarithmic distribution with probability 0.5.
(require (planet "logarithmic.ss" ("williams" "science.plt") "random-distributions")) (require (planet "discrete-histogram-with-graphics.ss" ("williams" "science.plt"))) (let ((h (make-discrete-histogram))) (do ((i 0 (+ i 1))) ((= i 10000) (void)) (discrete-histogram-increment! h (random-logarithmic 0.5))) (discrete-histogram-plot-scaled h "Histogram of Logarithmic Distribution"))
Figure 66 shows the resulting histogram.
|