Principal component analysis in GAMS

Problems with modeling
Post Reply
Posts: 23
Joined: 1 year ago

Principal component analysis in GAMS

Post by Luca » 1 year ago

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,

User avatar
Posts: 346
Joined: 3 years ago

Re: Principal component analysis in GAMS

Post by bussieck » 1 year ago


GAMS ships a couple of matrix algebra tools like eigenvalue, eigenvector, inverse, etc, see ... SFORMATION. The principle use of these utilities is shown in e.g. ... 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]):
gams.set('eVec', eVec)

endEmbeddedCode eVal eVec
display eVal, eVec;
You might have to install numpy/scipy into your GAMS/Python. See details at 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. and scale nicely.


Post Reply