Probability distributions are provided by the C++ standard library, but multidimensional distributions are not provided. For the time being, some experimental probability distributions are being included in O2scl .
#include <o2scl/mcmc.h>
#include <o2scl/vec_stats.h>
#include <o2scl/test_mgr.h>
#include <o2scl/hdf_io.h>
#include <o2scl/cubature.h>
using namespace std;
typedef std::function<int(size_t,const ubvector &,double &,
std::array<double,2> &)> point_funct;
typedef std::function<int(const ubvector &,double,std::vector<double> &,
std::array<double,2> &)> fill_funct;
class exc {
public:
int point(
size_t nv,
const ubvector &pars,
double &log_weight,
std::array<double,2> &dat) {
log_weight=-((pars[0]-0.2)*(pars[0]-0.2)+
(pars[1]-0.5)*(pars[1]-0.5));
dat[0]=pars[0]*pars[0];
dat[1]=pars[0]*pars[0]*pars[1]*pars[1];
return 0;
}
int fill_line(const ubvector &pars, double log_weight,
std::vector<double> &line, std::array<double,2> &dat) {
line.push_back(dat[0]);
line.push_back(dat[1]);
}
return 0;
}
exc(int i) {
}
private:
exc();
};
int f_cub(unsigned ndim, size_t npt, const double *x, unsigned fdim,
double *fval) {
for (size_t i=0;i<npt;i++) {
const double *x2=x+i*ndim;
double *f2=fval+i*fdim;
f2[0]=exp(-((x2[0]-0.2)*(x2[0]-0.2)+
(x2[1]-0.5)*(x2[1]-0.5)));
f2[1]=exp(-((x2[0]-0.2)*(x2[0]-0.2)+
(x2[1]-0.5)*(x2[1]-0.5)))*x2[0]*x2[0];
f2[2]=exp(-((x2[0]-0.2)*(x2[0]-0.2)+
(x2[1]-0.5)*(x2[1]-0.5)))*x2[0]*x2[0]*x2[1]*x2[1];
}
return 0;
}
int main(int argc, char *argv[]) {
cout.setf(ios::scientific);
exc e(2);
ubvector low(2), high(2), init(2);
low[0]=-2.0;
low[1]=-2.0;
high[0]=2.0;
high[1]=2.0;
init[0]=0.2;
init[1]=0.5;
std::vector<double> dlow(2), dhigh(2);
dlow[0]=-2.0;
dlow[1]=-2.0;
dhigh[0]=2.0;
dhigh[1]=2.0;
std::vector<double> dres(3), derr(3);
typedef std::function<
int(unsigned,size_t,const double *,unsigned,double *)> cub_funct_arr;
cub_funct_arr cfa=f_cub;
int ret=hc.
integ(3,cfa,2,dlow,dhigh,10000,0.0,1.0e-4,enh,dres,derr);
tm.
test_gen(ret==0,
"cubature success.");
double exact_res[2];
exact_res[0]=dres[1]/dres[0];
exact_res[1]=dres[2]/dres[0];
point_funct pf=std::bind
(std::mem_fn<int(size_t,const ubvector &,double &,
std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,
std::placeholders::_4);
fill_funct ff=std::bind
(std::mem_fn<int(const ubvector &,double,std::vector<double> &,
std::array<double,2> &)>(&exc::fill_line),&e,
std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,
std::placeholders::_4);
vector<point_funct> vpf;
vpf.push_back(pf);
vector<fill_funct> vff;
vff.push_back(ff);
vector<string> pnames={"x0","x1","x0sq","x0sq_x1sq"};
vector<string> punits={"MeV","MeV","MeV^2","MeV^4"};
shared_ptr<table_units<> > t=mct.
get_table();
cout << "MCMC with random walk:\n" << endl;
mct.
mcmc(2,init,low,high,vpf[0],vff[0]);
cout << "n_accept, n_reject, table lines: "
<< t->get_nlines() << endl;
cout << " i mult log_wgt x0 x1 "
<< "x0sq x0sq_x1sq" << endl;
cout.precision(4);
for(size_t i=0;i<t->get_nlines();i+=t->get_nlines()/10) {
cout.width(5);
cout << i << " " << t->get("mult",i) << " ";
cout.setf(ios::showpos);
cout << t->get("log_wgt",i) << " " << t->get("x0",i) << " ";
cout << t->get("x1",i) << " " << t->get("x0sq",i) << " "
<< t->get("x0sq_x1sq",i) << endl;
cout.unsetf(ios::showpos);
}
cout.precision(6);
cout << endl;
size_t n=t->get_nlines();
t->get_column("mult"));
t->get_column("mult"));
double t_avgerr=t_stddev/sqrt((double)n);
cout.setf(ios::showpos);
cout << t_avg << " " << t_stddev << " " << t_avgerr << endl;
cout.unsetf(ios::showpos);
tm.
test_abs(t_avg,exact_res[0],t_avgerr*10.0,
"tab 1");
t->get_column("mult"));
t->get_column("mult"));
t_avgerr=t_stddev/sqrt((double)n);
cout.setf(ios::showpos);
cout << t_avg << " " << t_stddev << " " << t_avgerr << endl;
cout.unsetf(ios::showpos);
tm.
test_abs(t_avg,exact_res[1],t_avgerr*10.0,
"tab 2");
cout << endl;
cout << "MCMC with affine-invariant sampling:\n" << endl;
mct.
mcmc(2,init,low,high,vpf[0],vff[0]);
cout << "n_accept, n_reject, table lines: "
<< t->get_nlines() << endl;
cout << " i mult log_wgt x0 x1 "
<< "x0sq x0sq_x1sq" << endl;
cout.precision(4);
for(size_t i=0;i<t->get_nlines();i+=t->get_nlines()/10) {
cout.width(5);
cout << i << " " << t->get("mult",i) << " ";
cout.setf(ios::showpos);
cout << t->get("log_wgt",i) << " " << t->get("x0",i) << " ";
cout << t->get("x1",i) << " " << t->get("x0sq",i) << " "
<< t->get("x0sq_x1sq",i) << endl;
cout.unsetf(ios::showpos);
}
cout.precision(6);
cout << endl;
n=t->get_nlines();
t->get_column("mult"));
t->get_column("mult"));
t_avgerr=t_stddev/sqrt((double)n);
cout.setf(ios::showpos);
cout << t_avg << " " << t_stddev << " " << t_avgerr << endl;
cout.unsetf(ios::showpos);
tm.
test_abs(t_avg,exact_res[0],t_avgerr*10.0,
"tab 1");
t->get_column("mult"));
t->get_column("mult"));
t_avgerr=t_stddev/sqrt((double)n);
cout.setf(ios::showpos);
cout << t_avg << " " << t_stddev << " " << t_avgerr << endl;
cout.unsetf(ios::showpos);
tm.
test_abs(t_avg,exact_res[1],t_avgerr*10.0,
"tab 2");
cout << endl;
return 0;
}