MATLAB FINANCIAL DERIVATIVES TOOLBOX Bedienungsanleitung

Stöbern Sie online oder laden Sie Bedienungsanleitung nach Software MATLAB FINANCIAL DERIVATIVES TOOLBOX herunter. Introductory Course to Matlab with Financial Case Studies Benutzerhandbuch

  • Herunterladen
  • Zu meinen Handbüchern hinzufügen
  • Drucken
  • Seite
    / 119
  • Inhaltsverzeichnis
  • LESEZEICHEN
  • Bewertet. / 5. Basierend auf Kundenbewertungen
Seitenansicht 0
University of Cyprus
Public Business Administration Department
Introductory Course to
Matlab with Financial Case
Studies
P
P
r
r
e
e
p
p
a
a
r
r
e
e
d
d
b
b
y
y:
Panayiotis Andreou
PhD Candidate
PBA UCY
Lefkosia, September 2003
Seitenansicht 0
1 2 3 4 5 6 ... 118 119

Inhaltsverzeichnis

Seite 1 - Studies

University of Cyprus Public Business Administration Department Introductory Course to Matlab with Financial Case Studies PPrreeppaarreedd b

Seite 2 - Table of Contents

9numbered in square brackets (e.g. [2] is the Matlab online help facilities; see the section with the references). Additionally, if a figure or a tab

Seite 3

