summaryrefslogtreecommitdiff
path: root/gsl-1.9/doc/histogram.texi
blob: f2fe423bfe500d34890c73c9de237c9ebaa8e619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
@cindex histograms
@cindex binning data
This chapter describes functions for creating histograms.  Histograms
provide a convenient way of summarizing the distribution of a set of
data. A histogram consists of a set of @dfn{bins} which count the number
of events falling into a given range of a continuous variable @math{x}.
In GSL the bins of a histogram contain floating-point numbers, so they
can be used to record both integer and non-integer distributions.  The
bins can use arbitrary sets of ranges (uniformly spaced bins are the
default).  Both one and two-dimensional histograms are supported.

Once a histogram has been created it can also be converted into a
probability distribution function.  The library provides efficient
routines for selecting random samples from probability distributions.
This can be useful for generating simulations based on real data.

The functions are declared in the header files @file{gsl_histogram.h}
and @file{gsl_histogram2d.h}.

@menu
* The histogram struct::        
* Histogram allocation::        
* Copying Histograms::          
* Updating and accessing histogram elements::  
* Searching histogram ranges::  
* Histogram Statistics::        
* Histogram Operations::        
* Reading and writing histograms::  
* Resampling from histograms::  
* The histogram probability distribution struct::  
* Example programs for histograms::  
* Two dimensional histograms::  
* The 2D histogram struct::     
* 2D Histogram allocation::     
* Copying 2D Histograms::       
* Updating and accessing 2D histogram elements::  
* Searching 2D histogram ranges::  
* 2D Histogram Statistics::     
* 2D Histogram Operations::     
* Reading and writing 2D histograms::  
* Resampling from 2D histograms::  
* Example programs for 2D histograms::  
@end menu

@node The histogram struct
@section The histogram struct

A histogram is defined by the following struct,

@deftp {Data Type} {gsl_histogram}
@table @code
@item size_t n
This is the number of histogram bins
@item double * range
The ranges of the bins are stored in an array of @math{@var{n}+1} elements
pointed to by @var{range}.
@item double * bin
The counts for each bin are stored in an array of @var{n} elements
pointed to by @var{bin}.  The bins are floating-point numbers, so you can
increment them by non-integer values if necessary.
@end table
@end deftp
@comment 

@noindent
The range for @var{bin}[i] is given by @var{range}[i] to
@var{range}[i+1].  For @math{n} bins there are @math{n+1} entries in the
array @var{range}.  Each bin is inclusive at the lower end and exclusive
at the upper end.  Mathematically this means that the bins are defined by
the following inequality,
@tex
\beforedisplay
$$
\hbox{bin[i] corresponds to range[i]} \le x < \hbox{range[i+1]}
$$
\afterdisplay
@end tex
@ifinfo
@display
bin[i] corresponds to range[i] <= x < range[i+1]
@end display

@end ifinfo
@noindent
Here is a diagram of the correspondence between ranges and bins on the
number-line for @math{x},

@smallexample

     [ bin[0] )[ bin[1] )[ bin[2] )[ bin[3] )[ bin[4] )
  ---|---------|---------|---------|---------|---------|---  x
   r[0]      r[1]      r[2]      r[3]      r[4]      r[5]

@end smallexample

@noindent
In this picture the values of the @var{range} array are denoted by
@math{r}.  On the left-hand side of each bin the square bracket
@samp{[} denotes an inclusive lower bound 
(@c{$r \le x$}
@math{r <= x}), and the round parentheses @samp{)} on the right-hand
side denote an exclusive upper bound (@math{x < r}).  Thus any samples
which fall on the upper end of the histogram are excluded.  If you want
to include this value for the last bin you will need to add an extra bin
to your histogram.

The @code{gsl_histogram} struct and its associated functions are defined
in the header file @file{gsl_histogram.h}.

@node Histogram allocation
@section Histogram allocation
The functions for allocating memory to a histogram follow the style of
@code{malloc} and @code{free}.  In addition they also perform their own
error checking.  If there is insufficient memory available to allocate a
histogram then the functions call the error handler (with an error
number of @code{GSL_ENOMEM}) in addition to returning a null pointer.
Thus if you use the library error handler to abort your program then it
isn't necessary to check every histogram @code{alloc}.

@deftypefun {gsl_histogram *} gsl_histogram_alloc (size_t @var{n})
This function allocates memory for a histogram with @var{n} bins, and
returns a pointer to a newly created @code{gsl_histogram} struct.  If
insufficient memory is available a null pointer is returned and the
error handler is invoked with an error code of @code{GSL_ENOMEM}. The
bins and ranges are not initialized, and should be prepared using one of
the range-setting functions below in order to make the histogram ready
for use.
@end deftypefun

@comment @deftypefun {gsl_histogram *} gsl_histogram_calloc (size_t @var{n})
@comment This function allocates memory for a histogram with @var{n} bins, and
@comment returns a pointer to its newly initialized @code{gsl_histogram} struct.
@comment The bins are uniformly spaced with a total range of 
@comment @c{$0 \le  x < n$}
@comment @math{0 <=  x < n},
@comment as shown in the table below.

@comment @tex
@comment \beforedisplay
@comment $$
@comment \matrix{
@comment \hbox{bin[0]}&\hbox{corresponds to}& 0 \le x < 1\cr
@comment \hbox{bin[1]}&\hbox{corresponds to}& 1 \le x < 2\cr
@comment \dots&\dots&\dots\cr
@comment \hbox{bin[n-1]}&\hbox{corresponds to}&n-1 \le x < n}
@comment $$
@comment \afterdisplay
@comment @end tex
@comment @ifinfo
@comment @display
@comment bin[0] corresponds to 0 <= x < 1
@comment bin[1] corresponds to 1 <= x < 2
@comment @dots{}
@comment bin[n-1] corresponds to n-1 <= x < n
@comment @end display
@comment @end ifinfo
@comment @noindent
@comment The bins are initialized to zero so the histogram is ready for use.

@comment If insufficient memory is available a null pointer is returned and the
@comment error handler is invoked with an error code of @code{GSL_ENOMEM}.
@comment @end deftypefun

