Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions. Downloads  Today’s work is in: matlab_lec02.m  Functions we need today: myfunction.m, windsorise.m, npv.m.

Similar presentations


Presentation on theme: "Functions. Downloads  Today’s work is in: matlab_lec02.m  Functions we need today: myfunction.m, windsorise.m, npv.m."— Presentation transcript:

1 Functions

2 Downloads  Today’s work is in: matlab_lec02.m  Functions we need today: myfunction.m, windsorise.m, npv.m

3 Overview  Logic  Control Structures (if, for)  Functions  Financial Example: NPV and Gordon Growth Model

4 Logic  1 means True, 0 means False  == is used for logic, = to assign values >>1==1 ans = 1 >>1==2 ans = 0 >>x=5; %assigns value 5 to x >>x==5; %checks if x is equal to 5, returns either 1 %or 0 >>x=(x==5); %checks if x is equal to 5, then %assigns True (1) or False (0) to x

5 Logical Operators == % equal to ~= % not equal to > % greater than >= % greater than or equal to < % less than <= % less than or equal to & % and | % or (top left of keyboard)

6 Examples >> A=[zeros(3,1); ones(3,1); 2*ones(3,1); 3*ones(3,1)]; >>in1=(A>0); >>A(A>0); % is same as A(in1) >>in2=(A 2); >>in3=(A>1 & A<3); >>in4=(A~=2);

7 Examples  In the matlab prompt

8

9

10

11 if statements >>if A(1)==0; x=5; y=x; end;  All function names are lower case >>if (logical statement); (command to be executed); end;

12 if-else statements >>if A(3)==A(4); x=A(5); y=A(4); elseif A(3)==0; x=5; y=0; elseif A(3)==1; x=4; y=5; else; x=3; y=8; end;  Just like if statement but adds a elseif and else

13 for statements and loops >>T=100; s=0; x=0; >>for i=1:T; s=s+i; x=x+i*i; end;  This loop calculates a sum and a sum of squares s=1+2+3+… 100 x=1 2 +2 2 +3 2 +… 100 2  Be careful with variables having same name as index, or making changes to index while inside loop  Make sure variables are initialized

14 Nested Loops >>for i=1:5; for j=1:5; B(i,j)=min(i,j); end;

15

16 Loop without using for >>i=0; >>while i<10; i=i+1; disp(i); end;  Beware of infinite loops!

17 Alternatives to Loops  Loops are slow, matrix operations are fast  Avoid using loops if you can! >>x=0; >>for i=1:5; x=x+i*i; end;  Alternative: >>A=[1:5]; x=sum(A.*A);

18 Example: Mean and StDev >>[T L]=size(bp); >>s=0; s2=0; >>for i=1:T; s=s+bp(i,4); s2=s2+(bp(i,4)^2); end; >>M=s/T; StD=sqrt((s2/T)-M*M); >>disp([mean(bp(:,4)) M]); >>disp([std(bp(:,4)) StD]);

19 Functions  Functions created in.m files  Unlike scripts, you can call on functions, and functions can return values  For example f(x,y)=5*x+3*y is a function that takes in arguments x and y, returns f(x,y)  Matlab has many intrinsic functions (i.e. log(.), corrcoef(.,.), mean(.)  Functions can take in zero, one or many arguments  Functions can return zero or one argument, but that argument can be a matrix

20 A simple function function z=myfunction(x,y); z=(x.^2)+2*x.*y+(y.^2); %-This function can take in scalars or vectors %-Functions do not change the values of the % arguments that are passed to them, that % is, they are independent of the external % environment

21 Windsorization function W=windsorise(x,lowcut,highcut); [T L]=size(x); z=x; y=sort(x); for i=1:T; if z(i)<y(round(lowcut*T)); z(i)=y(round(lowcut*T)); end; if z(i)>y(round(highcut*T)); z(i)=y(round(highcut*T)); end; W=[z y];

22 Using windsorise  In matlab prompt plot regular series and windsorised series >>data_bp; >>X=windsorise(bp(:,4),.05,.95); >>plot(bp(:,4)); >>hold on; >>plot(X(:,1), 'r');

23

24 NPV: The Model  Model:  When D(t+1)=D(t)*(1+g) and the sum is infinite, this reduces to Gordon Growth Model:  When g=0, this reduces to:  What if growth stops after Ts?  How else can you modify this model? Value firms vs. Growth firms?

25 NPV: The function function y=npv(d,r,g,T,Ts); y=0; cf=zeros(T,1); cf(1)=d; for t=1:T; if t<=Ts; cf(t+1)=cf(t)*(1+g); else; cf(t+1)=cf(t); end; y=y+cf(t)/((1+r)^t); end;

26 Using the NPV function >>d=1; r=.05; g=.02; T=20; Ts=10; >>p=npv(d,r,g,T,Ts); >>T=200; p=zeros(T,1); >>for t=1:T; p(t)=npv(d,r,g,t,t); end; >>hold off; plot([1:T],p, 'b'); >>hold on; >>plot([1:T],ones(1,T)*(d/(r-g)), ' r-- ' );

27

28 NPV function: extensions  Growth firms: little cash flow now, lots of cash flow later  Value firms: lots of cash flow now  Cash flows that vary arbitrarily? ie input an arbitrary cash flow stream  Cash flows that vary randomly? Use rand() and randn() functions  Time varying returns?

29 Next week  Randomization  Simulation  Real finance!

30 Functions we learned  Logic: ==, ~=,, >=, &, |  Control Structures: if, else, for  Math: sort  Ours: windsorize, npv


Download ppt "Functions. Downloads  Today’s work is in: matlab_lec02.m  Functions we need today: myfunction.m, windsorise.m, npv.m."

Similar presentations


Ads by Google