Esempio n. 1
0
        public ADouble ValueInstrumentFromFactoryAD(LinearRateModel model, string instrument)
        {
            // Values an instrument contained in the instrument factory using a
            // linear rate model. Note that value here means calculating the value
            // par rate (which is how trades are quoted).

            QuoteType type = InstrumentTypeMap[instrument];

            switch (type)
            {
            case QuoteType.ParSwapRate:
                return(model.IrParSwapRateAD(IrSwaps[instrument]));

            case QuoteType.ParBasisSpread:
                return(model.ParBasisSpreadAD(BasisSwaps[instrument]));

            case QuoteType.OisRate:
                return(model.OisRateSimpleAD(OisSwaps[instrument]));

            case QuoteType.FraRate:
                return(model.ParFraRateAD(Fras[instrument]));

            case QuoteType.FuturesRate:
                return(model.ParFutureRateAD(Futures[instrument]));

            case QuoteType.Deposit:
                return(model.ParDepositRateAD(Deposits[instrument]));

            default:
                throw new InvalidOperationException("Instrument QuoteType not supported...");
            }
        }
Esempio n. 2
0
        // Calcualate BnR disc risk on a linear rate instrument
        public double BumpAndRunDisc(LinearRateInstrument product, int curvePoint, double bump = 0.0001)
        {
            double          valueNoBump = ValueLinearRateProduct(product);
            LinearRateModel newModel    = BumpDiscCurveAndReturn(curvePoint, bump);
            double          valueBump   = newModel.ValueLinearRateProduct(product);

            return((valueBump - valueNoBump) / bump * 0.0001);
        }
Esempio n. 3
0
        // Calculate BnR forward risk on a linear rate instrument
        public double BumpAndRunFwdRisk(LinearRateInstrument product, CurveTenor fwdCurve, int curvePoint, double bump = 0.0001)
        {
            double          valueNoBump = ValueLinearRateProduct(product);
            LinearRateModel newModel    = BumpFwdCurveAndReturn(fwdCurve, curvePoint, bump);
            double          valueBump   = newModel.ValueLinearRateProduct(product);

            return((valueBump - valueNoBump) / bump * 0.0001);
        }
Esempio n. 4
0
 public RiskEngine(LinearRateModel model, Portfolio portfolio, RiskJacobian jacobian, bool useAd = false)
 {
     _linearRateModel   = model;
     _portfolio         = portfolio;
     _jacobian          = jacobian;
     ZcbRiskOutput      = new ZcbRiskOutputContainer();
     OutrightRiskOutput = new OutrightRiskContainer();
     _asOf  = jacobian.AsOf;
     _useAd = useAd;
 }
Esempio n. 5
0
        private void SetCurveDimensions(LinearRateModel model)
        {
            CurveDimensions = new Dictionary <CurveTenor, int>();
            foreach (CurveTenor key in model.FwdCurveCollection.Curves.Keys)
            {
                CurveDimensions[key] = model.FwdCurveCollection.Curves[key].Dimension;
            }

            CurveDimensions[CurveTenor.DiscOis] = model.DiscCurve.Dimension;
        }
Esempio n. 6
0
        public double GoalFunction(LinearRateModel model, double scaling = 1.0)
        {
            double quadraticSum = 0.0;

            for (int i = 0; i < InputInstruments.Count; i++)
            {
                quadraticSum += Math.Pow(InputInstruments[i].ValueInstrument(model, Factory) - InputInstruments[i].QuoteValue, 2.0) * scaling;
            }

            return(quadraticSum);
        }
Esempio n. 7
0
        public RiskJacobian(LinearRateModel model, DateTime asOf)
        {
            Model = model;
            AsOf  = asOf;

            SetCurveDimensions(model);

            Instruments          = new List <CalibrationInstrument>();
            _fullGradients       = new Dictionary <string, List <double> >();
            InstrumentDictionary = new Dictionary <CurveTenor, List <CalibrationInstrument> >();
        }
Esempio n. 8
0
        private double GoalFunction_AD(CurveTenor[] tenors, LinearRateModel model)
        {
            ADouble goalSum = 0.0;

            for (int i = 0; i < tenors.Length; i++)
            {
                goalSum = goalSum + _problemMap[tenors[i]].GoalFunction_AD(model, _settings.Scaling);
            }

            return(goalSum.Value);
        }
