protected void PrePorcessData(RateSet rateSetMC, ISingleRateCurve DiscountingCurve)
    {
        this.refDate    = rateSetMC.refDate;
        this.DCurve     = DiscountingCurve;    // my curve used in discounting
        FWDInterpolator = new Interpolation(); // Interpolator used in fwd
        this.mktRateSet = rateSetMC;           // pass market rate set

        // Create Building block
        IEnumerable <BuildingBlock> BB = mktRateSet.GetArrayOfBB();

        // Sort ascending end date
        BBArray = from c in BB
                  orderby c.endDate.SerialValue ascending
                  select c;

        // Only Given Swap from BBArray
        OnlyGivenSwap = (from c in BBArray
                         where c.GetType().BaseType == typeof(SwapStyle)
                         select(SwapStyle) c).ToArray();


        // validating underlying tenor: swap should be all vs the same tenor
        string UnderlyingTenor = ((SwapStyle)OnlyGivenSwap.First()).swapLeg2.UnderlyingRateTenor;

        // Getting the fixing
        fixing = (from c in BBArray
                  where c.GetType().BaseType == typeof(OnePaymentStyle)
                  where c.endDate == refDate.add_period(UnderlyingTenor)
                  select c.rateValue).Single();

        // From date of each fwd rate from longer swap(LS)
        // FromDatesSerial = Date.GetSerialValue(OnlyGivenSwap.Last().scheduleLeg2.fromDates);
        List <double> SerialDate = (from c in OnlyGivenSwap
                                    select c.scheduleLeg2.fromDates.Last().SerialValue).ToList <double>();

        // adding reference date at beginning (this is important since I use the fixing)
        SerialDate.Insert(0, refDate.SerialValue);
        FromDatesSerial = SerialDate.ToArray();

        // some data validation: swap should be all of same building block

        // the type of building block
        BuildingBlockType BBT = OnlyGivenSwap[0].buildingBlockType;

        // Are all them the same?
        bool IsSameSwapType = OnlyGivenSwap.All(s => s.buildingBlockType == BBT);

        if (IsSameSwapType)  // if true
        {
            // it is swap type used as inputs (i.e. EurSwapVs6m, EurSwapVs3m, ...)
            SwapType = (SwapStyle) new BuildingBlockFactory().CreateEmptyBuildingBlock(BBT);
        }
        else
        {
            throw new ArgumentException("error in building blocktype"); // if not throw an exception
        }
    }
