1

I'm trying to create a code to run Newton Raphson optimization. I'm using proc iml, but when I need to evaluate the error (e) I need to sum up all the square differences and don't know how to tell SAS that in that case I need the sum of the components of the vector and not the vector. The code is the following:

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;

Thanks!

Joe
  • 62,789
  • 6
  • 49
  • 67
GabyLP
  • 3,649
  • 7
  • 45
  • 66

1 Answers1

0

You should use the SSQ function to calculate sum of squares in IML.

e=ssq(s-t);

Here are several other ways to do this. Note the first way gives a different result (as it's summing the squares of s and -t), just an example of the difference of how you pass the arguments.

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 has a post about the ## operator, which is quite useful.

Joe
  • 62,789
  • 6
  • 49
  • 67