Search in sources :

Example 1 with IntegerParameter

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

the class ClockModelListInputEditor method setUpOperator.

/**
 * set up relative weights and parameter input *
 */
private void setUpOperator() {
    String weights = "";
    List<RealParameter> parameters = operator.parameterInput.get();
    parameters.clear();
    double commonClockRate = -1;
    boolean isAllClocksAreEqual = true;
    try {
        for (int i = 0; i < doc.alignments.size(); i++) {
            Alignment data = doc.alignments.get(i);
            int weight = data.getSiteCount();
            BranchRateModel.Base clockModel = (BranchRateModel.Base) doc.clockModels.get(i);
            RealParameter clockRate = clockModel.meanRateInput.get();
            // clockRate.m_bIsEstimated.setValue(true, clockRate);
            if (clockRate.isEstimatedInput.get()) {
                if (commonClockRate < 0) {
                    commonClockRate = clockRate.valuesInput.get().get(0);
                } else {
                    if (Math.abs(commonClockRate - clockRate.valuesInput.get().get(0)) > 1e-10) {
                        isAllClocksAreEqual = false;
                    }
                }
                weights += weight + " ";
                parameters.add(clockRate);
            }
        // doc.autoSetClockRate = false;
        }
        if (!fixMeanRatesCheckBox.isSelected()) {
            fixMeanRatesValidateLabel.setVisible(false);
            return;
        }
        if (parameters.size() == 0) {
            fixMeanRatesValidateLabel.setVisible(true);
            fixMeanRatesValidateLabel.m_circleColor = Color.red;
            fixMeanRatesValidateLabel.setToolTipText("The model is invalid: At least one clock rate should be estimated.");
            return;
        }
        IntegerParameter weightParameter = new IntegerParameter(weights);
        weightParameter.setID("weightparameter");
        weightParameter.isEstimatedInput.setValue(false, weightParameter);
        operator.parameterWeightsInput.setValue(weightParameter, operator);
        if (!isAllClocksAreEqual) {
            fixMeanRatesValidateLabel.setVisible(true);
            fixMeanRatesValidateLabel.m_circleColor = Color.orange;
            fixMeanRatesValidateLabel.setToolTipText("Not all clocks are equal. Are you sure this is what you want?");
        } else if (parameters.size() == 1) {
            fixMeanRatesValidateLabel.setVisible(true);
            fixMeanRatesValidateLabel.m_circleColor = Color.orange;
            fixMeanRatesValidateLabel.setToolTipText("At least 2 clock models should have their rate estimated");
        } else if (parameters.size() < doc.alignments.size()) {
            fixMeanRatesValidateLabel.setVisible(true);
            fixMeanRatesValidateLabel.m_circleColor = Color.orange;
            fixMeanRatesValidateLabel.setToolTipText("Not all partitions have their rate estimated");
        } else {
            fixMeanRatesValidateLabel.setVisible(false);
        }
        repaint();
    // doc.autoSetClockRate = true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) Alignment(beast.evolution.alignment.Alignment) BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) RealParameter(beast.core.parameter.RealParameter)

Example 2 with IntegerParameter

use of beast.core.parameter.IntegerParameter 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 3 with IntegerParameter

use of beast.core.parameter.IntegerParameter 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);
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) RealParameter(beast.core.parameter.RealParameter) Sum(beast.core.util.Sum) BooleanParameter(beast.core.parameter.BooleanParameter) Test(org.junit.Test)

Example 4 with IntegerParameter

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

the class UniformIntegerOperatorTest method testIntegerParameterBound.

