Vb a Course Work

Embed Size (px)

Citation preview

  • 8/22/2019 Vb a Course Work

    1/6

    PROJECT 1

    VBA IN EXCEL WITH FINANCIAL APPLICATIONS

    This assignment aims to introduce you to the development of user-defined functions and

    subroutines in VBA and guide you into how you can use a modular approach in the development

    of complex applications. We are interested in the process of developing computer codes to solve

    specific scientific problems. The development of such codes involves three distinct stages, the

    development of an appropriate numerical algorithm, the implementation of the algorithm in a

    computer language, and finally validation of the numerical results produced.

    In the first question you develop a simple VBA function that can return the option price, or the

    delta, or the vega of either a call or a putbased on the Black and Scholes model. You will be

    using this closed form solution to validate the results that you will later obtain using a different

    numerical method. In question 2 you develop a function that calculates the value ofEuropean put

    andEuropean call options using Monte-Carlo methodology. You will use your code to perform a

    number of numerical experiments which examine how the number of paths used influence the

    accuracy achieved and investigate how the antithetic variable technique can be used to improve

    the accuracy of your estimates.

    Finally, in question 3 you have to introduce a visual interface for the Black and Scholes function

    and the Monte-Carlo simulation subroutines that will make their use user-friendly.

    As you develop your code, make certain that each constituent part does produce the right result

    and can handle any possible errors that might occur due to user error. Design each component of

    your code in such a way that enables simple modifications without the need of major structural

    changes and can easily be used to create more complicated programs. Keep your functions and

    subroutines simple. Write your code in small modules each with a clearly defined purpose. Be

    consistent in the use of variables and select names for your variables, functions and procedures

    that are easy for you and for others to understand. Please bear in mind that you are expected to

    submit an Excel file containing all the modules that you created alongside a three pages report

    that addresses the questions set in questions 1 and 2 of the coursework. For your report you

    should use Time New Roman fonts, font size 12 and line spacing 1.5 lines. Good luck!

    Submission Date: October 28, 2009 Oral Examination Date: TBA

  • 8/22/2019 Vb a Course Work

    2/6

    QUESTION 1 [30 POINTS]

    Write a VBA function namedBS_Val ue that uses the Black and Scholes formula to calculate:

    (a) The price of a European put on a non-dividend paying stock.

    (b) The price of a European call on a non-dividend paying stock.(c) The delta of a European put on a non-dividend paying stock.

    (d) The delta of a European call on a non-dividend paying stock.

    (e) The vega of a European call option on a non-dividend paying stock.

    (f) The vega of a European put option on a non-dividend paying stock.

    The function arguments should be:

    0S : the current price of the underlying stock

    K: the strike price of the option

    : the stock price volatility

    r: the continuously compounded risk-free rate

    T: the time to maturity of the option

    TypeOf Out put : Option price, or delta, or vega

    TypeOf Payof f: Call or Put option

    To calculate the price of a European option on a non-dividend paying stock you should use the

    Black and Scholes pricing formulas. More specifically, the price of the European call and

    European put option are given by

    )()( 210 dNKedNSBSrT

    call

    = ,

    )()( 102 dNSdNKeBSrT

    put = ,

    where

    T

    TrKSd

    )2/()/ln( 201

    ++

    = ,

    Tdd = 12 ,

    andN(x) is the cumulative probability distribution for a standardized normal distribution. In

    EXCEL VBA we have a built-in function that returns theN(x) value:

    N(x) =Appl i cat i on. Wor ksheet Funct i on. Nor mSDi st ( x)

    The delta of a European call option on a non-dividend paying stock is given by, )( 1dNc = .

    The delta of a European put option on a non-dividend paying stock is given by, 1)( 1 = dNp .

  • 8/22/2019 Vb a Course Work

    3/6

    The vega for a European call or put option on a non-dividend paying stock is given by,

    )(' 10 dNTSv = , where2/

    1

    21

    2

    1)(' dedN

    =

    .

    Develop two versions of this function one in which you explicitly define the data type of all theinput variables and one in which you set all the input variables to be of variant type.

    Questions:

    (a) How have you verified that the developed functions provide the right answer?

    (b) What are the possible user-errors that you have identified? How have you dealt with

    trapping and recovering from those possible user-errors?

    (c) How do your error-handling techniques differ between the two different versions of your

    function? What type of different checks do you perform when you declare explicitly the

    data type of the function arguments compared with the case in which you declare all the

    function arguments to be of variant type?

    QUESTION 2 [40 POINTS]

    Monte-Carlo simulation is a very powerful and popular methodology for pricing various

    derivative products. The key steps in Monte-Carlo simulation are as follows:

    1. Sample a random path for the underlying asset S in a risk-neutral world based on the

    stochastic differential equation that describes the evolution ofS.

    2. Calculate the discounted payoff of the derivative based on this path.

    3. Repeat steps 1 and 2 a number of times.

    4. Calculate the mean of the sample of the discounted payoffs to get an estimate of the value

    of the derivative and the standard deviation of the sample of the discounted payoffs to

    check the accuracy of the simulation.

    A pseudo code is provided below that describes how a typical Mont eCar l oSi mul at i on

    procedure is organized:

    Pr ovi de ,,,,0 TrKS , TypeOf Payof f , NoOf Pat hs

    Def i ne RandomNumber s( NoOf Pat hs) , Opt i onVal ue( NoOf Pat hs)

    Cal l Gener at eRandomNumber s whi ch r et urns

    RandomNumber s( NoOf Pat hs)

  • 8/22/2019 Vb a Course Work

    4/6

    Cal l Gener at eOpt i onPr i ces whi ch r et ur ns

    Opt i onPr i ces( NoOf Pat hs)

    Retur n:

    Opt i onVal ue=Aver age[ Opt i onPr i ces( i ) ]

    St andardEr r orOf Est i mate=

    St andar dDevi at i on[ Opt i onPr i ces( i ) ] / Sqr t [ NoOf Pat hs]

    Based on the output of the subroutine it is very easy to calculate confidence intervals for the

    price. For example if we denote the Opt i onVal ue by OV and the

    St andar dEr r or Of Est i mat e by SE, a 95% confidence interval is given by:

    OV- 1. 96SE < OV < OV+1. 96SE.

    The VBA provides the built-in function Rnd that returns a random number between 0 and 1. The

    simplest way to obtain a random number from a univariate standardized normal distribution is to

    use the formula:

    6)(12

    1

    =

    =i

    iRnd .

    A pseudo code is provided below that describes how the subroutine

    Gener at eRandomNumber s works:

    Provi de NoOf Pat hs

    Def i ne RandomNumber s( NoOf Pat hs)

    I t er at e f or i=1 To NoOfPaths

    Sum = 0

    I t er at e f or j =1 To 12

    Sum = Sum + Rnd

    End of I t er at i on j

    RandomNumber s( i ) = Sum 6

    End of I t er at i on i

    Retur n:

    RandomNumber s( NoOf Pat hs)

  • 8/22/2019 Vb a Course Work

    5/6

    If we want each time we run the code a different set of random numbers to be generated, we must

    introduce the Randomi ze statement in the Mont eCar l oSi mul at i on before calling the

    Gener at eRandomNumber s procedure. To complete this basic Monte Carlo subroutine we

    also need a subroutine that would simulate the share prices and produce a large number of option

    prices. A pseudo code is provided below that describes how the subroutine

    Generat eOpt i onPr i ces works:

    Pr ovi de ,,,,0 TrKS , , TypeOf Payof f , NoOf Pat hs, _

    RandomNumber s( NoOf Pat hs)

    I t er at e f or i=1 To NoOfPaths

    += TiTrSST

    )(ersRandomNumb2

    2exp0

    ),()exp(es(i)OptionPric KSPayoffTr T=

    End of I t er at i on

    Retur n:

    Opt i onPr i ces( NoOf Pat hs)

    For the payoff we should use the Appl i cat i on. Wor kSheet Funct i on. Max( ) statement.

    Extra care should be given on how we find the average and the standard deviation. Using the

    built-in Excel functions Aver age andStD, is not advisable as they would impose an upper

    limit to the number of paths equal to 65,536. Therefore, you should write two simple functions

    that would return the average and the standard deviation of a matrix. If the simulation is carried

    out as described so far, it is usually necessary to use a large number of paths to estimate the

    option price with reasonable accuracy. A popular method that improves the estimate provided by

    the Mont eCar l oSi mul at i on is the antithetic variable technique. In employing the antitheticvariable technique, we calculate two values of the derivative. The first value P1 is calculated in

    the usual way, the second value, P2, is computed using the same random numbers we used for P1,

    but with changing the sign of all random numbers. An improved estimate is found by averaging

    P1 and P2.

    Questions:

    (a) Create a subroutine that uses Monte-Carlo simulation and can price both put and call

    European options. Using your developed code perform numerical experiments to analyzehow the number of paths influences the accuracy of the calculated option price.

  • 8/22/2019 Vb a Course Work

    6/6

    (b) Create a new subroutine that uses the antithetic variable technique to improve the

    accuracy of your calculations. What is the effect on the calculated price and the

    confidence intervals?

    QUESTION 3 [30 POINTS]Create a graphical interface for a calculator that provides the Black and Scholes value using

    either the analytical or the Monte-Carlo simulation. The graphical calculator should look like the

    one presented below. The calculator should appear when you press Ct r l +Shi f t +C. You

    should create a number of different subroutines:

    A subroutine that resets the data to their default values and should also be used when you

    first display the dialog box to initialize the data.

    A subroutine that displays the dialog box.

    Six different subroutines, one for each of the command keys.