public void InitiateTapeFromModel() { string[] identifiers = CreateIdentArray(); ADouble[] variables = CreateAdVariableArray(); AADTape.Initialize(variables, identifiers); }
public static void FuncDiv3(ADouble x1, ADouble x2, double K) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { x1, x2 }); ADouble Out = x1 / x2 + K / x2 + K / x1 + x1 / K + x2 / K; AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
public static void FuncDiv2(ADouble x) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { x }); ADouble Out = 1.0 / x; AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
public static void FuncDiv(ADouble x) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { x }); ADouble Out = x * x * x / (3.0 + 2.0 * x); AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
public static void FuncExp(ADouble x) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { x }); // Derivative: Fx(x) = 3 + 3*exp(3*x) ADouble Temp = 3.0 * x + ADouble.Exp(3.0 * x) + 50.0; AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
public static void FuncLog(ADouble x) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { x }); // Derivative: Fx(x) = 3 + 3/x ADouble Temp = 3.0 * x + 3.0 * ADouble.Log(x * 4.0) + 50.0; AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
public static void FuncPow(ADouble x, double k) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { x }); ADouble Out = 3.0 * ADouble.Log(x) + 5.0 * ADouble.Pow(x, k); Console.WriteLine(" "); Console.WriteLine("Testing Adjoint differentiation of a function involving powers"); AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
public static void Func1(ADouble x, ADouble y, ADouble z) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { x, y, z }); // Derivative // Fx(x,y,z) = y*z + 1 + y // Fy(x,y,z) = x*z - 1 + x // Fz(x,y,z) = x*y ADouble Out = x * y * z + x - y + x * y; AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
public static void BlackScholes(ADouble vol, ADouble spot, ADouble rate, ADouble time, ADouble mat, ADouble strike) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { vol, spot, rate, time, mat, strike }); ADouble Help1 = vol * ADouble.Sqrt(mat - time); ADouble d1 = 1.0 / Help1 * (ADouble.Log(spot / strike) + (rate + 0.5 * ADouble.Pow(vol, 2)) * (mat - time)); ADouble d2 = d1 - vol * ADouble.Sqrt(mat - time); ADouble Out = MyMath.NormalCdf(d1) * spot - strike * ADouble.Exp(-rate * (mat - time)) * MyMath.NormalCdf(d2); Console.WriteLine(""); Console.WriteLine("BLACK-SCHOLES TEST. Value: " + Out.Value); AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
public static void Func11(ADouble x, ADouble y, ADouble z) { AADTape.ResetTape(); AADTape.Initialize(new ADouble[] { x, y, z }); ADouble Out1, Out2, Out3; Out1 = x * y * z; Out2 = x * y; Out3 = Out1 + x - y + Out2; Out3 = Out3 * 3.0; Out3 = 2.0 * Out3; Out3 = 10.0 + Out3; AADTape.InterpretTape(); AADTape.PrintTape(); AADTape.ResetTape(); }
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(); }
public static void BlackScholes() { AADTape.Initialize(); BlackScholes(new ADouble(0.20), new ADouble(100.0), new ADouble(0.05), new ADouble(0.0), new ADouble(1.0), new ADouble(90.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(); }
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(); }
public void InitiateTapeFromModelFwdCurvesOnly() { ADouble[] variables = CreateAdVariableArrayFwdCurvesOnly(); AADTape.Initialize(variables); }