use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class TreeClusterSequentialSampling method setVirusLocationAndOffsets.
private void setVirusLocationAndOffsets() {
//change the mu in the toBin and fromBIn
//borrow from getLogLikelihood:
double[] meanYear = new double[binSize];
double[] groupCount = new double[binSize];
for (int i = 0; i < numdata; i++) {
int label = (int) clusterLabels.getParameterValue(i);
double year = 0;
if (virusOffsetsParameter != null) {
// System.out.print("virus Offeset Parameter present"+ ": ");
// System.out.print( virusOffsetsParameter.getParameterValue(i) + " ");
// System.out.print(" drift= " + drift + " ");
//just want year[i]
year = virusOffsetsParameter.getParameterValue(i);
//make sure that it is equivalent to double offset = year[virusIndex] - firstYear;
} else {
System.out.println("virus Offeset Parameter NOT present. We expect one though. Something is wrong.");
}
meanYear[label] = meanYear[label] + year;
groupCount[label] = groupCount[label] + 1;
}
for (int i = 0; i < binSize; i++) {
if (groupCount[i] > 0) {
meanYear[i] = meanYear[i] / groupCount[i];
}
//System.out.println(meanYear[i]);
}
mu0_offset = new double[binSize];
//now, change the mu..
for (int i = 0; i < binSize; i++) {
//System.out.println(meanYear[i]*beta);
mu0_offset[i] = meanYear[i];
//System.out.println("group " + i + "\t" + mu0_offset[i]);
}
//virus in the same cluster has the same position
for (int i = 0; i < numdata; i++) {
int label = (int) clusterLabels.getParameterValue(i);
Parameter vLoc = virusLocations.getParameter(i);
//setting the virus locs to be equal to the corresponding mu
double muValue = mu.getParameter(label).getParameterValue(0);
vLoc.setParameterValue(0, muValue);
double muValue2 = mu.getParameter(label).getParameterValue(1);
vLoc.setParameterValue(1, muValue2);
//System.out.println("vloc="+ muValue + "," + muValue2);
}
for (int i = 0; i < numdata; i++) {
int label = (int) clusterLabels.getParameterValue(i);
//if we want to apply the mean year virus cluster offset to the cluster
if (clusterOffsetsParameter != null) {
//setting the clusterOffsets to be equal to the mean year of the virus cluster
// by doing this, the virus changes cluster AND updates the offset simultaneously
clusterOffsetsParameter.setParameterValue(i, mu0_offset[label]);
}
// System.out.println("mu0_offset[label]=" + mu0_offset[label]);
// System.out.println("clusterOffsets " + i +" now becomes =" + clusterOffsetsParameter.getParameterValue(i) );
}
// System.out.println("===The on nodes===");
// for(int i=0; i < binSize; i++){
// if((int) excisionPoints.getParameterValue(i) == 1){
// System.out.println("Cluster node " + i + " = " + (int) indicators.getParameterValue(i) + "\tstatus=" + (int) excisionPoints.getParameterValue(i));
// }
// }
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class BeagleTreeLikelihood method main.
public static void main(String[] args) {
try {
MathUtils.setSeed(666);
System.out.println("Test case 1: simulateOnePartition");
int sequenceLength = 1000;
ArrayList<Partition> partitionsList = new ArrayList<Partition>();
// create tree
NewickImporter importer = new NewickImporter("(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
Tree tree = importer.importTree(null);
TreeModel treeModel = new TreeModel(tree);
// create Frequency Model
Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25, 0.25, 0.25 });
FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE, freqs);
// create branch model
Parameter kappa1 = new Parameter.Default(1, 1);
Parameter kappa2 = new Parameter.Default(1, 1);
HKY hky1 = new HKY(kappa1, freqModel);
HKY hky2 = new HKY(kappa2, freqModel);
HomogeneousBranchModel homogenousBranchSubstitutionModel = new HomogeneousBranchModel(hky1);
List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
substitutionModels.add(hky1);
substitutionModels.add(hky2);
List<FrequencyModel> freqModels = new ArrayList<FrequencyModel>();
freqModels.add(freqModel);
Parameter epochTimes = new Parameter.Default(1, 20);
// create branch rate model
Parameter rate = new Parameter.Default(1, 0.001);
BranchRateModel branchRateModel = new StrictClockBranchRates(rate);
// create site model
GammaSiteRateModel siteRateModel = new GammaSiteRateModel("siteModel");
BranchModel homogeneousBranchModel = new HomogeneousBranchModel(hky1);
BranchModel epochBranchModel = new EpochBranchModel(treeModel, substitutionModels, epochTimes);
// create partition
Partition partition1 = new //
Partition(//
treeModel, //
homogenousBranchSubstitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
0, // to
sequenceLength - 1, // every
1);
partitionsList.add(partition1);
// feed to sequence simulator and generate data
BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(partitionsList);
Alignment alignment = simulator.simulate(false, false);
BeagleTreeLikelihood nbtl = new BeagleTreeLikelihood(alignment, treeModel, homogeneousBranchModel, siteRateModel, branchRateModel, null, false, PartialsRescalingScheme.DEFAULT, false);
System.out.println("nBTL(homogeneous) = " + nbtl.getLogLikelihood());
nbtl = new BeagleTreeLikelihood(alignment, treeModel, epochBranchModel, siteRateModel, branchRateModel, null, false, PartialsRescalingScheme.DEFAULT, false);
System.out.println("nBTL(epoch) = " + nbtl.getLogLikelihood());
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
// END: try-catch block
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class ConjugateRootTraitPrior method parseConjugateRootTraitPrior.
public static ConjugateRootTraitPrior parseConjugateRootTraitPrior(XMLObject xo, final int dim) throws XMLParseException {
XMLObject cxo = xo.getChild(CONJUGATE_ROOT_PRIOR);
Parameter meanParameter = (Parameter) cxo.getChild(MultivariateDistributionLikelihood.MVN_MEAN).getChild(Parameter.class);
if (meanParameter.getDimension() != dim) {
throw new XMLParseException("Root prior mean dimension does not match trait diffusion dimension");
}
Parameter sampleSizeParameter = (Parameter) cxo.getChild(PRIOR_SAMPLE_SIZE).getChild(Parameter.class);
return new ConjugateRootTraitPrior(meanParameter.getParameterValues(), sampleSizeParameter.getParameterValue(0));
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class BranchCategoriesParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter allocationParameter = (Parameter) xo.getElementFirstChild(ALLOCATION);
CountableBranchCategoryProvider cladeModel;
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
if (!xo.getAttribute(RANDOMIZE, true)) {
CountableBranchCategoryProvider.CladeBranchCategoryModel cm = new CountableBranchCategoryProvider.CladeBranchCategoryModel(treeModel, allocationParameter);
for (int i = 0; i < xo.getChildCount(); ++i) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject xoc = (XMLObject) xo.getChild(i);
if (xoc.getName().equals(LocalClockModelParser.CLADE)) {
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
boolean includeStem = xoc.getAttribute(LocalClockModelParser.INCLUDE_STEM, false);
boolean excludeClade = xoc.getAttribute(LocalClockModelParser.EXCLUDE_CLADE, false);
// XML index-start = 1 not 0
int rateCategory = xoc.getIntegerAttribute(CATEGORY) - 1;
try {
cm.setClade(taxonList, rateCategory, includeStem, excludeClade, false);
} catch (TreeUtils.MissingTaxonException e) {
throw new XMLParseException("Unable to find taxon for clade in countable mixture model: " + e.getMessage());
}
} else if (xoc.getName().equals(LocalClockModelParser.TRUNK)) {
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
boolean includeStem = xoc.getAttribute(LocalClockModelParser.INCLUDE_STEM, false);
boolean excludeClade = xoc.getAttribute(LocalClockModelParser.EXCLUDE_CLADE, false);
// XML index-start = 1 not 0
int rateCategory = xoc.getIntegerAttribute(CATEGORY) - 1;
try {
cm.setClade(taxonList, rateCategory, includeStem, excludeClade, true);
} catch (TreeUtils.MissingTaxonException e) {
throw new XMLParseException("Unable to find taxon for trunk in countable mixture model: " + e.getMessage());
}
}
}
}
cladeModel = cm;
} else {
CountableBranchCategoryProvider.IndependentBranchCategoryModel cm = new CountableBranchCategoryProvider.IndependentBranchCategoryModel(treeModel, allocationParameter);
cm.randomize();
cladeModel = cm;
}
return cladeModel;
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class HamiltonianMonteCarloOperatorParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
int nSteps = xo.getIntegerAttribute(N_STEPS);
double stepSize = xo.getDoubleAttribute(STEP_SIZE);
double drawVariance = xo.getDoubleAttribute(DRAW_VARIANCE);
int mode = xo.getAttribute(MODE, 0);
GradientWrtParameterProvider derivative = (GradientWrtParameterProvider) xo.getChild(GradientWrtParameterProvider.class);
Parameter parameter = (Parameter) xo.getChild(Parameter.class);
Transform transform = null;
if (parameter == null) {
Transform.Collection collection = (Transform.Collection) xo.getChild(Transform.Collection.class);
parameter = collection.getParameter();
transform = collection;
}
if (derivative.getDimension() != parameter.getDimension()) {
throw new XMLParseException("Gradient (" + derivative.getDimension() + ") must be the same dimensions as the parameter (" + parameter.getDimension() + ")");
}
if (mode == 0) {
return new HamiltonianMonteCarloOperator(CoercionMode.DEFAULT, weight, derivative, parameter, transform, stepSize, nSteps, drawVariance);
} else {
return new NoUTurnOperator(CoercionMode.DEFAULT, weight, derivative, parameter, stepSize, nSteps, drawVariance);
}
}
Aggregations