Tips, Tricks and Traps
Mar 16, 2008 in Mathematica
Tips, Tricks and Traps Another Mathematica talk I gave covering some of my favorite tips, tricks and traps.
Mar 16, 2008 in Mathematica
Tips, Tricks and Traps Another Mathematica talk I gave covering some of my favorite tips, tricks and traps.
Mar 16, 2008 in Derivatives, Mathematica
In generating a binomial tree, we have to first generate a tree of stock prices. Using the centered tree approach of Cox-Rubenstein-Ross, we define Si,j =S0,0 uj di-j
If we want to generate a tree that looks like the one below,
|
 |  |  |  | 100.1 |
 |  |  |  | 100.08 |  |
 |  |  | 100.06 |  | 100.06 |
 |  | 100.04 |  | 100.04 |  |
 | 100.02 |  | 100.02 |  | 100.02 |
100 | Â | 100 | Â | 100 | Â |
 | 99.98 |  | 99.98 |  | 99.98 |
 |  | 99.96 |  | 99.96 |  |
 |  |  | 99.94 |  | 99.94 |
 |  |  |  | 99.92 |  |
 |  |  |  |  | 99.9 |
We can use code such as
StockTree=Table[S[i,j],{i,0,n},{j,i,0,-1}];Â Show[Graphics[{Text[“Stock Price”,{2,2}],Table[Text[Reverse[StockTreePi\[RightDoubleBracket]],{2,2-i}],{i,1,n+1}]}]]
However there is a problem when we want to evaluate the StockTree for calculation of puts or calls. In standard form, our stock tree looks like this:
{{100},{100.02,99.98},{100.04,100.,99.96},{100.06,100.02,99.98,99.94},{100.08,100.04,100.,99.96,99.92},{100.1,100.06,100.02,99.98,99.94,99.9}}
Suppose we want to calculate a call tree, where we want Max[S-K,0].Â
The Max function won’t map across the list.Â
For a simpler example, consider the list created as
myTable = Table[Table[i,{i,1,j}],{j,1,5}]{{1},{1,2},{1,2,3},{1,2,3,4},{1,2,3,4,5}}
If you try taking Max[myTable-3,0], you will just get the value of 2 (from the last element 5), when what we really wanted was to have the Max function applied across all elements.Â
We could flatten and restructure the list, applying Max element by element, but it seems there should be a better way. The reason you can’t get the result you want is because the built in Max function does not have the Listable attribute. To see the attributes, just type Attributes[Max]. The result is:
{Flat,NumericFunction,OneIdentity,Orderless,Protected,ReadProtected}
Not that it’s a good idea to change the attributes of built in functions, but you can type Attributes[Max]={Listable}. Now look what happens when you type Max[myTable]: you get the desired result of {{0},{0,0},{0,0,0},{0,0,0,1},{0,0,0,1,2}}
Now, we can go ahead and define our call and put functions CallTree = Max[StockTree-K,0] whose result is
{{0},{0.020002,0},{0.040008,0.,0},{0.060018,0.020002,0,0},{0.080032,0.040008,0,0,0},{0.10005,0.060018,0.020002,0,0,0}} and so forth.
Feb 24, 2008 in Mathematica
Here are some old, but hopefully still good, tutorials I wrote while teaching Mathematica. They are in pdf format, but if you want the notebook format, just email jennifer@quantcandy.com
Introduction to Mathematica – A TutorialÂ
Illuminating Numerical Analysis Using MathematicaÂ
Mathematica and the Glass Tempering Process This is from a talk I gave at a Mathematica conference from my PhD research.Â
Jan 30, 2008 in Copulas, Mathematica
 I wanted to construct a trivariate meta-t distribution from empirical marginal distributions. I wrote a Mathematica notebook that would solve for the optimal degrees of freedom in the distribution given the input empirical distributions and correlation matrix. The PDF version of the code to generate the optimal t value is shown in the attached PDF Multivariate Distributions. What is really great about Mathematica is that I could have it symbolically derive the log likelihood function, by giving some definition such as
loglik[n_]:=n Log[Gamma[(n+d)/2]/Gamma[n/2]]-n d/2 Log[Pi n] – n/2 Log[Det[tau]]-(n+d)/2 (Sum[Log[1+ {x[[i]],y[[j]]}.invSigma .{x[[i]],y[[j]]}/n],{i,1,d},{j,1,d}])
and then asking it to take the derivative with respect to n.