Esempio n. 9
0
        public void RiskAllInstruments(LinearRateModel model, DateTime asOf, bool useAd = false)
        {
            if (Products.Count == 0)
            {
                throw new InvalidOperationException("Portfolio does not contain any instruments to risk");
            }

            foreach (int key in Products.Keys)
            {
                RiskInstrument(key, model, asOf, useAd);
            }
        }
Esempio n. 10
0
        public ADouble GoalFunction_AD(LinearRateModel model, double scaling = 1.0)
        {
            ADouble quadraticSum = 0.0;

            for (int i = 0; i < InputInstruments.Count; i++)
            {
                ADouble tempDifference = InputInstruments[i].ValueInstrumentAD(model, Factory) - InputInstruments[i].QuoteValue;
                quadraticSum = quadraticSum + ADouble.Pow(tempDifference, 2.0) * scaling;
            }

            return(quadraticSum);
        }
Esempio n. 11
0
        private void OptimizationFunction(double[] x, ref double func, object obj)
        {
            Curve tempDiscCurve = new Curve(_problem.CurvePoints, x.ToList());

            FwdCurveContainer tempFwdCurves = new FwdCurveContainer();
            LinearRateModel   tempModel     = new LinearRateModel(tempDiscCurve, new FwdCurveContainer(), _settings.Interpolation);

            _internalState += 1;

            if (_internalState > _internalStateMax)
            {
                return;
            }

            func = _problem.GoalFunction(tempModel, _settings.Scaling);
        }
Esempio n. 12
0
        public void RiskInstrument(int instrumentNumber, LinearRateModel model, DateTime asOf, bool useAd = false)
        {
            if (Products.ContainsKey(instrumentNumber) == false)
            {
                throw new InvalidOperationException("Portfolio does not contain " + instrumentNumber + " as an identififer for a linearRateProduct.");
            }

            if (useAd)
            {
                RiskOutputs[instrumentNumber] = model.ZcbRiskProductOutputContainer(Products[instrumentNumber], asOf);
            }
            else
            {
                RiskOutputs[instrumentNumber] = model.RiskAgainstAllCurvesBumpAndRun(Products[instrumentNumber], asOf);
            }
        }
Esempio n. 13
0
        private void OptimizationFunction(double[] curveValues, ref double func, object obj)
        {
            // This is the main optimization function used to calibrate the curves.
            // It is dependent on the current calibration settings
            // defined by _currentTenor and _currentCurvePoints.

            _internalState += 1;

            List <List <double> > curveValueCollection = new List <List <double> >();

            int n = 0;

            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                List <double> temp = new List <double>();
                for (int j = 0; j < _currentCurvePoints[i]; j++)
                {
                    temp.Add(curveValues[n]);
                    n += 1;
                }
                curveValueCollection.Add(temp);
            }

            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                _fwdCurveCollection.UpdateCurveValues(curveValueCollection[i], _currentTenors[i]);
                //Curve tempFwdCurve = new Curve(_problemMap[_tenors[i]].CurvePoints, doubles[i]);
                //_fwdCurveCollection.AddCurve(tempFwdCurve, _tenors[i]);
            }

            // If max iterations has been hit, exit. For some reason
            // AlgLibs build in maxIterations does not exit the procedure..
            if (_internalState > _internalStateMax)
            {
                return;
            }

            // Construct new LinearRateModel based on the next iteration.
            LinearRateModel tempModel = new LinearRateModel(_discCurve, _fwdCurveCollection, _settings.Interpolation);

            func = GoalFunction(_currentTenors, tempModel);
        }
Esempio n. 14
0
        public void OptimizationFunction_AD_grad(double[] x, ref double func, double[] grad, object obj)
        {
            Curve tempDiscCurve = new Curve(_problem.CurvePoints, x.ToList());

            FwdCurveContainer tempFwdCurves = new FwdCurveContainer();
            LinearRateModel   tempModel     = new LinearRateModel(tempDiscCurve, new FwdCurveContainer(), _settings.Interpolation);

            _internalState += 1;

            if (_internalState > _internalStateMax)
            {
                return;
            }


            // Initialize AADTape with curve values
            AADTape.ResetTape();
            List <ADouble> adCurveValues = new List <ADouble>();

            for (int i = 0; i < tempDiscCurve.Dimension; i++)
            {
                adCurveValues.Add(new ADouble(x[i]));
            }

            // Initialize the tape with curve values defined above
            AADTape.Initialize(adCurveValues.ToArray());
            Curve_AD adCurve = new Curve_AD(tempDiscCurve.Dates, adCurveValues);

            tempModel.ADDiscCurve = adCurve;

            func = _problem.GoalFunction_AD(tempModel, _settings.Scaling);
            AADTape.InterpretTape();
            double[] gradient = AADTape.GetGradient();
            for (int i = 0; i < gradient.Length; i++)
            {
                grad[i] = gradient[i];
            }

            AADTape.ResetTape();
        }
