加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

STATA数据模拟中种子的作用(Seeds)

(2014-05-16 16:02:08)
标签:

体育

分类: 04STATA数据处理
种子的设置关系到模拟的结果,一定要设置好。
设置好了种子,下次模拟结果保持一致,无论repeat多少次;
还有,在一个程序中,如果有多次的  gen=随机函数,那么,前后两次的结果是不一样的,除非你在每一条的gen语句前定义一样的SEEDS,例如以下例子的y0105 and y0106。

SIMULATIONS EXAMPLE:

clear 
set obs 10000

set seed 1000000

gen  y0101=0+invnormal(uniform())*1

set seed 1000000

gen  y0102=0+invnormal(uniform())*1

set seed 100002

gen  y0103=0+invnormal(uniform())*1

set seed 100002

gen  y0104=0+invnormal(uniform())*1


set seed 10000003

gen  y0105=0+invnormal(uniform())*1


gen  y0106=0+invnormal(uniform())*1

summ y0101-y0106



You can just call rng(mySeed) to set the seed for the global stream (tested in Matlab R2011b). This affects the rand, randn, and randi functions.

The same page that James linked to lists this as the recommended alternative to various old methods (see the middle cell of the right column of the table).

Here's some example code:

format long;             % Display numbers with full precision
format compact;          % Get rid of blank lines between output
mySeed = 10;
rng(mySeed);             % Set the seed
disp(rand([1,3]));
disp(randi(10,[1,10]));
disp(randn([1,3]));
disp(' ');
rng(mySeed);             % Set the seed again to duplicate the results
disp(rand([1,3]));
disp(randi(10,[1,10]));
disp(randn([1,3]));
Its output is:

   0.771320643266746   0.020751949359402   0.633648234926275
     8     5     3     2     8     2     1     7    10     1
   0.060379730526407   0.622213879877005   0.109700311365407

   0.771320643266746   0.020751949359402   0.633648234926275
     8     5     3     2     8     2     1     7    10     1
   0.060379730526407   0.622213879877005   0.109700311365407


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有