LCOV - code coverage report
Current view: top level - bnmin1/src - priors.h (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 8 12 66.7 %
Date: 2024-12-11 20:54:31 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /**
       2             :    Bojan Nikolic <bojan@bnikolic.co.uk>, <b.nikolic@mrao.cam.ac.uk>
       3             : 
       4             :    \file priors.hxx
       5             :    Renamed to priors.h 2023
       6             : 
       7             :    Mechanism to include prior information when sampling posterior
       8             :    distributions
       9             : */
      10             : #ifndef _BNMIN_PRIORS_HXX__
      11             : #define _BNMIN_PRIORS_HXX__
      12             : 
      13             : #include "minimmodel.h"
      14             : 
      15             : #include <list>
      16             : #include <memory>
      17             : 
      18             : namespace Minim {
      19             : 
      20             :   /** \brief Represents combined priors and likelihood in a single
      21             :       object
      22             :       
      23             :       
      24             :    */
      25             :   class PriorNLikelihood:
      26             :     public MLikelihood
      27             :   {
      28             :     /// Keeps the encapsulated likelihoood model
      29             :     std::unique_ptr<MLikelihood> _mod;
      30             :     
      31             :   public:
      32             : 
      33             :     /// --------------- Public data -----------------------
      34             :     
      35             :     /** \brief Value representing very low likelihood 
      36             :         
      37             :         I.e., this is the value used for the negative log-likelihood
      38             :         when priors have "hard" constraints
      39             :      */
      40             :     static const double lkl_h;
      41             : 
      42             :     // ---------- Construction / Destruction --------------
      43             : 
      44             :     /** Construct with reference to an existing model
      45             : 
      46             :        \note This class takes ownership of the supplied pointer
      47             :     */
      48             :     PriorNLikelihood(MLikelihood * mod);    
      49             :     
      50             :     virtual ~PriorNLikelihood();
      51             : 
      52             :     // ---------- Public interface  --------------------------
      53             :     
      54             :     /** \brief Likelihood (without prior multiplied in)
      55             :         
      56             :      */
      57    77889705 :     double llprob(void) const
      58             :     {
      59    77889705 :       return _mod->lLikely();
      60             :     }
      61             : 
      62             :     /** \brief Prior probability at current point
      63             :      */
      64           0 :     virtual double pprob(void) const
      65             :     {
      66           0 :       return 0;
      67             :     }
      68             : 
      69             :     // Inherited from MLikelihood
      70           0 :     double lLikely(void) const
      71             :     {
      72           0 :       return llprob()+pprob();
      73             :     }
      74             : 
      75             :     // ------------   Inherited from Model   ----------------
      76             :     void AddParams (std::vector<Minim::DParamCtr> &pars);
      77             : 
      78             : 
      79             :   };
      80             :   
      81             :   /** \brief Independent priors on each parameter
      82             :       
      83             :       \bug Interface presented by this class has not yet been fully
      84             :       finalised.
      85             : 
      86             :    */
      87             :   class IndependentPriors:
      88             :     public PriorNLikelihood
      89             :   {
      90             : 
      91             :     std::vector< Minim::DParamCtr > _mpars;
      92             :     
      93             :     struct fprior_t
      94             :     {
      95             :       const double * p;
      96             :       double pmin;
      97             :       double pmax;
      98             :     };
      99             :     
     100             :   public:
     101             :     typedef std::list<fprior_t> priorlist_t;
     102             : 
     103             :   private:
     104             :     priorlist_t priorlist;
     105             : 
     106             :   public:
     107             : 
     108             :     // ---------- Construction / Destruction --------------
     109             : 
     110             :     /** Construct with reference to an existing model
     111             : 
     112             :        \note This class takes ownership of the supplied pointer
     113             :     */
     114             :     IndependentPriors(MLikelihood * mod);
     115             : 
     116             :     virtual ~IndependentPriors(void);
     117             : 
     118             :     // ---------- Public interface  --------------------------
     119             : 
     120             :     /// Add a flat prior between low and high, on parameter with name
     121             :     /// pname
     122             :     void AddPrior( const std::string & pname,
     123             :                    double low,
     124             :                    double high);
     125             : 
     126             :     /// Support iteration overpriors
     127    77889705 :     priorlist_t::const_iterator pbegin(void) const
     128             :     {
     129    77889705 :       return priorlist.begin();
     130             :     }
     131             : 
     132             :     /// Support iteration overpriors
     133   311558820 :     priorlist_t::const_iterator pend(void) const
     134             :     {
     135   311558820 :       return priorlist.end();
     136             :     }
     137             : 
     138             :     /// Number of priors
     139          81 :     size_t npriors(void) const
     140             :     {
     141          81 :       return priorlist.size();
     142             :     }
     143             : 
     144             :   };
     145             : 
     146             :   class IndependentFlatPriors:
     147             :     public IndependentPriors
     148             : 
     149             :   {
     150             :   public:
     151             : 
     152             :     // ---------- Construction / Destruction --------------
     153             : 
     154             :     /** 
     155             :         Construct with reference to an existing model
     156             : 
     157             :         \note This class takes ownership of the supplied pointer
     158             :     */
     159             :     IndependentFlatPriors(MLikelihood * mod);
     160             : 
     161             :     virtual ~IndependentFlatPriors(void);
     162             : 
     163             :     // ---------- Public interface  --------------------------
     164             : 
     165             :     // Inherited
     166             :     double pprob(void) const;
     167             :   };
     168             : 
     169             :   class LogFlatPriors:
     170             :     public IndependentPriors
     171             : 
     172             :   {
     173             :   public:
     174             : 
     175             :     // ---------- Construction / Destruction --------------
     176             : 
     177             :     /** 
     178             :         Construct with reference to an existing model
     179             :     */
     180             :     LogFlatPriors(MLikelihood * mod);
     181             : 
     182             :     virtual ~LogFlatPriors(void);
     183             : 
     184             :     // ---------- Public interface  --------------------------
     185             :     // Inherited
     186             :     double pprob(void) const;
     187             :   };
     188             :   
     189             : 
     190             : 
     191             : }
     192             : #endif

Generated by: LCOV version 1.16