an introduction to mathematica - university college...

70
An Introduction to Mathematica A System for Doing Mathematics by Computer (based on Ch 2 of revised MFDwM) including data acquisition and analysis; parallel methods. 2.1 Systems, Kernels and Front Ends The first thing that you need to do is to learn how to communicate with the computer and the Mathematica software system. It is important to realize that the software is in two pieces. The calculations are done by a computational "kernel", but you communicate with the kernel through a front end. The kernel and the front end may be running on the same computer, or on different computers linked by a network. This introduction is targeted at people using a windowing operating system, and the front end is a Notebook interface. This interface creates and uses Mathematica Notebook files - you can exchange Notebooks between computer systems such as MacOS, Windows XP/NT and UNIX. The Notebooks are just text files, and can be passed between systems using only text-translation utilities. If you have not used Mathematica before, how to proceed depends on whether you have Mathematica already installed on your computer. If it is not installed at all, you may need some help if you are working in a multi-computer networked environment running under UNIX, for example - under these circumstances, it is almost certainly best to request help from your System Administrator. If you are running on a solitary system, or otherwise doing an installation local to your computer, it is usually sufficient to follow the instructions in the Mathemat- ica installation instructions. These are contained in the booklet "Getting started with Mathematica on Macintosh/Windows (etc.) systems", specific to your own computer. If you have never used Mathematica before, it will be helpful to run through the examples contained in that booklet. The discussion below is designed to complement that booklet, but is essentially self-contained. Begin by starting Mathematica. You can do this on any system by navigating to the icon for Mathematica, and double-clicking on it. Do not use the "MathKernel" program icon for this. On particular systems there will be other routes to access Mathematica. Under Windows, the program can be found under the "Start" task. On both Windows and MacOS, it is convenient to make short-cuts/aliases and place them on your desktop - it is really up to you to customize your route to access the system once you have figured out what is most convenient. Mathe- matica will start up, giving the version number, your licence information, and warnings about software piracy. ü Input and Output When a new Notebook is opened you can type straight into the blank window. Type "3 + 3" into the system (not the quotes, just the expres- sion within the quotes). To enter this expression hold down the shift key and hit the "return" key. Mathematica labels both your input and its output with numbers, in this case [1], if you are starting a fresh session. You should see the following, with the input prefixed by In[1], the output prefixed by Out[1]: = 3+3 = 6 Now type the command "Expand[(2 x + y)^3]", and enter this by SHIFT-RETURN (ENTER, not to be confused with RETURN, and usually located at the bottom right of keyboards, may also work and is a useful short-cut on some computers). 12

Upload: others

Post on 29-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

An Introduction to Mathematica A System for Doing Mathematics by Computer(based on Ch 2 of revised MFDwM)including data acquisition and analysis; parallel methods.

2.1 Systems, Kernels and Front Ends

The first thing that you need to do is to learn how to communicate with the computer and the Mathematica software system. It is importantto realize that the software is in two pieces. The calculations are done by a computational "kernel", but you communicate with the kernelthrough a front end. The kernel and the front end may be running on the same computer, or on different computers linked by a network. Thisintroduction is targeted at people using a windowing operating system, and the front end is a Notebook interface. This interface creates anduses Mathematica Notebook files - you can exchange Notebooks between computer systems such as MacOS, Windows XP/NT and UNIX.The Notebooks are just text files, and can be passed between systems using only text-translation utilities.

If you have not used Mathematica before, how to proceed depends on whether you have Mathematica already installed on your computer. Ifit is not installed at all, you may need some help if you are working in a multi-computer networked environment running under UNIX, forexample - under these circumstances, it is almost certainly best to request help from your System Administrator. If you are running on asolitary system, or otherwise doing an installation local to your computer, it is usually sufficient to follow the instructions in the Mathemat-ica installation instructions. These are contained in the booklet "Getting started with Mathematica on Macintosh/Windows (etc.) systems",specific to your own computer. If you have never used Mathematica before, it will be helpful to run through the examples contained in thatbooklet. The discussion below is designed to complement that booklet, but is essentially self-contained.

Begin by starting Mathematica. You can do this on any system by navigating to the icon for Mathematica, and double-clicking on it. Do notuse the "MathKernel" program icon for this. On particular systems there will be other routes to access Mathematica. Under Windows, theprogram can be found under the "Start" task. On both Windows and MacOS, it is convenient to make short-cuts/aliases and place them onyour desktop - it is really up to you to customize your route to access the system once you have figured out what is most convenient. Mathe-matica will start up, giving the version number, your licence information, and warnings about software piracy.

ü Input and Output

When a new Notebook is opened you can type straight into the blank window. Type "3 + 3" into the system (not the quotes, just the expres-sion within the quotes). To enter this expression hold down the shift key and hit the "return" key. Mathematica labels both your input and itsoutput with numbers, in this case [1], if you are starting a fresh session. You should see the following, with the input prefixed by In[1], theoutput prefixed by Out[1]:

In[1]:= 3+3

Out[1]= 6

Now type the command "Expand[(2 x + y)^3]", and enter this by SHIFT-RETURN (ENTER, not to be confused with RETURN, andusually located at the bottom right of keyboards, may also work and is a useful short-cut on some computers).

12

Page 2: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[2]:= Expand[(2 x + y)^3]

Out[2]= 8 x3 + 12 x2 y + 6 x y2 + y3

Throughout this chapter, whenever we present examples like this, expressions in bold type are what you should type, followed by SHIFT-RETURN or ENTER. To see some of the capabilities of Mathematica try the following sequences of statements. When you enter thecommands in bold, they will acquire "In[n] := " labels. Also, if you make a mistake, just re-type the correct expression. We will come backto how to edit expressions shortly; the following are all short enough to make re-typing painless.

The first example uses the N function to ask for p, denoted in Mathematica's Input Form by Pi, to 100-digit precision.

In[3]:= N[Pi, 100]

Out[3]= 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

The next example introduces the plotting capabilities of Mathematica by using Plot. Given the function and the range over which to drawthe graph, the Plot function will produce the appropriate graph.

In[4]:= Plot[x^2 Sin[x], {x, -3, 3}]

Out[4]=-3 -2 -1 1 2 3

-4

-2

2

4

In this simple program we define the function f that gives the fourth power of its argument and then apply f twice to y. Use an ordinaryRETURN to get to the second line for typing, and then SHIFT-RETURN to execute the expression. Note the colon and semicolon which weshall examine later.

In[5]:= f[x_] := x^4; f[f[y]]

Out[6]= y16

13 MathematicaIntroduction.nb 2. Introduction to Mathematica

13

Page 3: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

The final example shows the incremental capabilities of Mathematica by using the Table function to produce a list of the cubes of integersup to 10.

In[7]:= cubes = Table[ i^3, {i, 0, 10}]

Out[7]= 80, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000<

We can plot given data like the list cubes using the function ListPlot.

In[8]:= ListPlot@cubesD

Out[8]=

2 4 6 8 10

200

400

600

800

1000

Don't worry about the details of the positioning of the axes for now. But you should note that if asked to plot a simple one-dimensional list,Mathematica will use the position in the list as the "x" or horizontal coordinate, so that, e.g., the last element is plotted as the point (11,1000).

ü Remarks

In our examples we have seen the four basic areas of Mathematica. These are:

ü Numerical Calculations

For example, we asked for the numerical value of p to 100 digits.

ü Symbolic or Algebraic Calculations

We asked for the expansion of H2 x + yL3.

2. Introduction to Mathematica MathematicaIntroduction.nb 14

14

Page 4: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

ü Graphics

We plotted a function, and a sequence of numbers.

ü Programming

We defined a function, and asked for it to be applied to a specific argument.

We will explore each of these topics in more detail later. Right now it is necessary to do some groundwork on understanding how Mathemat-ica works. From here on, the expressions that we enter may be a little longer, and if we make a mistake we don't want to have to keep re-typing our input. So at this point it will be useful to learn a few more tricks for managing our input.

ü Using the Cursor

If you move the cursor up and down the Notebook in which you have just done the previous calculations you will see how it changes formdependent on which type of cell it is lying in, or whether it is between cells. A horizontal line under a cell indicates that any text typed willbe placed after that cell. To place a horizontal line after any cell you move the cursor so that it indicates that it is lying between cells, i.e.when it is horizontal, and then click. Practice will get you used to being able to place text anywhere in a Notebook.

ü Opening and Closing Cells

In the Notebook environment all text is held in cells. These can have different styles associated with them, and can be set up to allowdifferent styles of cell to be nested within one another. By double clicking on any cell bracket which is grouping other cells you will be ableto hide the text contained in those sub-cells. This can be retrieved by double-clicking on the same cell bracket again. When a cell containshidden sub-cells you will notice how the lower edge of the bracket changes to a filled triangle. You may find this way of hiding informationin the Notebooks useful in organizing a presentation or teaching session. It may take some practice to get used to how these cells are nested.

ü Editing Text in Cells

Changing the text in a cell is done in a similar way to that used in word-processing applications. The cursor can be placed at any particularpoint in the text by moving the mouse and clicking on that spot. Selecting text with the mouse allows that text to be removed or typed over.Notice how you can only edit text in certain cells. You will be able to edit your input cells but not in the cells containing the output. This hasbeen set by the way in which the different cells are defined. Here are some handy tips:

(1) use the mouse and scroll bars to get up and down the Notebook;(2) use an ordinary RETURN to enter the next line of a multi-line expression;(3) use SHIFT-RETURN (or ENTER) to execute an expression;(4) use the mouse COPY and PASTE to grab previous formulae.

ü Quitting and Panicking

Sometimes, say if you make a mistake, Mathematica may go into a long internal session and all you want it to do is stop. If you need to stopMathematica in the middle of a calculation, go to the menu and choose "Kernel". You then get some options, of which the ones to pick are"Abort Evaluation" or "Interrupt Evaluation...". The menus also offer keyboard short-cuts specific to your operating system, which youshould learn. The kernel can sometimes take time to abort a calculation. It is better to wait for it to finish sorting itself out than to keepthumping the keyboard! If you lose patience waiting, or otherwise wish to exit Mathematica completely, you can click on "Quit" havinginterrupted the session, or type Quit at an "In" prompt. There are other options in the "Kernel" menu for quitting and re-starting.

15 MathematicaIntroduction.nb 2. Introduction to Mathematica

15

Page 5: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

Sometimes, say if you make a mistake, Mathematica may go into a long internal session and all you want it to do is stop. If you need to stopMathematica in the middle of a calculation, go to the menu and choose "Kernel". You then get some options, of which the ones to pick are"Abort Evaluation" or "Interrupt Evaluation...". The menus also offer keyboard short-cuts specific to your operating system, which youshould learn. The kernel can sometimes take time to abort a calculation. It is better to wait for it to finish sorting itself out than to keepthumping the keyboard! If you lose patience waiting, or otherwise wish to exit Mathematica completely, you can click on "Quit" havinginterrupted the session, or type Quit at an "In" prompt. There are other options in the "Kernel" menu for quitting and re-starting.

ü Brackets Galore

Our first examples show that we need to understand how Mathematica reads expressions, and especially different types of brackets: [ ], { }, (). Sometimes people use square brackets [], round brackets () and curly brackets {} more or less interchangeably. In order to ensure that thecomputer understands unambiguously what we mean, it is necessary to introduce some conventions.

ü Arguments of Functions

Arguments of functions are always enclosed in square brackets. This applies just as much to standard mathematical functions like Sin[x]as to built-in Mathematica graphics functions like Plot3D[], which take another function and some ranges as a minimum set of argu-ments, and user-defined functions like f above.

ü Grouping

When we wish to group together some expressions for applying a subsequent operation, round brackets are employed. For example (x +y)^3 means the cube of all of x + y. Note that this distinction gives the expressions f[x+y] and f(x+y) two quite different meanings,and at the same time we have acquired the freedom to use a space between adjacent quantities to denote multiplication. If you wish to youcan explicitly type a * to denote multiplication. For example 2 3 is identical in meaning to 2*3.

Note that you don't always need to use round brackets for grouping, since the usual rules of precedence apply. For example x^3 + 4 means(x^3) + 4 and not x^(3 + 4). A good policy is to use grouping brackets in any situation where there is a potential for ambiguity.

ü List Brackets

Curly brackets { } and double square brackets [[ ]] are also used in Mathematica. These are used with list structures to denote a list, andan indexed member of a list, respectively. We shall look specifically at these structures later on. For now it will suffice to appreciate that alist consists of a number of elements, separated by commas, with a curly bracket at each end.

ü Input/Output, Standard and Traditional Forms

