Solved
Contributor
Posts: 21

Complex Numbers

I'd like to work with complex numbers in SAS/IML.  In particular, I'd like to do option pricing in the Heston model as described in Kahl and Jaeckel.

I assume the complex numbers will be represented as two-component matrices.  I also assume I'll be using modules to do complex arithmetic, including multiplication and division, as well as things like arg(c), |c|, exp(c), ln(c), for complex c.

Is there a standard implementation of these compex arithmetic modules that I can use, or will I have to write my own?

Accepted Solutions
Solution
‎01-13-2016 02:52 PM
SAS Super FREQ
Posts: 4,243

Re: Complex Numbers

You will have to write your own modules. The functions you specify are all elementwise operations, so they don't require any linear algegra. Be sure to vectorize your functions for efficiency.

As an example, the Arg function returns the polar angle. You can use some of the ideas in this blog post about "Computing polar angles from data."

Another example is complex multiplication, which I would implement as follows:

``````proc iml;
/* Complex multiplication of A*B.
A vector of complex numbers is a two-column
matrix where Re(A)=A[,1] and Im(A)=A[,2].
If A = x + iy and B = u + iv then
C = A*B = (x#u - y#v) + i(x#v + y#u)
*/
start cplxMult(A, B);
C = j(nrow(A),2);
C[,1] = A[,1]#B[,1] - A[,2]#B[,2];
C[,2] = A[,1]#B[,2] + A[,2]#B[,1];
return( C );
finish;

/* test it */
A = {2  0, /* pure real */
0  3, /* pure imag */
1  3,
-1 -5};
B = {3  0, /* pure real */
0 -4, /* pure imag */
1 -3, /* conjugate */
4  2};
C = cplxMult(A, B);
print C;
``````

If you create a nice library of complex operations, I encourage you post it to the SAS/IML File Exchange when you are finished.  I think others might find it interesting and potentially useful.

All Replies
Super User
Posts: 10,784

Re: Complex Numbers

Ohh, I would doubt SAS can do some  complex arithmetic. I never saw the function or operator related to complex number in SAS/IML .

You need Mathematics or Matlab .

Maybe @Rick Wicklin could guide you to somewhere . He is PhD of Applied Mathematic .

Solution
‎01-13-2016 02:52 PM
SAS Super FREQ
Posts: 4,243

Re: Complex Numbers

You will have to write your own modules. The functions you specify are all elementwise operations, so they don't require any linear algegra. Be sure to vectorize your functions for efficiency.

As an example, the Arg function returns the polar angle. You can use some of the ideas in this blog post about "Computing polar angles from data."

Another example is complex multiplication, which I would implement as follows:

``````proc iml;
/* Complex multiplication of A*B.
A vector of complex numbers is a two-column
matrix where Re(A)=A[,1] and Im(A)=A[,2].
If A = x + iy and B = u + iv then
C = A*B = (x#u - y#v) + i(x#v + y#u)
*/
start cplxMult(A, B);
C = j(nrow(A),2);
C[,1] = A[,1]#B[,1] - A[,2]#B[,2];
C[,2] = A[,1]#B[,2] + A[,2]#B[,1];
return( C );
finish;

/* test it */
A = {2  0, /* pure real */
0  3, /* pure imag */
1  3,
-1 -5};
B = {3  0, /* pure real */
0 -4, /* pure imag */
1 -3, /* conjugate */
4  2};
C = cplxMult(A, B);
print C;
``````

If you create a nice library of complex operations, I encourage you post it to the SAS/IML File Exchange when you are finished.  I think others might find it interesting and potentially useful.

🔒 This topic is solved and locked.