StepMonteCarlo Example CS





http://www.QuantTools.com
CapeTools (Compact) Process Simulation function list
StepMonteCarlo function

Welcome | Documentation format | QuantTools Groups | QuantTools Categories | Licence

Key TAGs | Excel Index | API Index



Example C# Driver function. Preparing the parameters, sub-function calls and the final function call (the result).

High level view of the code structure (resulting in the final function call to StepMonteCarlo() )

These are the financial QuantTools function calls that are used within the examples :





The objects generated by these functions are inter-connected in the following way :




C# Example - StepMonteCarlo





    //     ##################################################################################
    //     The first function here StepMonteCarlo(), contains a series of
    //     function calls leading upto the main function call, the second function
    //     within this file ( StepMonteCarloPart() ).
    //     which contains the answer that we are looking for.

    //     The first function here is simply an example of how to construct the parameters 
    //     in order acquire either a string Key (that is to be passed to other functions) 
    //     or a computed result.

    //     If you are viewing this source code from the chm or web help file you can use the
    //     outlining features to collapse certain sections of the code for better readability. 
    //     ##################################################################################
    

using System;

// Optional using instruction. We will use a mix of utilising fully qualified names (in the case of the financial objects)
// and using the reduced version (in the case of declaring enumerations).
// This is just to demostrate both types of coding.

using CTQL; // You need to add a reference to the QuantToolsNET.v2.dll also

// Some global parameter in order to append to user defined keys.
// We use it here to ensure that we have unique Keys (in the case several of our examples
// use the same key-name)
// In normal use, a user defined string will be used and so this variable will be pointless.
static int nCTProcessSimCGlobal;
    
// Used by function parameters that take an optional range value. 
// In Excel we simply omit the value, within the API functions, 
// we pass an empty range object
static CTQL.CTRangeData oEmptyRange = new CTQL.CTRangeData();
    
public string CS_EX_StepMonteCarlo()
{
    nCTProcessSimCGlobal += 1;
            
    string szErrorMsg = "";

    try
    {


    //    Creates a centralized valuation date object.
    

    string MyValuationDate;
    MyValuationDate = 
        ValueDateObjPart();
    
    


    //    Creates a Generalized BlackScholes Process.
    
    string MyGBSProcess;
    MyGBSProcess = 
        GBSProcessPart(
        MyValuationDate);
    
    


    //    Creates a Generalized BlackScholes Process.
    
    string My2ndGBSProcess;
    My2ndGBSProcess = 
        GBSProcess__2Part(
        MyValuationDate);
    
    


    //    Creates a Generalized BlackScholes Process.
    
    string My3rdGBSProcess;
    My3rdGBSProcess = 
        GBSProcess__3Part(
        MyValuationDate);
    
    


    //    Creates a Generalized BlackScholes Process.
    
    string My4thGBSProcess;
    My4thGBSProcess = 
        GBSProcess__4Part(
        MyValuationDate);
    
    


    //    Creates a Generalized BlackScholes Process.
    
    string My5thGBSProcess;
    My5thGBSProcess = 
        GBSProcess__5Part(
        MyValuationDate);
    
    


    //    Creates an array of correlated one dimensional stochastic processes.
    
    string MyCorrArrayProcesses;
    MyCorrArrayProcesses = 
        CorrArrayProcessesPart(
        MyGBSProcess,
        My2ndGBSProcess,
        My3rdGBSProcess,
        My4thGBSProcess,
        My5thGBSProcess);
    
    


    //    Creates a Step Monte Carlo object given a process object and 
    //    a time line dates array.
    
    string MyStepMonteCarlo;
    MyStepMonteCarlo = 
        StepMonteCarloPart(
        MyCorrArrayProcesses,
        MyValuationDate);
    
    // This is the result we are looking for.
    return MyStepMonteCarlo;
    

    }
    catch(Exception e)
    {
        szErrorMsg = e.Message;
        throw e;
    }
                        
}                
        


// ///////////////////////////////////////////////////////////////////

