- 阅读权限
- 255
- 威望
- 2 级
- 论坛币
- 102549 个
- 通用积分
- 3.4687
- 学术水平
- 475 点
- 热心指数
- 493 点
- 信用等级
- 434 点
- 经验
- 62369 点
- 帖子
- 1555
- 精华
- 4
- 在线时间
- 2201 小时
- 注册时间
- 2009-5-4
- 最后登录
- 2025-12-25
学科带头人
还不是VIP/贵宾
TA的文库 其他... Python与统计
SAS与统计
- 威望
- 2 级
- 论坛币
 - 102549 个
- 通用积分
- 3.4687
- 学术水平
- 475 点
- 热心指数
- 493 点
- 信用等级
- 434 点
- 经验
- 62369 点
- 帖子
- 1555
- 精华
- 4
- 在线时间
- 2201 小时
- 注册时间
- 2009-5-4
- 最后登录
- 2025-12-25
 | 开心 2013-7-19 06:53:43 |
|---|
签到天数: 72 天 连续签到: 1 天 [LV.6]常住居民II
|
经管之家送您一份
应届毕业生专属福利!
求职就业群
感谢您参与论坛问题回答
经管之家送您两个论坛币!
+2 论坛币
各位, 以下CODE 9.3 出错, 9.4 正常, 求解?
- proc iml;
- /* Let X1, X2,...,Xd be binary variables, let
- p = (p1,p2,...,pd) the their expected values and let
- Delta be the d x d matrix of correlations.
- This function returns 1 if p and Delta are feasible for binary
- variables. The function also computes lower and upper bounds on the
- correlations, and returns them in LBound and UBound, respectively */
- start CheckMVBinaryParams(LBound, UBound, _p, Delta);
- p = rowvec(_p); q = 1 - p; /* make p a row vector */
- d = ncol(p); /*number of variables */
- /* 1. check range of Delta; make sure p and Delta are feasible */
- PP = p`*p; PQ = p`*q;
- QP = q`*p; QQ = q`*q;
- A = -sqrt(PP/QQ); B = -sqrt(QQ/PP); /* matrices */
- LBound = choose(A>B,A,B); /* elementwise max(A or B) */
- LBound[loc(I(d))] = 1; /* set diagonal to 1 */
- A = sqrt(PQ/QP); B = sqrt(QP/PQ);
- UBound = choose(A<B,A,B); /*min(A or B)*/
- UBound[loc(I(d))] = 1; /*set diagonal to 1*/
- /*return 1 <==> specified means and correlations are feasible*/
- return( all(Delta >= LBound) & all(Delta <= UBound) );
- finish;
- /*Objective: Find correlation, rho, that is zero of this function.
- Global variables:
- pj = prob of success for binary var Xj
- pk = prob of success for binary var Xk
- djk = target correlation between Xj and Xk */
- start MVBFunc(rho) global(pj, pk, djk);
- Phi = probbnrm(quantile("Normal",pj), quantile("Normal",pk), rho);
- qj = 1-pj; qk = 1-pk;
- return(Phi - pj*pk - djk*sqrt(pj*qj*pk*qk));
- finish;
- start bisection(a, b);
- dx = 1e-6; dy = 1e-4;
- do i = 1 to 100; /** max iterations **/
- c = (a+b)/2;
- if abs(MVBFunc(c)) < dy | (b-a)/2 < dx then
- return(c);
- if MVBFunc(a)#MVBFunc(c) > 0 then a = c;
- else b = c;
- end;
- return (.); /** no convergence **/
- finish;
- start RandMVBinary(N, p, Delta) global(pj, pk, djk);
- /*1. Check parameters. Compute lower/upper bounds for all (j,k)*/
- if ^CheckMVBinaryParams(LBound, UBound, p, Delta) then do;
- print "The specified correlation is invalid." LBound Delta UBound;
- STOP;
- end;
- q = 1 - p;
- d = ncol(Delta); /*number of variables*/
- /*2. Construct intermediate correlation matrix by solving the
- bivariate CDF (PROBBNRM) equation for each pair of vars */
- R = I(d);
- do j = 1 to d-1;
- do k = j+1 to d;
- pj=p[j]; pk=p[k]; djk = Delta[j,k]; /*set global vars*/
- R[j,k] = bisection(LBound[j,k], UBound[j,k]); /*pre-12.1, 9.3 SAS 需要运行这个*/
- /* R[j,k] = froot("MVBFunc", LBound[j,k]||UBound[j,k]);*/
- R[k,j] = R[j,k];
- end;
- end;
- /*3: Generate MV normal with mean 0 and covariance R*/
- X = RandNormal(N, j(1,d,0), R);
- /*4: Obtain binary variable from normal quantile*/
- do j = 1 to d;
- X[,j] = (X[,j] <= quantile("Normal", p[j])); /*convert to 0/1*/
- end;
- return (X);
- finish;
- call randseed(1234);
- p = {0.25 0.75 0.5}; /*expected values of the X[j]*/
- Delta = { 1 -0.1 -0.25,
- -0.1 1 0.25,
- -0.25 0.25 1 }; /*correlations between the X[j]*/
- X = RandMVBinary(1000, p, Delta);
- /*compare sample estimates to parameters*/
- mean = mean(X);
- corr = corr(X);
- print p, mean, Delta, corr[format=best6.];
- quit;
复制代码

扫码加我 拉你入群
请注明:姓名-公司-职位
以便审核进群资格,未注明则拒绝
|
|
|