use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class LatentFactorModelPrecisionGibbsOperatorParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String weightTemp = (String) xo.getAttribute(WEIGHT);
double weight = Double.parseDouble(weightTemp);
LatentFactorModel LFM = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
DistributionLikelihood prior = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
boolean randomScan = xo.getAttribute(RANDOM_SCAN, true);
return new LatentFactorModelPrecisionGibbsOperator(LFM, prior, weight, randomScan);
}
use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class LoadingsGibbsOperatorParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String weightTemp = (String) xo.getAttribute(WEIGHT);
double weight = Double.parseDouble(weightTemp);
LatentFactorModel LFM = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
DistributionLikelihood prior = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
MomentDistributionModel prior2 = (MomentDistributionModel) xo.getChild(MomentDistributionModel.class);
DistributionLikelihood cutoffPrior = null;
if (xo.hasChildNamed(CUTOFF_PRIOR)) {
cutoffPrior = (DistributionLikelihood) xo.getChild(CUTOFF_PRIOR).getChild(DistributionLikelihood.class);
}
boolean randomScan = xo.getAttribute(RANDOM_SCAN, true);
int numThreads = xo.getAttribute(NUM_THREADS, 4);
MatrixParameterInterface loadings = null;
if (xo.getChild(MatrixParameterInterface.class) != null) {
loadings = (MatrixParameterInterface) xo.getChild(MatrixParameterInterface.class);
}
DistributionLikelihood WorkingPrior = null;
if (xo.getChild(WORKING_PRIOR) != null) {
System.out.println("here");
WorkingPrior = (DistributionLikelihood) xo.getChild(WORKING_PRIOR).getChild(DistributionLikelihood.class);
}
boolean multiThreaded = xo.getAttribute(MULTI_THREADED, false);
if (prior != null)
//To change body of implemented methods use File | Settings | File Templates.
return new LoadingsGibbsOperator(LFM, prior, weight, randomScan, WorkingPrior, multiThreaded, numThreads);
else
return new LoadingsGibbsTruncatedOperator(LFM, prior2, weight, randomScan, loadings, cutoffPrior);
}
use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class LoadingsIndependenceOperatorParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
CoercionMode mode = CoercionMode.parseMode(xo);
String scaleFactorTemp = (String) xo.getAttribute(SCALE_FACTOR);
double scaleFactor = Double.parseDouble(scaleFactorTemp);
String weightTemp = (String) xo.getAttribute(WEIGHT);
double weight = Double.parseDouble(weightTemp);
LatentFactorModel LFM = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
DistributionLikelihood prior = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
boolean randomScan = xo.getAttribute(RANDOM_SCAN, true);
//To change body of implemented methods use File | Settings | File Templates.
return new LoadingsIndependenceOperator(LFM, prior, weight, randomScan, scaleFactor, mode);
}
use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class EllipticalSliceOperator method main.
public static void main(String[] arg) {
// Define normal model
// Starting values
Parameter thetaParameter = new Parameter.Default(new double[] { 1.0, 0.0 });
MaskedParameter meanParameter = new MaskedParameter(thetaParameter, new Parameter.Default(new double[] { 1.0, 0.0 }), true);
TransformedParameter precParameter = new TransformedParameter(new MaskedParameter(thetaParameter, new Parameter.Default(new double[] { 0.0, 1.0 }), true), new Transform.LogTransform(), true);
// System.err.println(thetaParameter);
// System.err.println(meanParameter);
// System.err.println(precParameter);
ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, precParameter, true);
DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);
// Define prior
MultivariateNormalDistribution priorDistribution = new MultivariateNormalDistribution(new double[] { 0.0, 0.0 }, new double[][] { { 0.001, 0.0 }, { 0.0, 0.001 } });
MultivariateDistributionLikelihood prior = new MultivariateDistributionLikelihood(priorDistribution);
prior.addData(thetaParameter);
// Define data
// likelihood.addData(new Attribute.Default<double[]>("Data", new double[] {0.0, 2.0, 4.0}));
likelihood.addData(new Attribute.Default<double[]>("Data", new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }));
List<Likelihood> list = new ArrayList<Likelihood>();
list.add(likelihood);
list.add(prior);
CompoundLikelihood posterior = new CompoundLikelihood(0, list);
EllipticalSliceOperator sliceSampler = new EllipticalSliceOperator(thetaParameter, priorDistribution, false, true);
final int dim = thetaParameter.getDimension();
final int length = 100000;
double[] mean = new double[dim];
double[] variance = new double[dim];
Parameter[] log = new Parameter[dim];
log[0] = meanParameter;
log[1] = precParameter;
for (int i = 0; i < length; i++) {
sliceSampler.doOperation(posterior);
for (int j = 0; j < dim; ++j) {
double x = log[j].getValue(0);
mean[j] += x;
variance[j] += x * x;
}
}
for (int j = 0; j < dim; ++j) {
mean[j] /= length;
variance[j] /= length;
variance[j] -= mean[j] * mean[j];
}
System.out.println("E(x)\tStErr(x)");
for (int j = 0; j < dim; ++j) {
System.out.println(mean[j] + " " + Math.sqrt(variance[j]));
}
}
use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class SliceOperator method main.
public static void main(String[] arg) {
// Define normal model
// Starting value
Parameter meanParameter = new Parameter.Default(1.0);
// Fixed value
Variable<Double> stdev = new Variable.D(1.0, 1);
ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, stdev);
DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);
// Define prior
// Hyper-priors
DistributionLikelihood prior = new DistributionLikelihood(new NormalDistribution(0.0, 1.0));
prior.addData(meanParameter);
// Define data
likelihood.addData(new Attribute.Default<double[]>("Data", new double[] { 0.0, 1.0, 2.0 }));
List<Likelihood> list = new ArrayList<Likelihood>();
list.add(likelihood);
list.add(prior);
CompoundLikelihood posterior = new CompoundLikelihood(0, list);
SliceOperator sliceSampler = new SliceOperator(meanParameter);
final int length = 10000;
double mean = 0;
double variance = 0;
for (int i = 0; i < length; i++) {
sliceSampler.doOperation(posterior);
double x = meanParameter.getValue(0);
mean += x;
variance += x * x;
}
mean /= length;
variance /= length;
variance -= mean * mean;
System.out.println("E(x) = " + mean);
System.out.println("V(x) = " + variance);
}
Aggregations