@comment @deftypefun {gsl_histogram *} gsl_histogram_calloc_uniform (size_t @var{n}, double @var{xmin}, double @var{xmax})
@comment This function allocates memory for a histogram with @var{n} uniformly
@comment spaced bins from @var{xmin} to @var{xmax}, and returns a pointer to the
@comment newly initialized @code{gsl_histogram} struct. 
@comment If insufficient memory is available a null pointer is returned and the
@comment error handler is invoked with an error code of @code{GSL_ENOMEM}.
@comment @end deftypefun

@comment @deftypefun {gsl_histogram *} gsl_histogram_calloc_range (size_t @var{n}, double * @var{range})
@comment This function allocates a histogram of size @var{n} using the @math{n+1}
@comment bin ranges specified by the array @var{range}.
@comment @end deftypefun

@deftypefun int gsl_histogram_set_ranges (gsl_histogram * @var{h}, const double @var{range}[], size_t @var{size})
This function sets the ranges of the existing histogram @var{h} using
the array @var{range} of size @var{size}.  The values of the histogram
bins are reset to zero.  The @code{range} array should contain the
desired bin limits.  The ranges can be arbitrary, subject to the
restriction that they are monotonically increasing.

The following example shows how to create a histogram with logarithmic
bins with ranges [1,10), [10,100) and [100,1000).

@example
gsl_histogram * 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 */

double range[4] = @{ 1.0, 10.0, 100.0, 1000.0 @};

gsl_histogram_set_ranges (h, range, 4);
@end example

