## Archive for the 'Mathematica' Category

### 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.

### Binomial Trees in Mathematica

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:

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.

### Mathematica Tutorials

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

Mathematica and the Glass Tempering ProcessÂ This is from a talk I gave at a Mathematica conference from my PhD research.Â

### Calculation of MLE for use in Copula using Mathematica

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.