00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Math Library 00005 * Module : BinomialDistribution 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 BinomialDistributionH 00033 #define BinomialDistributionH 00034 00035 #include <zeusmath/Stochastic/Interfaces/IDistribution.hpp> 00036 #include <zeusmath/Stochastic/Distribution.h> 00037 #include <zeusmath/System/Binomial.h> 00038 #include <zeusbase/System/Float.h> 00039 #include <math.h> 00040 00041 BEGIN_NAMESPACE_Zeus 00042 00043 /****************************************************************************/ 00046 /****************************************************************************/ 00047 zeusmath_class TBinomialDistribution : public IDistribution 00048 { 00049 public: 00050 /************************************************************************/ 00055 /************************************************************************/ 00056 inline TBinomialDistribution(Int iN, const Float& rfProb) 00057 { 00058 m_iN = iN; 00059 m_fProb = rfProb; 00060 } 00061 00062 /************************************************************************/ 00065 /************************************************************************/ 00066 inline virtual ~TBinomialDistribution() 00067 {} 00068 00069 //static methods 00070 static Float getProbability(Int iN, Int iK, const Float& rfProb); 00071 static void sampleDistribution(Int iN, const Float& rfProb, TArrayList<TMappedValue>& rlstData); 00072 static TDistribution sampleDistribution(Int iN, const Float& fProb); 00073 00074 //methods of IDistribution 00075 virtual Float MQUALIFIER getEntropy() const; 00076 virtual Float MQUALIFIER getExpectedValue() const; 00077 virtual Float MQUALIFIER getMean() const; 00078 virtual Float MQUALIFIER getMedian() const; 00079 virtual Float MQUALIFIER getVariance() const; 00080 virtual Float MQUALIFIER getStdDeviation() const; 00081 virtual Float MQUALIFIER getMode() const; 00082 virtual Float MQUALIFIER getSkewness() const; 00083 virtual Float MQUALIFIER getCumulativeProb(const Float& rfStart, const Float& rfEnd) const; 00084 virtual Float MQUALIFIER getProb(const Float& rfX) const; 00085 virtual Float MQUALIFIER getSampleProb(Int iSample) const; 00086 00087 00088 private: 00090 Int m_iN; 00092 Float m_fProb; 00093 00094 }; 00095 00096 //INLINE METHODS 00097 /****************************************************************************/ 00100 /****************************************************************************/ 00101 inline Float MQUALIFIER TBinomialDistribution::getEntropy() const 00102 { 00103 return 0.5 * TMath::ln(2 * TConstants::Pi * m_iN * m_fProb * TMath::exp(1) * (1.0 - m_fProb)) + 1.0 / m_iN; 00104 } 00105 00106 /****************************************************************************/ 00109 /****************************************************************************/ 00110 inline Float MQUALIFIER TBinomialDistribution::getExpectedValue() const 00111 { 00112 return m_iN * m_fProb; 00113 } 00114 00115 /****************************************************************************/ 00118 /****************************************************************************/ 00119 inline Float MQUALIFIER TBinomialDistribution::getMean() const 00120 { 00121 return m_iN * m_fProb; 00122 } 00123 00124 /****************************************************************************/ 00127 /****************************************************************************/ 00128 inline Float MQUALIFIER TBinomialDistribution::getMedian() const 00129 { 00130 //one of the floor(np) -1, floor (np), floor(np) +1 is the median 00131 //instead of floor(np) we take round(np) which will include both 00132 // floor(np) and floor(np)+1 with is ceil(np) in fact 00133 return TMath::roundInt(m_iN * m_fProb); 00134 } 00135 00136 /****************************************************************************/ 00139 /****************************************************************************/ 00140 inline Float MQUALIFIER TBinomialDistribution::getVariance() const 00141 { 00142 return m_iN * m_fProb * (1 - m_fProb); 00143 } 00144 00145 /****************************************************************************/ 00148 /****************************************************************************/ 00149 inline Float MQUALIFIER TBinomialDistribution::getStdDeviation() const 00150 { 00151 return TMath::sqrt(this->getVariance()); 00152 } 00153 00154 /****************************************************************************/ 00157 /****************************************************************************/ 00158 inline Float MQUALIFIER TBinomialDistribution::getMode() const 00159 { 00160 return TMath::floor((m_iN + 1)*m_fProb); 00161 } 00162 00163 /****************************************************************************/ 00166 /****************************************************************************/ 00167 inline Float MQUALIFIER TBinomialDistribution::getSkewness() const 00168 { 00169 return (1.0 - 2 * m_fProb) / TMath::sqrt(m_iN * m_fProb * (1.0 - m_fProb)); 00170 } 00171 00172 /****************************************************************************/ 00175 /****************************************************************************/ 00176 inline Float MQUALIFIER TBinomialDistribution::getProb(const Float& /*rfX*/) const 00177 { 00178 return 0.0; //not defined 00179 } 00180 00181 /****************************************************************************/ 00184 /****************************************************************************/ 00185 inline Float MQUALIFIER TBinomialDistribution::getSampleProb(Int iSample) const 00186 { 00187 return TBinomialDistribution::getProbability(m_iN, iSample, m_fProb); 00188 } 00189 00190 /****************************************************************************/ 00197 /****************************************************************************/ 00198 /*static*/ inline Float TBinomialDistribution::getProbability(Int iN, Int iK, const Float& rfProb) 00199 { 00200 return TBinomial::getBinomialF(iN, iK) * TMath::pow(rfProb, iK) * TMath::pow(1.0 - rfProb, iN - iK); 00201 } 00202 00203 00204 END_NAMESPACE_Zeus 00205 00206 00207 #endif