(*initialisation of variables*)
n = 10;(*expected value greater than 3*)
r = 0.03;
σ = 0.2;
sMax = 2;
sMin = -2;

(*define Δx.*)
Δx = (sMax - sMin)/(n - 1);

(*adding vector matrix... any assignment of v value should be 
included here*)
vecMatrix = Array[v, n];

(*reusable internal variables*)
p = 0.5 (σ^2)/(Δx^2);
q = (2 r - (σ^2))/Δx;

x = (2 p + r) - 0.75 q;
y = 5 p + q;
a = p - 0.25 q;
b = -2 p - r;
c = p + 0.25 q;

(*introducing matC for one time initialisation*)
matC = SparseArray[{Band[{1, 1}, {n, n}, n - 1] -> x, 
    Band[{1, 2}, {n, n}, {n - 1, n - 3}] -> y, 
    Band[{2, 1}, {n - 1, n}] -> a, Band[{2, 2}, {n - 1, n}] -> b, 
    Band[{2, 3}, {n - 1, n}] -> c}, {n, n}];
 MatrixForm [matC]*
  • $\begingroup$ Can someone help to debug,,it is not running,and i am not able to put the boundary conditions. $\endgroup$ – user3005 Oct 7 '12 at 9:52
  • $\begingroup$ Welcome to the site! Perhaps you could explain your problem somewhat better. $\endgroup$ – Dr. belisarius Oct 7 '12 at 10:21
  • $\begingroup$ Probably related: mathematica.stackexchange.com/questions/3098/… $\endgroup$ – Verbeia Oct 7 '12 at 10:57
  • 4
    $\begingroup$ Welcome, @user3005. Please understand that pasting a swathe of code with "it doesn't work" doesn't make it easy for people to help you. The problem is probably that you are multiplying not using Dot (.) and you wrapped the lists in MatrixForm before multiplying. See the question I linked previously. $\endgroup$ – Verbeia Oct 7 '12 at 10:59
  • 2
    $\begingroup$ @user3005 yes, posting the code is important, but we need to know more than just "it doesn't work". What error messages are you getting? Are you running from a freshly launched kernel? What output are you expecting? Have a look at some other questions on the site, especially those with at least 5 votes, and you will see how to write a question that shows a problem clearly. By the way, comments can be at least this long so you don't need to write in SMS speak. $\endgroup$ – Verbeia Oct 7 '12 at 19:32

I've done a lot of option pricing with MMA, but I can't relate your code to plain old Black-Scholes:

d1[spot_,strike_,ir_,div_,vol_, T_] = (Log[spot/strike] + (ir-div+vol^2/2) T) / (vol T^0.5]);  
d2[spot_,strike_,ir_,div_,vol_, T_] = (Log[spot/strike] + (ir-div-vol^2/2) T) / (vol T^0.5]);
N[z_] = (1 + Erf[z/Sqrt[2]])/2 ; (*cumulative normal density*)

bsCall[spot_,strike_,ir_,div_,vol_,T_] :=
   spot*E^(-div*T)*N[d1[spot,strike,ir,div,vol, T]] - 


ir = interest rate  
div=dividends paid  
vol=volatility of the stock as measured by the standard deviation of its price
T = time to expiry  
Log[] = the natural logarithm

(I added this simple stuff to help others understand your question.) I don't see where the Black-Scholes part comes into your code? It would help educate others if you put your question in a context, for example, black-scholes option pricing is easy if you have one option, but if you have 100,000 at varying strikes, etc. But your question seems to really be about matrix algebra and sparce arrays.

  • $\begingroup$ thank you very much for the reply. In fact it is more on ''code for pricing an European option under Black–Scholes model using ETD with central discretisation''. I have been introduced to the concept of boundaries in option pricing but i have no idea how to include it in the code. $\endgroup$ – user3005 Oct 7 '12 at 19:09
  • 1
    $\begingroup$ I wouldn't use capital N in your function definition, as it is a built-in one. Also, using n[z_]=CDF[NormalDistribution[0, 1], z] would arguably yield a slightly better readable and more generalizable version of your definition. $\endgroup$ – Sjoerd C. de Vries Oct 7 '12 at 19:26
  • $\begingroup$ @Sjoerd I actually used an italicized N in my code, along with other symbols that don't show well here, but didn't want to clutter my comment. I didn't know about CDF[] when I wrote this code. I just copied. I should look at it. Thanks. $\endgroup$ – George Wolfe Oct 7 '12 at 20:01

You have a syntax error in the last line. I suspect you mean:

matC.vecMatrix // MatrixForm

which yields:

0.19875 v[1]+0.55125 v[2]
0.09 v[1]-0.2325 v[2]+0.1125 v[3]
0.09 v[2]-0.2325 v[3]+0.1125 v[4]
0.09 v[3]-0.2325 v[4]+0.1125 v[5]
0.09 v[4]-0.2325 v[5]+0.1125 v[6]
0.09 v[5]-0.2325 v[6]+0.1125 v[7]
0.09 v[6]-0.2325 v[7]+0.1125 v[8]
0.09 v[7]-0.2325 v[8]+0.1125 v[9]
0.09 v[8]-0.2325 v[9]+0.1125 v[10]
0.55125 v[9]+0.19875 v[10]

As an aside, comments that just say (* define \[Delta] x *) aren't that helpful to the next person looking at the code. I would recommend using commenting to explain what the variables are for.

As a further comment on your code, all your variables are global to your session, so the comment "reusable internal variables" is misleading. If you want to scope variables as internal to a particular calculation, you need to use scoping constructs like Module, Block and With. You might find this question helpful.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.