440 likes | 475 Views
练习 1. clear x=0:0.1:10; y=sin(x); plot(x,y,'-mo','LineWidth',2,'MarkerEdgeColor','m','MarkerFaceColor',[.49 0.7 .63],'MarkerSize',8). 练习 2. 摘自: John F. Nash Jr, The bargaining problem, Econometrica, 18, 1950, 155-162 比尔与杰克如何交换手中的商品,使得两人效用的乘积最大,并且交换后两个人的效用都得到提高。. 数据的输入与输出 - 运用 Excel. 输入:
E N D
练习1 • clear • x=0:0.1:10; • y=sin(x); • plot(x,y,'-mo','LineWidth',2,'MarkerEdgeColor','m','MarkerFaceColor',[.49 0.7 .63],'MarkerSize',8)
练习2 • 摘自:John F. Nash Jr, The bargaining problem, Econometrica, 18, 1950, 155-162 • 比尔与杰克如何交换手中的商品,使得两人效用的乘积最大,并且交换后两个人的效用都得到提高。
数据的输入与输出-运用Excel • 输入: • x=xlsread('index'); t = datenum('30-Dec-1899') + x(:,1);y=x(:,2); plot(t,y), datetick('x',23); • 输出: • clear • m = rand(100,4); • header = 'my data'; • colnames = {'Ch1','Ch2','Ch3','Ch4'}; • xlswrite(m,header,colnames,'myfile.xls');
数据描述和数据预处理 • 描述性统计 • 频率分布图 • 相对频率分布图 • 累计频率分布图 • 样本密度函数图 • 随机性检验 • 正态性检验
频率分布图 • 步骤1:确定区间端点、分组数、组距。 • 区间端点:x1=min(X), x2=max(X), a=x1-e, b=x2+e • 分组数m=1.87*(n-1)2/5 • 组距d=(b-a)/m • n为样本容量,e根据情形可取一个较小的数值
频率分布图 • 步骤2:计算分组频数和频率,用m-1个分点将区间[a,b]等分成m个区间, 计算样本落在每个区间的个数 • 步骤3:画出频率分布图或相对频率分布图或累计频率分布或样本概率密度函数
运用Matlab • % frequency graph • clear • a1=xlsread('index'); • a2=a1(:,2); • x=price2ret(a2); • n=length(x); • m=round(1.87*(n-1)^0.4); • zeta=min(abs(x))/10; • min=min(x)-zeta; • max=max(x)+zeta; • y=linspace(min,max,m+1); • subplot(2,2,1); • hist(x,y); • title('frequency');
运用Matlab(续) • subplot(2,2,3); • x1=hist(x,y)./(n*(y(2)-y(1))); • plot(y,x1,'r-'); • title('probability density'); • subplot(2,2,2); • x2=hist(x,y)./n; • bar(y,x2); • title('relative frequency') • subplot(2,2,4); • x3=cumsum(x2); • plot(y,x3,'g-'); • title('cumulative probability');
游程检验 • clear • [z,p]=runtest(randn(1000,1),0.05)
描述性统计和正态性检验 • function [D,S]=discription(x) • %input; x is a matrix, and each colummn stands for a variable • Minimium=min(x); • Maximium=max(x); • Mean=mean(x); • Standard_deviation=std(x); • Skewness=skewness(x); • Kurtosis=kurtosis(x); • if size(x,1)<30 • error('too few observations') • end • for i=1:size(x,2) • [h(i),p(i),Jarque_Bera(i)]=jbtest(x(:,i)); • end • D=[Minimium;Maximium;Mean;Standard_deviation;Skewness;Kurtosis]; • S=[Jarque_Bera;p;h];
概率统计 • 常见分布的概率密度函数
用历史数据计算股票的贝塔-线性回归 • clear • x=xlsread('example001'); • x=price2ret(x); • [Coefficients, S_err, XTXI, R_sq, F_val, Coef_stats, Y_hat, residuals, covariance] = mregress(x(:,1), x(:,2), 1);
输出解释 • Coefficients = -0.0007 1.1360 • S_err = 0.0164 • R_sq = 0.5015 • F_val = • 1.0e+003 * • 1.0231 0 • Coef_stats = • -0.0007 0.0005 -1.4210 0.1556 • 1.1360 0.0355 31.9854 0
练习 • 计算数据exercise01中5只股票的贝塔值。 • 计算数据exercise01中5只股票的相关系数。 • 检验数据exercise01中5只股票收益率的正态性。 • 画出其中5只股票平均收益率的样本概率密度函数
回归中常见的问题 • 多重共线性:导致系数的估计不准 • 异方差:导致系数的方差(标准误差)估计有偏 • 自相关:导致系数的标准误差会被低估 • 伪回归:导致虚假的关系
伪回归的例子 • %program for spurious regression with respect to non-stationary variables • k=1000; • for j=1:k • n=500;t=(1:n)';x1=cumsum(randn(n,1));y1=cumsum(randn(n,1)); • [Coefficients, S_err, XTXI, R_sq, F_val, Coef_stats, Y_hat, residuals, covariance]=mregress(y1, x1, 1); • R21(j)=R_sq;tsta1(j)=Coef_stats(2,3); • end • rr2=linspace(0,1,50);ttsta=linspace(-10,10,50); • subplot(3,1,1) • plot(x1,y1,'r.') • subplot(3,1,2) • nR21=hist(R21,rr2);bar(rr2,nR21);ntsta1=hist(tsta1,ttsta); • subplot(3,1,3) • bar(ttsta,ntsta1)
单位根检验 • [crit,stat]=myunitroot(x,meandrift,timetrend,lag) • %input: • %x: must be a vector • %meandrift:must be 0(no drift) or 1 (drift) • %timetrend:must be 0 (no time trend)or 1(time trend) • %lag:must be the maximium lag time
clear • a=xlsread('test1001');%input date from 000016-000020 in Shenzhen stock market • a=a(1:22:end,2:end);%monthly date • c=price2ret(a,[],'periodic');%monthly returns • v=cov(c);% covariance among chosen stocks • ER=mean(c);% expected returns of stocks • Ae=[ER;ones(1,length(v))]; • Be=[0.05;1]; • W=quadprog(v,[],[],[],Ae,Be)
clear • a=xlsread('test1001');%input date from 000016-000020 in Shenzhen stock market • a=a(1:22:end,2:end);%monthly date • c=price2ret(a,[],'periodic');%monthly returns • %c=c(:,1:4);v=cov(c);% covariance among chosen stocks • ER=mean(c);% expected returns of stocks • n=length(v);Ae=[ER;ones(1,n)]; • re=min(ER):0.0002:max(ER); • for i=1:length(re) • [W1(:,i),fv1(i)]=quadprog(v,zeros(n,1),-eye(n),zeros(n,1),Ae,[re(i);1]); • [W2(:,i),fv2(i)]=quadprog(v,zeros(n,1),[],[],Ae,[re(i);1]); • end • stdp1=sqrt(2*fv1);stdp2=sqrt(2*fv2);h=figure;set(h,'color','w') • plot(stdp1,re,'b-*');hold on;plot(stdp2,re,'r-o') • plot(std(c),ER,'kpentagram') • legend('不允许卖空','允许卖空','单个股票的风险和收益',3) • xlabel('standard deviation') • ylabel('return')
clear • a=xlsread('test1001');%input date from 000016-000020 in Shenzhen stock market • a=a(1:22:end,2:end);%monthly date • c=price2ret(a,[],'periodic');%monthly returns • %c=c(:,1:4); • V=cov(c);% covariance among chosen stocks • er=mean(c);% expected returns of stocks • N=30;n=size(c,2);boundwt=[-0.1*ones(1,n);1.1*ones(1,n)]; • [Portrisk, Portreturn, Portwts] = frontcon(er,V, N,[],boundwt); • [Portrisk1, Portreturn1, Portwts1] = frontcon(er,V, N); • plot(Portrisk,Portreturn,'r-o') • hold on • plot(Portrisk1,Portreturn1,'b-*') • xlabel('standard deviation') • ylabel('expected return') • legend('short sale is permitted','no short sale',2)
%draw return-risk relationship graph for available portfolios • clear • a=xlsread('test1001');%input date from 000016-000020 in Shenzhen stock market • a=a(1:22:end,2:end);%monthly date • c=price2ret(a,[],'periodic');%monthly returns • v=cov(c);% covariance among chosen stocks • ER=mean(c);% expected returns of stocks • n=2000;nn=0.2;m=1; • w1=[0.5+m*rand(n,1),randn(n,1)*nn,randn(n,1)*nn,randn(n,1)*nn];w1=[w1,1-sum(w1,2)]; • w2=[randn(n,1)*nn,0.5+m*rand(n,1),randn(n,1)*nn,randn(n,1)*nn];w2=[w2,1-sum(w2,2)]; • w3=[randn(n,1)*nn,randn(n,1)*nn,0.5+m*rand(n,1),randn(n,1)*nn];w3=[w3,1-sum(w3,2)]; • w4=[randn(n,1)*nn,randn(n,1)*nn,randn(n,1)*nn,0.5+m*rand(n,1)];w4=[w4,1-sum(w4,2)]; • w5=[randn(n,1)*nn,randn(n,1)*nn,randn(n,1)*nn,0.5+m*rand(n,1)];w5=[1-sum(w5,2),w5];
for i=1:n • sig1(i)=sqrt(w1(i,:)*v*w1(i,:)');sig2(i)=sqrt(w2(i,:)*v*w2(i,:)'); • sig3(i)=sqrt(w3(i,:)*v*w3(i,:)');sig4(i)=sqrt(w4(i,:)*v*w4(i,:)'); • sig5(i)=sqrt(w5(i,:)*v*w5(i,:)');er1(i)=w1(i,:)*ER';er2(i)=w2(i,:)*ER'; • er3(i)=w3(i,:)*ER';er4(i)=w4(i,:)*ER';er5(i)=w5(i,:)*ER'; • end • plot(sig1.^2,er1,'ro'),hold on,plot(sig2.^2,er2,'b*') • plot(sig3.^2,er3,'g<'),plot(sig4.^2,er4,'ko'),plot(sig5.^2,er5,'y.') • xlabel('variance'),ylabel('expected return')
%construct efficient frontier • e=ER';n=size(c,2);A=ones(1,n)*inv(v)*e;B=e'*inv(v)*e; • C=ones(1,n)*inv(v)*ones(n,1);D=B*C-A^2; • g=1/D*(B*inv(v)*ones(n,1)-A*inv(v)*e); • h=1/D*(C*inv(v)*e-A*inv(v)*ones(n,1)); • varp=[];Erp=(0.005:0.001:0.02)'; • for Er=Erp' • varp1=(g+h*Er)'*v*(g+h*Er); • varp=[varp;varp1]; • end • plot(varp,Erp,'-mo','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 1 .63],'MarkerSize',8) • legend('000016>50%','000017>50%','000018>50%','000019>50%','000020>50%','efficient frontier')
Price2ret(x,[],’periodic’) • X=xlsread(‘ewiruoiew’); • X=x(2000:end,:) • X(isnan(x))=0
clear • x=xlsread('exercise01'); • x=x(2800:end,2:2:end); • x(isnan(x))=0;y=price2ret(x); • for i=1:5 • [Coefficients, S_err, XTXI, R_sq, F_val, Coef_stats, Y_hat, residuals, covariance] = mregress(y(:,1+i), y(:,1), 1); • beta(i)=Coefficients(2); • end • Corrcoef(y) • Z=y(:,2:end) • U=Mean(z,2)
function y=myreturn(x,k) • %k=0 denotes log return,k=1 denotes simple return • x1=x(2:end,:); • x2=x(1:(end-1),:); • If k==0 • y=log(x1./x2); • elseif k==1 • y=(x1-x2)./x2 • else error(‘something wrong with k’) • end
clear • x=xlsread(‘shindex’); • y=x(:,2); • r=myreturn(y,1); • plot(x(2:end,1)+ datenum('30-Dec-1899') ,r) • datetick(‘x’,23)
mean(r) • std(r) • skewness(r) • kurtosis(r) • min(r) • max(r)
function [D,S]=discription(x) • %input; x is a matrix, and each colummn stands for a variable • Minimium=min(x); • Maximium=max(x); • Mean=mean(x); • Standard_deviation=std(x); • Skewness=skewness(x); • Kurtosis=kurtosis(x); • if size(x,1)<30 • error('too few observations') • end • for i=1:size(x,2) • [h(i),p(i),Jarque_Bera(i)]=jbtest(x(:,i)); • end • D=[Minimium;Maximium;Mean;Standard_deviation;Skewness;Kurtosis]; • S=[Jarque_Bera;p;h];
clear • x=randn(1000,2); • corrcoef(x) • [Coefficients, S_err, XTXI, R_sq, F_val, Coef_stats, Y_hat, residuals, covariance] = mregress(x(:,1), x(:,2), 1);
clear • x=xlsread('shindex');x=x(1:1000,:); • y=x(:,2); • r=myreturn(y,1); • plot(x(2:end,1)+ datenum('30-Dec-1899') ,r) • datetick('x',23) • z=x(2:end,1); • g=myweekday(z); • for i=1:length(z) • D(i,g(i))=1; • end • [Coefficients, S_err, XTXI, R_sq, F_val, Coef_stats, Y_hat, residuals, • covariance] = mregress(r,D, 0);
clear • x=xlsread('shindex'); • y=x(:,2); • r=myreturn(y,1); • plot(x(2:end,1)+ datenum('30-Dec-1899') ,r) • datetick('x',23) • z=x(2:end,1); • g=mymonth(z); • for i=1:length(z) • D(i,g(i))=1; • end • [Coefficients, S_err, XTXI, R_sq, F_val, Coef_stats, Y_hat, residuals,covariance] = mregress(r,D, 0);