<commit_before>/*
 * BitFlipOperator.java
 *
 * Copyright (C) 2002-2006 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.inference.operators;

import dr.inference.model.Parameter;
import dr.math.MathUtils;
import dr.xml.*;

/**
 * A generic operator that flips bits.
 *
 * @author <NAME>
 *
 * @version $Id$
 */
public class BitFlipOperator extends SimpleMCMCOperator {
	
	public static final String BIT_FLIP_OPERATOR = "bitFlipOperator";

	public BitFlipOperator(Parameter parameter, int weight) {
		this.parameter = parameter;
		this.weight = weight;
	}
	
	/** @return the parameter this operator acts on. */
	public Parameter getParameter() { return parameter; }

	/** change the parameter and return the hastings ratio. 
	 * performs a delta exchange operation between two scalars in the vector
	 * and return the hastings ratio.
	 */
	public final double doOperation() {


        int n = parameter.getDimension();
        double sum = 0.0;
        double logq;

        for (int i = 0; i < n; i++) {
            sum += parameter.getParameterValue(i);
        }


        int pos = MathUtils.nextInt(parameter.getDimension());

        int value = (int)parameter.getParameterValue(pos);

        if (value == 0) {
            parameter.setParameterValue(pos, 1.0);

            logq = -Math.log((n-sum) / (sum+1));

        } else if (value == 1) {
            parameter.setParameterValue(pos, 0.0);

            logq = -Math.log(sum / (n-sum-1) );

        } else {
            throw new RuntimeException("expected 1 or 0");
        }

        // hastings ratio is designed to make move symmetric on sum of 1's
        return logq;
	}

	// Interface MCMCOperator
	public final String getOperatorName() { return parameter.getParameterName(); }

	public int getWeight() { return weight; }

	public void setWeight(int w) { weight = w; }

	public final String getPerformanceSuggestion() {

		return "no performance suggestion";
	}
	
	public String toString() { return getOperatorName(); }
	
	public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
		
		public String getParserName() { return BIT_FLIP_OPERATOR; }
		<commit_msg>fix incorrect hastings ratio<commit_after>/*
 * BitFlipOperator.java
 *
 * Copyright (C) 2002-2006 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.inference.operators;

import dr.inference.model.Parameter;
import dr.math.MathUtils;
import dr.xml.*;

/**
 * A generic operator that flips bits.
 *
 * @author <NAME>
 *
 * @version $Id$
 */
public class BitFlipOperator extends SimpleMCMCOperator {
	
	public static final String BIT_FLIP_OPERATOR = "bitFlipOperator";

	public BitFlipOperator(Parameter parameter, int weight) {
		this.parameter = parameter;
		this.weight = weight;
	}
	
	/** @return the parameter this operator acts on. */
	public Parameter getParameter() { return parameter; }

	/** change the parameter and return the hastings ratio. 
	 * performs a delta exchange operation between two scalars in the vector
	 * and return the hastings ratio.
	 */
	public final double doOperation() {
        final int dim = parameter.getDimension();
        double sum = 0.0;
       
        for (int i = 0; i < dim; i++) {
            sum += parameter.getParameterValue(i);
        }

        final int pos = MathUtils.nextInt(dim);

        final int value = (int)parameter.getParameterValue(pos);
        double logq;
        if (value == 0) {
            parameter.setParameterValue(pos, 1.0);

            logq = -Math.log((dim - sum) / (sum+1));

        } else if (value == 1) {
            parameter.setParameterValue(pos, 0.0);

            logq = -Math.log(sum / (dim - sum+1) );

        } else {
            throw new RuntimeException("expected 1 or 0");
        }

        // hastings ratio is designed to make move symmetric on sum of 1's
        return logq;
	}

	// Interface MCMCOperator
	public final String getOperatorName() { return parameter.getParameterName(); }

	public int getWeight() { return weight; }

	public void setWeight(int w) { weight = w; }

	public final String getPerformanceSuggestion() {

		return "no performance suggestion";
	}
	
	public String toString() { return getOperatorName(); }
	
	public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
		
		public String getParserName() { return BIT_FLIP_OPERATOR; }
		
{'fraction_non_alphanumeric': 0.09006775739547182, 'fraction_numerical': 0.01123781193191208, 'mean_word_length': 3.029294274300932, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '970888', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1628, 'n_words': 658}