sp_densityfnct.hGo to the documentation of this file.00001 /** @file sp_densityfnct.h Discrete density function approximation */ 00002 00003 /* 00004 Copyright (C) 2008 Thomas Moor 00005 Exclusive copyright is granted to Klaus Schmidt 00006 */ 00007 00008 00009 #ifndef FAUDES_SP_DENSITYFNCT_H 00010 #define FAUDES_SP_DENSITYFNCT_H 00011 00012 #include "corefaudes.h" 00013 #include "tp_include.h" 00014 00015 00016 namespace faudes { 00017 00018 /** 00019 00020 Density Function 00021 00022 A DiscreteDensityFnction models a piecewise constant 00023 map with non-negative support. It consists of a 00024 sorted vector (aka map) of disjoint time intervals 00025 with one associated value each. 00026 00027 The current implementation is rather incomplete, 00028 inefficient, and buggy. The ToDo list includes 00029 - we assume consecutive right open intervals, so either 00030 assert the restriction or implement more general 00031 - we assume an integer time type, so either assert the restriction 00032 or implement more general 00033 - fix token io 00034 - need proper access methods 00035 - implement method to evaluate random variable with given density 00036 - generalise to class DensityFunction to include some continuous functions 00037 eg exponential, gauss 00038 - have this a proper faudes type 00039 00040 00041 */ 00042 class DiscreteDensityFunction { 00043 00044 public: 00045 00046 // construct destruct 00047 DiscreteDensityFunction(void); 00048 virtual ~DiscreteDensityFunction(void) {}; 00049 00050 // clear all 00051 virtual void Clear(void); 00052 00053 // get values 00054 double Value(tpTime::Type time) const; 00055 const TimeInterval& TimeInt(tpTime::Type time) const; 00056 00057 // data per support interval 00058 typedef struct { 00059 TimeInterval timeint; // support 00060 double value; // value 00061 } Entry; 00062 00063 // convenience typedef 00064 typedef std::map<tpTime::Type,Entry>::const_iterator CIterator; 00065 typedef std::map<tpTime::Type,Entry>::iterator Iterator; 00066 00067 // access map 00068 CIterator Begin(void) const { return mValueMap.begin();} ; 00069 CIterator End(void) const { return mValueMap.end();} ; 00070 CIterator At(tpTime::Type time) const { return mValueMap.lower_bound(time);} ; 00071 const TimeInterval& TimeInt(CIterator mit) const { return mit->second.timeint;}; 00072 const double& Value(CIterator mit) const { return mit->second.value;}; 00073 00074 // access map (should be protected) 00075 Iterator Begin(void) { return mValueMap.begin();} ; 00076 Iterator End(void) { return mValueMap.end();} ; 00077 Iterator At(tpTime::Type time) { return mValueMap.lower_bound(time);} ; 00078 const Entry EntryAt(tpTime::Type time) const; 00079 00080 // get/set name 00081 const std::string& Name(void) const { return mName;}; 00082 void Name(const std::string& rName) { mName=rName;}; 00083 00084 // get/set count (-1 for not avail) 00085 int Count(void) const { return mCount;}; 00086 void Count(int count) { mCount= count;}; 00087 00088 // get characteristics (set by compile) 00089 double MaxValue(void) const { return mMaxValue; }; 00090 double MinValue(void) const { return mMinValue; }; 00091 tpTime::Type MaxTime(void) const { return mMaxTime; }; 00092 tpTime::Type MinTime(void) const { return mMinTime; }; 00093 double Sum(void) const { return mSum; }; 00094 double SquareSum(void) const { return mSquareSum; }; 00095 double Average(void) const { return mAverage; }; 00096 double Variance(void) const { return mVariance; }; 00097 double Quantile05(void) const { return mQuantile05; }; 00098 double Quantile95(void) const { return mQuantile95; }; 00099 00100 // file io 00101 void Write(TokenWriter& rTw) const; 00102 void Write(void) const; 00103 std::string ToString(void) const; 00104 void Read(TokenReader& rTr); 00105 00106 00107 // compute characteristics 00108 void Compile(void) const; 00109 00110 // pretty string 00111 std::string Str(void) const; 00112 00113 protected: 00114 00115 // map of all support intervals 00116 std::map<tpTime::Type,Entry> mValueMap; 00117 00118 // user data 00119 std::string mName; 00120 int mCount; 00121 00122 // characteristics 00123 double mMaxValue; 00124 double mMinValue; 00125 tpTime::Type mMaxTime; 00126 tpTime::Type mMinTime; 00127 double mSum; 00128 double mSquareSum; 00129 double mAverage; 00130 double mVariance; 00131 double mQuantile05; 00132 double mQuantile95; 00133 00134 // compute characteristics (non const version) 00135 virtual void CompileNonConst(void); 00136 00137 00138 }; 00139 00140 00141 00142 /* 00143 00144 Sampled Density Function 00145 00146 A SampledDensityFunction is a DiscreteDensityFunction that 00147 is set up by sampling experiment outcome. This implementation 00148 attempts to be smart in choosing sampling intervals and, hence, 00149 requires minimal configuration. 00150 00151 The current implementation is rather incomplete, 00152 inefficient, and buggy. The ToDo list includes 00153 - interface incomplete 00154 - compilation faulty 00155 - no token io 00156 - need to improve automatic sampling interval selection 00157 00158 */ 00159 00160 class SampledDensityFunction : public DiscreteDensityFunction { 00161 00162 public: 00163 00164 // construct destruct 00165 SampledDensityFunction(void); 00166 virtual ~SampledDensityFunction(void) {}; 00167 00168 // set/get dimension 00169 void Dim(Idx dim) { mDim=dim; Clear();}; 00170 Idx Dim(void) const { return mDim;}; 00171 00172 // clear all 00173 void Clear(void); 00174 00175 // add sample 00176 void Sample(tpTime::Type time); 00177 00178 // pretty string 00179 std::string SStr(void) const; 00180 00181 protected: 00182 00183 // data per support interval 00184 typedef struct { 00185 TimeInterval timeint; // support 00186 Idx count; // count 00187 } CountEntry; 00188 00189 // convenience typedef 00190 typedef std::map<tpTime::Type,CountEntry>::iterator CountIterator; 00191 typedef std::map<tpTime::Type,CountEntry>::const_iterator CCountIterator; 00192 00193 // map of all support intervals 00194 std::map<tpTime::Type,CountEntry> mCountMap; 00195 00196 // my target dimension; 00197 Idx mDim; 00198 00199 // overall count 00200 double mCountSum; 00201 double mCountSquareSum; 00202 00203 // compute characteristics (non const version) 00204 virtual void CompileNonConst(void); 00205 00206 }; 00207 00208 00209 00210 00211 00212 } // namespace 00213 00214 #endif libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen |