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

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

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;