[Developers] MCMC not working in ADMB10

dave fournier davef at otter-rsch.com
Fri Nov 19 09:07:51 PST 2010


The old better_rand function saves its state in static variables.
this is the old C approach. the problem is that you can't have two different
random number generators using this code. the general approach is to take
all the static variables and put them in a class. then different 
instances of the
class are different random generators with distinct states.

For people adding code to ADMB there is the follwong rule of thumb. Whenever
you use new in a constructor you need to write a destructor with delete.

If you had run the example with valgrind it would have picked this up 
immediately.


==10191== 1,137,337,344 bytes in 227,832 blocks are definitely lost in 
loss record 16 of 16
==10191==    at 0x4C27939: operator new[](unsigned long) 
(vg_replace_malloc.c:305)
==10191==    by 0x5464B6: 
random_number_generator::random_number_generator(int) (rngen.cpp:70)
==10191==    by 0x463D1A: better_rand(long&) (bet_rand.cpp:147)
==10191==    by 0x4772C7: bounded_multivariate_normal(int, dvector 
const&, dvector const&, dmatrix&, long&, double const&) (monte.cpp:81)
==10191==    by 0x45D655: function_minimizer::mcmc_routine(int, int, 
double, int) (xxmcmc.cpp:703)


destructor should be something like

random_number_generator::~random_number_generator()
{
   delete [] mt;
   mt=0;  // not  necessary but habit.
}




More information about the Developers mailing list