30
Functions

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

Embed Size (px)

Citation preview

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

Functions

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

Downloads

Today’s work is in: matlab_lec02.m

Functions we need today: myfunction.m, windsorise.m, npv.m

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

Overview

Logic

Control Structures (if, for)

Functions

Financial Example: NPV and Gordon Growth Model

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

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

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

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)

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

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<1 | A>2);>>in3=(A>1 & A<3);>>in4=(A~=2);

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

Examples

In the matlab prompt

Page 8: Functions. Downloads  Today’s work is in: matlab_lec02.m  Functions we need today: myfunction.m, windsorise.m, npv.m
Page 9: Functions. Downloads  Today’s work is in: matlab_lec02.m  Functions we need today: myfunction.m, windsorise.m, npv.m
Page 10: Functions. Downloads  Today’s work is in: matlab_lec02.m  Functions we need today: myfunction.m, windsorise.m, npv.m
Page 11: Functions. Downloads  Today’s work is in: matlab_lec02.m  Functions we need today: myfunction.m, windsorise.m, npv.m

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;

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

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

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

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=12+22+32+… 1002

Be careful with variables having same name as index, or making changes to index while inside loop

Make sure variables are initialized

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

Nested Loops

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

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

Loop withoutusing for

>>i=0;>>while i<10; i=i+1; disp(i); end; Beware of infinite loops!

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

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);

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

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]);

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

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

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

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

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

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;end;W=[z y];

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

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');

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

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?

Tt

tr

tDNPV

,1 )1(

)(

gr

DNPV

)0(

rrNPV

T

11

11

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

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;

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

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--');

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

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?

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

Next week

Randomization

Simulation

Real finance!

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

Functions we learned

Logic: ==, ~=, <, <=, >, >=, &, |

Control Structures: if, else, for

Math: sort

Ours: windsorize, npv