<commit_before>/*
 * MarginalLikelihoodEstimator.java
 *
 * Copyright (c) 2002-2012 <NAME>, <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.mcmc;

import dr.inference.loggers.Logger;
import dr.inference.loggers.MCLogger;
import dr.inference.markovchain.MarkovChain;
import dr.inference.markovchain.MarkovChainListener;
import dr.inference.model.Model;
import dr.inference.model.PathLikelihood;
import dr.inference.operators.*;
import dr.inference.prior.Prior;
import dr.util.Identifiable;
import dr.xml.*;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.BetaDistributionImpl;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class MarginalLikelihoodEstimator implements Runnable, Identifiable {

    public MarginalLikelihoodEstimator(String id, int chainLength, int burninLength, int pathSteps, double fixedRunValue,
//                                       boolean linear, boolean lacing,
PathScheme scheme,
PathLikelihood pathLikelihood,
OperatorSchedule schedule,
MCLogger logger) {

        this.id = id;
        this.chainLength = chainLength;
        this.pathSteps = pathSteps;
        this.scheme = scheme;
        this.schedule = schedule;
        this.fixedRunValue = fixedRunValue;
        // deprecated
        // this.linear = (scheme == PathScheme.LINEAR);
        // this.lacing = false; // Was not such a good idea

        this.burninLength = burninLength;

        MCMCCriterion criterion = new MCMCCriterion();

        pathDelta = 1.0 / pathSteps;
        pathParameter = 1.0;

        this.pathLikelihood = pathLikelihood;
        pathLikelihood.setPathParameter(pathParameter);

        mc = new MarkovChain(Prior.UNIFORM_PRIOR, pathLikelihood, schedule, criterion, 0, 0, true);

        this.logger = logger;
    }

    private void setDefaultBurnin() {
        if (burninLength == -1) {
            burnin = (int) (0.1 * chainLength);
        } else {
            burnin = burninLength;
        }
    }

    public void integrate(Integrator scheme) {
        setDefaultBurnin();
        mc.setCurrentLength(burnin);
        scheme.init();
        for (pathParameter = scheme.nextPathParameter(); pathParameter >= 0; pathParameter = scheme.nextPathParameter()) {
            pathLikelihood.setPathParameter(pathParameter);
            reportIteration(pathParameter, chainLength, burnin, scheme.pathSteps, scheme.step);

            for (int i = 0; i < schedule.getOperatorCount(); ++i) {
                MCMCOperator operator = schedule.getOperator(i);
                if (operator instanceof GibbsOperator) {
                    ((GibbsOperator)operator).setPathParameter(pathParameter);
                }
            }

            long cl = mc.getCurrentLength();
            mc.setCurrentLength(0);
            mc.runChain(burnin, false/*, 0*/);
            mc.setCurrentLength(cl);
            mc.runChain(chainLength, false);
            (new OperatorAnalysisPrinter(schedule)).showOperatorAnalysis(System.out);
            ((CombinedOperatorSchedule) schedule).reset();
        }
    }

    public abstract class Integrator {
        protected int step;
        protected int pathSteps;

        protected Integrator(int pathSteps) {
            this.pathSteps = pathSteps;
        }

        public void init() {
            step = 0;
        }

        abstract double nextPathParameter();
    }
    
    public class FixedThetaRun extends Integrator {
    	private double value;
    	
    	public FixedThetaRun(double value) {
    		super(1);
    		this.value = value;
    	}
    	
    	double nextPathParameter() {
    		if (step == 0) {
    			step++;
    			return value;
    		} else {
    			return -1.0;
    		}
    	}
    	
    }

    public class LinearIntegrator extends Integrator {
        public LinearIntegrator(int pathSteps) {
            super(pathSteps);
        }

        double nextPathParameter() {
            if (step > pathSteps) {
                return -1;
            }
            double pathParameter = 1.0 - (double)step / (double)(pathSteps - 1);
            step = step + 1;
            return pathParameter;
        }
    }

    public class SigmoidIntegrator extends Integrator {
    	private double alpha;

    	public SigmoidIntegrator(double alpha, int pathSteps) {
    		super(pathSteps);
    		this.alpha = alpha;
    	}

    	double nextPathParameter() {
    		if (step == 0) {
    			step++;
    			return 1.0;
    		} else if (step == pathSteps) {
    			step++;
    			return 0.0;
    		} else if (step > pathSteps) {
    			return -1.0;
    		} else {
    			double xvalue = ((pathSteps - step)/((double)pathSteps)) - 0.5;
    			step++;
    			return Math.exp(alpha*xvalue)/(Math.exp(alpha*xvalue) + Math.exp(-alpha*xvalue));
    		}
    	}
    }
