Search in sources :

Example 11 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class CalibratedBirthDeathModel method requiresRecalculation.

@Override
protected boolean requiresRecalculation() {
    if (super.requiresRecalculation() || birthRateInput.get().somethingIsDirty()) {
        return true;
    }
    if (!isYule) {
        RealParameter p = deathToBirthRatioInput.get();
        if (p != null && p.somethingIsDirty()) {
            return true;
        }
        p = sampleProbabilityInput.get();
        if (p != null && p.somethingIsDirty()) {
            return true;
        }
    }
    return false;
}
Also used : RealParameter(beast.core.parameter.RealParameter)

Example 12 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class CalibratedBirthDeathModel method calculateTreeLogLikelihood.

@Override
public double calculateTreeLogLikelihood(final TreeInterface tree) {
    final double lam = birthRateInput.get().getArrayValue();
    double logL;
    if (isYule) {
        logL = calculateYuleLikelihood(tree, lam);
        logL += getCorrection(tree, lam, 0, 1);
    } else {
        final RealParameter db = deathToBirthRatioInput.get();
        final double a = db != null ? db.getArrayValue() : 0;
        assert (a < 1);
        final RealParameter sp = sampleProbabilityInput.get();
        final double rho = sp != null ? sp.getArrayValue() : 1;
        logL = calculateBDLikelihood(tree, lam, a, rho);
        logL += getCorrection(tree, lam, a, rho);
    }
    // logL += mar;
    return logL;
}
Also used : RealParameter(beast.core.parameter.RealParameter)

Example 13 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class TreeWithMetaDataLogger method toNewick.

String toNewick(Node node, List<Function> metadataList, BranchRateModel.Base branchRateModel) {
    StringBuffer buf = new StringBuffer();
    if (node.getLeft() != null) {
        buf.append("(");
        buf.append(toNewick(node.getLeft(), metadataList, branchRateModel));
        if (node.getRight() != null) {
            buf.append(',');
            buf.append(toNewick(node.getRight(), metadataList, branchRateModel));
        }
        buf.append(")");
    } else {
        buf.append(node.labelNr + 1);
    }
    if (someMetaDataNeedsLogging) {
        buf.append("[&");
        if (metadataList.size() > 0) {
            for (Function metadata : metadataList) {
                buf.append(((BEASTObject) metadata).getID());
                buf.append('=');
                if (metadata instanceof Parameter<?>) {
                    Parameter<?> p = (Parameter<?>) metadata;
                    int dim = p.getMinorDimension1();
                    if (dim > 1) {
                        buf.append('{');
                        for (int i = 0; i < dim; i++) {
                            if (metadata instanceof RealParameter) {
                                RealParameter rp = (RealParameter) metadata;
                                appendDouble(buf, rp.getMatrixValue(node.labelNr, i));
                            } else {
                                buf.append(p.getMatrixValue(node.labelNr, i));
                            }
                            if (i < dim - 1) {
                                buf.append(',');
                            }
                        }
                        buf.append('}');
                    } else {
                        if (metadata instanceof RealParameter) {
                            RealParameter rp = (RealParameter) metadata;
                            appendDouble(buf, rp.getArrayValue(node.labelNr));
                        } else {
                            buf.append(metadata.getArrayValue(node.labelNr));
                        }
                    }
                } else {
                    buf.append(metadata.getArrayValue(node.labelNr));
                }
                if (metadataList.indexOf(metadata) < metadataList.size() - 1) {
                    buf.append(",");
                }
            }
            if (branchRateModel != null) {
                buf.append(",");
            }
        }
        if (branchRateModel != null) {
            buf.append("rate=");
            appendDouble(buf, branchRateModel.getRateForBranch(node));
        }
        buf.append(']');
    }
    buf.append(":");
    if (substitutions) {
        appendDouble(buf, node.getLength() * branchRateModel.getRateForBranch(node));
    } else {
        appendDouble(buf, node.getLength());
    }
    return buf.toString();
}
Also used : Function(beast.core.Function) Parameter(beast.core.parameter.Parameter) RealParameter(beast.core.parameter.RealParameter) RealParameter(beast.core.parameter.RealParameter)

Example 14 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class DeltaExchangeOperator method proposal.

