Upload
johnathan-chambers
View
216
Download
1
Embed Size (px)
Citation preview
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
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
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)
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);
Examples
In the matlab prompt
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;
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
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
Nested Loops
>>for i=1:5; for j=1:5; B(i,j)=min(i,j); end; end;
Loop withoutusing for
>>i=0;>>while i<10; i=i+1; disp(i); end; Beware of infinite loops!
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);
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]);
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
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
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];
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');
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
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;
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--');
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?
Next week
Randomization
Simulation
Real finance!
Functions we learned
Logic: ==, ~=, <, <=, >, >=, &, |
Control Structures: if, else, for
Math: sort
Ours: windsorize, npv