public final double doOperation() throws OperatorFailedException {
  final int dim=indicatorParameter.getDimension();
  double sum=0.0;
  for (int i=0; i < dim; i++) {
    sum+=indicatorParameter.getParameterValue(i);
  }
  final int pos=MathUtils.nextInt(dim);
  final int value=(int)indicatorParameter.getParameterValue(pos);
  double rand=0;
  if (rateParameter != null)   rand=MathUtils.nextDouble();
  double logq;
  if (value == 0) {
    indicatorParameter.setParameterValue(pos,1.0);
    logq=-Math.log((dim - sum) / (sum + 1));
  }
 else   if (value == 1) {
    indicatorParameter.setParameterValue(pos,0.0);
    logq=-Math.log(sum / (dim - sum + 1));
    rand*=-1;
  }
 else {
    throw new RuntimeException("expected 1 or 0");
  }
  if (rateParameter != null) {
    final double scale=Math.exp((rand) * scaleFactor);
    logq+=Math.log(scale);
    final double oldValue=rateParameter.getParameterValue(0);
    final double newValue=scale * oldValue;
    rateParameter.setParameterValue(0,newValue);
  }
  return logq;
}

public final double doOperation ( ) throws OperatorFailedException { final int dim = indicatorParameter . getDimension ( ) ; double sum = 0.0 ; for ( int i = 0 ; i < dim ; i ++ ) { sum += indicatorParameter . getParameterValue ( i ) ; } final int pos = MathUtils . nextInt ( dim ) ; final int value = ( int ) indicatorParameter . getParameterValue ( pos ) ; double rand = 0 ; if ( rateParameter != null ) rand = MathUtils . nextDouble ( ) ; double logq ; if ( value == 0 ) { indicatorParameter . setParameterValue ( pos , 1.0 ) ; logq = - Math . log ( ( dim - sum ) / ( sum + 1 ) ) ; } else if ( value == 1 ) { indicatorParameter . setParameterValue ( pos , 0.0 ) ; logq = - Math . log ( sum / ( dim - sum + 1 ) ) ; rand *= - 1 ; } else { throw new RuntimeException ( "expected 1 or 0" ) ; } if ( rateParameter != null ) { final double scale = Math . exp ( ( rand ) * scaleFactor ) ; logq += Math . log ( scale ) ; final double oldValue = rateParameter . getParameterValue ( 0 ) ; final double newValue = scale * oldValue ; rateParameter . setParameterValue ( 0 , newValue ) ; } return logq ; }
change the indicatorparameter and return the hastings ratio . flip ( switch a 0 to 1 or 1 to 0 ) for a random bit in a bit vector . return the hastings ratio which makes all subsets of vectors with the same number of 1 bits equiprobable .