Older versions of Mathematica prior to version 3.0 used styles where inputs and outputs were both based on pure text representations.Mathematica nwo routinely uses forms called "Standard Form" and "Traditional Form". These are much more like traditional mathematicalnotation - indeed, this is exactly the idea of "Traditional Form". "Standard Form" is a half-way form that goes as far as possible towardstraditional mathematical notation without introducing any ambiguities. Throughout this chapter the default output format type is "Standard".You can ask for explicit conversion as follows:

2. Introduction to Mathematica MathematicaIntroduction.nb 16

16

Page 6: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[9]:= expression = Exp@-x^2D;StandardForm@expressionD

Out[10]//StandardForm=

‰-x2

In[11]:= TraditionalForm@expressionD

Out[11]//TraditionalForm=

‰-x2

In[12]:= InputForm@expressionD

Out[12]//InputForm=E^(-x^2)

These conversions can be done using the menus, and defaults can also be set. It is impossible to give a hard and fast rule that has beenapplied throughout this book, for how expressions are represented, as the choice is rather sensitive to the context, but we try to adhere to the"WYSIWMAMA" (what you see is what Mathematica made) principle - note the partially double-struck notation. Generally, chaptersinvolving more mathematical development will make more use of the traditional form for outputs and equations.

2.2 Basic Graphics

Before we launch into investigating each of the main areas of Mathematica in detail, it will be useful to look at some of the basic graphiccommands used in Mathematica. While working in Mathematica it is very often helpful to involve graphics in a calculation. This is nevermore true than when looking at new ideas and will be used extensively through this chapter and the rest of the book, to illustrate points moreclearly. For this reason, it will be useful if you feel reasonably comfortable with the most basic graphics commands at the outset. Do not betoo concerned about the details of their operation at this point. We shall look more carefully at the range of graphics capabilities available inMathematica later.

ü Producing a Plot

We have already seen the function Plot which will display the plot for a given function. This needs to have the range of values over whichthe plot is to be made specified. You supply Plot with two arguments - the first is the function, and the second is a list in the form{variable, start, end}.

17 MathematicaIntroduction.nb 2. Introduction to Mathematica

17

Page 7: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[13]:= Plot[x^3 - 8 x^2 + 19 x - 8, {x, 0, 5}]

Out[13]=

1 2 3 4 5

2

4

6

8

10

12

Given a set of values expressed as a list data, the function ListPlot will plot data assuming that data is regularly spaced, and willuse the position in the list as the horizontal coordinate. The command which changes the PlotStyle plots the points large so that we cansee them clearly.

In[14]:= data = {3,-0.5,2,0,1,0.5};points = ListPlot[data,PlotStyle -> PointSize[0.03]]

Out[15]=

1 2 3 4 5 6

-0.5

0.5

1.0

1.5

2.0

2.5

3.0

ü Displaying a Plot Again

All graphics can be displayed again using the function Show. We have given our last plot the name points. We can now ask to see thatparticular plot again.

2. Introduction to Mathematica MathematicaIntroduction.nb 18

18

Page 8: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[16]:= Show[points]

Out[16]=

1 2 3 4 5 6

-0.5

0.5

1.0

1.5

2.0

2.5

3.0

All of the graphics displayed can be altered to suit particular needs. In our plot for ListPlot we specified a particular size for the pointsto be plotted. Similarly, we can ask for those points to be joined by a continuous line.

In[17]:= pointsb = ListPlot@data, Joined Ø TrueD

Out[17]=

1 2 3 4 5 6

-0.5

0.5

1.0

1.5

2.0

2.5

3.0

If you have executed the previous few commands on a computer you may have received a spelling warning. Here and elsewhere in this textwe have suppressed messages from Mathematica warning us about possible spelling mistakes suggested by a new variable name being verymuch like one previously introduced. The Show command can be used to combine plots:

19 MathematicaIntroduction.nb 2. Introduction to Mathematica

19

Page 9: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[18]:= Show@points, pointsbD

Out[18]=

1 2 3 4 5 6

-0.5

0.5

1.0

1.5

2.0

2.5

3.0

2.3 Numerical Calculations

ü Exact Arithmetic

ü Degrees of Precision

With most computer languages you are restricted to certain levels of precision in the values of numbers. This is not the case when you areusing Mathematica. Try the following series of inputs.

In[19]:= 30!

Out[19]= 265 252 859 812 191 058 636 308 480 000 000

In[20]:= {N[30!], N[30!, 23]}

Out[20]= 92.65253 µ 1032, 2.6525285981219105863631 µ 1032=

The first result is exact, the second is approximate and has used the degree of precision given by default. In the last expression we havespecified a precision of 23 digits.

ü Exact and Approximate Numbers

When we ask for the square root of a number, for example, Mathematica will return the most exact version.

2. Introduction to Mathematica MathematicaIntroduction.nb 20

20

Page 10: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[21]:= Sqrt[51]

Out[21]= 51

Note that if you are using a much older version of Mathematica, the output here will differ - you will still see the letters "Sqrt" in the output.Only when we ask explicitly for that to be given in a numerical form will Mathematica return a number which approximates this square root.

In[22]:= N[Sqrt[51]]

Out[22]= 7.14143

In evaluating the last expression Mathematica displays the answer to six significant figures. It will have been calculated, in its internalnumerical form, to a higher precision, which will be dependent on the machine you are using. We can find the degree of precision by askingfor the value of Precision on that particular output line,

In[23]:= Precision[%]

Out[23]= MachinePrecision

So in this case, the result will have been calculated to 16 SF, but only displayed to six figures. If that result were to be used in any furthercalculations, the full 16 figures would be used.

ü How Mathematica Decides on Exact or Approximate

This following example highlights the different routes Mathematica will take when dealing with numerical expressions.

In[24]:= (3/4)^100

Out[24]= 515 377 520 732 011 331 036 461 129 765 621 272 702 107 522 001 ê1 606 938 044 258 990 275 541 962 092 341 162 602 522 202 993 782 792 835 301 376

In this expression the answer has been returned in the most exact form possible. Mathematica will keep to that form unless explicitly askedotherwise, by the use of N[ ], for example. An alternative way of presenting the above calculation is

In[25]:= 0.75^100

Out[25]= 3.2072 µ 10-13

This second expression has numbers given in their floating point or approximate form, the expression is then kept in this approximate formand the answer given in that form. Any number with a decimal point in it will be taken to be approximate. Of course, we can always ask forthat approximation to be more precise:

21 MathematicaIntroduction.nb 2. Introduction to Mathematica

21

Page 11: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

This second expression has numbers given in their floating point or approximate form, the expression is then kept in this approximate formand the answer given in that form. Any number with a decimal point in it will be taken to be approximate. Of course, we can always ask forthat approximation to be more precise:

In[26]:= N[%,10]

Out[26]= 3.2072 µ 10-13

ü A Practical Example with Exact and Approximate Numbers

For another illustration of these two paths down which numbers may be sent, we can look at solutions to equations. Giving the equation inexact form:

In[27]:= Solve[{x^2 + 3 x + 11/10 == 0}, x]

Out[27]= ::x Ø1

10J-15 - 115 N>, :x Ø

1

10J-15 + 115 N>>

In[28]:= N[%]

Out[28]= 88x Ø -2.57238<, 8x Ø -0.427619<<

Alternatively, in an approximate form:

In[29]:= Solve[{x^2 + 3 x + 1.1 == 0}, x]

Out[29]= 88x Ø -2.57238<, 8x Ø -0.427619<<

Again, we chose the way in which numbers would be treated by the way we presented the equation - the presence of a decimal point impliedthe numerical treatment. This example raises some other issues that it is a good idea to explore right away.

ü A Note on "Equals"

You will have noticed that in the last section we used a double equals sign to denote that an expression was equal to zero. The use of thesingle equals sign will be examined more carefully shortly, where its role in assignment will be illustrated. For the moment it will be suffi-cient to highlight that when defining a symbolic equation the double equals sign should be used. This does not involve assigning any particu-lar values to any variables.

2. Introduction to Mathematica MathematicaIntroduction.nb 22

22

Page 12: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[30]:= x == 0

Out[30]= x ã 0

If actually setting a quantity equal to some value for future substitution the single equals sign is used. This assigns that value to the variable.

In[31]:= x = 0

Out[31]= 0

(Having set this value for x we must clear it for future calculations)

In[32]:= Clear[x];x

Out[33]= x

ü Extracting Results from the Solve Function

It can be frustrating for new users that Mathematica returns results from Solve as some replacement rules indicated by the arrow:

In[34]:= Solve[{x^2 + 3 x + 11/10 == 0}, x]

Out[34]= ::x Ø1

10J-15 - 115 N>, :x Ø

1

10J-15 + 115 N>>

What we really want are a list of values of x given these rules. In Mathematica the term "given" is invoked by the use of "/.", as shown here:

In[35]:= x ê. %

Out[35]= :1

10J-15 - 115 N,

1

10J-15 + 115 N>

You can do the whole thing at once:

23 MathematicaIntroduction.nb 2. Introduction to Mathematica

23

Page 13: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[36]:= x ê. Solve@8x^2 + 3 x + 11 ê 10 == 0<, xD

Out[36]= :1

10J-15 - 115 N,

1

10J-15 + 115 N>

ü Some Useful Numerical Functions

The usual arithmetic functions are as you would expect for +, -, *, ê etc. Note that multiplication is denoted by a star or a space. A cross or"times" symbol is not used. The square root of a number is calculated by the Sqrt function.

In[37]:= Sqrt[78543.]

Out[37]= 280.255

Similar functions exist for exponentials, logarithms and many other numerical functions. You should note that sometimes long words areused, and there may be capitals within the name. For example, The function FactorInteger produces a list of the prime factors of anumber and the power to which they are raised - useful in cryptography!

In[38]:= FactorInteger[15931157760]

Out[38]= 882, 8<, 83, 8<, 85, 1<, 87, 1<, 8271, 1<<

This is telling us that 15 931 157 760 = 28ä 38ä 5ä 7ä 271.

ü From Data to Functions

ü Simple Function Fitting

Suppose that we have a data set and wish to represent it in terms of functions by a least-squares fit. This can be accomplished in a verysimple way in Mathematica. Let's invent some data:

In[39]:= fitstuff = {6, 3, 5, 8, 7}

Out[39]= 86, 3, 5, 8, 7<

The elements of fitstuff are interpreted as being the values of a function at equally spaced value points 1, 2, 3, 4, 5. Now we can plotthose points. Calling the plot dataplot will allow us to use it again later.

2. Introduction to Mathematica MathematicaIntroduction.nb 24

24

Page 14: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[40]:= dataplot = ListPlot[fitstuff, PlotStyle-> PointSize[0.03]]

Out[40]=

1 2 3 4 5

2

4

6

8

Now type the following, exactly as written. Do not be too concerned about the syntax used to define the function psi, we will investigatethis later - you are most concerned about the function Fit.

In[41]:= psi[x_] = Fit[fitstuff, {1, x, x^2, x^3, x^4}, x]

Out[41]= 17. - 15.75 x + 5.04167 x2 - 0.25 x3 - 0.0416667 x4

In[42]:= psiplot = Plot[psi[x], {x, 1, 5}]

Out[42]=

2 3 4 5

4

5

6

7

8

Now we can show the original points of the data set as well as the fitting function, and see that the function passes through them. At thesame time we can see how to override Mathematica's conventions on the vertical plot range:

25 MathematicaIntroduction.nb 2. Introduction to Mathematica

25

Page 15: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[43]:= Show[psiplot, dataplot, PlotRange -> {0, 10}]

Out[43]=

2 3 4 5

0

2

4

6

8

10

ü Fitting Functions to an Irregular Array

This approach can be applied to lists of pairs of Hx, yL values also, and any set of functions can be used to construct the function, providedthey appear with unknown coefficients in a linear combination. For example for the set of data tStuff, we can fit a trigonometric functionto the data.

In[44]:= tStuff={{0,2.8},{2,11.},{3,5.3},{5,-4.4},{8,11.5}};

In[45]:= mi[x_] = Fit[tStuff, {1, Sin[x], Cos[x]}, x]

Out[45]= 3.47465 - 0.685387 Cos@xD + 7.99654 Sin@xD

In[46]:= miPlot = Plot[mi[x],{x,0,8}]

Out[46]=

2 4 6 8

5

10

2. Introduction to Mathematica MathematicaIntroduction.nb 26

26

Page 16: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[47]:= tStuffPlot = ListPlot[tStuff,PlotStyle->PointSize[0.03]]

Out[47]=

2 4 6 8

