Debian Codegraph @

src/dr/geo/distributions/HyperSphereDistribution.java

raw symbols SCIP
1 /*
2 * HyperSphereDistribution.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.math.distributions.MultivariateDistribution;
29 import dr.geo.math.SphericalPolarCoordinates;
30 import dr.geo.math.Space;
31
32 /**
33 * @author Marc Suchard
34 *
35 * Abstract base class for probability distributions on a hyper-sphere S^{d-1} in R^{d}.
36 * Usual cases are: circle (d=2) and sphere (d=3)
37 */
38
39 public abstract class HyperSphereDistribution implements MultivariateDistribution {
40
41 protected static double LOG_2_PI = Math.log(2 * Math.PI);
42
43 public HyperSphereDistribution(int dim, Space space, double[] mean, double kappa) {
44 this.dim = dim;
45 this.mean = mean;
46 this.kappa = kappa;
47 this.space = space;
48 checkImplementation();
49 }
50
51 public abstract double logPdf(double[] x);
52
53 public double[][] getScaleMatrix() {
54 return new double[0][];
55 }
56
57 public double[] getMean() {
58 return new double[0];
59 }
60
61 public String getType() {
62 return "Hyperspherical";
63 }
64
65 protected int getAllowableDim() {
66 return dim;
67 }
68
69 protected void checkImplementation() {
70 final int allowableDim = getAllowableDim();
71 if (allowableDim != dim) {
72 throw new RuntimeException(getType() + " distribution is not implemented in R^"+dim);
73 }
74 }
75
76
77 public static double innerProduct(double[] x, double[] y, Space space) {
78 switch (space) {
79 case LAT_LONG : return latLongToCartesianInnerProduct(x, y, 1.0);
80 case CARTESIAN : return cartestianInnerProduct(x, y);
81 case RADIANS : return radiansInnerProduct(x, y);
82 default : throw new RuntimeException("Inner product not yet implemented");
83 }
84 }
85
86 private static double radiansInnerProduct(double[] x, double[] y) {
87
88 // x[] and y[] should be in the form (radians)
89 if (x.length != 1 && y.length != 1) {
90 throw new RuntimeException("Wrong dimensions");
91 }
92
93 return Math.cos(x[0] - y[0]);
94 }
95
96 private static double cartestianInnerProduct(double[] x, double[] y) {
97
98 // x[] and y[] must be unit vectors
99 if (!isUnitVector(x) || !isUnitVector(y)) {
100 throw new RuntimeException("Inner produce must be on unit vectors");
101 }
102
103 final int len = x.length;
104 double innerProduct = 0;
105 for(int i = 0; i < len; i++) {
106 innerProduct += x[i] * y[i];
107 }
108 return innerProduct;
109 }
110
111 private static double tolerance = 1E-10;
112
113 public static boolean isUnitVector(double[] test) {
114 double norm = 0;
115 for(double d : test) {
116 norm += d * d;
117 }
118 return (Math.abs(norm - 1) < tolerance);
119 }
120
121 private static double latLongToCartesianInnerProduct(double[] x, double[] y, double radius) {
122
123 // x[] and y[] should be in the form (lat, long)
124 if (x.length != 2 || y.length != 2) {
125 throw new RuntimeException("Wrong dimensions");
126 }
127
128 final SphericalPolarCoordinates coordX = new SphericalPolarCoordinates(x[0], x[1], radius);
129 final SphericalPolarCoordinates coordY = new SphericalPolarCoordinates(y[0], y[1], radius);
130
131 return coordX.getCartesianCoordinates().dot(coordY.getCartesianCoordinates());
132 }
133
134 protected int dim;
135 protected Space space;
136 protected double[] mean;
137 protected double kappa;
138 }