| 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 dr.math.matrixAlgebra.SymmetricMatrix; |
| 29 |
import dr.xml.*; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class CorrelationStatistic extends Statistic.Abstract { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public static final String CORRELATION_STATISTIC = "correlation"; |
| 43 |
public static final String DIMENSION1 = "dimension1"; |
| 44 |
public static final String DIMENSION2 = "dimension2"; |
| 45 |
|
| 46 |
private MatrixParameter precision = null; |
| 47 |
private int dim1; |
| 48 |
private int dim2; |
| 49 |
|
| 50 |
public CorrelationStatistic(String name, MatrixParameter precision, |
| 51 |
int dim1, int dim2) { |
| 52 |
super(name); |
| 53 |
this.precision = precision; |
| 54 |
this.dim1 = dim1 - 1; |
| 55 |
this.dim2 = dim2 - 1; |
| 56 |
|
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
public int getDimension() { |
| 61 |
return 1; |
| 62 |
} |
| 63 |
|
| 64 |
public double getStatisticValue(int dim) { |
| 65 |
|
| 66 |
double[][] variance = new SymmetricMatrix( |
| 67 |
precision.getParameterAsMatrix()).inverse().toComponents(); |
| 68 |
return variance[dim1][dim2] / |
| 69 |
Math.sqrt(variance[dim1][dim1] * variance[dim2][dim2]); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 74 |
|
| 75 |
public String getParserName() { |
| 76 |
return CORRELATION_STATISTIC; |
| 77 |
} |
| 78 |
|
| 79 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 80 |
|
| 81 |
|
| 82 |
MatrixParameter matrix = (MatrixParameter) xo.getChild(MatrixParameter.class); |
| 83 |
int dim1 = xo.getIntegerAttribute(DIMENSION1); |
| 84 |
int dim2 = xo.getIntegerAttribute(DIMENSION2); |
| 85 |
|
| 86 |
if (dim1 < 1 || dim1 > matrix.getRowDimension() || dim2 < 1 || dim2 > matrix.getColumnDimension()) |
| 87 |
throw new XMLParseException("Invalid dimensions in " + getParserName() + " element"); |
| 88 |
|
| 89 |
return new CorrelationStatistic(CORRELATION_STATISTIC, matrix, dim1, dim2); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public String getParserDescription() { |
| 97 |
return "This element returns a precision that is the element-wise reciprocal of the child precision."; |
| 98 |
} |
| 99 |
|
| 100 |
public Class getReturnType() { |
| 101 |
return ReciprocalStatistic.class; |
| 102 |
} |
| 103 |
|
| 104 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 105 |
return rules; |
| 106 |
} |
| 107 |
|
| 108 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 109 |
new ElementRule(MatrixParameter.class, 1, 1), |
| 110 |
AttributeRule.newIntegerRule(DIMENSION1), |
| 111 |
AttributeRule.newIntegerRule(DIMENSION2) |
| 112 |
|
| 113 |
}; |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
} |