0% found this document useful (0 votes)
110 views11 pages

Rates Matlab and Excel (Barlan)

Uploaded by

julien_barlan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views11 pages

Rates Matlab and Excel (Barlan)

Uploaded by

julien_barlan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 1

Julien R. Barlan (IEP Paris) Austin, Texas, USA


Department of Economics April 2010
[email protected]
http://jrbpagetravaux.blogspot.com/

Credited use permitted for educational purposes only

Financial ratios in Matlab and Excel


In this handout, we will go over stock returns in Matlab and Excel. As usual, I will start by
introducing new commands, for instance indexing vectors and matrices. Then we will have a
look to two basic rates widely used in financial analysis, namely the total return and the rate
of return. The first is an easiest computation to estimate the outcome of a financial
transaction, which could result in a gain or a loss. The rate of return captures the magnitude of
the outcome.
Introduction to conditioning in Matlab is the main interest of this handout. It will be done in
the third part.

I) Matlab commands: Matrix indexing1


Indexing into a matrix or a vector means to “pick up” some particular entries. If we have the
following vector a=[-1:1:4] we might only be interested in 0 and 2 in order to perform
some calculations later on our paper.

 To do so we enter a(2) and a(4), to stress on the fact we need the second and
fourth entry of the vector.

1
This tutorial by Steven Eddins and Loren Shure is very well done. I must admit I will summarize their article
rather than writting a new one. As usual, it’s available for free on the Mathworks website.
http://www.mathworks.com/company/newsletters/digest/sept01/matrix.html
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 2

Basically, one might just want to use 0 and 2 further in their work rather and a 10 and
a12. Nevertheless, those numbers could be much more complicated such as irrationals.
In this case, we could argue indexing avoids rounding issues. Furthermore, if a is
obtained from a previous vector (or matrix), let’s say a0, we just want to make sure
a(2) and a(4) will automatically change, especially if they are used in some later
calculations. Think about how Excel works, when one modifies a cell, all other
indexed-cells change in consequence.

 Note the following a([an,…,am,…,ap)] will index the specified entries whereas
a(an:am) will index all entries ai where n≤i≤m . Similarly, a([an:am ap:at])
will give as many range of entries as requested. By the way a(end) refers to the last
entry.

 It is also possible to replace some of the entries of the original vector. The command is
a(a1j,…,a1k)=[b1j,…b1k]. Note you can also use intervals. For instance:

 If we are talking about a matrix, b(i j) extract the entry bij which is located at the
crossing of the ith row and the jth column. Moreover, b(i1:i2,j1:j2) displays rows from in
to im crossing with columns from ij to ik.
For example:
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 3

 Finally, we have to go over linear indexing. In other words, to get several entries into a
vectors. We have to know that Matlab will assign each entries of the b-matrix a
particular number called linear index, starting from 1 for b11 through x for bnm if b is an
n x m matrix following a column pattern, i.e. it goes on indexing columns by columns.
Let’s have a look to the next snapshot:

II) Stock rates in Excel


To download the attached file, click here.
Finance theory extensively uses rates to evaluate the return of an investment. You are
expected to be familiar with market finance theory (note those examples could by used in
corporate as well as in personal finance) even-though knowledge in mathematics – and
common sense in winning or losing money - is enough to understand the calculations.
Basically, we want to evaluate if our investment resulted in a loss or a gain.

 The total return is a very simple ratio of the revenues over the expenditures such as:
S 1 +D
Total return T= S 0 +C where S0 is the buying price and S1 the selling price. C
captures various costs such as transaction costs.
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 4

D captures potential dividends. Simply, if we made a profit, T>1, otherwise T<1. A


financially neutral operation would result in T=0.

 A similar ratio is the Rate of return R, expressed in percentages, is given by:


S 1+ D−( S 0 +C )
R(%) = S 0 +C x 100

 I arbitrarily selected five stocks listed on the NASDAQ: Apple, Cisco System, Dell,
