/// <summary>
        /// Create a CalibrationEngine using an interpolation across existing CalibrationEngines.
        /// The calibrationArray has the handles of the Calibrated Engines to use in the interpolation processs.
        /// The interpolation can only be used in the ExpiryTime dimension to generate a new engine.
        /// If the engine array refers to unknown engines the process will fail (TBD)
        /// </summary>
        /// <param name="engineHandle">The name of the new interpolated calibration engine</param>
        /// <param name="settingsHandle">The settings to use in calibrating the engine</param>
        /// <param name="calibrationArray">The array of engine handles to use</param>
        /// <param name="atmVolatility">The At The Money volatility to use in engine calibration</param>
        /// <param name="assetPrice">The Asset price to use</param>
        /// <param name="exerciseTime">The exercise time to create the new engine for</param>
        /// <param name="tenor">The tenor to create the new engine for. This must be a valid tenor</param>
        /// <returns>The engine handle</returns>
        public string SABRCalibrateInterpolatedModel(string engineHandle, string settingsHandle, string[] calibrationArray, decimal atmVolatility, decimal assetPrice, string exerciseTime, string tenor)
        {
            SortedDictionary <SABRKey, SABRCalibrationEngine> engines = null;
            SABRCalibrationSettings settings = _sabrSettings[settingsHandle];

            foreach (string handle in calibrationArray)
            {
                ExtractAllEngines(ref engines, handle);
            }
            decimal pATM   = atmVolatility;
            decimal pAsset = assetPrice;
            // Create the new InterpolatedCalibrationEngine and add to the SABREngineCollection
            SortedDictionary <SABRKey, SABRCalibrationEngine> sabrEngine = BuildEngineCollection(settings, engineHandle, engines, pATM, pAsset, exerciseTime, tenor);

            // Add the SABREngine to the persistent store
            if (_sabrEngines.ContainsKey(engineHandle))
            {
                _sabrEngines[engineHandle] = sabrEngine;
            }
            else
            {
                _sabrEngines.Add(engineHandle, sabrEngine);
            }

            return(engineHandle);
        }
        /// <summary>
        /// Helper function used by the master function to initialise the
        /// SABR settings object.
        /// </summary>
        private void InitialiseSABRSettings()
        {
            // Check that the currency in each Caplet bootstrap is unique.
            var currency =
                ((VolatilitySurfaceIdentifier)_atmBootstrapEngine.GetPricingStructureId()).Currency.Value.ToUpper();

            foreach (var eng in _fixedStrikeBootstrapEngines)
            {
                var temp = ((VolatilitySurfaceIdentifier)eng.GetPricingStructureId()).Currency.Value.ToUpper();
                if (String.CompareOrdinal(currency, temp) == 0)
                {
                }
                else
                {
                    const string errorMessage =
                        "Currency in the Caplet bootstrap engines must be unique";
                    throw new ArgumentException(errorMessage);
                }
            }
            // Instantiate the SABR settings object.
            _sabrSettings = new SABRCalibrationSettings
                                (SABRSettingsHandle,
                                InstrumentType.Instrument.CapFloor,
                                currency,
                                _smileSettings.Beta);
        }
        /// <summary>
        /// Create a new Calibration Settings object. Each new object holds the instrument, currency and beta
        /// to use for this settings object. Each new object is stored internally using the supplied handle
        /// as an identifying key.
        /// The Settings object is used by a Calibration Engine to define base parameters on which to calibrate.
        /// </summary>
        /// <param name="handle">A <see cref="SABRCalibrationSettings"/> handle</param>
        /// <param name="instrument">The instrument type to use</param>
        /// <param name="ccy">The currency setting</param>
        /// <param name="beta">The Beta parameter to use</param>
        /// <returns></returns>
        public string SABRAddCalibrationSettings(string handle, string instrument, string ccy, decimal beta)
        {
            // Setup some defaults for the currency and instrument settings
            const string calibrationCcy        = "AUD";
            var          calibrationInstrument = InstrumentType.Instrument.Swaption;
            // Identify the instrument used in this settings object
            Array instruments = Enum.GetValues(typeof(InstrumentType.Instrument));

            foreach (InstrumentType.Instrument s in instruments)
            {
                if (s.ToString() != instrument)
                {
                    continue;
                }
                calibrationInstrument = s;
                break;
            }
            // Create a new calibration settings object using the instrument/ccy/beta grouping
            var newCalibration =
                new SABRCalibrationSettings(handle, calibrationInstrument, calibrationCcy, beta);

            // Test whether the CalibrationSettings is already present
            // If not already present then add it
            if (_sabrSettings.ContainsKey(handle))
            {
                _sabrSettings[handle] = newCalibration;
            }
            else
            {
                _sabrSettings.Add(handle, newCalibration);
            }
            return(handle);
        }
