2014-07-11 4 views
1

Newton Raphson 최적화를 실행하는 코드를 만들려고합니다. 저는 proc iml을 사용하고 있습니다. 그러나 오류 (e)를 평가할 필요가있을 때 나는 모든 제곱 차이를 요약 할 필요가 있으며 SAS에이 경우 벡터의 구성 요소의 합이 필요하다고 말하는 방법을 모르겠습니다 벡터가 아닙니다.proc iml의 제곱 오류 합계

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; 

감사합니다 : 코드는 다음과 같습니다!

답변

0

IML의 제곱의 합을 계산하려면 SSQ 함수를 사용해야합니다.

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; 

릭 Wicklin은 매우 유용하다 ## 연산자에 대한 post 있습니다.