| 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.inference.model; |
| 27 |
|
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public abstract class AbstractAlgebraStatistic extends Statistic.Abstract { |
| 35 |
|
| 36 |
private int firstDataDimension = 0; |
| 37 |
|
| 38 |
private int dimension = 0; |
| 39 |
private boolean elementwise; |
| 40 |
private final double[] constants; |
| 41 |
|
| 42 |
public AbstractAlgebraStatistic(String name, boolean elementwise, double[] constants) { |
| 43 |
super(name); |
| 44 |
this.elementwise = elementwise; |
| 45 |
this.constants = constants; |
| 46 |
if (constants != null) { |
| 47 |
firstDataDimension = constants.length; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public void addStatistic(Statistic statistic) { |
| 52 |
if (dimension > 0) { |
| 53 |
throw new IllegalArgumentException("Cannot add more data after the statistic has been evaluated."); |
| 54 |
} |
| 55 |
|
| 56 |
if (!elementwise) { |
| 57 |
if (firstDataDimension == 0) { |
| 58 |
firstDataDimension = statistic.getDimension(); |
| 59 |
} else if (firstDataDimension != statistic.getDimension()) { |
| 60 |
throw new IllegalArgumentException("Data added doesn't match previous dimensions"); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
statistics.add(statistic); |
| 65 |
} |
| 66 |
|
| 67 |
public int getDimension() { |
| 68 |
if (dimension == 0) { |
| 69 |
if ((constants != null || statistics.size() > 1) && !elementwise) { |
| 70 |
dimension = statistics.get(0).getDimension(); |
| 71 |
} else { |
| 72 |
elementwise = true; |
| 73 |
dimension = 1; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return dimension; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public double getStatisticValue(int dim) { |
| 84 |
|
| 85 |
|
| 86 |
getDimension(); |
| 87 |
|
| 88 |
double value = Double.NaN; |
| 89 |
|
| 90 |
for (Statistic statistic : statistics) { |
| 91 |
if (elementwise) { |
| 92 |
|
| 93 |
|
| 94 |
if (Double.isNaN(value)) { |
| 95 |
value = statistic.getStatisticValue(0); |
| 96 |
} |
| 97 |
for (int j = 1; j < statistic.getDimension(); j++) { |
| 98 |
value = doOperation(value, statistic.getStatisticValue(j)); |
| 99 |
} |
| 100 |
} else { |
| 101 |
if (Double.isNaN(value)) { |
| 102 |
value = statistic.getStatisticValue(dim); |
| 103 |
} else { |
| 104 |
value = doOperation(value, statistic.getStatisticValue(dim)); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
if (constants != null && constants.length > 1) { |
| 109 |
if (constants.length == 1) { |
| 110 |
value = doOperation(value, constants[0]); |
| 111 |
} else { |
| 112 |
value = doOperation(value, constants[dim]); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return value; |
| 117 |
} |
| 118 |
|
| 119 |
protected abstract double doOperation(double a, double b); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private final List<Statistic> statistics = new ArrayList<Statistic>(); |
| 126 |
} |