Debian Codegraph @

src/dr/evomodel/continuous/VonMisesFisherDiffusionModel.java

raw symbols SCIP
1 /*
2 * VonMisesFisherDiffusionModel.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.evomodel.continuous;
27
28 import dr.inference.model.Parameter;
29 import dr.geo.math.Space;
30 import dr.geo.distributions.VonMisesFisherDistribution;
31 import dr.xml.*;
32
33 /**
34 * @author Marc Suchard
35 * <p/>
36 * The Von Mises-Fisher distributions provides an approximation to diffusion on a sphere
37 * <p/>
38 * Main paper:
39 * R.A. Fisher (1953) Dispersion on a sphere. Proceedings of the Royal Society of London, Series A, 217, 295 - 305.
40 * <p/>
41 * Generalizations are discussed in:
42 * K. Shimizu and K. Iida, Pearson type VII distributions on spheres, Commun. Stat. Theory Meth. 31 (2002), pp. 513 - 526
43 * K. shimizu and H.Y. Slew, The generalized symmetric Laplace distribution on the sphere, Statistical Methology, 5, 2008
44 */
45
46 public class VonMisesFisherDiffusionModel extends MultivariateDiffusionModel {
47
48 public static final String VON_MISES_FISHER_MODEL = "vonMisesFisherDiffusionModel";
49 public static final String KAPPA = "kappa";
50
51 public VonMisesFisherDiffusionModel(Parameter concentrationParameter) {
52 this(3, concentrationParameter); // Default is distribution on a sphere
53 }
54
55 public VonMisesFisherDiffusionModel(int p, Parameter concentrationParameter) {
56
57 super();
58 this.p = p;
59 this.concentrationParameter = concentrationParameter;
60 calculatePrecisionInfo();
61 addVariable(concentrationParameter);
62 }
63
64 protected void calculatePrecisionInfo() {
65 if (p == 1 || p > 3) {
66 throw new RuntimeException("Von Mises-Fisher distribution only implemented for circles and spheres");
67 }
68 // Nothing gets stored
69 }
70
71 private boolean isInBounds(double[] x) {
72 final double latitude = x[0];
73 final double longitude = x[1];
74 return (latitude >= -90 && latitude < 90 && longitude > -180 && longitude < 180);
75 }
76
77 protected double calculateLogDensity(double[] start, double[] stop, double time) {
78
79 // TODO Write transition kernel that wraps these parameters
80 if (!isInBounds(start) || !isInBounds(stop)) return Double.NEGATIVE_INFINITY;
81
82 final double kappa = concentrationParameter.getParameterValue(0) / time;
83 return VonMisesFisherDistribution.logPdf(start, stop, kappa, Space.LAT_LONG);
84 }
85
86 private Parameter concentrationParameter;
87 private int p;
88
89 public static void main(String[] arg) {
90
91 Parameter kappa = new Parameter.Default(2.0);
92 VonMisesFisherDiffusionModel model = new VonMisesFisherDiffusionModel(kappa);
93
94 double[] start = {90,0}; // North-pole
95 double[] stop = {0,90}; // Somewhere in the East
96 double time = 0.1;
97
98 System.err.println("logPDF = "+model.calculateLogDensity(start,stop,time)+" ?= -18.84214");
99
100 // R code check
101 // north = c(0,0,1)
102 // east = c(0,1,0)
103 // log(vmf(north,east,2/0.1,3))
104
105 }
106
107 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
108
109 public String getParserName() {
110 return VON_MISES_FISHER_MODEL;
111 }
112
113 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
114
115 XMLObject cxo = xo.getChild(KAPPA);
116 Parameter kappa = (Parameter) cxo.getChild(Parameter.class);
117
118 return new VonMisesFisherDiffusionModel(kappa);
119 }
120
121 //************************************************************************
122 // AbstractXMLObjectParser implementation
123 //************************************************************************
124
125 public String getParserDescription() {
126 return "Describes a von Mises-Fisher distributed diffusion process on a sphere.";
127 }
128
129 public XMLSyntaxRule[] getSyntaxRules() {
130 return rules;
131 }
132
133 private final XMLSyntaxRule[] rules = {
134 new ElementRule(KAPPA,
135 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
136 };
137
138 public Class getReturnType() {
139 return MultivariateDiffusionModel.class;
140 }
141 };
142
143 }