Exemple #1
0
 // Initialize ADouble variable from double
 public ADouble(double doubleValue)
 {
     AADType         = (int)AADUtility.AADCalculationType.Const;
     Value           = doubleValue;
     PlacementInTape = AADTape._tapeCounter;
     AADTape.AddEntry((int)AADType, PlacementInTape, 0, 0, Value);
 }
        public void InitiateTapeFromModel()
        {
            string[]  identifiers = CreateIdentArray();
            ADouble[] variables   = CreateAdVariableArray();

            AADTape.Initialize(variables, identifiers);
        }
Exemple #3
0
        public static ADouble Pow(ADouble x1, double exponent)
        {
            ADouble temp = new ADouble();

            temp.Value           = Math.Pow(x1.Value, exponent);
            temp.PlacementInTape = AADTape._tapeCounter;
            AADTape.AddEntry((int)AADUtility.AADCalculationType.Pow, x1.PlacementInTape, 0, 0, temp.Value, exponent);
            return(temp);
        }
Exemple #4
0
        public static ADouble Log(ADouble x1)
        {
            ADouble temp = new ADouble();

            temp.Value           = Math.Log(x1.Value);
            temp.PlacementInTape = AADTape._tapeCounter;
            AADTape.AddEntry((int)AADUtility.AADCalculationType.Log, x1.PlacementInTape, 0, 0, temp.Value);
            return(temp);
        }
Exemple #5
0
        public static ADouble operator /(ADouble x1, ADouble x2)
        {
            ADouble temp = new ADouble();

            temp.Value           = x1.Value / x2.Value;
            temp.PlacementInTape = AADTape._tapeCounter;
            AADTape.AddEntry((int)AADUtility.AADCalculationType.Div, x1.PlacementInTape, x2.PlacementInTape, 0, temp.Value);
            return(temp);
        }
Exemple #6
0
        public static ADouble Exp(ADouble x1)
        {
            ADouble Temp = new ADouble();

            Temp.Value           = Math.Exp(x1.Value);
            Temp.PlacementInTape = AADTape._tapeCounter;
            AADTape.AddEntry((int)AADUtility.AADCalculationType.Exp, x1.PlacementInTape, 0, 0, Temp.Value);
            return(Temp);
        }
Exemple #7
0
        public static ADouble operator /(double K, ADouble x)
        {
            ADouble temp = new MasterThesis.ADouble();

            temp.Value           = K / x.Value;
            temp.PlacementInTape = AADTape._tapeCounter;
            AADTape.AddEntry((int)AADUtility.AADCalculationType.ConsDiv, x.PlacementInTape, 0, 0, temp.Value, K);
            return(temp);
        }
Exemple #8
0
        public static ADouble operator -(double K, ADouble x)
        {
            ADouble temp = new ADouble();

            temp.Value           = K - x.Value;
            temp.PlacementInTape = AADTape._tapeCounter;
            AADTape.AddEntry((int)AADUtility.AADCalculationType.ConsSubInverse, x.PlacementInTape, 0, 0, temp.Value, K);
            return(temp);
        }
Exemple #9
0
        public static ADouble operator +(ADouble x, double K)
        {
            ADouble temp = new ADouble();

            temp.Value           = x.Value + K;
            temp.PlacementInTape = AADTape._tapeCounter;
            AADTape.AddEntry((int)AADUtility.AADCalculationType.ConsAdd, x.PlacementInTape, 0, 0, temp.Value, K);
            return(temp);
        }
Exemple #10
0
        public static ADouble operator /(ADouble x, double K)
        {
            // Note that we store this as a "ConsMul" on the tape
            ADouble temp = new MasterThesis.ADouble();

            temp.Value           = x.Value / K;
            temp.PlacementInTape = AADTape._tapeCounter;
            AADTape.AddEntry((int)AADUtility.AADCalculationType.ConsMul, x.PlacementInTape, 0, 0, temp.Value, 1 / K);
            return(temp);
        }
Exemple #11
0
        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();
        }
Exemple #12
0
        public static void FuncDiv2(ADouble x)
        {
            AADTape.ResetTape();
            AADTape.Initialize(new ADouble[] { x });

            ADouble Out = 1.0 / x;

            AADTape.InterpretTape();
            AADTape.PrintTape();
            AADTape.ResetTape();
        }
Exemple #13
0
        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();
        }
Exemple #14
0
        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();
        }
Exemple #15
0
        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();
        }
Exemple #16
0
        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();
        }
Exemple #17
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));
        }
Exemple #18
0
        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();
        }
Exemple #19
0
        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();
        }
Exemple #20
0
        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();
        }
Exemple #21
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();
        }
Exemple #22
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();
        }
Exemple #23
0
 public void InitiateTapeFromModelFwdCurvesOnly()
 {
     ADouble[] variables = CreateAdVariableArrayFwdCurvesOnly();
     AADTape.Initialize(variables);
 }
Exemple #24
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();
        }
Exemple #25
0
 // Assign ADouble to the tape. This is used when instantiating the tape.
 public void Assign()
 {
     PlacementInTape = AADTape._tapeCounter;
     AADTape.AddEntry((int)AADUtility.AADCalculationType.Const, AADTape._tapeCounter, 0, 0, Value);
 }
Exemple #26
0
 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));
 }