IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 303


Ignore:
Timestamp:
Mar 24, 2004, 2:39:57 PM (22 years ago)
Author:
eugene
Message:

added statistics, sorts, and smoothing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/pslib/psLibADD.tex

    r301 r303  
    1212\docnumber{PSDC-430-006}
    1313
     14\newcommand\citealt{}
     15
    1416\begin{document}
    1517\maketitle
     18
    1619
    1720% -- Revision History --
     
    252255\end{center}
    253256
     257\subsection{Sorting}
     258
     259A variety of sorting algorithms exist, with a wide range in speed for
     260different 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
     263sorting algorithms all run in a time of $O(n \log n)$ in the best
     264case, but have different worse-case run times.  Implementations of all
     265three sorting algorithms are described in references above and in
     266various places online (e.g.,
     267http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/algoen.htm).
     268The linux \code{qsort} function orignially mades use of the quicksort
     269algorithm, but now uses \tbd{what algorithm?}.  The GSL function
     270\code{gsl_heapsort} uses the heapsort algorithm.  Both of these
     271implemenations require a pointer to the comparison function, which may
     272result in a slower code. 
     273
     274For PSLib, the sorting functions shall be implemented via
     275\code{qsort}.  The function \code{psSort} shall return the sorted
     276result of the input array without over-writing the input array.  The
     277function \code{psSortIndex} shall return an integer index to the
     278sequence of the input array without overwriting the input array.
     279Given 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
     286Smoothing may occasionally be perfomed on data.  We present the
     287algorithms for two typical versions: boxcar and Gaussian smoothing.
     288In both smoothing techniques, given a series of data values $f_i$, the
     289smoothed values $g_i$ are determined by calculating a linear
     290combination based on the input data point and its nearest $2N$
     291neighbors in the form:
     292
     293\[ g_i = \sum_{n=-N}^N c_n f_i \]
     294%
     295where the values of $c_n$ determine the filter type.  For boxcar
     296smoothing, 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
     298the ends of the data range to reduce the value of $c_n$ as fewer input
     299data points may be used).  For Gaussian smoothing, the crucial
     300parameter is $\sigma$, the standard deviation.  The
     301value of $N$ should be chosen to be large enough, $N = 5\sigma$, and
     302the 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
     308The general statistics function \code{psStats} performs a variety of
     309statistical calculations on a collection of floating point numbers.
     310These statistics include both sample values, in which the formal
     311statistic is calculated for the complete sample, and robust values, in
     312which the statistic is estimated on the basis of an assumption of an
     313underlying population.  While more reliable in general, the robust
     314estimators may be somewhat slower than the sample statisic, so their
     315use may vary depending on the context.  In addition, certain
     316sigma-clipped values are defined as an intermediate choice between the
     317sample and robust estimators. 
     318
     319\subsubsection{Sample Statistics}
     320
     321We define the following statistical terms, assuming there is a set of
     322data elements $x_i$.
     323
     324\paragraph{Mean}
     325
     326The mean is defined as: \[ \bar{x} = \frac{1}{N} \sum_{i = 1}^{N} x_i \]
     327
     328\paragraph{Median}
     329
     330The median is defined as the value for which 50\% of the data values
     331are larger and 50\% are smaller.  For a sample, the values are sorted
     332and the median is given by the value of the middle element, if the
     333number of values is odd, and by the average of the two middle values
     334if the number of values is even.  This median should be avoided for
     335samples which are large (e.g., $N > 10000$ elements) as the basic
     336robust median is quicker and more accurate. 
     337
     338\paragraph{Upper and Lower Quartiles}
     339
     340The upper and lower quartiles ($U_{\frac{1}{4}}$ and
     341$L_{\frac{1}{4}}$) are first and last quarter equivalents to the
     342median.  The upper quartile is the value for which 25\% of the data
     343are larger and 75\% are smaller.  The lower quartile is the value for
     344which 75\% of the data are larger and 25\% are smaller.  For a sample,
     345the values are sorted and the quartiles are given by the values of
     346elements $N/4$ and $3N/4$.  For the purpose of a sample upper and
     347lower quartile, it is sufficient to provide the closest integer entry
     348to these values.  The sample quartiles should be avoided for samples
     349which are large (e.g., $N > 10000$ elements) as the robust quartiles
     350are quicker and more accurate.
     351
     352\paragraph{Standard Deviation}
     353
     354The 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
     358To minimize the numerical rounding error, this should be calculated
     359numerically 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
     365The clipped statistics are used to determine the mean and $\sigma$ of
     366a distribution in the presence of outliers. The clipped statistics are
     367quicker than the complete robust statistical estimators and more
     368reliable than the sample statistics.  The clipped statistics algorithm
     369produces the clipped mean and clipped standard deviation.  The
     370algorithm has 2 parameters: $N$, the number of iterations and $k$, the
     371multiplying factor of $\sigma$ used to exclude outliers.  Typical
     372values 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
     393The robust version of the statistics provide estimators of basic
     394statistical concepts which are reliable even for data samples with
     395significant contamination.  A typical case is the situation in which
     396the data of interest represent a primary population of interest with a
     397single-valued mean and standard deviation and a secondary population
     398of data with a substantially different distribution.  For example, an
     399image of an uncrowded night-time field may consist of a sparse
     400collection of stars and an overall background level.  The majority of
     401pixels have the background value, with some variance due to noise
     402sources, but many are significantly higher (contributed by stars) or
     403significantly lower (dead pixels).  If we want to measure the mean of
     404the background, a robust mean is necessary as the outliers will
     405strongly bias the sample statistics.
     406
     407The robust statistics are calculated by constructing a histogram of
     408the values and performing the measurements on the histogram.  The
     409choice of a bin size requires some care.  If the data are integer
     410valued, the natural bin size is an integer.  Otherwise, the bin should
     411be a fraction of an estimate of the standard deviation.  Use the
     412sample upper and lower quartiles to determine an estimate of the
     413standard deviation: $\sigma_e = (U_{\frac{1}{4}} - L_{\frac{1}{4}}) /
     4141.34$.  The bin size shall be set at $\sigma_e / 10$.  The remaining
     415steps 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
     430To determine the robust median, construct the cumulative histogram
     431from the histogram above. Select the bin which contains the 50th
     432percentile value and its two neighbors.  Fit a quadratic to these
     433three points.  The robust median value is the coordinate of the
     434quadratic which returns the 50\% value.
     435
    254436\end{document}
Note: See TracChangeset for help on using the changeset viewer.