@noindent
Note that the size of the @var{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.
@end deftypefun

@deftypefun int gsl_histogram_set_ranges_uniform (gsl_histogram * @var{h}, double @var{xmin}, double @var{xmax})
This function sets the ranges of the existing histogram @var{h} to cover
the range @var{xmin} to @var{xmax} uniformly.  The values of the
histogram bins are reset to zero.  The bin ranges are shown in the table
below,
@tex
\beforedisplay
$$
\matrix{\hbox{bin[0]}&\hbox{corresponds to}& xmin \le  x < xmin + d\cr
\hbox{bin[1]} &\hbox{corresponds to}& xmin + d \le  x < xmin + 2 d\cr
\dots&\dots&\dots\cr
\hbox{bin[n-1]} & \hbox{corresponds to}& xmin + (n-1)d \le  x < xmax}
$$
\afterdisplay
@end tex
@ifinfo
@display
bin[0] corresponds to xmin <= x < xmin + d
bin[1] corresponds to xmin + d <= x < xmin + 2 d
......
bin[n-1] corresponds to xmin + (n-1)d <= x < xmax
@end display

@end ifinfo
@noindent
where @math{d} is the bin spacing, @math{d = (xmax-xmin)/n}.
@end deftypefun

@deftypefun void gsl_histogram_free (gsl_histogram * @var{h})
This function frees the histogram @var{h} and all of the memory
associated with it.
@end deftypefun

@node Copying Histograms
@section Copying Histograms

@deftypefun int gsl_histogram_memcpy (gsl_histogram * @var{dest}, const gsl_histogram * @var{src})
This function copies the histogram @var{src} into the pre-existing
histogram @var{dest}, making @var{dest} into an exact copy of @var{src}.
The two histograms must be of the same size.
@end deftypefun

@deftypefun {gsl_histogram *} gsl_histogram_clone (const gsl_histogram * @var{src})
This function returns a pointer to a newly created histogram which is an
exact copy of the histogram @var{src}.
@end deftypefun

@node Updating and accessing histogram elements
@section Updating and accessing histogram elements

There are two ways to access histogram bins, either by specifying an
@math{x} coordinate or by using the bin-index directly.  The functions
for accessing the histogram through @math{x} coordinates use a binary
search to identify the bin which covers the appropriate range.

@deftypefun int gsl_histogram_increment (gsl_histogram * @var{h}, double @var{x})
This function updates the histogram @var{h} by adding one (1.0) to the
bin whose range contains the coordinate @var{x}. 

If @var{x} lies in the valid range of the histogram then the function
returns zero to indicate success.  If @var{x} is less than the lower
limit of the histogram then the function returns @code{GSL_EDOM}, and
none of bins are modified.  Similarly, if the value of @var{x} is greater
than or equal to the upper limit of the histogram then the function
returns @code{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.
@end deftypefun

@deftypefun int gsl_histogram_accumulate (gsl_histogram * @var{h}, double @var{x}, double @var{weight})
This function is similar to @code{gsl_histogram_increment} but increases
the value of the appropriate bin in the histogram @var{h} by the
floating-point number @var{weight}.
@end deftypefun

@deftypefun double gsl_histogram_get (const gsl_histogram * @var{h}, size_t @var{i})
This function returns the contents of the @var{i}-th bin of the histogram
@var{h}.  If @var{i} lies outside the valid range of indices for the
histogram then the error handler is called with an error code of
@code{GSL_EDOM} and the function returns 0.
@end deftypefun

@deftypefun int gsl_histogram_get_range (const gsl_histogram * @var{h}, size_t @var{i}, double * @var{lower}, double * @var{upper})
This function finds the upper and lower range limits of the @var{i}-th
bin of the histogram @var{h}.  If the index @var{i} is valid then the
corresponding range limits are stored in @var{lower} and @var{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 @var{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 @code{GSL_EDOM}.
@end deftypefun

@deftypefun double gsl_histogram_max (const gsl_histogram * @var{h})
@deftypefunx double gsl_histogram_min (const gsl_histogram * @var{h})
@deftypefunx size_t gsl_histogram_bins (const gsl_histogram * @var{h})
These functions return the maximum upper and minimum lower range limits
and the number of bins of the histogram @var{h}.  They provide a way of
determining these values without accessing the @code{gsl_histogram}
struct directly.
@end deftypefun

@deftypefun void gsl_histogram_reset (gsl_histogram * @var{h})
This function resets all the bins in the histogram @var{h} to zero.
@end deftypefun

@node Searching histogram ranges
@section Searching histogram ranges

The following functions are used by the access and update routines to
locate the bin which corresponds to a given @math{x} coordinate.

@deftypefun int gsl_histogram_find (const gsl_histogram * @var{h}, double @var{x}, size_t * @var{i})
This function finds and sets the index @var{i} to the bin number which
covers the coordinate @var{x} in the histogram @var{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 @var{x} is found in the range of the
histogram then the function sets the index @var{i} and returns
@code{GSL_SUCCESS}.  If @var{x} lies outside the valid range of the
histogram then the function returns @code{GSL_EDOM} and the error
handler is invoked.
@end deftypefun

@node Histogram Statistics
@section Histogram Statistics
@cindex histogram statistics
@cindex statistics, from histogram
@cindex maximum value, from histogram
@cindex minimum value, from histogram
@deftypefun double gsl_histogram_max_val (const gsl_histogram * @var{h})
This function returns the maximum value contained in the histogram bins.
@end deftypefun

@deftypefun size_t gsl_histogram_max_bin (const gsl_histogram * @var{h})
This function returns the index of the bin containing the maximum
value. In the case where several bins contain the same maximum value the
smallest index is returned.
@end deftypefun

@deftypefun double gsl_histogram_min_val (const gsl_histogram * @var{h})
This function returns the minimum value contained in the histogram bins.
@end deftypefun

@deftypefun size_t gsl_histogram_min_bin (const gsl_histogram * @var{h})
This function returns the index of the bin containing the minimum
value. In the case where several bins contain the same maximum value the
smallest index is returned.
@end deftypefun

@cindex mean value, from histogram
@deftypefun double gsl_histogram_mean (const gsl_histogram * @var{h})
This function returns the mean of the histogrammed variable, where the
histogram is regarded as a probability distribution. Negative bin values
are ignored for the purposes of this calculation.  The accuracy of the
result is limited by the bin width.
@end deftypefun

@cindex standard deviation, from histogram
@cindex variance, from histogram
@deftypefun double gsl_histogram_sigma (const gsl_histogram * @var{h})
This function returns the standard deviation of the histogrammed
variable, where the histogram is regarded as a probability
distribution. Negative bin values are ignored for the purposes of this
calculation. The accuracy of the result is limited by the bin width.
@end deftypefun

@deftypefun double gsl_histogram_sum (const gsl_histogram * @var{h})
This function returns the sum of all bin values. Negative bin values
are included in the sum.
@end deftypefun

@node Histogram Operations
@section Histogram Operations

@deftypefun int gsl_histogram_equal_bins_p (const gsl_histogram * @var{h1}, const gsl_histogram * @var{h2})
This function returns 1 if the all of the individual bin
ranges of the two histograms are identical, and 0
otherwise.
@end deftypefun

@deftypefun int gsl_histogram_add (gsl_histogram * @var{h1}, const gsl_histogram * @var{h2})
This function adds the contents of the bins in histogram @var{h2} to the
corresponding bins of histogram @var{h1},  i.e. @math{h'_1(i) = h_1(i) +
h_2(i)}.  The two histograms must have identical bin ranges.
@end deftypefun

@deftypefun int gsl_histogram_sub (gsl_histogram * @var{h1}, const gsl_histogram * @var{h2})
This function subtracts the contents of the bins in histogram @var{h2}
from the corresponding bins of histogram @var{h1}, i.e. @math{h'_1(i) =
h_1(i) - h_2(i)}.  The two histograms must have identical bin ranges.
@end deftypefun

@deftypefun int gsl_histogram_mul (gsl_histogram * @var{h1}, const gsl_histogram * @var{h2})
This function multiplies the contents of the bins of histogram @var{h1}
by the contents of the corresponding bins in histogram @var{h2},
i.e. @math{h'_1(i) = h_1(i) * h_2(i)}.  The two histograms must have
identical bin ranges.
@end deftypefun

@deftypefun int gsl_histogram_div (gsl_histogram * @var{h1}, const gsl_histogram * @var{h2})
This function divides the contents of the bins of histogram @var{h1} by
the contents of the corresponding bins in histogram @var{h2},
i.e. @math{h'_1(i) = h_1(i) / h_2(i)}.  The two histograms must have
identical bin ranges.
@end deftypefun

@deftypefun int gsl_histogram_scale (gsl_histogram * @var{h}, double @var{scale})
This function multiplies the contents of the bins of histogram @var{h}
by the constant @var{scale}, i.e. @c{$h'_1(i) = h_1(i) * \hbox{\it scale}$}
@math{h'_1(i) = h_1(i) * scale}.
@end deftypefun

@deftypefun int gsl_histogram_shift (gsl_histogram * @var{h}, double @var{offset})
This function shifts the contents of the bins of histogram @var{h} by
the constant @var{offset}, i.e. @c{$h'_1(i) = h_1(i) + \hbox{\it offset}$}
@math{h'_1(i) = h_1(i) + offset}.
@end deftypefun

@node Reading and writing histograms
@section Reading and writing histograms

The library provides functions for reading and writing histograms to a file
as binary data or formatted text.

@deftypefun int gsl_histogram_fwrite (FILE * @var{stream}, const gsl_histogram * @var{h})
This function writes the ranges and bins of the histogram @var{h} to the
stream @var{stream} in binary format.  The return value is 0 for success
and @code{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.
@end deftypefun

@deftypefun int gsl_histogram_fread (FILE * @var{stream}, gsl_histogram * @var{h})
This function reads into the histogram @var{h} from the open stream
@var{stream} in binary format.  The histogram @var{h} must be
preallocated with the correct size since the function uses the number of
bins in @var{h} to determine how many bytes to read.  The return value is
0 for success and @code{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.
@end deftypefun

@deftypefun int gsl_histogram_fprintf (FILE * @var{stream}, const gsl_histogram * @var{h}, const char * @var{range_format}, const char * @var{bin_format})
This function writes the ranges and bins of the histogram @var{h}
line-by-line to the stream @var{stream} using the format specifiers
@var{range_format} and @var{bin_format}.  These should be one of the
@code{%g}, @code{%e} or @code{%f} formats for floating point
numbers.  The function returns 0 for success and @code{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,

@example
range[0] range[1] bin[0]
range[1] range[2] bin[1]
range[2] range[3] bin[2]
....
range[n-1] range[n] bin[n-1]
@end example

@noindent
The values of the ranges are formatted using @var{range_format} and the
value of the bins are formatted using @var{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.
@end deftypefun

@deftypefun int gsl_histogram_fscanf (FILE * @var{stream}, gsl_histogram * @var{h})
This function reads formatted data from the stream @var{stream} into the
histogram @var{h}.  The data is assumed to be in the three-column format
used by @code{gsl_histogram_fprintf}.  The histogram @var{h} must be
preallocated with the correct length since the function uses the size of
@var{h} to determine how many numbers to read.  The function returns 0
for success and @code{GSL_EFAILED} if there was a problem reading from
the file.
@end deftypefun

@node Resampling from histograms
@section Resampling from histograms
@cindex resampling from histograms
@cindex sampling from histograms
@cindex probability distributions, from histograms

A histogram made by counting events can be regarded as a measurement of
a probability distribution.  Allowing for statistical error, the height
of each bin represents the probability of an event where the value of
@math{x} falls in the range of that bin.  The probability distribution
function has the one-dimensional form @math{p(x)dx} where,
@tex
\beforedisplay
$$
p(x) = n_i/ (N w_i)
$$
\afterdisplay
@end tex
@ifinfo

@example
p(x) = n_i/ (N w_i)
@end example

@end ifinfo
@noindent
In this equation @math{n_i} is the number of events in the bin which
contains @math{x}, @math{w_i} is the width of the bin and @math{N} is
the total number of events.  The distribution of events within each bin
is assumed to be uniform.

@node The histogram probability distribution struct
@section The histogram probability distribution struct
@cindex probability distribution, from histogram
@cindex sampling from histograms
@cindex random sampling from histograms
@cindex histograms, random sampling from
The probability distribution function for a histogram consists of a set
of @dfn{bins} which measure the probability of an event falling into a
given range of a continuous variable @math{x}. A probability
distribution function is defined by the following struct, which actually
stores the cumulative probability distribution function.  This is the
natural quantity for generating samples via the inverse transform
method, because there is a one-to-one mapping between the cumulative
probability distribution and the range [0,1].  It can be shown that by
taking a uniform random number in this range and finding its
corresponding coordinate in the cumulative probability distribution we
obtain samples with the desired probability distribution.

@deftp {Data Type} {gsl_histogram_pdf}
@table @code
@item size_t n
This is the number of bins used to approximate the probability
distribution function. 
@item double * range
The ranges of the bins are stored in an array of @math{@var{n}+1} elements
pointed to by @var{range}.
@item double * sum
The cumulative probability for the bins is stored in an array of
@var{n} elements pointed to by @var{sum}.
@end table
@end deftp
@comment 

@noindent
The following functions allow you to create a @code{gsl_histogram_pdf}
struct which represents this probability distribution and generate
random samples from it.

@deftypefun {gsl_histogram_pdf *} gsl_histogram_pdf_alloc (size_t @var{n})
This function allocates memory for a probability distribution with
@var{n} bins and returns a pointer to a newly initialized
@code{gsl_histogram_pdf} struct. If insufficient memory is available a
null pointer is returned and the error handler is invoked with an error
code of @code{GSL_ENOMEM}.
@end deftypefun

@deftypefun int gsl_histogram_pdf_init (gsl_histogram_pdf * @var{p}, const gsl_histogram * @var{h})
This function initializes the probability distribution @var{p} with
the contents of the histogram @var{h}. If any of the bins of @var{h} are
negative then the error handler is invoked with an error code of
@code{GSL_EDOM} because a probability distribution cannot contain
negative values.
@end deftypefun

@deftypefun void gsl_histogram_pdf_free (gsl_histogram_pdf * @var{p})
This function frees the probability distribution function @var{p} and
all of the memory associated with it.
@end deftypefun

@deftypefun double gsl_histogram_pdf_sample (const gsl_histogram_pdf * @var{p}, double @var{r})
This function uses @var{r}, a uniform random number between zero and
one, to compute a single random sample from the probability distribution
@var{p}.  The algorithm used to compute the sample @math{s} is given by
the following formula,
@tex
\beforedisplay
$$
s = \hbox{range}[i] + \delta * (\hbox{range}[i+1] - \hbox{range}[i])
$$
\afterdisplay
@end tex
@ifinfo

@example
s = range[i] + delta * (range[i+1] - range[i])
@end example

@end ifinfo
@noindent
where @math{i} is the index which satisfies 
@c{$sum[i] \le  r < sum[i+1]$}
@math{sum[i] <=  r < sum[i+1]} and 
@math{delta} is 
@c{$(r - sum[i])/(sum[i+1] - sum[i])$}
@math{(r - sum[i])/(sum[i+1] - sum[i])}.
@end deftypefun

@node Example programs for histograms
@section Example programs for histograms

The following program shows how to make a simple histogram of a column
of numerical data supplied on @code{stdin}.  The program takes three
arguments, specifying the upper and lower bounds of the histogram and
the number of bins.  It then reads numbers from @code{stdin}, one line at
a time, and adds them to the histogram.  When there is no more data to
read it prints out the accumulated histogram using
@code{gsl_histogram_fprintf}.

@example
@verbatiminclude examples/histogram.c
@end example

@noindent
Here is an example of the program in use.  We generate 10000 random
samples from a Cauchy distribution with a width of 30 and histogram
them over the range -100 to 100, using 200 bins.

@example
$ gsl-randist 0 10000 cauchy 30 
   | gsl-histogram -100 100 200 > histogram.dat
@end example

@noindent
A plot of the resulting histogram shows the familiar shape of the
Cauchy distribution and the fluctuations caused by the finite sample
size.

@example
$ awk '@{print $1, $3 ; print $2, $3@}' histogram.dat 
   | graph -T X
@end example

@iftex
@sp 1
@center @image{histogram,3.0in,2.8in}
@end iftex

@node Two dimensional histograms
@section Two dimensional histograms
@cindex two dimensional histograms
@cindex 2D histograms

A two dimensional histogram consists of a set of @dfn{bins} which count
the number of events falling in a given area of the @math{(x,y)}
plane.  The simplest way to use a two dimensional histogram is to record
two-dimensional position information, @math{n(x,y)}.  Another possibility
is to form a @dfn{joint distribution} by recording related
variables.  For example a detector might record both the position of an
event (@math{x}) and the amount of energy it deposited @math{E}.  These
could be histogrammed as the joint distribution @math{n(x,E)}.

@node The 2D histogram struct
@section The 2D histogram struct

Two dimensional histograms are defined by the following struct,

@deftp {Data Type} {gsl_histogram2d}
@table @code
@item size_t nx, ny
This is the number of histogram bins in the x and y directions.
@item double * xrange
The ranges of the bins in the x-direction are stored in an array of
@math{@var{nx} + 1} elements pointed to by @var{xrange}.
@item double * yrange
The ranges of the bins in the y-direction are stored in an array of
@math{@var{ny} + 1} elements pointed to by @var{yrange}.
@item double * bin
The counts for each bin are stored in an array pointed to by @var{bin}.
The bins are floating-point numbers, so you can increment them by
non-integer values if necessary.  The array @var{bin} stores the two
dimensional array of bins in a single block of memory according to the
mapping @code{bin(i,j)} = @code{bin[i * ny + j]}.
@end table
@end deftp
@comment 

@noindent
The range for @code{bin(i,j)} is given by @code{xrange[i]} to
@code{xrange[i+1]} in the x-direction and @code{yrange[j]} to
@code{yrange[j+1]} in the y-direction.  Each bin is inclusive at the lower
end and exclusive at the upper end.  Mathematically this means that the
bins are defined by the following inequality,
@tex
\beforedisplay
$$
\matrix{
\hbox{bin(i,j) corresponds to} & 
          \hbox{\it xrange}[i] \le x < \hbox{\it xrange}[i+1] \cr
   \hbox{and} & \hbox{\it yrange}[j] \le y < \hbox{\it yrange}[j+1]}
$$
\afterdisplay
@end tex
@ifinfo
@display
bin(i,j) corresponds to xrange[i] <= x < xrange[i+1]
                    and yrange[j] <= y < yrange[j+1]
@end display

@end ifinfo
@noindent
Note that any samples which fall on the upper sides of the histogram are
excluded.  If you want to include these values for the side bins you will
need to add an extra row or column to your histogram.

The @code{gsl_histogram2d} struct and its associated functions are
defined in the header file @file{gsl_histogram2d.h}.

@node 2D Histogram allocation
@section 2D Histogram allocation

The functions for allocating memory to a 2D histogram follow the style
of @code{malloc} and @code{free}.  In addition they also perform their
own error checking.  If there is insufficient memory available to
allocate a histogram then the functions call the error handler (with
an error number of @code{GSL_ENOMEM}) in addition to returning a null
pointer.  Thus if you use the library error handler to abort your program
then it isn't necessary to check every 2D histogram @code{alloc}.

@deftypefun {gsl_histogram2d *} gsl_histogram2d_alloc (size_t @var{nx}, size_t @var{ny})
This function allocates memory for a two-dimensional histogram with
@var{nx} bins in the x direction and @var{ny} bins in the y direction.
The function returns a pointer to a newly created @code{gsl_histogram2d}
struct. If insufficient memory is available a null pointer is returned
and the error handler is invoked with an error code of
@code{GSL_ENOMEM}. The bins and ranges must be initialized with one of
the functions below before the histogram is ready for use.
@end deftypefun

@comment @deftypefun {gsl_histogram2d *} gsl_histogram2d_calloc (size_t @var{nx}, size_t @var{ny})
@comment This function allocates memory for a two-dimensional histogram with
@comment @var{nx} bins in the x direction and @var{ny} bins in the y
@comment direction.  The function returns a pointer to a newly initialized
@comment @code{gsl_histogram2d} struct.  The bins are uniformly spaced with a
@comment total range of 
@comment @c{$0 \le  x < nx$}
@comment @math{0 <= x < nx} in the x-direction and 
@comment @c{$0 \le  y < ny$} 
@comment @math{0 <=  y < ny} in the y-direction, as shown in the table below.
@comment 
@comment The bins are initialized to zero so the histogram is ready for use.
@comment 
@comment If insufficient memory is available a null pointer is returned and the
@comment error handler is invoked with an error code of @code{GSL_ENOMEM}.
@comment @end deftypefun
@comment 
@comment @deftypefun {gsl_histogram2d *} gsl_histogram2d_calloc_uniform (size_t @var{nx}, size_t @var{ny}, double @var{xmin}, double @var{xmax}, double @var{ymin}, double @var{ymax})
@comment This function allocates a histogram of size @var{nx}-by-@var{ny} which
@comment uniformly covers the ranges @var{xmin} to @var{xmax} and @var{ymin} to
@comment @var{ymax} in the @math{x} and @math{y} directions respectively.
@comment @end deftypefun
@comment 
@comment @deftypefun {gsl_histogram2d *} gsl_histogram2d_calloc_range (size_t @var{nx}, size_t @var{ny}, double * @var{xrange}, double * @var{yrange})
@comment This function allocates a histogram of size @var{nx}-by-@var{ny} using
@comment the @math{nx+1} and @math{ny+1} bin ranges specified by the arrays
@comment @var{xrange} and @var{xyrange}.
@comment @end deftypefun

@deftypefun int gsl_histogram2d_set_ranges (gsl_histogram2d * @var{h},  const double @var{xrange}[], size_t @var{xsize}, const double @var{yrange}[], size_t @var{ysize})
This function sets the ranges of the existing histogram @var{h} using
the arrays @var{xrange} and @var{yrange} of size @var{xsize} and
@var{ysize} respectively.  The values of the histogram bins are reset to
zero.
@end deftypefun

@deftypefun int gsl_histogram2d_set_ranges_uniform (gsl_histogram2d * @var{h}, double @var{xmin}, double @var{xmax}, double @var{ymin}, double @var{ymax})
This function sets the ranges of the existing histogram @var{h} to cover
the ranges @var{xmin} to @var{xmax} and @var{ymin} to @var{ymax}
uniformly.  The values of the histogram bins are reset to zero.
@end deftypefun

@deftypefun void gsl_histogram2d_free (gsl_histogram2d * @var{h})
This function frees the 2D histogram @var{h} and all of the memory
associated with it.
@end deftypefun

@node Copying 2D Histograms
@section Copying 2D Histograms

@deftypefun int gsl_histogram2d_memcpy (gsl_histogram2d * @var{dest}, const gsl_histogram2d * @var{src})
This function copies the histogram @var{src} into the pre-existing
histogram @var{dest}, making @var{dest} into an exact copy of @var{src}.
The two histograms must be of the same size.
@end deftypefun

@deftypefun {gsl_histogram2d *} gsl_histogram2d_clone (const gsl_histogram2d * @var{src})
This function returns a pointer to a newly created histogram which is an
exact copy of the histogram @var{src}.
@end deftypefun

@node Updating and accessing 2D histogram elements
@section Updating and accessing 2D histogram elements

You can access the bins of a two-dimensional histogram either by
specifying a pair of @math{(x,y)} coordinates or by using the bin
indices @math{(i,j)} directly.  The functions for accessing the histogram
through @math{(x,y)} coordinates use binary searches in the x and y
directions to identify the bin which covers the appropriate range.

@deftypefun int gsl_histogram2d_increment (gsl_histogram2d * @var{h}, double @var{x}, double @var{y})
This function updates the histogram @var{h} by adding one (1.0) to the
bin whose x and y ranges contain the coordinates (@var{x},@var{y}).

If the point @math{(x,y)} lies inside the valid ranges of the
histogram then the function returns zero to indicate success.  If
@math{(x,y)} lies outside the limits of the histogram then the
function returns @code{GSL_EDOM}, and none of the bins are modified.  The
error handler is not called, since it is often necessary to compute
histograms for a small range of a larger dataset, ignoring any
coordinates outside the range of interest.
@end deftypefun

@deftypefun int gsl_histogram2d_accumulate (gsl_histogram2d * @var{h}, double @var{x}, double @var{y}, double @var{weight})
This function is similar to @code{gsl_histogram2d_increment} but increases
the value of the appropriate bin in the histogram @var{h} by the
floating-point number @var{weight}.
@end deftypefun

@deftypefun double gsl_histogram2d_get (const gsl_histogram2d * @var{h}, size_t @var{i}, size_t @var{j})
This function returns the contents of the (@var{i},@var{j})-th bin of the
histogram @var{h}.  If (@var{i},@var{j}) lies outside the valid range of
indices for the histogram then the error handler is called with an error
code of @code{GSL_EDOM} and the function returns 0.
@end deftypefun

@deftypefun int gsl_histogram2d_get_xrange (const gsl_histogram2d * @var{h}, size_t @var{i}, double * @var{xlower}, double * @var{xupper})
@deftypefunx int gsl_histogram2d_get_yrange (const gsl_histogram2d * @var{h}, size_t @var{j}, double * @var{ylower}, double * @var{yupper})
These functions find the upper and lower range limits of the @var{i}-th
and @var{j}-th bins in the x and y directions of the histogram @var{h}.
The range limits are stored in @var{xlower} and @var{xupper} or
@var{ylower} and @var{yupper}.  The lower limits are inclusive
(i.e. events with these coordinates are included in the bin) and the
upper limits are exclusive (i.e. events with the value of the upper
limit are not included and fall in the neighboring higher bin, if it
exists).  The functions return 0 to indicate success.  If @var{i} or
@var{j} lies outside the valid range of indices for the histogram then
the error handler is called with an error code of @code{GSL_EDOM}.
@end deftypefun

@deftypefun double gsl_histogram2d_xmax (const gsl_histogram2d * @var{h})
@deftypefunx double gsl_histogram2d_xmin (const gsl_histogram2d * @var{h})
@deftypefunx size_t gsl_histogram2d_nx (const gsl_histogram2d * @var{h})
@deftypefunx double gsl_histogram2d_ymax (const gsl_histogram2d * @var{h})
@deftypefunx double gsl_histogram2d_ymin (const gsl_histogram2d * @var{h})
@deftypefunx size_t gsl_histogram2d_ny (const gsl_histogram2d * @var{h})
These functions return the maximum upper and minimum lower range limits
and the number of bins for the x and y directions of the histogram
@var{h}.  They provide a way of determining these values without
accessing the @code{gsl_histogram2d} struct directly.
@end deftypefun

@deftypefun void gsl_histogram2d_reset (gsl_histogram2d * @var{h})
This function resets all the bins of the histogram @var{h} to zero.
@end deftypefun

@node Searching 2D histogram ranges
@section Searching 2D histogram ranges

The following functions are used by the access and update routines to
locate the bin which corresponds to a given @math{(x,y)} coordinate.

@deftypefun int gsl_histogram2d_find (const gsl_histogram2d * @var{h}, double @var{x}, double @var{y}, size_t * @var{i}, size_t * @var{j})
This function finds and sets the indices @var{i} and @var{j} to the to
the bin which covers the coordinates (@var{x},@var{y}). The bin is
located using a binary search.  The search includes an optimization for
histograms with uniform ranges, and will return the correct bin immediately
in this case. If @math{(x,y)} is found then the function sets the
indices (@var{i},@var{j}) and returns @code{GSL_SUCCESS}.  If
@math{(x,y)} lies outside the valid range of the histogram then the
function returns @code{GSL_EDOM} and the error handler is invoked.
@end deftypefun

@node 2D Histogram Statistics
@section 2D Histogram Statistics

@deftypefun double gsl_histogram2d_max_val (const gsl_histogram2d * @var{h})
This function returns the maximum value contained in the histogram bins.
@end deftypefun

@deftypefun void gsl_histogram2d_max_bin (const gsl_histogram2d * @var{h}, size_t * @var{i}, size_t * @var{j})
This function finds the indices of the bin containing the maximum value
in the histogram @var{h} and stores the result in (@var{i},@var{j}). In
the case where several bins contain the same maximum value the first bin
found is returned.
@end deftypefun

@deftypefun double gsl_histogram2d_min_val (const gsl_histogram2d * @var{h})
This function returns the minimum value contained in the histogram bins.
@end deftypefun

@deftypefun void gsl_histogram2d_min_bin (const gsl_histogram2d * @var{h}, size_t * @var{i}, size_t * @var{j})
This function finds the indices of the bin containing the minimum value
in the histogram @var{h} and stores the result in (@var{i},@var{j}). In
the case where several bins contain the same maximum value the first bin
found is returned.
@end deftypefun

@deftypefun double gsl_histogram2d_xmean (const gsl_histogram2d * @var{h})
This function returns the mean of the histogrammed x variable, where the
histogram is regarded as a probability distribution. Negative bin values
are ignored for the purposes of this calculation.
@end deftypefun

@deftypefun double gsl_histogram2d_ymean (const gsl_histogram2d * @var{h})
This function returns the mean of the histogrammed y variable, where the
histogram is regarded as a probability distribution. Negative bin values
are ignored for the purposes of this calculation.
@end deftypefun

@deftypefun double gsl_histogram2d_xsigma (const gsl_histogram2d * @var{h})
This function returns the standard deviation of the histogrammed
x variable, where the histogram is regarded as a probability
distribution. Negative bin values are ignored for the purposes of this
calculation.
@end deftypefun

@deftypefun double gsl_histogram2d_ysigma (const gsl_histogram2d * @var{h})
This function returns the standard deviation of the histogrammed
y variable, where the histogram is regarded as a probability
distribution. Negative bin values are ignored for the purposes of this
calculation.
@end deftypefun

@deftypefun double gsl_histogram2d_cov (const gsl_histogram2d * @var{h})
This function returns the covariance of the histogrammed x and y
variables, where the histogram is regarded as a probability
distribution. Negative bin values are ignored for the purposes of this
calculation.
@end deftypefun

@deftypefun double gsl_histogram2d_sum (const gsl_histogram2d * @var{h})
This function returns the sum of all bin values. Negative bin values
are included in the sum.
@end deftypefun

@node 2D Histogram Operations
@section 2D Histogram Operations

@deftypefun int gsl_histogram2d_equal_bins_p (const gsl_histogram2d * @var{h1}, const gsl_histogram2d * @var{h2})
This function returns 1 if all the individual bin ranges of the two
histograms are identical, and 0 otherwise.
@end deftypefun

@deftypefun int gsl_histogram2d_add (gsl_histogram2d * @var{h1}, const gsl_histogram2d * @var{h2})
This function adds the contents of the bins in histogram @var{h2} to the
corresponding bins of histogram @var{h1},
i.e. @math{h'_1(i,j) = h_1(i,j) + h_2(i,j)}.
The two histograms must have identical bin ranges.
@end deftypefun

@deftypefun int gsl_histogram2d_sub (gsl_histogram2d * @var{h1}, const gsl_histogram2d * @var{h2})
This function subtracts the contents of the bins in histogram @var{h2} from the
corresponding bins of histogram @var{h1},
i.e. @math{h'_1(i,j) = h_1(i,j) - h_2(i,j)}.
The two histograms must have identical bin ranges.
@end deftypefun

@deftypefun int gsl_histogram2d_mul (gsl_histogram2d * @var{h1}, const gsl_histogram2d * @var{h2})
This function multiplies the contents of the bins of histogram @var{h1}
by the contents of the corresponding bins in histogram @var{h2},
i.e. @math{h'_1(i,j) = h_1(i,j) * h_2(i,j)}.
The two histograms must have identical bin ranges.
@end deftypefun

@deftypefun int gsl_histogram2d_div (gsl_histogram2d * @var{h1}, const gsl_histogram2d * @var{h2})
This function divides the contents of the bins of histogram @var{h1}
by the contents of the corresponding bins in histogram @var{h2},
i.e. @math{h'_1(i,j) = h_1(i,j) / h_2(i,j)}.
The two histograms must have identical bin ranges.
@end deftypefun

@deftypefun int gsl_histogram2d_scale (gsl_histogram2d * @var{h}, double @var{scale})
This function multiplies the contents of the bins of histogram @var{h}
by the constant @var{scale}, i.e. @c{$h'_1(i,j) = h_1(i,j) * \hbox{\it scale}$}
@math{h'_1(i,j) = h_1(i,j) scale}.
@end deftypefun

@deftypefun int gsl_histogram2d_shift (gsl_histogram2d * @var{h}, double @var{offset})
This function shifts the contents of the bins of histogram @var{h}
by the constant @var{offset}, i.e. @c{$h'_1(i,j) = h_1(i,j) + \hbox{\it offset}$}
@math{h'_1(i,j) = h_1(i,j) + offset}.
@end deftypefun

@node Reading and writing 2D histograms
@section Reading and writing 2D histograms

The library provides functions for reading and writing two dimensional
histograms to a file as binary data or formatted text.

@deftypefun int gsl_histogram2d_fwrite (FILE * @var{stream}, const gsl_histogram2d * @var{h})
This function writes the ranges and bins of the histogram @var{h} to the
stream @var{stream} in binary format.  The return value is 0 for success
and @code{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.
@end deftypefun

@deftypefun int gsl_histogram2d_fread (FILE * @var{stream}, gsl_histogram2d * @var{h})
This function reads into the histogram @var{h} from the stream
@var{stream} in binary format.  The histogram @var{h} must be
preallocated with the correct size since the function uses the number of
x and y bins in @var{h} to determine how many bytes to read.  The return
value is 0 for success and @code{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.
@end deftypefun

@deftypefun int gsl_histogram2d_fprintf (FILE * @var{stream}, const gsl_histogram2d * @var{h}, const char * @var{range_format}, const char * @var{bin_format})
This function writes the ranges and bins of the histogram @var{h}
line-by-line to the stream @var{stream} using the format specifiers
@var{range_format} and @var{bin_format}.  These should be one of the
@code{%g}, @code{%e} or @code{%f} formats for floating point
numbers.  The function returns 0 for success and @code{GSL_EFAILED} if
there was a problem writing to the file.  The histogram output is
formatted in five columns, and the columns are separated by spaces,
like this,

@smallexample
xrange[0] xrange[1] yrange[0] yrange[1] bin(0,0)
xrange[0] xrange[1] yrange[1] yrange[2] bin(0,1)
xrange[0] xrange[1] yrange[2] yrange[3] bin(0,2)
....
xrange[0] xrange[1] yrange[ny-1] yrange[ny] bin(0,ny-1)

xrange[1] xrange[2] yrange[0] yrange[1] bin(1,0)
xrange[1] xrange[2] yrange[1] yrange[2] bin(1,1)
xrange[1] xrange[2] yrange[1] yrange[2] bin(1,2)
....
xrange[1] xrange[2] yrange[ny-1] yrange[ny] bin(1,ny-1)

....

xrange[nx-1] xrange[nx] yrange[0] yrange[1] bin(nx-1,0)
xrange[nx-1] xrange[nx] yrange[1] yrange[2] bin(nx-1,1)
xrange[nx-1] xrange[nx] yrange[1] yrange[2] bin(nx-1,2)
....
xrange[nx-1] xrange[nx] yrange[ny-1] yrange[ny] bin(nx-1,ny-1)
@end smallexample

@noindent
Each line contains the lower and upper limits of the bin and the
contents of the bin.  Since the upper limits of the each bin are the
lower limits of the neighboring bins there is duplication of these
values but this allows the histogram to be manipulated with
line-oriented tools.
@end deftypefun

@deftypefun int gsl_histogram2d_fscanf (FILE * @var{stream}, gsl_histogram2d * @var{h})
This function reads formatted data from the stream @var{stream} into the
histogram @var{h}.  The data is assumed to be in the five-column format
used by @code{gsl_histogram_fprintf}.  The histogram @var{h} must be
preallocated with the correct lengths since the function uses the sizes
of @var{h} to determine how many numbers to read.  The function returns 0
for success and @code{GSL_EFAILED} if there was a problem reading from
the file.
@end deftypefun

@node Resampling from 2D histograms
@section Resampling from 2D histograms

As in the one-dimensional case, a two-dimensional histogram made by
counting events can be regarded as a measurement of a probability
distribution.  Allowing for statistical error, the height of each bin
represents the probability of an event where (@math{x},@math{y}) falls in
the range of that bin.  For a two-dimensional histogram the probability
distribution takes the form @math{p(x,y) dx dy} where,
@tex
\beforedisplay
$$
p(x,y) = n_{ij}/ (N A_{ij})
$$
\afterdisplay
@end tex
@ifinfo

@example
p(x,y) = n_@{ij@}/ (N A_@{ij@})
@end example

@end ifinfo
@noindent
In this equation 
@c{$n_{ij}$}
@math{n_@{ij@}} is the number of events in the bin which
contains @math{(x,y)}, 
@c{$A_{ij}$}
@math{A_@{ij@}} is the area of the bin and @math{N} is
the total number of events.  The distribution of events within each bin
is assumed to be uniform.

@deftp {Data Type} {gsl_histogram2d_pdf}
@table @code
@item size_t nx, ny
This is the number of histogram bins used to approximate the probability
distribution function in the x and y directions.
@item double * xrange
The ranges of the bins in the x-direction are stored in an array of
@math{@var{nx} + 1} elements pointed to by @var{xrange}.
@item double * yrange
The ranges of the bins in the y-direction are stored in an array of
@math{@var{ny} + 1} pointed to by @var{yrange}.
@item double * sum
The cumulative probability for the bins is stored in an array of
@var{nx}*@var{ny} elements pointed to by @var{sum}.
@end table
@end deftp
@comment 

@noindent
The following functions allow you to create a @code{gsl_histogram2d_pdf}
struct which represents a two dimensional probability distribution and
generate random samples from it.

@deftypefun {gsl_histogram2d_pdf *} gsl_histogram2d_pdf_alloc (size_t @var{nx}, size_t @var{ny})
This function allocates memory for a two-dimensional probability
distribution of size @var{nx}-by-@var{ny} and returns a pointer to a
newly initialized @code{gsl_histogram2d_pdf} struct. If insufficient
memory is available a null pointer is returned and the error handler is
invoked with an error code of @code{GSL_ENOMEM}.
@end deftypefun

@deftypefun int gsl_histogram2d_pdf_init (gsl_histogram2d_pdf * @var{p}, const gsl_histogram2d * @var{h})
This function initializes the two-dimensional probability distribution
calculated @var{p} from the histogram @var{h}.  If any of the bins of
@var{h} are negative then the error handler is invoked with an error
code of @code{GSL_EDOM} because a probability distribution cannot
contain negative values.
@end deftypefun

@deftypefun void gsl_histogram2d_pdf_free (gsl_histogram2d_pdf * @var{p})
This function frees the two-dimensional probability distribution
function @var{p} and all of the memory associated with it.
@end deftypefun

@deftypefun int gsl_histogram2d_pdf_sample (const gsl_histogram2d_pdf * @var{p}, double @var{r1}, double @var{r2}, double * @var{x}, double * @var{y})
This function uses two uniform random numbers between zero and one,
@var{r1} and @var{r2}, to compute a single random sample from the
two-dimensional probability distribution @var{p}.
@end deftypefun

@page
@node Example programs for 2D histograms
@section Example programs for 2D histograms
This program demonstrates two features of two-dimensional histograms.
First a 10-by-10 two-dimensional histogram is created with x and y running
from 0 to 1.  Then a few sample points are added to the histogram, at
(0.3,0.3) with a height of 1, at (0.8,0.1) with a height of 5 and at
(0.7,0.9) with a height of 0.5.  This histogram with three events is
used to generate a random sample of 1000 simulated events, which are
printed out.

@example
@verbatiminclude examples/histogram2d.c
@end example

@noindent
@iftex
The following plot shows the distribution of the simulated events.  Using
a higher resolution grid we can see the original underlying histogram
and also the statistical fluctuations caused by the events being
uniformly distributed over the area of the original bins.

@sp 1
@center @image{histogram2d,3.4in}
@end iftex