00001 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 "tokenwriter.h" 00013 #include "tokenreader.h" 00014 #include "tp_timeinterval.h" 00015 00016 00017 namespace faudes { 00018 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 00157 */ 00158 00159 class SampledDensityFunction : public DiscreteDensityFunction { 00160 00161 public: 00162 00163 // construct destruct 00164 SampledDensityFunction(void); 00165 virtual ~SampledDensityFunction(void) {}; 00166 00167 // set/get dimension 00168 void Dim(Idx dim) { mDim=dim; Clear();}; 00169 Idx Dim(void) const { return mDim;}; 00170 00171 // clear all 00172 void Clear(void); 00173 00174 // add sample 00175 void Sample(tpTime::Type time); 00176 00177 // pretty string 00178 std::string SStr(void) const; 00179 00180 protected: 00181 00182 // data per support interval 00183 typedef struct { 00184 TimeInterval timeint; // support 00185 Idx count; // count 00186 } CountEntry; 00187 00188 // convenience typedef 00189 typedef std::map<tpTime::Type,CountEntry>::iterator CountIterator; 00190 typedef std::map<tpTime::Type,CountEntry>::const_iterator CCountIterator; 00191 00192 // map of all support intervals 00193 std::map<tpTime::Type,CountEntry> mCountMap; 00194 00195 // my target dimension; 00196 Idx mDim; 00197 00198 // overall count 00199 double mCountSum; 00200 double mCountSquareSum; 00201 00202 // compute characteristics (non const version) 00203 virtual void CompileNonConst(void); 00204 00205 }; 00206 00207 00208 00209 00210 00211 } // namespace 00212 00213 #endif