Example #1
0
        public virtual void test_recoverVolatility()
        {
            int    nSteps       = TREE_DATA.NumberOfSteps;
            double spot         = TREE_DATA.Spot;
            double timeToExpiry = TREE_DATA.getTime(nSteps);
            double dfDom        = RATE_PROVIDER.discountFactors(USD).discountFactor(timeToExpiry);
            double dfFor        = RATE_PROVIDER.discountFactors(EUR).discountFactor(timeToExpiry);
            double forward      = spot * dfFor / dfDom;

            for (int i = 0; i < 100; ++i)
            {
                double         strike     = spot * (0.8 + 0.004 * i);
                OptionFunction func       = EuropeanVanillaOptionFunction.of(strike, timeToExpiry, PutCall.CALL, nSteps);
                double         price      = TREE.optionPrice(func, TREE_DATA);
                double         impliedVol = BlackFormulaRepository.impliedVolatility(price / dfDom, forward, strike, timeToExpiry, true);
                double         orgVol     = VOLS.volatility(FX_PRODUCT.CurrencyPair, timeToExpiry, strike, forward);
                assertEquals(impliedVol, orgVol, orgVol * 0.1);   // large tol
                double priceMrkt      = TREE.optionPrice(func, TREE_DATA_MRKT);
                double impliedVolMrkt = BlackFormulaRepository.impliedVolatility(priceMrkt / dfDom, forward, strike, timeToExpiry, true);
                double orgVolMrkt     = VOLS_MRKT.volatility(FX_PRODUCT.CurrencyPair, timeToExpiry, strike, forward);
                assertEquals(impliedVolMrkt, orgVolMrkt, orgVolMrkt * 0.1);   // large tol
            }
        }