全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
682 0
2021-12-24
悬赏 6 个论坛币 未解决
各位大神好:       我想在SAS中实现蒙特卡洛模拟,现有的数据集一个股票的各类数据。想要对该数据集内的股票随机分成10个等市值的组。然后计算这是个组的一个统计量SD。想要通过10000次蒙特卡洛模拟,求出该SD的平均值。
       具体设计的函数如下:

/*分配服从(0,1)分布的随机数*/
data random; set sum_Ref; random=ranuni(1);
proc sort data= random; by qdate random; run;

/*计算Culm_Ref_MktCap*/
data culm; set random; by qdate random;
if first.qdate then Culm_Ref_MktCap=Ref_MktCap; else Culm_Ref_MktCap+Ref_MktCap; run;
proc sort data=culm; by qdate Culm_Ref_MktCap; run;

/*分配decildecile_random*/
data decile_random; set culm;
  if                  Culm_Ref_MktCap <= TRefMC*1/10 then decile_random=1;
else if TRefMC*1/10 < Culm_Ref_MktCap <= TRefMC*2/10 then decile_random=2;
else if TRefMC*2/10 < Culm_Ref_MktCap <= TRefMC*3/10 then decile_random=3;
else if TRefMC*3/10 < Culm_Ref_MktCap <= TRefMC*4/10 then decile_random=4;
else if TRefMC*4/10 < Culm_Ref_MktCap <= TRefMC*5/10 then decile_random=5;
else if TRefMC*5/10 < Culm_Ref_MktCap <= TRefMC*6/10 then decile_random=6;
else if TRefMC*6/10 < Culm_Ref_MktCap <= TRefMC*7/10 then decile_random=7;
else if TRefMC*7/10 < Culm_Ref_MktCap <= TRefMC*8/10 then decile_random=8;
else if TRefMC*8/10 < Culm_Ref_MktCap <= TRefMC*9/10 then decile_random=9;
else                                                      decile_random=10;
by qdate Culm_Ref_MktCap;
run;

/*计算DM_random, DF_random*/
proc sql;
create table sum_random as
select *, sum(MktCap) as DM_random "Decile MktCap", sum(NetFlows) as DF_random "Decile NetFlows"
from decile_random
group by qdate, decile_random
order by qdate, decile_random;
quit;

/*计算DR_random*/
proc sql;
create table ret_random as
select *, sum((MktCap/DM_random)*qret) as DR_random "Decile return (MktCap value weighted)"
from sum_random
group by qdate, decile_random
order by qdate, decile_random;
quit;

/*计算DER_random DEF_random*/
proc sql;
create table attr_random as
select *, (DR_random-MktRet) as DER_random "Decile Excess Return (Decile return over actual MktRet part)",
          ((DF_random/DM_random)-(TF/TM)) as DEF_random "Decile Excess Flow (All Available Flows and MktCap)"
from ret_random
group by qdate, decile_random
order by qdate, decile_random;
quit;

/*汇总DM, DF, DR, DER, DEF*/
proc sql;
create table decile_describe_random as
select distinct decile_random, qdate, DM_random, DF_random, DR_random, DER_random, DEF_random
from attr_random
group by qdate, decile_random
order by qdate, decile_random;
quit;

/*在decile describe中加入TM TF*/
proc sql;
create table info_random as
select *, i.TM, i.TF
from decile_describe_random s
inner join data.Info i on s.qdate=i.qdate
group by s.qdate, s.decile_random
order by s.qdate, s.decile_random;
quit;

/*计算每个季度的SD*/
proc sql;
create table SD_random as
select distinct qdate, decile_random,
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群