MeanWalker
|
00001 /* 00002 00003 This file is a part of MeanWalker, a library which provides utilities to 00004 sample from probability distributions using methods like the 00005 Metropilis-Hastings algorithm and Goodman-Weare algorithm. 00006 00007 Copyright (C) 2012 Jori Liesenborgs 00008 00009 Contact: jori.liesenborgs@gmail.com 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 GNU General Public License for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with this program; if not, write to the Free Software 00023 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00024 00025 */ 00026 00031 #ifndef MEANWALKER_HISTOGRAM_H 00032 00033 #define MEANWALKER_HISTOGRAM_H 00034 00035 #include "meanwalkerconfig.h" 00036 #include <errut/errorbase.h> 00037 #include <stdint.h> 00038 #include <iostream> 00039 #include <vector> 00040 #include <list> 00041 00042 namespace meanwalker 00043 { 00044 00046 class MEANWALKER_IMPORTEXPORT Histogram : public errut::ErrorBase 00047 { 00048 public: 00049 Histogram(); 00050 ~Histogram(); 00051 00055 bool init(double minValue, double maxValue, size_t numBins); 00056 00058 void clear(); 00059 00061 void destroy(); 00062 00064 void process(double x); 00065 00067 double getMinValue() const { return m_minValue; } 00068 00070 double getMaxValue() const { return m_maxValue; } 00071 00073 size_t getNumberOfBins() const { return m_histogram.size(); } 00074 00076 size_t getBinCount(size_t bin) const { return m_histogram[bin]; } 00077 00079 double getProbabilityDensity(size_t bin) const { return ((double)m_histogram[bin]/(double)m_totalCount)/m_binSize; } 00080 00083 double getInsideProbabilityDensity(size_t bin) const { return ((double)m_histogram[bin]/(double)(m_totalCount-m_outsideCount))/m_binSize; } 00084 00086 double getBinCenter(size_t bin) const { return ((double)bin+0.5)*m_binSize+m_minValue; } 00087 00089 double getBinWidth() const { return m_binSize; } 00090 00092 size_t getTotalCount() const { return m_totalCount; } 00093 00095 size_t getOutsideCount() const { return m_outsideCount; } 00096 00098 size_t getInsideCount() const { return m_totalCount - m_outsideCount; } 00099 00101 void print() const; 00102 protected: 00103 std::vector<size_t> m_histogram; 00104 double m_minValue, m_maxValue, m_binSize; 00105 double m_doubleBins; 00106 size_t m_totalCount, m_outsideCount; 00107 }; 00108 00109 inline void Histogram::process(double x) 00110 { 00111 double binDouble = (x-m_minValue)/m_binSize; 00112 00113 if (binDouble < 0 || binDouble >= m_doubleBins) 00114 m_outsideCount++; 00115 else 00116 m_histogram[(int)binDouble]++; 00117 00118 m_totalCount++; 00119 } 00120 00121 class MEANWALKER_IMPORTEXPORT DelayedHistogram : public Histogram 00122 { 00123 public: 00124 DelayedHistogram(); 00125 void record(double value); 00126 bool processRecordedValues(size_t numBins); 00127 void clearRecordedValues() { m_recordedValues.clear(); m_recPos = 0; } 00128 00129 const double *getRecordedValues() const { return &(m_recordedValues[0]); } 00130 size_t recordSize() const { return m_recPos; } 00131 void reserveMemory(size_t entries) { m_recordedValues.resize(entries); m_recPos = 0; } 00132 00133 static void getDefaultHistogramMinMax(const double *pValues, size_t numValues, double &minValue, double &maxValue); 00134 protected: 00135 virtual void getHistogramMinMax(const double *pValues, size_t numValues, double &minValue, double &maxValue) 00136 { getDefaultHistogramMinMax(pValues, numValues, minValue, maxValue); } 00137 private: 00138 std::vector<double> m_recordedValues; 00139 size_t m_recPos; 00140 }; 00141 00142 inline void DelayedHistogram::record(double value) 00143 { 00144 if (m_histogram.size() != 0) 00145 return; 00146 00147 if (m_recPos < m_recordedValues.size()) 00148 { 00149 m_recordedValues[m_recPos] = value; 00150 m_recPos++; 00151 } 00152 else 00153 std::cerr << "WARNING: " << m_recPos << " exceeds " << m_recordedValues.size() << std::endl; 00154 } 00155 00156 } // end namespace 00157 00158 #endif // MEANWALKER_HISTOGRAM_H