Examples of MixedLogNormalVolatilityFunction


Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

    final MixedLogNormalModelData inObjX = new MixedLogNormalModelData(inWeights, inSigmasX, inRelativePartialForwardsX);
    final MixedLogNormalModelData inObjY = new MixedLogNormalModelData(inWeights, inSigmasY, inRelativePartialForwardsY);

    final double[] xx = new double[] {0.9, 0.95, 1.0, 1.05, 1.1, 0.9, 0.95, 1.0, 1.05, 1.15 };

    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();
    Arrays.fill(yy, 0.);

    for (int j = 0; j < nDataPtsX; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdX, inObjX);
    }

    for (int j = nDataPtsX; j < nDataPts; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdY, inObjY);
    }

    for (int i = 0; i < nParams; ++i) {
      aaGuess1[i] = 0.5;
    }
    final MixedBivariateLogNormalFitter fitter1 = new MixedBivariateLogNormalFitter();

    for (int i = 0; i < nNorms; ++i) {
      aaGuess1X[i] = aaGuess1[i];
      aaGuess1Y[i] = aaGuess1[i + nNorms];
    }
    for (int i = 0; i < nNorms - 1; ++i) {
      aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
      aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
    }

    fitter1.doFit(aaGuess1, xx, yy, time, fwdX, fwdY, nNorms, nDataPtsX, 1);
    aaGuess1 = fitter1.getParams();

    for (int i = 0; i < nNorms; ++i) {
      aaGuess1X[i] = aaGuess1[i];
      aaGuess1Y[i] = aaGuess1[i + nNorms];
    }
    for (int i = 0; i < nNorms - 1; ++i) {
      aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
      aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
    }

    for (int i = 0; i < nNorms; ++i) {
      aaGuess1X[i] = aaGuess1[i];
      aaGuess1Y[i] = aaGuess1[i + nNorms];
    }
    for (int i = 0; i < nNorms - 1; ++i) {
      aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
      aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
    }

    final MixedLogNormalModelData objAns1X = new MixedLogNormalModelData(aaGuess1X, true);
    final double[] weights = objAns1X.getWeights();
    final double[] sigmasX = objAns1X.getVolatilities();
    final double[] relativePartialForwardsX = objAns1X.getRelativeForwards();

    final MixedLogNormalModelData objAns1Y = new MixedLogNormalModelData(aaGuess1Y, true);
    final double[] sigmasY = objAns1Y.getVolatilities();
    final double[] relativePartialForwardsY = objAns1Y.getRelativeForwards();

    for (int i = 0; i < nNorms; ++i) {

      assertEquals(weights[i], inWeights[i], Math.abs((inWeights[0] + inWeights[1]) / 2.) * 1e-9);
    }
    for (int i = 0; i < nNorms; ++i) {

      assertEquals(sigmasX[i], inSigmasX[i], Math.abs((inSigmasX[0] + inSigmasX[1]) / 2.) * 1e-9);
    }
    for (int i = 0; i < nNorms; ++i) {

      assertEquals(sigmasY[i], inSigmasY[i], Math.abs((inSigmasY[0] + inSigmasY[1]) / 2.) * 1e-9);
    }
    for (int i = 0; i < nNorms; ++i) {
      assertEquals(relativePartialForwardsX[i], inRelativePartialForwardsX[i], Math.abs((inRelativePartialForwardsX[0] + inRelativePartialForwardsX[1]) / 2.) * 1e-11);
    }
    for (int i = 0; i < nNorms; ++i) {
      assertEquals(relativePartialForwardsY[i], inRelativePartialForwardsY[i], Math.abs((inRelativePartialForwardsY[0] + inRelativePartialForwardsY[1]) / 2.) * 1e-9);
    }

    final double[] ansVolsX = new double[100];
    final double[] ansVolsY = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdX * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      ansVolsX[i] = volfunc.getVolatility(option, fwdX, objAns1X);
      ansVolsY[i] = volfunc.getVolatility(option, fwdY, objAns1Y);
    }

    final double[] trueVolsX = new double[100];
    final double[] trueVolsY = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdY * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      trueVolsX[i] = volfunc.getVolatility(option, fwdX, inObjX);
      trueVolsY[i] = volfunc.getVolatility(option, fwdY, inObjY);
    }

    for (int i = 0; i < 100; i++) {
      assertEquals(ansVolsX[i], trueVolsX[i], Math.abs((inSigmasX[0] + inSigmasX[1]) / 2.) * 1e-8);
      assertEquals(ansVolsY[i], trueVolsY[i], Math.abs((inSigmasY[0] + inSigmasY[1]) / 2.) * 1e-8);
 
View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

      aaX[i + 2 * nNorms - 1] = aaGuess2[i + 3 * nNorms - 1];
    }

    final MixedLogNormalModelData objAns1 = new MixedLogNormalModelData(aaGuessX, true);
    final MixedLogNormalModelData objAns2 = new MixedLogNormalModelData(aaX, true);
    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();

    for (int i = 0; i < 100; i++) {
      final double k = fwdX * (0.5 + 1. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      final double vol2 = volfunc.getVolatility(option, fwdX, objAns1);
      final double vol3 = volfunc.getVolatility(option, fwdX, objAns2);
      System.out.println(k + "\t" + vol2 + "\t" + vol3 + "\t" + yy[i]);
    }

  }
