| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |