Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

http://web.stanford.edu/~xing/statfinbook/functions.

html

Matlab/R functions for "Statistical models and


methods for financial markets"+

Chapter 1
http://web.stanford.edu/~xing/statfinbook/_BookFun/chap1_ana_data.txt
library(MASS)

ret<-read.table("chap1_lm_stock_return.txt", header=T)
ret[,-1]<-ret[,-1]*100
attach(ret)

day<-ret[,1]

### 1. scattor plot


plot(ret[,-1])

format(cor(ret[,-1]), digits=4)

### 2. fit the full model


fit.full<-
lm(msft~aapl+adbe+adp+amd+dell+gtw+hp+ibm+orcl+sunw+yhoo)
summary(fit.full)

### 3. variable selection

dropterm(fit.full, test="F")

fit1<-update(fit.full, .~.-sunw-hp)
dropterm(fit1, test="F")

fit2<-update(fit1, .~.-amd)
dropterm(fit2, test="F")

fit3<-update(fit2, .~.-adp-yhoo)
dropterm(fit3, test="F")

fit.reduce<-lm(msft ~ aapl + adbe + dell + gtw + ibm +


orcl)
summary(fit.reduce)

resstep<-stepwise(ret[,-c(1,10)], ret[,10])
plot(resstep$size, resstep$rss)

### Fig1.3 the summary plot of reduced model --- work in


Splus
### plot fitted results
par(mfrow=c(2,3))
plot(fit.reduce)dev.off()

### plot studentized residuals


studresid<-as.numeric(studres(fit.reduce))
qqnorm(studresid, ylab="Studentized residuals")
qqline(studresid)

### QQ-plot
qqnorm(standresid)
qqline(standresid)

qqnorm(resid)
qqline(resid)

### comparison of studentized and standardized residuals


par(mfrow=c(1, 2))
qqnorm(studresid, ylab="Studentized residuals")
qqline(studresid)
qqnorm(standresid, ylab="Standardized residuals")
qqline(standresid)

### the scatterplot matrix of the reduced model


plot(ret[,c(2, 3, 6, 7, 9, 10, 11)])

Chapter 2
http://web.stanford.edu/~xing/statfinbook/_BookFun/chap2_pca_swap.m
%%% The data file 'day_allswaps.txt' contains the
following daily swap
%%% rates from 7/3/2000 to 7/15/2005.
%%% swp1y swp2y swp3y sw4y sw5y sw7y
sw10y sw30y

%%% read the data file and get the data matrix
data = load('chap2_day_allswaps.txt');

%%% plot the time series


plotmargin = 50;
plot(data(:, 1), 'b'); hold on;
plot(data(:, 2), 'g'); plot(data(:, 3), 'r');
plot(data(:, 4), 'c');
plot(data(:, 5), 'm'); plot(data(:, 6), 'y');
plot(data(:, 7), 'k');
plot(data(:, 8), 'b:'); hold off; legend off;
xlim([1-plotmargin, 1257+plotmargin]);
ylim([0.9, 7.50]); grid off;
set(gca, 'Xtick', [2, 210, 419, 628, 837, 1046, 1257]);
set(gca, 'XtickLabel', ['07/05/00'; '05/03/01'; '03/11/02';
'01/09/03';...
'11/07/03'; '09/10/04'; '07/15/05']);
legend('swp1y', 'swp2y', 'swp3y', 'swp4y','swp5y', 'swp7y',
...
'swp10y', 'swp30y');

ddata=diff(data);
plotmargin = 50;
%subplot('position', [0.05, 0.05, 0.9, 0.9]);
plot(ddata(:, 1), 'b'); hold on;
plot(ddata(:, 2), 'g'); plot(ddata(:, 3), 'r');
plot(ddata(:, 4), 'c');
plot(ddata(:, 5), 'm'); plot(ddata(:, 6), 'y');
plot(ddata(:, 7), 'k');
plot(ddata(:, 8), 'b:'); hold off; legend off;
xlim([1-plotmargin, 1257+plotmargin]);
%ylim([0.9, 7.50]); grid off;
set(gca, 'Xtick', [1, 210, 419, 628, 837, 1046, 1256]);
set(gca, 'XtickLabel', ['07/05/00'; '05/03/01'; '03/11/02';
'01/09/03';...
'11/07/03'; '09/10/04'; '07/15/05']);
legend('swp1y', 'swp2y', 'swp3y', 'swp4y','swp5y', 'swp7y',
...
'swp10y', 'swp30y');