View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

    final MixedLogNormalModelData inObjX = new MixedLogNormalModelData(inWeights, inSigmasX, inRelativePartialForwardsX);
    final MixedLogNormalModelData inObjY = new MixedLogNormalModelData(inWeights, inSigmasY, inRelativePartialForwardsY);

    xx = new double[] {0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8, 0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8 };

    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();
    Arrays.fill(yy, 0.);

    for (int j = 0; j < nDataPtsX; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdX, inObjX);
    }

    for (int j = nDataPtsX; j < nDataPts; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdY, inObjY);
    }

    rhos[0] = 0.2;
    rhos[1] = 0.9;

    final MixedBivariateLogNormalModelVolatility objTrueZ = new MixedBivariateLogNormalModelVolatility(inWeights, inSigmasX,
        inSigmasY, inRelativePartialForwardsX, inRelativePartialForwardsY, rhos);

    final double[] inSigmasZ = objTrueZ.getSigmasZ();
    final double[] inRelativePartialForwardsZ = objTrueZ.getRelativeForwardsZ();

    System.out.println("true values: " + inSigmasX[0] + "\t" + inSigmasX[1]);
    System.out.println("\n");

    for (int i = 0; i < nParams; ++i) {
      aaGuess1[i] = 1e-2 + obj.nextDouble();
    }
    MixedBivariateLogNormalFitter fitter1 = new MixedBivariateLogNormalFitter();

    boolean fitDone = false;

    while (fitDone == false) {

      for (int i = 0; i < nNorms; ++i) {
        aaGuess1X[i] = aaGuess1[i];
        aaGuess1Y[i] = aaGuess1[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
        aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
      }

      MixedLogNormalModelData tmpObj1X = new MixedLogNormalModelData(aaGuess1X, true);
      double[] tmpSigmasX = tmpObj1X.getVolatilities();

      System.out.println("guess: " + tmpSigmasX[0] + "\t" + tmpSigmasX[1]);
      fitter1.doFit(aaGuess1, xx, yy, time, fwdX, fwdY, nNorms, nDataPtsX, 1);
      aaGuess1 = fitter1.getParams();
      System.out.println("inintial sq: " + fitter1.getInitialSq());

      for (int i = 0; i < nNorms; ++i) {
        aaGuess1X[i] = aaGuess1[i];
        aaGuess1Y[i] = aaGuess1[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
        aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
      }

      tmpObj1X = new MixedLogNormalModelData(aaGuess1X, true);
      tmpSigmasX = tmpObj1X.getVolatilities();

      System.out.println("answer: " + tmpSigmasX[0] + "\t" + tmpSigmasX[1]);
      System.out.println("sq: " + fitter1.getFinalSq());
      System.out.println("\n");

      if (fitter1.getFinalSq() <= fitter1.getInitialSq() * 1e-10) {
        fitDone = true;
      } else {
        for (int i = 0; i < nParams; ++i) {
          aaGuess1[i] = 1e-2 + obj.nextDouble();
        }
        fitter1 = new MixedBivariateLogNormalFitter();
      }

    }

    for (int i = 0; i < nNorms; ++i) {
      aaGuess1X[i] = aaGuess1[i];
      aaGuess1Y[i] = aaGuess1[i + nNorms];
    }
    for (int i = 0; i < nNorms - 1; ++i) {
      aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
      aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
    }

    final MixedLogNormalModelData objAns1X = new MixedLogNormalModelData(aaGuess1X, true);
    final double[] weights = objAns1X.getWeights();
    final double[] sigmasX = objAns1X.getVolatilities();
    final double[] relativePartialForwardsX = objAns1X.getRelativeForwards();

    final MixedLogNormalModelData objAns1Y = new MixedLogNormalModelData(aaGuess1Y, true);
    final double[] sigmasY = objAns1Y.getVolatilities();
    final double[] relativePartialForwardsY = objAns1Y.getRelativeForwards();

    final MixedBivariateLogNormalModelVolatility objZ = new MixedBivariateLogNormalModelVolatility(weights, sigmasX,
        sigmasY, relativePartialForwardsX, relativePartialForwardsY, rhos);

    final double[] sigmasZ = objZ.getSigmasZ();
    final double[] relativePartialForwardsZ = objZ.getRelativeForwardsZ();

    System.out.println("Parameters XY");
    for (int i = 0; i < nNorms; ++i) {

      System.out.println(weights[i] + "\t" + inWeights[i]);
      assertEquals(weights[i], inWeights[i], Math.abs((inWeights[0] + inWeights[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(sigmasX[i] + "\t" + inSigmasX[i]);
      assertEquals(sigmasX[i], inSigmasX[i], Math.abs((inSigmasX[0] + inSigmasX[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(sigmasY[i] + "\t" + inSigmasY[i]);
      assertEquals(sigmasY[i], inSigmasY[i], Math.abs((inSigmasY[0] + inSigmasY[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(relativePartialForwardsX[i] + "\t" + inRelativePartialForwardsX[i]);
      assertEquals(relativePartialForwardsX[i], inRelativePartialForwardsX[i], Math.abs((inRelativePartialForwardsX[0] + inRelativePartialForwardsX[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(relativePartialForwardsY[i] + "\t" + inRelativePartialForwardsY[i]);
      assertEquals(relativePartialForwardsY[i], inRelativePartialForwardsY[i], Math.abs((inRelativePartialForwardsY[0] + inRelativePartialForwardsY[1]) / 2.) * 1e-6);
    }

    System.out.println("\n");
    System.out.println("Parameters Z");

    for (int i = 0; i < nNorms; ++i) {
      System.out.println(sigmasZ[i] + "\t" + inSigmasZ[i]);
      assertEquals(sigmasZ[i], inSigmasZ[i], Math.abs((inSigmasZ[0] + inSigmasZ[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(relativePartialForwardsZ[i] + "\t" + inRelativePartialForwardsZ[i]);
      assertEquals(relativePartialForwardsZ[i], inRelativePartialForwardsZ[i], Math.abs((inRelativePartialForwardsZ[0] + inRelativePartialForwardsZ[1]) / 2.) * 1e-6);
    }

    System.out.println("\n");
    System.out.println("Imp Vols XYZ from fitting");

    final double[] ansVolsX = new double[100];
    final double[] ansVolsY = new double[100];
    final double[] ansVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdX * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      ansVolsX[i] = volfunc.getVolatility(option, fwdX, objAns1X);
      ansVolsY[i] = volfunc.getVolatility(option, fwdY, objAns1Y);
      ansVolsZ[i] = objZ.getImpliedVolatilityZ(option, fwdZ);
      System.out.println(k + "\t" + ansVolsX[i] + "\t" + ansVolsY[i] + "\t" + ansVolsZ[i]);
    }

    System.out.println("\n");
    System.out.println("True Imp Vols XYZ");

    final double[] trueVolsX = new double[100];
    final double[] trueVolsY = new double[100];
    final double[] trueVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdY * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      trueVolsX[i] = volfunc.getVolatility(option, fwdX, inObjX);
      trueVolsY[i] = volfunc.getVolatility(option, fwdY, inObjY);
      trueVolsZ[i] = objTrueZ.getImpliedVolatilityZ(option, fwdZ);
      System.out.println(k + "\t" + trueVolsX[i] + "\t" + trueVolsY[i] + "\t" + trueVolsZ[i]);

    }

View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

    final MixedLogNormalModelData inObjX = new MixedLogNormalModelData(inWeights, inSigmasX, inRelativePartialForwardsX);
    final MixedLogNormalModelData inObjY = new MixedLogNormalModelData(inWeights, inSigmasY, inRelativePartialForwardsY);

    xx = new double[] {0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8, 0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8 };

    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();
    Arrays.fill(yy, 0.);

    for (int j = 0; j < nDataPtsX; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdX, inObjX);
    }

    for (int j = nDataPtsX; j < nDataPts; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdY, inObjY);
    }

    for (int i = 0; i < nNorms; ++i) {
      rhos[i] = 0.3 * (i + 1.);
    }
View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

    final MixedLogNormalModelData inObjX = new MixedLogNormalModelData(inWeights, inSigmasX, inRelativePartialForwardsX);
    final MixedLogNormalModelData inObjY = new MixedLogNormalModelData(inWeights, inSigmasY, inRelativePartialForwardsY);

    final double[] xx = new double[] {0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8, 0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8 };

    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();
    Arrays.fill(yy, 0.);

    for (int j = 0; j < nDataPtsX; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdX, inObjX);
    }

    for (int j = nDataPtsX; j < nDataPts; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdY, inObjY);
    }

    for (int i = 0; i < nNorms; ++i) {
      rhos[i] = 0.2 * (0.5 * i + 1.);
    }

    System.out.println("true values: " + inSigmasX[0] + "\t" + inSigmasX[1]);
    System.out.println("\n");

    for (int i = 0; i < nParams; ++i) {
      aaGuess1[i] = 1e-2 + obj.nextDouble();
    }
    MixedBivariateLogNormalFitter fitter1 = new MixedBivariateLogNormalFitter();

    boolean fitDone = false;

    while (fitDone == false) {

      for (int i = 0; i < nNorms; ++i) {
        aaGuess1X[i] = aaGuess1[i];
        aaGuess1Y[i] = aaGuess1[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
        aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
      }

      MixedLogNormalModelData tmpObj1X = new MixedLogNormalModelData(aaGuess1X, true);
      double[] tmpSigmasX = tmpObj1X.getVolatilities();

      System.out.println("guess: " + tmpSigmasX[0] + "\t" + tmpSigmasX[1]);
      fitter1.doFit(aaGuess1, xx, yy, time, fwdX, fwdY, nNorms, nDataPtsX, 1);
      aaGuess1 = fitter1.getParams();
      System.out.println("inintial sq: " + fitter1.getInitialSq());

      for (int i = 0; i < nNorms; ++i) {
        aaGuess1X[i] = aaGuess1[i];
        aaGuess1Y[i] = aaGuess1[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
        aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
      }

      tmpObj1X = new MixedLogNormalModelData(aaGuess1X, true);
      tmpSigmasX = tmpObj1X.getVolatilities();

      if (fitter1.getFinalSq() <= fitter1.getInitialSq() * 1e-14) {
        fitDone = true;
        System.out.println("\n");
        System.out.println("answer: " + tmpSigmasX[0] + "\t" + tmpSigmasX[1]);
        System.out.println("sq: " + fitter1.getFinalSq());
        System.out.println("\n");
        System.out.println("\n");
      } else {
        for (int i = 0; i < nParams; ++i) {
          aaGuess1[i] = 1e-2 + obj.nextDouble();
        }
        fitter1 = new MixedBivariateLogNormalFitter();
      }

    }

    for (int i = 0; i < nNorms; ++i) {
      aaGuess1X[i] = aaGuess1[i];
      aaGuess1Y[i] = aaGuess1[i + nNorms];
    }
    for (int i = 0; i < nNorms - 1; ++i) {
      aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
      aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
    }

    final MixedLogNormalModelData objAns1X = new MixedLogNormalModelData(aaGuess1X, true);
    final double[] weights = objAns1X.getWeights();
    final double[] sigmasX = objAns1X.getVolatilities();
    final double[] relativePartialForwardsX = objAns1X.getRelativeForwards();

    final MixedLogNormalModelData objAns1Y = new MixedLogNormalModelData(aaGuess1Y, true);
    final double[] sigmasY = objAns1Y.getVolatilities();
    final double[] relativePartialForwardsY = objAns1Y.getRelativeForwards();

    final MixedBivariateLogNormalModelVolatility objZ = new MixedBivariateLogNormalModelVolatility(weights, sigmasX,
        sigmasY, relativePartialForwardsX, relativePartialForwardsY, rhos);

    final double[] ansVolsX = new double[100];
    final double[] ansVolsY = new double[100];
    final double[] ansVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdX * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      ansVolsX[i] = volfunc.getVolatility(option, fwdX, objAns1X);
      ansVolsY[i] = volfunc.getVolatility(option, fwdY, objAns1Y);
      ansVolsZ[i] = objZ.getImpliedVolatilityZ(option, fwdZ);
      //   System.out.println(k + "\t" + ansVolsX[i] + "\t" + ansVolsY[i] + "\t" + ansVolsZ[i]);
    }

    final MixedBivariateLogNormalModelVolatility objTrueZ = new MixedBivariateLogNormalModelVolatility(inWeights, inSigmasX,
        inSigmasY, inRelativePartialForwardsX, inRelativePartialForwardsY, rhos);

    final double[] trueVolsX = new double[100];
    final double[] trueVolsY = new double[100];
    final double[] trueVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdY * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      trueVolsX[i] = volfunc.getVolatility(option, fwdX, inObjX);
      trueVolsY[i] = volfunc.getVolatility(option, fwdY, inObjY);
      trueVolsZ[i] = objTrueZ.getImpliedVolatilityZ(option, fwdZ);
      //     System.out.println(k + "\t" + trueVolsX[i] + "\t" + trueVolsY[i] + "\t" + trueVolsZ[i]);

    }

View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

    final MixedLogNormalModelData inObjX = new MixedLogNormalModelData(inWeights, inSigmasX, inRelativePartialForwardsX);
    final MixedLogNormalModelData inObjY = new MixedLogNormalModelData(inWeights, inSigmasY, inRelativePartialForwardsY);

    xx = new double[] {0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8, 0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8 };

    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();
    Arrays.fill(yy, 0.);

    for (int j = 0; j < nDataPtsX; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdX, inObjX);
    }

    for (int j = nDataPtsX; j < nDataPts; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      yy[j] = volfunc.getVolatility(option, fwdY, inObjY);
    }

    for (int i = 0; i < nNorms; ++i) {
      rhos[i] = 0.2 * (0.5 * i + 1.);
    }

    //   System.out.println("true values: " + inSigmasX[0] + "\t" + inSigmasX[1]);
    //  System.out.println("\n");

    for (int i = 0; i < nParams; ++i) {
      aaGuess1[i] = 1e-2 + obj.nextDouble();
    }
    MixedBivariateLogNormalFitter fitter1 = new MixedBivariateLogNormalFitter();

    boolean fitDone = false;

    while (fitDone == false) {

      for (int i = 0; i < nNorms; ++i) {
        aaGuess1X[i] = aaGuess1[i];
        aaGuess1Y[i] = aaGuess1[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
        aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
      }

      //  MixedLogNormalModelData tmpObj1X = new MixedLogNormalModelData(aaGuess1X, true);
      //double[] tmpSigmasX = tmpObj1X.getVolatilities();

      // System.out.println("guess: " + tmpSigmasX[0] + "\t" + tmpSigmasX[1]);
      fitter1.doFit(aaGuess1, xx, yy, time, fwdX, fwdY, nNorms, nDataPtsX, 1.);
      aaGuess1 = fitter1.getParams();
      System.out.println("inintial sq: " + fitter1.getInitialSq());
      System.out.println("final sq: " + fitter1.getFinalSq());
      System.out.println("\n");

      for (int i = 0; i < nNorms; ++i) {
        aaGuess1X[i] = aaGuess1[i];
        aaGuess1Y[i] = aaGuess1[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
        aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
      }

      //   tmpObj1X = new MixedLogNormalModelData(aaGuess1X, true);
      //  tmpSigmasX = tmpObj1X.getVolatilities();

      if (fitter1.getFinalSq() <= fitter1.getInitialSq() * 1e-14) {
        fitDone = true;
        //System.out.println("\n");
        //System.out.println("answer: " + tmpSigmasX[0] + "\t" + tmpSigmasX[1]);

        System.out.println("XY Fitting Done");
        System.out.println("\n");
      } else {
        for (int i = 0; i < nParams; ++i) {
          aaGuess1[i] = 1e-2 + obj.nextDouble();
        }
        fitter1 = new MixedBivariateLogNormalFitter();
      }

    }

    for (int i = 0; i < nNorms; ++i) {
      aaGuess1X[i] = aaGuess1[i];
      aaGuess1Y[i] = aaGuess1[i + nNorms];
    }
    for (int i = 0; i < nNorms - 1; ++i) {
      aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
      aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
    }

    final MixedLogNormalModelData objAns1X = new MixedLogNormalModelData(aaGuess1X, true);
    final double[] weights = objAns1X.getWeights();
    final double[] sigmasX = objAns1X.getVolatilities();
    final double[] relativePartialForwardsX = objAns1X.getRelativeForwards();

    final MixedLogNormalModelData objAns1Y = new MixedLogNormalModelData(aaGuess1Y, true);
    final double[] sigmasY = objAns1Y.getVolatilities();
    final double[] relativePartialForwardsY = objAns1Y.getRelativeForwards();

    final MixedBivariateLogNormalModelVolatility objTrueZ = new MixedBivariateLogNormalModelVolatility(inWeights, inSigmasX,
        inSigmasY, inRelativePartialForwardsX, inRelativePartialForwardsY, rhos);

    final double[] xxZ = new double[nDataPtsX];
    final double[] yyZ = new double[nDataPtsX];
    for (int j = 0; j < nDataPtsX; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      xxZ[j] = xx[j];
      yyZ[j] = objTrueZ.getImpliedVolatilityZ(option, fwdZ);
    }

    double[] rhosGuess = new double[nNorms];
    for (int i = 0; i < nNorms; ++i) {
      rhosGuess[i] = 1. - obj.nextDouble();
    }

    MixedBivariateLogNormalCorrelationFinder fitter = new MixedBivariateLogNormalCorrelationFinder();

    boolean fitRhoDone = false;
    int counterRho = 0;

    while (fitRhoDone == false) {
      ++counterRho;

      fitter.doFit(rhosGuess, xxZ, yyZ, time, weights, sigmasX, sigmasY,
          relativePartialForwardsX,
          relativePartialForwardsY, fwdX, fwdY);
      rhosGuess = fitter.getParams();
      System.out.println("\n");
      System.out.println("inintial sq: " + fitter.getInitialSq());

      System.out.println("final sq: " + fitter.getFinalSq());
      System.out.println("\n");

      if (fitter.getFinalSq() <= 1e-14) {
        fitRhoDone = true;
        System.out.println("Rho Fitting Done");
      } else {
        for (int i = 0; i < nNorms; ++i) {
          rhosGuess[i] = 1. - obj.nextDouble();
        }
        fitter = new MixedBivariateLogNormalCorrelationFinder();
      }

      ArgumentChecker.isTrue(counterRho < 500, "Too many inerations for rho. Start with new guess parameters.");
    }

    System.out.println("\n");

    rhosGuess = fitter.getParams();

    final MixedBivariateLogNormalModelVolatility objZ = new MixedBivariateLogNormalModelVolatility(weights, sigmasX,
        sigmasY, relativePartialForwardsX, relativePartialForwardsY, rhosGuess);

    final double[] ansVolsX = new double[100];
    final double[] ansVolsY = new double[100];
    final double[] ansVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdX * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      ansVolsX[i] = volfunc.getVolatility(option, fwdX, objAns1X);
      ansVolsY[i] = volfunc.getVolatility(option, fwdY, objAns1Y);
      ansVolsZ[i] = objZ.getImpliedVolatilityZ(option, fwdZ);
      //   System.out.println(k + "\t" + ansVolsX[i] + "\t" + ansVolsY[i] + "\t" + ansVolsZ[i]);
    }

    final double[] trueVolsX = new double[100];
    final double[] trueVolsY = new double[100];
    final double[] trueVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdY * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      trueVolsX[i] = volfunc.getVolatility(option, fwdX, inObjX);
      trueVolsY[i] = volfunc.getVolatility(option, fwdY, inObjY);
      trueVolsZ[i] = objTrueZ.getImpliedVolatilityZ(option, fwdZ);
      //     System.out.println(k + "\t" + trueVolsX[i] + "\t" + trueVolsY[i] + "\t" + trueVolsZ[i]);

    }

View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

    final MixedLogNormalModelData inObjX = new MixedLogNormalModelData(inWeights, inSigmasX, inRelativePartialForwardsX);
    final MixedLogNormalModelData inObjY = new MixedLogNormalModelData(inWeights, inSigmasY, inRelativePartialForwardsY);

    final double[] xx = new double[] {0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8, 0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8 };

    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();
    Arrays.fill(yy, 0.);

    for (int j = 0; j < nDataPtsX; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      final double[] tmpRandNorm = objRandNorm.getVector(1);
      yyNoRand[j] = volfunc.getVolatility(option, fwdX, inObjX);
      yy[j] = yyNoRand[j] * (1. + tmpRandNorm[0]);
    }

    for (int j = nDataPtsX; j < nDataPts; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(xx[j], time, true);
      final double[] tmpRandNorm = objRandNorm.getVector(1);
      yyNoRand[j] = volfunc.getVolatility(option, fwdY, inObjY);
      yy[j] = yyNoRand[j] * (1. + tmpRandNorm[0]);
    }

    for (int j = 0; j < nDataPts; ++j) {
      System.out.println(yy[j] + "\t" + yyNoRand[j]);
    }

    final double[] rhos = new double[] {0.4, 0.4 };

    final MixedBivariateLogNormalModelVolatility objTrueZ = new MixedBivariateLogNormalModelVolatility(inWeights, inSigmasX,
        inSigmasY, inRelativePartialForwardsX, inRelativePartialForwardsY, rhos);

    //    final double[] inSigmasZ = objTrueZ.getSigmasZ();
    //    final double[] inRelativePartialForwardsZ = objTrueZ.getRelativeForwardsZ();

    System.out.println("true values: " + inSigmasX[0] + "\t" + inSigmasX[1]);
    System.out.println("\n");

    for (int i = 0; i < nParams; ++i) {
      aaGuess1[i] = 1e-2 + objRand.nextDouble();
    }
    MixedBivariateLogNormalFitter fitter1 = new MixedBivariateLogNormalFitter();

    boolean fitDone = false;

    while (fitDone == false) {

      for (int i = 0; i < nNorms; ++i) {
        aaGuess1X[i] = aaGuess1[i];
        aaGuess1Y[i] = aaGuess1[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
        aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
      }

      MixedLogNormalModelData tmpObj1X = new MixedLogNormalModelData(aaGuess1X, true);
      double[] tmpSigmasX = tmpObj1X.getVolatilities();

      System.out.println("guess: " + tmpSigmasX[0] + "\t" + tmpSigmasX[1]);
      fitter1.doFit(aaGuess1, xx, yy, time, fwdX, fwdY, nNorms, nDataPtsX, 1);
      aaGuess1 = fitter1.getParams();
      System.out.println("inintial sq: " + fitter1.getInitialSq());

      for (int i = 0; i < nNorms; ++i) {
        aaGuess1X[i] = aaGuess1[i];
        aaGuess1Y[i] = aaGuess1[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
        aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
        aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
      }

      tmpObj1X = new MixedLogNormalModelData(aaGuess1X, true);
      tmpSigmasX = tmpObj1X.getVolatilities();

      System.out.println("answer: " + tmpSigmasX[0] + "\t" + tmpSigmasX[1]);
      System.out.println("sq: " + fitter1.getFinalSq());
      System.out.println("\n");

      if (fitter1.getFinalSq() <= fitter1.getInitialSq() * 1e-6) {
        fitDone = true;
      } else {
        for (int i = 0; i < nParams; ++i) {
          aaGuess1[i] = 1e-2 + objRand.nextDouble();
        }
        fitter1 = new MixedBivariateLogNormalFitter();
      }

    }

    for (int i = 0; i < nNorms; ++i) {
      aaGuess1X[i] = aaGuess1[i];
      aaGuess1Y[i] = aaGuess1[i + nNorms];
    }
    for (int i = 0; i < nNorms - 1; ++i) {
      aaGuess1X[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1X[i + 2 * nNorms - 1] = aaGuess1[i + 3 * nNorms - 1];
      aaGuess1Y[i + nNorms] = aaGuess1[i + 2 * nNorms];
      aaGuess1Y[i + 2 * nNorms - 1] = aaGuess1[i + 4 * nNorms - 2];
    }

    final MixedLogNormalModelData objAns1X = new MixedLogNormalModelData(aaGuess1X, true);
    final double[] weights = objAns1X.getWeights();
    final double[] sigmasX = objAns1X.getVolatilities();
    final double[] relativePartialForwardsX = objAns1X.getRelativeForwards();

    final MixedLogNormalModelData objAns1Y = new MixedLogNormalModelData(aaGuess1Y, true);
    final double[] sigmasY = objAns1Y.getVolatilities();
    final double[] relativePartialForwardsY = objAns1Y.getRelativeForwards();

    final MixedBivariateLogNormalModelVolatility objZ = new MixedBivariateLogNormalModelVolatility(weights, sigmasX,
        sigmasY, relativePartialForwardsX, relativePartialForwardsY, rhos);

    //    final double[] sigmasZ = objZ.getSigmasZ();
    //    final double[] relativePartialForwardsZ = objZ.getRelativeForwardsZ();

    System.out.println("Parameters XY");
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(weights[i] + "\t" + inWeights[i]);
      //   assertEquals("weights ", weights[i], inWeights[i], Math.abs((inWeights[0] + inWeights[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(sigmasX[i] + "\t" + inSigmasX[i]);
      //    assertEquals("sigmasY ", sigmasX[i], inSigmasX[i], Math.abs((inSigmasX[0] + inSigmasX[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(sigmasY[i] + "\t" + inSigmasY[i]);
      //    assertEquals("sigmasY ", sigmasY[i], inSigmasY[i], Math.abs((inSigmasY[0] + inSigmasY[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(relativePartialForwardsX[i] + "\t" + inRelativePartialForwardsX[i]);
      //     assertEquals("relativePartialForwardsX ", relativePartialForwardsX[i], inRelativePartialForwardsX[i], Math.abs((inRelativePartialForwardsX[0] + inRelativePartialForwardsX[1]) / 2.) * 1e-6);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(relativePartialForwardsY[i] + "\t" + inRelativePartialForwardsY[i]);
      //    assertEquals("relativePartialForwardsY ", relativePartialForwardsY[i], inRelativePartialForwardsY[i], Math.abs((inRelativePartialForwardsY[0] + inRelativePartialForwardsY[1]) / 2.) * 1e-6);
    }
    //
    //    System.out.println("\n");
    //    System.out.println("Parameters Z");
    //
    //    for (int i = 0; i < nNorms; ++i) {
    //      System.out.println(sigmasZ[i] + "\t" + inSigmasZ[i]);
    //      assertEquals("sigmasZ ", sigmasZ[i], inSigmasZ[i], Math.abs((inSigmasZ[0] + inSigmasZ[1]) / 2.) * 1e-6);
    //    }
    //    for (int i = 0; i < nNorms; ++i) {
    //      System.out.println(relativePartialForwardsZ[i] + "\t" + inRelativePartialForwardsZ[i]);
    //      assertEquals("relativePartialForwardsZ ", relativePartialForwardsZ[i], inRelativePartialForwardsZ[i], Math.abs((inRelativePartialForwardsZ[0] + inRelativePartialForwardsZ[1]) / 2.) * 1e-6);
    //    }

    System.out.println("\n");
    System.out.println("Imp Vols XYZ from fitting");

    final double[] ansVolsX = new double[100];
    final double[] ansVolsY = new double[100];
    final double[] ansVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdX * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      ansVolsX[i] = volfunc.getVolatility(option, fwdX, objAns1X);
      ansVolsY[i] = volfunc.getVolatility(option, fwdY, objAns1Y);
      ansVolsZ[i] = objZ.getImpliedVolatilityZ(option, fwdZ);
      System.out.println(k + "\t" + ansVolsX[i] + "\t" + ansVolsY[i] + "\t" + ansVolsZ[i]);
    }

    System.out.println("\n");
    System.out.println("True Imp Vols XYZ");

    final double[] trueVolsX = new double[100];
    final double[] trueVolsY = new double[100];
    final double[] trueVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdY * (0.1 + 2. * i / 100.);
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      trueVolsX[i] = volfunc.getVolatility(option, fwdX, inObjX);
      trueVolsY[i] = volfunc.getVolatility(option, fwdY, inObjY);
      trueVolsZ[i] = objTrueZ.getImpliedVolatilityZ(option, fwdZ);
      System.out.println(k + "\t" + trueVolsX[i] + "\t" + trueVolsY[i] + "\t" + trueVolsZ[i]);

    }

View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

      xx[j + nDataPtsX] = strikeGBPUSD[choiceOfExpiry][j];
      yy[j] = volEURUSD[choiceOfExpiry][j];
      yy[j + nDataPtsX] = volGBPUSD[choiceOfExpiry][j];
    }

    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();

    for (int i = 0; i < nParams; ++i) {
      aaGuess[i] = (choiceOfExpiry + 1.) * (1e-2 + objRand.nextDouble());
    }
    final double aaGuessFactor = choiceOfExpiry + 1.;
    MixedBivariateLogNormalFitter fitter1 = new MixedBivariateLogNormalFitter();

    final double prec = 1e-6; // Precision should be chosen depending on maturities

    boolean fitDone = false;
    int counter = 0;

    while (fitDone == false) {

      ++counter;

      for (int i = 0; i < nNorms; ++i) {
        aaGuessX[i] = aaGuess[i];
        aaGuessY[i] = aaGuess[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuessX[i + nNorms] = aaGuess[i + 2 * nNorms];
        aaGuessX[i + 2 * nNorms - 1] = aaGuess[i + 3 * nNorms - 1];
        aaGuessY[i + nNorms] = aaGuess[i + 2 * nNorms];
        aaGuessY[i + 2 * nNorms - 1] = aaGuess[i + 4 * nNorms - 2];
      }

      fitter1.doFit(aaGuess, xx, yy, time, fwdX, fwdY, nNorms, nDataPtsX, aaGuessFactor);
      aaGuess = fitter1.getParams();
      System.out.println("XYinintial sq: " + fitter1.getInitialSq());

      for (int i = 0; i < nNorms; ++i) {
        aaGuessX[i] = aaGuess[i];
        aaGuessY[i] = aaGuess[i + nNorms];
      }
      for (int i = 0; i < nNorms - 1; ++i) {
        aaGuessX[i + nNorms] = aaGuess[i + 2 * nNorms];
        aaGuessX[i + 2 * nNorms - 1] = aaGuess[i + 3 * nNorms - 1];
        aaGuessY[i + nNorms] = aaGuess[i + 2 * nNorms];
        aaGuessY[i + 2 * nNorms - 1] = aaGuess[i + 4 * nNorms - 2];
      }

      System.out.println("XYsq: " + fitter1.getFinalSq());
      System.out.println("\n");

      if (fitter1.getFinalSq() <= prec) {
        fitDone = true;
        System.out.println("XY Fitting Done");
      } else {
        for (int i = 0; i < nParams; ++i) {
          aaGuess[i] = (choiceOfExpiry + 1.) * (1e-2 + objRand.nextDouble());
        }
        fitter1 = new MixedBivariateLogNormalFitter();
      }

      ArgumentChecker.isTrue(counter < 500, "Reduce the precision.");
    }

    for (int i = 0; i < nNorms; ++i) {
      aaGuessX[i] = aaGuess[i];
      aaGuessY[i] = aaGuess[i + nNorms];
    }
    for (int i = 0; i < nNorms - 1; ++i) {
      aaGuessX[i + nNorms] = aaGuess[i + 2 * nNorms];
      aaGuessX[i + 2 * nNorms - 1] = aaGuess[i + 3 * nNorms - 1];
      aaGuessY[i + nNorms] = aaGuess[i + 2 * nNorms];
      aaGuessY[i + 2 * nNorms - 1] = aaGuess[i + 4 * nNorms - 2];
    }

    final MixedLogNormalModelData objAns1X = new MixedLogNormalModelData(aaGuessX, true);
    final double[] weights = objAns1X.getWeights();
    final double[] sigmasX = objAns1X.getVolatilities();
    final double[] relativePartialForwardsX = objAns1X.getRelativeForwards();

    final MixedLogNormalModelData objAns1Y = new MixedLogNormalModelData(aaGuessY, true);
    final double[] sigmasY = objAns1Y.getVolatilities();
    final double[] relativePartialForwardsY = objAns1Y.getRelativeForwards();

    final Random randObj = new Random();

    for (int i = 0; i < nNorms; ++i) {
      rhosGuess[i] = 1. - randObj.nextDouble();
    }

    MixedBivariateLogNormalCorrelationFinder fitter = new MixedBivariateLogNormalCorrelationFinder();

    boolean fitRhoDone = false;
    int counterRho = 0;

    while (fitRhoDone == false) {
      ++counterRho;

      fitter.doFit(rhosGuess, strikeEURGBP[choiceOfExpiry], volEURGBP[choiceOfExpiry], time, weights, sigmasX, sigmasY,
          relativePartialForwardsX,
          relativePartialForwardsY, fwdX, fwdY);
      rhosGuess = fitter.getParams();
      System.out.println("\n");
      System.out.println("inintial sq: " + fitter.getInitialSq());

      System.out.println("final sq: " + fitter.getFinalSq());
      System.out.println("\n");

      if (fitter.getFinalSq() <= prec * 1e2) {
        fitRhoDone = true;
        System.out.println("Rho Fitting Done");
      } else {
        for (int i = 0; i < nNorms; ++i) {
          rhosGuess[i] = 1. - randObj.nextDouble();
        }
        fitter = new MixedBivariateLogNormalCorrelationFinder();
      }

      ArgumentChecker.isTrue(counterRho < 500, "Too many inerations for rho. Start with new guess parameters.");
    }

    System.out.println("\n");

    rhosGuess = fitter.getParams();

    final MixedBivariateLogNormalModelVolatility objZ = new MixedBivariateLogNormalModelVolatility(weights, sigmasX,
        sigmasY, relativePartialForwardsX, relativePartialForwardsY, rhosGuess);

    final double[] sigmasZ = objZ.getSigmasZ();
    final double[] relativePartialForwardsZ = objZ.getRelativeForwardsZ();

    System.out.println("Parameters XY(weights, sigmasX, sigmasY, relfwdX, relrwdY)");
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(weights[i]);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(sigmasX[i]);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(sigmasY[i]);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(relativePartialForwardsX[i]);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(relativePartialForwardsY[i]);
    }

    System.out.println("\n");
    System.out.println("fwdX: " + "\t" + fwdX);
    System.out.println("fwdY: " + "\t" + fwdY);

    System.out.println("\n");
    System.out.println("rhos");
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(rhosGuess[i]);
    }

    System.out.println("\n");
    System.out.println("Parameters Z");

    for (int i = 0; i < nNorms; ++i) {
      System.out.println(sigmasZ[i]);
    }
    for (int i = 0; i < nNorms; ++i) {
      System.out.println(relativePartialForwardsZ[i]);
    }

    System.out.println("\n");
    System.out.println("fwdZ: " + "\t" + fwdZ);

    System.out.println("\n");
    System.out.println("Imp Vols Z from fitting");

    final double[] ansVolsX = new double[100];
    final double[] ansVolsY = new double[100];
    final double[] ansVolsZ = new double[100];
    for (int i = 0; i < 100; i++) {
      final double k = fwdZ * (0.9 + choiceOfExpiry * 0.01 + (.2 - choiceOfExpiry * 0.02) * i / 100.); //The range should be appropriately chosen depending on expiry
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      ansVolsZ[i] = objZ.getImpliedVolatilityZ(option, fwdZ);
      System.out.println(k + "\t" + ansVolsZ[i]);
    }

    System.out.println("\n");
    System.out.println("Imp Vols X from fitting");
    for (int i = 0; i < 100; i++) {
      final double k = fwdX * (0.9 + .2 * i / 100.); //The range should be appropriately chosen depending on expiry
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      ansVolsX[i] = volfunc.getVolatility(option, fwdX, objAns1X);
      System.out.println(k + "\t" + ansVolsX[i]);
    }

    System.out.println("\n");
    System.out.println("Imp Vols Y from fitting");
    for (int i = 0; i < 100; i++) {
      final double k = fwdY * (0.9 + .2 * i / 100.); //The range should be appropriately chosen depending on expiry
      final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
      ansVolsY[i] = volfunc.getVolatility(option, fwdY, objAns1Y);
      System.out.println(k + "\t" + ansVolsY[i]);
    }

    System.out.println("\n");
    System.out.println("EURUSD(X)");
View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

  }

  private double[] getFunctionValues(final double[] params, final double[] dataStrikes, final double forwardX, final double forwardY, final double timeToExpiry, final int nNormals, final int nData,
      final int nDataX) {

    final MixedLogNormalVolatilityFunction volfunc = MixedLogNormalVolatilityFunction.getInstance();

    final int dof = 3 * nNormals - 2;

    final double[] paramsX = new double[dof];
    final double[] paramsY = new double[dof];

    for (int i = 0; i < nNormals; ++i) {
      paramsX[i] = params[i];
    }
    for (int i = 0; i < nNormals - 1; ++i) {
      paramsX[i + nNormals] = params[i + 2 * nNormals];
      paramsX[i + 2 * nNormals - 1] = params[i + 3 * nNormals - 1];
    }

    for (int i = 0; i < nNormals; ++i) {
      paramsY[i] = params[i + nNormals];
    }
    for (int i = 0; i < nNormals - 1; ++i) {
      paramsY[i + nNormals] = params[i + 2 * nNormals];
      paramsY[i + 2 * nNormals - 1] = params[i + 4 * nNormals - 2];
    }

    final MixedLogNormalModelData dataX = new MixedLogNormalModelData(paramsX, true);
    final MixedLogNormalModelData dataY = new MixedLogNormalModelData(paramsY, true);

    final double[] res = new double[nData];
    Arrays.fill(res, 0.);

    for (int j = 0; j < nDataX; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(dataStrikes[j], timeToExpiry, true);
      res[j] = volfunc.getVolatility(option, forwardX, dataX);
    }

    for (int j = nDataX; j < nData; ++j) {
      final EuropeanVanillaOption option = new EuropeanVanillaOption(dataStrikes[j], timeToExpiry, true);
      res[j] = volfunc.getVolatility(option, forwardY, dataY);
    }

    return res;
  }
View Full Code Here

Examples of com.opengamma.analytics.financial.model.volatility.smile.function.MixedLogNormalVolatilityFunction

    final int n = vols.length;
    final double[] errors = new double[n];
    Arrays.fill(errors, 1e-4);
    errors[0] = 1e5;
    errors[n - 1] = 1e5;
    final MixedLogNormalVolatilityFunction model = getModel();

    final MixedLogNormalModelFitter fitter = new MixedLogNormalModelFitter(forward, strikes, expiry, vols, errors, model, 3, true);

    double bestChi2 = Double.POSITIVE_INFINITY;
    LeastSquareResultsWithTransform best = null;
    final int tries = 10;
    int fails = 0;
    for (int i = 0; i < tries; i++) {
      final DoubleMatrix1D start = getRandomStart();
      try {
        final LeastSquareResultsWithTransform res = fitter.solve(start);
        if (res.getChiSq() < bestChi2) {
          bestChi2 = res.getChiSq();
          best = res;
        }
      } catch (final MathException e) {
        fails++;
      } catch (final IllegalArgumentException e) {
        System.out.print(e.toString());
        System.out.println(start);
      }
    }
    System.out.println("fail rate:" + (100.0 * fails) / tries + "%");

    // DoubleMatrix1D start = new DoubleMatrix1D(0.1653806454982462, 0.2981998932366687, 1.298321083180569, 0.16115590666749585);
    //  best = fitter.solve(start);
    if (best != null) {
      System.out.println(best.toString());
      final MixedLogNormalModelData data = new MixedLogNormalModelData(best.getModelParameters().getData());
      System.out.println(data.toString());
      for (int i = 0; i < 200; i++) {
        final double k = 500 + 1700 * i / 199.;

        final EuropeanVanillaOption option = new EuropeanVanillaOption(k, expiry, true);
        final double vol = model.getVolatility(option, forward, data);
        System.out.println(k + "\t" + vol);
      }
    }

    //    BitSet fixed = new BitSet();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.