Example #1
0
        public double[] ZcbRiskProductAD(LinearRateInstrument product)
        {
            AADTape.ResetTape();
            if (ADCurvesHasBeenSet == false)
            {
                SetAdCurvesFromOrdinaryCurve();
            }

            InitiateTapeFromModel();
            ValueLinearRateProductAD(product);
            AADTape.InterpretTape();
            double[] output = AADTape.GetGradient();
            AADTape.ResetTape();
            return(ScaleGradient(0.0001, output));
        }
Example #2
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();
        }
Example #3
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();
        }
Example #4
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();
        }