Package com.opengamma.analytics.math.statistics.leastsquare

Examples of com.opengamma.analytics.math.statistics.leastsquare.LeastSquareResults


    MODEL.getParameterizedFunction().evaluate(3., new DoubleMatrix1D(new double[] {1, 2, 3 }));
  }

  @Test
  public void testKnownParameters() {
    final LeastSquareResults result = NLLS.solve(T, Y, MODEL.getParameterizedFunction(), new DoubleMatrix1D(new double[] {2, -1, 1, 4 }));
    final DoubleMatrix1D fitted = result.getFitParameters();
    assertArrayEquals(fitted.getData(), new double[] {BETA0, BETA1, BETA2, LAMBDA }, 1e-6);
  }
View Full Code Here


    assertArrayEquals(fitted.getData(), new double[] {BETA0, BETA1, BETA2, LAMBDA }, 1e-6);
  }

  @Test
  public void testFit() {
    final LeastSquareResults result = NLLS.solve(TREASURY_T, TREASURY_19740102_Y, TREASURY_E, MODEL.getParameterizedFunction(), new DoubleMatrix1D(new double[] {1, 1, 1, 1 }));
    final DoubleMatrix1D fitted = result.getFitParameters();
    assertEquals(fitted.getEntry(0), 6.88649008, 1e-3);
    assertEquals(fitted.getEntry(1), 1.56992400, 1e-3);
    assertEquals(fitted.getEntry(2), -2.58262072, 1e-3);
    assertEquals(fitted.getEntry(3), 1.14781832, 1e-3);
    //    result = NLLS.solve(TREASURY_T, TREASURY_19740830_Y, TREASURY_E, MODEL.getParameterizedFunction(), new DoubleMatrix1D(new double[] {1, 1, 1, 1}));
View Full Code Here

    MODEL.getParameterizedFunction().evaluate(3., new DoubleMatrix1D(new double[] {1, 2, 3}));
  }

  @Test
  public void testKnownParameters() {
    final LeastSquareResults result = NLLS.solve(T, Y, MODEL.getParameterizedFunction(), new DoubleMatrix1D(new double[] {2, 1, 1, 4, 6, 1 }));
    final DoubleMatrix1D fitted = result.getFitParameters();
    assertArrayEquals(fitted.getData(), new double[] {BETA0, BETA1, BETA2, LAMBDA1, BETA3, LAMBDA2}, 1e-4);
  }
View Full Code Here

    assertArrayEquals(fitted.getData(), new double[] {BETA0, BETA1, BETA2, LAMBDA1, BETA3, LAMBDA2}, 1e-4);
  }

  @Test
  public void testFit() {
    final LeastSquareResults result = NLLS.solve(TREASURY_T, TREASURY_20110127_Y, TREASURY_E, MODEL.getParameterizedFunction(), new DoubleMatrix1D(new double[] {2, -1, -1, 2, 2, 9 }));
    final DoubleMatrix1D fitted = result.getFitParameters();
    assertEquals(fitted.getEntry(0), 4.07923660, 1e-2);
    assertEquals(fitted.getEntry(1), -3.74204358, 1e-2);
    assertEquals(fitted.getEntry(2), -6.18790519, 1e-2);
    assertEquals(fitted.getEntry(3), 1.92088325, 1e-2);
    assertEquals(fitted.getEntry(4), 5.43483123, 1e-2);
View Full Code Here

    final double[] capVols = pricer.impliedVols(capPrices);
    final double[] vega = pricer.vega(capVols);
    final double[] start = new double[getnCaplets()];
    Arrays.fill(start, capVols[capVols.length - 1]);

    final LeastSquareResults lsRes = solveForPrice(new DoubleMatrix1D(capPrices), new DoubleMatrix1D(vega), new DoubleMatrix1D(start));

    final double[] mPrices = pricer.priceFromCapletVols(lsRes.getFitParameters().getData());
    final double chiSqr = chiSqr(capPrices, mPrices);
    final VolatilityTermStructure vc = getVolCurve(lsRes.getFitParameters().getData());
    return new CapletStrippingSingleStrikeResult(chiSqr, lsRes.getFitParameters(), vc, new DoubleMatrix1D(mPrices));
  }
View Full Code Here

      sigma = new DoubleMatrix1D(errors);
    }

    final double[] start = new double[getnCaplets()];
    Arrays.fill(start, capVols[capVols.length - 1]);
    final LeastSquareResults lsRes = solveForPrice(new DoubleMatrix1D(capPrices), sigma, new DoubleMatrix1D(start));

    final double[] mPrices = pricer.priceFromCapletVols(lsRes.getFitParameters().getData());
    final double chiSqr = chiSqr(capPrices, mPrices, errors);
    final VolatilityTermStructure vc = getVolCurve(lsRes.getFitParameters().getData());
    return new CapletStrippingSingleStrikeResult(chiSqr, lsRes.getFitParameters(), vc, new DoubleMatrix1D(mPrices));
  }
