Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/libBM.h
r32 r33 31 31 //! len = number of individual rvs 32 32 int len; 33 //! Vector of unique IDs 33 34 ivec ids; 35 //! Vector of sizes 34 36 ivec sizes; 37 //! Vector of shifts from current time 35 38 ivec times; 36 ivec obs;39 //! Array of names 37 40 Array<std::string> names; 38 41 39 42 private: 43 //! auxiliary function used in constructor 40 44 void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times); 41 45 public: … … 78 82 class fnc { 79 83 protected: 84 //! Length of the output vector 80 85 int dimy; 81 86 public: … … 83 88 virtual vec eval ( const vec &cond ) { 84 89 return vec ( 0 ); 85 }; //Fixme: virtual?90 }; 86 91 87 92 //! access function … … 97 102 class epdf { 98 103 protected: 104 //! Identified of the random variable 99 105 RV rv; 100 106 public: … … 140 146 //! Returns the required moment of the epdf 141 147 // virtual fnc moment ( const int order = 1 ); 142 //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$.\param ll is a return value of log-likelihood of the sample.148 //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv \param ll is a return value of log-likelihood of the sample. 143 149 virtual vec samplecond ( vec &cond, double &ll ) {this->condition(cond);vec temp= ep->sample();ll=ep->evalpdflog(temp);return temp;}; 144 150 //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond 145 151 virtual void condition ( const vec &cond ) {}; 146 152 153 //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. 147 154 virtual double evalcond (const vec &dt, const vec &cond ) {this->condition(cond);return ep->eval(dt);}; 148 155 … … 238 245 class BMcond { 239 246 protected: 247 //! Identificator of the conditioning variable 240 248 RV rvc; 241 249 public: 242 250 //! Substitute \c val for \c rvc. 243 251 virtual void condition ( const vec &val ) =0; 252 //! Default constructor 244 253 BMcond(RV &rv0):rvc(rv0){}; 254 //! Destructor for future use 245 255 virtual ~BMcond(){}; 256 //! access function 246 257 const RV& _rvc() const {return rvc;} 247 258 }; -
bdm/stat/libDS.h
r19 r33 39 39 void write(vec &ut,ivec &indexes){it_error("MemDS::write is not supported");} 40 40 void step(); 41 //!Default constructor 41 42 MemDS(mat &Dat, ivec &rowid, ivec &delays); 42 43 }; -
bdm/stat/libEF.h
r32 r33 25 25 //! Global Uniform_RNG 26 26 extern Uniform_RNG UniRNG; 27 //! Global Normal_RNG 27 28 extern Normal_RNG NorRNG; 29 //! Global Gamma_RNG 28 30 extern Gamma_RNG GamRNG; 29 30 31 31 32 /*! … … 41 42 //! default constructor 42 43 eEF ( const RV &rv ) :epdf ( rv ) {}; 43 44 //!TODO decide if it is really needed 44 45 virtual void tupdate ( double phi, mat &vbar, double nubar ) {}; 45 46 //!TODO decide if it is really needed 46 47 virtual void dupdate ( mat &v,double nu=1.0 ) {}; 47 48 }; 48 49 50 /*! 51 * \brief Exponential family model. 52 53 * More?... 54 */ 55 49 56 class mEF : public mpdf { 50 57 51 58 public: 59 //! Default constructor 52 60 mEF ( const RV &rv0, const RV &rvc0 ) :mpdf ( rv0,rvc0 ) {}; 53 61 }; … … 74 82 public: 75 83 // enorm() :eEF() {}; 76 84 //!Default constructor 77 85 enorm ( RV &rv ); 86 //! Set mean value \c mu and covariance \c R 78 87 void set_parameters ( const vec &mu,const sq_T &R ); 79 88 //! tupdate in exponential form (not really handy) 80 89 void tupdate ( double phi, mat &vbar, double nubar ); 90 //! dupdate in exponential form (not really handy) 81 91 void dupdate ( mat &v,double nu=1.0 ); 82 92 83 93 vec sample() const; 94 //! TODO is it used? 84 95 mat sample ( int N ) const; 85 96 double eval ( const vec &val ) const ; … … 106 117 Multvariate Gamma density as product of independent univariate densities. 107 118 \f[ 108 f(x| a,b) = \prod f(x_i|a_i,b_i)119 f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) 109 120 \f] 110 121 */ … … 112 123 class egamma : public eEF { 113 124 protected: 125 //! Vector \f$\alpha\f$ 114 126 vec alpha; 127 //! Vector \f$\beta\f$ 115 128 vec beta; 116 129 public : … … 120 133 void set_parameters ( const vec &a, const vec &b ) {alpha=a,beta=b;}; 121 134 vec sample() const; 135 //! TODO: is it used anywhere? 122 136 mat sample ( int N ) const; 123 137 double evalpdflog ( const vec &val ) const; … … 126 140 vec mean()const {vec pom(alpha); pom/=beta; return pom;} 127 141 }; 128 142 /* 129 143 //! Weighted mixture of epdfs with external owned components. 130 144 class emix : public epdf { … … 140 154 vec sample() {it_error ( "Not implemented" );return 0;} 141 155 }; 156 */ 142 157 143 158 //! Uniform distributed density on a rectangular support … … 152 167 vec distance; 153 168 //! normalizing coefficients 154 double nk,lnk; 155 public: 169 double nk; 170 //! cache of log( \c nk ) 171 double lnk; 172 public: 173 //! Defualt constructor 156 174 euni ( const RV rv ) :epdf ( rv ) {} 157 175 double eval ( const vec &val ) const {return nk;} … … 161 179 return low+distance*smp; 162 180 } 181 //! set values of \c low and \c high 163 182 void set_parameters ( const vec &low0, const vec &high0 ) { 164 183 distance = high0-low0; … … 180 199 template<class sq_T> 181 200 class mlnorm : public mEF { 201 //! Internal epdf that arise by conditioning on \c rvc 182 202 enorm<sq_T> epdf; 183 203 vec* _mu; //cached epdf.mu; … … 186 206 //! Constructor 187 207 mlnorm ( RV &rv,RV &rvc ); 208 //! Set \c A and \c R 188 209 void set_parameters ( const mat &A, const sq_T &R ); 189 210 //!Generate one sample of the posterior … … 191 212 //!Generate matrix of samples of the posterior 192 213 mat samplecond ( vec &cond, vec &lik, int n ); 214 //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. 193 215 void condition ( vec &cond ); 194 216 }; … … 197 219 \brief Gamma random walk 198 220 199 Mean value, $\mu$, of this density is given by \c rvc .221 Mean value, \f$\mu\f$, of this density is given by \c rvc . 200 222 Standard deviation of the random walk is proportional to one $k$-th the mean. 201 This is achieved by setting $\alpha=k$ and $\beta=k/\mu$.202 203 The standard deviation of the walk is then: $\mu/\sqrt(k)$.223 This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. 224 225 The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. 204 226 */ 205 227 class mgamma : public mEF { 228 //! Internal epdf that arise by conditioning on \c rvc 206 229 egamma epdf; 230 //! Constant $k$ 207 231 double k; 232 //! cache of epdf.beta 208 233 vec* _beta; 209 234 … … 211 236 //! Constructor 212 237 mgamma ( const RV &rv,const RV &rvc ); 238 //! Set value of \c k 213 239 void set_parameters ( double k ); 214 240 //!Generate one sample of the posterior … … 230 256 //! Number of particles 231 257 int n; 258 //! Sample weights $w$ 232 259 vec w; 260 //! Samples \f$x^{(i)}, i=1..n\f$ 233 261 Array<vec> samples; 234 262 public: 263 //! Default constructor 235 264 eEmp ( const RV &rv0 ,int n0) :epdf ( rv0 ),n(n0),w(n),samples(n) {}; 265 //! Set sample 236 266 void set_parameters ( const vec &w0, epdf* pdf0 ); 237 267 //! Potentially dangerous, use with care. 238 268 vec& _w() {return w;}; 269 //! access function 239 270 Array<vec>& _samples() {return samples;}; 240 271 //! Function performs resampling, i.e. removal of low-weight samples and duplication of high-weight samples such that the new samples represent the same density. 241 272 ivec resample ( RESAMPLING_METHOD method = SYSTEMATIC ); 273 //! inherited operation : NOT implemneted 242 274 vec sample() const {it_error ( "Not implemented" );return 0;} 275 //! inherited operation : NOT implemneted 243 276 double evalpdflog(const vec &val) const {it_error ( "Not implemented" );return 0.0;} 244 277 vec mean()const {vec pom=zeros(rv.count()); -
bdm/stat/libFN.cpp
r22 r33 3 3 4 4 using std::endl; 5 6 linfn linfn::evalsome ( ivec &rvind )7 {8 return *this;9 }10 5 11 6 bilinfn::bilinfn ( const RV &rvx0, const RV &rvu0, const mat &A0, const mat &B0 ) : diffbifn ( rvx0,rvu0 ) -
bdm/stat/libFN.h
r22 r33 10 10 // 11 11 // 12 #ifndef FN_H 13 #define FN_H 14 12 15 #include <itpp/itbase.h> 13 16 #include "libBM.h" … … 18 21 class constfn : public fnc 19 22 { 20 RV rv;23 //! value of the function 21 24 vec val; 22 25 23 24 26 public: 25 vec eval() {return val;}; 26 vec eval ( vec &cond ) {return val;}; 27 //vec eval() {return val;}; 28 //! inherited 29 vec eval ( const vec &cond ) {return val;}; 27 30 //!Default constructor 28 constfn ( const vec &val0 ) : rv(),val ( val0 ) {};31 constfn ( const vec &val0 ) :val ( val0 ) {}; 29 32 }; 30 33 … … 32 35 class linfn: public fnc 33 36 { 37 //! Identification of $x$ 34 38 RV rv; 35 ivec indexlist; // needed by evalsome39 //! Matrix A 36 40 mat A; 41 //! Matrix B 37 42 vec B; 38 43 public : 39 vec eval ( vec &cond ) {it_assert_debug ( cond.length() ==rv.count(), "linfn::eval Wrong cond." );return A*cond+B;};44 vec eval (const vec &cond ) {it_assert_debug ( cond.length() ==rv.count(), "linfn::eval Wrong cond." );return A*cond+B;}; 40 45 41 linfn evalsome ( ivec &rvind ); 42 linfn ( const RV &rv0 ) :rv ( rv0 ),A ( eye ( rv0.count() ) ),B ( zeros ( rv0.count() ) ) { indexlist=rv.indexlist();}; 43 linfn ( const RV &rv0, const mat &A0 ) : rv ( rv0 ), A ( A0 ), B ( zeros ( rv0.count() ) ) { indexlist=rv.indexlist();}; 44 linfn ( const RV &rv0, const mat &A0, const vec &B0 ) :rv ( rv0 ), A ( A0 ), B ( B0 ) { indexlist=rv.indexlist();}; 46 // linfn evalsome ( ivec &rvind ); 47 //!default constructor 48 linfn ( const RV &rv0 ) :rv ( rv0 ),A ( eye ( rv0.count() ) ),B ( zeros ( rv0.count() ) ) { }; 49 //! Set values of \c A and \c B 50 void set_parameters ( const mat &A0 , const vec &B0 ) {A=A0; B=B0;}; 45 51 }; 46 52 … … 58 64 { 59 65 protected: 60 RV rvx,rvu; 66 //! Indentifier of the first rv. 67 RV rvx; 68 //! Indentifier of the second rv. 69 RV rvu; 70 //! cache for rvx.count() 61 71 int dimx; 72 //! cache for rvu.count() 62 73 int dimu; 63 74 public: … … 71 82 //! Evaluates $f(x0,u0)$ 72 83 virtual vec eval ( const vec &x0, const vec &u0 ) {return zeros ( dimy );}; 73 //! Evaluates \f$A=\frac{d}{dx}f(x,u)|_{x0,u0}\f$ and writes result into \c A . @param full denotes that even unchanged entries are to be rewritten. When, false only the changed elements are computed. 84 //! Evaluates \f$A=\frac{d}{dx}f(x,u)|_{x0,u0}\f$ and writes result into \c A . @param full denotes that even unchanged entries are to be rewritten. When, false only the changed elements are computed. @param x0 numeric value of $x$, @param u0 numeric value of $u$ @param A a place where the result will be stored. 74 85 virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full=true ) {}; 75 //! Evaluates \f$A=\frac{d}{du}f(x,u)|_{x0,u0}\f$ and writes result into \c A . @param full denotes that even unchanged entries are to be rewritten. When, false only the changed elements are computed. 86 //! Evaluates \f$A=\frac{d}{du}f(x,u)|_{x0,u0}\f$ and writes result into \c A . @param full denotes that even unchanged entries are to be rewritten. When, false only the changed elements are computed. @param x0 numeric value of $x$, @param u0 numeric value of $u$ @param A a place where the result will be stored. 76 87 virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {}; 77 88 //!Default constructor (dimy is not set!) … … 94 105 //! Default constructor 95 106 bilinfn ( const RV &rvx0, const RV &rvu0 ) : diffbifn ( rvx0,rvu0 ) ,A ( eye ( dimx ) ),B ( zeros ( dimx,dimu ) ) {}; 96 // 107 //! Alternative constructor 97 108 bilinfn ( const RV &rvx0, const RV &rvu0, const mat &A0, const mat &B0 ); 98 // 109 //! 99 110 void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) 100 111 { … … 102 113 if ( full ) F=A; //else : nothing has changed no need to regenerate 103 114 } 104 // 115 //! 105 116 void dfdu_cond ( const vec &x0, const vec &u0, mat &F, bool full=true ) 106 117 { … … 109 120 } 110 121 }; 122 123 #endif // FN_H