Skip to content

Commit

Permalink
lower memory implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathf committed Sep 28, 2016
1 parent 13fc577 commit db5a90d
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions skbayes/rvm_ard_models/fast_rvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def predict_proba(self,X):
----------
X: array-like of size [n_samples_test,n_features]
Matrix of explanatory variables (test set)
Returns
-------
probs: numpy array of size [n_samples_test]
Expand All @@ -618,7 +618,7 @@ def predict_proba(self,X):
prob = pr / np.reshape(np.sum(pr, axis = 1), (pr.shape[0],1))
return prob


def _predict_proba(self,X,y_hat,sigma):
'''
Calculates predictive distribution
Expand All @@ -627,26 +627,37 @@ def _predict_proba(self,X,y_hat,sigma):
ks = 1. / ( 1. + np.pi * var/ 8)**0.5
pr = expit(y_hat * ks)
return pr


def _sparsity_quality(self,X,Xa,y,B,A,Aa,active,Sn):
'''
Calculates sparsity & quality parameters for each feature
'''
XB = X.T*B
YB = y*B
XSX = np.dot(np.dot(Xa,Sn),Xa.T)
bxy = np.dot(XB,y)
Q = bxy - np.dot( np.dot(XB,XSX), YB)
S = np.sum( XB*X.T,1 ) - np.sum( np.dot( XB,XSX )*XB,1 )
qi = np.copy(Q)
si = np.copy(S)
Qa,Sa = Q[active], S[active]
qi[active] = Aa * Qa / (Aa - Sa )
si[active] = Aa * Sa / (Aa - Sa )
return [si,qi,S,Q]



def _sparsity_quality(self, X, Xa, y, B, A, Aa, active, Sn):
'''Calculates sparsity & quality parameters for each feature.'''
XB = X.T*B
XSX = np.dot(Xa, Sn)
XSX = np.dot(XSX, Xa.T)

Q = np.dot(XB, XSX)
Q = -np.dot(Q, y*B)
Q += np.dot(XB, y)

S = np.dot(XB, XSX)
del XSX
S *= XB
S = -np.sum(S, 1)
S += np.sum(XB*X.T, 1)
del XB

S -= XBX
del XBX

qi = np.copy(Q)
si = np.copy(S)
Qa, Sa = Q[active], S[active]
qi[active] = Aa * Qa / (Aa - Sa)
si[active] = Aa * Sa / (Aa - Sa)

return [si, qi, S, Q]


def _posterior_dist(self,X,y,A,intercept_prior):
'''
Uses Laplace approximation for calculating posterior distribution
Expand Down

0 comments on commit db5a90d

Please sign in to comment.