@Override
public final double proposal() {
    int[] parameterWeights = weights();
    final int dim = parameterWeights.length;
    // Find the number of weights that are nonzero
    int nonZeroWeights = 0;
    for (int i : parameterWeights) {
        if (i != 0) {
            ++nonZeroWeights;
        }
    }
    if (nonZeroWeights <= 1) {
        // it is impossible to select two distinct entries in this case, so there is nothing to propose
        return 0.0;
    }
    // Generate indices for the values to be modified
    int dim1 = Randomizer.nextInt(nonZeroWeights);
    int dim2 = Randomizer.nextInt(nonZeroWeights - 1);
    if (dim2 >= dim1) {
        ++dim2;
    }
    if (nonZeroWeights < dim) {
        // There are zero weights, so we need to increase dim1 and dim2 accordingly.
        int nonZerosBeforeDim1 = dim1;
        int nonZerosBeforeDim2 = dim2;
        dim1 = 0;
        dim2 = 0;
        while (nonZerosBeforeDim1 > 0 | parameterWeights[dim1] == 0) {
            if (parameterWeights[dim1] != 0) {
                --nonZerosBeforeDim1;
            }
            ++dim1;
        }
        while (nonZerosBeforeDim2 > 0 | parameterWeights[dim2] == 0) {
            if (parameterWeights[dim2] != 0) {
                --nonZerosBeforeDim2;
            }
            ++dim2;
        }
    }
    double logq = 0.0;
    if (compoundParameter == null) {
        // one parameter case
        // get two dimensions
        RealParameter realparameter = null;
        IntegerParameter intparameter = null;
        if (parameterInput.get().isEmpty()) {
            intparameter = intparameterInput.get().get(0);
        } else {
            realparameter = parameterInput.get().get(0);
        }
        if (intparameter == null) {
            // operate on real parameter
            double scalar1 = realparameter.getValue(dim1);
            double scalar2 = realparameter.getValue(dim2);
            if (isIntegerOperator) {
                final int d = Randomizer.nextInt((int) Math.round(delta)) + 1;
                if (parameterWeights[dim1] != parameterWeights[dim2])
                    throw new RuntimeException();
                scalar1 = Math.round(scalar1 - d);
                scalar2 = Math.round(scalar2 + d);
            } else {
                // exchange a random delta
                final double d = Randomizer.nextDouble() * delta;
                scalar1 -= d;
                if (parameterWeights[dim1] != parameterWeights[dim2]) {
                    scalar2 += d * parameterWeights[dim1] / parameterWeights[dim2];
                } else {
                    scalar2 += d;
                }
            }
            if (scalar1 < realparameter.getLower() || scalar1 > realparameter.getUpper() || scalar2 < realparameter.getLower() || scalar2 > realparameter.getUpper()) {
                logq = Double.NEGATIVE_INFINITY;
            } else {
                realparameter.setValue(dim1, scalar1);
                realparameter.setValue(dim2, scalar2);
            }
        } else {
            // operate on int parameter
            int scalar1 = intparameter.getValue(dim1);
            int scalar2 = intparameter.getValue(dim2);
            final int d = Randomizer.nextInt((int) Math.round(delta)) + 1;
            if (parameterWeights[dim1] != parameterWeights[dim2])
                throw new RuntimeException();
            scalar1 = Math.round(scalar1 - d);
            scalar2 = Math.round(scalar2 + d);
            if (scalar1 < intparameter.getLower() || scalar1 > intparameter.getUpper() || scalar2 < intparameter.getLower() || scalar2 > intparameter.getUpper()) {
                logq = Double.NEGATIVE_INFINITY;
            } else {
                intparameter.setValue(dim1, scalar1);
                intparameter.setValue(dim2, scalar2);
            }
        }
    } else {
        if (intparameterInput.get().isEmpty()) {
            // operate on real parameter
            double scalar1 = (Double) compoundParameter.getValue(dim1);
            double scalar2 = (Double) compoundParameter.getValue(dim2);
            if (isIntegerOperator) {
                final int d = Randomizer.nextInt((int) Math.round(delta)) + 1;
                if (parameterWeights[dim1] != parameterWeights[dim2])
                    throw new RuntimeException();
                scalar1 = Math.round(scalar1 - d);
                scalar2 = Math.round(scalar2 + d);
            } else {
                // exchange a random delta
                final double d = Randomizer.nextDouble() * delta;
                scalar1 -= d;
                if (parameterWeights[dim1] != parameterWeights[dim2]) {
                    scalar2 += d * parameterWeights[dim1] / parameterWeights[dim2];
                } else {
                    scalar2 += d;
                }
            }
            if (scalar1 < (Double) compoundParameter.getLower(dim1) || scalar1 > (Double) compoundParameter.getUpper(dim1) || scalar2 < (Double) compoundParameter.getLower(dim2) || scalar2 > (Double) compoundParameter.getUpper(dim2)) {
                logq = Double.NEGATIVE_INFINITY;
            } else {
                compoundParameter.setValue(dim1, scalar1);
                compoundParameter.setValue(dim2, scalar2);
            }
        } else {
            // operate on int parameter
            int scalar1 = (Integer) compoundParameter.getValue(dim1);
            int scalar2 = (Integer) compoundParameter.getValue(dim2);
            final int d = Randomizer.nextInt((int) Math.round(delta)) + 1;
            if (parameterWeights[dim1] != parameterWeights[dim2])
                throw new RuntimeException();
            scalar1 = Math.round(scalar1 - d);
            scalar2 = Math.round(scalar2 + d);
            if (scalar1 < (Integer) compoundParameter.getLower(dim1) || scalar1 > (Integer) compoundParameter.getUpper(dim1) || scalar2 < (Integer) compoundParameter.getLower(dim2) || scalar2 > (Integer) compoundParameter.getUpper(dim2)) {
                logq = Double.NEGATIVE_INFINITY;
            } else {
                compoundParameter.setValue(dim1, scalar1);
                compoundParameter.setValue(dim2, scalar2);
            }
        }
    }
    // symmetrical move so return a zero hasting ratio
    return logq;
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) RealParameter(beast.core.parameter.RealParameter)

