[2fc431]: src / modules / bugs / distributions / DBetaBin.cc  Maximize  Restore  History

Download this file

127 lines (101 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <config.h>
#include "DBetaBin.h"
#include <algorithm>
#include <JRmath.h>
using std::vector;
using std::max;
using std::min;
using std::string;
#define A(par) (*par[0])
#define B(par) (*par[1])
#define SIZE(par) (*par[2])
static double dbb(double x, double a, double b, double n)
{
//Probability density function
return choose(a+x-1, x) * choose(b+n-x-1, n-x) / choose(a+b+n-1, n);
}
static double ldbb(double x, double a, double b, double n)
{
//Log probability density function
return lchoose(a+x-1, x) + lchoose(b+n-x-1, n-x) - lchoose(a+b+n-1, n);
}
namespace jags {
namespace bugs {
DBetaBin::DBetaBin()
: RScalarDist("dbetabin", 3, DIST_SPECIAL, true)
{}
string DBetaBin::alias() const
{
return "dbetabinom";
}
bool
DBetaBin::checkParameterDiscrete (vector<bool> const &mask) const
{
return mask[2] == true;
}
bool DBetaBin::checkParameterValue (vector<double const *> const &par) const
{
return (SIZE(par) >= 0 && A(par) > 0.0 && B(par) > 0.0);
}
double DBetaBin::d(double x, PDFType type, vector<double const *> const &par,
bool give_log) const
{
if (give_log)
return ldbb(x, A(par), B(par), SIZE(par));
else
return dbb(x, A(par), B(par), SIZE(par));
}
static double pbb(double x, double a, double b, double n)
{
//Distribution function
if (x < 0) return 0;
if (x >= n) return 1;
double y = 0;
for (int i = 0; i <= x; ++i) {
y += dbb(i, a, b, n);
}
return y;
}
double DBetaBin::p(double x, vector<double const *> const &par,
bool lower, bool give_log) const
{
double y = pbb(x, A(par), B(par), SIZE(par));
if (!lower) y = 1 - y;
return give_log ? log(y) : y;
}
static double qbb(double p, double a, double b, double n)
{
//Quantile function
if (p < 0) return 0;
if (p >= 1) return n;
double pi = 0;
for (int i = 0; i < n; ++i) {
pi += dbb(i, a, b, n);
if (pi > p) return i;
}
return n;
}
double DBetaBin::q(double p, vector<double const *> const &par,
bool lower, bool log_p) const
{
if (log_p) p = exp(p);
if (!lower) p = 1 - p;
return qbb(p, A(par), B(par), SIZE(par));
}
double DBetaBin::r(vector<double const *> const &par, RNG *rng) const
{
return rbinom(SIZE(par), rbeta(A(par), B(par), rng), rng);
}
double DBetaBin::l(vector<double const *> const &par) const
{
return 0;
}
double DBetaBin::u(vector<double const *> const &par) const
{
return SIZE(par);
}
bool DBetaBin::isSupportFixed(vector<bool> const &fixmask) const
{
return fixmask[2]; //SIZE is fixed;
}
}}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks