Professional Documents
Culture Documents
Using Matlab in Mutual Funds Evaluation
Using Matlab in Mutual Funds Evaluation
INDEX
1. Load data using the Editor window and m-file
2. Learning to save results from the Editor window.
3. Computing the Sharpe Ratio
4. Obtaining the Treynor Ratio
5. The Alpha from the CAPM
6. The Alpha from the Fama and French three factors model.
7. Making ranking and obtaining conclusions.
1/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
Second, write some comments at the beginning that permits you to identify
this code.
>> %% This code has been created to evaluate Equity Mutual Funds - Mater in Finance
>> %% Date: February-2012
2/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
rMF: This contains the returns of all mutual funds. Each mutual fund is
represented in a different column. So rMF is a 24 x 55 matrix.
rtbills: It is a vector (24x1) with the monthly risk-free rate.
rMk: It contains the returns of a Market index.
smb: A column vector with the Small-minus-Big factor to be use in the Fama
and Frech model.
hml: A column vector with the High-minus-Low factor for the Fama and French
model.
wml: A column vector with the momentum factor for the Carhart model.
If you want, you could plot each of the variables to check that they are right or there are
not extreme outliers, etc.
>>
>>
>>
>>
plot(rtbills)
plot(rMk)
plot(SMB)
plot(HML)
>>
>>
>>
for i=1:30
plot(rMF(:,i))
end
3/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
4/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
meanrtbills=mean(rtbills);
Third, we compute the standard deviation of each mutual funds, using the
function std.
>>
stdrMF=std(rMF);
Sharpe=(meanrMF-meanrtbills)./ stdrMF;
It is must be noted that you are using ./ and not /. Because actually you do not want
to do a matrix division. Instead you need to divide each mutual fund excess return by its
standard deviation.
Additionally, you could compute the Average Sharpe Ratio (or the median) for all the
mutual funds, or plot the Sharpe Ratios to analyze them.
>>
>>
figure
bar(Sharpe)
5/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
>>
>>
>>
0.5
0.4
0.3
0.2
0.1
-0.1
-0.2
10
20
30
40
50
60
6/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
>>
for j=1:55
>>
>>
>>
rMF2(:,j)=rMF(:,j)-rtbills;
rMk2=rMk-rtbills;
varianrMk2=var(rMk2);
>>
>>
>>
>>
covarMatrix=cov(rMF2(:,j),rMk2);
covarMF=covarMatrix(1,2);
Beta(1,j)=covarMF/varianrMk2;
end
Treynor=(meanrMF-meanrtbills)./Beta;
7/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
10
20
30
40
50
60
8/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
ri = i + i (rM ) +
where
ri = Ri r f
rM = RM r f
[STATS]=regstats(Y,X, 'linear')
>> help regstats
REGSTATS Regression diagnostics for linear models.
STATS = REGSTATS(RESPONSES,DATA,MODEL,WHICHSTATS) creates an output
structure STATS containing the statistics listed in WHICHSTATS. WHICHSTATS can be a
single string such as 'leverage' or a cell array of strings such as {'leverage' 'standres' 'studres'}.
By default, REGSTATS returns all statistics. Valid statistic strings are:
9/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
Name
Meaning
'Q'
Q from the QR Decomposition of the design matrix
'R'
R from the QR Decomposition of the design matrix
'beta'
Regression coefficients
'covb'
Covariance of regression coefficients
'yhat'
Fitted values of the response data
'r'
Residuals
'mse'
Mean squared error
'rsquare' R-square statistic
'adjrsquare' Adjusted R-square statistic
'leverage' Leverage
'hatmat' Hat (projection) matrix
's2_i'
Delete-1 variance
'beta_i' Delete-1 coefficients
'standres' Standardized residuals
'studres' Studentized residuals
'dfbetas' Scaled change in regression coefficients
'dffit'
Change in fitted values
'dffits' Scaled change in fitted values
'covratio' Change in covariance
'cookd'
Cook's distance
'tstat'
t statistics for coefficients
'fstat'
F statistic
'dwstat' Durbin Watson statistic
'all'
Create all of the above statistics
for j=1:55
Y=rMF2(:,j);
X= rMk2;
stats=regstats(Y, X,'linear');
coefic=stats.beta;
Beta(j,1)=coefic(2)
alpha_J(j,1)=coefic(1)
t_Jensen(j,1)=stats.tstat.t(1);
clear Y X coefic stats
end
bar(alpha_J)
10/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
Remember that you can introduce titles with the command "title', and the text in the
axes with the commands "xlabel", "ylabel"
>>
>>
hist(alpha_J)
meanAlpha=mean(alpha_J);
0.02
0.015
alpha
0.01
0.005
-0.005
-0.01
10
20
30
40
number of mutual fund
50
60
12
10
0
-0.01
-0.005
0.005
0.01
0.015
0.02
11/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
for j=1:55
Y=rMF2(:,j);
X= [rMk2, SMB, HML];
stats=regstats(Y, X,'linear');
coefic=stats.beta;
alphaFF(j,1)=coefic(1)
t_FF(j,1)=stats.tstat.t(1);
clear Y X coefic stats
end
bar(alphaFF)
hist(alphaFF)
12/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
>>
meanAlphaFF=mean(alphaFF);
Alpha`s FF model from Equity Mutual Funds
0.01
0.005
alpha FF
-0.005
-0.01
-0.015
-0.02
-0.025
10
20
30
40
number of mutual fund
50
60
14
12
10
0
-0.025
-0.02
-0.015
-0.01
-0.005
0.005
0.01
13/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
Third, we can analyze the differences between using Jensens Alpha and
Alpha from the FF three factors model.
>>
>>
>>
>>
Diff_alpha=alpha-alphaFF
figure
plot(Diff_alpha,s-r)
title(Difference among Alphas), grid on
0.015
0.01
0.005
-0.005
-0.01
10
20
30
40
50
60
14/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
First, we can sort the mutual funds with the function sort.
>> help sort
SORT for vectors, SORT(X) sorts the elements of X in ascending
order.
>>
[SharpeRank, SharpeRank2]=sort(Sharpe)
You must note that in SharpeRank variable you have the Sharpe Ratio values for each
mutual fund ranked by Sharpe Ratio. But in the variable SharpeRank2 you have the
number (from colums in rMF) of the mutual fund in that position.
>>
>>
>>
[TreynorRank, TreynorRank2]=sort(Treynor)
[alphaRank, alphaRank2]=sort(alpha)
[alphaFFRank, alphaFFRank2]=sort(alphaFF)
Now, we can built a new matrix with the index of each funds in the
rankings computed previously.
>>
TableRank =
25 25 25 25
7 7 24 49
24 24 7 50
15/16
Mutual Funds Evaluation Using Matlab (Master in Finance, University Carlos III)
27
17
19
17 27
27 19
49 49
52
38
37
33
33
33
33
subplot(2,2,1)
bar(Sharpe)
subplot(2,2,2)
bar(Treynor)....
.....
Treynor
Sharpe
0.6
0.03
0.4
0.02
0.2
0.01
-0.2
20
40
60
alpha
0.02
-0.01
20
40
60
40
60
alphaFF
0.01
0
0.01
-0.01
0
-0.01
-0.02
20
40
60
-0.03
20
16/16