Example 15 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class RealRandomWalkOperator method proposal.

/**
 * override this for proposals,
 * returns log of hastingRatio, or Double.NEGATIVE_INFINITY if proposal should not be accepted *
 */
@Override
public double proposal() {
    RealParameter param = parameterInput.get(this);
    int i = Randomizer.nextInt(param.getDimension());
    double value = param.getValue(i);
    double newValue = value;
    if (useGaussian) {
        newValue += Randomizer.nextGaussian() * windowSize;
    } else {
        newValue += Randomizer.nextDouble() * 2 * windowSize - windowSize;
    }
    if (newValue < param.getLower() || newValue > param.getUpper()) {
        return Double.NEGATIVE_INFINITY;
    }
    if (newValue == value) {
        // this saves calculating the posterior
        return Double.NEGATIVE_INFINITY;
    }
    param.setValue(i, newValue);
    return 0.0;
}
Also used : RealParameter(beast.core.parameter.RealParameter)

Aggregations

RealParameter (beast.core.parameter.RealParameter)97 Test (org.junit.Test)39 IntegerParameter (beast.core.parameter.IntegerParameter)23 Tree (beast.evolution.tree.Tree)16 State (beast.core.State)14 SCMigrationModel (beast.evolution.tree.SCMigrationModel)13 Alignment (beast.evolution.alignment.Alignment)11 TypeSet (beast.evolution.tree.TypeSet)11 MCMC (beast.core.MCMC)10 SiteModel (beast.evolution.sitemodel.SiteModel)10 Frequencies (beast.evolution.substitutionmodel.Frequencies)10 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)10 StructuredCoalescentTreeDensity (multitypetree.distributions.StructuredCoalescentTreeDensity)10 TaxonSet (beast.evolution.alignment.TaxonSet)9 MultiTypeTreeStatLogger (multitypetree.util.MultiTypeTreeStatLogger)9 MultiTypeTreeFromNewick (beast.evolution.tree.MultiTypeTreeFromNewick)8 Node (beast.evolution.tree.Node)8 Operator (beast.core.Operator)7 RandomTree (beast.evolution.tree.RandomTree)7 Locus (bacter.Locus)6