Cephalon and Gilead Sciences. I focused on the following trading date interval: from
February 22 to April 20, 2010

Here is a table summarizing the data. Prices are adjusted, therefore we do not have to
worry about dividends. For the sake of simplicity, consider transaction cost is 0.
Table 1
Stock Buying price ($) Selling price ($)

AAPL 200.42 244.59

CSCO 24.3 27.18

DELL 13.36 17.01

CEPH 67.4 66.88

GILD 47.88 45.07

It highlights that if our time boundaries correspond to buying and selling dates, then
we made a profit with the three first stocks of the portfolio since their prices have
increased. For the same reason, our investment resulted in a loss for out bets on
Cephalon and Gilead.
Consequently we expect the total of return T to be above 1 and the rate of return R
greater than 0 in the first case. On the other hand, we will face opposite statistics when
it comes to losses. We can use Excel to compute the ratios. For T, we need to divide
selling price cells by corresponding buying price cells entering =Xi/Yi and for R: =(Xi-
Yi)/Yi recalling that transaction costs are assumed null.
Table 2
Stock Buying price Selling price Total Return T Rate of Return R(%)

AAPL 200.42 244.59 1.220387187 22.04%

CSCO 24.3 27.18 1.118518519 11.85%

DELL 13.36 17.01 1.273203593 27.32%

CEPH 67.4 66.88 0.992284866 -0.77%

GILD 47.88 45.07 0.941311612 -5.87%


JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 5

Graphically, we can represent our rates of return with an histogram. I am using Excel
2008 for Mac, which is not as easy to handle as the 2007 version of PCs. To be able to
use some text for the X-axis, it is critical one enters the text columns contiguous with
the value columns. It other words, we have to rearrange Table 2 to display “stocks”
and “rate of returns” side-by-side.
Table 3

It becomes visually clear that our largest profit was made betting on Dell and the
largest loss is due to our position on Gilead sciences.
Note: costs will be included in the next portfolio management handout.

III) Stock rates in Matlab and introduction to conditioning


To download the attached file, click here.
We can do exactly the same work in Matlab but it turns out it is more complicated. Unlike
Excel, it does not work with cells but with vectors and matrices, what complicates the task.
Nevertheless we will go over the computation in Matlab especially to introduce conditioning
which soon enough will become a big deal in next handouts.

 Here is the beginning of the Matlab script:


