public void GetVolatilityTest1()
        {
            var           target = new SwaptionDataMatrix(_volTenors, _volStrikes, _volExpiry, _volData, _id);
            decimal       actual;
            const decimal expected = 9.83m / 100.0m;
            Period        tenor    = PeriodHelper.Parse("2yr");
            decimal       strike   = 50.0m / 10000.0m;

            try
            {
                actual = target.GetVolatility(tenor, strike);
                Assert.AreEqual(expected, actual);
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("The matrix is malformed. No volatilities are available.", ex.Message);
            }

            tenor  = PeriodHelper.Parse("32yr");
            strike = 50.0m / 10000.0m;

            try
            {
                actual = target.GetVolatility(tenor, strike);
                Assert.AreEqual(expected, actual);
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("The matrix is malformed. No volatilities are available.", ex.Message);
            }
        }
        public void GetVolatilityTest()
        {
            var target = new SwaptionDataMatrix(_volTenors, _volStrikes, _volExpiry, _volData, _id);

            decimal       actual;
            const decimal expected = 10.26m / 100.0m;
            string        tenor    = "2yr";
            decimal       strike   = -0.50m;

            try
            {
                actual = target.GetVolatility(tenor, strike);
                Assert.AreEqual(expected, actual);
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("The matrix is malformed. No volatilities are available.", ex.Message);
            }

            tenor  = "22yr";
            strike = -7.50m;

            try
            {
                actual = target.GetVolatility(tenor, strike);
                Assert.AreEqual(expected, actual);
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("The matrix is malformed. No volatilities are available.", ex.Message);
            }
        }
        public void GetVolatilityTest4()
        {
            var           target   = new SwaptionDataMatrix(_volTenors, _volStrikes, _volExpiry, _volData, _id);
            const decimal expected = 10.26m / 100.0m;

            decimal[] actual;
            Period    tenor = PeriodHelper.Parse("2yr");

            try
            {
                actual = target.GetVolatility(tenor);
                CollectionAssert.Contains(actual, expected);
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("The matrix is malformed. No volatilities are available.", ex.Message);
            }
            tenor = PeriodHelper.Parse("21yr");
            try
            {
                actual = target.GetVolatility(tenor);
                CollectionAssert.Contains(actual, expected);
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("The matrix is malformed. No volatilities are available.", ex.Message);
            }
        }
        public void GetVolatilityTest5()
        {
            var           target   = new SwaptionDataMatrix(_volTenors, _volStrikes, _volExpiry, _volData, _id);
            const decimal expected = 10.26m / 100.0m;
            var           actual   = new decimal[0];
            Period        tenor    = PeriodHelper.Parse("2yr");

            try
            {
                actual = target.GetVolatility(tenor);
                CollectionAssert.Contains(actual, expected);
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("The matrix is malformed. No volatilities are available.", ex.Message);
            }
            var expected1 = new decimal[actual.Length];

            Array.Copy(actual, expected1, actual.Length);
            const string tenor1 = "2yr";

            try
            {
                int i = 0;
                actual = target.GetVolatility(tenor1);
                foreach (decimal d in actual)
                {
                    Assert.AreEqual(expected1[i++], d);
                }
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("The matrix is malformed. No volatilities are available.", ex.Message);
            }
        }
        /// <summary>
        /// Create a full calibration model.This version uses the volatility grid to generate an engine
        /// for each swap tenor (row values)
        /// </summary>
        /// <param name="volatilityGrid">The vols grid</param>
        /// <param name="assetGrid">The asset grid</param>
        /// <param name="settings">The SABR settings</param>
        /// <param name="calibrationEngineId">The id of this engine</param>
        /// <param name="optionExpiry">The ATM pointer</param>
        private static SortedDictionary <SABRKey, SABRCalibrationEngine> BuildEngineCollection(SwaptionDataMatrix volatilityGrid,
                                                                                               ForwardRatesMatrix assetGrid, SABRCalibrationSettings settings, string calibrationEngineId, string optionExpiry)
        {
            var engineCollection = new SortedDictionary <SABRKey, SABRCalibrationEngine>(new SABRKey());

            // Generate a new entry in the engineCollection for each row in the volatility grid
            foreach (string tenor in volatilityGrid.GetTenors())
            {
                var assetPrice   = assetGrid.GetAssetPrice(optionExpiry, tenor);
                var exerciseTime = (decimal)SABRHelper.GenerateDayValue(optionExpiry, 365.0d);
                // Generate the Vols and Strikes lists for the engine
                List <decimal> vols    = volatilityGrid.GetVolatility(tenor).ToList();
                List <decimal> strikes = volatilityGrid.GetStrikes().Select(strike => assetPrice + strike).ToList();
                // Only add a new Calibration Engine (and Calibrate it) if the vols are greater than 0
                if (!SABRHelper.ValidateData(vols))
                {
                    continue;
                }
                // Create a new instance of the engine
                var calibrationEngine =
                    new SABRCalibrationEngine(calibrationEngineId, settings, strikes, vols, assetPrice, exerciseTime);
                // Calibrate the engine
                calibrationEngine.CalibrateSABRModel();
                // Add the new engine to our collection
                var key = new SABRKey(optionExpiry, tenor);
                engineCollection.Add(key, calibrationEngine);
            }
            return(engineCollection);
        }