<commit_before>/***
  This file is part of mc3kit.
  
  Copyright (C) 2013 <NAME>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***/

package mc3kit.types.doublevalue.distributions;

import mc3kit.model.Model;
import mc3kit.model.Variable;
import mc3kit.step.univariate.VariableProposer;
import mc3kit.types.doublevalue.DoubleDistribution;
import mc3kit.types.doublevalue.DoubleVariable;
import mc3kit.types.doublevalue.proposers.MHUniformProposer;
import static java.lang.Math.*;

public class UniformDistribution extends DoubleDistribution {
	double min;
	double max;
	double logP;
	
	protected UniformDistribution() {
	}
	
	public UniformDistribution(Model model) {
		this(model, null);
	}
	
	public UniformDistribution(Model model, String name) {
		this(model, name, 0.0, 1.0);
	}
	
	public UniformDistribution(Model model, double min, double max)
			throws IllegalArgumentException {
		this(model, null, min, max);
	}
	
	public UniformDistribution(Model model, String name, double min, double max)
			throws IllegalArgumentException {
		super(model, name);
		
		if(Double.isNaN(min) || Double.isNaN(max)) {
			throw new IllegalArgumentException("min or max is NaN");
		}
		
		if(min >= max) {
			throw new IllegalArgumentException("min must be less than max.");
		}
		
		if(Double.isInfinite(min) || Double.isInfinite(max)) {
			throw new IllegalArgumentException("min or max is infinite");
		}
		
		this.min = min;
		this.max = max;
		logP = getLogP(min, max);
	}
	
	@Override
	public VariableProposer makeVariableProposer(String varName) {
		return new MHUniformProposer(varName, min, max);
	}
	
	@Override
	public boolean valueIsValid(double value) {
		return value > min && value < max;
	}
	
	@Override
	public double getLogP(Variable var) {
		return logP;
	}
	
	@Override
	public void sample(Variable var) {
		double newVal = min + (max - min) * getRng().nextDouble();
		
		assert !Double.isNaN(newVal);
		assert !Double.isInfinite(newVal);
		assert newVal > min;<commit_msg>Switching to multiplier proposal if minimum is zero.<commit_after>/***
  This file is part of mc3kit.
  
  Copyright (C) 2013 <NAME>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***/

package mc3kit.types.doublevalue.distributions;

import mc3kit.model.Model;
import mc3kit.model.Variable;
import mc3kit.step.univariate.VariableProposer;
import mc3kit.types.doublevalue.DoubleDistribution;
import mc3kit.types.doublevalue.DoubleVariable;
import mc3kit.types.doublevalue.proposers.MHMultiplierProposer;
import mc3kit.types.doublevalue.proposers.MHUniformProposer;
import static java.lang.Math.*;

public class UniformDistribution extends DoubleDistribution {
	double min;
	double max;
	double logP;
	
	protected UniformDistribution() {
	}
	
	public UniformDistribution(Model model) {
		this(model, null);
	}
	
	public UniformDistribution(Model model, String name) {
		this(model, name, 0.0, 1.0);
	}
	
	public UniformDistribution(Model model, double min, double max)
			throws IllegalArgumentException {
		this(model, null, min, max);
	}
	
	public UniformDistribution(Model model, String name, double min, double max)
			throws IllegalArgumentException {
		super(model, name);
		
		if(Double.isNaN(min) || Double.isNaN(max)) {
			throw new IllegalArgumentException("min or max is NaN");
		}
		
		if(min >= max) {
			throw new IllegalArgumentException("min must be less than max.");
		}
		
		if(Double.isInfinite(min) || Double.isInfinite(max)) {
			throw new IllegalArgumentException("min or max is infinite");
		}
		
		this.min = min;
		this.max = max;
		logP = getLogP(min, max);
	}
	
	@Override
	public VariableProposer makeVariableProposer(String varName) {
		if(min == 0) {
			return new MHMultiplierProposer(varName);
		}
		return new MHUniformProposer(varName, min, max);
	}
	
	@Override
	public boolean valueIsValid(double value) {
		return value > min && value < max;
	}
	
	@Override
	public double getLogP(Variable var) {
		return logP;
	}
	
	@Override
	public void sample(Variable var) {
		double newVal = min + (max - min) * getRng().nextDouble();
		
		assert !Double.isNaN(newVal);
		assert !Double.isInfinite(newVal);
		assert newVal > min;
{'fraction_non_alphanumeric': 0.09016240433078215, 'fraction_numerical': 0.006720179204778794, 'mean_word_length': 4.201941747572816, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '6541641', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1566, 'n_words': 539}