| 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; |
| 27 |
|
| 28 |
import dr.inference.distribution.MultivariateDistributionLikelihood; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.xml.XMLParseException; |
| 31 |
import org.jdom.Element; |
| 32 |
import java.awt.geom.Point2D; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Random; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class Polygon2DSampling extends Polygon2D { |
| 43 |
|
| 44 |
public Polygon2DSampling(double[] x, double[] y, double fillValue) { |
| 45 |
super(x,y); |
| 46 |
this.fillValue = fillValue; |
| 47 |
} |
| 48 |
|
| 49 |
public Polygon2DSampling(Element e, double fillValue) { |
| 50 |
super(e); |
| 51 |
|
| 52 |
this.fillValue = fillValue; |
| 53 |
|
| 54 |
this.logFillValue = Math.log(fillValue); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
public double getProbability(Point2D Point2D, boolean outside) { |
| 59 |
boolean contains = containsPoint2D(Point2D); |
| 60 |
if (contains) { |
| 61 |
return this.fillValue; |
| 62 |
} else { |
| 63 |
return 0.0; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public double getLogProbability(Point2D Point2D, boolean outside) { |
| 68 |
boolean contains = containsPoint2D(Point2D); |
| 69 |
if (contains) { |
| 70 |
return this.logFillValue; |
| 71 |
} else { |
| 72 |
return Double.NEGATIVE_INFINITY; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void setFillValue(double value) { |
| 78 |
fillValue = value; |
| 79 |
logFillValue = Math.log(value); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public double getFillValue() { |
| 85 |
return fillValue; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public double getLogFillValue() { |
| 90 |
return logFillValue; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public boolean hasFillValue() { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
private double fillValue; |
| 99 |
private double logFillValue; |
| 100 |
|
| 101 |
|
| 102 |
public static void main(String[] args) { |
| 103 |
|
| 104 |
List<AbstractPolygon2D> polygons = Polygon2D.readKMLFile(args[0]); |
| 105 |
System.out.println("Reading from file: " + args[0] + "\n"); |
| 106 |
|
| 107 |
List<GeoSpatialDistribution> geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>(); |
| 108 |
|
| 109 |
boolean overAllFill = false; |
| 110 |
AbstractPolygon2D first = polygons.get(0); |
| 111 |
System.out.println("Polygon 0: " + first.getFillValue()); |
| 112 |
boolean equalFills = first.hasFillValue(); |
| 113 |
if (polygons.size() > 1) { |
| 114 |
for (int i = 1; i < polygons.size(); i++) { |
| 115 |
if (!(equalFills == polygons.get(i).hasFillValue())) { |
| 116 |
throw new RuntimeException("Inconsistent fillValue attributes provided."); |
| 117 |
} else { |
| 118 |
System.out.println("Polygon " + i + ": " + polygons.get(i).getFillValue()); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
if (equalFills) { |
| 123 |
double sum = 0.0; |
| 124 |
for (AbstractPolygon2D region : polygons) { |
| 125 |
sum += region.getFillValue(); |
| 126 |
} |
| 127 |
if (Math.abs(sum - 1.0) > 1E-12) { |
| 128 |
throw new RuntimeException("Fill values in " + args[0] + " do not sum to 1 : " + sum); |
| 129 |
} else { |
| 130 |
System.out.println("Provided sampling probabilities sum to 1.0"); |
| 131 |
} |
| 132 |
} |
| 133 |
overAllFill = equalFills; |
| 134 |
for (AbstractPolygon2D region : polygons) { |
| 135 |
geoSpatialDistributions.add(new GeoSpatialDistribution("", region, true)); |
| 136 |
} |
| 137 |
|
| 138 |
System.out.println("\nTesting sampling probabilities:"); |
| 139 |
|
| 140 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood( |
| 141 |
new MultiRegionGeoSpatialDistribution("", geoSpatialDistributions, false, overAllFill)); |
| 142 |
double[] coordinates = new double[2]; |
| 143 |
coordinates[0] = 12.68379879; |
| 144 |
coordinates[1] = 108.12982178; |
| 145 |
Parameter parameter = new Parameter.Default(coordinates); |
| 146 |
likelihood.addData(parameter); |
| 147 |
|
| 148 |
System.out.println("logL = " + likelihood.getLogLikelihood()); |
| 149 |
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood())); |
| 150 |
|
| 151 |
parameter.setParameterValue(0,13.68379879); |
| 152 |
System.out.println("logL = " + likelihood.getLogLikelihood()); |
| 153 |
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood())); |
| 154 |
|
| 155 |
parameter.setParameterValue(0,12.68); |
| 156 |
System.out.println("logL = " + likelihood.getLogLikelihood()); |
| 157 |
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood())); |
| 158 |
|
| 159 |
parameter.setParameterValue(0, 13.03387928); |
| 160 |
parameter.setParameterValue(1, 108.51604462); |
| 161 |
System.out.println("logL = " + likelihood.getLogLikelihood()); |
| 162 |
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood())); |
| 163 |
|
| 164 |
parameter.setParameterValue(0, 13.01971245); |
| 165 |
parameter.setParameterValue(1, 108.56764221); |
| 166 |
System.out.println("logL = " + likelihood.getLogLikelihood()); |
| 167 |
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood())); |
| 168 |
|
| 169 |
parameter.setParameterValue(0, 13.018); |
| 170 |
System.out.println("logL = " + likelihood.getLogLikelihood()); |
| 171 |
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood())); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
} |