<commit_before>
/*
 * File Operator.java
 *
 * Copyright (C) 2010 <NAME> <EMAIL>
 *
 * This file is part of BEAST2.
 * 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 beast.core;

import beast.core.Input.Validate;


@Description("Proposes a move in state space.")
public abstract class Operator extends Plugin {
	public Input<Double> m_pWeight = new Input<Double>("weight","weight with which this operator is selected", Validate.REQUIRED);

	/** Implement this for proposing a new State. 
	 * The proposal is responsible for keeping the State valid,
	 * and if the State becomes invalid (e.g. a parameter goes out
	 * of its range) Double.NEGATIVE_INFINITY should be returned.
	 * 
	 * If the operator is a Gibbs operator, hence the proposal should 
	 * always be accepted, the method should return Double.POSITIVE_INFINITY.
	 * 
	 * @return log of Hastings Ratio, or Double.NEGATIVE_INFINITY if proposal 
	 * should not be accepted (because the proposal is invalid) or 
	 * Double.POSITIVE_INFINITY if the proposal should always be accepted 
	 * (for Gibbs operators).
     **/
	abstract public double proposal();

	/**
     * @return the relative weight which determines the probability this proposal is chosen
	 * from among all the available proposals
	 */
	public double getWeight() {
		return m_pWeight.get();
	}

	public String getName() {
        return this.getClass().getName() + (getID()!=null?"_" +getID():"");
    }


	/** keep statistics of how often this operator was used, accepted or rejected **/
	int m_nNrRejected = 0;
	int m_nNrAccepted = 0;
	public void accept() {
		m_nNrAccepted++;
	}

	public void reject() {
		m_nNrRejected++;
	}

	/** called after every invocation of this operator to see whether
	 * a parameter can be optimised for better acceptance hence faster
	 * mixing
	 * @param logAlpha difference in posterior between previous state & proposed state + hasting ratio
	 */
	public void optimize(double logAlpha) {
		// must be overridden by operator implementation to have an effect
	}

	/** @return  change of value of a parameter for MCMC chain optimisation
     * @param logAlpha difference in posterior between previous state & proposed state + hasting ratio
     **/
	protected double calcDelta(double logAlpha) {

        final double target = getTargetAcceptanceProbability();

        final double deltaP = ((1.0 / (m_nNrRejected + m_nNrAccepted + 1.0)) * (Math.exp(Math.min(logAlpha, 0)) - target));

        if (deltaP > -Double.MAX_VALUE && deltaP < Double.MAX_VALUE) {
            return deltaP;
        }
        return 0;
	} // calcDelta

	/** @return target for automatic operator optimisation
     **/
    public double getTargetAcceptanceProbability() {
        return 0.234;
    }
    
	public String toString() {
		String sName = getName();
		if (sName.length() < 40) {
			sName +=  "                                        ".substring(sName.length(), 40);
		}
		return sName + "\t" + m_nNrAccepted + "\t" + m_nNrRejected + "\t" +
		(m_nNrAccepted + m_nNrRejected) + "\t" +
		((m_nNrAccepted+0.0)/(m_nNrAccepted + m_nNrRejected));
	}

} // class Operator<commit_msg>delay auto optimise for 100.000 optimisable steps<commit_after>
/*
 * File Operator.java
 *
 * Copyright (C) 2010 <NAME> <EMAIL>
 *
 * This file is part of BEAST2.
 * 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 beast.core;

import beast.core.Input.Validate;


@Description("Proposes a move in state space.")
public abstract class Operator extends Plugin {
	public Input<Double> m_pWeight = new Input<Double>("weight","weight with which this operator is selected", Validate.REQUIRED);

	/** Implement this for proposing a new State. 
	 * The proposal is responsible for keeping the State valid,
	 * and if the State becomes invalid (e.g. a parameter goes out
	 * of its range) Double.NEGATIVE_INFINITY should be returned.
	 * 
	 * If the operator is a Gibbs operator, hence the proposal should 
	 * always be accepted, the method should return Double.POSITIVE_INFINITY.
	 * 
	 * @return log of Hastings Ratio, or Double.NEGATIVE_INFINITY if proposal 
	 * should not be accepted (because the proposal is invalid) or 
	 * Double.POSITIVE_INFINITY if the proposal should always be accepted 
	 * (for Gibbs operators).
     **/
	abstract public double proposal();

	/**
     * @return the relative weight which determines the probability this proposal is chosen
	 * from among all the available proposals
	 */
	public double getWeight() {
		return m_pWeight.get();
	}

	public String getName() {
        return this.getClass().getName() + (getID()!=null?"_" +getID():"");
    }


	/** keep statistics of how often this operator was used, accepted or rejected **/
	int m_nNrRejected = 0;
	int m_nNrAccepted = 0;
	public void accept() {
		m_nNrAccepted++;
	}

	public void reject() {
		m_nNrRejected++;
	}

	/** called after every invocation of this operator to see whether
	 * a parameter can be optimised for better acceptance hence faster
	 * mixing
	 * @param logAlpha difference in posterior between previous state & proposed state + hasting ratio
	 */
	public void optimize(double logAlpha) {
		// must be overridden by operator implementation to have an effect
	}

	/** @return  change of value of a parameter for MCMC chain optimisation
     * @param logAlpha difference in posterior between previous state & proposed state + hasting ratio
     **/
	static int g_autoOptimizeDelay = 0;
	protected double calcDelta(double logAlpha) {
		// do no optimisation for the first N optimisable operations
		if (g_autoOptimizeDelay < 100000) {
			g_autoOptimizeDelay++;
			return 0;
		}
        final double target = getTargetAcceptanceProbability();

        final double deltaP = ((1.0 / (m_nNrRejected + m_nNrAccepted + 1.0)) * (Math.exp(Math.min(logAlpha, 0)) - target));

        if (deltaP > -Double.MAX_VALUE && deltaP < Double.MAX_VALUE) {
            return deltaP;
        }
        return 0;
	} // calcDelta
	
	/** @return target for automatic operator optimisation
     **/
    public double getTargetAcceptanceProbability() {
        return 0.234;
    }
    
	public String toString() {
		String sName = getName();
		if (sName.length() < 40) {
			sName +=  "                                        ".substring(sName.length(), 40);
		}
		return sName + "\t" + m_nNrAccepted + "\t" + m_nNrRejected + "\t" +
		(m_nNrAccepted + m_nNrRejected) + "\t" +
		((m_nNrAccepted+0.0)/(m_nNrAccepted + m_nNrRejected));
	}

} // class Operator
{'fraction_non_alphanumeric': 0.08792569659442724, 'fraction_numerical': 0.010402476780185759, 'mean_word_length': 3.3302949061662197, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2947588', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2238, 'n_words': 922}