<commit_msg>bug fix<commit_after>/*
 * MarginalLikelihoodEstimator.java
 *
 * Copyright (c) 2002-2012 <NAME>, <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.mcmc;

import dr.inference.loggers.Logger;
import dr.inference.loggers.MCLogger;
import dr.inference.markovchain.MarkovChain;
import dr.inference.markovchain.MarkovChainListener;
import dr.inference.model.Model;
import dr.inference.model.PathLikelihood;
import dr.inference.operators.*;
import dr.inference.prior.Prior;
import dr.util.Identifiable;
import dr.xml.*;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.BetaDistributionImpl;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class MarginalLikelihoodEstimator implements Runnable, Identifiable {

    public MarginalLikelihoodEstimator(String id, int chainLength, int burninLength, int pathSteps, double fixedRunValue,
//                                       boolean linear, boolean lacing,
PathScheme scheme,
PathLikelihood pathLikelihood,
OperatorSchedule schedule,
MCLogger logger) {

        this.id = id;
        this.chainLength = chainLength;
        this.pathSteps = pathSteps;
        this.scheme = scheme;
        this.schedule = schedule;
        this.fixedRunValue = fixedRunValue;
        // deprecated
        // this.linear = (scheme == PathScheme.LINEAR);
        // this.lacing = false; // Was not such a good idea

        this.burninLength = burninLength;

        MCMCCriterion criterion = new MCMCCriterion();

        pathDelta = 1.0 / pathSteps;
        pathParameter = 1.0;

        this.pathLikelihood = pathLikelihood;
        pathLikelihood.setPathParameter(pathParameter);

        mc = new MarkovChain(Prior.UNIFORM_PRIOR, pathLikelihood, schedule, criterion, 0, 0, true);

        this.logger = logger;
    }

    private void setDefaultBurnin() {
        if (burninLength == -1) {
            burnin = (int) (0.1 * chainLength);
        } else {
            burnin = burninLength;
        }
    }

    public void integrate(Integrator scheme) {
        setDefaultBurnin();
        mc.setCurrentLength(burnin);
        scheme.init();
        for (pathParameter = scheme.nextPathParameter(); pathParameter >= 0; pathParameter = scheme.nextPathParameter()) {
            pathLikelihood.setPathParameter(pathParameter);
            reportIteration(pathParameter, chainLength, burnin, scheme.pathSteps, scheme.step);

            for (int i = 0; i < schedule.getOperatorCount(); ++i) {
                MCMCOperator operator = schedule.getOperator(i);
                if (operator instanceof GibbsOperator) {
                    ((GibbsOperator)operator).setPathParameter(pathParameter);
                }
            }

            long cl = mc.getCurrentLength();
            mc.setCurrentLength(0);
            mc.runChain(burnin, false/*, 0*/);
            mc.setCurrentLength(cl);
            mc.runChain(chainLength, false);
            (new OperatorAnalysisPrinter(schedule)).showOperatorAnalysis(System.out);
            ((CombinedOperatorSchedule) schedule).reset();
        }
    }

    public abstract class Integrator {
        protected int step;
        protected int pathSteps;

        protected Integrator(int pathSteps) {
            this.pathSteps = pathSteps;
        }

        public void init() {
            step = 0;
        }

        abstract double nextPathParameter();
    }
    
    public class FixedThetaRun extends Integrator {
    	private double value;
    	
    	public FixedThetaRun(double value) {
    		super(1);
    		this.value = value;
    	}
    	
    	double nextPathParameter() {
    		if (step == 0) {
    			step++;
    			return value;
    		} else {
    			return -1.0;
    		}
    	}
    	
    }

    public class LinearIntegrator extends Integrator {
        public LinearIntegrator(int pathSteps) {
            super(pathSteps);
        }

        double nextPathParameter() {
            if (step > pathSteps) {
                return -1;
            }
            double pathParameter = 1.0 - (double)step / (double)(pathSteps);
            step = step + 1;
            return pathParameter;
        }
    }

    public class SigmoidIntegrator extends Integrator {
    	private double alpha;

    	public SigmoidIntegrator(double alpha, int pathSteps) {
    		super(pathSteps);
    		this.alpha = alpha;
    	}

    	double nextPathParameter() {
    		if (step == 0) {
    			step++;
    			return 1.0;
    		} else if (step == pathSteps) {
    			step++;
    			return 0.0;
    		} else if (step > pathSteps) {
    			return -1.0;
    		} else {
    			double xvalue = ((pathSteps - step)/((double)pathSteps)) - 0.5;
    			step++;
    			return Math.exp(alpha*xvalue)/(Math.exp(alpha*xvalue) + Math.exp(-alpha*xvalue));
    		}
    	}
    }

{'fraction_non_alphanumeric': 0.09511681011768838, 'fraction_numerical': 0.009046197084138416, 'mean_word_length': 2.350102971462195, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '412274', 'n_tokens_mistral': 3437, 'n_tokens_neox': 3243, 'n_words': 880}