DeltaGammaCovarianceMatrixFisherKurtosisCalculator extends Function1D<Map<Integer, ParametricVaRDataBundle>, Double> { @Override public Double evaluate(final Map<Integer, ParametricVaRDataBundle> data) { Validate.notNull(data, "data"); final ParametricVaRDataBundle firstOrderData = data.get(1); Validate.notNull(firstOrderData, "first order data"); final ParametricVaRDataBundle secondOrderData = data.get(2); if (secondOrderData == null) { return 0.; } final DoubleMatrix1D delta = (DoubleMatrix1D) firstOrderData.getSensitivities(); final Matrix<?> gamma = secondOrderData.getSensitivities(); if (gamma == null || gamma.getNumberOfElements() == 0) { return 0.; } final DoubleMatrix2D gammaMatrix = (DoubleMatrix2D) gamma; final DoubleMatrix2D deltaCovariance = firstOrderData.getCovarianceMatrix(); if (gammaMatrix.getNumberOfColumns() != deltaCovariance.getNumberOfColumns()) { throw new IllegalArgumentException("Gamma matrix and covariance matrix were incompatible sizes"); } final Matrix<?> product = _algebra.multiply(gammaMatrix, deltaCovariance); final double std = _std.evaluate(data); final double numerator = _algebra.getTrace(_algebra.getPower(product, 4)) + 12 * _algebra.getInnerProduct(delta, _algebra.multiply(_algebra.multiply(deltaCovariance, _algebra.getPower(product, 2)), delta)) + 3 * std * std; final double denominator = Math.pow(0.5 * _algebra.getTrace(_algebra.getPower(product, 2)) + _algebra.getInnerProduct(delta, _algebra.multiply(deltaCovariance, delta)), 2); return numerator / denominator - 3; } DeltaGammaCovarianceMatrixFisherKurtosisCalculator(final MatrixAlgebra algebra); @Override Double evaluate(final Map<Integer, ParametricVaRDataBundle> data); @Override int hashCode(); @Override boolean equals(final Object obj);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullData() { F.evaluate((Map<Integer, ParametricVaRDataBundle>) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testGammaMatrixSize() { final ParametricVaRDataBundle delta = new ParametricVaRDataBundle(DELTA_VECTOR, COVARIANCE_MATRIX, 1); final ParametricVaRDataBundle gamma = new ParametricVaRDataBundle(new DoubleMatrix2D(new double[][] {new double[] {1, 2, 3}, new double[] {4, 5, 6}, new double[] {7, 8, 9}}), new DoubleMatrix2D(new double[][] {new double[] {1, 2, 3}, new double[] {4, 5, 6}, new double[] {7, 8, 9}}), 2); final Map<Integer, ParametricVaRDataBundle> m = new HashMap<>(); m.put(1, delta); m.put(2, gamma); F.evaluate(m); }
@Test public void testNoGamma() { final ParametricVaRDataBundle delta = new ParametricVaRDataBundle(DELTA_VECTOR, COVARIANCE_MATRIX, 1); final Map<Integer, ParametricVaRDataBundle> m = new HashMap<>(); m.put(1, delta); assertEquals(F.evaluate(m), 0, 0); m.put(2, new ParametricVaRDataBundle(new DoubleMatrix2D(new double[0][0]), new DoubleMatrix2D(new double[0][0]), 2)); assertEquals(F.evaluate(m), 0, 0); }
@Test public void test() { final ParametricVaRDataBundle delta = new ParametricVaRDataBundle(DELTA_VECTOR, COVARIANCE_MATRIX, 1); final ParametricVaRDataBundle gamma = new ParametricVaRDataBundle(GAMMA_MATRIX, COVARIANCE_MATRIX, 1); final Map<Integer, ParametricVaRDataBundle> m = new HashMap<>(); m.put(1, delta); m.put(2, gamma); assertEquals(F.evaluate(m), 47.153, 1e-3); }