use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.
the class ScaleOperator method proposal.
/**
* override this for proposals,
*
* @return log of Hastings Ratio, or Double.NEGATIVE_INFINITY if proposal should not be accepted *
*/
@Override
public double proposal() {
try {
double hastingsRatio;
final double scale = getScaler();
if (m_bIsTreeScaler) {
final Tree tree = treeInput.get(this);
if (rootOnlyInput.get()) {
final Node root = tree.getRoot();
final double newHeight = root.getHeight() * scale;
if (newHeight < Math.max(root.getLeft().getHeight(), root.getRight().getHeight())) {
return Double.NEGATIVE_INFINITY;
}
root.setHeight(newHeight);
return -Math.log(scale);
} else {
// scale the beast.tree
final int internalNodes = tree.scale(scale);
return Math.log(scale) * (internalNodes - 2);
}
}
// not a tree scaler, so scale a parameter
final boolean scaleAll = scaleAllInput.get();
final int specifiedDoF = degreesOfFreedomInput.get();
final boolean scaleAllIndependently = scaleAllIndependentlyInput.get();
final RealParameter param = parameterInput.get(this);
assert param.getLower() != null && param.getUpper() != null;
final int dim = param.getDimension();
if (scaleAllIndependently) {
// update all dimensions independently.
hastingsRatio = 0;
final BooleanParameter indicators = indicatorInput.get();
if (indicators != null) {
final int dimCount = indicators.getDimension();
final Boolean[] indicator = indicators.getValues();
final boolean impliedOne = dimCount == (dim - 1);
for (int i = 0; i < dim; i++) {
if ((impliedOne && (i == 0 || indicator[i - 1])) || (!impliedOne && indicator[i])) {
final double scaleOne = getScaler();
final double newValue = scaleOne * param.getValue(i);
hastingsRatio -= Math.log(scaleOne);
if (outsideBounds(newValue, param)) {
return Double.NEGATIVE_INFINITY;
}
param.setValue(i, newValue);
}
}
} else {
for (int i = 0; i < dim; i++) {
final double scaleOne = getScaler();
final double newValue = scaleOne * param.getValue(i);
hastingsRatio -= Math.log(scaleOne);
if (outsideBounds(newValue, param)) {
return Double.NEGATIVE_INFINITY;
}
param.setValue(i, newValue);
}
}
} else if (scaleAll) {
// update all dimensions
// hasting ratio is dim-2 times of 1dim case. would be nice to have a reference here
// for the proof. It is supposed to be somewhere in an Alexei/Nicholes article.
// all Values assumed independent!
final int computedDoF = param.scale(scale);
final int usedDoF = (specifiedDoF > 0) ? specifiedDoF : computedDoF;
hastingsRatio = (usedDoF - 2) * Math.log(scale);
} else {
hastingsRatio = -Math.log(scale);
// which position to scale
final int index;
final BooleanParameter indicators = indicatorInput.get();
if (indicators != null) {
final int dimCount = indicators.getDimension();
final Boolean[] indicator = indicators.getValues();
final boolean impliedOne = dimCount == (dim - 1);
// available bit locations. there can be hundreds of them. scan list only once.
final int[] loc = new int[dimCount + 1];
int locIndex = 0;
if (impliedOne) {
loc[locIndex] = 0;
++locIndex;
}
for (int i = 0; i < dimCount; i++) {
if (indicator[i]) {
loc[locIndex] = i + (impliedOne ? 1 : 0);
++locIndex;
}
}
if (locIndex > 0) {
final int rand = Randomizer.nextInt(locIndex);
index = loc[rand];
} else {
// no active indicators
return Double.NEGATIVE_INFINITY;
}
} else {
// any is good
index = Randomizer.nextInt(dim);
}
final double oldValue = param.getValue(index);
if (oldValue == 0) {
// Error: parameter has value 0 and cannot be scaled
return Double.NEGATIVE_INFINITY;
}
final double newValue = scale * oldValue;
if (outsideBounds(newValue, param)) {
// reject out of bounds scales
return Double.NEGATIVE_INFINITY;
}
param.setValue(index, newValue);
// provides a hook for subclasses
// cleanupOperation(newValue, oldValue);
}
return hastingsRatio;
} catch (Exception e) {
// whatever went wrong, we want to abort this operation...
return Double.NEGATIVE_INFINITY;
}
}
use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.
the class ScaleOperator method initAndValidate.
@Override
public void initAndValidate() {
m_fScaleFactor = scaleFactorInput.get();
m_bIsTreeScaler = (treeInput.get() != null);
upper = scaleUpperLimit.get();
lower = scaleLowerLimit.get();
final BooleanParameter indicators = indicatorInput.get();
if (indicators != null) {
if (m_bIsTreeScaler) {
throw new IllegalArgumentException("indicator is specified which has no effect for scaling a tree");
}
final int dataDim = parameterInput.get().getDimension();
final int indsDim = indicators.getDimension();
if (!(indsDim == dataDim || indsDim + 1 == dataDim)) {
throw new IllegalArgumentException("indicator dimension not compatible from parameter dimension");
}
}
}
use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.
the class BitFlipOperator method proposal.
/**
* Change the parameter and return the hastings ratio.
* Flip (Switch a 0 to 1 or 1 to 0) for a random bit in a bit vector.
* Return the hastings ratio which makes all subsets of vectors with the same number of 1 bits
* equiprobable, unless !usesPriorOnSum , then all configurations are equiprobable
*/
@Override
public double proposal() {
final BooleanParameter p = parameterInput.get(this);
final int dim = p.getDimension();
double sum = 0.0;
if (usesPriorOnSum) {
for (int i = 0; i < dim; i++) {
if (p.getValue(i))
sum += 1;
}
}
final int pos = Randomizer.nextInt(dim);
final boolean value = p.getValue(pos);
double logq = 0.0;
if (!value) {
p.setValue(pos, true);
if (usesPriorOnSum) {
logq = -Math.log((dim - sum) / (sum + 1));
}
} else {
// assert value;
p.setValue(pos, false);
if (usesPriorOnSum) {
logq = -Math.log(sum / (dim - sum + 1));
}
}
return logq;
}
use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.
the class RandomLocalClockModel method recalculateScaleFactor.
private void recalculateScaleFactor() {
BooleanParameter indicators = indicatorParamInput.get();
RealParameter rates = rateParamInput.get();
calculateUnscaledBranchRates(m_tree.getRoot(), 1.0, indicators, rates);
double timeTotal = 0.0;
double branchTotal = 0.0;
for (int i = 0; i < m_tree.getNodeCount(); i++) {
Node node = m_tree.getNode(i);
if (!node.isRoot()) {
double branchInTime = node.getParent().getHeight() - node.getHeight();
double branchLength = branchInTime * unscaledBranchRates[node.getNr()];
timeTotal += branchInTime;
branchTotal += branchLength;
}
}
scaleFactor = timeTotal / branchTotal;
scaleFactor *= meanRate.getValue();
}
use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.
the class SumTest method testSum.
@Test
public void testSum() {
RealParameter p1 = new RealParameter("1.0 2.0");
Sum sum = new Sum();
// single argument sum
sum.initByName("arg", p1);
double v = sum.getArrayValue();
assertEquals(3.0, v, 1e-10);
// multiple argument sum
sum = new Sum();
RealParameter p2 = new RealParameter("2.0 2.5");
sum.initByName("arg", p1, "arg", p2);
v = sum.getArrayValue();
assertEquals(7.5, v, 1e-10);
// multiple same argument sum
sum = new Sum();
sum.initByName("arg", p1, "arg", p1);
v = sum.getArrayValue();
assertEquals(6.0, v, 1e-10);
// sum of integers
IntegerParameter p3 = new IntegerParameter("1 2 5");
sum = new Sum();
sum.initByName("arg", p3);
v = sum.getArrayValue();
assertEquals(8.0, v, 1e-10);
// sum of boolean
BooleanParameter p4 = new BooleanParameter("true false false true true");
sum = new Sum();
sum.initByName("arg", p4);
v = sum.getArrayValue();
assertEquals(3.0, v, 1e-10);
// sum of booleans and integer
sum = new Sum();
sum.initByName("arg", p4, "arg", p3);
v = sum.getArrayValue();
assertEquals(11.0, v, 1e-10);
// sum of booleans and real
sum = new Sum();
sum.initByName("arg", p1, "arg", p4);
v = sum.getArrayValue();
assertEquals(6.0, v, 1e-10);
}
Aggregations