Debian Codegraph @

hmmer/easel/esl_histogram.tex

raw
1
2 The \eslmod{histogram} module is for collecting scores, fitting
3 them to expected distributions, and displaying them.
4
5 The histogram automatically reallocates its bins as data points
6 arrive, so the caller only needs to provide some initial guidance
7 about bin size and ``phase'' (offset of the bins relative to the real
8 number line). It accumulates counts in 64-bit unsigned integers, so
9 it can handle over $10^19$ total counts. Optionally (and provided
10 that the caller knows it has enough memory to support this), a
11 ``full'' histogram can be created and used to collect a sorted vector
12 of raw (unbinned) values.
13
14 Various different ways of fitting histogram data to different sorts of
15 expected distributions are supported, with interfaces to all of
16 Easel's statistical distribution modules. Data fitting is oriented
17 toward the case where the values are scores, with high scores being of
18 the most interest; for instance, routines for obtaining and fitting
19 the right (high-scoring) tail are provided, but not for the left tail.
20
21 Several of the output functions output data as XY data files suitable
22 for input into the popular and freely available \prog{xmgrace}
23 graphing program [\url{http://plasma-gate.weizmann.ac.il/Grace/}].
24
25 The API for the \eslmod{histogram} module is summarized in
26 Table~\ref{tbl:histogram_api}.
27
28 \begin{table}[hbp]
29 \begin{center}
30 {\small
31 \begin{tabular}{|ll|}\hline
32 \apisubhead{Collecting data in an \ccode{ESL\_HISTOGRAM}}\\
33 \hyperlink{func:esl_histogram_Create()}{\ccode{esl\_histogram\_Create()}} & Create a new \ccode{ESL\_HISTOGRAM}.\\
34 \hyperlink{func:esl_histogram_CreateFull()}{\ccode{esl\_histogram\_CreateFull()}} & A \ccode{ESL\_HISTOGRAM} to keep all data samples.\\
35 \hyperlink{func:esl_histogram_Destroy()}{\ccode{esl\_histogram\_Destroy()}} & Frees a \ccode{ESL\_HISTOGRAM}.\\
36 \hyperlink{func:esl_histogram_Add()}{\ccode{esl\_histogram\_Add()}} & Add a sample to the histogram.\\
37 \apisubhead{Declarations about binned data, before fitting}\\
38 \hyperlink{func:esl_histogram_DeclareCensoring()}{\ccode{esl\_histogram\_DeclareCensoring()}} & Collected data were left-censored.\\
39 \hyperlink{func:esl_histogram_DeclareRounding()}{\ccode{esl\_histogram\_DeclareRounding()}} & Declare collected data were no more accurate than bins.\\
40 \hyperlink{func:esl_histogram_SetTail()}{\ccode{esl\_histogram\_SetTail()}} & Declare only tail $>$ some threshold is considered "observed".\\
41 \hyperlink{func:esl_histogram_SetTailByMass()}{\ccode{esl\_histogram\_SetTailByMass()}} & Declare only right tail mass is considered "observed".\\
42 \apisubhead{Accessing raw data samples}\\
43 \hyperlink{func:esl_histogram_GetRank()}{\ccode{esl\_histogram\_GetRank()}} & Retrieve n'th high score.\\
44 \hyperlink{func:esl_histogram_GetData()}{\ccode{esl\_histogram\_GetData()}} & Retrieve vector of all raw scores.\\
45 \hyperlink{func:esl_histogram_GetTail()}{\ccode{esl\_histogram\_GetTail()}} & Retrieve all raw scores above some threshold.\\
46 \hyperlink{func:esl_histogram_GetTailByMass()}{\ccode{esl\_histogram\_GetTailByMass()}} & Retrieve all raw scores in right tail mass.\\
47 \apisubhead{Setting expected counts}\\
48 \hyperlink{func:esl_histogram_SetExpect()}{\ccode{esl\_histogram\_SetExpect()}} & Set expected counts for complete distribution.\\
49 \hyperlink{func:esl_histogram_SetExpectedTail()}{\ccode{esl\_histogram\_SetExpectedTail()}} & Set expected counts for right tail.\\
50 \apisubhead{Output}\\
51 \hyperlink{func:esl_histogram_Write()}{\ccode{esl\_histogram\_Write()}} & Print a "pretty" ASCII histogram.\\
52 \hyperlink{func:esl_histogram_Plot()}{\ccode{esl\_histogram\_Plot()}} & Output a histogram in xmgrace XY format.\\
53 \hyperlink{func:esl_histogram_PlotSurvival()}{\ccode{esl\_histogram\_PlotSurvival()}} & Output $P(X>x)$ in xmgrace XY format.\\
54 \hyperlink{func:esl_histogram_PlotQQ()}{\ccode{esl\_histogram\_PlotQQ()}} & Output a Q-Q plot in xmgrace XY format.\\
55 \hyperlink{func:esl_histogram_Goodness()}{\ccode{esl\_histogram\_Goodness()}} & Evaluate fit between observed, expected. \\
56 \hline
57 \end{tabular}
58 }
59 \end{center}
60 \caption{The \eslmod{histogram} API.}
61 \label{tbl:histogram_api}
62 \end{table}
63
64 \subsection{Example of using the histogram API}
65
66 The example code below stores 10,000 samples from a Gumbel
67 distribution in a histogram, retrieves a vector containing the sorted
68 samples, fits a Gumbel distribution to that dataset, sets the expected
69 counts in the histogram, prints the observed and expected counts in an
70 ASCII histogram, and evaluates the goodness-of-fit.
71
72 \input{cexcerpts/histogram_example}
73
74 Some points of interest:
75
76 \begin{itemize}
77 \item When the histogram is created, the arguments \ccode(-100, 100, 0.5)
78 tell it to bin data into bins of width 0.5, initially
79 starting at -100 and ending at 100. This initialization
80 is described below (see ``Specifying binning of data values'').
81
82 \item Samples are collected one at a time with
83 \ccode{esl\_histogram\_Add()}.
84
85 \item After the data have been collected in a \emph{full} histogram, a
86 vector of sorted raw data values can be retrieved using functions
87 like \ccode{esl\_histogram\_GetData()}, and used to fit parameters
88 of an expected distribution to the data.
89
90 \item In addition to the observed binned counts, you can optionally
91 set \emph{expected} binned counts in the histogram by calling
92 \ccode{esl\_histogram\_SetExpect()} and providing pointers
93 to an appropriate distribution function and its parameters.
94
95 \item The \ccode{esl\_histogram\_Print()} function shows an ASCII text
96 representation of the observed counts (and expected counts, if set)
97 that looks a lot like FASTA's nice histogram output.
98
99 \item The \ccode{esl\_histogram\_Goodness()} function compares the
100 observed and expected binned counts, and calculates two goodness of
101 fit tests: a G-test, and a $\chi^2$ test.
102 \end{itemize}
103
104
105 \subsection{Specifying binning of data values}
106
107 The histogram collects data values into bins. When the histogram is
108 created, the bin width and the relative offset of the bins is
109 permanently set, and an initial range is allocated.
110
111 For example, the call \ccode{esl\_histogram\_Create(-10, 10, 0.5)}
112 creates 40 bins of width 0.5 from -10 to 10, with the first bin
113 collecting scores from $-10 < x \leq -9.5$, and the last bin
114 collecting scores $9.5 < x \leq 10.0$.
115
116 The lower bound of the initialization permanently sets the relative
117 offset of the bins. That is, \ccode{esl\_histogram\_Create(-10, 10,
118 0.5)} makes the first bin $-10 < x \leq -9.5$, whereas
119 \ccode{esl\_histogram\_Create(-10.1, 9.9, 0.5)} makes the first bin
120 $-10.1 < x \leq -9.6$.
121
122 Aside from that, the initial range is only a suggestion. You can add
123 any real-valued $x$ to the histogram. The histogram will silently
124 reallocate itself to a wider range as needed. The ability of a
125 histogram to store data is effectively unlimited. Up to $2^{64}-1$
126 (more than $10^{19}$) counts can be collected. The histogram requires 16
127 bytes of storage per bin, and the number of bins it allocates scales
128 as $x_{\mbox{max}} - x_{\mbox{min}} / w$.
129
130 \subsection{Optional collection of raw data values: full histograms}
131
132 Normally a histogram would store only binned counts, so it can
133 efficiently summarize even very large numbers of samples.
134
135 In some cases it is useful to keep a list of the raw data values --
136 for instance, for more accurate parameter fitting to expected
137 distributions. This can be done by creating a ``full'' histogram with
138 \ccode{esl\_histogram\_CreateFull()} instead of
139 \ccode{esl\_histogram\_Create()}. (The example code above did this,
140 because it did parameter fitting to the raw data.) After data have
141 been collected in a full histogram, individual raw values or pointers
142 to sorted arrays of raw values can be retrieved using the
143 \ccode{esl\_histogram\_Get*} functions.
144
145 A full histogram may require much more memory: about 4 bytes per data
146 point. You may not want to use full histograms if your problem
147 involves collecting many ($> 10^9$, say) data points.
148
149
150
151 \subsection{Different parameter fitting scenarios}
152
153 By default, the data you collect are assumed to be \emph{complete}.
154 You observed all samples; if you fit to any expected distribution, the
155 expected distribution is assumed to describe the complete data; the
156 parameters of the expected distribution are to be fitted to an array
157 of the complete raw data samples; and any goodness of fit test is to
158 be applied to the complete data. This is the simplest, most obvious
159 case.
160
161 Other situations may arise. In addition to complete data, Easel is
162 designed to deal with four other cases:
163
164 \begin{enumerate}
165 \item The collected data are complete, and they are fit to a
166 distribution that describes the complete data, but parameter
167 fitting is done only in the right (highest-scoring) tail. This
168 makes parameter fitting focus on the most important,
169 high-scoring region of a score distribution, and ignore
170 low-scoring outliers.
171
172 \item The collected data are complete, but they are fit to a
173 distribution that only describes the right (highest scoring)
174 tail, and the goodness-of-fit test is only performed on that
175 tail. This case arises when we don't know the form of the
176 expected distribution for the complete data, but the tail
177 follows a predictable decay (an exponential tail, for example).
178
179 \item The collected data are left-censored such that no values $<
180 \phi$ were recorded in the histogram, but the data are fit to a
181 complete distribution that predicts the probability even of the
182 censored (unobserved) values. Goodness of fit is only evaluated
183 in the observed data. (This case is what is actually meant by
184 left-censored data.)
185
186 \item The high-scoring right tail of the collected data are fit as the
187 \emph{binned} counts in the histogram (not raw sample values) to
188 a distribution that describes the tail, such as an
189 exponential. This case becomes useful when the raw data values
190 have limited precision (because of rounding, for example), which
191 can cause numerical problems with parameter fitting to tails.
192 Another case where this is useful is when there are so many data
193 points that the data must be binned just as a matter of
194 practicality (not enough memory to hold a full histogram).
195 \end{enumerate}
196
197 A variety of other situations can be dealt with by using different
198 combinations of the function calls that deal with these four cases.
199
200
201 \subsubsection{Focusing parameter fitting on the highest scores}
202
203 An example of focusing a Gumbel parameter fit on the right half of an
204 observed distribution:
205
206 \input{cexcerpts/histogram_example2}
207
208 The key differences from the complete data case are:
209
210 \begin{itemize}
211 \item Only the high-scoring 50\% of the data samples are
212 retrieved, by calling
213 \ccode{esl\_histogram\_GetTailByMass(h, 0.5, \&xv, \&n, \&z)}.
214 This returns \ccode{z}, the number of samples that
215 were \emph{censored}.
216
217 \item These data are fit to a Gumbel distribution
218 as a \emph{left-censored} dataset by calling
219 \ccode{esl\_gumbel\_FitCensored(xv, n, z, xv[0], \&mu, \&lambda)}.
220 \end{itemize}
221
222 The expected counts and the goodness of fit tests are still evaluated
223 for the complete data, even though the fit was performed only on the
224 highest scores.
225
226
227 \subsubsection{Fitting to a tail distribution}
228
229 An example of fitting an exponential tail to the high-scoring 10\% of
230 a Gumbel-distributed dataset:
231
232 \input{cexcerpts/histogram_example3}
233
234 The differences to note are:
235
236 \begin{itemize}
237 \item The tail is fit as if it is \emph{complete} data as far
238 as the exponential distribution is concerned.
239
240 \item As a result, to use the exponential tail to predict expected
241 data, we have to keep in mind how much probability mass the tail
242 is supposed to predict (here, 10\%), and that
243 is provided to
244 \ccode{esl\_histogram\_SetExpectedTail()}, which specifically
245 calculates expected counts for a tail.
246 \end{itemize}
247
248 \subsubsection{Fitting left-censored data}
249
250 Fitting a Gumbel distribution to data that are \emph{truly} left
251 censored looks a lot like the case where we extracted the high scoring
252 data for a censored fit:
253
254 \input{cexcerpts/histogram_example4}
255
256 \subsubsection{Fitting binned data to a tail distribution}
257
258 Normally, you want to fit parameters to the actual individual data
259 samples, not to binned data, because you'll get more accurate results.
260 An exception can arise when the data samples have limited precision
261 because they've been rounded off. Most distributions are not sensitive
262 to this, but some tail densities are, especially those with
263 singularities ($P(X=x) \rightarrow \infty$) at their origin. In such a
264 case, a fit to binned data may be superior, especially if you can
265 match the histogram's bins to the rounding procedure that was used.
266
267 The following code shows an example of fitting for samples that were
268 already rounded up to the nearest integer before adding them to the
269 histogram:
270
271 \input{cexcerpts/histogram_example5}
272
273 Issues to note:
274
275 \begin{itemize}
276 \item The \ccode{esl\_histogram\_Create(-100, 100, 1.0)} call
277 defined bins that exactly match the rounding procedure
278 defined by \ccode{ceil(x)} -- all $x$ that are rounded
279 to the same value by \ccode{ceil(x)} would also go in
280 the same bin of the histogram.
281
282 \item The \ccode{esl\_histogram\_SetTailByMass()} function sets flags
283 in the histogram to demarcate the desired tail. However,
284 because the data have been binned, and we can only define the
285 tail by a range of bins, it will generally be impossible to
286 match the requested tail mass with adequate accuracy; the actual
287 tail mass is $\geq$ the requested tail mass. It is returned
288 to the caller, and it is the actual mass, not the requested mass,
289 that should be used when setting expected counts.
290
291 \item The \ccode{esl\_histogram\_SetRounding()} declaration
292 sets a flag in the histogram that tells binned parameter
293 fitting functions that the origin of the fitted
294 density ($\mu$) should be set at the lower bound of the smallest bin,
295 rather than the smallest raw data value observed in that
296 bin.
297 \end{itemize}
298
299