source: trunk/src/Utils/Statistics.hh @ 1213

Last change on this file since 1213 was 1170, checked in by MatthewWhiting, 11 years ago

Fixing #184 - did not have the defined flag set in the StatsContainer?. Have written a new function to do so.

File size: 6.5 KB
Line 
1// -----------------------------------------------------------------------
2// Statistics.hh: Definition of the StatsContainer class, that holds
3// statistical parameters such as mean, median, rms, madfm.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#ifndef STATS_H
30#define STATS_H
31
32#include <iostream>
33#include <fstream>
34#include <math.h>
35
36/// A namespace to control everything to do with statistical
37/// calculations.
38
39namespace Statistics
40{
41
42  /// @brief Divide by the following correction factor to convert from MADFM to sigma (rms) estimator.
43  const float correctionFactor = 0.6744888;
44
45  /// @brief Multiply by the following correction factor to convert from trimmedSigma to sigma estimator.
46  const double trimToNormal = 1.17036753077;
47
48  /// @brief A templated function to do the MADFM-to-rms conversion.
49  template <class T> float madfmToSigma(T madfm);
50  /// @brief A non-templated function to do the rms-to-MADFM conversion.
51  float sigmaToMADFM(float sigma);
52
53
54  /// @brief
55  ///  Class to hold statistics for a given set of values.
56  /// @details
57  ///  This class is designed to hold all useful statistics for a given
58  ///  array of numbers.  It does *not* hold the data themselves. It
59  ///  provides the functions to calculate mean, rms, median and MADFM
60  ///  (median absolute deviation from median), as well as functions to
61  ///  control detection (ie. defining thresholds) in both standard
62  ///  (sigma-clipping) cases and False Detection Rate scenarios.
63
64  template <class Type>
65  class StatsContainer
66  {
67  public:
68    StatsContainer();
69    virtual ~StatsContainer(){};
70    StatsContainer(const StatsContainer<Type>& s);
71    StatsContainer<Type>& operator= (const StatsContainer<Type>& s);
72
73    /// @brief A way of printing the statistics.
74    template <class T>
75    friend std::ostream& operator<< ( std::ostream& theStream,
76                                      StatsContainer<T> &s);
77
78    float getMean(){return mean;};
79    void  setMean(float f){mean=f;};
80    float getStddev(){return stddev;};
81    void  setStddev(float f){stddev=f;};
82    Type  getMedian(){return median;};
83    void  setMedian(Type f){median=f;};
84    Type  getMadfm(){return madfm;};
85    void  setMadfm(Type f){madfm=f;};
86    float getThreshold(){return threshold;};
87    void  setThreshold(float f){threshold=f;};
88    float getPThreshold(){return pThreshold;};
89    void  setPThreshold(float f){pThreshold=f;};
90    bool  getRobust(){return useRobust;};
91    void  setRobust(bool b){useRobust=b;};
92    bool  setUseFDR(){return useFDR;};
93    void  setUseFDR(bool b){useFDR=b;};
94
95    /// @brief Return the threshold as a signal-to-noise ratio.
96    float getThresholdSNR();
97
98    /// @brief Set the threshold in units of a signal-to-noise ratio.
99    void  setThresholdSNR(float snr);
100
101    /// @brief Convert a value to a signal-to-noise ratio.
102    float valueToSNR(float value);
103
104    /// @brief Convert a signal-to-noise ratio to a flux value
105    float snrToValue(float snr);
106   
107    /// @brief Return the estimator of the middle value of the data.
108    float getMiddle();
109    void  setMiddle(float middle);
110   
111    /// @brief Return the estimator of the amount of spread of the data.
112    float getSpread();
113    void  setSpread(float spread);
114
115    /// @brief Scale the noise by a given factor.
116    void  scaleNoise(float scale);
117
118    /// @brief Return the Gaussian probability of a value given the stats.
119    float getPValue(float value);
120
121    /// @brief Is a value above the threshold?
122    bool isDetection(float value);
123
124    /// @brief Set the comment characters
125    void setCommentString(std::string comment){commentString = comment;};
126
127    // Functions to calculate the stats for a given array.
128    // The idea here is that there are two options to do the calculations:
129    //   *The first just uses all the points in the array. If you need to
130    //     remove BLANK points (or something similar), do this beforehand.
131    //   *Alternatively, construct a mask array of the same size,
132    //     showing which points are good, and use the second option.
133
134      /// @brief Directly set the statistics that have been calculated elsewhere.
135      void define(float mean, Type median, float stddev, Type madfm);
136
137    /// @brief Calculate statistics for all elements of a data array
138    void calculate(Type *array, long size);
139
140    /// @brief Calculate statistics for a subset of a data array
141    void calculate(Type *array, long size, bool *mask);
142
143    void writeToBinaryFile(std::string filename);
144    std::streampos readFromBinaryFile(std::string filename, std::streampos loc=0);
145
146  private:
147    bool   defined;      // a flag indicating whether the stats are defined.
148
149    float  mean;         ///< The mean, or average, of the values.
150    float  stddev;       ///< The standard deviation, or R.M.S., or sigma...
151    Type   median;       ///< The median of the values.
152    Type   madfm;        ///< The median absolute deviation from the median
153
154    float  threshold;    ///< a threshold for simple sigma-clipping
155    float  pThreshold;   ///< a threshold for the FDR case -- the upper limit of P values that detected pixels can have.
156    bool   useRobust;    ///< whether we use the two robust stats or not
157    bool   useFDR;       ///< whether the FDR method is used for determining a detection
158
159    std::string commentString; ///< Any comment characters etc that need to be prepended to any output via the << operator.
160
161  };
162
163}
164
165#endif // STATS_H
Note: See TracBrowser for help on using the repository browser.