예제 #1
0
    public static void CapletVol20Y_InputInterp()
    {
        #region Data
        // We load a set of data (not real)
        DataForCapletExample1 d = new DataForCapletExample1();
        double[] df             = d.df();
        double[] yf             = d.yf();
        double[] T        = d.T();
        double[] fwd      = d.fwd();
        double[] atmfwd   = d.atmfwd();
        double[] capVol   = d.capVol();
        double[] avT      = d.avT();
        double[] avcapVol = d.avcapVol();
        #endregion

        // All Data available
        double[] CapletVol = Formula.CapletVolBootstrapping(T, df, fwd, yf, capVol, atmfwd);

        // Cubic input
        SimpleCubicInterpolator CubicInt = new SimpleCubicInterpolator(avT, avcapVol);
        double[] cubicInterpCapVol       = CubicInt.Curve(T);
        double[] CapletVolCubicInput     = Formula.CapletVolBootstrapping(T, df, fwd, yf, cubicInterpCapVol, atmfwd);

        // Linear Input
        LinearInterpolator LinearInt            = new LinearInterpolator(avT, avcapVol);
        double[]           linearInterpCapVol   = LinearInt.Curve(T);
        double[]           CapletVolLinearInput = Formula.CapletVolBootstrapping(T, df, fwd, yf, linearInterpCapVol, atmfwd);

        #region print results
        ExcelMechanisms exl                   = new ExcelMechanisms();
        Vector <double> CapletVol_            = new Vector <double>(CapletVol, 1);
        Vector <double> CapletVolCubicInput_  = new Vector <double>(CapletVolCubicInput, 1);
        Vector <double> CapletVolLinearInput_ = new Vector <double>(CapletVolLinearInput, 1);
        Vector <double> xarr                  = new Vector <double>(T, 1);
        List <string>   labels                = new List <string>()
        {
            "CapletVol All input", "CapVol Cubic Input", "CapVol Linear Input"
        };
        List <Vector <double> > yarrs = new List <Vector <double> >()
        {
            CapletVol_, CapletVolCubicInput_, CapletVolLinearInput_
        };

        exl.printInExcel <double>(xarr, labels, yarrs, "Caplet Vol Input Interpolation", "Term", "Volatility");
        #endregion
    }
예제 #2
0
    public static void MatrixCaplet()
    {
        #region Data
        // We load a set of data (not real)
        DataForCapletExample2 d = new DataForCapletExample2();
        double[]        df      = d.df();
        double[]        yf      = d.yf();
        double[]        T       = d.T();
        double[]        avT     = d.avT();
        List <double[]> VolArr  = d.VolArr();
        double[]        fwd     = d.fwd();
        double[]        strike  = d.strike();

        List <double[]> VolSilos = new List <double[]>();
        foreach (double[] cVol in VolArr)
        {
            LinearInterpolator LinearInt = new LinearInterpolator(avT, cVol);
            VolSilos.Add(LinearInt.Curve(T));
        }

        #endregion
        // Here you can change the MonoStrikeCaplet Builder
        CapletMatrixVolBuilder <MonoStrikeCapletVolBuilderPWC> B = new CapletMatrixVolBuilder <MonoStrikeCapletVolBuilderPWC>(T, df, fwd, yf, avT, VolSilos, strike);

        // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitSmooth> B = new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitSmooth>(T, df, fwd, yf, avT, VolSilos, strike);
        // and more...

        #region print results
        ExcelMechanisms exl    = new ExcelMechanisms();
        Vector <double> xarr   = new Vector <double>(T, 1);
        List <string>   labels = new List <string>();
        foreach (double myD in strike)
        {
            labels.Add(myD.ToString());
        }
        ;
        List <Vector <double> > yarrs = new List <Vector <double> >();
        foreach (double[] arr in B.CapletVolMatrix)
        {
            yarrs.Add(new Vector <double>(arr, 1));
        }

        exl.printInExcel <double>(xarr, labels, yarrs, "Caplet Vol Input Interpolation", "Term", "Volatility");
        #endregion
    }
