Debian Codegraph @

src/dr/inference/operators/SliceOperator.java

raw symbols SCIP
1 /*
2 * SliceOperator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.operators;
27
28 import dr.inference.distribution.DistributionLikelihood;
29 import dr.inference.distribution.NormalDistributionModel;
30 import dr.inference.distribution.ParametricDistributionModel;
31 import dr.inference.model.CompoundLikelihood;
32 import dr.inference.model.Likelihood;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Variable;
35 import dr.math.MathUtils;
36 import dr.math.distributions.NormalDistribution;
37 import dr.util.Attribute;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
42 /**
43 * Implements a generic univariate slice sampler.
44 *
45 * See: RM Neal (2003) Slice Sampling, Annals of Statistics, 31, 705-767 (with discussion)
46 *
47 * @author Marc A. Suchard
48 */
49 public class SliceOperator extends SimpleMetropolizedGibbsOperator {
50
51 public SliceOperator(Variable<Double> variable) {
52 this(new SliceInterval.SteppingOut(), variable);
53 }
54
55 public SliceOperator(SliceInterval sliceInterval, Variable<Double> variable) {
56 this.sliceInterval = sliceInterval;
57
58 if (variable.getSize() != 1) {
59 throw new RuntimeException("Generic slice sampler is currently for univariate parameters only");
60 }
61 this.variable = variable;
62 sliceInterval.setSliceSampler(this);
63 }
64
65 public Variable<Double> getVariable() {
66 return variable;
67 }
68
69 public double doOperation(Likelihood likelihood) {
70 double logPosterior = evaluate(likelihood, 1.0);
71 double cutoffDensity = logPosterior + MathUtils.randomLogDouble();
72 sliceInterval.drawFromInterval(likelihood, cutoffDensity, width);
73 // No need to set variable, as SliceInterval has already done this (and recomputed posterior)
74 return 0;
75 }
76
77 public int getStepCount() {
78 return 1;
79 }
80
81 public String getOperatorName() {
82 return "genericSliceSampler";
83 }
84
85 public static void main(String[] arg) {
86
87 // Define normal model
88 Parameter meanParameter = new Parameter.Default(1.0); // Starting value
89 Variable<Double> stdev = new Variable.D(1.0, 1); // Fixed value
90 ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, stdev);
91 DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);
92
93 // Define prior
94 DistributionLikelihood prior = new DistributionLikelihood(new NormalDistribution(0.0, 1.0)); // Hyper-priors
95 prior.addData(meanParameter);
96
97 // Define data
98 likelihood.addData(new Attribute.Default<double[]>("Data", new double[] {0.0, 1.0, 2.0}));
99
100 List<Likelihood> list = new ArrayList<Likelihood>();
101 list.add(likelihood);
102 list.add(prior);
103 CompoundLikelihood posterior = new CompoundLikelihood(0, list);
104 SliceOperator sliceSampler = new SliceOperator(meanParameter);
105
106 final int length = 10000;
107 double mean = 0;
108 double variance = 0;
109
110 for(int i = 0; i < length; i++) {
111 sliceSampler.doOperation(posterior);
112 double x = meanParameter.getValue(0);
113 mean += x;
114 variance += x*x;
115 }
116 mean /= length;
117 variance /= length;
118 variance -= mean*mean;
119 System.out.println("E(x) = "+mean);
120 System.out.println("V(x) = "+variance);
121 }
122
123 private final SliceInterval sliceInterval;
124 private final double width = 1.0;
125 private final Variable<Double> variable;
126 }