}
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]);
}
System.out.println("\n");
final double[] kDataSet = {0.5, 0.7, 0.9, 1.0, 1.2, 1.5, 1.8 };
for (final double k : kDataSet) {
final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
final double trueVol = objTrueZ.getImpliedVolatilityZ(option, fwdZ);
System.out.println(k + "\t" + trueVol);
}
System.out.println("\n");
System.out.println("Imp Vols XYZ (1e-6)");
for (int i = 0; i < 100; i++) {
// double k = fwdX * (0.1 + 2. * i / 100.);
assertEquals(ansVolsX[i], trueVolsX[i], Math.abs((inSigmasX[0] + inSigmasX[1]) / 2.) * 1e-6);
assertEquals(ansVolsY[i], trueVolsY[i], Math.abs((inSigmasY[0] + inSigmasY[1]) / 2.) * 1e-6);
assertEquals(ansVolsZ[i], trueVolsZ[i], Math.abs((inSigmasZ[0] + inSigmasZ[1]) / 2.) * 1e-6);
}
System.out.println("\n");
System.out.println("Density X (1e-6)");
final double[] ansDensityX = new double[100];
final double[] ansDensityY = new double[100];
final double[] ansDensityZ = new double[100];
final double[] trueDensityX = new double[100];
final double[] trueDensityY = new double[100];
final double[] trueDensityZ = new double[100];
for (int i = 0; i < 100; i++) {
final double k = fwdX * (0.01 + 2. * i / 100.);
final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
ansDensityX[i] = getDualGamma(option, fwdX, objAns1X);
trueDensityX[i] = getDualGamma(option, fwdX, inObjX);
// System.out.println(k + "\t" + ansDensityX[i] + "\t" + trueDensityX[i]);
assertEquals(ansDensityX[i], trueDensityX[i], 1e-6);
}
System.out.println("\n");
System.out.println("Density Y (1e-6)");
for (int i = 0; i < 100; i++) {
final double k = fwdY * (0.01 + 2. * i / 100.);
final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
ansDensityY[i] = getDualGamma(option, fwdY, objAns1Y);
trueDensityY[i] = getDualGamma(option, fwdY, inObjY);
// System.out.println(k + "\t" + ansDensityY[i] + "\t" + trueDensityY[i]);
assertEquals(ansDensityY[i], trueDensityY[i], 1e-6);
}
System.out.println("\n");
System.out.println("Density Z (1e-6)");
for (int i = 0; i < 100; i++) {
final double k = fwdY * (0.01 + 2. * i / 100.);
final EuropeanVanillaOption option = new EuropeanVanillaOption(k, time, true);
ansDensityZ[i] = getDualGammaZ(option, fwdZ, objZ);
trueDensityZ[i] = getDualGammaZ(option, fwdZ, objTrueZ);
// System.out.println(k + "\t" + ansDensityZ[i] + "\t" + trueDensityZ[i]);
assertEquals(ansDensityZ[i], trueDensityZ[i], 1e-6);
}
System.out.println("\n");
System.out.println("Density(exp)");
for (int i = 0; i < 100; i++) {
final double k = fwdX * (0.1 + 9. * i / 100.);
final double denValueX = getDensity(weights, sigmasX, relativePartialForwardsX, k);
final double denValueY = getDensity(weights, sigmasY, relativePartialForwardsY, k);
final double denValueZ = getDensity(weights, sigmasZ, relativePartialForwardsZ, k);
System.out.println(Math.log(k) + "\t" + denValueX + "\t" + denValueY + "\t" + denValueZ);
}
System.out.println("\n");
System.out.println("Put-Call Parity X (1e-10)");
for (int i = 0; i < 100; i++) {
final double k = fwdY * (0.01 + 2. * i / 100.);
final EuropeanVanillaOption optionCall = new EuropeanVanillaOption(k, time, true);
final EuropeanVanillaOption optionPut = new EuropeanVanillaOption(k, time, false);
final double callPrice = getPrice(optionCall, fwdX, objAns1X);
final double putPrice = getPrice(optionPut, fwdX, objAns1X);
// System.out.println(k + "\t" + (callPrice - putPrice) + "\t" + (fwdX - k));
assertEquals((callPrice - putPrice), (fwdX - k), fwdX * 1e-10);
}
System.out.println("\n");
System.out.println("Put-Call Parity Y (1e-10)");
for (int i = 0; i < 100; i++) {
final double k = fwdY * (0.01 + 2. * i / 100.);
final EuropeanVanillaOption optionCall = new EuropeanVanillaOption(k, time, true);
final EuropeanVanillaOption optionPut = new EuropeanVanillaOption(k, time, false);
final double callPrice = getPrice(optionCall, fwdY, objAns1Y);
final double putPrice = getPrice(optionPut, fwdY, objAns1Y);
// System.out.println(k + "\t" + (callPrice - putPrice) + "\t" + (fwdY - k));
assertEquals((callPrice - putPrice), (fwdY - k), fwdY * 1e-10);
}
System.out.println("\n");
System.out.println("Put-Call Parity Z (1e-8)");
for (int i = 0; i < 100; i++) {
final double k = fwdZ * (0.01 + 2. * i / 100.);
final EuropeanVanillaOption optionCall = new EuropeanVanillaOption(k, time, true);
final EuropeanVanillaOption optionPut = new EuropeanVanillaOption(k, time, false);
final double callPrice = objZ.getPriceZ(optionCall, fwdZ);
final double putPrice = objZ.getPriceZ(optionPut, fwdZ);
// System.out.println(k + "\t" + (callPrice - putPrice) + "\t" + (fwdY - k));
assertEquals((callPrice - putPrice), (fwdY - k), fwdY * 1e-10);
}
}