-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirg.h
129 lines (112 loc) · 3.26 KB
/
dirg.h
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
127
128
129
#ifndef _DIRICHLET_GENERALISE_PDF_
#define _DIRICHLET_GENERALISE_PDF_
//
#include "sampler.h"
namespace Sampler{
//!dirg distribution
template <typename T = double>
class DirG:public Distribution<T>
{
public:
DirG():_mean(0.),_sig(0.),_npars(2){}
DirG(const std::vector<std::vector<T> > &pars):_npars(2) {this->set_parameters(pars);}
DirG(const DirG<T> &rhs):_npars(4){if(this == &rhs)return;_mean = rhs.mean();_sig=rhs.sigma();this->_r=rhs.random_gsl();}
~DirG(){}
void set_parameters(const std::vector<std::vector<T> > ¶meters);
void sample(unsigned int nsample,std::vector<std::vector<T> > &sample) const;
void sample_indirect(unsigned int nsample, std::vector<std::vector<T> > &U01, std::vector<std::vector<T> > &sample) const;
unsigned int npars() const {return _mean.size();}
const std::vector<T> mean() const {return _mean;}
const std::vector<T> sigma() const {return _sig;}
void print(std::ostream &out = std::cout) const;
friend std::ostream & operator << (std::ostream &out, const DirG<T> &dirichlet)
{
dirichlet.print(out);
return out;
}
private:
std::vector<T> _mean;
std::vector<T> _sig;
const unsigned int _npars;
};
template <typename T>
void DirG<T>::print(std::ostream &out) const
{
out << "DirG{";
for(unsigned int i = 0; i < this->npars() - 1; i++)
{
out << _mean[i] << ",";
}
out << _mean.back() << " ; ";
for(unsigned int i = 0; i < this->npars() - 1; i++)
{
out << _sig[i] << ",";
}
out << _sig.back() << "}";
}
template <typename T>
void DirG<T>::set_parameters(const std::vector<std::vector<T> > ¶meters)
{
_mean = parameters[0];
_sig = parameters[1];
}
template <typename T>
void DirG<T>::sample(unsigned int nsample, std::vector<std::vector<T> > &sample) const
{
sample.clear();
sample.resize(this->npars());
//dirg
double dirgA[_mean.size()];
double dirgB[_mean.size()];
for(unsigned int i = 0; i < _mean.size(); i++)
{
dirgA[i] = std::pow(_mean[i]/_sig[i],2);
dirgB[i] = std::pow(_sig[i],2)/_mean[i];
}
for(unsigned int i = 0; i < nsample; i++)
{
double x[_mean.size()],sum(0.L);
std::vector<T> diriv;
for(unsigned int ibr = 0; ibr < this->npars(); ibr++)
{
x[ibr] = gsl_ran_gamma_knuth(this->_r,dirgA[ibr],dirgB[ibr]);
sum += x[ibr];
}
for(unsigned int ibr = 0; ibr < this->npars(); ibr++)
{
sample[ibr].push_back(x[ibr]/sum);
}
}
return;
}
template <typename T>
void DirG<T>::sample_indirect(unsigned int nsample, std::vector<std::vector<T> > &U01, std::vector<std::vector<T> > &sample) const
{
sample.clear();
sample.resize(this->npars());
//dirg
double dirgA[_mean.size()];
double dirgB[_mean.size()];
for(unsigned int i = 0; i < _mean.size(); i++)
{
dirgA[i] = std::pow(_mean[i]/_sig[i],2);
dirgB[i] = std::pow(_sig[i],2)/_mean[i];
}
for(unsigned int i = 0; i < nsample; i++)
{
double x[_mean.size()],sum(0.L);
std::vector<T> diriv;
for(unsigned int ibr = 0; ibr < this->npars(); ibr++)
{
x[ibr] = gsl_cdf_gamma_Pinv(U01[i][ibr],dirgA[ibr],dirgB[ibr]);
sum += x[ibr];
}
for(unsigned int ibr = 0; ibr < _mean.size(); ibr++)
{
sample[ibr].push_back(x[ibr]/sum);
}
}
return;
}
}
#endif