Changeset 303
- Timestamp:
- Mar 24, 2004, 2:39:57 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/doc/pslib/psLibADD.tex (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/pslib/psLibADD.tex
r301 r303 12 12 \docnumber{PSDC-430-006} 13 13 14 \newcommand\citealt{} 15 14 16 \begin{document} 15 17 \maketitle 18 16 19 17 20 % -- Revision History -- … … 252 255 \end{center} 253 256 257 \subsection{Sorting} 258 259 A variety of sorting algorithms exist, with a wide range in speed for 260 different set sizes. Three of the best sorting algorithms are 261 ``heapsort'', ``quicksort'', and ``mergesort'' (see, e.g., 262 \citealt{knuth}, \citealt{sedgewick}, \citealt{press}). These three 263 sorting algorithms all run in a time of $O(n \log n)$ in the best 264 case, but have different worse-case run times. Implementations of all 265 three sorting algorithms are described in references above and in 266 various places online (e.g., 267 http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/algoen.htm). 268 The linux \code{qsort} function orignially mades use of the quicksort 269 algorithm, but now uses \tbd{what algorithm?}. The GSL function 270 \code{gsl_heapsort} uses the heapsort algorithm. Both of these 271 implemenations require a pointer to the comparison function, which may 272 result in a slower code. 273 274 For PSLib, the sorting functions shall be implemented via 275 \code{qsort}. The function \code{psSort} shall return the sorted 276 result of the input array without over-writing the input array. The 277 function \code{psSortIndex} shall return an integer index to the 278 sequence of the input array without overwriting the input array. 279 Given the following line of code, \code{out = psSortIndex (NULL, 280 &in);}, the elements of the array \code{out} are in the sequence 281 \code{in.arr[out->arr[0]]} to \code{in.arr[out->arr[in.n - 1]]}. 282 283 \subsection{Smoothing: Boxcar and Gaussian} 284 \label{smooth} 285 286 Smoothing may occasionally be perfomed on data. We present the 287 algorithms for two typical versions: boxcar and Gaussian smoothing. 288 In both smoothing techniques, given a series of data values $f_i$, the 289 smoothed values $g_i$ are determined by calculating a linear 290 combination based on the input data point and its nearest $2N$ 291 neighbors in the form: 292 293 \[ g_i = \sum_{n=-N}^N c_n f_i \] 294 % 295 where the values of $c_n$ determine the filter type. For boxcar 296 smoothing, the values $c_n$ are constant, and must be equal to $1/(2N 297 + 1)$ to maintain the zeroth moment of the data (care must be taken at 298 the ends of the data range to reduce the value of $c_n$ as fewer input 299 data points may be used). For Gaussian smoothing, the crucial 300 parameter is $\sigma$, the standard deviation. The 301 value of $N$ should be chosen to be large enough, $N = 5\sigma$, and 302 the values of $c_n$ are then just the Gaussian curve: 303 304 \[ c_n = \frac{e^{\frac{-n^2}{2\sigma^2}}}{\sqrt{2\pi\sigma^2}} \] 305 306 \subsection{Statistics} 307 308 The general statistics function \code{psStats} performs a variety of 309 statistical calculations on a collection of floating point numbers. 310 These statistics include both sample values, in which the formal 311 statistic is calculated for the complete sample, and robust values, in 312 which the statistic is estimated on the basis of an assumption of an 313 underlying population. While more reliable in general, the robust 314 estimators may be somewhat slower than the sample statisic, so their 315 use may vary depending on the context. In addition, certain 316 sigma-clipped values are defined as an intermediate choice between the 317 sample and robust estimators. 318 319 \subsubsection{Sample Statistics} 320 321 We define the following statistical terms, assuming there is a set of 322 data elements $x_i$. 323 324 \paragraph{Mean} 325 326 The mean is defined as: \[ \bar{x} = \frac{1}{N} \sum_{i = 1}^{N} x_i \] 327 328 \paragraph{Median} 329 330 The median is defined as the value for which 50\% of the data values 331 are larger and 50\% are smaller. For a sample, the values are sorted 332 and the median is given by the value of the middle element, if the 333 number of values is odd, and by the average of the two middle values 334 if the number of values is even. This median should be avoided for 335 samples which are large (e.g., $N > 10000$ elements) as the basic 336 robust median is quicker and more accurate. 337 338 \paragraph{Upper and Lower Quartiles} 339 340 The upper and lower quartiles ($U_{\frac{1}{4}}$ and 341 $L_{\frac{1}{4}}$) are first and last quarter equivalents to the 342 median. The upper quartile is the value for which 25\% of the data 343 are larger and 75\% are smaller. The lower quartile is the value for 344 which 75\% of the data are larger and 25\% are smaller. For a sample, 345 the values are sorted and the quartiles are given by the values of 346 elements $N/4$ and $3N/4$. For the purpose of a sample upper and 347 lower quartile, it is sufficient to provide the closest integer entry 348 to these values. The sample quartiles should be avoided for samples 349 which are large (e.g., $N > 10000$ elements) as the robust quartiles 350 are quicker and more accurate. 351 352 \paragraph{Standard Deviation} 353 354 The standard deviation of the sample is given by: 355 356 \[ \sigma = \sqrt{\sum_{i = 1}^N \frac{(x_i - \bar{x})^2}{N - 1}} \] 357 358 To minimize the numerical rounding error, this should be calculated 359 numerically as: 360 361 \[ \sigma = \sqrt{\frac{1}{N - 1} [ \sum_{i = 1}^{N} (x_i - \bar{x})^2 - \frac{1}{N} (\sum_{i = 1}^{N} (x_i - \bar{x}))^2 ]} \] 362 363 \subsubsection{Clipped Statistics} 364 365 The clipped statistics are used to determine the mean and $\sigma$ of 366 a distribution in the presence of outliers. The clipped statistics are 367 quicker than the complete robust statistical estimators and more 368 reliable than the sample statistics. The clipped statistics algorithm 369 produces the clipped mean and clipped standard deviation. The 370 algorithm has 2 parameters: $N$, the number of iterations and $k$, the 371 multiplying factor of $\sigma$ used to exclude outliers. Typical 372 values for both $N$ and $k$ are 3. The algorithm is as follows: 373 374 \begin{enumerate} 375 \item Compute the sample median. 376 \item Compute the sample standard deviation. 377 \item Use the sample median as the first estimator of the mean, $\bar{x}$. 378 \item Use the sample standard deviation as the first estimator of the 379 true standard deviation, $\sigma$. 380 \item Repeat the following N times: 381 \begin{enumerate} 382 \item Exclude all values $x_{i}$ for while $|x_{i} - \bar{x}| > k \sigma$. 383 \item Compute the mean and standard deviation of the sub-sample. 384 \item Use the new mean for $\bar{x}$. 385 \item Use the new standard deviations for $\sigma$. 386 \end{enumerate} 387 \item The last calulcated value of $\bar{x}$ is the clipped mean. 388 \item The last calulcated value of $\sigma$ is the clipped standard deviation. 389 \end{enumerate} 390 391 \subsubsection{Robust Statistics} 392 393 The robust version of the statistics provide estimators of basic 394 statistical concepts which are reliable even for data samples with 395 significant contamination. A typical case is the situation in which 396 the data of interest represent a primary population of interest with a 397 single-valued mean and standard deviation and a secondary population 398 of data with a substantially different distribution. For example, an 399 image of an uncrowded night-time field may consist of a sparse 400 collection of stars and an overall background level. The majority of 401 pixels have the background value, with some variance due to noise 402 sources, but many are significantly higher (contributed by stars) or 403 significantly lower (dead pixels). If we want to measure the mean of 404 the background, a robust mean is necessary as the outliers will 405 strongly bias the sample statistics. 406 407 The robust statistics are calculated by constructing a histogram of 408 the values and performing the measurements on the histogram. The 409 choice of a bin size requires some care. If the data are integer 410 valued, the natural bin size is an integer. Otherwise, the bin should 411 be a fraction of an estimate of the standard deviation. Use the 412 sample upper and lower quartiles to determine an estimate of the 413 standard deviation: $\sigma_e = (U_{\frac{1}{4}} - L_{\frac{1}{4}}) / 414 1.34$. The bin size shall be set at $\sigma_e / 10$. The remaining 415 steps of the algorithm are as follows: 416 417 \begin{itemize} 418 \item Construct the histogram with the specified bin size. 419 \item Smooth the histogram by a Gaussian with $\sigma_s = \sigma_e / 420 4$. 421 \item Find the bin with the peak value in the range $L_{\frac{1}{4}}$ 422 to $U_{\frac{1}{4}}$; this is the robust mode $\mbox{mode}_r$. 423 \item Determine $dL = (U_{\frac{1}{4}} - L_{\frac{1}{4}}) / 8$. 424 \item Fit a Gaussian to the bins in the range $\mbox{mode}_r - dL$ to 425 $\mbox{mode}_r + dL$. 426 \item The resulting fit parameters are the robust mean, 427 $\mbox{mean}_r$ and the robust standard deviation, $\sigma_r$. 428 \end{itemize} 429 430 To determine the robust median, construct the cumulative histogram 431 from the histogram above. Select the bin which contains the 50th 432 percentile value and its two neighbors. Fit a quadratic to these 433 three points. The robust median value is the coordinate of the 434 quadratic which returns the 50\% value. 435 254 436 \end{document}
Note:
See TracChangeset
for help on using the changeset viewer.