示例#4
0
        /// <summary>
        /// Generate an ATM (At-The-Money) Swaption Calibration engine using the supplied parameters
        /// This form of engine creates a single cell engine that does not support asset/volatility grid data.
        /// </summary>
        /// <param name="engineHandle">The engine identifier</param>
        /// <param name="settingsHandle">The settings identifier</param>
        /// <param name="nu">Nu value</param>
        /// <param name="rho">Rho value</param>
        /// <param name="atmVolatility">The ATM volatility</param>
        /// <param name="assetPrice">Asset Price to use</param>
        /// <param name="exerciseTime">Exercise time for the option</param>
        /// <returns></returns>
        public string[] SABRCalibrateATMModels(string[] engineHandle, string[] settingsHandle, decimal[] nu, decimal[] rho,
                                               decimal[] atmVolatility, decimal[] assetPrice, string[] exerciseTime)
        {
            for (int i = 0; i < engineHandle.Length; i++)
            {
                // Create the parameters used in this engine
                if (!_sabrSettings.ContainsKey(settingsHandle[i]))
                {
                    throw new ArgumentException(string.Format("Configuration '{0}' has not been set up.", settingsHandle));
                }
                SABRCalibrationSettings settings = _sabrSettings[settingsHandle[i]];
                string tenor = GenerateTenorLabel(exerciseTime[i]);
                // Create the engine
                SortedDictionary <SABRKey, SABRCalibrationEngine> sabrEngine
                    = BuildEngineCollection(settings, engineHandle[i], nu[i], rho[i], atmVolatility[i], assetPrice[i], tenor, null);
                // Add the SABREngine to the persistent store
                if (_sabrEngines.ContainsKey(engineHandle[i]))
                {
                    _sabrEngines[engineHandle[i]] = sabrEngine;
                }
                else
                {
                    _sabrEngines.Add(engineHandle[i], sabrEngine);
                }
            }

            return(engineHandle);
        }
示例#5
0
        /// <summary>
        /// Create a new Calibration Settings object. Each new object holds the instrument, currency and beta
        /// to use for this settings object. Each new object is stored internally using the supplied handle
        /// as an identifying key.
        /// The Settings object is used by a Calibration Engine to define base parameters on which to calibrate.
        /// </summary>
        /// <param name="handles">A handle</param>
        /// <param name="instrument">The instrument type to use</param>
        /// <param name="ccy">The currency setting</param>
        /// <param name="betas">The Beta parameter to use</param>
        /// <returns></returns>
        public string[] SabrAddCalibrationSettings(string[] handles, string instrument, string ccy, decimal[] betas)
        {
            // Setup some defaults for the currency and instrument settings
            var calibrationInstrument = InstrumentType.Instrument.Swaption;
            // Identify the instrument used in this settings object
            Array instruments = Enum.GetValues(typeof(InstrumentType.Instrument));

            foreach (InstrumentType.Instrument s in instruments)
            {
                if (s.ToString() == instrument)
                {
                    calibrationInstrument = s;
                    break;
                }
            }

            for (int i = 0; i < handles.Length; i++)
            {
                string handle = handles[i];
                // Create a new calibration settings object using the instrument/ccy/beta grouping
                var newCalibration =
                    new SABRCalibrationSettings(handle, calibrationInstrument, ccy, betas[i]);
                // Test whether the CalibrationSettings is already present
                // If not already present then add it
                if (_sabrSettings.ContainsKey(handle))
                {
                    _sabrSettings[handle] = newCalibration;
                }
                else
                {
                    _sabrSettings.Add(handle, newCalibration);
                }
            }
            return(handles);
        }
