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_HISTOGRAM2D_H 00032 00033 #define MEANWALKER_HISTOGRAM2D_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 Histogram2D : public errut::ErrorBase 00047 { 00048 public: 00049 Histogram2D(); 00050 ~Histogram2D(); 00051 00058 bool init(double minValueX, double maxValueX, size_t numBinsX, 00059 double minValueY, double maxValueY, size_t numBinsY); 00060 00062 void clear(); 00063 00065 void destroy(); 00066 00068 void process(double x, double y); 00069 00071 double getMinValueX() const { return m_minValueX; } 00072 00074 double getMaxValueX() const { return m_maxValueX; } 00075 00077 size_t getNumberOfBinsX() const { return m_numX; } 00078 00080 double getMinValueY() const { return m_minValueY; } 00081 00083 double getMaxValueY() const { return m_maxValueY; } 00084 00086 size_t getNumberOfBinsY() const { return m_numY; } 00087 00089 size_t getBinCount(size_t bin1, size_t bin2) const { return m_histogram[bin1 + bin2*m_numX]; } 00090 00092 double getProbabilityDensity(size_t bin1, size_t bin2) const { return ((double)m_histogram[bin1 + bin2*m_numX]/(double)m_totalCount)/m_binArea; } 00093 00096 double getInsideProbabilityDensity(size_t bin1, size_t bin2) const { return ((double)m_histogram[bin1 + bin2*m_numX]/(double)(m_totalCount-m_outsideCount))/m_binArea; } 00097 00099 double getBinCenterX(size_t bin1) const { return ((double)bin1+0.5)*m_binWidth+m_minValueX; } 00100 00102 double getBinCenterY(size_t bin2) const { return ((double)bin2+0.5)*m_binHeight+m_minValueY; } 00103 00105 double getBinWidth() const { return m_binWidth; } 00106 00108 double getBinHeight() const { return m_binHeight; } 00109 00111 size_t getTotalCount() const { return m_totalCount; } 00112 00114 size_t getOutsideCount() const { return m_outsideCount; } 00115 00117 size_t getInsideCount() const { return m_totalCount - m_outsideCount; } 00118 00120 void print(bool blocks = true) const; 00121 protected: 00122 std::vector<size_t> m_histogram; 00123 double m_minValueX, m_maxValueX, m_binWidth; 00124 double m_minValueY, m_maxValueY, m_binHeight; 00125 double m_doubleBinsX, m_doubleBinsY; 00126 double m_binArea; 00127 size_t m_totalCount, m_outsideCount; 00128 size_t m_numX, m_numY; 00129 }; 00130 00131 inline void Histogram2D::process(double x, double y) 00132 { 00133 double binDoubleX = (x-m_minValueX)/m_binWidth; 00134 double binDoubleY = (y-m_minValueY)/m_binHeight; 00135 00136 if (binDoubleX < 0 || binDoubleX >= m_doubleBinsX || 00137 binDoubleY < 0 || binDoubleY >= m_doubleBinsY) 00138 m_outsideCount++; 00139 else 00140 m_histogram[(int)binDoubleX + (int)binDoubleY * m_numX]++; 00141 00142 m_totalCount++; 00143 } 00144 00145 } // end namespace 00146 00147 #endif // MEANWALKER_HISTOGRAM2D_H