-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiut.h
96 lines (80 loc) · 2.28 KB
/
diut.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
#ifndef _DIRICHLET_UNIFORM_TRONCATED_PDF_
#define _DIRICHLET_UNIFORM_TRONCATED_PDF_
//
#include "sampler.h"
namespace Sampler{
//!diut distribution
template <typename T = double>
class DiUT:public Distribution<T>
{
public:
DiUT():_npars(2){}
DiUT(const std::vector<std::vector<T> > &pars):_npars(2) {this->set_parameters(pars);}
DiUT(const DiUT<T> &rhs):_npars(4){if(this == &rhs)return;_min = rhs.min();_max=rhs.max();this->_r=rhs.random_gsl();}
~DiUT(){}
void set_parameters(const std::vector<std::vector<T> > ¶meters);
void sample(unsigned int nsample,std::vector<std::vector<T> > &sample) const;
unsigned int npars() const {return _min.size();}
const std::vector<T> min() const {return _min;}
const std::vector<T> max() const {return _max;}
void print(std::ostream &out = std::cout);
friend std::ostream & operator << (std::ostream &out, const DiUT<T> &dirichlet)
{
dirichlet.print(out);
return out;
}
private:
std::vector<T> _min;
std::vector<T> _max;
const unsigned int _npars;
bool accept(const double *run) const;
};
template <typename T>
void DiUT<T>::print(std::ostream &out)
{
out << "DiUT{";
for(unsigned int i = 0; i < this->npars() - 1; i++)
{
out << "[" << _min[i] << "," << _max[i] << "],";
}
out << "[" << _min.back() << "," << _max.back() << "]}";
}
template <typename T>
bool DiUT<T>::accept(const double *run) const
{
for(unsigned int i = 0; i < this->npars(); i++)
{
if(_min[i] > run[i] || _max[i] < run[i])return false;
}
return true;
}
template <typename T>
void DiUT<T>::set_parameters(const std::vector<std::vector<T> > ¶meters)
{
_min = parameters[0];
_max = parameters[1];
}
template <typename T>
void DiUT<T>::sample(unsigned int nsample, std::vector<std::vector<T> > &sample) const
{
sample.clear();
sample.resize(this->npars());
//diut -> troncated diun
double alpha[_min.size()];
double diri[_min.size()];
for(unsigned int i = 0; i < this->npars(); i++)alpha[i] = 1.L;
unsigned int ns(0);
while(ns != nsample)
{
gsl_ran_dirichlet(this->_r,this->npars(),alpha,diri);
if(!accept(diri))continue;
ns++;
for(unsigned int j = 0; j < this->npars(); j++)
{
sample[j].push_back(diri[j]);
}
}
return;
}
}
#endif