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

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


  }

  public LeastSquareResultsWithTransform solve(final DoubleMatrix1D start) {
    final LeastSquareResults lsRes = SOLVER.solve(_vols, _errors, getModelValueFunction(), getModelJacobianFunction(), start);
    return new LeastSquareResultsWithTransform(lsRes, new UncoupledParameterTransforms(start, getTransforms(), new BitSet(start.getNumberOfElements())));
  }
View Full Code Here


    final double[] errors = new double[n];
    Arrays.fill(errors, FIT_ERROR);

    final SmileModelFitter<T> globalFitter = getFitter(forward, strikes, expiry, impliedVols, errors);
    final BitSet gFixed = getGlobalFixedValues();
    LeastSquareResultsWithTransform gBest = null;
    double chiSqr = Double.POSITIVE_INFINITY;

    //TODO set these in sub classes
    int tries = 0;
    int count = 0;
    while (chiSqr > 100.0 * n && count < 5) { //10bps average error
      final DoubleMatrix1D gStart = getGlobalStart(forward, strikes, expiry, impliedVols);
      try {
        final LeastSquareResultsWithTransform glsRes = globalFitter.solve(gStart, gFixed);
        if (glsRes.getChiSq() < chiSqr) {
          gBest = glsRes;
          chiSqr = gBest.getChiSq();
        }
        count++;
      } catch (final Exception e) {
      }
      tries++;
      if (tries > 20) {
        throw new MathException("Cannot fit data");
      }
    }
    if (gBest == null) {
      throw new IllegalStateException("Global estimate was null; should never happen");
    }
    if (n == 3) {
      if (gBest.getChiSq() / n > 1.0) {
        s_logger.debug("chi^2 on fit to ", +n + " points is " + gBest.getChiSq());
      }
      modelParameters.add(toSmileModelData(gBest.getModelParameters()));
    } else {
      final BitSet lFixed = getLocalFixedValues();
      DoubleMatrix1D lStart = gBest.getModelParameters();
      for (int i = 0; i < n - 2; i++) {
        final double[][] temp = getStrikesVolsAndErrors(i, strikes, impliedVols, errors);
        final double[] tStrikes = temp[0];
        final double[] tVols = temp[1];
        final double[] tErrors = temp[2];
        final SmileModelFitter<T> localFitter = getFitter(forward, tStrikes, expiry, tVols, tErrors);
        LeastSquareResultsWithTransform lRes = localFitter.solve(lStart, lFixed);
        LeastSquareResultsWithTransform best = lRes;

        count = 0;
        while (lRes.getChiSq() > 3.0 && count < 10) {
          lStart = getGlobalStart(forward, strikes, expiry, impliedVols);
          lRes = localFitter.solve(lStart, lFixed);
          if (lRes.getChiSq() < best.getChiSq()) {
            best = lRes;
          }
          count++;
        }

        if (best.getChiSq() > 3.0) {
          s_logger.debug("chi^2 on 3-point fit #" + i + " is " + best.getChiSq());
        }
        modelParameters.add(toSmileModelData(best.getModelParameters()));
      }
    }
    return modelParameters;
  }
View Full Code Here

    if (n == 3 || !_globalBetaSearch) {
      fixed.set(1); //fixed beta
    }

    //do a global fit first
    final LeastSquareResultsWithTransform gRes = globalFitter.solve(start, fixed);

    if (n == 3) {
      if (gRes.getChiSq() / n > 1.0) {
        s_logger.warn("chi^2 on SABR fit to ", +n + " points is " + gRes.getChiSq());
      }
      modelParams[0] = new SABRFormulaData(gRes.getModelParameters().getData());
    } else {
      //impose a global beta on the remaining 3 point fits
      final double[] gFitParms = gRes.getModelParameters().getData();
      final double beta = gFitParms[1];
      start = new DoubleMatrix1D(gFitParms[0], gFitParms[2], gFitParms[3]);
      final BroydenVectorRootFinder rootFinder = new BroydenVectorRootFinder();

      double[] tStrikes = new double[3];
View Full Code Here

    if (n == 3 || !_globalBetaSearch) {
      fixed.set(1); //fixed beta
    }

    //do a global fit first
    final LeastSquareResultsWithTransform gRes = globalFitter.solve(start, fixed);

    if (n == 3) {
      if (gRes.getChiSq() / n > 1.0) {
        s_logger.warn("chi^2 on SABR fit to ", +n + " points is " + gRes.getChiSq());
      }
      modelParams[0] = new MixedLogNormalModelData(gRes.getModelParameters().getData());
    } else {
      //impose a global beta on the remaining 3 point fits
      //final double[] gFitParms = gRes.getModelParameters().getData();
      //final double theta = gFitParms[2];
      //start = new DoubleMatrix1D(gFitParms[0], gFitParms[1], gFitParms[3]);
      start = gRes.getModelParameters();
      fixed.set(2); //fixed weight
      //final BroydenVectorRootFinder rootFinder = new BroydenVectorRootFinder(1e-8, 1e-8, 100, new SVDecompositionCommons());

      double[] tStrikes = new double[4];
      double[] tVols = new double[4];

      for (int i = 0; i < n - 2; i++) {
        tStrikes = Arrays.copyOfRange(strikes, i, i + 3);
        tVols = Arrays.copyOfRange(impliedVols, i, i + 3);
        errors = new double[3];
        Arrays.fill(errors, 0.0001); //1bps
        //        Function1D<DoubleMatrix1D, DoubleMatrix1D> func = getVolDiffFunc(forward, tStrikes, expiry, tVols, theta);
        //        Function1D<DoubleMatrix1D, DoubleMatrix2D> jac = getVolJacFunc(forward, tStrikes, expiry, theta);
        //        NonLinearTransformFunction tf = new NonLinearTransformFunction(func, jac, TRANSFORM);
        //
        //        DoubleMatrix1D res = rootFinder.getRoot(tf.getFittingFunction(),tf.getFittingJacobian(), start);
        //        double[] root = TRANSFORM.inverseTransform(res).getData();

        final SmileModelFitter<MixedLogNormalModelData> fitter = new MixedLogNormalModelFitter(forward, tStrikes, expiry, tVols, errors, MODEL, 2, true);
        final LeastSquareResultsWithTransform lRes = fitter.solve(start, fixed);
        if (lRes.getChiSq() > 3.0) {
          s_logger.warn("chi^2 on 3-point SABR fit #" + i + " is " + lRes.getChiSq());
        }
        modelParams[i] = new MixedLogNormalModelData(lRes.getModelParameters().getData());

        // modelParams[i] = new MixedLogNormalModelData(new double[] {root[0], root[1], theta, root[2] });
      }
    }
View Full Code Here

          data[p] = new BlackFunctionData(forwards[i], 1, vols[i][j]);
          errors[p] = 0.001;
          p++;
        }
      }
      final LeastSquareResultsWithTransform result = FITTER.getFitResult(options, data, errors, start, new BitSet(4));
      final DoubleMatrix1D params = result.getModelParameters();
      alpha[i] = params.getEntry(0);
      beta[i] = params.getEntry(1);
      nu[i] = params.getEntry(2);
      rho[i] = params.getEntry(3);