99dzep)z(Nxz∫∞−−=22121 represents the cumulative normal distribution function with mean zero and unity standard deviation (the Matlab build in funct

Seite 4 - First Section

100 a function) named as “options_data.txt” in which the columns should contain S, X, T, s, r, d. The data values are given below: },,,,,,,,{S120115

Seite 5

101 8.1.2 BSM Derivatives From the same script as above, call a function with the name: BBSSMMddeerriivvaattiivveess that returns the partial derivat

Seite 6

102 8.1.3 BSM Plots and Surfaces Make the following plots: • Two subplot figures, one for call options with T=0.15 and s=0.20, and one for put optio

Seite 7 - 1.2 Basic Definitions

103 for simp. It is known that such equation has no analytic solution, so to solve it someone has to implement a numerical root-finding algorithm. T

Seite 8

104 arguments precede the optional ones (the optional are “dv”, “maxNumIter” and “tol”). Use as inputs the initial data from the loaded txt file (wi

Seite 9

105 the ones that you have initially used (these are the random values) to price calls and puts with the BSM. Note that the implied volatility of cal

Seite 10 - 1.5 Matlab’s Windows

106 stable convergence but with extremely slow rate. Typical values of a range between 0.01 and 0.15 depending on the faced problem. Specificall

Seite 11

107 Specifically, )x(''f for a function with n variables is: ∂∂∂∂∂∂∂∂∂∂∂∂∂∂∂∂∂∂∂∂∂∂=2222122222212212212212nnnnnx

Seite 12

108 To implement the previous algorithms, the user should be in a position to find the gradient vector and the Hessian matrix. Although it is better

Seite 13

10 Figure[2] 1: The Matlab desktop The above is termed as the Matlab Desktop (graphical user interfaces) and contains tools for managing files, vari

Seite 14

109 2212121212124h)hx,hx(f)hx,hx(f)hx,hx(f)hx,hx(fxx)x(f −+−+−−−−+++≈∂∂∂ 122212xx)x(fxx)x(f∂∂∂≡∂∂∂ 22121212122242222h)hxx(f)hxx(f)hxx(f)hxx(fx)x(f

Seite 15

110 • Call a function with the name: GGrraaddDDeesscceenntt that implements the gradient descent algorithm for minimizing the aforementioned functio

Seite 16

111 • Call a function with the name: NNeewwttoonn that implements the Newton descent algorithm for minimizing the aforementioned function. The calli

Seite 17

112 8.3 Portfolio Optimization Many times, a researcher needs to create a portfolio of securities that for a desire level of return/profit, it bears

Seite 18

113 =nnnnnnsssssssssOLMMMMLL212222111211 with )r,rcov(sjiij= . Additionally, for the portfolio optimization problem, it is required t

Seite 19 - The format function:

114 ReWT= 1=ITNW and for the case of the three assets in an analytic form: BCCBACCAABBACCBBAAwswwswwswwswswswmini222222222+++++ s.t. R)r(Ew)r(

Seite 20 - Entering Long Statements

115 Since this equals zero, we add it to the objective function that we want to minimize without affecting the results. Finally, we minimize the quad

Seite 21 - Current Directory

116 ==150100050...rrrEeCBA ==280090170090210150170150250222...sssssssssOCCBCABC?BAACAB? Write a sc

Seite 22 - 1.5.2 Editor/Debugger

117 change slightly the NNeewwttoonn function so that its calling syntax becomes: “[MinPoints] = NNeewwttoonnPPoorrtt(fun, x, r, V,

Seite 23

118 References [1] Ela Pekalska, Marjolein van der Glas, (2001), “Introduction to MATLAB”, Pattern Recognition Group, Faculty of Applied Sciences, D

Seite 24

11window text. For more information, click the Help button in the Preferences dialog box [2]. Additionally, two more windows that can be seen in Fi

Seite 25

12 Select one or more lines and right-click to copy and re-use the command with the command window, to evaluate it or to create an m-file Figure 3:

Seite 26 - Second Section

13 Figure 4: A basic calculation The Matlab displays the results into a variable named as aannss. This is a default variable name that is used by t

Seite 27

14The square brackets “[ ]” indicate the definition of the vector. Also, the space between the vector numbers separates the vector’s elements. The co

Seite 28 - 2.1.1 The Colon Notation

15Type also the following: “x=[(2^2+1)^2-15/4*6.1, 1.23e-2]” and after pressing [Enter] you get the Matlab’s result (Figure 7) Figure 7: A 2-e

Seite 29

16• “*” and “/” follow by working from left to right: (e.g. 2*8/4 = 16/4 = 4) • “+” and “-” follow last, from left to right: (e.g. 6-7+2= -1+2 = 1)

Seite 30 - 2.1.2 Column Vectors

17operations like 1/0. Matlab as a calculator includes a variety of build-in mathematical functions like: trigonometric, exponential, complex, roundi

Seite 31 - 2.1.3 Transporting Vectors

18List of Useful Commands: wwhhoo Lists current variables located in the workspace. wwhhooss A long form of wwhhoo. It lists all the variables i

Seite 32

1Table of Contents 1. Introduction...3 1.1 Learning Matlab...

Seite 33

19ffoorrmmaatt lloonngg ee Floating-point format with 15 digits. ffoorrmmaatt lloonngg gg Best of fixed or floating-point format with 15 digits

Seite 34

20 “s = 0.65321” Blank spaces around the “=”, “+”, and “-“operators are optional, but they improve readability. Command Line Editing

Seite 35

21current directory or on the search path (the search path is a default list of paths that include all folders with Matlab build in functions and too

Seite 36

22It’s like a text editor (e.g. MS Word) suitable for writing executable Matlab code. Notice that reserved words (e.g. wwhhiillee, iiff, eellssee, ee

Seite 37

23Type also: “hheellpp eellffuunn” to view the help results concerning the build-in elementary functions exhibited in Table 1 (notice also t

Seite 38 - 14131211

24 Figure[2] 11: Matlab’s help browser To become familiar with the Matlab’s accompanied toolboxes, navigate in the contexts, open up the droppin

Seite 39

25Second Section 2. Manipulating Vectors and Matrices A matrix or an array is the basic element on which Matlab can operate. A 1-by-1 matrix forms

Seite 40 - 2.2.1 Transpose of a Matrix

26functions element-wise. The lleennggtthh of the above vector is obtained via the following command: Matlab’s command: >> length(y) Matlab’s

Seite 41

27Matlab’s command: >> c1=5+b Matlab’s response: c1 = 7 6 7 Comments: An element-by-element addition of vector bb with 5. Matlab’

Seite 42

28parts of vectors (afterwards, with matrices, the colon can be used to view a certain part of a matrix). For instance, the following commands produc

Seite 43

23.2 Creating 3D Graphs...59 3.2.1 Creation of 3D Line Plots ...

Seite 44

292.1.2 Column Vectors Column vectors are created via the use of semi-colon “;” instead of commas and spaces. Operations with column vectors are simi

Seite 45 - Powers

302.1.3 Transporting Vectors To perform operation with column and row vectors of similar length, it is first needed to transpose the vectors in order

Seite 46 - )ln()nm(

31Matlab’s command: >> Z=[], Z=1:3, Z=[], length(Z) Matlab’s response: Z = [] Z = 1 2 3 Z = [] ans = 0 Comments:

Seite 47

32 The inner product is defined as the linear combination: ∑==tiiiuwu~w~1 The following depicts some examples with inner products. Matlab’s comman

Seite 48

33The dot product in Matlab is performed via the “.*” symbol. By the use of dot product we can get the inner product. This is explained in the follow

Seite 49

34Matlab’s command: >> d_div1=-3:1.5:3, d_div2=1:5 >> d_div2./d_div2, d_div1./d_div2, d_div2./5 Matlab’s response: d_div1 = -3.0000

Seite 50

35and a Not-a-Number, NNaaNN,, is returned in the vector. In the third case, the inverse of each vector element is required, but Matlab returns an er

Seite 51

36Comments: Examples of dot power. The second or the forth power of a certain vector can be defined either with the dot power or the dot product oper

Seite 52 - Third Section

37Matlab’s response: ans = 1 x = 1.0000 2.8000 4.6000 6.4000 8.2000 10.0000 x = 1.2589 1.5136 1.8197 2.1878 2.

Seite 53

38It is obvious that the above matrix can be decomposed to either 2 row vectors of 41× dimensions or to 4 column vectors of 12X. So, row and column

Seite 54

3First Section 1. Introduction This handout demonstrates a comprehensive introduction to the basic utilities of a high technical programming langua

Seite 55

39A2 = 1.0000 1.7500 2.5000 3.2500 4.0000 1.0000 2.0000 3.0000 4.0000 5.0000 1.0000 1.7500 2.5000 3.2500

Seite 56

40 It is the same idea with the transpose of a row vector to a column one and vise versa. The transpose of an nm×matrix is an mn×one that is found if

Seite 57

41in the workspace; use the wwhhoo function to view the variables that you have saved in the workspace or view them from the workspace browser): M

Seite 58

42position (2,1) is taken to be the second (2), the (3,1) the third and finally the (m, n) element is the last one (n*m). View the following example

Seite 59

43In addition, there are special cases where the step is defined to be a different integer number other than 1. Some paradigms follow: Matlab’s comm

Seite 60

442.2.2 Matrix Basic Manipulations Subtraction and multiplication with a scalar are similar as with the vector case. Matrix dimensions (ssiizzee) mus

Seite 61 - −+−−−+−−

45 Matlab’s command: >> clear; U=[-1 2 1; 0 -2 3]; V=[1:3; 5 2 4]; >> U.^2, U.*V, (1./V).^2.*U, U.^U Matlab’s response: ans = 1

Seite 62

46 111××××=nnmmCAC Let’s define the “A” and “B” matrices: =×242322211413121142aaaaaaaaA , =×424132312221121124bbbbbbbbB Like

Seite 63

47Col = 13 4 5 1 ans = 39 -310 297 314 Comments: Matrix-vector and matrix-matrix products. 2.1.5 Special Cases of Mat

Seite 64

48 ans = 0.9501 0.6068 0.2311 0.4860 ans = 0 0 Comments: Examples with matrices build-in functions. 2.1.5 Additional Useful

Seite 65

4Matlab features a family of add-on application-specific solutions called toolboxes. A toolbox is a comprehensive collection of Matlab functions (M-f

Seite 66

49 nmnmnnmmmmbxa...xaxabxa...xaxabxa...xaxa=+++=+++=+++221122122212111212111M The solution of the above system can be expressed as: bAx1−= given

Seite 67

50To see that these two procedures lead to different results, elaborate on the following example (search online help to see the use of the command: p

Seite 68

51Third Section 3. Plots and Graphs (2D and 3D) It is quite easy to create a plot or a graph by using the variables or parameters that have alrea

Seite 69 - Fourth Section

52Matlab’s response: 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-8-6-4-20246 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-15-10-505101520 Comments: Plot of a function. The us

Seite 70

53 Figure[2] 10: The figure window This figure window has tools that look like a photo editor (e.g. Microsoft® Photo Editor). You can select objects

Seite 71

54to mark each plotted point with a symbol. To do this, the user should call the plot function as: “pplloott(X,Y, '???')” where “?

Seite 72 - 4.2. Conditional Statements

55View the following examples to digest the plot facilities. Matlab’s command: >> plot(x, y, 'md: '); figure; plot(x, y, 'r*-

Seite 73 - 4.2.1 The

560 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-15-10-505101520 Comments: Experimenting with various functions and facilities related with the plots. The last ppll

Seite 74

57Matlab’s response: 0 2 4 6-20-1001020Subplot 10 2 4 600.20.40.60.81Subplot 20 0.5 100.20.40.60.810 10 20 30-2-1012Subplot 50 2 4 6-20-1001020Subplo

Seite 75

58where “PropertyName” relates with an element of the plot and “PropertyValue” is defined by the user. Some useful properties concern the following:

Seite 76

5It is better to use this handout in direct use with the Matlab. It will be more beneficial if while reading these notes you sit in front of a PC and

Seite 77

593.2 Creating 3D Graphs Matlab offers fancy 3-dimensional line plots and surface graphs. 3.2.1 Creation of 3D Line Plots There is the function: p

Seite 78 - Fifth Section

60functional expression, z=f(x,y), is evaluated. Usually, we evaluate the z behavior in a certain area of the (x,y)-plane in which z has some interes

Seite 79

61Matlab’s command: >> clear; x=-2:1:2; y=-4:1:4; [X,Y]=meshgrid(x,y) >> plot(X,Y, 'rh'); axis([-3 3 -5 5]); xlabel('x-axi

Seite 80

62rectangular domain. The subsequent 2D plot of XX and YY shows how the grid is sampled. Now, we should evaluate the ZZ at each pair of the grid an

Seite 81

63-2-1.5-1-0.500.511.52-4-2024-6-4-20246xyz Comments: Creating the 3D mesh and a 3D surface. The function is not smooth because the dense of the xx a

Seite 82 - 5.1.1.3 The Help Text

64Matlab’s response: -2-1.5-1-0.500.511.52-4-2024-8-6-4-202468xPeaksyZ -2-1.5-1-0.500.511.52-4-2024-8-6-4-202468xPeaksyZ Comments: Creating the 3D

Seite 83 - 5.2 Scripts

65To find what other useful commands can be accompanied the mmeesshh and ssuurrff commands, use the online help. 3.2.2.1 Examination of a Function’

Seite 84

66Comments: Creating the contour plot of the peaks function. Viewing this plot, it is easy to identify the critical points of the function. There is

Seite 85

67Comments: Locating the maximum of the peak function via an exhaustive search. “Fmax=mmaaxx(max(Z))” stores in Fmmaaxx the maximum value of the ZZ m

Seite 86 - Sixth Section

68Fourth Section 4. Control Flow A collection of commands that include repetition of a conditional code segment is termed as control flow structu

Seite 87 - Cell indexing

61.2 Basic Definitions Before moving to the introduction of issues specific to Matlab, some very basic definitions on computers and programming shou

Seite 88 - Content indexing

69example and for the case of a scalar: 0 | 1, 1 | 0, and 1 | 1 is 1 (TRUE) while 0 | 0 is 0 (FALSE). • “~A” is a matrix whose elements are 1&apos

Seite 89

70 d = 1 Comments: Logical and relational operations. aa is 1 because the statement evaluated is TRUE since xx is 2 and greater than 1. bb is FA

Seite 90 - 6.2 Structures

71matrix, returning a row vector of 1's and 0's. The second one, aallll, becomes TRUE if all elements of a vector are nonzero. For vectors,

Seite 91

724.2.1 The iiff Statement The iiff statement evaluates a logical expression and executes a group of statements when the expression is TRUE. The opti

Seite 92

73Although is possible to run these examples via the command window, it is better to use the Matlab Editor/Debugger to write a script instead. Real

Seite 93

744.3.1. The ffoorr Loop The ffoorr loop repeats a group of statements a fixed, predetermined number of times. A matching eenndd delineates the state

Seite 94

75 4.3.3. Nested Conditional and Loop Expressions All conditional and loop statements can be combined in any way that is desirable. If for example a

Seite 95 - Seventh Section

76Statement Comment bbrreeaakk Terminates the execution of a ffoorr or wwhhiillee loop. Statements in the loop that appear after the bbrreeaakk st

Seite 96

77Fifth Section 5. m-files: Scripts and Functions Up to this point, all commands passed to Matlab, were done through the command window. But when

Seite 97

78the theoretical price of a call option contract according to various options pricing models. Options pricing models like the Black and Scholes, the

Seite 98

7• m-file is a file that contains Matlab’s language code. m-files can be functions that accept arguments and produce output, or they can be scripts

Seite 99 - Eighth Section

79error); you have to return to the Editor to debug the error, re-save the function and try to run it again. If you debug your function or add somet

Seite 100 - )d(NSe)d(NXep

80 (Figure copied from [2]) Matlab function names have the same constraints as variable names. The name must begin with a letter, which may be fol

Seite 101

81 “ffuunnccttiioonn [] = pprriinntt__rreessuullttss (x)” The variables that you pass to the function do not need to have the same name as those

Seite 102 - =∂∂ , )d(nTSs/p

82 5.1.1.4 The Body Text The function body contains all the Matlab code that performs computations and assigns values to output arguments. The statem

Seite 103

83script that calls various options pricing function in order to price a call option and compare the pricing accuracy of each model). Unlike functio

Seite 104

84 Try to see what this script does. After executing the script, figure out what is the difference between the command ddiisspp and the ffpprriinnttf

Seite 105

85Sixth Section 6. Cells and Structures Structures are collections of different kinds of data organized by named fields. Cell arrays are a specia

Seite 106 - )x('faxx

86To create a cell array use the command: cceellll. For example if we want AA to be a 3-by-3 cell array, then you should use the following syntax:

Seite 107 - )]x(''f[

87 “A = [3x3 double] 'Anne Smith' [] [3.0000+ 7.0000i] [1x21 double]

Seite 108 - )]x(''f)[x('f

88The only difference between the cceellll command and the cell and content indexing is that by using cceellll you pre-allocating the cell’s dimens

Seite 109 - 1 ∈= )

8• Every build in or user made function has a calling syntax that is unique for each function. In Matlab, to call a function you type the function’s

Seite 110 - )yxyyx(e)x,x(f

89 [1x21 double] [5] ” So, if “{ }” are used after a cell array then Matlab returns the array’s contents but if “( )” are used instead, t

Seite 111

90 Building structure arrays using assignment statements You can build a simple 1-by-1 structure array by assigning data to individual fields. Matlab

Seite 112

91individual field contents when you type the array name. Instead, it shows a summary of the kind of information the structure contains. You can also

Seite 113 - 8.3 Portfolio Optimization

92 patient(1)=ssttrruucctt('name', 'John Doe', 'billing', 127.00, 'test', ...

Seite 114 - )r,rcov(s

93 “structure_name(index_of_entry).field_name( ) ” For example type: “patient(2).test(2:end,1:end-1)” in the command window to get: “ans

Seite 115 - BCCBACCAABBA

94Seventh Section 7. Entering and Saving Data Files Matlab can be used to both load a file of data from an external source and to save to your P

Seite 116

95should have the same number of elements (of course the advance user can handle via certain methodology missing values and text in an ASCII file). T

Seite 117 - = , r to be

96 Figure 16: Excel®’s Save As window The data will be saved in a file with the name: “random_number_data.txt” that can be loaded in Matlab. If by m

Seite 118

97name variable_name has components like: NaN or Inf, then these are saved as text and not numbers in the saved txt file. Concerning the options in

Seite 119 - References

98Eighth Section 8. Case Studies In the following sub-section, some very useful examples with functions and scripts are illustrated. Read, impleme

Kommentare zu diesen Handbüchern

Keine Kommentare