[coeff, eigenvalue, explained] =


pcacov(corrcoef(diff(data)));

mat = [1, 2, 3, 4, 5, 6, 7, 8];


plot(mat, coeff(:,1), '-', 'LineWidth', 2); hold on;
plot(mat, coeff(:,2), '-.', 'LineWidth', 2);
plot(mat, coeff(:,3), ':', 'LineWidth', 2); hold off;
ylim([-0.7, 0.65]); xlim([0.5, 8.5]);
legend('trend', 'tilt', 'convexity');
print -deps fig_multi_swap_comp

pcadata = data*coeff;
plot(pcadata(:,1), '-', 'LineWidth', 1.5); hold on;
plot(pcadata(:,2), '-.', 'LineWidth', 1.5);
plot(pcadata(:,3), ':', 'LineWidth', 1.5); hold off;
xlim([-plotmargin, length(pcadata(:,1))+plotmargin]);
ylim([-5, 22]); grid off;
set(gca, 'Xtick', datats.dates([1, 210, 419, 628, 837,
1046, 1257]));
set(gca, 'XtickLabel', ['07/03/00'; '05/03/01'; '03/11/02';
'01/09/03';...
'11/07/03'; '09/10/04'; '07/15/05']);
legend('Level', 'Slope', 'Curvature');
%%% plot the factor loadings
bar(-coeff(:,1:3), 'grouped')
ylim([-0.7, 0.7])
legend('the 1st', 'the 2nd', 'the 3rd')

Chapter 3
http://web.stanford.edu/~xing/statfinbook/_BookFun/
ex3.2.4_plot_6assets_effifrontier.m
%%% This file plot figures of general information of these
six stocks
%%%% Pfizer Intel Citigroup AmerExpre Exxon
GenMotor

data = load('RawData/chap7_6assets_nodate.txt');

mu=mean(data);
iSigma=inv(cov(data));
ind=ones(1, 6);

B = mu*iSigma*transpose(mu);
A = ind*iSigma*transpose(mu);
C = ind*iSigma*transpose(ind);
D = B*C-A*A;
frontMu = (A/C:0.0001:0.002);
frontSd = sqrt( C/D*(frontMu-A/C).^2 + 1/C)*100;

plot(frontSd/100, frontMu, 'LineWidth', 1.5)


ylim([-0.05, 0.21]/100)
xlim([1.78, 1.91]/100)
ylabel('Monthly log return');
xlabel('Monthly standard deviation')

print -deps chap7_6assets_effifrontier


print -depsc chap7_6assets_effifrontier_color

http://web.stanford.edu/~xing/statfinbook/_BookFun/
ex3.3.4_capm.txt
### This file perform a CAPM analysis for six stocks vs Dow
Jones index
###

data <- read.table('RawData/capm_excessret.txt', header=T)


### A, C, E, G, I, P, Dow

lm.Am <- lm(data$A ~ data$Dow)


lm.C <- lm(data$C ~ data$Dow)
lm.Exx <- lm(data$E ~ data$Dow)
lm.GM <- lm(data$G ~ data$Dow)
lm.In <- lm(data$I ~ data$Dow)
lm.Pf <- lm(data$P ~ data$Dow)
fitted <- cbind(lm.Am$fit, lm.C$fit, lm.Exx$fit, lm.GM$fit,
lm.In$fit, lm.Pf$fit)
write(t(fitted), file="6asseets_lmfitted.txt",ncol=6)

> summary(lm.Am)

Call:
lm(formula = data$A ~ data$Dow)

Residuals:
Min 1Q Median 3Q Max
-0.070844 -0.011195 -0.001957 0.013602 0.040571

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0008688 0.0023900 0.364 0.717
data$Dow 1.2312153 0.1227558 10.030 1.59e-14 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 '
' 1

Residual standard error: 0.01886 on 61 degrees of freedom


Multiple R-Squared: 0.6225, Adjusted R-squared: 0.6163
F-statistic: 100.6 on 1 and 61 DF, p-value: 1.591e-14

