/// <summary>
 /// Calibrates the SABR engine for the Caplet smile at the particular
 /// expiry.
 /// Exception: System.Exception
 /// </summary>
 /// <param name="logger">The logger.</param>
 /// <param name="cache">The cache.</param>
 /// <param name="nameSpace">The clients namespace</param>
 /// <param name="expiry">Caplet expiry.</param>
 private void CalibrateSABREngine(ILogger logger, ICoreCache cache, string nameSpace, DateTime expiry)
 {
     // Set the inputs for the SABR calibration engine.
     SetStrikesForSABREngine();
     SetVolatilitiesForSABREngine(expiry);
     SetExerciseTimeForSABREngine(expiry);
     SetATMDataForSABREngine(logger, cache, nameSpace, expiry);
     // Calibrate the SABR engine.
     _sabrEngine = new SABRCalibrationEngine
                       (SABREngineHandle,
                       _sabrSettings,
                       _sabrStrikes,
                       _sabrVolatilities,
                       _assetPrice,
                       _excerciseTime);
     _sabrEngine.CalibrateSABRModel();
     // Check the status of the SABR calibration.
     if (_sabrEngine.IsSABRModelCalibrated)
     {
     }
     else
     {
         var errorMessage =
             "SABR calibration failed at Caplet expiry: " +
             expiry;
         throw new Exception(errorMessage);
     }
 }
        /// <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);
        }
Esempio n. 3
0
        /// <summary>
        /// Initialises the specified strikes.
        /// </summary>
        /// <param name="strikes">The strikes.</param>
        /// <param name="volatilities">The volatilities.</param>
        public void Initialize(double[] strikes, double[] volatilities)

        {
            // Convert the x-array to the Decimal data type.
            var strikeValues = new List <decimal>();

            InitHandles(ExpiryTime);
            decimal expiry = Convert.ToDecimal(ExpiryTime);
            var     vols   = new List <Decimal>();
            int     n      = volatilities.Length;

            for (int jdx = 0; jdx < n; jdx++)
            {
                if (volatilities[jdx] > 0)
                {
                    strikeValues.Add((decimal)strikes[jdx] * AssetPrice);
                    vols.Add(Convert.ToDecimal(volatilities[jdx]));
                }
            }
            // Calibrate and cache SABR handle for each expiry.
            CalibrationEngine = new SABRCalibrationEngine(SettingsHandle,
                                                          CalibrationSettings, strikeValues, vols, AssetPrice, expiry);
            CalibrationEngine.CalibrateSABRModel();
            string expiryTime = ExpiryTime + "Y";

            EngineHandles.Add(new SABRKey(expiryTime, Tenor), CalibrationEngine);
            IsCalibrated = CalibrationEngine.IsSABRModelCalibrated;
        }
Esempio n. 4
0
        public void TestCalibrateSABRModel()
        {
            DateTime start = DateTime.Now;

            _calibrationEngine.CalibrateSABRModel();
            DateTime end = DateTime.Now;
            double   x   = end.Subtract(start).Seconds;

            Debug.Print("%d seconds", x);

            // Test: calibration status.
            Assert.IsTrue(_calibrationEngine.IsSABRModelCalibrated);

            // Test: Calculation of the ATM slope.
            _expected = -0.2724637m;
            _actual   = _calibrationEngine.ATMSlope;
            Assert.AreEqual(decimal.ToDouble(_expected),
                            decimal.ToDouble(_actual),
                            decimal.ToDouble(_tolerance));

            // Test: Initial guess for the transformed SABR parameter theta.
            _expected = 1.85616576m;
            _actual   = _calibrationEngine.ThetaGuess;
            Assert.AreEqual(decimal.ToDouble(_expected),
                            decimal.ToDouble(_actual),
                            decimal.ToDouble(_tolerance));

            // Test: Initial guess for the transformed SABR parameter mu.
            _expected = 0.99883966m;
            _actual   = _calibrationEngine.MuGuess;
            Assert.AreEqual(decimal.ToDouble(_expected),
                            decimal.ToDouble(_actual),
                            decimal.ToDouble(_tolerance));

            // Test: SABR parameter alpha.
            _expected = 0.5477212m;
            _actual   = _calibrationEngine.GetSABRParameters.Alpha;
            Assert.AreEqual(decimal.ToDouble(_expected),
                            decimal.ToDouble(_actual),
                            decimal.ToDouble(_tolerance));

            // Test: SABR parameter beta.
            _expected = 0.85m;
            _actual   = _calibrationEngine.GetSABRParameters.Beta;
            Assert.AreEqual(decimal.ToDouble(_expected),
                            decimal.ToDouble(_actual));

            // Test: SABR parameter nu.
            _expected = 1.23498996m;
            _actual   = _calibrationEngine.GetSABRParameters.Nu;
            _nu       = _actual;
            Assert.AreEqual(decimal.ToDouble(_expected),
                            decimal.ToDouble(_actual),
                            decimal.ToDouble(_tolerance));


            // Test: SABR parameter rho.
            _expected = -0.2724164m;
            _actual   = _calibrationEngine.GetSABRParameters.Rho;
            _rho      = _actual;
            Assert.AreEqual(decimal.ToDouble(_expected),
                            decimal.ToDouble(_actual),
                            decimal.ToDouble(_tolerance));
        }