5

10

In[48]:= Show[miPlot,tStuffPlot]

Out[48]=

2 4 6 8

5

10

You should appreciate that using this ordinary Fit function you can fit any model that is linear in the unknown parameters. The basisfunctions can be anything you like.

ü Other Numerical Operations

ü Numerical Solution of Equations

We have already seen how to solve a quadratic. Mathematica can cope with all polynomials up to degree 4 analytically. For degree 5 andabove, usually a numerical approach is required. This involves the NSolve function.

In[49]:= NSolve[5 x^5 - 15 x^3 + 10 x + 1 == 0, x]

Out[49]= 88x Ø -1.45682<, 8x Ø -0.901736<, 8x Ø -0.101566<, 8x Ø 1.11145<, 8x Ø 1.34867<<

27 MathematicaIntroduction.nb 2. Introduction to Mathematica

27

Page 17: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

NSolve and its symbolic counterpart can be used to solve sets of equations also:

In[50]:= NSolve[{y + 3 x == x^3, 4 y^2 - x == 4}, {x, y}]

Out[50]= 88x Ø 1.48747, y Ø -1.17127<, 8x Ø -1.58411, y Ø 0.777157<, 8x Ø 0.364297, y Ø -1.04455<,8x Ø -0.331356, y Ø 0.957685<, 8x Ø -1.84345, y Ø -0.73426<, 8x Ø 1.90714, y Ø 1.21523<<

To obtain the results as a list of Hx, yL pairs, you use /. for given, as before:

In[51]:= {x, y} /. %

Out[51]= 881.48747, -1.17127<, 8-1.58411, 0.777157<, 80.364297, -1.04455<,8-0.331356, 0.957685<, 8-1.84345, -0.73426<, 81.90714, 1.21523<<

For functions other than linear and polynomial systems, the FindRoot function is better. You give it a starting point and it iterates fromthere to a root:

In[52]:= FindRoot[Cos[x] == 0, {x, 1.5}]

Out[52]= 8x Ø 1.5708<

The result is a numerical approximation to p/2:

In[53]:= N[Pi/2]

Out[53]= 1.5708

