-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyalgorithm.hpp
67 lines (55 loc) · 1.49 KB
/
myalgorithm.hpp
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
#ifndef MYALGORITHM_HPP
#define MYALGORITHM_HPP
#include<vector>
#include<random>
#include<numeric>
#include"myutility.hpp"
#include<cstdint>
namespace ytd{
//DBL is double or long double.
template<typename DBL>
class dirichlet_distribution{
private:
std::vector<uint32_t> alpha;
public:
dirichlet_distribution(uint32_t size)
:alpha(size)
{}
dirichlet_distribution(const std::vector<uint32_t>& args)
:alpha(args)
{}
template<typename T1>
void setParam(const T1& args)
{
std::copy(begin(args),end(args),begin(alpha));
}
template<typename T2>
std::vector<DBL> operator()(T2& gen) const
{
std::vector<DBL> beta(alpha.size());
ytd::double_for(alpha,beta,[&](const int& a,DBL& b){
std::gamma_distribution<DBL> dist(a,1.0);
b = dist(gen);
});
DBL beta_sum = accumulate(begin(beta),end(beta),0.0);
for(auto& b:beta){b = b/beta_sum;}
return beta;
}
template<typename T2>
void operator()(T2& gen,std::vector<DBL>& args) const
{
if(args.size() != alpha.size()){
std::cout << "Error In ytd::dirichlet_distribution::operator()" \
<< std::endl;
exit(1);
}
ytd::double_for(alpha,args,[&](const int& a,DBL& b){
std::gamma_distribution<DBL> dist(a,1.0);
b = dist(gen);
});
DBL argsSum = accumulate(begin(args),end(args),0.0);
for(auto& b:args){b = b/argsSum;}
}
};
}
#endif