00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Math Library 00005 * Module : PoissonDistribution 00006 * Package : Zeus.ZeusMath.Stochastic 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef PoissonDistributionH 00033 #define PoissonDistributionH 00034 00035 #include <zeusmath/Stochastic/Interfaces/IDistribution.hpp> 00036 #include <zeusmath/System/Constants.h> 00037 #include <zeusbase/System/Float.h> 00038 00039 BEGIN_NAMESPACE_Zeus 00040 00041 /***************************************************************************/ 00045 /***************************************************************************/ 00046 zeusmath_class TPoissonDistribution : public IDistribution 00047 { 00048 public: 00049 /***********************************************************************/ 00053 /***********************************************************************/ 00054 inline TPoissonDistribution(const Float& rfLambda) 00055 { 00056 m_fLambda = rfLambda; 00057 } 00058 00059 /***********************************************************************/ 00062 /***********************************************************************/ 00063 virtual inline ~TPoissonDistribution() {} 00064 00065 static Float getProbability(const Float& rfLambda, const Float& rfX); 00066 00067 //methods of IDistribution 00068 virtual Float MQUALIFIER getEntropy() const; 00069 virtual Float MQUALIFIER getExpectedValue() const; 00070 virtual Float MQUALIFIER getMean() const; 00071 virtual Float MQUALIFIER getMedian() const; 00072 virtual Float MQUALIFIER getVariance() const; 00073 virtual Float MQUALIFIER getMode() const; 00074 virtual Float MQUALIFIER getSkewness() const; 00075 virtual Float MQUALIFIER getCumulativeProb(const Float& rfStart, const Float& rfEnd) const; 00076 virtual Float MQUALIFIER getStdDeviation() const; 00077 virtual Float MQUALIFIER getProb(const Float& rfX) const; 00078 virtual Float MQUALIFIER getSampleProb(Int iSample) const; 00079 00080 private: 00082 Float m_fLambda; 00083 00084 static Float getCumulativeProb_internal(const Float& rfLambda, const Float& rfEnd); 00085 }; 00086 00087 //INLINE METHODS 00088 /****************************************************************************/ 00091 /****************************************************************************/ 00092 inline Float MQUALIFIER TPoissonDistribution::getEntropy() const 00093 { 00094 return 0.5 * TMath::ln(TConstants::Pi * TMath::exp(1)*m_fLambda) - 1.0/(12*m_fLambda) - 00095 1.0/(241*m_fLambda*m_fLambda) - 19.0/(360*m_fLambda*m_fLambda*m_fLambda); 00096 } 00097 00098 /****************************************************************************/ 00101 /****************************************************************************/ 00102 inline Float MQUALIFIER TPoissonDistribution::getExpectedValue() const 00103 { 00104 return m_fLambda; 00105 } 00106 00107 /****************************************************************************/ 00110 /****************************************************************************/ 00111 inline Float MQUALIFIER TPoissonDistribution::getMean() const 00112 { 00113 return m_fLambda; 00114 } 00115 00116 /****************************************************************************/ 00119 /****************************************************************************/ 00120 inline Float MQUALIFIER TPoissonDistribution::getMedian() const 00121 { 00122 //formula taken from wikipedia 00123 return TMath::floor(m_fLambda + 1.0/3.0 - 0.02/m_fLambda); 00124 } 00125 00126 /****************************************************************************/ 00129 /****************************************************************************/ 00130 inline Float MQUALIFIER TPoissonDistribution::getVariance() const 00131 { 00132 return m_fLambda; 00133 } 00134 00135 /****************************************************************************/ 00138 /****************************************************************************/ 00139 inline Float MQUALIFIER TPoissonDistribution::getStdDeviation() const 00140 { 00141 return TMath::sqrt(m_fLambda); 00142 } 00143 00144 /****************************************************************************/ 00147 /****************************************************************************/ 00148 inline Float MQUALIFIER TPoissonDistribution::getMode() const 00149 { 00150 Float fRetval = TMath::floor(m_fLambda); 00151 00152 //if lambda is an interger 00153 if (TFloat::equalsFloat(m_fLambda, fRetval)) 00154 { 00155 fRetval = m_fLambda - 1; 00156 } 00157 return fRetval; 00158 } 00159 00160 /****************************************************************************/ 00163 /****************************************************************************/ 00164 inline Float MQUALIFIER TPoissonDistribution::getSkewness() const 00165 { 00166 return TMath::pow(m_fLambda, -0.5); 00167 } 00168 00169 /****************************************************************************/ 00172 /****************************************************************************/ 00173 inline Float MQUALIFIER TPoissonDistribution::getProb(const Float& rfX) const 00174 { 00175 return TPoissonDistribution::getProbability(m_fLambda, rfX); 00176 } 00177 00178 /****************************************************************************/ 00181 /****************************************************************************/ 00182 inline Float MQUALIFIER TPoissonDistribution::getSampleProb(Int iSample) const 00183 { 00184 return TPoissonDistribution::getProbability(m_fLambda, iSample); 00185 } 00186 00187 00188 /****************************************************************************/ 00194 /****************************************************************************/ 00195 inline /*static*/ Float TPoissonDistribution::getProbability(const Float& rfLambda, const Float& rfX) 00196 { 00197 return TMath::pow(rfLambda, rfX) / TFloat::getFactorial(rfX) * TMath::exp(-rfLambda); 00198 } 00199 00200 00201 END_NAMESPACE_Zeus 00202 00203 #endif