public static void InterpolationExample()
    {
        ExcelMechanisms exl = new ExcelMechanisms();
        // I Create initial t and r arrays.
        Vector <double> t = new Vector <double>(new double[] { 0.1, 1, 4, 9, 20, 30 }, 0);
        Vector <double> r = new Vector <double>(new double[] { 0.081, 0.07, 0.044, 0.07, 0.04, 0.03 }, 0);

        // A Compute log df
        Vector <double> logDF = new Vector <double>(r.Size, r.MinIndex);

        for (int n = logDF.MinIndex; n <= logDF.MaxIndex; ++n)
        {
            logDF[n] = Math.Log(Math.Exp(-t[n] * r[n]));
        }

        // II Hyman interpolator
        HymanHermiteInterpolator_V4 myInterpolatorH =
            new HymanHermiteInterpolator_V4(t, logDF);
        int             M    = 299;
        Vector <double> term = new Vector <double>(M, 1);

        term[term.MinIndex] = 0.1;
        double step = 0.1;

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

        // III Compute interpolated values
        Vector <double> interpolatedlogDFH = myInterpolatorH.Curve(term);

        exl.printOneExcel <double>(term, interpolatedlogDFH, "Hyman cubic", "t", "CS", "int logDF");

        // IV Compute continuously compounded rate from the ZCB Z(0,t),
        // using equation (3) Hagan and West (2008).
        Vector <double> rCompounded = new Vector <double>(interpolatedlogDFH.Size, interpolatedlogDFH.MinIndex);

        for (int j = rCompounded.MinIndex; j <= rCompounded.MaxIndex; j++)
        {
            rCompounded[j] = -interpolatedlogDFH[j] / term[j];
        }
        exl.printOneExcel <double>(term, rCompounded, "RCompound Hyman Cubic", "term", "r cns compound", "r com");

        // V Compute discrete forward rates using equation (6)
        // from Hagan and West (2008)
        Vector <double> f = new Vector <double>(rCompounded.Size, rCompounded.MinIndex);

        f[f.MinIndex] = 0.081;
        for (int j = f.MinIndex + 1; j <= rCompounded.MaxIndex; j++)
        {
            f[j] = (rCompounded[j] * term[j] - rCompounded[j - 1] * term[j - 1]) / (term[j] - term[j - 1]);
        }
        exl.printOneExcel <double>(term, f, "Hyman Cubic", "term", "discrete fwd", "dis fwd");
    }
    public static void SimplifiedMonotoneConvex()
    {
        SortedList <double, double> TermRate = new SortedList <double, double>();

        #region using a different contructor
        double[] t = new double[] { 0.1, 1, 4, 9, 20, 30 };
        double[] r = new double[] { 0.081, 0.07, 0.044, 0.07, 0.04, 0.03 };

        #endregion
        // We consider inputs as continuous rates and negative forwards are not allowed

        // Monotone Convex interpolator (Hagan-West approach)
        MonotoneConvex HaganWest = new MonotoneConvex(t, r);
        List <double>  rr        = new List <double>();
        List <double>  fwd       = new List <double>();
        List <double>  tt        = new List <double>();
        double         step      = 0.01;
        int            MaxIndex  = System.Convert.ToInt32((100 * HaganWest.dTerms.Last() + 20));

        double x_ = 0.0;
        for (int i = 0; i <= MaxIndex; i++)
        {
            x_ = step * i;
            tt.Add(x_);
            double k = HaganWest.Interpolant(x_);
            rr.Add(HaganWest.Interpolant(x_));
            fwd.Add(HaganWest.Forward(x_));
        }
        // Create the abscissa values
        double[] terms = tt.ToArray();
        // Compute interpolated values
        double[] rates    = HaganWest.GetInterpolatedRate(terms);
        double[] forwards = HaganWest.GetInterpolatedFwd(terms);
        // My excel mechanism
        ExcelMechanisms exl = new ExcelMechanisms();
        exl.printOneExcel <double>(new Vector <double>(tt.ToArray()), new Vector <double>(forwards)
                                   , "Monotone Convex", "time", "discrete forward", "dis fwd");

        exl.printOneExcel <double>(new Vector <double>(tt.ToArray()), new Vector <double>(rates)
                                   , "Monotone Convex", "time", "discrete forward", "dis fwd");
    }