示例#6
0
        public void TestConstructor()
        {
            var settingsObj = new
                              SABRCalibrationSettings(_handle, _instrument, _currency, _beta);

            Assert.IsNotNull(settingsObj);
            Assert.AreEqual(_beta, settingsObj.Beta);
            Assert.AreEqual(_handle, settingsObj.Handle);
            Assert.AreEqual(_instrument, settingsObj.Instrument);
            Assert.AreEqual(_currency, settingsObj.Currency);
        }
        /// <summary>
        /// Generate an ATM (At-The-Money) Swaption Calibration engine using the supplied parameters
        /// This form of engine creates a single cell engine that does not support asset/volatility grid data.
        /// </summary>
        /// <param name="engineHandle">The engine identifier</param>
        /// <param name="settingsHandle">The settings identifier</param>
        /// <param name="nu">Nu value</param>
        /// <param name="rho">Rho value</param>
        /// <param name="atmVolatility">The ATM volatility</param>
        /// <param name="assetPrice">Asset Price to use</param>
        /// <param name="exerciseTime">Exercise time for the option</param>
        /// <returns></returns>
        public string SABRCalibrateATMModel(string engineHandle, string settingsHandle, decimal nu, decimal rho, decimal atmVolatility,
                                            decimal assetPrice, string exerciseTime)
        {
            // Create the parameters used in this engine
            SABRCalibrationSettings settings = _sabrSettings[settingsHandle];
            // Create the engine
            SortedDictionary <SABRKey, SABRCalibrationEngine> sabrEngine = BuildEngineCollection(settings, engineHandle, nu, rho, atmVolatility, assetPrice, SABRHelper.GenerateTenorLabel(exerciseTime), null);

            // Add the SABREngine to the persistent store
            if (_sabrEngines.ContainsKey(engineHandle))
            {
                _sabrEngines[engineHandle] = sabrEngine;
            }
            else
            {
                _sabrEngines.Add(engineHandle, sabrEngine);
            }

            return(engineHandle);
        }