public void testIntegerParameterBound() {
    try {
        // 4 vaules {0, 1, 2, 3}
        count = new int[dimension][4];
        IntegerParameter parameter = new IntegerParameter(new Integer[] { 1, 0, 3 });
        parameter.setLower(0);
        parameter.setUpper(3);
        State state = new State();
        state.initByName("stateNode", parameter);
        state.initialise();
        UniformOperator uniformOperator = new UniformOperator();
        uniformOperator.initByName("parameter", parameter, "howMany", 3, "weight", 1.0);
        for (int i = 0; i < 400; i++) {
            uniformOperator.proposal();
            Integer[] values = parameter.getValues();
            for (int k = 0; k < values.length; k++) {
                int j = values[k];
                count[k][j] += 1;
            }
        }
        System.out.println("Integer distributions lower = 0, upper = 3");
        for (int j = 0; j < count.length; j++) {
            System.out.println("x[" + j + "] = " + Arrays.toString(count[j]));
        }
        assertTrue("Expected count[0][0-3] > 0", count[0][0] > 0 && count[0][1] > 0 && count[0][2] > 0 && count[0][3] > 0);
        assertTrue("Expected count[1][0-3] > 0", count[1][0] > 0 && count[1][1] > 0 && count[1][2] > 0 && count[1][3] > 0);
        assertTrue("Expected count[2][0-3] > 0", count[2][0] > 0 && count[2][1] > 0 && count[2][2] > 0 && count[2][3] > 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) State(beast.core.State) UniformOperator(beast.evolution.operators.UniformOperator)

Example 5 with IntegerParameter

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

the class SiteModelInputEditor method customConnector.

public static boolean customConnector(BeautiDoc doc) {
    try {
        DeltaExchangeOperator operator = (DeltaExchangeOperator) doc.pluginmap.get("FixMeanMutationRatesOperator");
        if (operator == null) {
            return false;
        }
        List<RealParameter> parameters = operator.parameterInput.get();
        parameters.clear();
        // String weights = "";
        CompoundDistribution likelihood = (CompoundDistribution) doc.pluginmap.get("likelihood");
        boolean hasOneEstimatedRate = false;
        List<String> rateIDs = new ArrayList<>();
        List<Integer> weights = new ArrayList<>();
        for (Distribution d : likelihood.pDistributions.get()) {
            GenericTreeLikelihood treelikelihood = (GenericTreeLikelihood) d;
            Alignment data = treelikelihood.dataInput.get();
            int weight = data.getSiteCount();
            if (data.isAscertained) {
                weight -= data.getExcludedPatternCount();
            }
            if (treelikelihood.siteModelInput.get() instanceof SiteModel) {
                SiteModel siteModel = (SiteModel) treelikelihood.siteModelInput.get();
                RealParameter mutationRate = siteModel.muParameterInput.get();
                // clockRate.m_bIsEstimated.setValue(true, clockRate);
                if (mutationRate.isEstimatedInput.get()) {
                    hasOneEstimatedRate = true;
                    if (rateIDs.indexOf(mutationRate.getID()) == -1) {
                        parameters.add(mutationRate);
                        weights.add(weight);
                        rateIDs.add(mutationRate.getID());
                    } else {
                        int k = rateIDs.indexOf(mutationRate.getID());
                        weights.set(k, weights.get(k) + weight);
                    }
                }
            }
        }
        IntegerParameter weightParameter;
        if (weights.size() == 0) {
            weightParameter = new IntegerParameter();
        } else {
            String weightString = "";
            for (int k : weights) {
                weightString += k + " ";
            }
            weightParameter = new IntegerParameter(weightString);
            weightParameter.setID("weightparameter");
        }
        weightParameter.isEstimatedInput.setValue(false, weightParameter);
        operator.parameterWeightsInput.setValue(weightParameter, operator);
        return hasOneEstimatedRate;
    } catch (Exception e) {
    }
    return false;
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) ArrayList(java.util.ArrayList) RealParameter(beast.core.parameter.RealParameter) SiteModel(beast.evolution.sitemodel.SiteModel) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompoundDistribution(beast.core.util.CompoundDistribution) Alignment(beast.evolution.alignment.Alignment) CompoundDistribution(beast.core.util.CompoundDistribution) DeltaExchangeOperator(beast.evolution.operators.DeltaExchangeOperator)

Aggregations

IntegerParameter (beast.core.parameter.IntegerParameter)31 RealParameter (beast.core.parameter.RealParameter)23 Test (org.junit.Test)13 State (beast.core.State)6 SCMigrationModel (beast.evolution.tree.SCMigrationModel)4 TypeSet (beast.evolution.tree.TypeSet)4 PrintStream (java.io.PrintStream)4 CFEventList (bacter.CFEventList)3 Function (beast.core.Function)3 MCMC (beast.core.MCMC)3 Operator (beast.core.Operator)3 StructuredCoalescentTreeDensity (multitypetree.distributions.StructuredCoalescentTreeDensity)3 MultiTypeTreeStatLogger (multitypetree.util.MultiTypeTreeStatLogger)3 ConversionGraph (bacter.ConversionGraph)2 BooleanParameter (beast.core.parameter.BooleanParameter)2 Alignment (beast.evolution.alignment.Alignment)2 DeltaExchangeOperator (beast.evolution.operators.DeltaExchangeOperator)2 MultiTypeTreeFromNewick (beast.evolution.tree.MultiTypeTreeFromNewick)2 BEASTObjectDialog (beast.app.draw.BEASTObjectDialog)1 InputEditor (beast.app.draw.InputEditor)1