private string StepMonteCarloPart(
    string MyCorrArrayProcesses,
    string MyValuationDate)
{

        //  Create example range for parameter StepMonteCarlo_MandatoryDates
        CTQL.CTRangeData StepMonteCarlo_MandatoryDates;    
        

        int[] arrBStepMonteCarlo_MandatoryDates = { 
            CTQL.Date.serialNumber("19/7/2005", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/1/2006", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/7/2006", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/1/2007", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/7/2007", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/1/2008", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/7/2008", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/1/2009", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/7/2009", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/1/2010", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/7/2010", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("19/1/2011", "dd/mm/yyyy")  //  Array Data
        
        };
        
        CTQL.IntVector arrStepMonteCarlo_MandatoryDates = 
            new  CTQL.IntVector(arrBStepMonteCarlo_MandatoryDates);
    
        // Second parameter determines whether the array is a column array (false) or a row array (true)
        StepMonteCarlo_MandatoryDates = new  CTQL.CTRangeData(arrStepMonteCarlo_MandatoryDates, false);
            


    //    Key value to use as a handle for the created object
        string MyStepMonteCarlo = "MyStepMonteCarlo" + "_" + System.Convert.ToString(nCTProcessSimCGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Used to calculate time in years.
        CTIEnums.DayCountEnum dayCounter = CTIEnums.DayCountEnum.DayCount_30360;

    //    The minimum number of steps that the discretization of the 'MandatoryDates' 
    //    parameter will take.
        int MinNoOfSteps = 50;

    //    The random generator type to use.
        string MCMethod = "Pseudo";

    //    Seed value.
        int Seed = 0;

                    
    //  Excel function call would be this - "CT.PRO.StepMonteCarlo()"

    //    Creates a Step Monte Carlo object given a process object and 
    //    a time line dates array.
        string rStepMonteCarlo;
                                        
        rStepMonteCarlo = CTQL.CTProcessSimCSA.StepMonteCarlo(
                MyStepMonteCarlo,
                Reload,
                MyCorrArrayProcesses,
                MyValuationDate,
                dayCounter,
                StepMonteCarlo_MandatoryDates,
                MinNoOfSteps,
                MCMethod,
                Seed);


    return rStepMonteCarlo;
}        



// ///////////////////////////////////////////////////////////////////

private string CorrArrayProcessesPart(
    string MyGBSProcess,
    string My2ndGBSProcess,
    string My3rdGBSProcess,
    string My4thGBSProcess,
    string My5thGBSProcess)
{

        //  Create example range for parameter CorrArrayProcesses_stochProcesses
        CTQL.CTRangeData CorrArrayProcesses_stochProcesses;    
        

        string[] arrBCorrArrayProcesses_stochProcesses = { 
            MyGBSProcess, 
            My2ndGBSProcess, 
            My3rdGBSProcess, 
            My4thGBSProcess, 
            My5thGBSProcess  //  Array Data
        
        };
        
        CTQL.StringVector arrCorrArrayProcesses_stochProcesses = 
            new  CTQL.StringVector(arrBCorrArrayProcesses_stochProcesses);
    
        // Second parameter determines whether the array is a column array (false) or a row array (true)
        CorrArrayProcesses_stochProcesses = new  CTQL.CTRangeData(arrCorrArrayProcesses_stochProcesses, false);
            
        //  Create example range for parameter CorrArrayProcesses_corrMatrix
        CTQL.CTRangeData CorrArrayProcesses_corrMatrix = 
            new CTQL.CTRangeData();
             
        System.Text.StringBuilder CorrArrayProcesses_corrMatrix_builder = 
            new System.Text.StringBuilder(100);
                         
        // We could set the value for each cell individually, but for display
        // purposes, this is quicker and more informative.
        CorrArrayProcesses_corrMatrix_builder.Append("{");
        CorrArrayProcesses_corrMatrix_builder.Append("1     | 0.7612     | 0.7799     | 0.7994     | 0.8274 ;");
        CorrArrayProcesses_corrMatrix_builder.Append("0.7612     | 1     | 0.7859     | 0.8047     | 0.8341 ;");
        CorrArrayProcesses_corrMatrix_builder.Append("0.7799     | 0.7859     | 1     | 0.8101     | 0.8422 ;");
        CorrArrayProcesses_corrMatrix_builder.Append("0.7994     | 0.8047     | 0.8101     | 1     | 0.8477 ;");
        CorrArrayProcesses_corrMatrix_builder.Append("0.8274     | 0.8341     | 0.8422     | 0.8477     | 1");
        CorrArrayProcesses_corrMatrix_builder.Append("}");
        
        CorrArrayProcesses_corrMatrix.RangeFromStr
        (
            CorrArrayProcesses_corrMatrix_builder.ToString()
        );
                     


    //    Key value to use as a handle for the created object
        string MyCorrArrayProcesses = "MyCorrArrayProcesses" + "_" + System.Convert.ToString(nCTProcessSimCGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

                    
    //  Excel function call would be this - "CT.PRO.CorrArrayProcesses()"

    //    Creates an array of correlated one dimensional stochastic processes.
        string rCorrArrayProcesses;
                                        
        rCorrArrayProcesses = CTQL.CTProcessesSA.CorrArrayProcesses(
                MyCorrArrayProcesses,
                Reload,
                CorrArrayProcesses_stochProcesses,
                CorrArrayProcesses_corrMatrix);


    return rCorrArrayProcesses;
}        



// ///////////////////////////////////////////////////////////////////

private string ValueDateObjPart()
{



    //    Key value to use as a handle for the created object
        string MyValuationDate = "MyValuationDate" + "_" + System.Convert.ToString(nCTProcessSimCGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Valuation Date (typically equal to Today's date)
        CTQL.Date ValueDate =  new CTQL.Date("19/7/2005", "dd/mm/yyyy");

                    
    //  Excel function call would be this - "CT.DATE.ValueDateObj()"

    //    Creates a centralized valuation date object.
        string rValueDateObj;
                                        
        rValueDateObj = CTQL.CTUtilsSA.ValueDateObj(
                MyValuationDate,
                Reload,
                ValueDate.serialNumber());


    return rValueDateObj;
}        



// ///////////////////////////////////////////////////////////////////

private string GBSProcessPart(
    string MyValuationDate)
{



    //    Key value to use as a handle for the created object
        string MyGBSProcess = "MyGBSProcess" + "_" + System.Convert.ToString(nCTProcessSimCGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Initial value.
        double x0 = 80.0;

    //    risk free rate.
        double rate = 0.05;

    //    External Rate (Dividend, foreign FX, holding cost) value.
        double ext = 0.01;

    //    The DayCounter of the 'rate' and 'ext' parameters.
        CTIEnums.DayCountEnum RateDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

    //    volatility value of the process.
        double sigma = 0.20;

    //    The DayCounter of the 'sigma' parameter.
        CTIEnums.DayCountEnum VolDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

                    
    //  Excel function call would be this - "CT.PRO.GBSProcess()"

    //    Creates a Generalized BlackScholes Process.
        string rGBSProcess;
                                        
        rGBSProcess = CTQL.CTProcessesSA.GBSProcess(
                MyGBSProcess,
                Reload,
                MyValuationDate,
                x0,
                rate,
                ext,
                RateDayCount,
                sigma,
                VolDayCount);


    return rGBSProcess;
}        



// ///////////////////////////////////////////////////////////////////

private string GBSProcess__2Part(
    string MyValuationDate)
{



    //    Key value to use as a handle for the created object
        string My2ndGBSProcess = "My2ndGBSProcess" + "_" + System.Convert.ToString(nCTProcessSimCGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Initial value.
        double x0 = 100.0;

    //    risk free rate.
        double rate = 0.055;

    //    External Rate (Dividend, foreign FX, holding cost) value.
        double ext = 0.01;

    //    The DayCounter of the 'rate' and 'ext' parameters.
        CTIEnums.DayCountEnum RateDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

    //    volatility value of the process.
        double sigma = 0.21;

    //    The DayCounter of the 'sigma' parameter.
        CTIEnums.DayCountEnum VolDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

                    
    //  Excel function call would be this - "CT.PRO.GBSProcess()"

    //    Creates a Generalized BlackScholes Process.
        string rGBSProcess__2;
                                        
        rGBSProcess__2 = CTQL.CTProcessesSA.GBSProcess(
                My2ndGBSProcess,
                Reload,
                MyValuationDate,
                x0,
                rate,
                ext,
                RateDayCount,
                sigma,
                VolDayCount);


    return rGBSProcess__2;
}        



// ///////////////////////////////////////////////////////////////////

private string GBSProcess__3Part(
    string MyValuationDate)
{



    //    Key value to use as a handle for the created object
        string My3rdGBSProcess = "My3rdGBSProcess" + "_" + System.Convert.ToString(nCTProcessSimCGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Initial value.
        double x0 = 120.0;

    //    risk free rate.
        double rate = 0.06;

    //    External Rate (Dividend, foreign FX, holding cost) value.
        double ext = 0.025;

    //    The DayCounter of the 'rate' and 'ext' parameters.
        CTIEnums.DayCountEnum RateDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

    //    volatility value of the process.
        double sigma = 0.22;

    //    The DayCounter of the 'sigma' parameter.
        CTIEnums.DayCountEnum VolDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

                    
    //  Excel function call would be this - "CT.PRO.GBSProcess()"

    //    Creates a Generalized BlackScholes Process.
        string rGBSProcess__3;
                                        
        rGBSProcess__3 = CTQL.CTProcessesSA.GBSProcess(
                My3rdGBSProcess,
                Reload,
                MyValuationDate,
                x0,
                rate,
                ext,
                RateDayCount,
                sigma,
                VolDayCount);


    return rGBSProcess__3;
}        



// ///////////////////////////////////////////////////////////////////

private string GBSProcess__4Part(
    string MyValuationDate)
{



    //    Key value to use as a handle for the created object
        string My4thGBSProcess = "My4thGBSProcess" + "_" + System.Convert.ToString(nCTProcessSimCGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Initial value.
        double x0 = 60.0;

    //    risk free rate.
        double rate = 0.45;

    //    External Rate (Dividend, foreign FX, holding cost) value.
        double ext = 0.02;

    //    The DayCounter of the 'rate' and 'ext' parameters.
        CTIEnums.DayCountEnum RateDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

    //    volatility value of the process.
        double sigma = 0.21;

    //    The DayCounter of the 'sigma' parameter.
        CTIEnums.DayCountEnum VolDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

                    
    //  Excel function call would be this - "CT.PRO.GBSProcess()"

    //    Creates a Generalized BlackScholes Process.
        string rGBSProcess__4;
                                        
        rGBSProcess__4 = CTQL.CTProcessesSA.GBSProcess(
                My4thGBSProcess,
                Reload,
                MyValuationDate,
                x0,
                rate,
                ext,
                RateDayCount,
                sigma,
                VolDayCount);


    return rGBSProcess__4;
}        



// ///////////////////////////////////////////////////////////////////

private string GBSProcess__5Part(
    string MyValuationDate)
{



    //    Key value to use as a handle for the created object
        string My5thGBSProcess = "My5thGBSProcess" + "_" + System.Convert.ToString(nCTProcessSimCGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Initial value.
        double x0 = 110.0;

    //    risk free rate.
        double rate = 0.04;

    //    External Rate (Dividend, foreign FX, holding cost) value.
        double ext = 0.01;

    //    The DayCounter of the 'rate' and 'ext' parameters.
        CTIEnums.DayCountEnum RateDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

    //    volatility value of the process.
        double sigma = 0.19;

    //    The DayCounter of the 'sigma' parameter.
        CTIEnums.DayCountEnum VolDayCount = CTIEnums.DayCountEnum.DayCount_actual365_fixed;

                    
    //  Excel function call would be this - "CT.PRO.GBSProcess()"

    //    Creates a Generalized BlackScholes Process.
        string rGBSProcess__5;
                                        
        rGBSProcess__5 = CTQL.CTProcessesSA.GBSProcess(
                My5thGBSProcess,
                Reload,
                MyValuationDate,
                x0,
                rate,
                ext,
                RateDayCount,
                sigma,
                VolDayCount);


    return rGBSProcess__5;
}        








Copyright (c) 2003-2007 CapeTools - All Rights Reserved.