View Full Code Here

    final ConjugateDirectionVectorMinimizer minimzer = new ConjugateDirectionVectorMinimizer(lineMinimizer, 1e-6, 10000);
    final DoubleMatrix1D fp = transforms.transform(new DoubleMatrix1D(initialFitParameters));
    final DoubleMatrix1D minPos = minimzer.minimize(function, fp);
    final double chiSquare = function.evaluate(minPos);
    final DoubleMatrix1D res = transforms.inverseTransform(minPos);
    return new LeastSquareResultsWithTransform(new LeastSquareResults(chiSquare, res, new DoubleMatrix2D(new double[N_PARAMETERS][N_PARAMETERS])), transforms);
    // return new LeastSquareResults(chiSquare, res, new DoubleMatrix2D(new double[N_PARAMETERS][N_PARAMETERS]));
  }
View Full Code Here

    final NonLinearLeastSquare ls = new NonLinearLeastSquare();
    //solve approx first
    LeastSquareResults solverRes = ls.solve(new DoubleMatrix1D(mrkVols), new DoubleMatrix1D(sigma), funcAppox, TRANSFORMS.transform(initialGuess));
    // now solve pde model
    solverRes = ls.solve(new DoubleMatrix1D(mrkVols), new DoubleMatrix1D(sigma), func, solverRes.getFitParameters());
    return new LeastSquareResultsWithTransform(solverRes, TRANSFORMS);
  }
View Full Code Here

    final DoubleMatrix1D fp = transforms.transform(new DoubleMatrix1D(initialFitParameters));
    final LeastSquareResults lsRes = errors == null ? _solver.solve(new DoubleMatrix1D(strikes), new DoubleMatrix1D(blackVols), function, fp) : _solver.solve(new DoubleMatrix1D(strikes),
        new DoubleMatrix1D(blackVols), new DoubleMatrix1D(errors), function, fp);
    // final DoubleMatrix1D mp = transforms.inverseTransform(lsRes.getFitParameters());
    return new LeastSquareResultsWithTransform(lsRes, transforms);
    // return new LeastSquareResults(lsRes.getChiSq(), mp, new DoubleMatrix2D(new double[N_PARAMETERS][N_PARAMETERS]));

  }
View Full Code Here

    final Map<DoublesPair, Double> marketVolsMap = convertFormatt(marketVols);
    final Set<DoublesPair> dataPoints = marketVolsMap.keySet();

    // first fit calibrate the basic Markov chain model (i.e. fixed vol levels) to market data
    final LeastSquareResultsWithTransform chiSqRes = CHAIN_FITTER.fit(forward, marketVols, initialGuess);

    final DoubleMatrix1D modelParms = chiSqRes.getModelParameters();

    final double vol1 = modelParms.getEntry(0);
    final double vol2 = modelParms.getEntry(1) + vol1;
    final double lambda12 = modelParms.getEntry(2);
    final double lambda21 = modelParms.getEntry(3);
View Full Code Here

  @Test
  public void testExactFit() {

    final double[] start = new double[] {0.1, 0.7, 0.0, 0.3};
    final LeastSquareResultsWithTransform results = FITTER.solve(new DoubleMatrix1D(start));
    final double[] res = results.getModelParameters().getData();
    final double eps = 1e-6;
    assertEquals(ALPHA, res[0], eps);
    assertEquals(BETA, res[1], eps);
    assertEquals(RHO, res[2], eps);
    assertEquals(NU, res[3], eps);
    assertEquals(0.0, results.getChiSq(), eps);
  }
View Full Code Here

TOP

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

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.