Professional Documents
Culture Documents
Matlab-STATISTICAL MODELS AND METHODS FOR FINANCIAL MARKETS
Matlab-STATISTICAL MODELS AND METHODS FOR FINANCIAL MARKETS
html
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]
format(cor(ret[,-1]), digits=4)
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")
resstep<-stepwise(ret[,-c(1,10)], ret[,10])
plot(resstep$size, resstep$rss)
### QQ-plot
qqnorm(standresid)
qqline(standresid)
qqnorm(resid)
qqline(resid)
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');
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');
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;
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
###
> 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
> 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
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
> 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
> 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
> 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
#### 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
mean(data$Dow)/sd(data$Dow)
# -0.1057871
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
# 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
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
#####################################
# 1-diag(ana1$load%*%t(ana1$load))
[1] 0.4069552 0.4063576 0.5008067 0.6681062 0.2873461
0.7580388
http://web.stanford.edu/~xing/statfinbook/_BookFun/ex3.4.2_factorana.txt
### This file perform a factor analysis for six stocks
# 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
$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
#####################################
# 1-diag(ana1$load%*%t(ana1$load))
[1] 0.4069552 0.4063576 0.5008067 0.6681062 0.2873461
0.7580388