feb22=[200.42 24.30 13.36 67.40 47.88];
a=[1/feb22(1) 1/feb22(2) 1/feb22(3) 1/feb22(4) 1/feb22(5)];
apr20=[244.59 27.18 17.01 66.88 45.07];
total_return=[diag(apr20'*a)]',
rate_of_return_prcent=(diag((apr20-feb22)'*a))'
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 6

The first and third vectors represent the stocks prices on February 22 and April 20, 2010,
conserving the following order: Apple, Cisco System, Dell, Cephalon and Gilead
Sciences.

 Indexing becomes important in a. Indeed, we need to have S1/S0 but we cannot


simply divide apr20/feb22. A solution would be to create a new matrix C such that
diag((feb22)’*C)’ computes a vector V = [1 / feb221,… ,1 / feb225].
Nevertheless, it would be especially created according to feb22 entries and would not
work for any other vectors or if any feb22 i would happen to be modified. This it why
do not have no other choice than creating a new vector a but indexed into feb22
entries and not fixed numbers. In a nutshell, any modifications in feb22 would be
captured in x, and accordingly to the end of the model.
Eventually, Matlab computes two vectors: total returns T and rates of return R.

 Programming is what makes Matlab less friendly than Excel when one could use both
for the same piece of work. On the other hand, programming means freedom. The user
can let their imagination flow, especially when it comes to graphing.
First of all, we have to create a “bar chart”. The command is bar. Note that bar3
would create a 3-D bar chard. As for plotting a line, bar(A) will plot a bar chart
based on the vector A. Property commands for colors, size, shape etc. are the same as
ones covered in previous handouts. For instance, we start with the following:

bar(rate_of_return_prcent,'w'),set(gca,'XTickLabel',{'APPL',
'CSCO', 'DELL', 'CEPH','GILD'}),...


The set command is very interesting. It allows one to define properties of certain
elements in the code. By gca we explicitely refer to the axis, and xticklabel
allows ones to replace 1:m by anything. Indeed, without those precisions, Matlab
would assign each value of A an x-axis value corresponding to its position in A. The
first element in rate_of_return_percent will be placed first on the axis. But
we do not want it to be labeled 1. For the sake of precision and readability, we want it
to be APPL since we are focusing on the Apple stock.

Conditioning
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 7

 The main advantage of Matlab, will be conditioning. There is no doubt our bar chart
will not bas as fancy as Excel’s histograms. But we will have the opportunity to
introduce conditioning. Typically, the syntax is the following (obtained with help
if)
if I == J
A(I,J) = 2;
elseif abs(I-J) == 1
A(I,J) = -1;
else
A(I,J) = 0;
end

It is actually pretty straightforward: we define a condition, an alternative, and a


general statement in case the two first ones are not satisfied. It will allow us to index
a value or a command into another. For instance, we can define a value for any C,
depending on the value of any D.

 Let’s have a look to this quick example


% 1 = short
% .5 = wait and see
% 0 = stay
% v=volatility (%)

v=2.96;

if v>3
positon=1
elseif 2<v<3
position=.5
else
position=0
end

position =

0.5000

 As we can see, the value of “position” depends on what has previously been set up.
Moreover, one notices that the value “position” is created at the same time, we just
had to define v before.

 Let’s use conditioning in our bar chart.


if rate_of_return_prcent(1)<0
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 8

text(0.5,rate_of_return_prcent(1),...
['',num2str(rate_of_return_prcent(1))],'backgroundcolor','r',...
'FontWeight','b'),
elseif rate_of_return_prcent(1)==0
text(0.5,rate_of_return_prcent(1),...
['',num2str(rate_of_return_prcent(1))]),
else rate_of_return_prcent(1)>0
text(0.5,rate_of_return_prcent(1),...
['',num2str(rate_of_return_prcent(1))],'backgroundcolor','g',...
'FontWeight','b'),
end

 It turns out that the color of the text depends on its value. This is a part of the source
code. Depending on the value of the ith value in rate_of_return_prcent it will be
displayed in different colors. By common standards, green highlights a gain whereas
red will be use to stress on a loss. Note that instead of conditioning the color of the
text, we define its background color. It turns out that it is visually nicer. Nevertheless,
it is up to the reader to modify the code to any extense.
Note that Matlab will return ans = 1 anytime a condition is satisfied.
Here is the final result:

Note that the full code can be founded in appendix, and of course is in the attached file.
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 9

Conclusion
This was a simple introduction to portfolio management. We’ve been through two simple
ratios and we went over a basic spreadsheet in Excel. The more interesting part was the
computations in Matlab where we learnt basic conditioning programming. That will allow us
to go further in next handouts. Indeed, conditioning is particularly useful in decision-making.
One could make a decision based on some previously set-up conditions. If they happen to be
satisfied, we might decide to move for a position based on our investment pattern; or
inversely stay away from the assets.
An extensive application of conditioning can be made in the field of economics and business.
Those handouts are first and foremost examples, it is up to the reader to use the tools we
developed. Broadly speaking, one should draw their inspiration from what they read.
Practicing is, as usual, strongly advised.

Appendix 1: full Matlab code source


% Copyright Julien R. Barlan (SciencesPo University)

% Stock returns on Matlab


% Using five arbitrarily chosen components of the NASDAQ-100

% Apple Inc. (AAPL)


% Cisco Systems, Inc. (CSCO)
% Dell Inc. (DELL)
% Cephalon Inc. (CEPH)
% Gilead Sciences Inc. (GILD)

% Note: this is not an investment pattern whatsoever.

% Investment period: Feb 22 - Apr 20, 2010

feb22=[200.42 24.30 13.36 67.40 47.88];


a=[1/feb22(1) 1/feb22(2) 1/feb22(3) 1/feb22(4) 1/feb22(5)];
apr20=[244.59 27.18 17.01 66.88 45.07];
total_return=[diag(apr20'*a)]';
rate_of_return_prcent=(diag((apr20-feb22)'*a))';

bar(rate_of_return_prcent,'w'),set(gca,'XTickLabel',{'APPL',...
'CSCO', 'DELL', 'CEPH','GILD'}),...

if rate_of_return_prcent(1)<0
text(0.5,rate_of_return_prcent(1),...
['',num2str(rate_of_return_prcent(1))],'backgroundcolor','r',...
'FontWeight','b'),
elseif rate_of_return_prcent(1)==0
text(0.5,rate_of_return_prcent(1),...
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 1
0

['',num2str(rate_of_return_prcent(1))]),
else rate_of_return_prcent(1)>0
text(0.5,rate_of_return_prcent(1),...
['',num2str(rate_of_return_prcent(1))],'backgroundcolor','g',...
'FontWeight','b'),
end

if rate_of_return_prcent(2)<0
text(0.5+1,rate_of_return_prcent(2),...
['',num2str(rate_of_return_prcent(2))],'backgroundcolor','r',...
'FontWeight','b'),
elseif rate_of_return_prcent(2)==0
text(0.5+1,rate_of_return_prcent(2),...
['',num2str(rate_of_return_prcent(2))]),
else rate_of_return_prcent(2)>0
text(0.5+1,rate_of_return_prcent(2),...
['',num2str(rate_of_return_prcent(2))],'backgroundcolor','g',...
'FontWeight','b'),
end,

if rate_of_return_prcent(3)<0
text(0.5+2,rate_of_return_prcent(3),...
['',num2str(rate_of_return_prcent(3))],'backgroundcolor','r',...
'FontWeight','b'),
elseif rate_of_return_prcent(3)==0
text(0.5+2,rate_of_return_prcent(3),...
['',num2str(rate_of_return_prcent(3))]),
else rate_of_return_prcent(3)>0
text(0.5+2,rate_of_return_prcent(3),...
['',num2str(rate_of_return_prcent(3))],'backgroundcolor','g',...
'FontWeight','b'),
end,

if rate_of_return_prcent(4)<0
text(0.5+3,rate_of_return_prcent(4),...
['',num2str(rate_of_return_prcent(4))],'backgroundcolor','r',...
'FontWeight','b'),
elseif rate_of_return_prcent(4)==0
text(0.5+3,rate_of_return_prcent(4),...
['',num2str(rate_of_return_prcent(4))]),
else rate_of_return_prcent(4)>0
text(0.5+3,rate_of_return_prcent(4),...
['',num2str(rate_of_return_prcent(4))],'backgroundcolor','g',...
'FontWeight','b'),
end,

if rate_of_return_prcent(5)<0
text(0.5+4,rate_of_return_prcent(5),...
['',num2str(rate_of_return_prcent(5))],'backgroundcolor','r',...
'FontWeight','b'),
JULIEN BARLAN, INSTITUT D’ETUDES POLITIQUES DE PARIS 1
1

elseif rate_of_return_prcent(5)==0
text(0.5+4,rate_of_return_prcent(5),...
['',num2str(rate_of_return_prcent(5))]),
else rate_of_return_prcent(5)>0
text(0.5+4,rate_of_return_prcent(5),...
['',num2str(rate_of_return_prcent(5))],'backgroundcolor','g',...
'FontWeight','b'),
end,

title('\fontsize{16} Stocks rates of return (Feb 22 - Apr 20, 2010)');

total_return, rate_of_return_prcent,

You might also like