-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdior.h
86 lines (72 loc) · 2.06 KB
/
dior.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
#ifndef _DIRICHLET_ORDONNE_PDF_
#define _DIRICHLET_ORDONNE_PDF_
//
#include "sampler.h"
namespace Sampler{
//!dior distribution
template <typename T = double>
class DiOr:public Distribution<T>
{
public:
DiOr():_npars(2){}
DiOr(const std::vector<T> &pars):_npars(2) {this->set_parameters(pars);}
DiOr(const DiOr<T> &rhs):_npars(4){if(this == &rhs)return;_par = rhs.index_pars();this->_r=rhs.random_gsl();}
~DiOr(){}
void set_parameters(const std::vector<T> ¶meters);
void sample(unsigned int nsample,std::vector<std::vector<T> > &sample) const;
unsigned int npars() const {return _par.size();}
const std::vector<T> index_pars() const {return _par;}
void print(std::ostream &out = std::cout);
friend std::ostream & operator << (std::ostream &out, const DiOr<T> &dirichlet)
{
dirichlet.print(out);
return out;
}
private:
std::vector<T> _par;
const unsigned int _npars;
unsigned int offset;
};
template <typename T>
void DiOr<T>::print(std::ostream &out)
{
out << "DiOr{";
for(unsigned int i = 0; i < this->npars() - 1; i++)
{
out << _par[i] << ",";
}
out << _par.back() << "}";
}
template <typename T>
void DiOr<T>::set_parameters(const std::vector<T> ¶meters)
{
_par = parameters;
offset = _par.size();
for(unsigned int i = 0; i < this->npars(); i++)
{
if(_par[i] < offset)offset = _par[i];
}
}
template <typename T>
void DiOr<T>::sample(unsigned int nsample, std::vector<std::vector<T> > &sample) const
{
sample.clear();
sample.resize(this->npars());
//dior -> sorted diun
double alpha[_par.size()];
double diri[_par.size()];
for(unsigned int i = 0; i < this->npars(); i++)alpha[i] = 1.L;
for(unsigned int ns = 0; ns < nsample; ns++)
{
gsl_ran_dirichlet(this->_r,this->npars(),alpha,diri);
gsl_sort(diri,1,_par.size());
std::vector<T> diriv;
for(unsigned int j = 0; j < this->npars(); j++)
{
sample[j].push_back(diri[_par.size() - 1 - (unsigned int)_par[j] + offset]); //reverse order
}
}
return;
}
}
#endif