Principal component analysis in GAMS

Problems with modeling
Post Reply
Luca
User
User
Posts: 23
Joined: 6 years ago

Principal component analysis in GAMS

Post by Luca »

Dear all,
I would like to implement principal component analysis in GAMS. I would ask you if there are linear algebra facilities in GAMS (such as tools to compute eigenvalues...) or there are libraries already available for PCA in GAMS.

Thanks very much for your help. Best regards,
Luca
User avatar
bussieck
Moderator
Moderator
Posts: 1037
Joined: 7 years ago

Re: Principal component analysis in GAMS

Post by bussieck »

Luca,

GAMS ships a couple of matrix algebra tools like eigenvalue, eigenvector, inverse, etc, see https://www.gams.com/latest/docs/T_MAIN ... SFORMATION. The principle use of these utilities is shown in e.g. https://www.gams.com/latest/testlib_ml/ ... val01.html.

You can also use GAMS Embedded Python Code and numpy or for sparse matrices scipy. Here is a simple dense code with numpy:

Code: Select all

set i /i1*i3/;
alias (i,j);

table a(i,j)
      i1   i2   i3
i1     9    1    1
i2     1    9    1
i3     1    1    9;

parameter eVal(i) 'eigenvalues';
parameter eVec(i,j) 'eigenvectors';

embeddedCode Python:
import numpy as np
from numpy import linalg as LA

i = list(gams.get('i'))
id = { k[0]:k[1] for k in zip(i,range(len(i))) }
A = np.zeros(shape=(len(i),len(i)))
for a in gams.get('a'):
   A[id[a[0][0]],id[a[0][1]]] = a[1]
e = LA.eigh(A)
gams.set('eVal', [ (k[0],k[1]) for k in zip(i,e[0]) ])
eVec = []
for k in zip(i,e[1]):
  for j in zip(i,k[1]):
    eVec.append((k[0],j[0],j[1])) 
gams.set('eVec', eVec)

endEmbeddedCode eVal eVec
display eVal, eVec;
You might have to install numpy/scipy into your GAMS/Python. See details at https://www.gams.com/latest/docs/UG_EmbeddedCode.html. I don't know good the numpy/scipy algorithms are, the algorithms in the GAMS tools (like eigenvector) work with sparse matrix algebra (e.g. http://www.netlib.org/lapack/) and scale nicely.

-Michael
Post Reply