# Debugging the code for pricing European option

Posted 8 years ago
10588 Views
|
7 Replies
|
2 Total Likes
|
 I wrote a program to price European option. The program is not showing any error but not giving price also. The function (boptvol) is expected to price a European option taking Stock (stock), age (time), volatility (vol), interest rate (int), contractual expiry (expn), type of payoff (payoff),Strike ( strike), number of asset steps used for evaluation option (nas) using finite difference method. I am expecting function to return price of the option based stock price and age (if option is bought today age is zero).Can anybody opine where I am going wrong. Just to give some extra color about the code. Table d2 in the code is designed to store value of option at all the grid points of finite difference grid. Table d1 is used to generate stock price which are used for computation in table d2. Table d3 is used to store time steps. European option price is give by euro. euro takes one of the values in table d2 if stock price is exactly same as one of the values in table d1 and timestep is exactly same as one of the values in table d3, otherwise it uses black sholes PDE to generate option price from immediate nodes of table d2. Any help in debugging code will be very useful.Regards Attachments:
7 Replies
Sort By:
Posted 8 years ago
 Kausik you've double posted this.You left your former discussion adrift, labeled in many groups, and not updated it to show the progress of this discussion. This has lead me to comment there on out of date information. You will struggle to develop and debug your nested do loops. Other users will find it near impossible as we do not know your intent.I suggest reducing to where you see a problem: run your program to generate 'fd' and your other variables. Output these and check they behave as you'd expect. Then check that for the first instance of 'i' and 'j' iterators your code does as you'd expect.
Posted 8 years ago
 Hi Sean. I uploaded the new version of code (replaced original code). Now it is calculating finite difference table fd but still some problem in Do Loop where I have two different criteria to update matrix "fd". I am expecting logic in IF which is supposed to work for all element in row (nas1+1) as I start updating matrix from right. However logic is failing after second iteration. Hence matrix is getting incorrectly updated from (10,4)th element. (10,4)th element is computed using previous value of (11,5)th matrix fd (which is 1).Can you opine why Do loop is not updating in correct sequence? Attachments:
Posted 8 years ago
 I started decoding the code by part. It seems the way I started generating table d2 is problem. So I thinking about using alternative way. Can you pl help in finding efficient way to replace part of matrix? i want to replace 1st row of the matrix fd below using logic fd [0, nts-1] = (1- intratedt2) fd [ 0, nts] . The elements in the 1st row to be replaced backward using intrate and dt2. I am getting erroroptionprice[strike1, nas1, expn1, vol1, int_] := Module[{ds, s, dt, dt1, t, nts, fd}, ds = 2*strike1/nas1; dt = 0.9/(vol1vol1nas1* nas1) ; nts = IntegerPart [expn1 /dt] + 1; dt1 = expn1/nts; s = Table[i*ds, {i, 0, nas1 + 1}]; t = Table [j*dt1, {j, 0, nts}]; fd = ConstantArray[1, {nas1 + 1, nts}]; fd = ReplacePart[fd, {i_, nts} :> Max[(s[[i]] - strike1), 0]]; fd = Do [ ReplacePart[fd, {1, j} :> (1 - intdt1)fd[[1, j + 1]]], {j, nts - 1, 1, -1}]; Grid[fd] ]
Posted 8 years ago
 It looks like you need to debug your code. Debugging code is easier when the code has been broking up into smaller parts. Each smaller part can be tested and checked to make sure it works correctly. What kinds of error messages do you see? Maybe pick one at a time. What part of your code does the error message refer to? If looks like you've set the variable nas to be a string, but later on you use it as a number.
Posted 8 years ago
 I am not getting any error message. That's why I am assuming code is correct but obviously some problem in the code that's why it is not returning the euro.It seems I have written the code in one cell. So not sure how to debug in parts. Can you please explain little more on how to debug in parts. Also I have only one function boptval which is supposed to return price of the option. but yes I can always see if table d1, d2 or d3 is correct. Can you please help in providing one example on how to debug in parts?Also I am providing nas as input in boptval like boptval[100, 1, 0.2, 0.05, 2, "c", 100, 80]. So nas is 80. So don't see how function can recognize it as string.
Posted 8 years ago
 The Wolfram Language uses square brackets for calling a function. So you should write: boptval["100", "1", "20", "5", "c", "100", "80"] Also, boptval is defined to take 8 arguments. Here you have given it 7 arguments.
Posted 8 years ago
 Hi Sean. Thanks for comments. I made changes it still does return option value. I made some changes in the code too and uploaded the revised version. Pl use the most updated version of code