However, this function should be used with some care. (The function FindRoot operates using Newton's method which, if you haveexperience with its use, you will know is most effective when supplied with an approximation very near to a root.) For example if we supplya first approximation of 3.1, the root finally given is nowhere near 3.1.

In[54]:= FindRoot[Cos[x] == 0, {x, 3.1}]

Out[54]= 8x Ø -20.4204<

The tangent to the curve y = cosHxL, at x = 3.1, is nearly horizontal, so that the first iteration takes us to a point far away from the startingvalue. The process then converges! In financial applications FindRoot is particularly useful for solving non-linear equations, such as arisein calculating implied volatility, solving for an internal rate of return, or obtaining a critical price defined by a non-linear equation. We shallsee its use many times, and consider in more detail later.

2. Introduction to Mathematica MathematicaIntroduction.nb 28

28

Page 18: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

2.4 Lists

There are some recurrent patterns in Mathematica of which you will need to be aware, no matter what the type of calculation you are doing.The use of list structures in Mathematica occurs in many diverse areas. We have already used them without too much fuss - now is a goodtime to go into a little more detail.

ü One-Dimensional Lists

Lists are used when dealing with data, or tables of values. A list is denoted by using curly brackets to enclose the elements of the list, whichare separated from each other by commas. We have already used some lists in our examples. An example of a simple list is

In[55]:= data = {0,1,4,9,16,25,36,49,64,81,100}

Out[55]= 80, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

We can select individual elements of such a list, or index elements of the list using the number which shows their position in the list. Thisinvolves the fourth type of bracketing, that of double square brackets. For example we can select the fourth element of the list data:

In[56]:= data[[4]]

Out[56]= 9

There are built-in functions for re-arranging the elements of lists including functions such as Reverse, which re-orders the list in theopposite direction, and RotateRight, which takes the last element of the list and brings it to the front.

In[57]:= Reverse[data]

Out[57]= 8100, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0<

In[58]:= RotateRight[data]

Out[58]= 8100, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81<

The functions Prepend and Append can be used to add elements to lists.

29 MathematicaIntroduction.nb 2. Introduction to Mathematica

29

Page 19: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[59]:= Prepend[data, 1001]

Out[59]= 81001, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

In[60]:= Append[data, 25000]

Out[60]= 80, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 25 000<

Note that neither of these changes the meaning of data:

In[61]:= data

Out[61]= 80, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

The functions PrependTo and AppendTo have the same effect as Prepend and Append, but also update their argument:

In[62]:= PrependTo[data, 1001]

Out[62]= 81001, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

In[63]:= data

Out[63]= 81001, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

We select particular parts using functions, First, Rest and Last.

In[64]:= First[data]

Out[64]= 1001

In[65]:= Rest[data]

Out[65]= 80, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

2. Introduction to Mathematica MathematicaIntroduction.nb 30

30

Page 20: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[66]:= Last[data]

Out[66]= 100

Our lists may well be generated within Mathematica by use of the Table command (examined in more detail later), or they may be gener-ated outside Mathematica, imported and then manipulated, possibly to obtain a graphic representation.

ü Arrays, Matrices and Lists of Lists

Next up from a list is the concept of a list of lists. We use this idea in several ways. Here is an example of a list of lists:

In[67]:= listb = {{1,2,1,2}, {4,3,4,3}, {7,8,9,6}}

Out[67]= 881, 2, 1, 2<, 84, 3, 4, 3<, 87, 8, 9, 6<<

We have already seen how the function FactorInteger supplies its answer in a list of lists form. Also, lists of lists are how Mathemat-ica represents matrices, and lists of Hx, yL pairs.

In[68]:= MatrixForm[listb]

Out[68]//MatrixForm=1 2 1 24 3 4 37 8 9 6

We can ask about the dimensions of this matrix.

In[69]:= Dimensions[listb]

Out[69]= 83, 4<

A list of lists is the way we represent any two-dimensional array of numbers. It could be an array for plotting or a matrix, or a rank-2 tensor.Matrix multiplication is specified by a "dot" (.), to distinguish it from element by element multiplication of lists. The simple square matrixSqMatrix will illustrate this.

In[70]:= SqMatrix={{1,6},{4,2}}

Out[70]= 881, 6<, 84, 2<<

31 MathematicaIntroduction.nb 2. Introduction to Mathematica

31

Page 21: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[71]:= SqMatrix.SqMatrix

Out[71]= 8825, 18<, 812, 28<<

In[72]:= SqMatrix SqMatrix

Out[72]= 881, 36<, 816, 4<<

Selecting out specific rows is done in a similar way to that in which we select individual elements from a list. The second row of SqMatrixis therefore denoted by

In[73]:= SqMatrix[[2]]

Out[73]= 84, 2<

Similarly we can select an individual element in a matrix by specifying its position in the matrix by row and then column, just as whenreferring to an entry in the form Ai j :

SqMatrix[[1,2]]

6

Various other standard matrix operations may be carried out using specific Mathematica functions. For example we can transpose a matrix,

In[74]:= MatrixForm[Transpose[listb]]

Out[74]//MatrixForm=1 4 72 3 81 4 92 3 6

or find the inverse of a square matrix. To demonstrate this we first add a row to listb to make a square matrix:

2. Introduction to Mathematica MathematicaIntroduction.nb 32

32

Page 22: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[75]:= invertible = Prepend[listb, {4, 2, 6, 1}];inverse = Inverse[invertible];MatrixForm[inverse]

Out[77]//MatrixForm=

-1 -5

21 1

2

-1 -11

5-

1

51

1 19

10-

3

5-

1

2

1 3 0 -1

In[78]:= [email protected]

Out[78]//MatrixForm=1 0 0 00 1 0 00 0 1 00 0 0 1

ü Flatten and Partition

One of the manipulations which it may be necessary to perform on a list is the conversion from an n-dimensional list to a one-dimensionallist and back again. This is easily accomplished using Flatten and Partition.

In[79]:= listc = Flatten[listb]

Out[79]= 81, 2, 1, 2, 4, 3, 4, 3, 7, 8, 9, 6<

In[80]:= Partition[listc, 4]

Out[80]= 881, 2, 1, 2<, 84, 3, 4, 3<, 87, 8, 9, 6<<

Note that the second argument to Partition represents the length of the sub-lists generated.

ü Applying Functions to Lists

There are two different situations under which you may want to apply a function to a list. Either the elements of the list are to be used as thearguments to a function, or a function is to be applied to each element of the list individually, the result being another list. These are twosimilar processes, but should not be confused. For functions which act on a sequence of arguments we use the Apply function to supply theargument set.

33 MathematicaIntroduction.nb 2. Introduction to Mathematica

33

Page 23: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[81]:= data

Out[81]= 81001, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

In[82]:= Apply[Plus,data]

Out[82]= 1386

When a function is to be applied to each element of the set we can use the function Map, or for certain numerical functions we can use thewhole set as the argument.

In[83]:= Sqrt[data]

Out[83]= : 1001 , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10>

In[84]:= Map[Sqrt, data]

Out[84]= : 1001 , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10>

In Mathematica you can use lists of arbitrary dimension without making further definitions. So e.g tensors of arbitrary rank may be defined.People doing General Relativity can work with the rank 4 Riemann tensor and its derivatives (rank 5!). In finance you should compare thiswith the utter chaos that pervades the C++ world, where the basic language does not include proper definitions even of matrices. While thatlanguage is powerfully extendable to define anything you like, and indeed many authors of books and software now have invented their ownstructures for arrays, there are now umpteen Classes for matrices lying around!

ü Incrementation and Nesting

ü Incrementation

To perform a simple incremental function there are a few Mathematica functions which can be used.

In[85]:= Do[Print[{n, n^2, n^3}], {n, 1, 4}]

2. Introduction to Mathematica MathematicaIntroduction.nb 34

34

Page 24: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

81, 1, 1<

82, 4, 8<

83, 9, 27<

84, 16, 64<

In this example the command Print[{n, n^2, n^3}] is executed for n taking values starting at 1 and being incremented by one eachtime until n equals 4. The function Table, used to produce lists, is of a similar form.

In[86]:= Table[i^2 +3 i,{i,1,15}]

Out[86]= 84, 10, 18, 28, 40, 54, 70, 88, 108, 130, 154, 180, 208, 238, 270<

In this case the resulting list or table is made up of each of the results obtained when the quantity i2 + 3 i is calculated for i running from 1 to15 in steps of 1.

ü Iterative Application of Functions

Iteration is at the heart of many modern areas of mathematics including dynamical systems, chaos and fractals. Mathematica has somesimple tools for dealing with iterated systems, based on Nest and related functions. Nest in particular is a handy way of simulatingrandom walks. We shall give some examples shortly. Before giving some simpler examples, we make sure that we have removed anyprevious definitions of several handy symbols:

In[87]:= Clear[x, y, f, g, h, z]

The Nest function applies a given function (its first argument) to its second argument a certain number of times, specified by its lastargument:

In[88]:= Nest[g, x, 3]

Out[88]= g@g@g@xDDD

An interesting related function is NestList.

35 MathematicaIntroduction.nb 2. Introduction to Mathematica

35

Page 25: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[89]:= NestList[Sin, x, 3]

Out[89]= 8x, Sin@xD, Sin@Sin@xDD, Sin@Sin@Sin@xDDD<

There is also a neat pair of generalizations to functions of two variables:

In[90]:= Fold[h, x, {a, b, c}]

Out[90]= h@h@h@x, aD, bD, cD

In[91]:= FoldList[h, x, {a, b, c}]

Out[91]= 8x, h@x, aD, h@h@x, aD, bD, h@h@h@x, aD, bD, cD<

ü An Example Using Nest

For example, what does Sin[Sin[Sin ... (100 times)[x]...]] look like? Ask Mathematica:

In[92]:= Plot[Evaluate[Nest[Sin, x, 100]], {x, -2 Pi, 2 Pi}]

Out[92]=-6 -4 -2 2 4 6

-0.15

-0.10

-0.05

0.05

0.10

0.15

The Evaluate function has been used to make the plot easy for Mathematica to draw. It prevents Mathematica repeating the Nestoperation at every point at which the function is sampled. With Evaluate in place it will only do that once. The example shown, and whyit looks like a square wave, are discussed in detail by Gray and Glynn (1990).

ü A Few Useful Observations

Once or twice we have made use of a couple of features that deserve explicit comment.

2. Introduction to Mathematica MathematicaIntroduction.nb 36

36

Page 26: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

ü Referencing Earlier Results

During a sequence of calculations you will probably want to refer to the results of previous calculations. This is achieved easily using a percent sign, %, immediately followed by the number of the output corresponding to the result. Recall the definition of data:

In[93]:= data

Out[93]= 81001, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

A special shorthand for the last computed result is %; %% denotes the next-to-last result.

In[94]:= %

Out[94]= 81001, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100<

In[95]:= %%[[3]]

Out[95]= 1

ü Suppressing Data Output

Our list data is short, so there is no disadvantage in having Mathematica output the full result. If you are working with large data sets,however, you will almost certainly not want to see the results at each stage. Consequently, it may sometimes be desirable to suppress largeresults of purely symbolic operations. In the first of the following operations, the data set bigdata is created without being displayed atall. The semicolon is the key ingredient for suppressing output completely.

In[96]:= bigdata=Table[i^2,{i,0,10000}];

The next example shows a way of producing a shortened form of output. Remember this is only shortening what is displayed. The actualresult is still a list of 10001 terms.

In[97]:= Short@bigdataD

Out[97]//Short=80, 1, 4, á9995à, 99 960 004, 99 980 001, 100 000 000<

You may get different results depending on the exact version of Mathematica you are using.

n.b. An understanding of the suppression of the output with a semi-colon is important in Mathematica technologies beyond version 5.2. Ifyou get a more recent version and find that graphics are not appearing, make sure you delete any semi-colons after the graphics command. Inversion 5.2 or earlier, the graphic would appear even when a semi-colon was present - this is changing!

37 MathematicaIntroduction.nb 2. Introduction to Mathematica

37

Page 27: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

You may get different results depending on the exact version of Mathematica you are using.

n.b. An understanding of the suppression of the output with a semi-colon is important in Mathematica technologies beyond version 5.2. Ifyou get a more recent version and find that graphics are not appearing, make sure you delete any semi-colons after the graphics command. Inversion 5.2 or earlier, the graphic would appear even when a semi-colon was present - this is changing!

2.5 Algebra

ü Simple Algebraic Manipulation

We'll begin by doing some simple algebra. Enter the following statements executing each one as you go. You will see that Mathematica hasits own ideas about how to order the terms in a polynomial – do not let this worry you.

In[98]:= y^4 + 4 y^3 + 6 y^2

Out[98]= 6 y2 + 4 y3 + y4

Now, add 4 y to this last result:

In[99]:= % + 4 y

Out[99]= 4 y + 6 y2 + 4 y3 + y4

Add 1 to this last result, and factorize this:

In[100]:= Factor[% + 1]

Out[100]= H1 + yL4

Undo the factorization by expanding this last result:

In[101]:= Expand[%]

Out[101]= 1 + 4 y + 6 y2 + 4 y3 + y4

Assign x to be this last quartic expression in y:

2. Introduction to Mathematica MathematicaIntroduction.nb 38

38

Page 28: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[102]:= x = %

Out[102]= 1 + 4 y + 6 y2 + 4 y3 + y4

Find the solutions for the quartic expression to be zero, using x as a shorter version of the quartic expression:

In[103]:= Solve[x == 0, y]

Out[103]= 88y Ø -1<, 8y Ø -1<, 8y Ø -1<, 8y Ø -1<<

Assign the value 2 to y, and without displaying that result ask for the resulting value of x.

In[104]:= y = 2;x

Out[105]= 81

Now cancel any values associated with y, and ask for the value of x.

In[106]:= Clear[y];x

Out[106]= 1 + 4 y + 6 y2 + 4 y3 + y4

The three things you should appreciate from this little session are the use of Expand, Factor and Clear. Also, we used Solve asencountered in the Numerical Calculations section, to give us solutions to an algebraic equation. It is a good idea to get into the habit ofusing Clear frequently, to make sure that you do not cause problems simply by trying to define quantities that conflict with earlier defini-tions. You should clear the definitions of x and y now, if you are running this session on a computer.

In[107]:= Clear[x,y]

ü Simplifying Rational Expressions

It is essential that you should have cleared the values for x and y from the last sequence of calculations, if you are trying out this chapter ona computer. If you have not done this Mathematica will go into an internal loop and will complain about exceeding its recursion depth. Ifthis happens you will want to abort the calculation, which as you will remember from the introductory section is done by selecting "InterruptEvaluation..." or "Abort Evaluation" from the Kernel menu.

First, we will input this simple rational expression involving x and y.

39 MathematicaIntroduction.nb 2. Introduction to Mathematica

39

Page 29: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[108]:= y = 1/(x^3 - 4) - 1/(x^2 - 2)

Out[108]= -1

-2 + x2+

1

-4 + x3

Now, we can ask for all of the terms to be gathered together over a common denominator, and this expression to be assigned to be u.

In[109]:= u = Together[y]

Out[109]=2 + x2 - x3

I-2 + x2M I-4 + x3M

Using u, the shorthand for the expression, we can select out the denominator and numerator of the expression.

In[110]:= Denominator[u]

Out[110]= I-2 + x2M I-4 + x3M

In[111]:= Numerator[u]

Out[111]= 2 + x2 - x3

Expansion can be aimed only at the denominator of the expression.

In[112]:= ExpandDenominator[u]

Out[112]=2 + x2 - x3

8 - 4 x2 - 2 x3 + x5

With rational, rather than polynomial expressions, it may help to separate out the top and bottom for manipulation. In most cases you willfind Expand, Factor and Simplify sufficient. You should take care in the use of Simplify. It can sometimes get into quite a lengthyprocess, and even have unpredictable results. It is much easier to use more functions but keep control over what changes are taking place.

ü Applying Simplification Rules Selectively

The following example will give some idea of how to apply operations selectively. We begin by defining the algebraic expression expr.

2. Introduction to Mathematica MathematicaIntroduction.nb 40

40

Page 30: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[113]:= expr = (x^3 - 216) (x - 3)^2/((x - 2)(x - 4)(x - 6))

Out[113]=H-3 + xL2 I-216 + x3M

H-6 + xL H-4 + xL H-2 + xL

Applying Expand to this expression will expand each term in the numerator.

In[114]:= Expand[expr]

Out[114]= -1944

H-6 + xL H-4 + xL H-2 + xL+

1296 x

H-6 + xL H-4 + xL H-2 + xL-

216 x2

H-6 + xL H-4 + xL H-2 + xL+

9 x3

H-6 + xL H-4 + xL H-2 + xL-

6 x4

H-6 + xL H-4 + xL H-2 + xL+

x5

H-6 + xL H-4 + xL H-2 + xL

ExpandAll, however, will take every part of the expression.

In[115]:= ExpandAll[expr]

Out[115]= -1944

-48 + 44 x - 12 x2 + x3+

1296 x

-48 + 44 x - 12 x2 + x3-

216 x2

-48 + 44 x - 12 x2 + x3+

9 x3

-48 + 44 x - 12 x2 + x3-

6 x4

-48 + 44 x - 12 x2 + x3+

x5

-48 + 44 x - 12 x2 + x3

You have already used Together, which will gather all terms over a common denominator.

In[116]:= Together[%]

Out[116]=324 - 162 x + 9 x2 + x4

8 - 6 x + x2

Splitting this expression into partial fractions uses Apart.

In[117]:= Apart[%]

Out[117]= 37 +38

-4 + x-

26

-2 + x+ 6 x + x2

The Factor function will implicitly gather the terms together and factorize the resulting expression:

41 MathematicaIntroduction.nb 2. Introduction to Mathematica

41

Page 31: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[118]:= Factor[%]

Out[118]=H-3 + xL2 I36 + 6 x + x2M

H-4 + xL H-2 + xL

Another route to this simplified version of expr would have been using the function Cancel. This will make sure that any possiblecancellations are carried out.

In[119]:= Cancel[expr]

Out[119]=H-3 + xL2 I36 + 6 x + x2M

H-4 + xL H-2 + xL

ü Other Useful Functions

Another useful pair of operations are Collect and Coefficient. We shall use another polynomial expression to illustrate their use.

In[120]:= Clear[x, y];stuff = (2 x + 3)^2 (x + y)^2

Out[121]= H3 + 2 xL2 Hx + yL2

In[122]:= Expand[stuff]

Out[122]= 9 x2 + 12 x3 + 4 x4 + 18 x y + 24 x2 y + 8 x3 y + 9 y2 + 12 x y2 + 4 x2 y2

In[123]:= Collect[%, x]

Out[123]= 4 x4 + 9 y2 + x3 H12 + 8 yL + x2 I9 + 24 y + 4 y2M + x I18 y + 12 y2M

So Collect literally collects together terms with the same power of x. We can pick out the coefficients by using Coefficient. Herewe ask for the coefficient of x^2 (all the terms, including those in y) in the expression.

In[124]:= Coefficient[Expand[stuff], x^2]

Out[124]= 9 + 24 y + 4 y2

2. Introduction to Mathematica MathematicaIntroduction.nb 42

42

Page 32: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

This type of operation can be very useful in doing perturbation theory, and can also be used with the Series function which we will cometo shortly. An alternative syntax is as follows - this must be used to extract the "constant" term:

In[125]:= Coefficient[Expand[stuff], x, 2]

Out[125]= 9 + 24 y + 4 y2

In[126]:= Coefficient[Expand[stuff], x, 0]

Out[126]= 9 y2

ü Making Substitutions

Substitutions are a common requirement in manipulation. One way of doing this is to make a simple assignment and then ask for the newvalue of an expression.

In[127]:= x=3 y;

In[128]:= stuff

Out[128]= 16 y2 H3 + 6 yL2

This will mean however that from now on x will always be read as 3 y and stuff will always equal 16 y2H3 + 6 yL2. To prevent this we willhave to explicitly remove the assignment of x.

In[129]:= Clear[x]

The technique to use to make a substitution which will not make a permanent change is to use a replacement rule. These are the same as aregiven as solutions to NSolve and FindRoot. To make the same substitution define the rule that x goes to 3 y, and apply that using /. tostuff.

In[130]:= stuff /. x -> 3 y

Out[130]= 16 y2 H3 + 6 yL2

In[131]:= stuff

Out[131]= H3 + 2 xL2 Hx + yL2

43 MathematicaIntroduction.nb 2. Introduction to Mathematica

43

Page 33: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

You can see that this temporary substitution does not alter the stored definition of stuff. This technique can be used for any temporarysubstitutions whether they are numerical or algebraic.

2.6 Calculus

We now come to a topic that is fundamental to appreciating how useful Mathematica can be in modelling financial derivatives. We areinterested in finding the values of certain options, together with the derivatives of those values with respect to variables such as the underly-ing price, its volatility, interest rates, and so on. When there is some form of analytical prescription available, it is very convenient, not to sayless error-prone, to perform the computation of such "Greeks" symbolically. Even when one is working with an essentially numericalscheme, we shall still want to test it against known analytic solutions for special cases, or we may wish to differentiate a function based oninterpolated numerical values.

Differentiation

Differentiation and integration work in a straightforward way. There are various notations for differentiation, of which the D operator isperhaps the most flexible.

In[132]:= Clear[x];D[x^n, x]

Out[133]= n x-1+n

In[134]:= Integrate[%, x]

Out[134]= xn

Partial differentiation can also be carried out by stipulating the variable which you wish to differentiate by.

In[135]:= D[x^4 y^2 + x^2, y]

Out[135]= 2 x4 y

Mixed and multiple derivatives are also supported. We can differentiate once with respect to y and once with respect to x.

In[136]:= D[x^3 y^2 + x^2, y, x]

Out[136]= 6 x2 y

2. Introduction to Mathematica MathematicaIntroduction.nb 44

44

Page 34: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

Higher derivatives are specified using the list structure to supply the variable and the number of times by which to differentiate with respectto that variable.

In[137]:= D[x^3 y^2 + x^2, {x, 3}, {y, 2}]

Out[137]= 12

ü Integration - Symbolic and Numeric

Integrating with respect to y will reverse differentiation up to an arbitrary additive function of x.

In[138]:= Integrate[%%, y]

Out[138]= 3 x2 y2

Similarly multiple integration can be carried out. As in the examples for differentiation, to integrate with respect to x and y:

In[139]:= Integrate[x^2 y,x,y]

Out[139]=x3 y2

6

Definite integration needs a list supplying the upper and lower limits of the integration variable.

In[140]:= Integrate[1/(1-x),{x,2,4}]

Out[140]= -Log@3D

There is also the facility to calculate integrals numerically. This is available for any functions, including those which Mathematica cannotdo symbolically. As in the case of definite symbolic integration, we must specify the range of integration. This can frequently be used tocompute expected values of complicated payoffs, if they do not work symbolically. This next example shows how symbolic integration isnot possible, but numerical integration yields a result.

In[141]:= Integrate[Sin[Sin[z]],{z,0,2}]

Out[141]= ‡0

2Sin@Sin@zDD „z

45 MathematicaIntroduction.nb 2. Introduction to Mathematica

45

Page 35: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[142]:= NIntegrate[Sin[Sin[z]],{z,0,2}]

Out[142]= 1.24706

This is another function under which problems can occur, and you should take care. For example:

In[149]:= Integrate[Sin[1000000 x],{x,0,2}]

Out[149]=1 - Cos@2 000 000D

1 000 000

In[150]:= N[%]

Out[150]= 2.44991 µ 10-7

In[151]:= NIntegrate[Sin[1000000 x],{x,0,2}]

Out[151]= 2.44991 µ 10-7

In trickier cases you might get a warning. Older versions can be fooled by highly oscillatory integrals but it usually works now!

ü Limits

Of course, differentiation is defined by a limiting procedure. Mathematica can take limits explicitly:

In[152]:= Limit[((x + h)^3 - x^3)/h, h -> 0]

Out[152]= 3 x2

2. Introduction to Mathematica MathematicaIntroduction.nb 46

46

Page 36: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

ü Series

Mathematica also knows how to use Taylor's Theorem through the Series command. The use of Series generates a certain type ofexpression truncated by an "O" expression. These can be manipulated according to the usual rules of asymptotic series, or converted to anormal form without the "O". Note that the next power in the "O" term is not necessarily computed exactly:

In[153]:= Series[Sin[Sin[x]], {x, 0, 5}]

Out[153]= x -x3

3+x5

10+ O@xD6

In[154]:= %^2

Out[154]= x2 -2 x4

3+14 x6

45+ O@xD7

In[155]:= Normal[%%]

Out[155]= x -x3

3+x5

10

ü Simple Ordinary Differential Equations

A large part of this book is concerned with various methods for solving partial differential equations, so we shall explore that topic in somedetail later. In our introduction we shall take a brief look at ODEs.

ü Symbolic Solution of Differential Equations

Mathematica is a useful tool for dealing with certain differential equations. The function DSolve can deal with a large class of equations.It is used in a similar way to Solve, except that you need to specify both the independent and dependent variables. Notice how the equationis specified by using the double = sign.

In[156]:= DSolve[y''[x] + y[x] == 0, y[x], x]

Out[156]= 88y@xD Ø C@1D Cos@xD + C@2D Sin@xD<<

If you give appropriate initial conditions, the arbitrary constants are removed.

In[157]:= DSolve[{y'[x] + x y[x] == 0, y[0] == 1}, y[x], x]

47 MathematicaIntroduction.nb 2. Introduction to Mathematica

47

Page 37: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

Out[157]= ::y@xD Ø ‰-x2

2 >>

To check that this is indeed a solution of the given equation we use the technique of replacement. The solutions to these equations are givenas replacement rules presented in a list. We therefore need to extract a particular element of our solution list and apply that rule to get thesolution. Setting the solution to be represented by s we select the first element of the list (we only have one in this example), and then applythat rule to y[x] to see the expression which is a solution.

In[158]:= s=y[x]/.%[[1]]

Out[158]= ‰-x2

2

Now we can check if this is indeed a solution to our original equation.

In[159]:= D[s,x] + x s

Out[159]= 0

ü Numerical Solutions to Differential Equations

In a similar way to numerical integration there is a function NDSolve which will find numerical solutions to differential equations andextends the scope of the equations to which we can find solutions. This function returns an interpolating function, rather than the usual tableof values. This allows you to ask for the values of the solution anywhere in the solution range, for example. As usual, the use of "/." allowsfor the extraction of numerical values.

In[160]:= Clear[x, y, t];soln = NDSolve[ {x'[t] == y[t],y'[t] == -4.0*x[t] - 0.1*y[t],x[0] == 1,y[0] == 1},{x, y}, {t, 10}]

Out[161]= 88x Ø InterpolatingFunction@880., 10.<<, <>D, y Ø InterpolatingFunction@880., 10.<<, <>D<<

Note that the answer is supplied as an InterpolatingFunction - this allows a numerical solution computed in steps to be used justlike an ordinary function. We can select out the specific solution for any t. At t = 2,

In[162]:= {x[2], y[2]} /. soln

Out[162]= 88-0.951532, 0.793324<<

We shall use the InterpolatingFunction as a means of handling the output of finite-difference solvers for option equations. Weshall explore numerical methods for solving PDEs in some detail later in this book, starting in Chapter 13.

2. Introduction to Mathematica MathematicaIntroduction.nb 48

48

Page 38: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

ü Complex Numbers

ü Built-In Functions for Complex Manipulation

The use of complex numbers and functions is relatively unusual in derivatives modelling, but recent research has introduced Laplacetransform methods as a powerful tool for the analytical pricing of options previously thought to be unpriceable except through an essentiallynumerical scheme (e.g. arithmetically averaged Asian options). So we need to use complex numbers, and to deal with them in Mathematica.The symbol I (capital) is the square root of -1, in Standard Form. Mathematica operations work on complex numbers in just the same waythey work on real numbers. In addition, various special operations for dealing with complex variables are provided.

Let us find the solution set for the following equation.

In[163]:= qsoln = Solve[3 x^2 - 4 x + 6 == 0, x]

Out[163]= ::x Ø1

3J2 - Â 14 N>, :x Ø

1

3J2 + Â 14 N>>

We put that into an approximate numerical form,

In[164]:= N[qsoln]

Out[164]= 88x Ø 0.666667 - 1.24722 Â<, 8x Ø 0.666667 + 1.24722 Â<<

and then create the set containing the numerical solutions to the original equation:

In[165]:= solutions = x /. %

Out[165]= 80.666667 - 1.24722 Â, 0.666667 + 1.24722 Â<

As with the exponential function, a special typeface is used for I in Traditional Form notation:

In[166]:= TraditionalForm@qsolnD

Out[166]//TraditionalForm=

::xØ1

3K2 - Â 14 O>, :xØ

1

3K2 + Â 14 O>>

The function Re will return the real part of any complex number. We can apply this function to our set solutions to get the real parts ofour set.

49 MathematicaIntroduction.nb 2. Introduction to Mathematica

49

Page 39: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[167]:= Re[solutions]

Out[167]= 80.666667, 0.666667<

Similarly, the function Im will give the imaginary part of a complex number. But note that symbols are in general treated as complex - forexample:

In[168]:= z = x + I y;

In[169]:= Im[z]

Out[169]= Im@xD + Re@yD

The function Conjugate will give the complex conjugate of a complex number:

In[170]:= Conjugate[solutions]

Out[170]= 80.666667 + 1.24722 Â, 0.666667 - 1.24722 Â<

The function Abs gives the absolute value of a complex number (i.e. x2 + y2 ):

In[171]:= Abs[solutions]

Out[171]= 81.41421, 1.41421<

The function Arg will give the argument of the complex number (i.e. ArcTanHy êxL)

In[172]:= Arg[solutions]

Out[172]= 8-1.07991, 1.07991<

There is a useful ComplexExpand function, which carries out an expansion assuming that all variables are real:

2. Introduction to Mathematica MathematicaIntroduction.nb 50

50

Page 40: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[173]:= ComplexExpand[Cot[x + I y]]

Out[173]= -Sin@2 xD

Cos@2 xD - Cosh@2 yD+

 Sinh@2 yD

Cos@2 xD - Cosh@2 yD

This function is very useful for getting Mathematica to output what you expect to see when your mind assumes things are real, but thecomputer can't read your mind!

ü Complex Contour Integration

For integration in the complex plane the function NIntegrate can take complex numbers to specify the boundary around which thefunction is to be integrated.

In[174]:= NIntegrate[1/z, {z, 2, I, -2, -I, 2}]

Out[174]= 3.88578 µ 10-16 + 6.28319 Â

We shall use complex contour integration to invert certain types of Laplace transform for option values.

2.7 More about Functions

We have already seen several Mathematica functions in the course of our investigations so far. These are only a small proportion of thoseavailable. Some of the others are obvious. One example is trigonometric functions. Since you already know we can use Sin you wouldexpect Cos, Tan and all the usual inverse and hyperbolic versions. These can all be used in Mathematica in the same way as Sin, as canthe inverses of these trigonometric functions, as illustrated below.

In[175]:= Plot[ArcSin[x], {x, -1, 1}]

Out[175]=-1.0 -0.5 0.5 1.0

-1.5

-1.0

-0.5

0.5

1.0

1.5

You will have already spotted that all Mathematica's built-in functions begin with a capital letter. Names which can be thought of as com-posed of two or more pieces may have capitals in the middle. This means that when naming your own functions you can use any namestarting with lower-case letters and be sure that you are not duplicating any Mathematica built-in function. We'll come back to user-definedfunctions shortly.

51 MathematicaIntroduction.nb 2. Introduction to Mathematica

51

Page 41: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

You will have already spotted that all Mathematica's built-in functions begin with a capital letter. Names which can be thought of as com-posed of two or more pieces may have capitals in the middle. This means that when naming your own functions you can use any namestarting with lower-case letters and be sure that you are not duplicating any Mathematica built-in function. We'll come back to user-definedfunctions shortly.

ü Getting Information about Built-In Functions

As you start to explore the capabilities of Mathematica you will need to get information about the functions available. There is the facility toget information about built-in functions from within Mathematica. To ask about a function just type a "?" before the function name.

In[176]:= ?Sqrt

Sqrt@zD or z gives the square root of z.  à

The wild card "*" can also be used to obtain a list of functions, with a similar sequence in common. So we can obtain a list of all built-infunctions related to the trigonometric function Sin.

In[177]:= ?*Sin*

System`

ArcSin SingleEvaluationArcSinDistribution SingleLetterItalicsArcSinh SingleLetterStyleFourierSinCoefficient SingularValueDecompositionFourierSinSeries SingularValueListFourierSinTransform SingularValuePlotIncludeSingularTerm SingularValuesInverseFourierSinTransform SinhSin SinhIntegralSinc SinIntegralSinghMaddalaDistribution

Many of the built-in functions that you will use have options associated with them. These set default values for various variables associatedwith the execution of that function. To see how the options are set for a function, Plot for example,

2. Introduction to Mathematica MathematicaIntroduction.nb 52

52

Page 42: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[178]:= Options[Plot]

Out[178]= :AlignmentPoint Ø Center, AspectRatio Ø1

GoldenRatio, Axes Ø True, AxesLabel Ø None,

AxesOrigin Ø Automatic, AxesStyle Ø 8<, Background Ø None, BaselinePosition Ø Automatic, BaseStyle Ø 8<,ClippingStyle Ø None, ColorFunction Ø Automatic, ColorFunctionScaling Ø True, ColorOutput Ø Automatic,ContentSelectable Ø Automatic, CoordinatesToolOptions Ø Automatic, DisplayFunction ß $DisplayFunction,Epilog Ø 8<, Evaluated Ø Automatic, EvaluationMonitor Ø None, Exclusions Ø Automatic,ExclusionsStyle Ø None, Filling Ø None, FillingStyle Ø Automatic, FormatType ß TraditionalForm,Frame Ø False, FrameLabel Ø None, FrameStyle Ø 8<, FrameTicks Ø Automatic, FrameTicksStyle Ø 8<,GridLines Ø None, GridLinesStyle Ø 8<, ImageMargins Ø 0., ImagePadding Ø All, ImageSize Ø Automatic,ImageSizeRaw Ø Automatic, LabelStyle Ø 8<, MaxRecursion Ø Automatic, Mesh Ø None, MeshFunctions Ø 8Ò1 &<,MeshShading Ø None, MeshStyle Ø Automatic, Method Ø Automatic, PerformanceGoal ß $PerformanceGoal,PlotLabel Ø None, PlotPoints Ø Automatic, PlotRange Ø 8Full, Automatic<,PlotRangeClipping Ø True, PlotRangePadding Ø Automatic, PlotRegion Ø Automatic,PlotStyle Ø Automatic, PreserveImageOptions Ø Automatic, Prolog Ø 8<, RegionFunction Ø HTrue &L,

RotateLabel Ø True, Ticks Ø Automatic, TicksStyle Ø 8<, WorkingPrecision Ø MachinePrecision>

Changing any of these options can be done when executing the function by explicitly stating the desired option value as the final argument tothe function. For example, we can change the label points on the x-axis for a sine curve plot, and add suitable grid lines.

In[179]:= Plot[Sin[x],{x,0,4 Pi},Ticks -> {{0, Pi, 2Pi, 3Pi, 4Pi}, Automatic},GridLines -> {Table[k*Pi, {k, 0, 4, 1/2}] ,None} ]

Out[179]=p 2 p 3 p 4 p

-1.0

-0.5

0.5

1.0

Note that Mathematica allows a slight abuse of notation when it comes to pre-multiplying a symbol by a number. In the above, 2Pi isinterpreted as twice p, even in the absence of a star or space. Options may also be used to refine any problems with functions such asNIntegrate:

In[180]:= Options@NIntegrateD

Out[180]= 8AccuracyGoal Ø ¶, Compiled Ø Automatic, EvaluationMonitor Ø None,Exclusions Ø None, MaxPoints Ø Automatic, MaxRecursion Ø Automatic, Method Ø Automatic,MinRecursion Ø 0, PrecisionGoal Ø Automatic, WorkingPrecision Ø MachinePrecision<

It is especially important that you understand the Assumptions option to Integrate. The following example should help you appreci-ate that Mathematica will actually analyze a symbolic integration containing parameters to figure out when the integral makes sense. Youmight well have some assumptions of your own that are actually your internal presumptions - Mathematica cannot read your mind and couldwell considerably more time figuring out when the integral makes sense than it does working it out.

53 MathematicaIntroduction.nb 2. Introduction to Mathematica

53

Page 43: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

It is especially important that you understand the Assumptions option to Integrate. The following example should help you appreci-ate that Mathematica will actually analyze a symbolic integration containing parameters to figure out when the integral makes sense. Youmight well have some assumptions of your own that are actually your internal presumptions - Mathematica cannot read your mind and couldwell considerably more time figuring out when the integral makes sense than it does working it out.

In[181]:= Integrate@Exp@-a xD, 8x, 0, Infinity<D

Out[181]= ConditionalExpressionB1

a, Re@aD > 0F

In[182]:= Integrate@Exp@-a xD, 8x, 0, Infinity<, Assumptions Ø a > 0D

Out[182]=1

a

In[183]:= Integrate@Exp@-a xD Sin@b xD, 8x, 0, Infinity<D

Out[183]= ConditionalExpressionBb

a2 + b2, Abs@Im@bDD < Re@aDF

In[184]:= FullForm@%D

Out[184]//FullForm=ConditionalExpression@Times@b, Power@Plus@Power@a, 2D, Power@b, 2DD, -1DD, Less@Abs@Im@bDD, Re@aDDD

In[185]:= Integrate@Exp@-a xD Sin@b xD, 8x, 0, Infinity<, Assumptions Ø 8a > 0, b > 0<D

Out[185]=b

a2 + b2

Here is the example of the Call payoff from the Heston model:

In[187]:= Integrate@HExp@xD - KL Exp@I w xD, 8x, K, Infinity<D

Out[187]= ConditionalExpressionB- ‰Â K w I-‰K w + K H- + wLM

w H-Â + wL, Im@wD > 1F

In[189]:= Integrate@HK - Exp@xDL Exp@I w xD, 8x, -Infinity, K<D

Out[189]= ConditionalExpressionB- ‰Â K w I-‰K w + K H- + wLM

w H-Â + wL, Im@wD < 0F

2. Introduction to Mathematica MathematicaIntroduction.nb 54

54

Page 44: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

Note how you get the correct restrictions on the domain on which the transform is defined!

ü Built-In Special Functions

ü Examples of Special Functions

Mathematica also knows a phenomenal number of special functions. If you do the kind of work that employs handbooks of functions andtheir values and properties, you may be surprised by the breadth of Mathematica's built-in capabilities. As well as a host of number-theoreticand combinatorial functions, there are many "classical" functions. Let us take a look at a few examples. First of all, we can use Besselfunctions:

In[190]:= Plot[{BesselJ[0, x], BesselI[1, x]}, {x, 0, 10},PlotRange -> {-5, 5}, PlotStyle -> {Thickness[0.015], Thickness[0.01]}]

Out[190]=2 4 6 8 10

-4

-2

2

4

There is a selection of orthogonal polynomials:

In[191]:= LegendreP[5, x]

Out[191]=1

8I15 x - 70 x3 + 63 x5M

In[192]:= SphericalHarmonicY[1, 1, theta, phi]

Out[192]= -1

2‰Â phi

3

2 pSin@thetaD

This is probably a good place to remark on how Greek letters are input - previously we have used p without explaining how we wrote it, andour spherical harmonic function looks rather better written as:

55 MathematicaIntroduction.nb 2. Introduction to Mathematica

55

Page 45: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[193]:= TraditionalForm[SphericalHarmonicY[1, 1, q, f]]

Out[193]//TraditionalForm=

-1

2

3

2 p‰Â f sinHqL

The trick is to write "ESC\thetaESC" where ESC stands for the escape key. Occurrences of p are generated with "ESC\piESC".

ü Naming of Special Functions

This should give you a feel for the way in which these functions are named. Many special functions can be classified under an overall name,like Legendre. Within this there may be more than one type, like the P and Q Legendre functions. And finally, these may be indexed by aninteger or some other variable. Hence the Bessel function KnHxL is denoted by

In[194]:= BesselK[n, x]

Out[194]= BesselK@n, xD

Most of these functions are in the core kernel. However, the statistical distributions are accessed from one of the Packages. We shall look atPackages later. Note that Mathematica knows some particular values of special functions, and also knows series:

In[195]:= Gamma[1/2]

Out[195]= p

In[196]:= Series[BesselJ[0, x], {x, 0, 6}]

Out[196]= 1 -x2

4+x4

64-

x6

2304+ O@xD7

ü Building Your Own Functions

ü Defining a Simple Function

When defining a function by using an explicit formula, it is important to specify the variables involved in that function by use of the under-score "_". The delayed assignment sign ":=" then allows you to define the formula of your function.

In[197]:= h[x_, y_] := x^2 / y^3

2. Introduction to Mathematica MathematicaIntroduction.nb 56

56

Page 46: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

This has defined the function h. We can now ask for that function to be evaluated for specific values of x and y.

In[198]:= h[1, 2]

Out[198]=1

8

You can now ask about your function:

In[199]:= ?h

Global`h

h@x_, y_D := x2

y3

If you wish to clear that definition, you use Clear.

In[200]:= Clear[h]

Your definition for h no longer exists.

In[201]:= ?h

Global`h

There are many types of function which can be defined, in the same way as the algebraic one we have just defined. Functions can employany of the Mathematica built-in functions;

In[202]:= funcSin[x_,y_] := Sin[x] + Cos[y]^2

In[203]:= funcSq[x_, y_, z_] := Sqrt[x^2 + y^2 + z^2]

Functions can also be defined which will perform a test on the argument(s) returning the result True or False. testDiv5 will checkwhether the argument is divisible by 5:

57 MathematicaIntroduction.nb 2. Introduction to Mathematica

57

Page 47: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[204]:= testDiv5[t_] := IntegerQ[t/5]

In[205]:= testDiv5[8]

Out[205]= False

In[206]:= testDiv5[15]

Out[206]= True

ü Defining Functions in Their Pure Form

If you only intend to use the function once, or to use it with operations such as Map and Nest, there is a more economical way to define it.Instead of using a letter or name for the arguments in the function, the syntax for "pure" functions uses the "#" to indicate the arguments. Sothe expression which defines the action of a function to add 3 to a number is # + 3 instead of x + 3. To actually turn this into a functionwe use an "&" after the expression of the function. So

In[207]:= f[x_] := x + 3

can be written as

In[208]:= (# + 3) &

Out[208]= Ò1 + 3 &

without using the definition of f. So if we apply that function to 2,

In[209]:= (# + 3) & [2]

Out[209]= 5

This technique can also be used instead of defining functions of more than one variable. Using the # notation, we now label each of thearguments with a number immediately (i.e. no space) after the #. So instead of

In[210]:= g[x_, y_] := x^2 + y

In[211]:= g[2,3]

2. Introduction to Mathematica MathematicaIntroduction.nb 58

58

Page 48: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

Out[211]= 7

we use

In[212]:= (#1^2 + #2) & [2,3]

Out[212]= 7

Notice how the numbers used for labelling directly correspond to the order in which those arguments will be given. The advantage of thistechnique for defining functions which will only be used once is that the definitions do not need to be stored, they are just used at theappropriate moment.

ü Using Pure Functions in Simple Programs

Programs can be shortened in length by using pure functions instead of separately defining a general global function which can be used inthose functions which specifically ask for function definitions.

In[213]:= NestList[#^2 &, x, 5]

Out[213]= 9x, x2, x4, x8, x16, x32=

In[214]:= Fold[Sqrt[#1 + #2] &, t, {l, m, n}]

Out[214]= n + m + l + t

In[215]:= Clear@p, q, r, sD

In[216]:= NestList[Append[Rest[#],1]&, {p,q,r,s}, 3]

Out[216]= 88p, q, r, s<, 8q, r, s, 1<, 8r, s, 1, 1<, 8s, 1, 1, 1<<

This form of writing functions can also be used for defining functions which will perform a test. As an example we can define a functionwhich will return True if a number is divisible by 3, and use this as the criterion in the function Select.

59 MathematicaIntroduction.nb 2. Introduction to Mathematica

59

Page 49: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[217]:= Select[ {1, 2, 3, 4, 5, 6, 7, 8, 9}, IntegerQ[(#/3)] &]

Out[217]= 83, 6, 9<

A function which will return True if the second argument is less than the first can be used as the test by which a list can be sorted.

In[218]:= Sort[ {8, 34, 9, 2, 7, 4, 75, 4}, (#2 < #1) &]

Out[218]= 875, 34, 9, 8, 7, 4, 4, 2<

This technique can be particularly useful when manipulating lists.

ü Defining a Function Recursively

There may be functions which you wish to define recursively giving values for the end points. This can be done in the following way.

In[219]:= Clear[f]

In[220]:= f[1] = 1;

In[221]:= f[2] = 1;

In[222]:= f[n_] := f[n-1] + f[n-2]

We can now evaluate f[3].

In[223]:= f[3]

Out[223]= 2

There is a useful modification of this function which makes it much more efficient, that forces the function to remember previously com-puted values.

In[224]:= Clear[g]

In[225]:= g[1] = 1;

2. Introduction to Mathematica MathematicaIntroduction.nb 60

60

Page 50: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[226]:= g[2] = 1;

In[227]:= g[n_] := g[n] =g[n-1] + g[n-2]

The following illustration makes it clear that the use of the second function for computing the Fibonacci series is strongly preferred:

In[228]:= Timing@f@35DD

Out[228]= 821.2905, 9 227 465<

Timing@g@35DD

80.000428, 9 227 465<

Such a device will turn out to be useful in a simple implementation of the binomial tree model, where we recurse back through a tree fromthe payoff. Note that here and throughout this text, the timings reported are based on the computer used for editing. You must makeallowances for the clock speed and efficiency of your own system when making comparisons or assessments about what computations arefeasible.

ü The Difference between := and =

Constructing functions is dependent on the use of ":=". The use of the colon with equals means that the evaluation of the right side of theequation is delayed, and it will only be evaluated when that function is actually called. As an example let us define some functions whichwill be dependent on the value of a variable that we call M and to which we assign a value of 2 initially:

In[229]:= M=2

Out[229]= 2

Now if we define funcOne, using :=, the right-hand side will not be evaluated yet.

In[230]:= funcOne[x_] := M x

Defining the function funcTwo using = will mean that the right-hand side is evaluated using the present value of M.

61 MathematicaIntroduction.nb 2. Introduction to Mathematica

61

Page 51: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[231]:= funcTwo[x_] = M x

Out[231]= 2 x

With the present definitions both funcOne[2] and funcTwo[2] will evaluate to 4.

In[232]:= {funcOne[2],funcTwo[2]}

Out[232]= 84, 4<

If we were to change the value of M however, the definition of funcOne would: allow the new value to be used, whereas the definition offuncTwo has already been set by the original definition of M:

In[233]:= M=100

Out[233]= 100

In[234]:= {funcOne[2],funcTwo[2]}

Out[234]= 8200, 4<

ü Assignment and Random Walks

Another way to see the difference between = and := is to define two functions from which we can produce random walks and plot thoseresultant functions. Firstly let us define two functions which will add a random number between -0.5 and +0.5 to their argument:

In[235]:= addrandONE@x_D := x + [email protected], 0.5<D

In[236]:= addrandTWO@x_D = x + [email protected], 0.5<D

Out[236]= -0.437868 + x

So if we ask for each of these functions evaluated twice on the same value,

2. Introduction to Mathematica MathematicaIntroduction.nb 62

62

Page 52: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[237]:= {addrandONE[0],addrandONE[0]}

Out[237]= 80.0205625, -0.493336<

In[238]:= {addrandTWO[0],addrandTWO[0]}

Out[238]= 8-0.437868, -0.437868<

The second function does not act as a random function. Now we can form random walks based on these functions using the functionNestList,

In[239]:= walkONE = NestList[addrandONE,0,1000];

In[240]:= walkTWO = NestList[addrandTWO,0,1000];

and now we can display these lists using ListPlot and asking that the points should be joined.

In[241]:= ListPlot@walkONE, Joined Ø TrueD

Out[241]=200 400 600 800 1000

-6

-4

-2

2

4

63 MathematicaIntroduction.nb 2. Introduction to Mathematica

63

Page 53: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[242]:= ListPlot@walkTWO, Joined Ø TrueD

Out[242]=

200 400 600 800 1000

-400

-300

-200

-100

From this it is obvious what has happened in the function addrandTWO. The function has added a constant number to the argument ofaddrandTWO, that number being the first random number which was generated when the function was defined. The functionaddrandTWO was displayed with the Random function replaced by a number when the function definition was entered and from that pointon the function definition remained as such.

Note the ease with which random walks may be constructed - we shall use this facility when carrying out Monte Carlo simulation of pathsfollowed by stock prices.

2. Introduction to Mathematica MathematicaIntroduction.nb 64

64

Page 54: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

ü Functions of Two or More Variables

The basic functions to know about are Plot3D, ContourPlot, DensityPlot and their List analogues. The forms of the commandare very similar to those for Plot etc. A few examples will suffice. Let's make a function to use in our examples.

In[243]:= func@x_, y_D := Sin@x^2D Cos@yD;Plot3D@func@x, yD, 8x, -2, 2<, 8y, -2, 2<, PlotPoints -> 40D

Out[244]=

In[245]:= ContourPlot@func@x, yD, 8x, -2, 2<, 8y, -2, 2<, PlotPoints -> 40D

Out[245]=

In[247]:= DensityPlot@func@x, yD, 8x, -2, 2<, 8y, -2, 2<, PlotPoints -> 60, Mesh -> FalseD

65 MathematicaIntroduction.nb 2. Introduction to Mathematica

65

Page 55: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

Out[247]=

In the same way as for two-dimensional curves, the functions for three-dimensional surfaces also have options associated with them whichallow the alteration of graphics to suit your needs.

In[248]:= Plot3D@func@x, yD, 8x, -2, 2<, 8y, -2, 2<,ViewPoint -> 80, 1, 0.3<,Axes -> None, Boxed -> False, PlotPoints -> 20D

Out[248]=

2. Introduction to Mathematica MathematicaIntroduction.nb 66

66

Page 56: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[249]:= ContourPlot@func@x, yD, 8x, -2, 2<, 8y, -2, 2<,Contours Ø 40, ContourStyle Ø None, ColorFunction Ø Hue, Frame Ø FalseD

Out[249]=

ü Animation

There is also the facility in Mathematica to be able to animate graphics which you have produced. This is done by creating all of the cellswhich will make up your movie and then asking to see them displayed one after the other. As an example we can see how a three-dimen-sional surface alters as we step x and y over slightly larger values. Only the first frame is shown in the book. Such techniques can be used topresent plots of option values and their Greeks as functions of other variables, using the animation to represent the evolution through time, orperhaps to vary the angle of view, in order to bring out particular features.

67 MathematicaIntroduction.nb 2. Introduction to Mathematica

67

Page 57: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[250]:= Manipulate[Plot3D[Sin[x+t] Cos[y+t], {x, -Pi, Pi}, {y, -Pi, Pi}],{t,0,2*Pi-Pi/10,Pi/10}]

Out[250]=

t

Data analysis and distributional estimation

ü FTSE

In[251]:= series = "^FTSE"

Out[251]= ^FTSE

In[252]:= FinancialData@series, "Name"D

Out[252]= FTSE 100

In[253]:= ftserets = FinancialData@series, "Return", AllD;logftrets = Map@Log@1 + Ò@@2DDD &, ftseretsD;

In[255]:= First@ftseretsD

Out[255]= 881984, 4, 3<, -0.0114611<

2. Introduction to Mathematica MathematicaIntroduction.nb 68

68

Page 58: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[256]:= Last@ftseretsD

Out[256]= 882011, 11, 28<, 0.0286953<

In[257]:= Skewness@logftretsD

Out[257]= -0.382038

In[258]:= Kurtosis@logftretsD

Out[258]= 11.3539

In[259]:= gaussestimate = EstimatedDistribution@logftrets, NormalDistribution@a, bDD

Out[259]= [email protected], 0.0113252D

In[260]:= gausslike = Likelihood@gaussestimate, logftretsD

Out[260]= 5.4921664802 µ 109290

In[261]:= testimate = EstimatedDistribution@logftrets, StudentTDistribution@a, b, nDD

Out[261]= [email protected], 0.0078555, 3.83234D

In[262]:= tlike = Likelihood@testimate, logftretsD

Out[262]= 9.8052883929 µ 109566

In[263]:= Log@10, tlike ê gausslikeD

Out[263]= 276.25171667842

In[264]:= jsu = EstimatedDistribution@logftrets, JohnsonDistribution@"SU", g, d, m, sDD

Out[264]= JohnsonDistribution@SU, 0.136675, 1.35736, 0.00169118, 0.0111673D

69 MathematicaIntroduction.nb 2. Introduction to Mathematica

69

Page 59: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[265]:= jsulike = Likelihood@jsu, logftretsD

Out[265]= 1.00144791820 µ 109568

In[266]:= Log@10, jsulike ê gausslikeD

Out[266]= 277.26088467485

See Fergusson and Platen, Applied Mathematical Finance, 2006.

Parallel Programming GridMathematica

ü Introduction

This note gives an introduction to parallel financial computation using Mathematica V8. The standard version will support 4 kernels if yourmachine has at least 4 cores. In this lecture I will be working off an 8-core box that is also networked.

Although this lecture is targetted at Mathematica, some of the ideas are relevant much more generally and I will flag these as I go along.

There are ways of configuring parallel kernel configuration and start-up that is more automatic than what I will do. I have stayed rathermanual in my own implementation. Once all the needed kernels are up the computational methods will be universal and not dependent onconfiguration issues.

My own small grid is also pretty homogenous - all the machines are 8-core Macs running Leopard or Snow Leopard and Mathematica 7.01,clocked at 2.8GHz. You can work much more heterogeneously, with different OS and clock speeds. It is generally a good idea to use thesame version of the software, so as to avoid code incompatibility. There are load adjustment tools if you have machines with wildly varyingperformance.

ü Local Parallel Start-up

You can trigger this by asking for a parallel computation, but I like to launch my kernels and see what I have to play with before doingcalcutions. First of all I will just do a launch of kernels on my own machine (bear in mind I wrote these notes on both a home 8-way machineand revised them on the KCL cluster, so do not worry about variations in the names of machines or Mathematica version/date).

In[271]:= Quit@D

In[2]:= Kernels@D

Out[2]= 8KernelObject@1, localD, KernelObject@2, localD, KernelObject@3, localD, KernelObject@4, localD,KernelObject@5, localD, KernelObject@6, localD, KernelObject@7, localD, KernelObject@8, localD<

2. Introduction to Mathematica MathematicaIntroduction.nb 70

70

Page 60: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

If these have not started automatically you do this:

In[1]:= LaunchKernels@D

If you go to the parallel kernel status menu you can see something like this:

This reveals that I am running a master kernels and 8 local "slave" kernels. I can do work on either the master or slaves now. There are a pileof basic functions to do with kernels and parallel commands that you can explore.

In[3]:= ? *Kernel*

System`

AbortKernels LaunchKernelsAllowKernelInitialization MaxMixtureKernelsCloseKernels SendFontInformationToKernelKernelMixtureDistribution SmoothKernelDistributionKernelObject $ConfiguredKernelsKernels $KernelCountKernelSpeed $KernelID

71 MathematicaIntroduction.nb 2. Introduction to Mathematica

71

Page 61: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[4]:= ? *Parallel*

System`

ParallelArray ParallelNeedsParallelCombine ParallelProductParallelDo ParallelSubmitParallelEvaluate ParallelSumParallelization ParallelTableParallelize ParallelTryParallelMap SystemsModelParallelConnect

Parallel`Debug`

$Parallel

For example, I can ask from the notebook some information about my slave kernels.

In[5]:= TableForm@ParallelEvaluate@8$MachineName, $SystemID, $Version, TimeUsed@D, MaxMemoryUsed@D<D,TableHeadings -> 8None, 8"host", "OS", "Mathematica Version", "CPU Time", "Memory"<<D

Out[5]//TableForm=host OS Mathematica Version CPU Timeunknown-00-1d-4f-49-af-0c MacOSX-x86-64 8.0 for Mac OS X x86 H64-bitL HFebruary 23, 2011L 0.251883unknown-00-1d-4f-49-af-0c MacOSX-x86-64 8.0 for Mac OS X x86 H64-bitL HFebruary 23, 2011L 0.246187unknown-00-1d-4f-49-af-0c MacOSX-x86-64 8.0 for Mac OS X x86 H64-bitL HFebruary 23, 2011L 0.247523unknown-00-1d-4f-49-af-0c MacOSX-x86-64 8.0 for Mac OS X x86 H64-bitL HFebruary 23, 2011L 0.245331unknown-00-1d-4f-49-af-0c MacOSX-x86-64 8.0 for Mac OS X x86 H64-bitL HFebruary 23, 2011L 0.248193unknown-00-1d-4f-49-af-0c MacOSX-x86-64 8.0 for Mac OS X x86 H64-bitL HFebruary 23, 2011L 0.246201unknown-00-1d-4f-49-af-0c MacOSX-x86-64 8.0 for Mac OS X x86 H64-bitL HFebruary 23, 2011L 0.245934unknown-00-1d-4f-49-af-0c MacOSX-x86-64 8.0 for Mac OS X x86 H64-bitL HFebruary 23, 2011L 0.24579

Key Ideas

You can consider various types of calculation for parellelization. Clearly any computation that can be broken down into many identical orsimilar tasks can benefit in principle. What you do need to watch out for is the balance between doing the task and communication theresults back to the master kernel. A demanding task requiring a short result to be sent back is clearly most efficient. You will get muchbenefit by sending huge lists around the system - this will apply to any type of software. People often spend large amounts of money toincrease the inter-node bandwidth. Here I have 1G ethernet between machines, but you can even bog down on-chip communication by beingcareless with data transfer.

ü ParallelTable

If you can write what you want to do as Table[something] then there is an easy route to parallelization - ParallelTable!

2. Introduction to Mathematica MathematicaIntroduction.nb 72

72

Page 62: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[6]:= AbsoluteTiming@Table@Integrate@Sin@xD ê Sqrt@xD, 8x, i Pi, Infinity<D, 8i, 0, 63<D;D

Out[6]= 812.343941, Null<

In[7]:= AbsoluteTiming@ParallelTable@Integrate@Sin@xD ê Sqrt@xD, 8x, i Pi, Infinity<D, 8i, 0, 63<D;D

Out[7]= 82.214739, Null<

We have factor of six speedup right away. I have suppressed the actual results in the above - you can delete the semi-colon if you wish to seethe output. But you must not do this blindly with huge piles of data. Here is my favourite screw-up! The right way to make lots of randomnumbers is to use RandomReal with its second argument applied to make lots.

In[8]:= AbsoluteTiming@data = RandomReal@80, 1<, 1 000 000D;D

Out[8]= 80.023197, Null<

This is a lot faster than using Table:

In[9]:= AbsoluteTiming@data = Table@RandomReal@80, 1<D, 81 000 000<D;D

Out[9]= 80.096600, Null<

And the following speaks for itself.

In[10]:= AbsoluteTiming@data = ParallelTable@RandomReal@80, 1<D, 81 000 000<D;D

Out[10]= 80.398699, Null<

And this does not help that much

In[11]:= AbsoluteTiming@data = ParallelTable@RandomReal@80, 1<, 25 000D, 840<D;D

Out[11]= 80.118708, Null<

The lesson is to simulate what you want and keep the lengthy stuff there, working on it remotely and only sending back what you reallyneed. Let's do a basic expectation summing up locally and then returning that one piece of information.

73 MathematicaIntroduction.nb 2. Introduction to Mathematica

73

Page 63: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[12]:= AbsoluteTiming@Apply@Plus, ParallelEvaluate@Apply@Plus, RandomReal@80, 1<, 25 000DDDD ê 1 000 000D

Out[12]= 80.025164, 0.0999853<

The real benefit comes when you do lots of computation on each slave and send back a simple short result. This is the approach we shall usefrom now on.

ü Monte Carlo Parallel foul-up

Another thing NOT to do is to be silly about seeding your Monte Carlo simulations. GridMathematica is set up carefully in such a way thateach slave kernel is in a different random seed state.

In[13]:= ParallelTable@RandomReal@D, 88<D

Out[13]= 80.375267, 0.494178, 0.878917, 0.609701, 0.382528, 0.00209608, 0.0569686, 0.324117<

Here is how to foul it all up by careless seeding:

In[14]:= ParallelEvaluate@SeedRandom@100DD;

In[15]:= ParallelTable@RandomReal@D, 88<D

Out[15]= 80.0128701, 0.0128701, 0.0128701, 0.0128701, 0.0128701, 0.0128701, 0.0128701, 0.0128701<

You might well wish to seed each slave in a certain way (e.g. to get reproducible random numbers), but you should do it another way. I tendto feed off the kernel ID, maybe with an integer multiplier.

In[16]:= ParallelEvaluate@SeedRandom@100 * $KernelIDDD;

In[17]:= ParallelTable@RandomReal@D, 88<D

Out[17]= 80.749696, 0.660871, 0.198367, 0.812479, 0.0739547, 0.531837, 0.736668, 0.0128701<

Example: Bivariate Exponential Distribution and the DistributeDefinitions command

So what is the BV exponential distribution anyway? Something I have been exploring with my student Asad Munir is the generation ofnatural multivariate distributions using equilibria of coupled SDEs, each of whose marginals has as an equilibrium a certain target distribu-tion. The maths of this is in our paper that has just appeared:

W.T. Shaw & A. Munir, Dependency without Copulas or Ellipticity, European Journal of Finance, Vol 15, Issues 7&8, October 2009, pp661-674.

2. Introduction to Mathematica MathematicaIntroduction.nb 74

74

Page 64: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

So what is the BV exponential distribution anyway? Something I have been exploring with my student Asad Munir is the generation ofnatural multivariate distributions using equilibria of coupled SDEs, each of whose marginals has as an equilibrium a certain target distribu-tion. The maths of this is in our paper that has just appeared:

W.T. Shaw & A. Munir, Dependency without Copulas or Ellipticity, European Journal of Finance, Vol 15, Issues 7&8, October 2009, pp661-674.

dXt =s2

2dt H1 - l XtL + s dWt

dYt =s2

2dt H1 - l YtL + s dZt

and the two Brownian motions are correlated. We get a natural bivariate exponential distribution in equilibrium.

In[19]:= BiExpPathGen@8x0_, y0_<, sig_, rho_, l_, t_, ts_D :=Module@8srt = sig * Sqrt@t ê tsD, nlista = RandomReal@NormalDistribution@0, 1D, 8ts<D,

nlistb = RandomReal@NormalDistribution@0, 1D, 8ts<D, corrlist, pairlist<,corrlist = rho * nlista + Sqrt@1 - rho^2D * nlistb;pairlist = Transpose@8nlista, corrlist<D;Fold@Abs@Ò1 + 1 ê 2 srt^2 H1 - l Ò1L + srt * Sqrt@Ò1D Ò2D &, 8x0, y0<, pairlistDD

So that is fine, but the slave kernels do not know the definition - only the master kernel does. We fix this as follows:

In[20]:= DistributeDefinitions@BiExpPathGenD

Out[20]= 8BiExpPathGen<

In[21]:= AbsoluteTiming@bidata = Table@BiExpPathGen@81, 1<, 0.4, 0.5, 0.5, 200, 3200D, 8k, 1, 4000<D;D

Out[21]= 822.154923, Null<

In[22]:= AbsoluteTiming@bidata = ParallelTable@BiExpPathGen@81, 1<, 0.4, 0.5, 0.5, 200, 3200D, 8k, 1, 4000<D;D

Out[22]= 83.017867, Null<

In[23]:= %% ê %

Out[23]= 87.341252, 1<

So here is the efficiency of parallelization for this case:

75 MathematicaIntroduction.nb 2. Introduction to Mathematica

75

Page 65: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[24]:= 100 * %@@1DD ê 8

Out[24]= 91.7657

Here is a nice bivariate exponential distribution as well!

In[25]:= ListPlot@bidata, PlotRange Ø 88-1, 10<, 8-1, 10<<, AspectRatio Ø 1, PlotStyle Ø AbsolutePointSize@2DD

Out[25]=

2 4 6 8 10

2

4

6

8

10

While we are on this topic, note that by varying the SDEs you can go heterogeneous on the choice of marginal distirbutions with thistechnique.

Bivariate Exotic: "exponential-normal" distribution!

dXt =s2

2dt H1 - l XtL + s dWt

dYt = -s2

2dt Yt + s dZt

The equilibrium is exponential in X and Gaussian in Y, and they are correlated:

2. Introduction to Mathematica MathematicaIntroduction.nb 76

76

Page 66: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[26]:= ExpNorPathGen@8x0_, y0_<, sig_, rho_, l_, t_, ts_D :=Module@8srt = sig * Sqrt@t ê tsD, nlista = RandomReal@NormalDistribution@0, 1D, 8ts<D,

nlistb = RandomReal@NormalDistribution@0, 1D, 8ts<D, corrlist, pairlist<,corrlist = rho * nlista + Sqrt@1 - rho^2D * nlistb;pairlist = Transpose@8nlista, corrlist<D;Fold@8Abs@Ò1@@1DD + 1 ê 2 srt^2 H1 - l Ò1@@1DDL + srt * Sqrt@Ò1@@1DDD Ò2@@1DDD,

Ò1@@2DD - 1 ê 2 srt^2 Ò1@@2DD + srt * Ò2@@2DD< &, 8x0, y0<, pairlistDD

In[27]:= ExpNorPathGen@81, 0<, 0.4, 0.5, 0.5, 1, 10D

Out[27]= 81.75272, 0.424628<

In[28]:= DistributeDefinitions@ExpNorPathGenD

Out[28]= 8ExpNorPathGen<

In[29]:= bidata = ParallelTable@ExpNorPathGen@81, 1<, 0.4, 0.5, 0.5, 200, 3200D, 8k, 1, 4000<D;

In[30]:= ListPlot@Map@Reverse, bidataD, PlotRange Ø 8 8-4, 4<, 80, 10<<,AspectRatio Ø 5 ê 4, PlotStyle Ø AbsolutePointSize@2DD

Out[30]=

-4 -2 0 2 4

2

4

6

8

10

This is a naturally-generated bivariate exponential-normal distribution!

77 MathematicaIntroduction.nb 2. Introduction to Mathematica

77

Page 67: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[31]:= Correlation@bidataD

Out[31]= 881., 0.406267<, 80.406267, 1.<<

In[32]:= Needs@"MultivariateStatistics`"D

In[33]:= ? KendallRankCorrelation

KendallRankCorrelation @ xlist , ylist D gives Kendall's rank correlation coefficient t for the real-valued vectors xlist and ylist . à

In[34]:= SpearmanRankCorrelation@Transpose@bidataD@@1DD, Transpose@bidataD@@2DDD êê N

Out[34]= 0.449747

In[35]:= KendallRankCorrelation@Transpose@bidataD@@1DD, Transpose@bidataD@@2DDD êê N

Out[35]= 0.309678

We know how to do this with mixtures of normal, exp, gamma, Student and so on. See "Dependency without copulas".

Other Parallel*** Commands

Once you have seen ParallelTable in operation the operation of some of the other commands becomes pretty much self-evident:

In[36]:= ? ParallelSum

ParallelSum@expr, 8i, imax<D evaluates in parallel the sum ‚

i=1

imaxexpr .

ParallelSum@expr, 8i, imin, imax<D starts with i = imin.ParallelSum@expr, 8i, imin, imax, di<D uses steps di.ParallelSum@expr, 8i, 8i1, i2,…<<D uses successive values i1, i2, ….

ParallelSum@expr, 8i, imin, imax<, 8 j, jmin, jmax<,…D evaluates in parallel the multiple sum ‚i=imin

imax

‚j= jmin

jmax

…expr . à

2. Introduction to Mathematica MathematicaIntroduction.nb 78

78

Page 68: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

More about ParallelMap - an example doing many implied vols

This one I find particularly useful. If you can run your computer model essentially in the form

Map@model, lotsofdataD

then provided you have told the slaves what "model" is then you an try instead

ParallelMap@model, lotsofdataD

Here is a classic example working out many implied volatilities. We first define the BS model in the usual way:

In[37]:= Ncdf@x_D := H1 + Erf@x ê Sqrt@2DDL ê 2

The analytical valuation of the vanilla Put is then given by the following definitions

In[38]:= done@s_, s_, k_, t_, r_, q_D := HHr - qL * t + Log@s ê kDL ê Hs * Sqrt@tDL + Hs * Sqrt@tDL ê 2;dtwo@s_, s_, k_, t_, r_, q_D := HHr - qL * t + Log@s ê kDL ê Hs * Sqrt@tDL - Hs * Sqrt@tDL ê 2;

In[40]:= BlackScholesPut@s_, k_, s_, r_, q_, t_D :=k * Exp@-r * tD * Ncdf@-dtwo@s, s, k, t, r, qDD - s * Exp@-q * tD * Ncdf@-done@s, s, k, t, r, qDD

Now I am going to make up a lot of random option data (normally this would come from markets)

In[41]:= SeedRandom@200D

In[42]:= randomoptionparameters = Table@[email protected], 30<D,RandomReal@830, 200<D, RandomReal@880, 120<D, 0.045, 0, RandomReal@81, 10<D<, 82000<D;

Some of these might, being random, be pathological with regard to an IV compuation, so I will suppress some complaints here, just for thedemo - one would normally trap and report these.

In[43]:= Off@FindRoot::lstolD; Off@FindRoot::jsingD;ParallelEvaluate@Off@FindRoot::lstolD; Off@FindRoot::jsingDD;

In[45]:= test = randomoptionparameters@@1DD

Out[45]= 822.1003, 80.1958, 103.968, 0.045, 0, 1.70541<

79 MathematicaIntroduction.nb 2. Introduction to Mathematica

79

Page 69: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

So here is the usual IV computation using FindRoot (which to remind you is a Newton-Raphson solver if you feed it one starting value):

In[46]:= FindRoot@test@@1DD == BlackScholesPut@test@@2DD, test@@3DD, sigma, 0.045, 0, test@@6DDD, 8sigma, 0.1<D

Out[46]= 8sigma Ø 0.272633<

In[47]:= ta = AbsoluteTiming@valsa = Map@sigma ê. FindRoot@Ò@@1DD == BlackScholesPut@Ò@@2DD, Ò@@3DD, sigma, 0.045, 0, Ò@@6DDD,

8sigma, 0.1<D &, randomoptionparametersD;D

Out[47]= 82.551596, Null<

ü Parallelization

Important - you just export the definitions to the grid environment and change Map to ParallelMap:

In[48]:= DistributeDefinitions@Ncdf, done, dtwo, BlackScholesPutD;

In[49]:= tb =AbsoluteTiming@valsb = ParallelMap@sigma ê. FindRoot@Ò@@1DD == BlackScholesPut@Ò@@2DD, Ò@@3DD, sigma,

0.045, 0, Ò@@6DDD, 8sigma, 0.1<D &, randomoptionparametersD;D

Out[49]= 80.416869, Null<

Some people call this sort of thing "embarassingly parallel". It's certainly easy! The performance improvement is a factor of:

In[50]:= ta@@1DD ê tb@@1DD

Out[50]= 6.12086

Close Down

In[51]:= CloseKernels@D

Out[51]= 8KernelObject@1, local, <defunct>D, KernelObject@2, local, <defunct>D,KernelObject@3, local, <defunct>D, KernelObject@4, local, <defunct>D, KernelObject@5, local, <defunct>D,KernelObject@6, local, <defunct>D, KernelObject@7, local, <defunct>D, KernelObject@8, local, <defunct>D<

2. Introduction to Mathematica MathematicaIntroduction.nb 80

80

Page 70: An Introduction to Mathematica - University College Londonucahwts/lgsnotes/MathematicaIntroduction.pdf · An Introduction to Mathematica A System for Doing Mathematics by Computer

In[52]:= Quit@D

81 MathematicaIntroduction.nb 2. Introduction to Mathematica

81