示例#8
0
        /// <summary>
        /// Generate a new set of full calibration engines for the supplied data.
        /// Add or overwrite the engine store for the new engine.
        /// Each engineId will point to a set of engines indexed by swap tenor and option expiry
        /// Calibration assumes that the format of the grid data is as follows:
        ///
        ///     + XXXX |  lbl0  |  lbl1  | ... |  lbln  +
        ///     | lbl0 | d[0,0] | d[0,1] | ... | d[0,n] |
        ///     | lbl1 | d[1,0] | d[1,1] | ... | d[1,n] |
        ///     | ...  |   ...  |   ...  | ... |   ...  |
        ///     + lbln | d[n,0] | d[n,1] | ... | d[n,n] +
        ///
        /// </summary>
        /// <param name="engineHandle">Calibration Engine handle</param>
        /// <param name="settingsHandle">Calibartion settings handle</param>
        /// <param name="rawVols">A grid of volatilities (with row/column labels)</param>
        /// <param name="rawAssets">A grid of asset values</param>
        /// <param name="optionEx">The ption expiry to index against</param>
        /// <returns></returns>
        public string SABRCalibrateModel(string engineHandle, string settingsHandle, object[,] rawVols, object[,] rawAssets, string optionEx)
        {
            // Create the asset and volatility data grids
            SwaptionDataMatrix volatilityGrid = ParseVolatilityInput(rawVols, optionEx);
            ForwardRatesMatrix assetGrid      = ParseAssetInputWithInterpolation(rawAssets);

            // Retrieve the calibration settings to use with this calibration engine
            if (!_sabrSettings.ContainsKey(settingsHandle))
            {
                throw new ArgumentException($"Configuration '{settingsHandle}' has not been set up.");
            }
            SABRCalibrationSettings settings = _sabrSettings[settingsHandle];
            // Generate the CalibrationEngine Id
            string calibrationEngineId = engineHandle;
            string optionExpiry        = GenerateTenorLabel(optionEx);
            // Create a new engine holder object
            SortedDictionary <SABRKey, SABRCalibrationEngine> sabrEngine =
                BuildEngineCollection(volatilityGrid, assetGrid, settings, calibrationEngineId, optionExpiry);

            // We have an asset grid (forward rates) with this engine type so we should keep it
            // for future reference (that is during the lifetime of this session)
            if (_engineRatesGrid.ContainsKey(engineHandle))
            {
                _engineRatesGrid[engineHandle] = assetGrid;
            }
            else
            {
                _engineRatesGrid.Add(engineHandle, assetGrid);
            }

            // Add the SABREngine to the persistent store
            if (_sabrEngines.ContainsKey(calibrationEngineId))
            {
                _sabrEngines[calibrationEngineId] = sabrEngine;
            }
            else
            {
                _sabrEngines.Add(calibrationEngineId, sabrEngine);
            }
            return(engineHandle);
        }
示例#9
0
        /// <summary>
        /// Initialises the handles.
        /// </summary>
        public void InitHandles(double expiryTime)
        {
            ExpiryTime = expiryTime;
            string expiryTimeStr = expiryTime.ToString(CultureInfo.InvariantCulture);

            SettingsHandle = $"SABR Full Calibration {expiryTimeStr:D}Y";
            //No tenor for equity derivatives
            Tenor = "0Y";
            // Initialise the SABR calibration settings object.
            string currency = Currency;

            //InstrumentType.Instrument instrument = Instrument;
            CalibrationSettings = new SABRCalibrationSettings(SettingsHandle,
                                                              Instrument,
                                                              currency,
                                                              Beta);
            var unsorted = new Dictionary <SABRKey, SABRCalibrationEngine>
                               (new SABRKey());

            EngineHandles = new SortedDictionary <SABRKey, SABRCalibrationEngine>(unsorted, new SABRKey());
        }
示例#10
0
        /// <summary>
        /// Generate an ATM (At-The-Money) Swaption Calibration engine using the supplied parameters
        /// This form of engine creates a single cell engine that does not support asset/volatility grid data.
        /// </summary>
        /// <param name="engineHandle">The engine identifier</param>
        /// <param name="settingsHandle">The settings identifier</param>
        /// <param name="nu">Nu value</param>
        /// <param name="rho">Rho value</param>
        /// <param name="atmVolatility">The ATM volatility</param>
        /// <param name="assetPrice">Asset Price to use</param>
        /// <param name="exerciseTime">Exercise time for the option</param>
        /// <param name="assetCode">The asset code. </param>
        /// <returns></returns>
        public string SABRCalibrateATMModel(string engineHandle, string settingsHandle, decimal nu, decimal rho, decimal atmVolatility, decimal assetPrice, string exerciseTime, string assetCode)
        {
            // Create the parameters used in this engine
            if (!_sabrSettings.ContainsKey(settingsHandle))
            {
                throw new ArgumentException($"Configuration '{settingsHandle}' has not been set up.");
            }
            SABRCalibrationSettings settings = _sabrSettings[settingsHandle];
            // Create the engine
            SortedDictionary <SABRKey, SABRCalibrationEngine> sabrEngine = BuildEngineCollection(settings, engineHandle, nu, rho, atmVolatility,
                                                                                                 assetPrice, GenerateTenorLabel(exerciseTime), GenerateTenorLabel(assetCode));

            // Add the SABREngine to the persistent store
            if (_sabrEngines.ContainsKey(engineHandle))
            {
                _sabrEngines[engineHandle] = sabrEngine;
            }
            else
            {
                _sabrEngines.Add(engineHandle, sabrEngine);
            }
            return(engineHandle);
        }
