![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <WNS/evaluation/statistics/moments.hpp> 00029 00030 #include <cmath> 00031 #include <climits> 00032 #include <cfloat> 00033 00034 using namespace wns::evaluation::statistics; 00035 00036 STATIC_FACTORY_REGISTER_WITH_CREATOR(Moments, 00037 StatEvalInterface, 00038 "openwns.evaluation.statistics.Moments", 00039 wns::PyConfigViewCreator); 00040 00041 00042 Moments::Moments(): 00043 StatEval(StatEval::fixed, "", ""), 00044 wSum_(0.0) 00045 {} 00046 00047 Moments::Moments(std::string name, 00048 std::string description, 00049 formatType format) 00050 : StatEval(format, name, description), 00051 wSum_(0.0) 00052 {} 00053 00054 Moments::Moments(const wns::pyconfig::View& config) : 00055 StatEval(config), 00056 wSum_(0.0) 00057 {} 00058 00059 00060 Moments::~Moments() 00061 {} 00062 00063 00064 void 00065 Moments::print(std::ostream& stream) const 00066 { 00067 std::string errorString; 00068 errorString = "I/O Error: Can't dump Moments results"; 00069 00070 printBanner(stream, 00071 (prefix_ + " Evaluation: Moments"), 00072 errorString); 00073 00074 if (!stream) 00075 { 00076 throw(wns::Exception(errorString)); 00077 } 00078 } 00079 00080 00081 void 00082 Moments::put(double xI, double wI) 00083 { 00084 // only positive weights 00085 assert(wI > 0.0); 00086 00087 xI *= scalingFactor_; 00088 00089 // this causes the square to be positive 00090 double square = wI * xI * xI; 00091 double cube = xI * square; 00092 sum_ += wI * xI; 00093 00094 // if all weights are 1, this is equal to numTrials_ 00095 // wSum_ is positive 00096 wSum_ += wI; 00097 squareSum_ += square; 00098 cubeSum_ += cube; 00099 00100 if (xI > maxValue_) 00101 { 00102 maxValue_ = xI; 00103 } 00104 00105 if (xI < minValue_) 00106 { 00107 minValue_ = xI; 00108 } 00109 00110 numTrials_++; 00111 } 00112 00113 void 00114 Moments::put(double xI) 00115 { 00116 put(xI, 1.0); 00117 } 00118 00119 00120 double 00121 Moments::mean() const 00122 { 00123 return numTrials_ ? sum_ / wSum_ : 0.0; 00124 } 00125 00126 double 00127 Moments::variance() const 00128 { 00129 double theMean; 00130 00131 if (numTrials_ > 1) 00132 { 00133 theMean = mean(); 00134 assert((sqrt(DBL_MAX) > fabs(theMean))); 00135 00136 double squareMean = theMean * theMean; 00137 00138 // wSum_ is always positive 00139 assert(DBL_MAX / squareMean > wSum_); 00140 double sumSquare = wSum_ * squareMean; 00141 00142 return std::max((squareSum_ - sumSquare) / 00143 (wSum_ - 1.0) , 0.0); 00144 } 00145 else 00146 { 00147 return 0.0; 00148 } 00149 } 00150 00151 double 00152 Moments::getConfidenceInterval95Mean() const 00153 { 00154 // ci = 0.95 00155 // --> p = 1-(1-cl)/2 = 0.975 00156 // --> norminv of p with mean 0 and variance 1 = 1.95996398454005 00157 00158 return this->getConfidenceIntervalMean(1.95996398454005); 00159 } 00160 00161 double 00162 Moments::getConfidenceInterval99Mean() const 00163 { 00164 // see above 00165 return this->getConfidenceIntervalMean(2.57582930354890); 00166 } 00167 00168 double 00169 Moments::getConfidenceIntervalMean(double x) const 00170 { 00171 if (this->numTrials_ < 100) 00172 { 00173 // need at least 100 trials to approximate the student-t distribution 00174 // with the Gaussian distribution (--> Error < 5%) 00175 return (DBL_MAX); 00176 } 00177 00178 double std = sqrt(this->variance()); 00179 unsigned long int n = this->trials(); 00180 00181 double cimu = (2*x*std)/(sqrt(n)); 00182 return cimu; 00183 } 00184 00185 double 00186 Moments::M2() const 00187 { 00188 return (wSum_ > getMaxError<double>()) ? 00189 (squareSum_ / wSum_) : 0.0; 00190 } 00191 00192 double 00193 Moments::M3() const 00194 { 00195 return (wSum_ > getMaxError<double>()) ? 00196 (cubeSum_ / wSum_) : 0.0 ; 00197 } 00198 00199 void 00200 Moments::reset() 00201 { 00202 StatEval::reset(); 00203 wSum_ = 0; 00204 }
1.5.5