> summary(lm.C)

Call:
lm(formula = data$C ~ data$Dow)

Residuals:
Min 1Q Median 3Q Max
-0.040653 -0.013766 -0.001579 0.011850 0.044592

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0008112 0.0026119 0.311 0.757
data$Dow 1.1994953 0.1341545 8.941 1.08e-12 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 '
' 1

Residual standard error: 0.02061 on 61 degrees of freedom


Multiple R-Squared: 0.5672, Adjusted R-squared: 0.5601
F-statistic: 79.94 on 1 and 61 DF, p-value: 1.077e-12
> summary(lm.Exx)

Call:
lm(formula = data$E ~ data$Dow)

Residuals:
Min 1Q Median 3Q Max
-0.054813 -0.012082 -0.002366 0.010403 0.081798

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.002234 0.002625 0.851 0.398166
data$Dow 0.521592 0.134851 3.868 0.000269 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 '
' 1

Residual standard error: 0.02072 on 61 degrees of freedom


Multiple R-Squared: 0.197, Adjusted R-squared: 0.1838
F-statistic: 14.96 on 1 and 61 DF, p-value: 0.0002693

> summary(lm.GM)

Call:
lm(formula = data$G ~ data$Dow)

Residuals:
Min 1Q Median 3Q Max
-0.128126 -0.018814 -0.003893 0.019662 0.081133

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.002410 0.004417 -0.546 0.587
data$Dow 1.440794 0.226845 6.351 2.98e-08 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 '
' 1

Residual standard error: 0.03486 on 61 degrees of freedom


Multiple R-Squared: 0.3981, Adjusted R-squared: 0.3882
F-statistic: 40.34 on 1 and 61 DF, p-value: 2.977e-08

> summary(lm.In)

Call:
lm(formula = data$I ~ data$Dow)

Residuals:
Min 1Q Median 3Q Max
-0.193590 -0.019357 0.000855 0.034688 0.091088
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.004315 0.006597 -0.654 0.515
data$Dow 2.281603 0.338829 6.734 6.63e-09 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 '
' 1

Residual standard error: 0.05207 on 61 degrees of freedom


Multiple R-Squared: 0.4264, Adjusted R-squared: 0.417
F-statistic: 45.34 on 1 and 61 DF, p-value: 6.631e-09

> summary(lm.Pf)

Call:
lm(formula = data$P ~ data$Dow)

Residuals:
Min 1Q Median 3Q Max
-0.054395 -0.016159 0.001901 0.015705 0.047271

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.005210 0.002751 -1.894 0.06300 .
data$Dow 0.459534 0.141307 3.252 0.00187 **
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 '
' 1

Residual standard error: 0.02171 on 61 degrees of freedom


Multiple R-Squared: 0.1478, Adjusted R-squared: 0.1338
F-statistic: 10.58 on 1 and 61 DF, p-value: 0.001869

#### alpha
c(lm.Am$co[1], lm.C$co[1], lm.Exx$co[1], lm.GM$co[1],
lm.In$co[1], lm.Pf$co[1])*10^3
(Intercept) (Intercept) (Intercept) (Intercept) (Intercept)
(Intercept)
0.8687740 0.8112259 2.2339467 -2.4096714 -4.3151270
-5.2100435

#### beta
c(lm.Am$co[2], lm.C$co[2], lm.Exx$co[2], lm.GM$co[2],
lm.In$co[2], lm.Pf$co[2])
1.2312153 1.1994953 0.5215921 1.4407939 2.2816034 0.4595335

#### sharp ratio


c(mean(data$A)/sd(data$A), mean(data$C)/sd(data$C),
mean(data$E)/sd(data$E), mean(data$G)/sd(data$G),
mean(data$I)/sd(data$I), mean(data$P)/sd(data$P))*100
[1] -5.493749 -5.357144 5.045060 -12.081508 -13.235988
-26.398117

mean(data$Dow)/sd(data$Dow)
# -0.1057871

#### Treynor index


c(mean(data$A), mean(data$C), mean(data$E), mean(data$G),
mean(data$I),
mean(data$P)) / c(lm.Am$co[2], lm.C$co[2], lm.Exx$co[2],
lm.GM$co[2],
lm.In$co[2], lm.Pf$co[2])*1000
-1.358833 -1.388150 2.218482 -3.736917 -3.955725 -
13.402135

#### sigma_re^2 * 10000


> c(var(lm.Am$re), var(lm.C$re), var(lm.Exx$re),
var(lm.GM$re), var(lm.In$re), var(lm.Pf$re))*10000
[1] 3.500740 4.181059 4.224559 11.954549 26.670876
4.638747

#### beta^2 * var(dow) * 10000


c(lm.Am$co[2], lm.C$co[2], lm.Exx$co[2], lm.GM$co[2],
lm.In$co[2], lm.Pf$co[2]
)^2 * var(data$Dow) * 10000
5.7731609 5.4795226 1.0361141 7.9058644 19.8255822
0.8042295

c(mean(data$A),mean(data$C),mean(data$E),mean(data$G),mean(
data$I),mean(data$P),mean(data$Dow))
#-0.001673016 -0.001665079 0.001157143 -0.005384127 -
0.009025397 -0.006158730 -0.002064456

c(sd(data$A),sd(data$C),sd(data$E),sd(data$G),sd(data$I),sd
(data$P),sd(data$Dow))
# [1] 0.03045308 0.03108148 0.02293616 0.04456502
0.06818831 0.02333019 0.01951519

########## F-stat
n<-63; q<-6
alphahat<-c(lm.Am$co[1], lm.C$co[1], lm.Exx$co[1],
lm.GM$co[1], lm.In$co[1], lm.Pf$co[1])
tmp <- cbind(lm.Am$re, lm.C$re, lm.Exx$re, lm.GM$re,
lm.In$re, lm.Pf$re)
bSigmahat <- t(tmp)%*%tmp/n
nu <- t(alphahat) %*% solve(bSigmahat) %*% alphahat
de <- 1 + mean(data$Dow)^2/ (var(data$Dow)*(n-1)/n)
stat <- (n-q-1)/q * nu / de
1.070011

qf(0.90, 6, n-q-1)
[1] 1.882094

http://web.stanford.edu/~xing/statfinbook/_BookFun/
ex3.4.2_factorana.txt

### This file perform a factor analysis for six stocks

data <- read.table('RawData/capm_excessret.txt', header=T)

#data <- read.table('RawData/chap7_6assets.txt',header=T)


ret <- cbind(data$Am, data$Ci, data$Exx, data$Gen,
data$Int, data$Pf)

# these two
ana1<-factanal(ret, factors=2, rotation="none")
rot1<-varimax(loadings(ana1), normalize = TRUE)
# these two lines are same as the following line
# ana2rot2<-factanal(ret, factors=2,
rotation="varimax")

> ana1

Call:
factanal(x = ret, factors = 2, rotation = "none")

Uniquenesses:
[1] 0.407 0.406 0.501 0.668 0.287 0.758

Loadings:
Factor1 Factor2
[1,] 0.755 0.152
[2,] 0.769
[3,] 0.341 0.619
[4,] 0.574
[5,] 0.758 -0.371
[6,] 0.297 0.392

Factor1 Factor2
SS loadings 2.271 0.701
Proportion Var 0.379 0.117
Cumulative Var 0.379 0.495

Test of the hypothesis that 2 factors are sufficient.


The chi square statistic is 5.88 on 4 degrees of freedom.
The p-value is 0.209
$loadings

Loadings:
Factor1 Factor2
[1,] 0.653 0.409
[2,] 0.705 0.311
[3,] 0.101 0.699
[4,] 0.521 0.246
[5,] 0.840
[6,] 0.140 0.472

Factor1 Factor2
SS loadings 1.930 1.043
Proportion Var 0.322 0.174
Cumulative Var 0.322 0.495

$rotmat
[,1] [,2]
[1,] 0.9356250 0.3529955
[2,] -0.3529955 0.9356250

#####################################

matB <- ana1$load


matBBtrans<-ana1$load%*%t(ana1$load)
matV<- diag(1-diag(ana1$load%*%t(ana1$load)))
ana1$corr - matBBtrans - matV

# 1-diag(ana1$load%*%t(ana1$load))
[1] 0.4069552 0.4063576 0.5008067 0.6681062 0.2873461
0.7580388

# ana1$corr - matBBtrans - matV


