How To Code the Newton-Raphson Method in Excel VBA Newton’s Algorithm
Transcription
How To Code the Newton-Raphson Method in Excel VBA Newton’s Algorithm
How To Code the Newton-Raphson Method in Excel VBA Newton’s Algorithm Newton's algorithm, alternately called the Newton-Raphson method, is a numerical method for solving equations of the type f(x)=0. It has numerous applications in finance, and we will use it to determine the volatility surface for a call option using the Black Scholes VBA function. The above is the simpler method of interpretation, but the statement f(x) = mx+c is actually derived from the more generalised Taylor's series expansion where a continuous, differentiable function f may be expanded about a point c as follows: f(x) =f(c)+f'(c)(x-c)+f''(c)/2!*(xc)^2+... The method applies when we have a close approximation for a solution to the equation. The method assumes that the tangent to the graph at our approximation intersects the x-axis closer to the solution of the equation, i.e. closer to where the line we are evaluating intersects the x-axis. By solving for where the tangent intersects the xaxis, and repeating the process from that point, we get closer and closer to the actual solution of the equation. This is the univariate version of the Taylor expansion, but it will suffice for finding, for example, volatility smiles since we assume that all other variables remain constant. Let's say we're at our approximate solution, x0. The value of y-axis at this point is y = f(x0). The equation of the intersection of the tangent with the y-axis is given by the following: 0 = f'(x0)(x1-x0) + f(x0) We know that the tangent has gradient f'(x) - the same gradient as the line, from the definition of a tangent. We also know that a univariate linear function takes the form f(x) = mx + c, where m is the gradient, x is the horizontal displacement from the starting point, and c is the vertical displacement from the starting point. As such, we have y = f'(x0)(x-x0) + f(x0) as the line of the tangent. We solve this for y = 0 in order to get the next guesstimate of the solution to f(x) = 0. 1 Nyasha Madavo, VBA Developer.net Since the equation is linear, no second order derivatives exist so these fall away, leaving f(x) =f(c)+f'(c)(x-c). In our example, x0=c, giving y = f'(x0)(x-x0) + f(x0) We then solve for x1 x1 = x0 - (f(x0)/f'(x0) From this we can induce (though actual proof is out of scope here), that xn+1 = xn - f(xn)/f'(xn) , which is the formula for Newton's method. The formula for Newton's method works in cases where the function is differentiable, and where the gradient is not equal to zero. How To Code the Newton-Raphson Method in Excel VBA How To Code the Newton-Raphson Method in Excel VBA VBA implementation of Newton's algorithm Here is an implementation of Newton's algorithm for a simple function, f(x) = x2 - 3. For the volatility surface version, func(x_n) will be replaced by the Black Scholes VBA pricing function, and func_dash(x_n) will be replaced by the VBA function for calculating vega, the first derivative of the option price with respect to volatility. Function Newton(Seed As Double, Precision As Double) As Double Dim x_next As Double, x_n As Double, error_val As Double Dim ctr As Integer x_n = Seed ctr = 0 Do x_next = x_n - func(x_n) / func_dash(x_n) error_val = x_next - x_n x_n = x_next ctr = ctr + 1 Loop Until (Abs(error_val) <= Precision Or ctr = 1000) Newton = x_next End Function Function func(x_n) As Double func = x_n ^ 2 - 3 End Function Function func_dash(x_n) As Double func_dash = 2 * x_n End Function How VBA Developer.net Can Save You Time and money You can get complete Excel apps from VBA Develeoper.net containing the code in this document, customisation, VBA development of any Excel, Access and Outlook apps, as well as C# and C++ add-ins and technical documentation. Visit VBA Developer.net Examples of VBA documents from VBA Developer.net How to build a Black Scholes VBA Option Pricer 2 Nyasha Madavo, VBA Developer.net How To Code the Newton-Raphson Method in Excel VBA How To Code the Newton-Raphson Method in Excel VBA How to Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation How to build a Black Scholes C# Option Pricer How to build a Black Scholes VBA Option Pricer for FX Options How to build a Black Scholes VBA Option Pricer for Equity Options How To Code the Newton-Raphson Method in Excel VBA How to Model Volatility Smiles, Volatility Term Structure and the Volatility Surface in Excel VBA How To Write Use Cases for a Portfolio Reporting VBA Tool How to build a Black Scholes VBA Option Pricer using Monte Carlo Simulation How to build a Black Scholes VBA Option Pricer for Binary Options How To Write a User Interface Model For a Portfolio Reporting VBA Tool How To Create a Semantic Object Model For a Portfolio Reporting VBA Tool How to build a Black Scholes VBA Option Pricer for Equity Barrier Options How To Normalise a Database For VBA Apps How to build a Black Scholes VBA Option Pricer for Exotic Asian Options How To Create a Database using SQL Scripts for a Portfolio Reporting VBA App How to build a Black Scholes VBA Option Pricer for Exotic Lookback Options How to Write Stored Procedures in SQL/Access/VBA for a Portfolio Reporting VBA App How to build an Equity Option Pricer using the Binomial Tree in Excel VBA How to Use Cursors in SQL for a Portfolio Reporting VBA Tool How to code a Choleskey Decomposition in VBA (Numerical Methods for Excel) How to Move Data from Access to Excel with SQL for a Portfolio Reporting VBA App 3 ways to sort in VBA 3 Nyasha Madavo, VBA Developer.net How To Code the Newton-Raphson Method in Excel VBA How To Code the Newton-Raphson Method in Excel VBA Portfolio Reporting VBA Tool: Inserting Data into SQL/Access Databases from Excel Portfolio Reporting VBA Tool: Connecting Excel with SQL & Access Databases How To Design Classes For an Option Pricer in VBA: UML Concepts How To Design Classes for Object Orientated VBA Programming 4 Nyasha Madavo, VBA Developer.net How To Code the Newton-Raphson Method in Excel VBA