示例#11
0
        //
        // Use TestInitialize to run code before running each test
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Setup

        public void Initialisation()
        {
            // Initialise the SABR calibration settings object.
            _settingsHandle = "SABR Calibration Engine Unit Test.";
            _currency       = "AUD";
            _instrument     = InstrumentType.Instrument.CallPut;
            _beta           = 0.85m;
            //_calibrationError = 0.0m;
            _calibrationSettings = new SABRCalibrationSettings(_settingsHandle,
                                                               _instrument,
                                                               _currency,
                                                               _beta);

            // Initialise  the SABR calibration engine object.
            _assetPrice   = 1350.00m;
            _engineHandle = _settingsHandle;
            _exerciseTime = 5.0m;

            decimal[] tempStrikes = { 1242.00m,
                                      1269.00m,
                                      1283.00m,
                                      1296.00m,
                                      1323.00m,
                                      1350.00m,
                                      1377.00m,
                                      1404.00m,
                                      1418.00m,
                                      1431.00m,
                                      1458.00m,
                                      1485.00m,
                                      1519.00m };

            _strikes = new List <decimal>();
            foreach (decimal strike in tempStrikes)
            {
                _strikes.Add(strike);
            }

            decimal[] tempVolatilities =
            { 0.3108m,
              0.3012m,
              0.2966m,
              0.2925m,
              0.2847m,
              0.2784m,
              0.2738m,
              0.2709m,
              0.2700m,
              0.2696m,
              0.2697m,
              0.2710m,
              0.2744m };

            _volatilities = new List <decimal>();
            foreach (decimal volatility in tempVolatilities)
            {
                _volatilities.Add(volatility);
            }

            _calibrationEngine = new SABRCalibrationEngine(_engineHandle,
                                                           _calibrationSettings,
                                                           _strikes,
                                                           _volatilities,
                                                           _assetPrice,
                                                           _exerciseTime);

            // Initialise the collection of SABR calibration engine objects.
            //_engineHandles = null;
        }
        /// <summary>
        /// Create an Interpolated calibration engine. Such an engine is designed for a single value
        /// derived from a set of base engines.
        /// </summary>
        /// <param name="settings">The settings object to use</param>
        /// <param name="calibrationEngineId">The id of this engine</param>
        /// <param name="engines">The array of engine handles to use</param>
        /// <param name="atmVolatility">The ATM volatility</param>
        /// <param name="assetPrice">Asset Price to use</param>
        /// <param name="optionExpiry">The ATM pointer</param>
        /// <param name="tenor">The tenor to create the new engine for. This must be a valid tenor</param>
        private static SortedDictionary <SABRKey, SABRCalibrationEngine> BuildEngineCollection(SABRCalibrationSettings settings, string calibrationEngineId, IEnumerable <KeyValuePair <SABRKey, SABRCalibrationEngine> > engines, decimal atmVolatility, decimal assetPrice, string optionExpiry, string tenor)
        {
            var engineCollection = new SortedDictionary <SABRKey, SABRCalibrationEngine>(new SABRKey());
            var exerciseTime     = (decimal)SABRHelper.GenerateDayValue(optionExpiry, 365.0d);
            var indexTenor       = (decimal)SABRHelper.GenerateDayValue(tenor, 365.0d);
            // Create a new instance of the engine
            var calibrationEngine =
                new SABRCalibrationEngine(calibrationEngineId, settings, engines, atmVolatility, assetPrice, exerciseTime, indexTenor);

            // Calibrate the engine
            calibrationEngine.CalibrateInterpSABRModel();
            // Add the new engine to our collection
            var key = new SABRKey(optionExpiry, tenor);

            engineCollection.Add(key, calibrationEngine);
            return(engineCollection);
        }
        /// <summary>
        /// Create an ATM calibration engine. Such an engine is designed for a single value
        /// </summary>
        /// <param name="settings">The settings object to use</param>
        /// <param name="calibrationEngineId">The id of this engine</param>
        /// <param name="nu">Nu value</param>
        /// <param name="rho">Rho value</param>
        /// <param name="atmVolatility">The ATM volatility</param>
        /// <param name="assetPrice">Asset Price to use</param>
        /// <param name="optionExpiry">The ATM pointer</param>
        /// <param name="assetCode">The ATM identifiier (if used)</param>
        private static SortedDictionary <SABRKey, SABRCalibrationEngine> BuildEngineCollection(SABRCalibrationSettings settings,
                                                                                               string calibrationEngineId, decimal nu, decimal rho, decimal atmVolatility, decimal assetPrice,
                                                                                               string optionExpiry, string assetCode)
        {
            // basic setup
            var engineCollection = new SortedDictionary <SABRKey, SABRCalibrationEngine>(new SABRKey());
            var exerciseTime     = (decimal)SABRHelper.GenerateDayValue(optionExpiry, 365.0d);
            // Create a new instance of the engine
            var calibrationEngine =
                new SABRCalibrationEngine(calibrationEngineId, settings, nu, rho, atmVolatility, assetPrice,
                                          exerciseTime);

            // Calibrate the engine
            calibrationEngine.CalibrateATMSABRModel();
            // Add the new engine to our collection
            SABRKey key = assetCode != null ? new SABRKey(optionExpiry, assetCode) : new SABRKey(optionExpiry);

            engineCollection.Add(key, calibrationEngine);
            return(engineCollection);
        }
        /// <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);
        }
