Menu

Adding Log F Distribution to runjags module

runjags
2018-01-11
2018-01-13
  • Achmad Syahrul Choir

    Dear Forum,
    I added a logF distribution into runjags module, but i failed to compile it.

    This is my code in DLogf.h

    #ifndef DLOGF_H_
    #define DLOGF_H_
    // Checks the JAGS version and sets necessary macros:
    #include "../jagsversions.h"
    
    #ifndef INCLUDERSCALARDIST
    #include <distribution/RScalarDist.h>
    namespace jags {
    #else
    #include "jags/RScalarDist.h"
    #endif  /* INCLUDERSCALARDIST */
    
    namespace runjags {
    
    /**
     * <pre>
     *
     * </pre>
     * @short Normal distribution
     */
    class DLogf : public RScalarDist {
     public:
       DLogf();
    
      double d(double z, PDFType type,
           std::vector<double const *> const &parameters, 
           bool give_log) const;
      double p(double q, std::vector<double const *> const &parameters, bool lower,
           bool give_log) const;
      double q(double p, std::vector<double const *> const &parameters, bool lower,
           bool log_p) const;
      double r(std::vector<double const *> const &parameters, RNG *rng) const;
      /**
       * Checks that tau > 0
       */
      bool checkParameterValue(std::vector<double const *> const &parameters) const;
      /**
       *    * that is built into the JAGS library, overloading the generic
       * functionality of RScalarDist.
       */
    
    };
    }// namespace runjags
    #ifndef INCLUDERSCALARDIST
    }  // namespace jags
    #endif  /* INCLUDERSCALARDIST */
    #endif /* DMNLOGF_H_ */
    
    
    

    Code in DLogf.cc

    #include "DLogf.h"
    #include <util/nainf.h>
    #include <rng/RNG.h>
    
    #include <cmath>
    #include <cfloat>
    #include <JRmath.h>
    using std::vector;
    using std::exp;
    using std::log;
    using std::pow;
    
    #define ALPHA1(par) (*par[0])
    #define ALPHA2(par) (*par[1])
    
    #ifndef INCLUDERSCALARDIST
    namespace jags {
    #endif  /* INCLUDERSCALARDIST */
    
    namespace runjags {
    
    DLogf::DLogf()
      : RScalarDist("dmnlogf",2, DIST_UNBOUNDED)
    {}
    
    bool DLogf::checkParameterValue (vector<double const *> const &par) const
    {
      return ( ALPHA1(par) > 0 && ALPHA2(par) > 0 );
    }
    
    //Computing pdf
    
    double
    DLogf::d(double z, PDFType type,
                vector<double const *> const &par, bool give_log) const
    {
    
      double w=exp(z);
      double log_pdf=dF(w,2*ALPHA1(par),2*ALPHA2(par),1)+z;
      return give_log?log_pdf: exp(log_pdf);
    }
    
    //Computing CDF
    double
    DLogf::p(double z, vector<double const *> const &par, bool lower, bool give_log)
    const
    {
    
      double w = exp(z);  
      return pF(w,2*ALPHA1(par),2*ALPHA2(par),lower,give_log);
    
    }
    
    //Computing Invers CDF
    
    double 
    DLogf::q(double p, vector<double const *> const &par, bool lower, bool log_p)
    const
    {
      if ( (log_p  && p > 0) || (!log_p && (p < 0 || p > 1)) )          
        return JAGS_NAN;
    
      double  w =qF(p,2*ALPHA1(par),2*ALPHA2(par),lower,log_p);
      return log(w);
    
    }
    
    double 
    DLogf::r(vector<double const *> const &par, RNG *rng) const
    {
      return q(rng->uniform(), par,true, false);  
    }
    
    }
    #ifndef INCLUDERSCALARDIST
    }  // namespace jags
    #endif  /* INCLUDERSCALARDIST */
    

    I had a errormassage while i compiled it

    distributions/DLogf.o:DLogf.cc:(.text+0x67): undefined reference to jags_dF' distributions/DLogf.o:DLogf.cc:(.text+0x14a): undefined reference tojags_qF'
    distributions/DLogf.o:DLogf.cc:(.text+0x1dc): undefined reference to jags_qF' distributions/DLogf.o:DLogf.cc:(.text+0xd7): undefined reference tojags_pF'

    How to solve this problem? Thank You.

    Best Regard

    Achmad Syahrul

     

    Last edit: Achmad Syahrul Choir 2018-01-13
  • Achmad Syahrul Choir

    I have successfully fixed the code so it can be compiled. I convert JRmath to Rmath, dF to df, pF to pf and qF to qf. However, when I use this distribution, runjags (JAGS) do not report the DIC value. Why JAGS can't calculate DIC for this distribution? Thank you.

     
    • Martyn Plummer

      Martyn Plummer - 2018-01-12

      That is not the way to solve your first problem. You need to add the library jrmath, which provides the missing symbols jags_qF, jags_pF, jags_dF when you compile the shared object runjags.so by modifying the file src/Makevars.in. The line

       PKG_LIBS=-L@JAGS_LIB@ -ljags @JAGS_RPATH@
      

      should be changed to

       PKG_LIBS=-L@JAGS_LIB@ -ljags -ljrmath @JAGS_RPATH@
      

      and you will need to modify Makevars.win if you want to compile your modified package on Windows.

       
      • Achmad Syahrul Choir

        My problem was solved by your suggestion. Thank you.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.