Example #2
0
    public RateSet mktRateSet;                  // market starting data

    // constructor: RateSet rateSet are market data inputs
    public SingleCurveBuilder(RateSet rateSet)
    {
        // RefDate
        refDate            = rateSet.refDate;
        PostProcessInterpo = new Interpolation(); // Post process interpolator
        interpAdapter      = new DoInterpOn();
        mktRateSet         = rateSet;

        // Create Building block
        IEnumerable <BuildingBlock> BB = rateSet.GetArrayOfBB();

        // Sort ascending end date
        BBArray = from c in BB
                  orderby c.endDate.SerialValue ascending
                  select c;

        // Only Given Swap from BBArray
        OnlyGivenSwap = (from c in BBArray
                         where c.GetType().BaseType == typeof(SwapStyle)
                         select(SwapStyle) c).ToArray();

        // some data validation: swap should be all of same building block
        // the type of building block
        BuildingBlockType BBT = OnlyGivenSwap[0].buildingBlockType;

        // Are all them the same?
        bool IsSameSwapType = OnlyGivenSwap.All(s => s.buildingBlockType == BBT);

        if (IsSameSwapType)  // if true
        {
            // it is swap type used as inputs (i.e. EurSwapVs6m, EurSwapVs3m, ...)
            SwapType = (SwapStyle) new BuildingBlockFactory().CreateEmptyBuildingBlock(BBT);
        }
        else
        {
            throw new ArgumentException("error in building blocktype"); // if not throw an exception
        }
    }
    // Print on excel forward rate using different curve builder for OIS fwd 3m
    public static void CheckFwdRatesOIS3m()
    {
        #region Inputs
        // ref date
        Date refDate = (new Date(DateTime.Now)).mod_foll();

        // I populate market rates set: from file, from real time, ...
        RateSet mktRates = new RateSet(refDate);

        mktRates.Add(2.338e-2, "1d", BuildingBlockType.EURDEPO);    //
        mktRates.Add(2.272e-2, "1w", BuildingBlockType.EONIASWAP);  //
        mktRates.Add(2.241e-2, "2w", BuildingBlockType.EONIASWAP);  //
        mktRates.Add(2.16e-2, "3w", BuildingBlockType.EONIASWAP);   //
        mktRates.Add(2.226e-2, "1m", BuildingBlockType.EONIASWAP);  //
        mktRates.Add(2.299e-2, "2m", BuildingBlockType.EONIASWAP);  //
        mktRates.Add(2.323e-2, "3m", BuildingBlockType.EONIASWAP);  //
        mktRates.Add(2.344e-2, "4m", BuildingBlockType.EONIASWAP);  //
        mktRates.Add(2.371e-2, "5m", BuildingBlockType.EONIASWAP);  //
        mktRates.Add(2.39e-2, "6m", BuildingBlockType.EONIASWAP);   //
        mktRates.Add(2.41e-2, "7m", BuildingBlockType.EONIASWAP);   //
        mktRates.Add(2.4316e-2, "8m", BuildingBlockType.EONIASWAP); //
        mktRates.Add(2.449e-2, "9m", BuildingBlockType.EONIASWAP);  //
        mktRates.Add(2.466e-2, "10m", BuildingBlockType.EONIASWAP); //
        mktRates.Add(2.48e-2, "11m", BuildingBlockType.EONIASWAP);  //

        mktRates.Add(2.529e-2, "15m", BuildingBlockType.EONIASWAP); //
        mktRates.Add(2.565e-2, "18m", BuildingBlockType.EONIASWAP); //
        mktRates.Add(2.603e-2, "21m", BuildingBlockType.EONIASWAP); //

        mktRates.Add(2.493e-2, "1Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(2.644e-2, "2Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(2.849e-2, "3Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(3.08e-2, "4Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(3.292e-2, "5Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(3.471e-2, "6Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(3.621e-2, "7Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(3.748e-2, "8Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(3.86e-2, "9Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(3.965e-2, "10Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(4.064e-2, "11Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(4.155e-2, "12Y", BuildingBlockType.EONIASWAP);
        // From here interpolation is need
        mktRates.Add(4.358e-2, "15Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(4.48e-2, "20Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(4.465e-2, "25Y", BuildingBlockType.EONIASWAP);
        mktRates.Add(4.415e-2, "30Y", BuildingBlockType.EONIASWAP);

        List <IRateCurve> CurveList   = new List <IRateCurve>(); // list containing curve
        List <string>     CurveString = new List <string>();     // list containing labels

        #endregion end Inputs

        #region building curve
        SingleCurveBuilderStandard <OnDf, LinearInterpolator> C1 = new SingleCurveBuilderStandard <OnDf, LinearInterpolator>(mktRates, OneDimensionInterpolation.Linear);
        SingleCurveBuilderInterpBestFit <OnLogDf, SimpleCubicInterpolator> C2 = new SingleCurveBuilderInterpBestFit <OnLogDf, SimpleCubicInterpolator>(mktRates);
        #endregion end building curve

        // populate lists
        CurveList.Add(C1); CurveString.Add(C1.ToString());
        CurveList.Add(C2); CurveString.Add(C2.ToString());

        #region printing output
        // I get the longer eonia swap available from the input data
        SwapStyle LS = (SwapStyle)mktRates.GetArrayOfBB().Last();
        Schedule  s  = new Schedule(refDate, LS.endDate, "3m", Rule.Backward, LS.swapLeg1.SwapBusDayRollsAdj, "0d", LS.swapLeg1.SwapBusDayPayAdj);

        Dc     dc       = Dc._Act_360;
        Date[] FromDate = s.fromDates;
        Date[] ToDate   = s.toDates;
        int    N        = FromDate.Length;
        List <Vector <double> > Fwds = new List <Vector <double> >();
        double[] dt = new double[N];
        for (int i = 0; i < N; i++)
        {
            dt[i] = FromDate[0].YF(ToDate[i], Dc._30_360);
        }

        foreach (IRateCurve myC in CurveList)
        {
            double[] fwd = new double[N];
            for (int i = 0; i < N; i++)
            {
                double yf     = FromDate[i].YF(ToDate[i], dc);
                double df_ini = myC.Df(FromDate[i]);
                double df_end = myC.Df(ToDate[i]);
                fwd[i] = ((df_ini / df_end) - 1) / yf;
            }
            Fwds.Add(new Vector <double>(fwd));
        }

        ExcelMechanisms exl = new ExcelMechanisms();

        exl.printInExcel(new Vector <double>(dt), CurveString, Fwds, "Fwd 3M", "time", "rate"); // .printInExcel<T>
        #endregion end printing output
    }
    // Print on excel forward rate using different curve builder for 3m
    public static void CheckFwdRatesVs3m()
    {
        #region Inputs
        // Start input
        Date refDate = (new Date(DateTime.Now)).mod_foll();

        // I populate market rates set: from file, from real time, ...
        RateSet mktRates = new RateSet(refDate);

        // Depos
        mktRates.Add(0.434e-2, "3m", BuildingBlockType.EURDEPO);

        // Swap Vs 3M
        mktRates.Add(0.813e-2, "1Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(1.096e-2, "2Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(1.322e-2, "3Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(1.529e-2, "4Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(1.709e-2, "5Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(1.862e-2, "6Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(1.991e-2, "7Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(2.101e-2, "8Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(2.197e-2, "9Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(2.285e-2, "10Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(2.443e-2, "12Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(2.614e-2, "15Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(2.711e-2, "20Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(2.671e-2, "25Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(2.589e-2, "30Y", BuildingBlockType.EURSWAP3M);
        #endregion end Inputs

        #region building curve

        SingleCurveBuilderInterpBestFit <OnLogDf, SimpleCubicInterpolator> C1 = new SingleCurveBuilderInterpBestFit <OnLogDf, SimpleCubicInterpolator>(mktRates);
        double firstFixing = 0.434e-2; SingleCurveBuilderSmoothingFwd <OnLogDf, SimpleCubicInterpolator> C2 = new SingleCurveBuilderSmoothingFwd <OnLogDf, SimpleCubicInterpolator>(mktRates, firstFixing);
        SingleCurveBuilderStandard <OnLogDf, LinearInterpolator> C3 = new SingleCurveBuilderStandard <OnLogDf, LinearInterpolator>(mktRates, OneDimensionInterpolation.Linear);
        #endregion end building curve

        List <IRateCurve> CurveList   = new List <IRateCurve>(); // list containing curve
        List <string>     CurveString = new List <string>();     // list containing labels

        // populate lists
        CurveList.Add(C1); CurveString.Add(C1.ToString());
        CurveList.Add(C2); CurveString.Add(C2.ToString());
        CurveList.Add(C3); CurveString.Add(C3.ToString());

        #region printing output
        // I get the longer swap
        SwapStyle LS = (SwapStyle)mktRates.GetArrayOfBB().Last();

        Dc     dc       = Dc._Act_360;
        Date[] FromDate = LS.scheduleLeg2.fromDates;
        Date[] ToDate   = LS.scheduleLeg2.toDates;
        int    N        = FromDate.Length;
        List <Vector <double> > Fwds = new List <Vector <double> >();
        double[] dt = new double[N];
        for (int i = 0; i < N; i++)
        {
            dt[i] = FromDate[0].YF(ToDate[i], Dc._30_360);
        }

        foreach (IRateCurve myC in CurveList)
        {
            double[] fwd = new double[N];
            for (int i = 0; i < N; i++)
            {
                double yf     = FromDate[i].YF(ToDate[i], dc);
                double df_ini = myC.Df(FromDate[i]);
                double df_end = myC.Df(ToDate[i]);
                fwd[i] = ((df_ini / df_end) - 1) / yf;
            }
            Fwds.Add(new Vector <double>(fwd));
        }

        ExcelMechanisms exl = new ExcelMechanisms();

        exl.printInExcel(new Vector <double>(dt), CurveString, Fwds, "Fwd vs 3M", "time", "rate"); // .printInExcel<T>
        #endregion end printing output
    }
    // Check if the process will match the starting inputs
    public static void CheckInputsVs3m()
    {
        #region Inputs
        // Start input
        Date refDate = (new Date(DateTime.Now)).mod_foll();

        // I populate markets rates set: from file, from real time, ...
        RateSet mktRates = new RateSet(refDate);

        // Depos
        mktRates.Add(2.243e-2, "1m", BuildingBlockType.EURDEPO);
        mktRates.Add(2.435e-2, "3m", BuildingBlockType.EURDEPO);

        // Swap Vs 3M
        mktRates.Add(2.869e-2, "1Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(3.316e-2, "2Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(3.544e-2, "3Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(3.745e-2, "4Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(3.915e-2, "5Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.057e-2, "6Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.175e-2, "7Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.273e-2, "8Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.362e-2, "9Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.442e-2, "10Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.589e-2, "12Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.750e-2, "15Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.835e-2, "20Y", BuildingBlockType.EURSWAP3M);
        mktRates.Add(4.787e-2, "25Y", BuildingBlockType.EURSWAP3M);

        #endregion end Inputs

        // Uncomment to chose the curve
        // SingleCurveBuilderStandard<OnLogDf, LinearInterpolator> C = new SingleCurveBuilderStandard<OnLogDf, LinearInterpolator>(mktRates, OneDimensionInterpolation.Linear);
        double firstFixing = 1.435e-2; SingleCurveBuilderSmoothingFwd <OnLogDf, LinearInterpolator> C = new SingleCurveBuilderSmoothingFwd <OnLogDf, LinearInterpolator>(mktRates, firstFixing);
        // SingleCurveBuilderInterpBestFit<OnLogDf, LinearInterpolator> C = new SingleCurveBuilderInterpBestFit<OnLogDf, LinearInterpolator>(mktRates);

        #region print output
        IEnumerable <BuildingBlock> BBArray = mktRates.GetArrayOfBB();

        // Only Given Swap from BBArray
        IEnumerable <BuildingBlock> OnlyGivenDepo = from c in BBArray
                                                    where c.GetType().BaseType == typeof(OnePaymentStyle)
                                                    select c;

        Console.WriteLine(C.ToString());

        Console.WriteLine("Recalc Df at Ref Date: {0}", C.DF(refDate));

        foreach (OnePaymentStyle BB in OnlyGivenDepo)
        {
            double yf       = refDate.YF(BB.endDate, BB.dayCount);
            double df       = C.DF(BB.endDate);
            double CalcRate = ((1 / df) - 1) / yf;
            Console.WriteLine("{0}  Input Rate: {1}  Recalc Rate: {2}", BB.Tenor.GetPeriodStringFormat(), BB.rateValue, CalcRate);
        }

        // Only Given Swap from BBArray
        IEnumerable <BuildingBlock> OnlyGivenSwap = from c in BBArray
                                                    where c.GetType().BaseType == typeof(SwapStyle)
                                                    select c;

        foreach (SwapStyle BB in OnlyGivenSwap)
        {
            // fixed leg data
            double[] yfFixLeg = BB.scheduleLeg1.GetYFVect(BB.swapLeg1.DayCount); // fixed is leg 1

            // dfs array of fixed lag
            Date[] dfDates = BB.scheduleLeg1.payDates; // serial date of fixed lag (each dates we should find df)

            // initialise array for df
            double[] dfFixLeg = new double[dfDates.Length];

            // calculate df
            for (int i = 0; i < dfDates.Length; i++)
            {
                dfFixLeg[i] = C.DF(dfDates[i]);
            }

            // Interpolation Methods for Curve Construction PATRICK S. HAGAN & GRAEME WEST Applied Mathematical Finance,Vol. 13, No. 2, 89–129, June 2006
            // Formula 2) page 4
            double CalcRate = Formula.ParRate(yfFixLeg, dfFixLeg); // Calculate par rate

            Console.WriteLine("{0}  Input Rate: {1}  Recalc Rate: {2}", BB.Tenor.GetPeriodStringFormat(), BB.rateValue, CalcRate);
        }
        #endregion end print output
    }