Revision 306 trunk/contrib/qfclib/qfc_sim.cpp

qfc_sim.cpp (revision 306)
6 6
  * \ingroup QFC
7 7
  *
8 8
  *  Following user defined functions are more useful for doing simulation model in admb, 
9
  *  the easy way to use these functions is put this cpp file in the same directory with your tpl file,
10
  *  and under the GLOBALS_SECTION , add a line on top as   #include "qfc_sim.cpp", 
9
  *  the easy way to use these functions is with your tpl file
10
  *  under the GLOBALS_SECTION , add a line on top as   #include "qfclib.h", 
11 11
  *  there is a testfunction.tpl file within this folder show how to use these functions.
12 12
  *
13 13
  *  ================  brief list for usage  ============================\n
......
28 28
  */
29 29

  
30 30

  
31
  #define __QFC_SIM__
32
  #if !defined(__QFC_EST__)  //only include the following header file and constants once
33
    #include <math.h>
34
    #include <admodel.h>
35
    #include <df1b2fun.h>
36
    #include <adrndeff.h>
37
    // define constant variable
38
    const double EPS = 1.e-30;          //tiny number to avoid 0 in log
39
  #endif
31
  #include "qfclib.h"
32
 
40 33

  
41

  
42 34
  /** get how many rows for one specific variable(varName) in admb output file(filename)
43 35
   * \ingroup QFC
44 36
   * \param filename : search for admb output file name
......
172 164
   * \param source : baseline samples as input
173 165
   * \param nSample : random sample size for output
174 166
   * \param withReplace : if 0 for without replacement, nonzero  means with replacement
175
   * \param rnd :  admb build in random number generator
167
   * \param rng :  admb build in random number generator
176 168
   * \return return an index vector which are the index from the samples, 
177 169
   * if want to access the values, use return dvector version 
178 170
  */
179
  ivector sample(const dvector& source,int nSample,int withReplace,const random_number_generator& rnd)
171
  ivector sample(const dvector& source,int nSample,int withReplace,const random_number_generator& rng)
180 172
  {
181 173
    int totN=source.size();
182 174
    dvector lookup(source.indexmin(),source.indexmax());
......
184 176
    ivector out(1,nSample);
185 177
  
186 178
    if(withReplace==0){  //sampling without replacement, all unique site index
187
      out(1)=source.indexmin()+int(totN*randu(rnd)); 
179
      out(1)=source.indexmin()+int(totN*randu(rng)); 
188 180
      lookup(out(1))=0;//if selected, then mark lookup as 0
189 181
  
190 182
      for(int i=2;i<=nSample;i++){ 
191
        out(i)=source.indexmin()+int(totN*randu(rnd)); 
183
        out(i)=source.indexmin()+int(totN*randu(rng)); 
192 184
        while(lookup(out(i))==0){ //which means already being selected
193
          out(i)=source.indexmin()+int(totN*randu(rnd));
185
          out(i)=source.indexmin()+int(totN*randu(rng));
194 186
        }
195 187
        lookup(out(i))=0;//if selected, then mark lookup as 0
196 188
      }
197 189
    }else{  //with replacement,allow repeat sampling    
198 190
      for(int i=1;i<=nSample;i++) 
199
        out(i)=source.indexmin()+int(totN*randu(rnd)); 
191
        out(i)=source.indexmin()+int(totN*randu(rng)); 
200 192
    }
201 193

  
202 194
    return out;  //you can use source(out) to access the sample
......
378 370

  
379 371

  
380 372

  
381
  /** rounding values as specified digits for scalar, vector, matrix, easy for display in report_section,
382
   * if input is variable type such as dvar_, use value(r) as input instead
383
   * \ingroup QFC
384
   * \param r : input values either for sclar, vector or matrix
385
   * \return return the same type and size as input with specified number of decimals 
386
   */
387
  double round(double r) {
388
    return double((r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5));
389
  }
373
  
390 374

  
391
  /** rounding values as specified digits for scalar, vector, matrix, easy for display in report_section,
392
   * if input is variable type such as dvar_, use value(r) as input instead,
393
   * overloading function
394
   * \ingroup QFC
395
   * \param r : input values either for sclar, vector or matrix
396
   * \param places : the number of decimals 
397
   * \return return the same type and size as input with specified number of decimals 
398
   */
399
  double round(double r, unsigned int places){
400
    double off=pow(10,places);
401
    return round(r*off)/off;
402
  }
403

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff