matlab: notions de programmation applications en finance

36
MatLab: Notions de Programmation Applications en Finance

Upload: marcellin-lacaze

Post on 04-Apr-2015

144 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MatLab: Notions de Programmation Applications en Finance

MatLab: Notions de Programmation

Applications en Finance

Page 2: MatLab: Notions de Programmation Applications en Finance

Introduction

• MatLab=Matrix Laboratory• Permet la manipulation de matrices de maniere souple• Developpement d’outils dans diverses disciplines:

– Econometrie, finance– Analyse des signaux– Ingenierie etc...

Page 3: MatLab: Notions de Programmation Applications en Finance

Fenetre d’execution

•Indiquee par >>

•Toutes les fonctions en Matlab sont des lignes de texte et sont executees en appelant le nom de la fonction avec ses parametres

Page 4: MatLab: Notions de Programmation Applications en Finance

Help•Fonction d’aide en ligne help help non_fonction

Page 5: MatLab: Notions de Programmation Applications en Finance

Fichiers Matlab•Les fichiers sont en texte

•Extension .m

•Edit corrcoef

• %: Commentaire

Page 6: MatLab: Notions de Programmation Applications en Finance

Ponctuation

• Les instructions sont terminees par un point-virgule

• Sinon, les calculs sont affiches

Page 7: MatLab: Notions de Programmation Applications en Finance

Commandes Generales

• whowho: liste des variables en memoires• whoswhos: informations complementaires sur leur type• clearclear: annule les variables specifiees (ou toutes)• savesave: sauver une variables sous format specifie• loadload: charger une variable a partir d’un fichier• whatwhat: liste des fonctions dans le fichier de travail

Page 8: MatLab: Notions de Programmation Applications en Finance
Page 9: MatLab: Notions de Programmation Applications en Finance

Operations de Base

•++, --, **, //, ^̂,

• sinsin, coscos, tantan,

• asinasin, acosacos, atanatan,

• expexp, log log (naturel), log10 log10 (en base 10),

• absabs, sqrtsqrt, signsign

Page 10: MatLab: Notions de Programmation Applications en Finance

Matrices• Entre parentheses carrees

Vecteur ligne x = [1, 2, 3];

Vecteur colonne: y = [1; 4; 7];

Matrice: A = [1 2 3; 4 5 6; 7 8 9];

ou: A = [1 2 3 4 5 6 7 8 9 ];

Page 11: MatLab: Notions de Programmation Applications en Finance

Exemple

Page 12: MatLab: Notions de Programmation Applications en Finance

Pour faire reference a des elements d’une matrice

• l’element amn est indique A(m,n)A(m,n);

A(2,3) donne 6• La ligne m est indiquee par A(m,:)A(m,:), toutes les colonnes sont

selectionnees par :

A(2,:) donne [4 5 6]• La colonne n est indiquee par A(:,n)A(:,n), toutes les lignes sont

selectionnees

A(:,3) donne [3; 6; 9]

• La matrice partielle amn, avec m1mm2 et n1nn2

A(m1:m2,n1:n2)A(m1:m2,n1:n2);

A(1:2,2:3) dà [2, 3; 5, 6]

Manipulation de Matrices

Page 13: MatLab: Notions de Programmation Applications en Finance

• Les operateurs matriciels + - * ^ / \ '+ - * ^ / \ '

Transposition Division a gauche:A\B=inv(A)*BDivision a droite: B/A=B*inv(A)

Attention: L’ordre des matrices n’est pas indifferent

A*B A*B B*A B*A

Page 14: MatLab: Notions de Programmation Applications en Finance

X=3x2Y=2x3XY=3x3YX=2x2

Page 15: MatLab: Notions de Programmation Applications en Finance

• Autres fonctions utiles maxmax, minmin, sortsort, sumsum, prod prod, medianmedian

• Manipulation element par element

y = sin(x) ..* * cos(x);• Valeurs manquantes NaN• Isnan, nanmean, nanstd

Page 16: MatLab: Notions de Programmation Applications en Finance

• Autres

invinv

detdet

sizesize

rankrank

eigeigOn ne peut inverser que des

matrices carrees

Inverse de la matrice

Determinant

Dimensions

rang

Valeurs propres

» y=normrnd(0,1,100,1);

