// Fras public ADouble ParFraRateAD(Fra fra) { Curve_AD fwdCurve = this.ADFwdCurveCollection.GetCurve(fra.ReferenceIndex); ADouble rate = fwdCurve.FwdRate(fra.AsOf, fra.StartDate, fra.EndDate, fra.DayRule, fra.DayCount, Interpolation); return(rate); }
// The functionality below takes the original "double" curves // and converts them to AD curves. public void SetAdCurvesFromOrdinaryCurve() { List <ADouble> discValues = new List <ADouble>(); for (int i = 0; i < DiscCurve.Values.Count; i++) { discValues.Add(new MasterThesis.ADouble(DiscCurve.Values[i])); } ADDiscCurve = new MasterThesis.Curve_AD(DiscCurve.Dates, discValues); ADFwdCurveCollection = new ADFwdCurveContainer(); foreach (CurveTenor tenor in FwdCurveCollection.Curves.Keys) { List <ADouble> tempValues = new List <ADouble>(); for (int i = 0; i < FwdCurveCollection.GetCurve(tenor).Values.Count; i++) { tempValues.Add(new ADouble(FwdCurveCollection.GetCurve(tenor).Values[i])); } Curve_AD tempCurve = new Curve_AD(FwdCurveCollection.GetCurve(tenor).Dates, tempValues); ADFwdCurveCollection.AddCurve(tempCurve, tenor); } ADCurvesHasBeenSet = true; }
public void SetAdDiscCurveFromOrdinaryCurve() { List <ADouble> discValues = new List <ADouble>(); for (int i = 0; i < DiscCurve.Values.Count; i++) { discValues.Add(new MasterThesis.ADouble(DiscCurve.Values[i])); } ADDiscCurve = new MasterThesis.Curve_AD(DiscCurve.Dates, discValues); }
public FwdCurveRepresentation(Curve_AD curve, CurveTenor tenor, DateTime asOf, DayCount dayCount, DayRule dayRule, InterpMethod interpolation) { Dates = curve.Dates; Dimension = curve.Dimension; Tenor = tenor; AsOf = asOf; _fwdDayCount = dayCount; _fwdDayRule = dayRule; _interpolation = interpolation; for (int i = 0; i < Dimension; i++) { Values.Add(curve.Values[i].Value); } ConstructZcbCurveFromDatesAndValues(); ConstructFwdRates(); }
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(); }
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(); }
public LinearRateModel(Curve_AD discCurve, ADFwdCurveContainer fwdCurveCollection, InterpMethod interpolation = InterpMethod.Linear) { ADDiscCurve = discCurve; ADFwdCurveCollection = fwdCurveCollection; Interpolation = interpolation; }
public void AddCurve(Curve_AD curve, CurveTenor curveType) { Curves[curveType] = curve; }