def fit(self, X, Y): n, d = X.shape[0], X.shape[1] Y = Y.reshape(-1, 1) L = self.calculateL(self.xi) #secOrd = np.linalg.pinv((X.T.dot(X) * L + self.reg * np.eye(d)) / n) # I = np.eye(d) # I[-1, -1] = 0 #Not regularize the intercept try: secOrd = np.linalg.pinv(X.T.dot(X) * L / n + self.reg * np.eye(d)) except Exception as e: print(e.message) if not self.manualInitial: self.initialize(X, Y) t = 0. while t < self.iterations: v = X.dot(self._beta) GEVFunc.clip(self.xi, v) Y_hat = GEVFunc.inverseLink(self.xi, v) #firOrd = (X.T.dot(Y_hat - Y) + self.reg * self._beta) / n # tmp = self.reg * self._beta # tmp[-1] = 0 firOrd = X.T.dot(Y_hat - Y) / n + self.reg * self._beta deltaBeta = self.step * secOrd.dot(firOrd) if t >= 100 and np.abs(deltaBeta).sum() < self.tol: self._beta -= deltaBeta # print "Converged. t = %d" % (t) break self._beta -= deltaBeta t += 1 self._beta = np.array(self._beta).flatten()
def fit(self, X, Y): self.__initialize(X, Y) xi = self.xi t = 0 while t < self.iterations: #Caculate weight matrix w = self._eta*np.power(-np.log(self._eta), xi+1) W = np.diag(w) #Z is used for updating beta tmp = GEVFunc.derivLink(xi, self._eta) Z = self._v + self._gamma \ *tmp \ *(Y - self._eta) #Update beta mat = np.matrix(X.T.dot(W).dot(X)) \ + np.eye(X.shape[1])*self.regular #self._beta = mat.I.dot(X.T).dot(W).dot(Z).getA1() self._beta = np.linalg.pinv(mat).dot(X.T).dot(W).dot(Z).getA1() #Calculate v self._v = X.dot(self._beta) GEVFunc.clip(xi, self._v) #Judge if eta is convergent newEta = GEVFunc.inverseLink2(xi, self._v) if np.abs(newEta - self._eta).sum() < self.tol: self._eta = newEta break self._eta = newEta t += 1
def betaDeriva(self, X, Y, beta, xi): v = X.dot(beta).reshape(-1, 1) y = Y.reshape(-1, 1) GEVFunc.clip(xi, v) gev = GEVFunc.GEV(xi, v) loggev = GEVFunc.logGEV(xi, v) res = - np.sum(X * (loggev * (y - gev) / ((1 - gev) * (1 + xi*v))), axis=0) - beta return res
def xiDeriva(self, X, Y, beta, xi): v = X.dot(beta).reshape(-1, 1) y = Y.reshape(-1, 1) GEVFunc.clip(xi, v) gev = GEVFunc.GEV(xi, v) loggev = GEVFunc.logGEV(xi, v) res = np.sum((np.log(1 + xi*v) / xi**2 - v / xi / (1 + xi*v)) * loggev * (y - gev) / (1 - gev)) - xi return res
def oneStep3(self, X, Y): n, d = X.shape[0], X.shape[1] Y = Y.reshape(-1, 1) v = X.dot(self._beta) GEVFunc.clip(self.xi, v) W = np.diag(GEVFunc.derivInverseLink(self.xi, v).flatten()) secOrd = np.linalg.pinv(X.T.dot(W).dot(X) / n + self.reg * np.eye(d)) Y_hat = GEVFunc.inverseLink(self.xi, v) firOrd = X.T.dot(Y_hat - Y) / n + self.reg * self._beta self._beta -= self.step * secOrd.dot(firOrd)
def betaSecDeriva(self, X, Y, beta, xi): pos, neg = X[Y == 1], X[Y == 0] v = pos.dot(beta).reshape(-1, 1) GEVFunc.clip(xi, v) w = (-1-xi) * np.power(1 + xi * v, -1./xi-2) comp1 = pos.T.dot(np.diag(w.flatten())).dot(pos) v = neg.dot(beta).reshape(-1, 1) GEVFunc.clip(xi, v) gev = GEVFunc.GEV(xi, v) a = 1 + xi * v w = np.power(a, -1./xi-2)*gev/(1-gev)*(1+xi - np.power(a, -1./xi)/(1-gev)) comp2 = neg.T.dot(np.diag(w.flatten())).dot(neg) return comp1 + comp2 - np.eye(beta.size)
def oneStep(self, X, Y): n, d = X.shape[0], X.shape[1] Y = Y.reshape(-1, 1) L = self.calculateL(self.xi) try: secOrd = np.linalg.pinv(X.T.dot(X) * L / n + self.reg * np.eye(d)) except Exception as e: print(e.message) v = X.dot(self._beta) GEVFunc.clip(self.xi, v) Y_hat = GEVFunc.inverseLink(self.xi, v) firOrd = X.T.dot(Y_hat - Y) / n + self.reg * self._beta self._beta -= self.step * secOrd.dot(firOrd)
def generateData(D=3, N=50, sigmaBeta=1, sigmaXi=1, miuXi=0.5): flag = True while flag: X = np.random.randn(N, D) beta = np.random.randn(D) * sigmaBeta # xi = np.random.randn() * sigmaXi + miuXi xi = miuXi v = X.dot(beta) GEVFunc.clip(xi, v) y = GEVFunc.GEV(xi, v) y[y < 0.5] = 0 y[y >= 0.5] = 1 if 0.3 < (y == 1).sum()/float(N) < 0.7: flag = False return X, y, xi, beta
def xiSecDeriva(self, X, Y, beta, xi): v = X.dot(beta).reshape(-1, 1) GEVFunc.clip(xi, v) y = Y.reshape(-1, 1) a = 1 + xi*v gev = GEVFunc.GEV(xi, v) loggev = GEVFunc.logGEV(xi, v) y_gev = y - gev one_gev = 1 - gev y_gev_1_gev = y_gev/one_gev xia = xi * a xiv = xi * v lna = np.log(a) comp1 = (xiv * (3*a-1) - 2*lna*(a**2)) / (xi * xia**2) * y_gev_1_gev * loggev comp2 = (1/xi**2 * lna - v/xia) * ((y-1)*loggev*gev/one_gev**2 + y_gev_1_gev) * (v/xia-lna/xi**2)/np.power(a, 1./xi) return np.sum(comp1 + comp2) - 1
def fit3(self, X, Y): n, d = X.shape[0], X.shape[1] Y = Y.reshape(-1, 1) if not self.manualInitial: self.initialize(X, Y) t = 0. while t < self.iterations: v = X.dot(self._beta) GEVFunc.clip(self.xi, v) W = np.diag(GEVFunc.derivInverseLink(self.xi, v).flatten()) # secOrd = np.linalg.pinv(X.T.dot(W).dot(X) / n + self.reg * np.eye(d)) A = X.T.dot(W).dot(X) / n + self.reg * np.eye(d) Y_hat = GEVFunc.inverseLink(self.xi, v) b = X.T.dot(Y_hat - Y) / n + self.reg * self._beta self._beta += np.linalg.lstsq(A, -b)[0] # self._beta -= self.step * secOrd.dot(firOrd) t += 1 self._beta = np.array(self._beta).flatten()
def predict(self, X): v = X.dot(self.beta) GEVFunc.clip(self.xi, v) return GEVFunc.inverseLink(self.xi, v).flatten()
def firstDeriva(self, X, Y, beta): v = X.dot(beta) GEVFunc.clip(self.xi, v) Y_hat = GEVFunc.inverseLink(self.xi, v) return X.T.dot(Y_hat - Y) / X.shape[0] + self.reg * beta
def predict(self, X): self._v = X.dot(self._beta) GEVFunc.clip(self.xi, self._v) return GEVFunc.inverseLink2(self.xi, self._v)
def __initialize(self, X, Y): self._eta = np.array([0.25] * Y.size) self._eta[Y == 1] = 0.75 self._v = GEVFunc.link(self.xi, self._eta)
def loglikelihood(self, beta, xi): v = self.X.dot(beta.reshape(-1, 1)) GEVFunc.clip(xi, v) res = GEVFunc.inverseLink(xi, v) res[self.targets == 0] = 1 - res[self.targets == 0] return sum([np.log(x + 1e-8) for x in res])