34 results.assign(alphabetSize,0);
35 for (
size_t i = 0; i < (size_t) alphabetSize; i++){
36 results[i]= (size_t)value + i * (
size_t)
POWER[order-1][alphabetSize-1];
43 double interpolate(std::pair<double,double>& a, std::pair<double,double>& b,
double& cx){
47 return a.second+(b.second-a.second)*((cx-a.first)/(b.first-a.first));
51 double extrapolate(std::pair<double,double>& a, std::pair<double,double>& b,
double& cx){
52 return a.second+((cx-a.first)/(b.first-a.first))*(b.second-a.second);
56 float entropy (std::vector<float> &
set){
58 for(
size_t i=0;i<
set.size();i++){
59 entropy+=
set[i]*(log(
set[i])/log(2));
65 double entropy (std::vector<double> &
set){
67 for(
size_t i=0;i<
set.size();i++){
68 entropy+=
set[i]*(log(
set[i])/log(2));
75 float rel_entropy (std::vector<float> &
set, std::vector<float> &set2){
77 if (
set.size()!=set2.size()){
78 std::cerr <<
"Distributions aren't the same size";
81 for(
size_t i=0;i<
set.size();i++){
82 rel_entropy+=0.5*(
set[i]* (log (
set[i]/set2[i]) /log(2) )+set2[i]*(log(set2[i]/
set[i])/log(2)));
89 double rel_entropy (std::vector<double> &
set, std::vector<double> &set2){
91 if (
set.size()!=set2.size()){
92 std::cerr <<
"Distributions aren't the same size";
95 for(
size_t i=0;i<
set.size();i++){
96 rel_entropy+=0.5*(
set[i]* (log (
set[i]/set2[i]) /log(2) )+set2[i]*(log(set2[i]/
set[i])/log(2)));
107 double _integrate (
double (*funct)(
double, std::vector<double>&),
double upper,
double lower,std::vector<double>& param){
108 double mid=(lower+upper)/2.0;
109 double h3=fabs(upper-lower)/6.0;
110 return h3*(funct(lower,param)+4*funct(mid,param)+funct(upper,param));
113 double integrate (
double (*funct)(
double,std::vector<double>&),
double lower,
double upper ,std::vector<double>& param,
double max_error=0.000001,
double sum=0){
114 double mid=(upper+lower)/2.0;
115 double left=
_integrate(funct,lower, mid, param);
116 double right=
_integrate(funct,mid,upper, param);
119 if (fabs(left+right-sum)<=15*max_error){
120 return left+right +(left+right-sum)/15;
126 return integrate(funct,lower,mid,param,max_error/2,left) +
integrate(funct,mid,upper,param,max_error/2,right);
132 double simpson (
double (*funct)(
double,
double,
double),
double alpha,
double beta,
double lower,
double upper){
133 double mid=(lower+upper)/2.0;
134 double h3=fabs(upper-lower)/6.0;
135 return h3*(funct(lower,alpha,beta)+4*funct(mid,alpha,beta)+funct(upper,alpha,beta));
138 double adapt_simpson (
double (*funct)(
double,
double,
double),
double alpha,
double beta,
double lower,
double upper,
double max_error,
double sum){
139 double mid=(upper+lower)/2.0;
140 double left=
simpson(funct,alpha,beta,lower, mid);
142 double right=
simpson(funct,alpha,beta,mid,upper);
143 if (fabs(left+right-sum)<=15*max_error){
144 return left+right +(left+right-sum)/15;
146 return adapt_simpson(funct,alpha,beta,lower,mid,max_error/2,left) +
adapt_simpson(funct,alpha,beta,mid,upper,max_error/2,right);
149 double summation (
double (*funct)(
int,std::vector<double>),
int lower,
int upper, std::vector<double> param){
151 for(
int i=lower;i<=upper;i++){
167 double constant = pow(x,a) * exp(-1 * x);
169 for (
int n=0;n<60;n++){
170 double num = pow(x,(
double)n);
172 for(
int m=1;m<=n;m++){
178 return constant * sum;
192 double beta(
double a,
double b){
193 double value=(tgamma(a)*tgamma(b))/tgamma(a+b);
198 double ibeta(
double x,
double a,
double b){
199 return betaPDF(x,a,b) * exp(log(tgamma(a)) + log(tgamma(b)) - log(tgamma(a+b)));
208 float constant = 1/
beta(a,b);
209 constant*=pow(x,a-1) * pow(1-x,b-1);
231 double ribeta(
double x,
double a,
double b){
237 double f=sqrt((2*x+(1/3))*M_PI)*pow(x,x)*exp(-1*x);
244 for(
int i=r+1;i<=n;i++) {c+=log(i);}
245 for(
int j=1;j<=(n-r);j++) {c-=log(j);}
251 for(
int i=r+1;i<=n;i++) {c+=log(i);}
252 for(
int j=1;j<=(n-r);j++) {c-=log(j);}