1

我正在尝试创建一个代码来运行 Newton Raphson 优化。我正在使用 proc iml,但是当我需要评估错误 (e) 时,我需要总结所有平方差并且不知道如何告诉 SAS 在这种情况下我需要向量分量的总和而不是向量。代码如下:

proc iml;  use chap0; read all var{X} into X; 
            read all var{t} into t; 

W=1;           
s= exp(X*w)/(1+ exp(X*w)); print s;
e = (s - t) ** 2; /*here I need the result of the sum for that and not the matrix*/
g=2*(s-t)*s*(1-s);
h=2 * s * (1 - s) * (s * (1 - s) + (s - t) * (1 - 2 * s));

count=0;/*init count number*/


do until (e<1e-8); 

 count=count+1; 
 w=w0-g/h;   /*here I also need the sum of g and h*/
 s= exp(X*w)/(1+ exp(X*w));  
 e = (s - t) ** 2;  
 wo=w;  
end;

谢谢!

4

1 回答 1

0

您应该使用SSQ函数来计算 IML 中的平方和。

e=ssq(s-t);

这里有其他几种方法可以做到这一点。请注意,第一种方法给出了不同的结果(因为它是对 s 和 -t 的平方求和),这只是传递参数方式不同的一个示例。

proc iml;
 x = 1:5;
 y = j(1,5,2);
 e1=ssq(x,-y);     *sum of the squares, not actually subtracting note, so it is not the same answer;
 e2=ssq(x-y);      *sum of squares of the differences;
 e3=(x-y)*(x-y)`;  *multiplying a vector by its transpose sums it;
 e4=(x-y)[##];     *summation subscript operator, see note;
 print e1 e2 e3 e4;
quit;

Rick Wicklin 有一篇关于 ## 运算符的帖子,非常有用。

于 2014-07-11T17:59:18.780 回答