Exemple #1
0
    // 13.12.4	Bilinear Interpolation
    public static void BilinearInterpolation1()
    {
        Console.WriteLine("Vectors initialised: ");

        // Create mesh arrays
        int startIndex = 0;

        int             N     = 1;
        Vector <double> x1arr = new Vector <double>(N + 1, startIndex, 0.0);

        x1arr[0] = 20.0; x1arr[1] = 21.0;

        Vector <double> x2arr = new Vector <double>(x1arr);

        x2arr[0] = 14.0; x2arr[1] = 15.0;

        Console.WriteLine(x1arr);
        Console.WriteLine(x2arr);

        // Control values;
        NumericMatrix <double> Control
            = new NumericMatrix <double>(N + 1, N + 1, startIndex, startIndex);

        Control[0, 0] = 91.0; Control[1, 1] = 95.0;
        Control[0, 1] = 210.0; Control[1, 0] = 162.0;

        BilinearInterpolator myInterpolator
            = new BilinearInterpolator(x1arr, x2arr, Control);

        double x = 20.2; double y = 14.5;  // 146.1

        double value = myInterpolator.Solve(x, y);

        Console.WriteLine("Interpolated value: {0}", value);
    }
Exemple #2
0
    public static double CapBlack(string Tenor, double strike, double N, IRateCurve curve, BilinearInterpolator VolCapletMatrix)
    {
        Date      refDate = curve.RefDate();
        SwapStyle y       = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(refDate, 0, Tenor, curve.GetSwapStyle().buildingBlockType);

        double[] yf       = y.scheduleLeg2.GetYFVect(Dc._Act_360);
        int      toRemove = yf.Length - 1;

        yf = yf.Where((val, inx) => inx != toRemove).ToArray();

        List <Date> ToDate = y.scheduleLeg2.toDates.ToList();

        ToDate.RemoveAt(ToDate.Count - 1);      // remove last element
        double[] T = (from c in ToDate
                      select refDate.YF_365(c)).ToArray();

        // df- getting relevant dates
        Date[] dfDate  = y.scheduleLeg2.payDates;
        int    Ncaplet = yf.Length;   // number of caplets

        double[] df = new double[Ncaplet];
        // fwd rate
        double[] fwd     = new double[Ncaplet];
        Date[]   fwdDate = y.scheduleLeg2.fromDates;

        for (int i = 0; i < Ncaplet; i++) // Note the loop start from 1
        {                                 // first discount factor is on first payment date of caplet (first caplet skipped)
            df[i]  = curve.Df(dfDate[i + 1]);
            fwd[i] = curve.Fwd(fwdDate[i + 1]);
        }

        double[] sigma = (from t in T
                          select VolCapletMatrix.Solve(t, strike)).ToArray();
        return(CapBlack(T, yf, N, strike, sigma, df, fwd));
    }
Exemple #3
0
 // Cap Price using VolCapletMatrix as argument
 public static double CapBlack(double[] T, double[] yf, double N, double K, BilinearInterpolator VolCapletMatrix, double[] df, double[] fwd)
 {
     // LINQ: getting interpolated vol
     double[] sigma = (from t in T
                       select VolCapletMatrix.Solve(t, K)).ToArray();
     return(CapBlack(T, yf, N, K, sigma, df, fwd));
 }
Exemple #4
0
    public static double Swaption(double N, double K, string Start, string SwapTenor, bool isPayer, BilinearInterpolator VolMatrix, IRateCurve Curve)
    {
        Date refDate = Curve.RefDate();                      // curve ref date
        // false/true with fwd swap matrix
        Date   startDate = refDate.add_period(Start, false); // swap start 2 business days after the expiry
        Date   expDate   = startDate.add_workdays(-2);       // expiry of swaption
        Date   today     = refDate.add_workdays(-2);
        double T         = today.YF_365(expDate);
        Period p         = new Period(SwapTenor); // should be in year 1Y, 2Y (not 3m,...)
        double sigma     = VolMatrix.Solve(T, p.tenor);

        return(Swaption(N, K, Start, SwapTenor, isPayer, sigma, Curve));
    }
Exemple #5
0
    public static void BilinearInterpolation2()
    {
        // Create mesh arrays
        int startIndex = 0;

        // Number of subdivisions N,M in the x and y directions
        int             N     = 4;
        int             M     = 3;
        Vector <double> x1arr = new Vector <double>(N + 1, startIndex, 0.0);

        double a = 0.0; double b = 1.0;

        double h1 = (b - a) / (double)N;

        x1arr[x1arr.MinIndex] = a;

        for (int j = x1arr.MinIndex + 1; j <= x1arr.MaxIndex; j++)
        {
            x1arr[j] = x1arr[j - 1] + h1;
        }

        Vector <double> x2arr = new Vector <double>(M + 1, startIndex, 0.0);
        double          h2    = (b - a) / (double)M;

        x1arr[x1arr.MinIndex] = a;

        for (int j = x2arr.MinIndex + 1; j <= x2arr.MaxIndex; j++)
        {
            x2arr[j] = x2arr[j - 1] + h2;
        }
        Console.WriteLine(x1arr);
        Console.WriteLine(x2arr);

        // Control values;
        NumericMatrix <double> Control
            = new NumericMatrix <double>(N + 1, M + 1,
                                         startIndex, startIndex);

        Func <double, double, double> func = (double x1, double x2) => x1 + x2;

        for (int i = Control.MinRowIndex; i <= Control.MaxRowIndex; i++)
        {
            for (int j = Control.MinColumnIndex; j <= Control.MaxColumnIndex; j++)
            {
                Control[i, j] = func(x1arr[i], x2arr[j]);
            }
        }

        BilinearInterpolator myInterpolator
            = new BilinearInterpolator(x1arr, x2arr, Control);

        double x = 0.1; double y = 0.7;
        double value = myInterpolator.Solve(x, y);

        Console.WriteLine("Interpolated value: {0}", value);

        // Take center point (xm, ym) of each element and interpolate on it
        NumericMatrix <double> InterpolatedMatrix
            = new NumericMatrix <double>(N, M, startIndex, startIndex);

        // Abscissa points of new interpolated matrix
        Vector <double> Xarr
            = new Vector <double>(InterpolatedMatrix.Rows, startIndex, 0.0);
        Vector <double> Yarr
            = new Vector <double>(InterpolatedMatrix.Columns, startIndex, 0.0);

        for (int i = InterpolatedMatrix.MinRowIndex;
             i <= InterpolatedMatrix.MaxRowIndex; i++)
        {
            for (int j = InterpolatedMatrix.MinColumnIndex;
                 j <= InterpolatedMatrix.MaxColumnIndex; j++)
            {
                Xarr[i] = 0.5 * (x1arr[i] + x1arr[i + 1]);   // xm
                Yarr[j] = 0.5 * (x2arr[j] + x2arr[j + 1]);   // ym

                InterpolatedMatrix[i, j]
                    = myInterpolator.Solve(Xarr[i], Yarr[j]);
            }
        }

        // Present the interpolated matrix in Excel
        ExcelMechanisms driver = new ExcelMechanisms();

        string title = "Interpolated matrix";

        driver.printMatrixInExcel <double>(InterpolatedMatrix,
                                           Xarr, Yarr, title);
    }