View Full Code Here

    // for (int i = 0; i < nCaplets; i++) {
    // start[i] = altVC.getVolatility(t[i]);
    // }

    final int n = getnCaps();
    LeastSquareResults lsRes;
    if (solveByPrice) {
      final double[] capPrices = pricer.price(capVols);
      final double[] capVega = pricer.vega(capVols);
      for (int i = 0; i < n; i++) {
        capVega[i] *= errors[i];
      }
      final DoubleMatrix1D sigma = new DoubleMatrix1D(capVega);
      lsRes = solveForPrice(new DoubleMatrix1D(capPrices), sigma, new DoubleMatrix1D(start));
    } else {
      lsRes = solveForVol(new DoubleMatrix1D(capVols), new DoubleMatrix1D(errors), new DoubleMatrix1D(start));
    }

    final double[] mVols = pricer.impliedVols(pricer.priceFromCapletVols(lsRes.getFitParameters().getData()));
    final double chiSqr = chiSqr(capVols, mVols, errors);
    final VolatilityTermStructure vc = getVolCurve(lsRes.getFitParameters().getData());
    return new CapletStrippingSingleStrikeResult(chiSqr, lsRes.getFitParameters(), vc, new DoubleMatrix1D(mVols));
  }
View Full Code Here

    checkPrices(capPrices);
    checkErrors(errors);

    final MultiCapFloorPricer pricer = getPricer();
    final double[] capVols = pricer.impliedVols(capPrices);
    final LeastSquareResults lsRes = solveForPrice(capPrices, capVols, errors, scaleByVega);

    final VolatilityTermStructure volCurve = getVolCurve(lsRes.getFitParameters());
    final double[] mPrices = pricer.price(volCurve);

    // least-squares gives chi2 including the penalty, and is calculated via the price differecne and vega w we just want the fit error
    // TODO maybe the solver should provide this?
    final double chi2 = chiSqr(capPrices, mPrices, errors); // ignore the vega weighting here
    return new CapletStrippingSingleStrikeResult(chi2, lsRes.getFitParameters(), volCurve, new DoubleMatrix1D(mPrices));
  }
View Full Code Here

    checkVols(capVols);
    checkErrors(errors);

    final MultiCapFloorPricer pricer = getPricer();

    LeastSquareResults lsRes;
    if (solveViaPrice) {
      final double[] capPrices = pricer.price(capVols);
      lsRes = solveForPrice(capPrices, capVols, errors, true);
    } else {
      final Function1D<DoubleMatrix1D, DoubleMatrix1D> modelVolFunc = new Function1D<DoubleMatrix1D, DoubleMatrix1D>() {
        @SuppressWarnings("synthetic-access")
        @Override
        public DoubleMatrix1D evaluate(final DoubleMatrix1D x) {
          final double[] modelVols = pricer.impliedVols(_volModel.evaluate(x));
          return new DoubleMatrix1D(modelVols);
        }
      };
      lsRes = NLLSWP.solve(new DoubleMatrix1D(capVols), new DoubleMatrix1D(errors), modelVolFunc, new DoubleMatrix1D(_nWeights, capVols[capVols.length - 1]), _penalty);
    }

    final VolatilityTermStructure volCurve = getVolCurve(lsRes.getFitParameters());
    final double[] mVols = pricer.impliedVols(volCurve);

    // least-squares gives chi2 including the penalty, and is calculated via the price differecne and vega w we just want the fit error
    // TODO maybe the solver should provide this?
    final double chi2 = chiSqr(capVols, mVols, errors);
    return new CapletStrippingSingleStrikeResult(chi2, lsRes.getFitParameters(), volCurve, new DoubleMatrix1D(mVols));

  }
View Full Code Here

    DoubleMatrix1D sigma = new DoubleMatrix1D(n, 1e-4);
    DoubleMatrix1D start = new DoubleMatrix1D(n - 1, 0.8);
    //  DoubleMatrix1D maxJump = new DoubleMatrix1D(n - 1, Math.PI / 20);

    LeastSquareResults res = SOLVER.solve(new DoubleMatrix1D(w), sigma, func, start/*, maxJump*/);
    assertEquals("chi sqr", 0.0, res.getChiSq(), 1e-9);
    double[] fit = res.getFitParameters().getData();
    double[] expected = trans.inverseTransform(w);
    for (int i = 0; i < n - 1; i++) {
      //put the fit result back in the range 0 - pi/2
      double x = fit[i];
      if (x < 0) {
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.math.statistics.leastsquare.LeastSquareResults

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.