收益波动率的计算SAS—How to Calculate Historical Volatility
(2014-03-11 14:08:30)
标签:
教育 |
分类: 03SAS数据处理 |
今天在读WEINING
ZHANG(张维宁)老师新发表的文章——Executive Pay–Performance Sensitivity and
Litigation(Journal:Contemporary Accounting
Research,forthcoming,2014)时,张老师用到了收益波动去衡量公司价值的变动delta_Wealth,也就是公司金融中经常要用到的股票收益波动率指标(Historical
Volatility),下面就是这个指标的计算方法
(SAS_Script,Select移动加权平均的方法衡量).
filename mac "d:\stock\compufin\宏文本\全部A股.txt" ;
libname stoindiv 'D:\stock\Stoindiv';
%macro b(aa,bb,cc);
data stoindiv.sma_&aa(keep=date);
set stoindiv.a1a0001;
data stoindiv.sma_&bb;
set stoindiv.sma_&bb;
delete;
%macro a(x,y);
data a;
set stoindiv.a&x;
r_1=log(clpr)-log(lag(clpr));
clpr_1= clpr*(1+divrat+Rigrat+reisvol/lag(Shrout))-rigpr*Rigrat-reispr*reisvol/lag(Shrout)+Divamt;
if clpr_1=. then clpr_1=clpr;
r_2=log(clpr_1)-log(lag(clpr_1));
data a(drop=r_2 );
set a nobs=nobs;
n=nobs;
if exdt=. then r_2=0;
if exdt^=. then r_1=0;
r_1=r_1+r_2;
id=1;
data b(keep=id clpr_begin clpr_end);
retain clpr_begin clpr_end;
set a end=lastobs;
if _n_=1 then clpr_begin=clpr_1;
if lastobs then do;
clpr_end=clpr_1;
output;
end;
data a(drop=clpr_begin clpr_end r_mean);
merge a b;
by id;
r_mean=(log(clpr_end)-log(clpr_begin))/n;
r=r_1-r_mean;
rr=r*r;
set stoindiv.a1a0001;
data stoindiv.sma_&bb;
set stoindiv.sma_&bb;
delete;
%macro a(x,y);
data a;
set stoindiv.a&x;
r_1=log(clpr)-log(lag(clpr));
clpr_1= clpr*(1+divrat+Rigrat+reisvol/lag(Shrout))-rigpr*Rigrat-reispr*reisvol/lag(Shrout)+Divamt;
if clpr_1=. then clpr_1=clpr;
r_2=log(clpr_1)-log(lag(clpr_1));
data a(drop=r_2 );
set a nobs=nobs;
n=nobs;
if exdt=. then
if exdt^=. then r_1=0;
r_1=r_1+r_2;
id=1;
data b(keep=id clpr_begin clpr_end);
retain clpr_begin clpr_end;
set a end=lastobs;
if _n_=1 then clpr_begin=clpr_1;
if lastobs then do;
clpr_end=clpr_1;
output;
end;
data a(drop=clpr_begin clpr_end r_mean);
merge a b;
by id;
r_mean=(log(clpr_end)-log(clpr_begin))/n;
r=r_1-r_mean;
rr=r*r;
data a;
set a;
sum+rr;
data b(keep=date z&x);
merge a a (firstobs=&cc rename=(sum=sum_1));
z&x=(sum_1-sum)/(&aa-1);
z&x=sqrt(z&x);
if z&x=. then delete;
proc sort data=b;
by date;
data stoindiv.sma_&aa;
merge stoindiv.sma_&aa b;
by date;
set a;
sum+rr;
data b(keep=date z&x);
merge a a (firstobs=&cc rename=(sum=sum_1));
z&x=(sum_1-sum)/(&aa-1);
z&x=sqrt(z&x);
if z&x=. then delete;
proc sort data=b;
by date;
data stoindiv.sma_&aa;
merge stoindiv.sma_&aa b;
by date;
proc means data=b noprint;
var z&x;
output out=c max=max mean=mean min=min;
data c(drop=_type_);
length hstocd $ 8;
set c;
hstocd="&x";
hcomnam="&y";
data stoindiv.sma_&bb;
set stoindiv.sma_&bb c;
%mend a;
%include mac ;
%mend b;
%b(05,051,6);
%b(10,101,11);
%b(20,201,21);
run;
var z&x;
output out=c max=max
data c(drop=_type_);
length hstocd $ 8;
set c;
hstocd="&x";
hcomnam="&y";
data stoindiv.sma_&bb;
set stoindiv.sma_&bb c;
%mend a;
%include mac
%mend b;
%b(05,051,6);
%b(10,101,11);
%b(20,201,21);
run;
宏文本的制作:
libname dd 'D:\可保存公司资料';
DATA dd.TRD_Co (Label="公司文件");
Infile 'D:\可保存公司资料\bynwxozp\TRD_Co.txt' encoding="utf-8" delimiter = '09'x Missover Dsd lrecl=32767 firstobs=2;
Format Cuntrycd 5.;
Format Stkcd $6.;
Format Stknme $20.;
Format Conme $100.;
Format Conme_en $100.;
Format Indcd $4.;
Format Indnme $50.;
Format Nindcd $10.;
Format Nindnme $50.;
Format Nnindcd $10.;
Format Nnindnme $50.;
Format Estbdt $10.;
Format Listdt $10.;
Format Favaldt $10.;
Format Curtrd $3.;
Format Ipoprm 10.4;
Format Ipoprc 10.4;
Format Ipocur $3.;
Format Nshripo 14.;
Format Parvcur $3.;
Format Ipodt $10.;
Format Parval 8.3;
Format Sctcd 1.;
Format Statco $1.;
Format Crcd $6.;
Format Statdt $10.;
Format Commnt $200.;
Format Markettype 10.;
Informat Cuntrycd 5.;
Informat Stkcd $6.;
Informat Stknme $20.;
Informat Conme $100.;
Informat Conme_en $100.;
Informat Indcd $4.;
Informat Indnme $50.;
Informat Nindcd $10.;
Informat Nindnme $50.;
Informat Nnindcd $10.;
Informat Nnindnme $50.;
Informat Estbdt $10.;
Informat Listdt $10.;
Informat Favaldt $10.;
Informat Curtrd $3.;
Informat Ipoprm 10.4;
Informat Ipoprc 10.4;
Informat Ipocur $3.;
Informat Nshripo 14.;
Informat Parvcur $3.;
Informat Ipodt $10.;
Informat Parval 8.3;
Informat Sctcd 1.;
Informat Statco $1.;
Informat Crcd $6.;
Informat Statdt $10.;
Informat Commnt $200.;
Informat Markettype 10.;
Label Cuntrycd="国家代码";
Label Stkcd="证券代码";
Label Stknme="证券简称";
Label Conme="公司全称";
Label Conme_en="公司英文全称";
Label Indcd="行业代码A";
Label Indnme="行业名称A";
Label Nindcd="行业代码B";
Label Nindnme="行业名称B";
Label Nnindcd="行业代码C";
Label Nnindnme="行业名称C";
Label Estbdt="公司成立日期";
Label Listdt="上市日期";
Label Favaldt="数据库最早交易记录的日期";
Label Curtrd="数据库中交易数据的计量货币";
Label Ipoprm="股票发行溢价";
Label Ipoprc="发行价格";
Label Ipocur="发行价格的计量货币";
Label Nshripo="发行数量";
Label Parvcur="股票面值的计量货币";
Label Ipodt="发行日期";
Label Parval="股票面值";
Label Sctcd="区域码";
Label Statco="公司活动情况";
Label Crcd="AB股交叉码";
Label Statdt="情况变动日";
Label Commnt="H股交叉码";
Label Markettype="市场类型";
Input Cuntrycd Stkcd $ Stknme $ Conme $ Conme_en $ Indcd $ Indnme $ Nindcd $ Nindnme $ Nnindcd $ Nnindnme $ Estbdt $ Listdt $ Favaldt $ Curtrd $ Ipoprm Ipoprc Ipocur $ Nshripo Parvcur $ Ipodt $ Parval Sctcd Statco $ Crcd $ Statdt $ Commnt $ Markettype ;
Run;
Infile 'D:\可保存公司资料\bynwxozp\TRD_Co.txt' encoding="utf-8" delimiter = '09'x Missover Dsd lrecl=32767 firstobs=2;
Format Cuntrycd 5.;
Format Stkcd $6.;
Format Stknme $20.;
Format Conme $100.;
Format Conme_en $100.;
Format Indcd $4.;
Format Indnme $50.;
Format Nindcd $10.;
Format Nindnme $50.;
Format Nnindcd $10.;
Format Nnindnme $50.;
Format Estbdt $10.;
Format Listdt $10.;
Format Favaldt $10.;
Format Curtrd $3.;
Format Ipoprm 10.4;
Format Ipoprc 10.4;
Format Ipocur $3.;
Format Nshripo 14.;
Format Parvcur $3.;
Format Ipodt $10.;
Format Parval 8.3;
Format Sctcd 1.;
Format Statco $1.;
Format Crcd $6.;
Format Statdt $10.;
Format Commnt $200.;
Format Markettype 10.;
Informat Cuntrycd 5.;
Informat Stkcd $6.;
Informat Stknme $20.;
Informat Conme $100.;
Informat Conme_en $100.;
Informat Indcd $4.;
Informat Indnme $50.;
Informat Nindcd $10.;
Informat Nindnme $50.;
Informat Nnindcd $10.;
Informat Nnindnme $50.;
Informat Estbdt $10.;
Informat Listdt $10.;
Informat Favaldt $10.;
Informat Curtrd $3.;
Informat Ipoprm 10.4;
Informat Ipoprc 10.4;
Informat Ipocur $3.;
Informat Nshripo 14.;
Informat Parvcur $3.;
Informat Ipodt $10.;
Informat Parval 8.3;
Informat Sctcd 1.;
Informat Statco $1.;
Informat Crcd $6.;
Informat Statdt $10.;
Informat Commnt $200.;
Informat Markettype 10.;
Label Cuntrycd="国家代码";
Label Stkcd="证券代码";
Label Stknme="证券简称";
Label Conme="公司全称";
Label Conme_en="公司英文全称";
Label Indcd="行业代码A";
Label Indnme="行业名称A";
Label Nindcd="行业代码B";
Label Nindnme="行业名称B";
Label Nnindcd="行业代码C";
Label Nnindnme="行业名称C";
Label Estbdt="公司成立日期";
Label Listdt="上市日期";
Label Favaldt="数据库最早交易记录的日期";
Label Curtrd="数据库中交易数据的计量货币";
Label Ipoprm="股票发行溢价";
Label Ipoprc="发行价格";
Label Ipocur="发行价格的计量货币";
Label Nshripo="发行数量";
Label Parvcur="股票面值的计量货币";
Label Ipodt="发行日期";
Label Parval="股票面值";
Label Sctcd="区域码";
Label Statco="公司活动情况";
Label Crcd="AB股交叉码";
Label Statdt="情况变动日";
Label Commnt="H股交叉码";
Label Markettype="市场类型";
Input Cuntrycd Stkcd $ Stknme $ Conme $ Conme_en $ Indcd $ Indnme $ Nindcd $ Nindnme $ Nnindcd $ Nnindnme $ Estbdt $ Listdt $ Favaldt $ Curtrd $ Ipoprm Ipoprc Ipocur $ Nshripo Parvcur $ Ipodt $ Parval Sctcd Statco $ Crcd $ Statdt $ Commnt $ Markettype ;
Run;
data asss;
set dd.TRD_Co;
a='%a(';
b=',';
c=');' ;
file "Stkffff.txt" ;
这个就是宏文本,存储于 'D:\可保存公司资料\';
mac=a||stkcd||b||stknme||c; 这个是格式
put mac ;
run;