Esempio n. 15
0
        public void UpdateAllInstrumentsToParGivenModel(LinearRateModel model)
        {
            // Given a linear rate model, updates fixed rates of all
            // instruments in factory to par. This is used when we calculate risk.
            // We want to hedge with liquid instruments, and trades are usually
            // initiated at par.

            foreach (string key in Fras.Keys)
            {
                Fras[key].UpdateFixedRateToPar(model);
            }

            foreach (string key in Futures.Keys)
            {
                Futures[key].UpdateFixedRateToPar(model);
            }

            foreach (string key in IrSwaps.Keys)
            {
                IrSwaps[key].UpdateFixedRateToPar(model);
            }

            foreach (string key in BasisSwaps.Keys)
            {
                BasisSwaps[key].UpdateFixedRateToPar(model);
            }

            foreach (string key in OisSwaps.Keys)
            {
                OisSwaps[key].UpdateFixedRateToPar(model);
            }

            foreach (string key in Deposits.Keys)
            {
                Deposits[key].UpdateFixedRateToPar(model);
            }
        }
Esempio n. 16
0
 public void UpdateFixedRateToPar(LinearRateModel model)
 {
     FloatLegSpread.Spread = model.ParBasisSpread(this);
 }
Esempio n. 17
0
 public void UpdateFixedRateToPar(LinearRateModel model)
 {
     FixedLeg.FixedRate = model.IrParSwapRate(this);
 }
Esempio n. 18
0
 public void RiskInstrumentBumpAndRun(LinearRateModel model, DateTime asOf)
 {
     RiskOutput = model.RiskAgainstAllCurvesBumpAndRun(Instrument, asOf);
     RiskOutput.ConstructFullGradient();
 }
Esempio n. 19
0
 public ADouble ValueInstrumentAD(LinearRateModel model, InstrumentFactory factory)
 {
     return(factory.ValueInstrumentFromFactoryAD(model, _identifier));
 }
Esempio n. 20
0
 public void UpdateFixedRateToPar(LinearRateModel model)
 {
     FixedRate = model.FwdCurveCollection.GetCurve(ReferenceIndex).FwdRate(AsOf, StartDate, EndDate, DayRule, DayCount, InterpMethod.Hermite);
 }
Esempio n. 21
0
 public void UpdateFixedRateToPar(LinearRateModel model)
 {
     FraSameSpec.UpdateFixedRateToPar(model);
 }
Esempio n. 22
0
 public void UpdateFixedRateToPar(LinearRateModel model)
 {
     FixedRate = model.ParDepositRate(this);
 }
Esempio n. 23
0
        private void OptimizationFunction_AD_Current_OLD(double[] curveValues, ref double func, double[] grad, object obj)
        {
            // This is a working optimizationFunction that does not work with order.
            // Kept as reference in case something goes wrong with the more general implementation.

            _internalState += 1;

            if (_internalState > _internalStateMax)
            {
                return;
            }

            List <List <double> > curveValueCollection = new List <List <double> >();
            List <ADouble>        curveValuesAd        = new List <ADouble>();

            int n = 0;

            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                List <double> temp = new List <double>();
                for (int j = 0; j < _currentCurvePoints[i]; j++)
                {
                    temp.Add(curveValues[n]);
                    curveValuesAd.Add(new ADouble(curveValues[n]));
                    n += 1;
                }
                curveValueCollection.Add(temp);
            }

            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                _fwdCurveCollection.UpdateCurveValues(curveValueCollection[i], _currentTenors[i]);
            }

            AADTape.ResetTape();

            LinearRateModel tempModel = new LinearRateModel(_discCurve, _fwdCurveCollection, _settings.Interpolation);

            tempModel.ADFwdCurveCollection = new ADFwdCurveContainer();
            AADTape.Initialize(curveValuesAd.ToArray());
            tempModel.SetAdDiscCurveFromOrdinaryCurve();

            n = 0;
            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                List <ADouble> tempADouble = new List <ADouble>();
                for (int j = 0; j < _currentCurvePoints[i]; j++)
                {
                    tempADouble.Add(curveValuesAd[n]);
                    n += 1;
                }

                tempModel.ADFwdCurveCollection.AddCurve(new Curve_AD(_fwdCurveCollection.GetCurve(_currentTenors[i]).Dates, tempADouble), _currentTenors[i]);
            }

            func = GoalFunction_AD(_currentTenors, tempModel);
            AADTape.InterpretTape();
            double[] gradient = AADTape.GetGradient();
            for (int i = 0; i < gradient.Length; i++)
            {
                grad[i] = gradient[i];
            }

            AADTape.ResetTape();
        }
