Пример #1
0
 public static void BlackScholesNoReset(ADouble vol, ADouble spot, ADouble rate, ADouble time, ADouble mat, ADouble 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);
 }
Пример #2
0
        public static ADouble InvNormalCdf(ADouble p)
        {
            // Courtesy to Antoine Savine (Danske Bank)
            if (p > 0.5)
            {
                return(-InvNormalCdf(1.0 - p));
            }

            ADouble Up = new ADouble();

            if (p < 1.0e-15)
            {
                Up = new ADouble(1.0e-15);
            }
            else
            {
                Up = p;
            }

            //	constants
            double a0 = 2.50662823884;
            double a1 = -18.61500062529;
            double a2 = 41.39119773534;
            double a3 = -25.44106049637;

            double b0 = -8.47351093090;
            double b1 = 23.08336743743;
            double b2 = -21.06224101826;
            double b3 = 3.13082909833;

            double c0 = 0.3374754822726147;
            double c1 = 0.9761690190917186;
            double c2 = 0.1607979714918209;
            double c3 = 0.0276438810333863;
            double c4 = 0.0038405729373609;
            double c5 = 0.0003951896511919;
            double c6 = 0.0000321767881768;
            double c7 = 0.0000002888167364;
            double c8 = 0.0000003960315187;

            ADouble x = Up - 0.5;
            ADouble r = new ADouble();

            if (Math.Abs(x.Value) < 0.42)
            {
                r = x * x;
                r = x * (((a3 * r + a2) * r + a1) * r + a0) / ((((b3 * r + b2) * r + b1) * r + b0) * r + 1.0);
                return(r);
            }

            // Log-Log approximation
            r = Up;
            r = ADouble.Log(-ADouble.Log(r));
            r = c0 + r * (c1 + r * (c2 + r * (c3 + r * (c4 + r * (c5 + r * (c6 + r * (c7 + r * c8)))))));

            r = -r;
            return(r);
        }
Пример #3
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();
        }
Пример #4
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();
        }
Пример #5
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();
        }
Пример #6
0
 // For example in thesis
 public static ADouble ExampleFunctionThesis(ADouble x1, ADouble x2)
 {
     return(2.0 * x1 * x2 + ADouble.Log(x1 - 4.0 * x2));
 }