[301] | 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 | // ----------------------------------------------------------------------- |
---|
[190] | 29 | #ifndef STATS_H |
---|
| 30 | #define STATS_H |
---|
| 31 | |
---|
[201] | 32 | #include <iostream> |
---|
[190] | 33 | #include <math.h> |
---|
| 34 | |
---|
[528] | 35 | /// A namespace to control everything to do with statistical |
---|
| 36 | /// calculations. |
---|
[221] | 37 | |
---|
[190] | 38 | namespace Statistics |
---|
| 39 | { |
---|
| 40 | |
---|
[528] | 41 | /// @brief Divide by the following correction factor to convert from MADFM to sigma (rms) estimator. |
---|
[190] | 42 | const float correctionFactor = 0.6744888; |
---|
[221] | 43 | |
---|
[528] | 44 | /// @brief Multiply by the following correction factor to convert from trimmedSigma to sigma estimator. |
---|
[190] | 45 | const double trimToNormal = 1.17036753077; |
---|
| 46 | |
---|
[528] | 47 | /// @brief A templated function to do the MADFM-to-rms conversion. |
---|
[266] | 48 | template <class T> float madfmToSigma(T madfm); |
---|
[528] | 49 | /// @brief A non-templated function to do the rms-to-MADFM conversion. |
---|
[282] | 50 | float sigmaToMADFM(float sigma); |
---|
[190] | 51 | |
---|
[266] | 52 | |
---|
[528] | 53 | /// @brief |
---|
| 54 | /// Class to hold statistics for a given set of values. |
---|
| 55 | /// @details |
---|
| 56 | /// This class is designed to hold all useful statistics for a given |
---|
| 57 | /// array of numbers. It does *not* hold the data themselves. It |
---|
| 58 | /// provides the functions to calculate mean, rms, median and MADFM |
---|
| 59 | /// (median absolute deviation from median), as well as functions to |
---|
| 60 | /// control detection (ie. defining thresholds) in both standard |
---|
| 61 | /// (sigma-clipping) cases and False Detection Rate scenarios. |
---|
[221] | 62 | |
---|
[190] | 63 | template <class Type> |
---|
| 64 | class StatsContainer |
---|
| 65 | { |
---|
| 66 | public: |
---|
[205] | 67 | StatsContainer(){useRobust=true; defined=false; useFDR=false;}; |
---|
[190] | 68 | virtual ~StatsContainer(){}; |
---|
[192] | 69 | StatsContainer(const StatsContainer<Type>& s); |
---|
| 70 | StatsContainer<Type>& operator= (const StatsContainer<Type>& s); |
---|
[221] | 71 | |
---|
[528] | 72 | /// @brief A way of printing the statistics. |
---|
[222] | 73 | template <class T> |
---|
[282] | 74 | friend std::ostream& operator<< ( std::ostream& theStream, |
---|
| 75 | StatsContainer<T> &s); |
---|
[190] | 76 | |
---|
| 77 | float getMean(){return mean;}; |
---|
| 78 | void setMean(float f){mean=f;}; |
---|
| 79 | float getStddev(){return stddev;}; |
---|
| 80 | void setStddev(float f){stddev=f;}; |
---|
| 81 | Type getMedian(){return median;}; |
---|
| 82 | void setMedian(Type f){median=f;}; |
---|
| 83 | Type getMadfm(){return madfm;}; |
---|
| 84 | void setMadfm(Type f){madfm=f;}; |
---|
| 85 | float getThreshold(){return threshold;}; |
---|
| 86 | void setThreshold(float f){threshold=f;}; |
---|
| 87 | float getPThreshold(){return pThreshold;}; |
---|
| 88 | void setPThreshold(float f){pThreshold=f;}; |
---|
| 89 | bool getRobust(){return useRobust;}; |
---|
| 90 | void setRobust(bool b){useRobust=b;}; |
---|
| 91 | bool setUseFDR(){return useFDR;}; |
---|
| 92 | void setUseFDR(bool b){useFDR=b;}; |
---|
| 93 | |
---|
[528] | 94 | /// @brief Return the threshold as a signal-to-noise ratio. |
---|
[271] | 95 | float getThresholdSNR(); |
---|
[222] | 96 | |
---|
[528] | 97 | /// @brief Set the threshold in units of a signal-to-noise ratio. |
---|
[271] | 98 | void setThresholdSNR(float snr); |
---|
| 99 | |
---|
[528] | 100 | /// @brief Convert a value to a signal-to-noise ratio. |
---|
[481] | 101 | float valueToSNR(float value); |
---|
| 102 | |
---|
[528] | 103 | /// @brief Convert a signal-to-noise ratio to a flux value |
---|
[481] | 104 | float snrToValue(float snr); |
---|
[258] | 105 | |
---|
[528] | 106 | /// @brief Return the estimator of the middle value of the data. |
---|
[271] | 107 | float getMiddle(); |
---|
[475] | 108 | void setMiddle(float middle); |
---|
[271] | 109 | |
---|
[528] | 110 | /// @brief Return the estimator of the amount of spread of the data. |
---|
[271] | 111 | float getSpread(); |
---|
[475] | 112 | void setSpread(float spread); |
---|
[222] | 113 | |
---|
[528] | 114 | /// @brief Scale the noise by a given factor. |
---|
[275] | 115 | void scaleNoise(float scale); |
---|
| 116 | |
---|
[528] | 117 | /// @brief Return the Gaussian probability of a value given the stats. |
---|
[271] | 118 | float getPValue(float value); |
---|
[190] | 119 | |
---|
[528] | 120 | /// @brief Is a value above the threshold? |
---|
[271] | 121 | bool isDetection(float value); |
---|
[190] | 122 | |
---|
| 123 | // Functions to calculate the stats for a given array. |
---|
| 124 | // The idea here is that there are two options to do the calculations: |
---|
| 125 | // *The first just uses all the points in the array. If you need to |
---|
| 126 | // remove BLANK points (or something similar), do this beforehand. |
---|
[282] | 127 | // *Alternatively, construct a mask array of the same size, |
---|
| 128 | // showing which points are good, and use the second option. |
---|
[222] | 129 | |
---|
[528] | 130 | /// @brief Calculate statistics for all elements of a data array |
---|
[190] | 131 | void calculate(Type *array, long size); |
---|
[222] | 132 | |
---|
[528] | 133 | /// @brief Calculate statistics for a subset of a data array |
---|
[282] | 134 | void calculate(Type *array, long size, bool *mask); |
---|
[190] | 135 | |
---|
| 136 | private: |
---|
| 137 | bool defined; // a flag indicating whether the stats are defined. |
---|
| 138 | |
---|
[258] | 139 | float mean; ///< The mean, or average, of the values. |
---|
[221] | 140 | float stddev; ///< The standard deviation, or R.M.S., or sigma... |
---|
[258] | 141 | Type median; ///< The median of the values. |
---|
[221] | 142 | Type madfm; ///< The median absolute deviation from the median |
---|
[190] | 143 | |
---|
[221] | 144 | float threshold; ///< a threshold for simple sigma-clipping |
---|
[528] | 145 | float pThreshold; ///< a threshold for the FDR case -- the upper limit of P values that detected pixels can have. |
---|
[221] | 146 | bool useRobust; ///< whether we use the two robust stats or not |
---|
[528] | 147 | bool useFDR; ///< whether the FDR method is used for determining a detection |
---|
[190] | 148 | |
---|
| 149 | }; |
---|
| 150 | |
---|
| 151 | } |
---|
| 152 | |
---|
[531] | 153 | #endif // STATS_H |
---|