예제 #3
0
        public void Solve()
        {
            #region data
            DataForCapletExample1 d = new DataForCapletExample1();
            df         = d.df();
            yf         = d.yf();
            T          = d.T();
            fwd        = d.fwd();
            atmfwd     = d.atmfwd();
            x          = d.avT();      // available maturity for market data
            y          = d.avcapVol(); // available Cap volatility from market
            capPremium = new double[x.Length];
            #endregion end data

            double cP = 0.0;
            // Calculate Cap price using Cap volatility available
            for (int i = 0; i < x.Length; i++)
            {
                int maxJ = Array.IndexOf(T, x[i]); // right index
                cP = 0.0;
                for (int j = 0; j <= maxJ; j++)
                {
                    cP += Formula.CapletBlack(T[j], yf[j], 1, atmfwd[maxJ], y[i], df[j], fwd[j]);
                }
                capPremium[i] = cP; // collecting values
            }

            #region Setting up minimisation
            // Starting missing caplet vol guess
            double[] VolGuess = Enumerable.Repeat(y[0], y.Length).ToArray();

            double             epsg   = 0.000000000001; // original setting
            double             epsf   = 0;
            double             epsx   = 0;
            int                maxits = 0;
            alglib.minlmstate  state;
            alglib.minlmreport rep;

            // Number of equation to match
            int NConstrains = x.Length;

            // see alglib documentation
            alglib.minlmcreatev(NConstrains, VolGuess, 0.000001, out state);
            alglib.minlmsetcond(state, epsg, epsf, epsx, maxits);
            alglib.minlmoptimize(state, function_fvec, null, null);
            alglib.minlmresults(state, out VolGuess, out rep);
            #endregion

            // Minimisation Done!

            // Uncomment to change interpolator
            LinearInterpolator interp = new LinearInterpolator(x, VolGuess);
            // SimpleCubicInterpolator interp = new SimpleCubicInterpolator(x, VolGuess);

            double[] Vols = interp.Curve(T); // Vols from interpolator

            #region print results
            ExcelMechanisms exl    = new ExcelMechanisms();
            Vector <double> CapVol = new Vector <double>(Vols, 1);
            Vector <double> xarr   = new Vector <double>(T, 1);
            List <string>   labels = new List <string>()
            {
                "CapVol"
            };
            List <Vector <double> > yarrs = new List <Vector <double> >()
            {
                CapVol
            };

            exl.printInExcel <double>(xarr, labels, yarrs, "Caplet vs Cap Vol", "Term", "Volatility");
            #endregion
        }
