Search in sources :

Example 6 with Function

use of beast.core.Function in project MultiTypeTree by tgvaughan.

the class ExcludablePrior method calculateLogP.

@Override
public double calculateLogP() {
    Function x = m_x.get();
    if (x instanceof RealParameter || x instanceof IntegerParameter) {
        // test that parameter is inside its bounds
        double l = 0.0;
        double h = 0.0;
        if (x instanceof RealParameter) {
            l = ((RealParameter) x).getLower();
            h = ((RealParameter) x).getUpper();
        } else {
            l = ((IntegerParameter) x).getLower();
            h = ((IntegerParameter) x).getUpper();
        }
        for (int i = 0; i < x.getDimension(); i++) {
            if (!xIncludeInput.get().getValue(i))
                continue;
            double value = x.getArrayValue(i);
            if (value < l || value > h) {
                return Double.NEGATIVE_INFINITY;
            }
        }
    }
    // Inline modified version of ParametricDistribution.calcLogP()
    final double fOffset = distInput.get().offsetInput.get();
    logP = 0;
    for (int i = 0; i < x.getDimension(); i++) {
        if (!xIncludeInput.get().getValue(i))
            continue;
        final double fX = x.getArrayValue(i) - fOffset;
        logP += distInput.get().logDensity(fX);
    }
    return logP;
}
Also used : Function(beast.core.Function) IntegerParameter(beast.core.parameter.IntegerParameter) RealParameter(beast.core.parameter.RealParameter)

Example 7 with Function

use of beast.core.Function in project beast2 by CompEvol.

the class BeautiBase method assertParameterCountInPriorIs.

void assertParameterCountInPriorIs(int i) {
    // count nr of parameters in Prior objects in prior
    // including those for prior distributions (Normal, etc)
    // useful to make sure they do (or do not) get linked
    Set<Function> parameters = new LinkedHashSet<>();
    CompoundDistribution prior = (CompoundDistribution) doc.pluginmap.get("prior");
    for (Distribution p : prior.pDistributions.get()) {
        if (p instanceof Prior) {
            Prior p2 = (Prior) p;
            parameters.add(p2.m_x.get());
            for (BEASTInterface o : p2.distInput.get().listActiveBEASTObjects()) {
                if (o instanceof Parameter) {
                    parameters.add((Parameter<?>) o);
                }
            }
        }
    }
    System.err.println("Number of parameters in prior = " + parameters.size());
    if (i >= 0) {
        assertThat(parameters.size()).as("Expected " + i + " parameters in prior").isEqualTo(i);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CompoundDistribution(beast.core.util.CompoundDistribution) Function(beast.core.Function) Prior(beast.math.distributions.Prior) CompoundDistribution(beast.core.util.CompoundDistribution) Distribution(beast.core.Distribution) Parameter(beast.core.parameter.Parameter) BEASTInterface(beast.core.BEASTInterface)

Example 8 with Function

use of beast.core.Function in project beast2 by CompEvol.

the class SpeciesTreeLogger method toNewick.

String toNewick(final Node node, final Function metadata, final Function metadataTop, List<Function> metadataList) {
    final StringBuilder buf = new StringBuilder();
    if (node.getLeft() != null) {
        buf.append("(");
        buf.append(toNewick(node.getLeft(), metadata, metadataTop, metadataList));
        if (node.getRight() != null) {
            buf.append(',');
            buf.append(toNewick(node.getRight(), metadata, metadataTop, metadataList));
        }
        buf.append(")");
    } else {
        buf.append(node.getNr() + Tree.taxaTranslationOffset);
    }
    buf.append("[&");
    switch(popSizeFunction) {
        case constant:
            {
                final double popStart = metadata.getArrayValue(node.getNr());
                buf.append(dmv + "=").append(popStart);
                break;
            }
        case linear:
        case linear_with_constant_root:
            buf.append(dmt + "=");
            final double b;
            if (node.isRoot()) {
                b = treeTopFinderInput.get().getHighestTreeHeight() - node.getHeight();
            } else {
                b = node.getLength();
            }
            buf.append(b).append("," + dmv + "={");
            final double popStart;
            if (node.isLeaf()) {
                popStart = metadata.getArrayValue(node.getNr());
            } else {
                popStart = (getMetaDataTopValue(node.getLeft(), metadataTop) + getMetaDataTopValue(node.getRight(), metadataTop));
            }
            buf.append(popStart);
            final double popEnd;
            if (node.isRoot() && popSizeFunction == TreePopSizeFunction.linear_with_constant_root) {
                popEnd = popStart;
            } else {
                popEnd = getMetaDataTopValue(node, metadataTop);
            }
            buf.append(",").append(popEnd).append("}");
            break;
    }
    if (metadataList.size() > 0) {
        for (Function metadata2 : metadataList) {
            if (metadataList.indexOf(metadata2) > 0 || buf.length() > 1) {
                buf.append(",");
            }
            buf.append(((BEASTObject) metadata2).getID());
            buf.append('=');
            if (metadata2 instanceof Parameter<?>) {
                Parameter<?> p = (Parameter<?>) metadata2;
                int dim = p.getMinorDimension1();
                if (dim > 1) {
                    buf.append('{');
                    for (int i = 0; i < dim; i++) {
                        buf.append(p.getMatrixValue(node.getNr(), i));
                        if (i < dim - 1) {
                            buf.append(',');
                        }
                    }
                    buf.append('}');
                } else {
                    buf.append(metadata2.getArrayValue(node.getNr()));
                }
            } else {
                buf.append(metadata2.getArrayValue(node.getNr()));
            }
        }
    }
    buf.append(']');
    if (!node.isRoot()) {
        buf.append(":").append(node.getLength());
    }
    return buf.toString();
}
Also used : TreePopSizeFunction(beast.evolution.speciation.SpeciesTreePrior.TreePopSizeFunction) Function(beast.core.Function) Parameter(beast.core.parameter.Parameter)

Example 9 with Function

use of beast.core.Function in project beast2 by CompEvol.

the class Sum method initAndValidate.

@Override
public void initAndValidate() {
    List<Function> valuable = functionInput.get();
    mode = Mode.integer_mode;
    for (Function v : valuable) {
        if (!(v instanceof IntegerParameter || v instanceof BooleanParameter)) {
            mode = Mode.double_mode;
        }
    }
}
Also used : Function(beast.core.Function) IntegerParameter(beast.core.parameter.IntegerParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 10 with Function

use of beast.core.Function in project beast2 by CompEvol.

the class Sum method compute.

/**
 * do the actual work, and reset flag *
 */
void compute() {
    sum = 0;
    for (Function v : functionInput.get()) {
        for (int i = 0; i < v.getDimension(); i++) {
            sum += v.getArrayValue(i);
        }
    }
    needsRecompute = false;
}
Also used : Function(beast.core.Function)

Aggregations

Function (beast.core.Function)10 IntegerParameter (beast.core.parameter.IntegerParameter)3 Parameter (beast.core.parameter.Parameter)3 RealParameter (beast.core.parameter.RealParameter)3 BEASTObject (beast.core.BEASTObject)2 StateNode (beast.core.StateNode)2 TreePopSizeFunction (beast.evolution.speciation.SpeciesTreePrior.TreePopSizeFunction)2 BEASTInterface (beast.core.BEASTInterface)1 Distribution (beast.core.Distribution)1 BooleanParameter (beast.core.parameter.BooleanParameter)1 CompoundDistribution (beast.core.util.CompoundDistribution)1 Tree (beast.evolution.tree.Tree)1 Prior (beast.math.distributions.Prior)1 LinkedHashSet (java.util.LinkedHashSet)1