» x=0.5*y+normrnd(0,1,100,1);

» beta=inv(x'*x)*x'*y

beta =

0.3541

Page 17: MatLab: Notions de Programmation Applications en Finance

Creation de matrices

eye(n) eye(n) : matrice identite

zeros(m,n)zeros(m,n): matrice de zeros

ones(m,n) ones(m,n) : matrice de 1

rand(m,n) rand(m,n) : matrice aleatoire [0,1]

diag(X) diag(X) : extraire la diagonale

Page 18: MatLab: Notions de Programmation Applications en Finance
Page 19: MatLab: Notions de Programmation Applications en Finance

• Sans specifier d’increment t=1:5 => t=[1 2 3 4 5]• Increment positif specifie t=0:0.2:1 => t=[0 0.2 0.4 0.6 0.8 1]

• Increment negatif t=2:-0.2:1 => t=[2 1.8 1.6 1.4 1.2 1]

Generation de Vecteurs

Page 20: MatLab: Notions de Programmation Applications en Finance

IF-THEN-ELSE

•ifif condition1,,

operation1;;

elseifelseif condition2,,

operation2;;

elseelse

operation3;;

end;end;

Page 21: MatLab: Notions de Programmation Applications en Finance

Condition1,2Condition1,2 doivent donner comme resultats TRUE ou FALSE. Les Operateurs sont:Les Operateurs sont:<< , > > <= <= , >= >= == == ~=~=&&||

egal

different

Et

ou

Page 22: MatLab: Notions de Programmation Applications en Finance
Page 23: MatLab: Notions de Programmation Applications en Finance

Boucles

Syntaxe de base

for kfor k = 1:step:n,= 1:step:n,

operationi,

end;end;

Executer l’operation pour k=1,1+step,1+2step....jusqu’a n

while while condition

operationi;

end;end;

Page 24: MatLab: Notions de Programmation Applications en Finance

Exemple

» y=[];for x=0:pi/4:2*pi; si=sin(x); y=[y;si];endyy = 0 0.7071 1.0000 0.7071 0.0000 -0.7071 -1.0000 -0.7071 -0.0000

Page 25: MatLab: Notions de Programmation Applications en Finance

Graphiques

plotplot cree des graphiques en deux dimensions

plot(x,y);

x=0:pi/20:2*pi;

» y=sin(x)

Page 26: MatLab: Notions de Programmation Applications en Finance

plot(x,y,'g+'); •Choix possibles r red . point

g green o circle b blue x x-mark w white + plus m magenta * star c cyan - solid y yellow : dotted k black -- dashed

-. dash-dot

Page 27: MatLab: Notions de Programmation Applications en Finance

• Autres commandes

grid grid : lignes

title title : titre

xlabel xlabel : legende abscisses

ylabel ylabel : legende ordonnees

axis axis : changer echelle

•figurefigure cree une nouvelle fenetre graphique

Page 28: MatLab: Notions de Programmation Applications en Finance

Pour visualiser differents graphiques

subplot(211), plot(funz1);

subplot(212), plot(funz2);

subplot(2,1,1)

» plot(normrnd(0,1,100,1))

» subplot(2,1,2)

» plot(normrnd(0,1,100,1)*100)

Page 29: MatLab: Notions de Programmation Applications en Finance

Fonction graphique en 3D

xx = -2:0.1:2;yy = xx;[x,y] = meshdom(xx,yy);z = x .* exp(-x.^2 - y.^2);mesh(z);

Page 30: MatLab: Notions de Programmation Applications en Finance

Nouvelles Fonctions

• Creer un nouveau fichier en mode editeur

• La premiere ligne donne function z = fun1(a,b) ou function [x,y] = fun2(a,b) a et b sont les inputsZ, ou x et y sont les resultats

Page 31: MatLab: Notions de Programmation Applications en Finance

Finance

• Frontiere Efficiente: – Utiliser la Boite d’Outils MatLab Finance– Creer routine de maximisation de la fonction

d’utilite

Page 32: MatLab: Notions de Programmation Applications en Finance
Page 33: MatLab: Notions de Programmation Applications en Finance
Page 34: MatLab: Notions de Programmation Applications en Finance
Page 35: MatLab: Notions de Programmation Applications en Finance
Page 36: MatLab: Notions de Programmation Applications en Finance