Beispiel #1
0
        public void SetEverything(Curve discCurve, CurveCalibrationProblem[] problems, CurveTenor[] tenors, CalibrationSpec settings, int[] OrderOfCalibration = null)
        {
            if (problems.Length != tenors.Length)
            {
                throw new InvalidOperationException("Number of problems and number of tenors have to match. ");
            }

            _problemMap         = new Dictionary <CurveTenor, CurveCalibrationProblem>();
            _hasBeenCalibrated  = new Dictionary <CurveTenor, bool>();
            _discCurve          = discCurve;
            _tenors             = tenors;
            _settings           = settings;
            _fwdCurveCollection = new FwdCurveContainer();


            List <int> curvePoints = new List <int>();

            for (int i = 0; i < problems.Length; i++)
            {
                _problemMap[tenors[i]] = problems[i];
                _fwdCurveCollection.AddCurve(problems[i].CurveToBeCalibrated, tenors[i]);
                curvePoints.Add(_fwdCurveCollection.GetCurve(tenors[i]).Dimension);
            }

            _curvePoints   = curvePoints.ToArray();
            _internalState = 0;
        }
Beispiel #2
0
        private LinearRateModel BumpFwdCurveAndReturn(CurveTenor fwdCurve, int curvePoint, double bump = 0.0001)
        {
            Curve newCurve = FwdCurveCollection.GetCurve(fwdCurve).Copy();

            newCurve.BumpCurvePoint(curvePoint, bump);
            return(ReturnModelWithReplacedFwdCurve(fwdCurve, newCurve));
        }
Beispiel #3
0
        public double[] ConstructStartingValuesFromCurves_AD_Current(FwdCurveContainer fwdCurves)
        {
            // This method constructs starting values based on existing forward curves.
            // Intended to be used for fast calibration for "live" calibration

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

            for (int i = 0; i < _currentTenors.Length; i++)
            {
                for (int j = 0; j < _currentCurvePoints[i]; j++)
                {
                    output.Add(fwdCurves.GetCurve(_currentTenors[i]).Values[j]);
                }
            }

            return(output.ToArray());
        }
Beispiel #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();
        }