Esempio n. 24
0
 public void UpdateFixedRateToPar(LinearRateModel model)
 {
     FixedRate = model.DiscCurve.OisRateSimple(this, InterpMethod.Hermite);
 }
Esempio n. 25
0
        private void OptimizationFunction_AD(double[] curveValues, ref double func, double[] grad, object obj)
        {
            _internalState += 1;

            if (_internalState > _internalStateMax)
            {
                return;
            }

            List <List <double> > curveValueCollection = new List <List <double> >();
            int n = 0;

            // Update curve values to newest iteration
            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                List <double> temp = new List <double>();
                for (int j = 0; j < _currentCurvePoints[i]; j++)
                {
                    temp.Add(curveValues[n]);
                    n += 1;
                }
                curveValueCollection.Add(temp);
            }

            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                _fwdCurveCollection.UpdateCurveValues(curveValueCollection[i], _currentTenors[i]);
            }

            // Initialize temporary LinearRateModel with new curves.
            LinearRateModel tempModel = new LinearRateModel(_discCurve, _fwdCurveCollection, _settings.Interpolation);

            tempModel.ADFwdCurveCollection = new ADFwdCurveContainer();
            tempModel.SetAdDiscCurveFromOrdinaryCurve();
            n = 0;

            // Create ADouble array of curve values - active variables.
            List <ADouble> curveValuesAd = new List <ADouble>();

            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                for (int j = 0; j < _currentCurvePoints[i]; j++)
                {
                    curveValuesAd.Add(new ADouble(curveValues[n]));
                    n += 1;
                }
            }

            AADTape.ResetTape();
            AADTape.Initialize(curveValuesAd.ToArray());

            // Set curves in tempModel that has already been calibrated.
            foreach (CurveTenor tempTenor in _hasBeenCalibrated.Keys)
            {
                if (_hasBeenCalibrated[tempTenor])
                {
                    Curve          tempCurve  = _fwdCurveCollection.GetCurve(tempTenor).Copy();
                    List <ADouble> tempValues = new List <ADouble>();


                    for (int i = 0; i < _fwdCurveCollection.GetCurve(tempTenor).Values.Count; i++)
                    {
                        tempValues.Add(new ADouble(_fwdCurveCollection.GetCurve(tempTenor).Values[i]));
                    }

                    Curve_AD tempADCurve = new Curve_AD(_fwdCurveCollection.GetCurve(tempTenor).Dates, tempValues);
                    tempModel.ADFwdCurveCollection.AddCurve(tempADCurve, tempTenor);
                }
            }

            n = 0;

            // Convert active ADouble variables to curves in the temporary model
            for (int i = 0; i < _currentCurvePoints.Length; i++)
            {
                List <ADouble> tempADouble = new List <ADouble>();
                for (int j = 0; j < _currentCurvePoints[i]; j++)
                {
                    tempADouble.Add(curveValuesAd[n]);
                    n += 1;
                }
                tempModel.ADFwdCurveCollection.AddCurve(new Curve_AD(_fwdCurveCollection.GetCurve(_currentTenors[i]).Dates, tempADouble), _currentTenors[i]);
            }

            // Evaluate goal function and obtain gradient.
            func = GoalFunction_AD(_currentTenors, tempModel);
            AADTape.InterpretTape();
            double[] gradient = AADTape.GetGradient();
            for (int i = 0; i < gradient.Length; i++)
            {
                grad[i] = gradient[i];
            }

            AADTape.ResetTape();
        }