[Problem with loops in Mathematica] - A New Kind of Science: The NKS Forum

A New Kind of Science: The NKS Forum

Pages:1



Problem with loops in Mathematica

(Click here to view the original thread with full colors/images)



Posted by: Kristofer

Hi all,

I am trying to work out the value of S at different nodes in a binomial tree, using the following formula:

Table[S u^j d^(n - j)p^j q^(n - j), {j, 0, n}]

I would like to loop this in some way so that I don't have to assign new values to n all the time (S, d, p, q are given). I have tried For and Do, but when i print the result, Mathematica produces n lists that are equal to the final value of n, and not for n=0,1,..., n-1.

Anyone who could help me with this?



Posted by: Jason Cawley

Do you just want -


With[ {u = .55, d = .45, p = .3, q = 1 - p, n = 10},
 Table[(u p)  ^ i    (d q) ^ (j - i), {j, 0, n}, {i, 0, j}]  ]


Your question isn't entirely clear, because you call an expression a formula for S that has no "equals" sign between a rhs and a lhs, but does contain S, and later say that S is a given (when I think you probably mean u, d, p and q are given - from the sort of problem this is). But I think the above is what you actually wanted.

You can of course have the parts I put in "with" as global variables elsewhere in your code, instead of the "with" construct above, which makes them local to this evaluation. Then you would only need the bit from "Table" to "{i,0,j}]". Obviously you also have to pick some maximum n at which you stop going down additional branches of your tree.

As for the mere programming point, Table supports multiple loops. The first one in the argument list after the expression to be evaluated is the outermost or "largest structure" loop (here, j running from 0 to n). Interior loops can have iterators that depend on exterior ones but not vice versa (the exterior ones are evaluated for the interior ones, so they are integers by the time the interior loop gets them).

General Mathematica support is available through MathGroup, an email list, which is also available as a newsgroup and archived online -

http://forums.wolfram.com/mathgroup/archive/2004/Jul/

Info on how to do particular things in Mathematica can often be found at our Mathematica Information Center, here -

http://library.wolfram.com/infocenter

If all else fails, you can try tech support, here -

http://www.wolfram.com/services/techsupport/

You should always start by just using ?"CommandName" (in this case, ?Table ), then if necessary using the help browser and its examples. Many questions are simple if you look in the right places for answers. While we are happy to answer questions specifically about using Mathematica to investigate NKS type systems, this forum is really for NKS issues rather than general Mathematica help.



Posted by: Kristofer

Jason, first of all I didn't know it was only for NKS and I am sorry about that. Then, let me at least try to explain a bit more thoroughly what I am trying to do.

I am looking to find different values for S(n) in a binomial tree, where n is the number of periods I will be considering. S(n) will take two values for period 1, three values for period 2, etc, and I need these values (in terms of lists) for all periods. Since I intend to look at a large number of periods, I need to automate the way of finding (and storing) these values, and this is where I am stuck. With S(0)=S, u, d, p, and q given, I have to manually assign a new value to n, and use the below formula, to obtain a list of values for S(n) (for the different nodes in the tree) for each n.

S(n)=Table[S u^j d^(n - j)p^j q^(n - j), {j, 0, n}]

However, I can't do this for a large amount of periods and it would be great if I could tell Mathematica to evaluate S(n), for n=1, 2, 3,... by using some sort of loop. I have tried to put the above formula as the expr inside Do[ ] and For[ ], but it doesn't work.

I hope that explanation is a bit better than the last one.

Best,

Kristofer



Posted by: Jason Cawley

Yeah, that is what I thought. Did you try the line I gave you?

You get a list of lists of progressively larger sizes. For 0, just one value, 1. For 1, two values, 0.315 and 0.165. For 2, three values, etc. Obviously these are numbers because I put in parameter values for the variables you said are known. If you leave them as variables you will get the symbolic expressions instead. E.g. -

{{1}, {d q, p u}, {d^2 q ^2, d p q u, p^2 u^2}, {d^3  q^3, 
   d^2 p q^2 u, d p ^2 q u^2, p^3 u^3}, ...


If you call that "Svalues" or something, then Svalues[[0]] will be 1, Svalues[[1]] will be a list of 2 values, {d q, pu}, Svalues[[2]] will be a list of 3 values, etc. That is, for the symbolic expression form you'd use an assignment like -

Svalues = With[{n = 10}, 
  Table[(u*p)^i*(d*q)^(j - i), {j, 0, n}, {i, 0, j}]]


If you want the first 1000 expressions, just change n to 1000, and you will get lists of symbolic expressions from 2 long to 1001 long. That will take about 15 seconds. Each symbolic expression in the middle will about fill a screen, so you might want to put a semicolon after it before evaluating, and then just ask for the one you care about at any given later stage - e.g. Svalues[[456]] or whatever - or evaluate beforehand if you have numerical values for any of your variables, to reduce the blizzard of u's and p's to numbers.

If you want to multiply them all by S you can, obviously. Then Svalues[[0]] will equal S, etc.



Posted by: Kristofer

Great, thanks a lot!





Forum Sponsored by Wolfram Research

© 2004-2009 Wolfram Research, Inc. | Powered by vBulletin 2.3.0 © 2000-2002 Jelsoft Enterprises, Ltd. | Disclaimer
vB Easy Archive Final - Created by Xenon and modified/released by SkuZZy from the Job Openings