};
We have provided the full code on the CD.
7.8 APPLYING NONLINEAR SOLVERS:
CALCULATING VOLATILITY
In previous chapters we gave some formulae for call and put options based on (known) para-meters such as the strike price and the volatility, for example. But we now have the ‘inverse’
problem: given that we know the price of the option and all its parameters except the volatility, how do we calculate the volatility? We have an equation with one unknown, namely the volatility. It is a scalar nonlinear equation and we solve using one of the above nonlinear solvers. In general it is difficult to measure volatility directly and the objective in general is to use that volatility that is compatible with the market price. We call this theimplied volatility. In general we estimate the volatility using call prices. There is no closed formula for the implied volatility. In general we could calculate it for a set of strike prices and expiry dates on a single underlying. This results in an array of values and hence we conclude that implied volatility is not constant.
We have taken the test case from Haug (1998) and the code that calculates the price of a call option (see the CD again):
double CallPrice(double sig) {
// Test case Haug p. 172; student exercise to extend it double S = 59.0;
double K = 60.0;
double r = 0.067;
double marketPrice = 2.82; // The call price double b = r;
double T = 0.25; // Three months
double tmp = sig * sqrt(T);
double d1 = ( log(S/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
double d2 = d1 - tmp;
double calculatedValue =
(S * exp((b-r)*T) * N(d1)) - (K * exp(-r * T)* N(d2));
// Function in the form f(x) = 0 return marketPrice - calculatedValue;
}
We have found the same results as in Haug (1998) for both the Secant Method and Steffensen’s method but the Bisection method does not seem to converge to the correct solution for some reason.
Some code is now given:
// Steffensen's method double guess = 0.2;
SteffensensSolver steff(guess, CallPrice);
steff.tol = 0.0001;
double resultST= steff.solve();
cout << "Steffensen's Method: " << resultST << endl;
steff.printStatistics();
7.9 SUMMARY AND CONCLUSIONS
We gave an introduction to a number of important topics in C++, namely function pointers, namespaces and an introduction to inheritance. We also gave some examples of how to use them and also how to write nonlinear solvers that calculate the implied volatility.
We continue in the next chapter with advanced issues related to inheritance.
7.10 EXERCISES AND PROJECTS
1. We wish to define a variable called MAX ITERATIONS that represents the maximum number of iterations allowed during the execution of a nonlinear solver. This is a variable that is common to all instances of the classes in the hierarchy and we model this as a static variable.
You should also define a ‘running counter’ variable that is incremented at each leg of the iterative scheme. If it becomes greater than MAX ITERATIONS we know that the scheme has not converged and we should stop.
Answer the following questions:
(a) Set up the code to declare, define, initialise and integrate MAX ITERATIONS into the class hierarchy
(b) Use assert() or the Datasim Exception class in conjunction with the excep-tion handling mechanism to throw an excepexcep-tion when the running counter exceeds MAX ITERATIONSand then catch the exception in the client code (you may need to read the sections in Chapter 9 pertaining to exception handling first and examine the code on the CD that implements a class for exception handling)
2. We test the Bisection and Newton methods on a problem from a fixed income application (Fabozzi, 1993). In this case we calculate the yield measure of a bond and hence its relative attractiveness. The yield in this case is the interest rate that will make the present value of the cash flows from the investment equal to the price (or cost) of the investment. The yield y satisfies the equation:
where
P= present value CFt= Cash flow in year t
n= number of years
We write this in the form that is suitable for computation:
f (y)= P −
n t=1
C Ft
(1+ y)t = 0 (7.10)
We now have a function of the unknown variable y and in fact we wish to find a root of this equation.
You have to pay attention to the following issues:
(a) The interval (a, b) in which the solution is to be found must be known
(b) In the case of the Bisection method we must have f (a) f (b)< 0; the values a = 0 (zero interest rate) and b= .25 (25 % interest rate) might be good choices but you may have to experiment with some sample values
(c) In the case of Newton’s method we need to calculate the derivative of f with respect to y, that is:
(d) Now compare your results by applying the Secant and Steffensen’s method. Examine speed of convergence and dependency of closeness of the initial guess to the exact solution.
3. Generalise the results and code from section 7.8 to allow us to calculate implied volatility for a number of strike prices. The output will now be a vector containing implied volatility data.
4. Use any of the nonlinear solvers to find a value for the square root of 2. The function to use is:
double SquareRoot(double x) {
return ::sqrt(2.0) - x;
}
Generalise the code to find a root of any power of x.
5. Implement the ancient methodRegula Falsi (method of false positions). This is a method that uses linear interpolation by using two previous approximations. See Figure 7.1. Here we see that a and b are two estimates and that the new estimate c is given by the linear interpolation formula:
y= f (a) + f (a)− f (b)
a− b (x− a) (7.12)
a c b
y = f(x)
Figure 7.1 Regula falsi
giving the estimate for c:
c= a − (a− b) f (a)
f (a)− f (b) (7.13)
This all translates to the algorithm:
xn = xn−1−(xn−1− xn−2)f (xn−1)
f (xn−1)− f (xn−2) (7.14) We have produced the code for this algorithm in the directory relating to Chapter 7.
Answer the following questions:
(a) Check the code against the algorithm (this is what is called acode review) (b) Test the code against the examples that we discussed in this chapter 6. (Inheritance scenarios)
Discuss the merits (or otherwise) of the following examples of deriving a class from a given base class:
r
Stack derived from a list (and vice versa)r
Circle derived from an ellipse (and vice versa)r
Call Option and Put Option derived from Optionr
What about Chooser Option?r
SortedArray is derived from Array 7. (Research)What is ananonymous namespace and in which circumstances would you use it? What are the advantages and disadvantages? Find out what the idea behind the keyword ‘protected’
is. When would it be useful to use it?
112