Debian Codegraph @

src/dr/geo/distributions/VonMisesFisherDistribution.java

raw symbols SCIP
1 /*
2 * VonMisesFisherDistribution.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.geo.distributions;
27
28 import dr.geo.math.Space;
29 import dr.math.LogTricks;
30
31 /**
32 * @author Marc Suchard
33 */
34 public class VonMisesFisherDistribution extends HyperSphereDistribution {
35
36 public VonMisesFisherDistribution(int dim, Space space, double[] mean, double kappa) {
37 super(dim, space, mean, kappa);
38 }
39
40 public double logPdf(double[] x) {
41 return logPdf(x, mean, kappa, space);
42 }
43
44 public String getType() {
45 return "von Mises-Fisher";
46 }
47
48 protected int getAllowableDim() {
49 return 3;
50 }
51
52 private static double logNormalizationConstant(double kappa) {
53 // 'sinh' has some numerical instability for small arguments
54 if (kappa < 1E-10) {
55 return -Math.log(2) - LOG_2_PI;
56 }
57 return Math.log(kappa) - LOG_2_PI - LogTricks.logDiff(kappa, -kappa);
58 // return Math.log(kappa) - LOG_2_PI - Math.log(Math.exp(+kappa) - Math.exp(-kappa));
59 }
60
61 public static double logPdf(double[] x, double[] mean, double kappa, Space space) {
62 return logNormalizationConstant(kappa) + kappa * HyperSphereDistribution.innerProduct(x, mean, space);
63 }
64
65 public static void main(String[] arg) {
66 // Test in cartesian coordinates
67 double kappa1 = 1;
68 double[] mean1 = { 1,0,0 };
69 double[] x1 = { 0,1,0 };
70 System.err.println("logP = "+logPdf(x1, mean1, kappa1, Space.CARTESIAN)+" ?= -2.692464\n");
71
72
73 // Test in (lat,long) coordinates
74 double kappa2 = 1;
75 double[] mean2 = { 0,0 };
76 double[] x2 = { 0,180 };
77 System.err.println("logP = "+logPdf(x2, mean2, kappa2, Space.LAT_LONG )+" ?= -3.692464\n");
78
79 // Test in (lat,long) coordinates
80 double kappa3 = 2;
81 double[] mean3 = { 90,0 };
82 double[] x3 = { 0,180 };
83 System.err.println("logP = "+logPdf(x3, mean3, kappa3, Space.LAT_LONG )+" ?= -3.126244");
84 }
85 }
86
87
88 // R test code
89 //
90 //x = c(1,0,0)
91 //y = c(0,1,0)
92 //
93 //vmf = function(x, mean, kappa, p) {
94 // order = p/2 - 1
95 // norm = kappa^(order) / (2*pi)^(p/2) / besselI(kappa,order)
96 // norm * exp(kappa * sum(x * mean))
97 //}
98 //
99 //log(vmf(x,y,1,3))
100 //
101 //pt1 = c(0,1,0) # lat_long (0,0)
102 //pt2 = c(0,-1,0) # lat_long (0,180)
103 //
104 //log(vmf(pt1,pt2,1,3))
105 //
106 //pt3 = c(0,0,1) # lat_long (90,0)
107 //pt4 = c(0,-1,0) # lat_long (0,180)
108 //
109 //log(vmf(pt3,pt4,2,3))