示例#3
0
    // 13.12.3	Cubic Spline Interpolation
    #region CubicSplineInterpolator
    public static void LogisticI()
    {
        Console.WriteLine("Vectors initialised: ");

        int    N          = 280;
        double a          = -15.0;       // Left of interval
        double b          = 15.0;        // Right of interval
        double h          = (b - a) / N;
        int    startIndex = 0;

        Vector <double> xarr = new Vector <double>(N + 1, startIndex, 0.0);
        Vector <double> yarr = new Vector <double>(N + 1, startIndex, 0.0);

        for (int j = xarr.MinIndex; j <= xarr.MaxIndex; j++)
        {
            xarr[j] = a + h * j;
            yarr[j] = Potpourri.Sigmoid1(xarr[j]);
            // yarr[j] = Potpourri.Sigmoid2(xarr[j]);
        }

        Console.WriteLine("Vectors initialised: ");

        int FirstDeriv = 1;

        CubicSplineInterpolator csi
            = new CubicSplineInterpolator(xarr, yarr, FirstDeriv, 10, 10);

        // Display arrays in Excel
        ExcelMechanisms exl = new ExcelMechanisms();

        exl.printOneExcel <double>(xarr, csi.Curve(), "Logistic I", "x", "value", "I");

        // Now choose 1st order derivative at zero
        double leftBC  = 1.0;
        double rightBC = -1.0;
        CubicSplineInterpolator csi2 = new CubicSplineInterpolator(xarr, yarr, FirstDeriv, leftBC, rightBC);

        // Display arrays in Excel
        exl.printOneExcel(xarr, csi2.Curve(), "Logistic II", "x", "value", "II");
    }
示例#4
0
    public static void Main()
    {
        // 1. Create the option (Factory Method pattern)
        Option myOption = CreateOption();

        // 2. Define the pde of concern (Bridge pattern)
        IIBVPImp pde = new BSIBVPImp(myOption);


        // 3. Discrete mesh sizes.
        int J = 325;
        int N = J;

        Console.Write("NS: ");
        J = Convert.ToInt32(Console.ReadLine());

        Console.Write("NT (NT ~ O(NS^2 for explicit, NT any value for implicit): ");
        N = Convert.ToInt32(Console.ReadLine());

        // 4. The domain in which the PDE is defined.
        Range <double> rangeX = new Range <double>(0.0, myOption.FarFieldCondition);
        Range <double> rangeT = new Range <double>(0.0, myOption.ExpiryDate);


        // 5. Create FDM Solver.

        Console.Write("Implicit [1] or Explicit Euler [2]: ");
        int choice = Convert.ToInt32(Console.ReadLine());

        IBVPFDM fdm = new ImplicitEulerIBVP(pde, rangeX, rangeT, J, N);

        if (choice != 1)
        {
            fdm = new ExplicitEulerIBVP(pde, rangeX, rangeT, J, N);
        }

        // 6. Calculate the matrix result.
        NumericMatrix <double> sol = fdm.result();


        // 7. Display the results in Excel.
        ExcelMechanisms exl = new ExcelMechanisms();

        try
        {
            exl.printOneExcel(fdm.XValues, fdm.vecNew, "Euler method", "Stock", "Value", "V");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
示例#5
0
    public static void Main()
    {
        // 1. Create the option (Factory Method pattern)
        Option myOption = CreateOption();

        // 2. Define the pde of concern (Bridge pattern)
        IBSPde pde = new Pde_BS(myOption);


        // 3. Discrete mesh sizes.
        int J = 325;
        int N = J;

        Console.Write("NS: ");
        J = Convert.ToInt32(Console.ReadLine());

        Console.Write("NT: ");
        N = Convert.ToInt32(Console.ReadLine());

        // 4. The domain in which the PDE is defined.
        Range <double> rangeX = new Range <double>(0.0, myOption.FarFieldCondition);
        Range <double> rangeT = new Range <double>(0.0, myOption.ExpiryDate);


        // 5. Create FDM Solver.
        IBVPFDM fdm = new ADE(pde, rangeX, rangeT, J, N);


        // 6. Calculate the matrix result.
        NumericMatrix <double> sol = fdm.result();


        // 7. Display the results in Excel.
        ExcelMechanisms exl = new ExcelMechanisms();


        try
        {
            exl.printOneExcel(fdm.XValues, fdm.vecNew, "ADE method", "Stock", "Value", "V");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
// RECALL: void printOneExcel(Vector<double>  x,
//						Vector<double> functionResult,string title)

    public void displayinExcel(OptionValueType yval)
    {
        string text = "Value";

        if (yval == OptionValueType.Delta)
        {
            text = "Delta";
        }
        if (yval == OptionValueType.Gamma)
        {
            text = "Gamma";
        }

        if (yval == OptionValueType.Vega)
        {
            text = "Vega";
        }

        if (yval == OptionValueType.Theta)
        {
            text = "Theta";
        }
        if (yval == OptionValueType.Rho)
        {
            text = "Rho";
        }

        if (yval == OptionValueType.Coc)
        {
            text = "Coc";
        }

        Vector <double> yarr = calculate(yval);
        //	cout << "array ..."; int yy; cin >> yy;
        //	print(yarr);

        ExcelMechanisms excel = new ExcelMechanisms();

        excel.printOneExcel(XARR, yarr, text, text, text, text);
    }