27 w(scale),Kmax(kmax),dim(Dim),n_evals(0){
28 assert(scale.size()>=Dim);
33 ,std::vector<V> &chain
39 if(lnprob(xo) <= -1.0e20){
40 std::cerr <<
"Slicer: Initial point has 0 probability" << std::endl;
41 throw std::runtime_error(
"bad point");
47 if(verbose) std::cout << std::endl;
49 chain[i] = chain[i-1];
50 if(step_type) step_double(chain[i],lnprob,k,ran);
51 else step_out(chain[i],lnprob,k,ran);
53 if(verbose) std::cout << i <<
" " << n_evals <<
" " << chain[i] << std::endl;
61 V xl,xr,ll,rr,lshrink,rshrink,x_new;
68 void step_double(V &x,L &lnprob,
int i,R &ran){
70 double y = lnprob(x) + log(ran()); ++n_evals;
72 assert(!std::isnan(y));
76 xl = x; xl[i] = x[i] - w[i] * ran();;
77 xr = x; xr[i] = xl[i] + w[i];
79 double lprob = lnprob(xl); ++n_evals;
80 double rprob = lnprob(xr); ++n_evals;
82 while( k > 0 && ( y < lprob || y < rprob )){
85 xl[i] = 2*xl[i] - xr[i];
86 lprob = lnprob(xl); ++n_evals;
88 xr[i] = 2*xr[i] - xl[i];
89 rprob = lnprob(xr); ++n_evals;
100 x_new[i] = lshrink[i] + ran()*( rshrink[i] - lshrink[i] );
102 if( y < lnprob(x_new) && accept(y,x,x_new,i,lnprob))
break;
104 lshrink[i] = x_new[i];
106 rshrink[i] = x_new[i];
113 void step_out(V &x,L &lnprob,
int i,R &ran){
116 double y = lnprob(x) + log(ran()); ++n_evals;
121 xl[i] = x[i] - w[i] * ran();;
122 xr[i] = xl[i] + w[i];
123 int j = floor(Kmax*ran());
124 int k = Kmax - 1 - j;
126 while( j > 0 && y < lnprob(xl)){
128 xl[i] = xl[i] - w[i];
131 while( k > 0 && y < lnprob(xr)){
133 xr[i] = xr[i] + w[i];
143 x_new[i] = lshrink[i] + ran()*( rshrink[i] - lshrink[i] );
145 if( y < lnprob(x_new) )
break;
147 lshrink[i] = x_new[i];
149 rshrink[i] = x_new[i];
156 bool accept(
const double y,V &xo,V &x1,
int i,L &lnprob){
161 double lprob = lnprob(ll); ++n_evals;
162 double rprob = lnprob(rr); ++n_evals;
164 while( (rr[i] - ll[i]) > 1.1 * w[i]){
165 double m = (ll[i]+rr[i])/2;
166 if( (xo[i] < m)*(x1[i] >= m) ||
167 (xo[i] >= m)*(x1[i] < m) ) D =
true;
171 rprob = lnprob(rr); ++n_evals;
174 lprob = lnprob(ll); ++n_evals;
176 if(D && y >= lprob && y >= rprob )
return false;
197 w(scale),dim(Dim),n_evals(0),active(active_parameters)
void run(L &lnprob, std::vector< V > &chain, V xo, R &ran, int step_type, bool verbose=false)
run the MC chain
Definition slicer.h:32