示例#15
0
        /// <summary>
        /// Create an Interpolated calibration engine. Such an engine is designed for a single value
        /// derived from a set of base engines.
        /// </summary>
        /// <param name="settings">The settings object to use</param>
        /// <param name="calibrationEngineId">The id of this engine</param>
        /// <param name="engines">The array of engine handles to use</param>
        /// <param name="atmVolatility">The ATM volatility</param>
        /// <param name="assetPrice">Asset Price to use</param>
        /// <param name="optionExpiry">The ATM pointer</param>
        /// <param name="tenor">The tenor to create the new engine for. This must be a valid tenor</param>
        private SortedDictionary <SABRKey, SABRCalibrationEngine> BuildEngineCollection(SABRCalibrationSettings settings,
                                                                                        string calibrationEngineId, ref SortedDictionary <SABRKey, SABRCalibrationEngine> engines,
                                                                                        decimal atmVolatility, decimal assetPrice, string optionExpiry, string tenor)
        {
            var     engineCollection = new SortedDictionary <SABRKey, SABRCalibrationEngine>(new SABRKey());
            decimal exerciseTime     = GenerateDayValue(optionExpiry, 365.0m);
            decimal indexTenor       = GenerateDayValue(tenor, 365.0m);
            // Create a new instance of the engine
            //(string handle, SABRCalibrationSettings calibrationSettings,
            var calibrationEngine =
                new SABRCalibrationEngine(calibrationEngineId, settings, engines,
                                          atmVolatility, assetPrice, exerciseTime, indexTenor);

            // Calibrate the engine
            calibrationEngine.CalibrateInterpSABRModel();
            // Add the new engine to our collection
            var key = new SABRKey(optionExpiry, tenor);

            engineCollection.Add(key, calibrationEngine);
            return(engineCollection);
        }