Debian Codegraph @

src/dr/inference/tutorial/Tutorial1.java

raw symbols SCIP
1 /*
2 * Tutorial1.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.tutorial;
27
28 import dr.inference.distribution.DistributionLikelihood;
29 import dr.inference.distribution.NormalDistributionModel;
30 import dr.inference.loggers.Logger;
31 import dr.inference.loggers.MCLogger;
32 import dr.inference.mcmc.MCMC;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Variable;
35 import dr.inference.operators.MCMCOperator;
36 import dr.inference.operators.ScaleOperator;
37 import dr.inference.trace.TraceAnalysis;
38 import dr.inference.trace.TraceException;
39 import dr.util.Attribute;
40
41 import java.io.IOException;
42
43 /**
44 * This tutorial in code covers:
45 * (1) The creation of random variables with bounds.
46 * (2) The creation of a one-dimensional normal distribution model.
47 * (3) Creation of normal likelihood and data
48 * (4) Initialization of MCMC chain with proposal distribution and two loggers.
49 * (5) Post-run trace analysis
50 *
51 * @author Alexei Drummond
52 */
53 public class Tutorial1 {
54
55 public static void main(String[] arg) throws IOException, TraceException {
56
57 // constructing random variable representing mean of normal distribution
58 Variable.D mean = new Variable.D("mean", 1.0);
59 // give mean a uniform prior [-1000, 1000]
60 mean.addBounds(new Parameter.DefaultBounds(1000, -1000, 1));
61
62 // constructing random variable representing stdev of normal distribution
63 Variable.D stdev = new Variable.D("stdev", 1.0);
64 // give stdev a uniform prior [0, 1000]
65 stdev.addBounds(new Parameter.DefaultBounds(1000, 0, 1));
66
67 // construct normal distribution model
68 NormalDistributionModel normal = new NormalDistributionModel(mean, stdev);
69
70 // construct a likelihood for normal distribution
71 DistributionLikelihood likelihood = new DistributionLikelihood(normal);
72
73 // construct data
74 Attribute.Default<double[]> d = new Attribute.Default<double[]>(
75 "x", new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9});
76
77 // add data (representing 9 independent observations) to likelihood
78 likelihood.addData(d);
79
80 // construct two "operators" to be used as the proposal distribution
81 MCMCOperator meanMove = new ScaleOperator(mean, 0.75);
82 MCMCOperator stdevMove = new ScaleOperator(stdev, 0.75);
83
84 // construct a logger to log progress of MCMC run to stdout (screen)
85 MCLogger logger1 = new MCLogger(100);
86 logger1.add(mean);
87 logger1.add(stdev);
88
89 // construct a logger to log to a log file for later analysis
90 MCLogger logger2 = new MCLogger("tutorial1.log", 100, false, 0);
91 logger2.add(mean);
92 logger2.add(stdev);
93
94 // construct MCMC object
95 MCMC mcmc = new MCMC("tutorial1:normal");
96
97 // initialize MCMC with chain length, likelihood, operators and loggers
98 mcmc.init(100000, likelihood, new MCMCOperator[]{meanMove, stdevMove}, new Logger[]{logger1, logger2});
99
100 // run the mcmc
101 mcmc.chain();
102
103 // perform post-analysis
104 TraceAnalysis.report("tutorial1.log");
105 }
106 }