Multiple lib changes

This commit is contained in:
2007-01-25 11:58:10 +00:00
parent a65d79697f
commit 1c2c2c8895
7 changed files with 519 additions and 152 deletions

View File

@@ -1,4 +1,3 @@
"""Module contain algorithms for (burdensome) calculations.
There is no typechecking of any kind here, just focus on speed
@@ -7,7 +6,7 @@ There is no typechecking of any kind here, just focus on speed
from scipy.linalg import svd,norm,inv,pinv,qr
from scipy import dot,empty,eye,newaxis,zeros,sqrt,diag,\
apply_along_axis,mean,ones,randn,empty_like,outer,c_,\
rand,sum,cumsum
rand,sum,cumsum,matrix
def pca(a, aopt, scale='scores', mode='normal'):
""" Principal Component Analysis model
@@ -17,8 +16,9 @@ def pca(a, aopt, scale='scores', mode='normal'):
-- detailed : returns all model params and all residuals
"""
m,n = a.shape
u,s,vt = svd(a, full_matrices=0)
m, n = a.shape
u, s, vt = svd(a, full_matrices=0)
eigvals = (1./m)*s
T = u*s
T = T[:,:aopt]
P = vt[:aopt,:].T
@@ -43,17 +43,40 @@ def pca(a, aopt, scale='scores', mode='normal'):
return {'T':T, 'P':P, 'E':E}
def pcr(a, b, aopt=2, scale='scores', mode='normal'):
"""Returns Principal component regression model."""
m, n = a.shape
try:
k, l = b.shape
except:
k = b.shape[0]
l = 1
B = empty((aopt, n, l))
U, s, Vt = svd(a, full_matrices=True)
T = U*s
T = T[:,:aopt]
P = Vt[:aopt,:].T
Q = dot(dot(inv(dot(T.T, T)), T.T), b).T
for i in range(aopt):
ti = T[:,:i+1]
r = dot(dot(inv(dot(ti.T,ti)), ti.T), b)
B[i] = dot(P[:,:i+1], r)
E = a - dot(T, P.T)
F = b - dot(T, Q.T)
return {'T':T, 'P':P,'Q': Q, 'B':B, 'E':E, 'F':F}
def pls(a, b, aopt=2, scale='scores', mode='normal', ab=None):
"""Kernel pls for tall/wide matrices.
Fast pls for calibration. Only inefficient for many Y-vars.
"""
m,n = a.shape
m, n = a.shape
if ab!=None:
mm,l = ab.shape
mm, l = m_shape(ab)
else:
k,l = b.shape
k, l = m_shape(b)
W = empty((n, aopt))
P = empty((n, aopt))
@@ -66,10 +89,10 @@ def pls(a, b, aopt=2, scale='scores', mode='normal', ab=None):
ab = dot(a.T, b)
for i in range(aopt):
if ab.shape[1]==1:
w = ab
w = ab.reshape(mm, l)
else:
u,s,vh = svd(dot(ab.T, ab))
w = dot(ab,u[:,:1])
u, s, vh = svd(dot(ab.T, ab))
w = dot(ab, u[:,:1])
w = w/norm(w)
r = w.copy()
@@ -99,9 +122,9 @@ def pls(a, b, aopt=2, scale='scores', mode='normal', ab=None):
if mode=='detailed':
E = empty((aopt, m, n))
F = empty((aopt, k, l))
for i in range(1,aopt+1,1):
E[i-1] = a - dot(T[:,:i],P[:,:i].T)
F[i-1] = b - dot(T[:,:i],Q[:,:i].T)
for i in range(1, aopt+1, 1):
E[i-1] = a - dot(T[:,:i], P[:,:i].T)
F[i-1] = b - dot(T[:,:i], Q[:,:i].T)
else:
E = a - dot(T[:,:aopt], P[:,:aopt].T)
F = b - dot(T[:,:aopt], Q[:,:aopt].T)
@@ -121,17 +144,17 @@ def w_simpls(aat, b, aopt):
There is no P,W. T is normalised
"""
bb = b.copy()
m,m = aat.shape
m, m = aat.shape
U = empty((m, aopt))
T = empty((m, aopt))
H = empty((m, aopt)) #just like W in simpls
PROJ = empty((m, aopt)) #just like R in simpls
for i in range(aopt):
u,s,vh = svd(dot(dot(b.T, aat), b), full_matrices=0)
u, s, vh = svd(dot(dot(b.T, aat), b), full_matrices=0)
u = dot(b, u[:,:1]) #y-factor scores
U[:,i] = u.ravel()
t =dot(aat, u)
t = dot(aat, u)
t = t/norm(t)
T[:,i] = t.ravel()
h = dot(aat, t) #score-weights
@@ -141,19 +164,18 @@ def w_simpls(aat, b, aopt):
b = b - dot(PROJ[:,:i+1], dot(H[:,:i+1].T,b) )
C = dot(bb.T, T)
return {'T':T,'U':U,'Q':C,'H':H}
return {'T':T, 'U':U, 'Q':C, 'H':H}
def bridge(a, b, aopt, scale='scores', mode='normal', r=0):
"""Undeflated Ridged svd(X'Y)
"""
m, n = a.shape
k, l = b.shape
u,s,vt = svd(b, full_matrices=0)
k, l = m_shape(b)
u, s, vt = svd(b, full_matrices=0)
g0 = dot(u*s, u.T)
g = (1 - r)*g0 + r*eye(m)
ag = dot(a.T, g)
u,s,vt = svd(ag, full_matrices=0)
u, s, vt = svd(ag, full_matrices=0)
W = u[:,:aopt]
K = vt[:aopt,:].T
T = dot(a, W)
@@ -166,8 +188,8 @@ def bridge(a, b, aopt, scale='scores', mode='normal', r=0):
return {'T':T, 'W':W}
U = dot(g0, K) #fixme check this
Q = dot(b.T, dot(T, inv(dot(T.T,T)) ))
B = zeros((aopt, n, l))
Q = dot(b.T, dot(T, inv(dot(T.T, T)) ))
B = zeros((aopt, n, l), dtype='f')
for i in range(aopt):
B[i] = dot(W[:,:i+1], Q[:,:i+1].T)
# leverages
@@ -198,3 +220,6 @@ def bridge(a, b, aopt, scale='scores', mode='normal', r=0):
return {'B':B, 'W':W, 'T':T, 'Q':Q, 'E':E, 'F':F, 'U':U, 'P':W}
def m_shape(array):
return matrix(array).shape