StepMonteCarlo Example VBNET

VB.NET 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. ' ################################################################################## ![]() Imports 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.![]() Imports 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. Global nCTProcessSimCGlobal As Integer ' 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 Global oEmptyRange As CTQL.CTRangeData Public Function VB_EX_StepMonteCarlo() As String![]() nCTProcessSimCGlobal = nCTProcessSimCGlobal + 1 ![]() Dim szErrorMsg As String Try![]() ![]() ' Creates a centralized valuation date object.![]() Dim MyValuationDate As String MyValuationDate = _ ValueDateObjPart()![]() ![]() ![]() ' Creates a Generalized BlackScholes Process. Dim MyGBSProcess As String MyGBSProcess = _ GBSProcessPart( _ MyValuationDate)![]() ![]() ![]() ' Creates a Generalized BlackScholes Process. Dim My2ndGBSProcess As String My2ndGBSProcess = _ GBSProcess__2Part( _ MyValuationDate)![]() ![]() ![]() ' Creates a Generalized BlackScholes Process. Dim My3rdGBSProcess As String My3rdGBSProcess = _ GBSProcess__3Part( _ MyValuationDate)![]() ![]() ![]() ' Creates a Generalized BlackScholes Process. Dim My4thGBSProcess As String My4thGBSProcess = _ GBSProcess__4Part( _ MyValuationDate)![]() ![]() ![]() ' Creates a Generalized BlackScholes Process. Dim My5thGBSProcess As String My5thGBSProcess = _ GBSProcess__5Part( _ MyValuationDate)![]() ![]() ![]() ' Creates an array of correlated one dimensional stochastic processes. Dim MyCorrArrayProcesses As String MyCorrArrayProcesses = _ CorrArrayProcessesPart( _ MyGBSProcess, _ My2ndGBSProcess, _ My3rdGBSProcess, _ My4thGBSProcess, _ My5thGBSProcess)![]() ![]() ![]() ' Creates a Step Monte Carlo object given a process object and ' a time line dates array. Dim MyStepMonteCarlo As String MyStepMonteCarlo = _ StepMonteCarloPart( _ MyCorrArrayProcesses, _ MyValuationDate)![]() ' This is the result we are looking for. VB_EX_StepMonteCarlo = MyStepMonteCarlo![]() Catch e As Exception szErrorMsg = e.Message Throw e Catch e As System.ApplicationException szData = e.Message End Try End Function ![]() ![]() ' ///////////////////////////////////////////////////////////////////![]() Private Function StepMonteCarloPart( _ MyCorrArrayProcesses As String, _ MyValuationDate As String) As String![]() ![]() ' Create example range for parameter StepMonteCarlo_MandatoryDates Dim StepMonteCarlo_MandatoryDates _ As CTQL.CTRangeData ![]() Dim arrBStepMonteCarlo_MandatoryDates As Integer() = { _ 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 } Dim arrStepMonteCarlo_MandatoryDates As CTQL.IntVector = _ 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 Dim MyStepMonteCarlo As String MyStepMonteCarlo = "MyStepMonteCarlo" + "_" + CStr(nCTProcessSimCGlobal)![]() ![]() ' When creating this object for the first time, set this parameter ' to a positive value. Dim Reload As Integer Reload = 1![]() ![]() ' Used to calculate time in years. Dim dayCounter As CTIEnums.DayCountEnum dayCounter = CTIEnums.DayCountEnum.DayCount_30360![]() ![]() ' The minimum number of steps that the discretization of the 'MandatoryDates' ' parameter will take. Dim MinNoOfSteps As Integer MinNoOfSteps = 50![]() ![]() ' The random generator type to use. Dim MCMethod As String MCMethod = "Pseudo"![]() ![]() ' Seed value. Dim Seed As Integer Seed = 0![]() ' Excel function call is : "CT.PRO.StepMonteCarlo()"![]() ' Creates a Step Monte Carlo object given a process object and ' a time line dates array. Dim rStepMonteCarlo As String Dim oCTProcessSimC As New CTQL.CTProcessSimC rStepMonteCarlo = CTQL.CTProcessSimCSA.StepMonteCarlo( _ MyStepMonteCarlo, _ Reload, _ MyCorrArrayProcesses, _ MyValuationDate, _ dayCounter, _ StepMonteCarlo_MandatoryDates, _ MinNoOfSteps, _ MCMethod, _ Seed)![]() ![]() StepMonteCarloPart = rStepMonteCarlo End Function ![]() ![]() ![]() ![]() |