[,1] [,2] [,3]
[,4] [,5]
[1,] -1.110223e-16 2.505239e-02 -3.165067e-04 3.674059e-
02 -2.077318e-02
[2,] 2.505239e-02 -1.110223e-16 -3.298060e-02 -8.381984e-
02 5.476723e-03
[3,] -3.165067e-04 -3.298060e-02 -2.220446e-16 5.219531e-
02 5.037665e-03
[4,] 3.674059e-02 -8.381984e-02 5.219531e-02 2.220446e-
16 2.974841e-02
[5,] -2.077318e-02 5.476723e-03 5.037665e-03 2.974841e-
02 -2.220446e-16
[6,] -6.110514e-02 8.566058e-02 1.241495e-02 -6.009117e-
02 -2.135001e-03
[,6]
[1,] -6.110514e-02
[2,] 8.566058e-02
[3,] 1.241495e-02
[4,] -6.009117e-02
[5,] -2.135001e-03
[6,] 2.220446e-16

http://web.stanford.edu/~xing/statfinbook/_BookFun/ex3.4.2_factorana.txt
### This file perform a factor analysis for six stocks

data <- read.table('RawData/capm_excessret.txt', header=T)

#data <- read.table('RawData/chap7_6assets.txt',header=T)


ret <- cbind(data$Am, data$Ci, data$Exx, data$Gen,
data$Int, data$Pf)

# these two
ana1<-factanal(ret, factors=2, rotation="none")
rot1<-varimax(loadings(ana1), normalize = TRUE)
# these two lines are same as the following line
# ana2rot2<-factanal(ret, factors=2,
rotation="varimax")

> ana1

Call:
factanal(x = ret, factors = 2, rotation = "none")

Uniquenesses:
[1] 0.407 0.406 0.501 0.668 0.287 0.758

Loadings:
Factor1 Factor2
[1,] 0.755 0.152
[2,] 0.769
[3,] 0.341 0.619
[4,] 0.574
[5,] 0.758 -0.371
[6,] 0.297 0.392

Factor1 Factor2
SS loadings 2.271 0.701
Proportion Var 0.379 0.117
Cumulative Var 0.379 0.495

Test of the hypothesis that 2 factors are sufficient.


The chi square statistic is 5.88 on 4 degrees of freedom.
The p-value is 0.209

$loadings

Loadings:
Factor1 Factor2
[1,] 0.653 0.409
[2,] 0.705 0.311
[3,] 0.101 0.699
[4,] 0.521 0.246
[5,] 0.840
[6,] 0.140 0.472

Factor1 Factor2
SS loadings 1.930 1.043
Proportion Var 0.322 0.174
Cumulative Var 0.322 0.495

$rotmat
[,1] [,2]
[1,] 0.9356250 0.3529955
[2,] -0.3529955 0.9356250

#####################################

matB <- ana1$load


matBBtrans<-ana1$load%*%t(ana1$load)
matV<- diag(1-diag(ana1$load%*%t(ana1$load)))
ana1$corr - matBBtrans - matV

# 1-diag(ana1$load%*%t(ana1$load))
[1] 0.4069552 0.4063576 0.5008067 0.6681062 0.2873461
0.7580388

# ana1$corr - matBBtrans - matV


[,1] [,2] [,3]
[,4] [,5]
[1,] -1.110223e-16 2.505239e-02 -3.165067e-04 3.674059e-
02 -2.077318e-02
[2,] 2.505239e-02 -1.110223e-16 -3.298060e-02 -8.381984e-
02 5.476723e-03
[3,] -3.165067e-04 -3.298060e-02 -2.220446e-16 5.219531e-
02 5.037665e-03
[4,] 3.674059e-02 -8.381984e-02 5.219531e-02 2.220446e-
16 2.974841e-02
[5,] -2.077318e-02 5.476723e-03 5.037665e-03 2.974841e-
02 -2.220446e-16
[6,] -6.110514e-02 8.566058e-02 1.241495e-02 -6.009117e-
02 -2.135001e-03
[,6]
[1,] -6.110514e-02
[2,] 8.566058e-02
[3,] 1.241495e-02
[4,] -6.009117e-02
[5,] -2.135001e-03
[6,] 2.220446e-16

You might also like