Line data Source code
1 : /** 2 : Bojan Nikolic <bojan@bnikolic.co.uk> 3 : Initial version 2008 4 : 5 : This file is part of BNMin1 and is licensed under GNU General 6 : Public License version 2 7 : 8 : \file metro_propose.hxx 9 : Renamed to metro_propose.h 10 : 11 : Calculation of proposal points for Metropolis sampling 12 : */ 13 : #ifndef _BNMIN1_METRO_PROPOSAL_HXX__ 14 : #define _BNMIN1_METRO_PROPOSAL_HXX__ 15 : 16 : #include <vector> 17 : 18 : #include <random> 19 : 20 : namespace Minim { 21 : 22 : /** \brief A helper class which handles the random number parts of 23 : the Metropolis algorithm 24 : 25 : The two objectives of this class functions are to generate the 26 : proposal points [via the function displace()] and to generate 27 : uniform random numbers for accepting less likely points 28 : 29 : It encapsulates a random number generator 30 : */ 31 : class MetroPropose { 32 : 33 : protected: 34 : 35 : /// Standard distributions of the proposal points 36 : std::vector<double> sigmas; 37 : 38 : // Stuff for random numbers 39 : typedef std::mt19937 base_generator_type; 40 : base_generator_type generator; 41 : std::normal_distribution<double> norm_dist; 42 : std::uniform_real_distribution<double> uni_dist; 43 : 44 : 45 : public: 46 : 47 : // ---------- Construction / Destruction -------------- 48 : 49 : /** 50 : \param sigmas The standard distribution to be used for each of 51 : the parameters that is being varied in the model 52 : */ 53 : MetroPropose(const std::vector<double> & sigmas, 54 : unsigned seed=0); 55 : 56 : virtual ~MetroPropose(); 57 : 58 : // ---------- Public interface -------------------------- 59 : 60 : /// Normal distribution for creating proposal points 61 : double norm(); 62 : 63 : /// Uniform distribution for accepting according to probability 64 : double uni(); 65 : 66 : /** 67 : Generate a proposad point by displacing the point x 68 : */ 69 : virtual void displace(std::vector<double> &x); 70 : 71 : /** 72 : \returns number of parameters to propose for 73 : */ 74 : size_t nPars(void); 75 : 76 : /** 77 : Generate a uniform number between zero and one for acceptance 78 : probability 79 : */ 80 0 : double raccept(void) 81 : { 82 0 : return uni(); 83 : } 84 : 85 : /** \brief Scale the variance of the of the proposal distributions 86 : */ 87 : void scaleSigma(double c); 88 : 89 : }; 90 : 91 : /** \brief Proposes each parameter in turn 92 : 93 : Intead of displacing the entire vector at once, each parameter is 94 : displaced in turn. See MetropolisMCMC::Options::Sequence. 95 : */ 96 : class MetroProposeSeq : 97 : public MetroPropose 98 : { 99 : 100 : size_t count; 101 : 102 : const size_t n; 103 : 104 : public: 105 : 106 : // ---------- Construction / Destruction -------------- 107 : 108 : MetroProposeSeq(const std::vector<double> & sigmas, 109 : unsigned seed=0); 110 : 111 : // ---------- Public interface -------------------------- 112 : virtual void displace( std::vector<double> &x); 113 : 114 : }; 115 : 116 : } 117 : 118 : #endif