예제 #4
0
    public static void MatrixCapletWithRateCurve()
    {
        #region Inputs
        // We load a set of data (not real)
        DataForCapletExample2 d = new DataForCapletExample2();
        RateSet         rs      = d.GetRateSet();
        double[]        T       = d.T();
        string[]        avT     = d.avTString();
        List <double[]> VolArr  = d.VolArr();
        double[]        strike  = d.strike();

        // We build our multi curve
        SingleCurveBuilderStandard <OnLogDf, SimpleCubicInterpolator> Curve = new SingleCurveBuilderStandard <OnLogDf, SimpleCubicInterpolator>(rs, OneDimensionInterpolation.LogLinear); // discount curve

        #endregion

        #region Testing all available MonoStrikeCapletVolBuilder
        // Uncomment one of these
        CapletMatrixVolBuilder <MonoStrikeCapletVolBuilderInputInterpLinear> B =
            new CapletMatrixVolBuilder <MonoStrikeCapletVolBuilderInputInterpLinear>(avT, Curve, strike, VolArr);

        // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderInputInterpCubic> B =
        //    new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderInputInterpCubic>(avT, Curve, strike, VolArr);

        // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitStd> B =
        //    new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitStd>(avT, Curve, strike, VolArr);

        // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitSmooth> B =
        //    new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitSmooth>(avT, Curve, strike, VolArr);

        // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitPWL> B =
        //    new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitPWL>(avT, Curve, strike, VolArr);

        // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitCubic> B =
        //    new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitCubic>(avT, Curve, strike, VolArr);

        // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitFunct> B =
        //    new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitFunct>(avT, Curve, strike, VolArr);

        // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderPWC> B =
        //    new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderPWC>(avT, Curve, strike, VolArr);

        #endregion

        List <double[]> VolSilos = B.CapletVolMatrix;

        #region print results
        ExcelMechanisms exl    = new ExcelMechanisms();
        Vector <double> xarr   = new Vector <double>(T, 1);
        List <string>   labels = new List <string>();
        foreach (double myD in strike)
        {
            labels.Add(myD.ToString());
        }
        ;
        List <Vector <double> > yarrs = new List <Vector <double> >();
        foreach (double[] arr in VolSilos)
        {
            yarrs.Add(new Vector <double>(arr, 1));
        }

        exl.printInExcel <double>(xarr, labels, yarrs, "Caplet Vol Input Interpolation", "Term", "Volatility");
        #endregion
    }
예제 #5
0
    public static void SimpleBootstrap20Y()
    {
        #region data
        // We load a set of data (not real)
        DataForCapletExample1 data1 = new DataForCapletExample1();
        double[] df        = data1.df();
        double[] yf        = data1.yf();
        double[] T         = data1.T();
        int      N         = df.Length;
        double[] fwd       = new double[N];
        double[] atmfwd    = new double[N];
        double[] capletVol = new double[N];
        double[] capPrice  = new double[N];
        double   df_ini    = data1.df_ini;
        double[] capVol    = data1.capVol();
        #endregion
        // calculate fwd
        fwd[0] = ((df_ini / df[0]) - 1) / yf[0];
        for (int i = 1; i < df.Length; i++)
        {
            fwd[i] = ((df[i - 1] / df[i]) - 1) / yf[i];
        }

        // calculate ATM strike
        double summ = 0.0;
        for (int i = 0; i < df.Length; i++)
        {
            summ     += yf[i] * df[i];
            atmfwd[i] = (df_ini - df[i]) / summ;
        }

        double shorterCap = 0.0;
        // calculate cap price using flat vol
        for (int i = 0; i < N; i++)
        {
            shorterCap = 0.0;

            for (int j = 0; j <= i; j++)
            {
                capPrice[i] += Formula.CapletBlack(T[j], yf[j], 100, atmfwd[i], capVol[i], df[j], fwd[j]);
            }
            for (int j = 0; j < i; j++)
            {
                shorterCap += Formula.CapletBlack(T[j], yf[j], 100, atmfwd[i], capletVol[j], df[j], fwd[j]);
            }

            NumMethod.myMethodDelegate fname =
                s => capPrice[i] - shorterCap - Formula.CapletBlack(T[i], yf[i], 100, atmfwd[i], s, df[i], fwd[i]);
            capletVol[i] = NumMethod.NewRapNum(fname, 0.20);
        }

        #region print results
        ExcelMechanisms exl = new ExcelMechanisms();

        Vector <double> CapletVol = new Vector <double>(capletVol, 1);
        Vector <double> CapVol    = new Vector <double>(capVol, 1);
        Vector <double> xarr      = new Vector <double>(T, 1);
        List <string>   labels    = new List <string>()
        {
            "CapletVol", "CapVol"
        };
        List <Vector <double> > yarrs = new List <Vector <double> >()
        {
            CapletVol, CapVol
        };

        exl.printInExcel <double>(xarr, labels, yarrs, "Caplet vs Cap Vol", "Term", "Volatility");
        #endregion
    }
예제 #6
0
    // 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
    }
예제 #7
0
    // 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
    }