Ejemplo n.º 1
0
        // 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);
        }
Ejemplo n.º 2
0
        public ADouble ParDepositRateAD(Deposit deposit)
        {
            ADouble discFactor = ADDiscCurve.DiscFactor(deposit.StartDate, deposit.EndDate, deposit.DayCount, Interpolation);
            ADouble cvg        = DateHandling.Cvg(deposit.StartDate, deposit.EndDate, deposit.DayCount);

            return(1.0 / cvg * (1.0 / discFactor - 1.0));
        }
Ejemplo n.º 3
0
        public ADouble DepositNpvAD(Deposit deposit)
        {
            ADouble discFactor = ADDiscCurve.DiscFactor(deposit.AsOf, deposit.EndDate, deposit.DayCount, Interpolation);
            ADouble cvg        = DateHandling.Cvg(deposit.StartDate, deposit.EndDate, deposit.DayCount);

            return(deposit.TradeSign * discFactor * deposit.Notional * (1.0 + deposit.FixedRate * cvg));
        }
Ejemplo n.º 4
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);
 }
Ejemplo n.º 5
0
        public ADouble IrParSwapRateAD(IrSwap swap)
        {
            ADouble floatPv      = ValueFloatLegAD(swap.FloatLeg) / swap.FloatLeg.Notional;
            ADouble fixedAnnuity = AnnuityAD(swap.FixedLeg.Schedule, Interpolation);

            return(floatPv / fixedAnnuity);
        }
Ejemplo n.º 6
0
        public ADouble ParBasisSpreadTwoLegsAD(TenorBasisSwap swap)
        {
            ADouble pvNoSpread    = ValueFloatLegNoSpreadAD(swap.FloatLegNoSpread) / swap.FloatLegNoSpread.Notional;
            ADouble pvSpread      = ValueFloatLegNoSpreadAD(swap.FloatLegSpread) / swap.FloatLegSpread.Notional;
            ADouble annuitySpread = AnnuityAD(swap.FloatLegSpread.Schedule, Interpolation);

            return((pvNoSpread - pvSpread) / annuitySpread);
        }
Ejemplo n.º 7
0
        public ADouble OisSwapNpvAD(OisSwap swap, InterpMethod interpolation)
        {
            ADouble oisAnnuity = OisAnnuityAD(swap.FloatSchedule, interpolation);
            double  notional   = swap.TradeSign * swap.Notional;
            ADouble oisRate    = OisRateSimpleAD(swap, interpolation);

            return(notional * (oisRate - swap.FixedRate) * oisAnnuity);
        }
Ejemplo n.º 8
0
        public ADouble FwdRate(DateTime asOf, DateTime startDate, DateTime endDate, DayRule dayRule, DayCount dayCount, InterpMethod interpolation)
        {
            ADouble ps  = DiscFactor(asOf, startDate, dayCount, interpolation);
            ADouble pe  = DiscFactor(asOf, endDate, dayCount, interpolation);
            ADouble cvg = DateHandling.Cvg(startDate, endDate, dayCount);

            return((ps / pe - 1.0) / cvg);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        public ADouble FraNpvAD(Fra fra)
        {
            ADouble fraRate    = ADFwdCurveCollection.GetCurve(fra.ReferenceIndex).FwdRate(fra.AsOf, fra.StartDate, fra.EndDate, fra.DayRule, fra.DayCount, Interpolation);
            ADouble notional   = fra.Notional;
            ADouble discFactor = ADDiscCurve.DiscFactor(fra.AsOf, fra.EndDate, fra.DayCount, Interpolation);
            ADouble coverage   = DateHandling.Cvg(fra.StartDate, fra.EndDate, fra.DayCount);

            return(fra.TradeSign * notional * discFactor * coverage * (fraRate - fra.FixedRate));
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
        public ADouble OisRateSimpleAD(OisSwap swap, InterpMethod interpolation)
        {
            // Simple calculation of par OIS rate. Holds only under the assumption
            // that FRA's can be perfectly hedged by OIS zero coupon bonds.

            ADouble annuity = OisAnnuityAD(swap.FixedSchedule, interpolation);

            return((DiscFactor(swap.AsOf, swap.StartDate, swap.FixedSchedule.DayCount, interpolation) - DiscFactor(swap.AsOf, swap.EndDate, swap.FixedSchedule.DayCount, interpolation)) / annuity);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
        public ADouble FuturesNpvAD(Futures future)
        {
            Fra     fra         = future.FraSameSpec;
            ADouble fraRate     = ParFraRateAD(fra);
            ADouble notional    = fra.Notional;
            ADouble convexity   = future.Convexity;
            ADouble futuresRate = fraRate + convexity;

            return(notional * (1.0 - futuresRate));
        }
Ejemplo n.º 19
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();
        }
Ejemplo n.º 20
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();
        }
Ejemplo n.º 21
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();
        }
Ejemplo n.º 22
0
        public ADouble AnnuityAD(DateTime asOf, DateTime startDate, DateTime endDate, CurveTenor tenor, DayCount dayCount, DayRule dayRule, InterpMethod interpolation)
        {
            SwapSchedule annuitySchedule = new SwapSchedule(asOf, startDate, endDate, dayCount, dayRule, tenor);
            ADouble      result          = 0.0;

            for (int i = 0; i < annuitySchedule.AdjEndDates.Count; i++)
            {
                result = result + annuitySchedule.Coverages[i] * ADDiscCurve.DiscFactor(asOf, annuitySchedule.AdjEndDates[i], dayCount, interpolation);
            }
            return(result);
        }
Ejemplo n.º 23
0
        private double GoalFunction_AD(CurveTenor[] tenors, LinearRateModel model)
        {
            ADouble goalSum = 0.0;

            for (int i = 0; i < tenors.Length; i++)
            {
                goalSum = goalSum + _problemMap[tenors[i]].GoalFunction_AD(model, _settings.Scaling);
            }

            return(goalSum.Value);
        }
Ejemplo n.º 24
0
        public ADouble OisAnnuityAD(OisSchedule schedule, InterpMethod interpolation)
        {
            ADouble output = 0.0;
            ADouble discFactor;

            for (int i = 0; i < schedule.AdjEndDates.Count; i++)
            {
                discFactor = DiscFactor(schedule.AsOf, schedule.AdjEndDates[i], schedule.DayCount, interpolation);
                output     = output + schedule.Coverages[i] * discFactor;
            }
            return(output);
        }
Ejemplo n.º 25
0
        public ADouble GoalFunction_AD(LinearRateModel model, double scaling = 1.0)
        {
            ADouble quadraticSum = 0.0;

            for (int i = 0; i < InputInstruments.Count; i++)
            {
                ADouble tempDifference = InputInstruments[i].ValueInstrumentAD(model, Factory) - InputInstruments[i].QuoteValue;
                quadraticSum = quadraticSum + ADouble.Pow(tempDifference, 2.0) * scaling;
            }

            return(quadraticSum);
        }
Ejemplo n.º 26
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();
        }
Ejemplo n.º 27
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();
        }
Ejemplo n.º 28
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();
        }
Ejemplo n.º 29
0
        // for IComparable (sorting)
        public int CompareTo(Object obj)
        {
            ADouble otherAdouble = obj as ADouble;

            if (this.Value > otherAdouble.Value)
            {
                return(-1);
            }
            if (this.Value == otherAdouble.Value)
            {
                return(0);
            }

            return(1);
        }
Ejemplo n.º 30
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();
        }