use of beast.core.parameter.IntegerParameter in project beast2 by CompEvol.
the class DeltaExchangeOperatorTest method testKeepsWeightedSum.
@Test
public void testKeepsWeightedSum() {
RealParameter parameter = new RealParameter(new Double[] { 1., 1., 1., 1. });
register(new DeltaExchangeOperator(), "weightvector", new IntegerParameter(new Integer[] { 0, 1, 2, 1 }), "parameter", parameter);
Double[] p = parameter.getValues();
assertEquals("The DeltaExchangeOperator should not change the sum of a parameter", 0 * p[1] + 1 * p[1] + 2 * p[2] + 1 * p[3], 4, 0.00001);
}
use of beast.core.parameter.IntegerParameter in project beast2 by CompEvol.
the class IntRandomWalkOperatorTest method instantiate.
public void instantiate(int dimension, int upper, int runs, Integer[] windowSizes) {
for (int r = 0; r < runs; r++) {
for (Integer windowSize : windowSizes) {
try {
int[][] count = new int[dimension][upper + 1];
Integer[] init = new Integer[dimension];
Arrays.fill(init, 0);
IntegerParameter parameter = new IntegerParameter(init);
parameter.setLower(0);
parameter.setUpper(upper);
State state = new State();
state.initByName("stateNode", parameter);
state.initialise();
IntRandomWalkOperator operator = new IntRandomWalkOperator();
operator.initByName("parameter", parameter, "windowSize", windowSize, "weight", 1.0);
for (int i = 0; i < 1000000 * (upper + 1); i++) {
operator.proposal();
Integer[] values = parameter.getValues();
for (int k = 0; k < values.length; k++) {
int j = values[k];
count[k][j] += 1;
}
}
System.out.print("Distribution lower = 0, upper = " + upper + " windowSize = " + windowSize);
for (int j = 0; j < count.length; j++) {
// System.out.println("x[" +j + "] = " + Arrays.toString(count[j]));
}
int sum = 0;
for (int i = 0; i < dimension; i++) {
for (int k = 0; k < count[i].length; k++) {
sum += Math.abs(count[i][k] - 1000000);
}
}
System.out.println(" Average deviation: " + sum / (dimension * (upper + 1)));
assertTrue("average deviation (" + sum / (dimension * (upper + 1)) + ") exceeds 10000", sum / (dimension * (upper + 1)) < 10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
use of beast.core.parameter.IntegerParameter in project beast2 by CompEvol.
the class IntUniformOperator method proposal.
/**
* override this for proposals,
* returns log of hastingRatio, or Double.NEGATIVE_INFINITY if proposal should not be accepted *
*/
@Override
public double proposal() {
IntegerParameter param = parameterInput.get(this);
int i = Randomizer.nextInt(param.getDimension());
int newValue = Randomizer.nextInt(param.getUpper() - param.getLower() + 1) + param.getLower();
param.setValue(i, newValue);
return 0.0;
}
use of beast.core.parameter.IntegerParameter in project beast2 by CompEvol.
the class UCRelaxedClockModel method initAndValidate.
@Override
public void initAndValidate() {
tree = treeInput.get();
branchCount = tree.getNodeCount() - 1;
categories = categoryInput.get();
usingQuantiles = (categories == null);
if (!usingQuantiles) {
LATTICE_SIZE_FOR_DISCRETIZED_RATES = numberOfDiscreteRates.get();
if (LATTICE_SIZE_FOR_DISCRETIZED_RATES <= 0)
LATTICE_SIZE_FOR_DISCRETIZED_RATES = branchCount;
Log.info.println(" UCRelaxedClockModel: using " + LATTICE_SIZE_FOR_DISCRETIZED_RATES + " rate " + "categories to approximate rate distribution across branches.");
} else {
if (numberOfDiscreteRates.get() != -1) {
throw new RuntimeException("Can't specify both numberOfDiscreteRates and rateQuantiles inputs.");
}
Log.info.println(" UCRelaxedClockModel: using quantiles for rate distribution across branches.");
}
if (usingQuantiles) {
quantiles = quantileInput.get();
quantiles.setDimension(branchCount);
Double[] initialQuantiles = new Double[branchCount];
for (int i = 0; i < branchCount; i++) {
initialQuantiles[i] = Randomizer.nextDouble();
}
RealParameter other = new RealParameter(initialQuantiles);
quantiles.assignFromWithoutID(other);
quantiles.setLower(0.0);
quantiles.setUpper(1.0);
} else {
categories.setDimension(branchCount);
Integer[] initialCategories = new Integer[branchCount];
for (int i = 0; i < branchCount; i++) {
initialCategories[i] = Randomizer.nextInt(LATTICE_SIZE_FOR_DISCRETIZED_RATES);
}
// set initial values of rate categories
IntegerParameter other = new IntegerParameter(initialCategories);
categories.assignFromWithoutID(other);
categories.setLower(0);
categories.setUpper(LATTICE_SIZE_FOR_DISCRETIZED_RATES - 1);
}
distribution = rateDistInput.get();
if (!usingQuantiles) {
// rates are initially zero and are computed by getRawRate(int i) as needed
rates = new double[LATTICE_SIZE_FOR_DISCRETIZED_RATES];
storedRates = new double[LATTICE_SIZE_FOR_DISCRETIZED_RATES];
// System.arraycopy(rates, 0, storedRates, 0, rates.length);
}
normalize = normalizeInput.get();
meanRate = meanRateInput.get();
if (meanRate == null) {
meanRate = new RealParameter("1.0");
}
try {
double mean = rateDistInput.get().getMean();
if (Math.abs(mean - 1.0) > 1e-6) {
Log.warning.println("WARNING: mean of distribution for relaxed clock model is not 1.0.");
}
} catch (RuntimeException e) {
// ignore
}
}
use of beast.core.parameter.IntegerParameter in project beast2 by CompEvol.
the class IntRandomWalkOperator method proposal.
/**
* override this for proposals,
* returns log of hastingRatio, or Double.NEGATIVE_INFINITY if proposal should not be accepted *
*/
@Override
public double proposal() {
final IntegerParameter param = parameterInput.get(this);
final int i = Randomizer.nextInt(param.getDimension());
final int value = param.getValue(i);
final int newValue = value + Randomizer.nextInt(2 * windowSize + 1) - windowSize;
if (newValue < param.getLower() || newValue > param.getUpper()) {
// invalid move, can be rejected immediately
return Double.NEGATIVE_INFINITY;
}
if (newValue == value) {
// this saves calculating the posterior
return Double.NEGATIVE_INFINITY;
}
param.setValue(i, newValue);
return 0.0;
}
Aggregations