Beispiel #1
0
    def optimize(self, X, C, y, kernelNames, regions, reml=True, maxiter=100):
        methodName = ('REML' if reml else 'ML')
        if self.verbose:
            print 'Finding MKLMM', methodName, 'parameters for', len(
                regions), 'regions with lengths:', [
                    np.sum(r) for r in regions
                ]

        #prepare initial values for sig2e and for fixed effects
        hyp0_sig2e, hyp0_fixedEffects = self.getInitialHyps(X, C, y)

        #build kernel and train a model
        t0 = time.time()
        kernel, hyp0_kernels = self.buildKernel(X, kernelNames, regions,
                                                y.var())

        hyp0 = np.concatenate((hyp0_sig2e, hyp0_fixedEffects, hyp0_kernels))
        args = (kernel, C, y, reml)
        funcToSolve = self.infExact_scipy

        # # #check gradient correctness
        # # if (len(hyp0) < 10):
        # # self.optimization_counter=0
        # # likFunc  = lambda hyp: funcToSolve(hyp, kernel, C, y, reml)[0]
        # # gradFunc = lambda hyp: funcToSolve(hyp, kernel, C, y, reml)[1]
        # # err = optimize.check_grad(likFunc, gradFunc, hyp0)
        # # print 'gradient error:', err

        if self.verbose: print 'Beginning Optimization'
        self.optimization_counter = 0
        optObj = gpUtils.minimize(hyp0, funcToSolve, -maxiter, *args)

        if (not optObj.success):
            print 'Optimization status:', optObj.status
            print 'optimization message:', optObj.message
            raise Exception('Optimization failed with message: ' +
                            optObj.message)

        sig2e = np.exp(2 * optObj.x[0])
        fixedEffects = optObj.x[1:C.shape[1] + 1]
        hyp_kernels = optObj.x[C.shape[1] + 1:]
        kernelObj = kernel

        if self.verbose:
            print 'done in %0.2f' % (time.time() - t0), 'seconds'
            print 'sig2e:', sig2e
            print 'Fixed effects:', fixedEffects
            if (hyp_kernels.shape[0] < 18): print 'kernel params:', hyp_kernels

        return kernelObj, hyp_kernels, sig2e, fixedEffects
Beispiel #2
0
	def optimize(self, X, C, y, kernelNames, regions, reml=True, maxiter=100):
		methodName = ('REML' if reml else 'ML')
		if self.verbose: print 'Finding MKLMM',  methodName, 'parameters for', len(regions), 'regions with lengths:', [np.sum(r) for r in regions]
		
		#prepare initial values for sig2e and for fixed effects
		hyp0_sig2e, hyp0_fixedEffects = self.getInitialHyps(X, C, y)
		
		#build kernel and train a model
		t0 = time.time()				
		kernel, hyp0_kernels = self.buildKernel(X, kernelNames, regions, y.var())
			
		hyp0 = np.concatenate((hyp0_sig2e, hyp0_fixedEffects, hyp0_kernels))		
		args = (kernel, C, y, reml)
		funcToSolve = self.infExact_scipy

		# # #check gradient correctness
		# # if (len(hyp0) < 10):
			# # self.optimization_counter=0
			# # likFunc  = lambda hyp: funcToSolve(hyp, kernel, C, y, reml)[0]
			# # gradFunc = lambda hyp: funcToSolve(hyp, kernel, C, y, reml)[1]
			# # err = optimize.check_grad(likFunc, gradFunc, hyp0)
			# # print 'gradient error:', err
		
		if self.verbose: print 'Beginning Optimization'
		self.optimization_counter=0			
		optObj = gpUtils.minimize(hyp0, funcToSolve, -maxiter, *args)
			
		if (not optObj.success):
			print 'Optimization status:', optObj.status
			print 'optimization message:', optObj.message
			raise Exception('Optimization failed with message: ' + optObj.message)

		sig2e = np.exp(2*optObj.x[0])
		fixedEffects = optObj.x[1:C.shape[1]+1]
		hyp_kernels = optObj.x[C.shape[1]+1:]
		kernelObj = kernel

		if self.verbose:
			print 'done in %0.2f'%(time.time()-t0), 'seconds'
			print 'sig2e:', sig2e
			print 'Fixed effects:', fixedEffects
			if (hyp_kernels.shape[0] < 18): print 'kernel params:', hyp_kernels
			
		return kernelObj, hyp_kernels, sig2e, fixedEffects
Beispiel #3
0
	def buildKernelAdapt(self, X, C, y, regions, reml=True, maxiter=100):
	
		#prepare initial values for sig2e and for fixed effects
		hyp0_sig2e, hyp0_fixedEffects = self.getInitialHyps(X, C, y)	
		
		
		bestKernelNames = []
		kernelsListAll 	= []
		hyp_kernels = []

		funcToSolve = self.infExact_scipy
		yVar = y.var()		
		
		for r_i, r in enumerate(regions):
		
			#if (r_i == 0): kernelsToTry = ['lin']
			#else:
			#	kernelsToTry = ['lin', 'poly2_lin', 'rbf_lin', 'nn_lin']		
			kernelsToTry = ['lin', 'poly2_lin', 'rbf_lin', 'nn_lin']
			if self.verbose:
				print
				print 'selecting a kernel for region', r_i, 'with', r.sum(), 'SNPs'
		
			#add linear kernel
			X_lastRegion = X[:, r]
			linKernel = kernels.linearKernel(X_lastRegion)
			kernelsListAll.append(kernels.ScaledKernel(linKernel))
			kernelsListAll.append(None)
			
			bestFun = np.inf
			bestKernelName = None
			best_hyp0 = None
			bestKernel = None
			bestPval = np.inf
		
			#iterate over every possible kernel			
			for kernelToTry in kernelsToTry:
				hyp0 = [0.5*np.log(0.5*yVar)]
				if self.verbose: print 'Testing kernel:', kernelToTry
				
				#create the kernel
				if (kernelToTry == 'lin'):
					kernel = None
					df = None
				elif (kernelToTry == 'rbf_lin'):
					kernel = kernels.RBFKernel(X_lastRegion)
					hyp0.append(np.log(1.0))	#ell
					df = 2
				elif (kernelToTry == 'nn_lin'):
					kernel = kernels.NNKernel(X_lastRegion)
					hyp0.append(np.log(1.0))	#ell
					df = 2
				elif (kernelToTry == 'poly2_lin'):
					kernel = kernels.Poly2KernelHomo(linKernel)				
					df = 1
				else:
					raise Exception('unrecognized kernel name')
					
				if (kernel is not None):
					#scale the kernel
					kernel = kernels.ScaledKernel(kernel)
					hyp0.append(0.5*np.log(0.5*yVar))	#scaling hyp
					
					#add the kernel as the final kernel in the kernels list
					kernelsListAll[-1] = kernel
					sumKernel = kernels.SumKernel(kernelsListAll)
				else:
					sumKernel = kernels.SumKernel(kernelsListAll[:-1])
				
				#test log likelihood obtained with this kernel for this region							
				args = (sumKernel, C, y, reml)
				self.optimization_counter=0
				hyp0_all = np.concatenate((hyp0_sig2e, hyp0_fixedEffects, hyp_kernels+hyp0))
				optObj = gpUtils.minimize(hyp0_all, funcToSolve, -maxiter, *args)
				if (not optObj.success):
					print 'Optimization status:', optObj.status
					print 'optimization message:', optObj.message
					raise Exception('optimization failed')
					
				print 'final LL: %0.5e'%(-optObj.fun)
				if (kernelToTry == 'lin'):
					linLL = -optObj.fun
					pVal = 1.0
				else:
					llDiff = -optObj.fun - linLL
					if (llDiff < 0): pVal = 1.0
					else: pVal = 0.5*stats.chi2(df).sf(llDiff)
					print 'llDiff: %0.5e'%llDiff, 'pVal:%0.5e'%pVal
					
				if (kernelToTry == 'lin' or (pVal < bestPval and (len(kernelsToTry)==1 or pVal < 0.05/(len(kernelsToTry)-1)))):
					bestOptObj = optObj
					bestPval = pVal
					bestKernelName = kernelToTry				
					best_hyp0 = hyp0
					best_sumKernel = sumKernel
					bestKernel = kernel		
			
			if (bestKernel is not None): kernelsListAll[-1] = bestKernel
			else: kernelsListAll = kernelsListAll[:-1]		
			hyp_kernels += best_hyp0
			bestKernelNames.append(bestKernelName)
			
			if self.verbose: print 'selected kernel:', bestKernelName
		
		if self.verbose:
			print 'selected kernels:', bestKernelNames
			print			
			
		return bestKernelNames
Beispiel #4
0
    def buildKernelAdapt(self, X, C, y, regions, reml=True, maxiter=100):

        #prepare initial values for sig2e and for fixed effects
        hyp0_sig2e, hyp0_fixedEffects = self.getInitialHyps(X, C, y)

        bestKernelNames = []
        kernelsListAll = []
        hyp_kernels = []

        funcToSolve = self.infExact_scipy
        yVar = y.var()

        for r_i, r in enumerate(regions):

            #if (r_i == 0): kernelsToTry = ['lin']
            #else:
            #	kernelsToTry = ['lin', 'poly2_lin', 'rbf_lin', 'nn_lin']
            kernelsToTry = ['lin', 'poly2_lin', 'rbf_lin', 'nn_lin']
            if self.verbose:
                print
                print 'selecting a kernel for region', r_i, 'with', r.sum(
                ), 'SNPs'

            #add linear kernel
            X_lastRegion = X[:, r]
            linKernel = kernels.linearKernel(X_lastRegion)
            kernelsListAll.append(kernels.ScaledKernel(linKernel))
            kernelsListAll.append(None)

            bestFun = np.inf
            bestKernelName = None
            best_hyp0 = None
            bestKernel = None
            bestPval = np.inf

            #iterate over every possible kernel
            for kernelToTry in kernelsToTry:
                hyp0 = [0.5 * np.log(0.5 * yVar)]
                if self.verbose: print 'Testing kernel:', kernelToTry

                #create the kernel
                if (kernelToTry == 'lin'):
                    kernel = None
                    df = None
                elif (kernelToTry == 'rbf_lin'):
                    kernel = kernels.RBFKernel(X_lastRegion)
                    hyp0.append(np.log(1.0))  #ell
                    df = 2
                elif (kernelToTry == 'nn_lin'):
                    kernel = kernels.NNKernel(X_lastRegion)
                    hyp0.append(np.log(1.0))  #ell
                    df = 2
                elif (kernelToTry == 'poly2_lin'):
                    kernel = kernels.Poly2KernelHomo(linKernel)
                    df = 1
                else:
                    raise Exception('unrecognized kernel name')

                if (kernel is not None):
                    #scale the kernel
                    kernel = kernels.ScaledKernel(kernel)
                    hyp0.append(0.5 * np.log(0.5 * yVar))  #scaling hyp

                    #add the kernel as the final kernel in the kernels list
                    kernelsListAll[-1] = kernel
                    sumKernel = kernels.SumKernel(kernelsListAll)
                else:
                    sumKernel = kernels.SumKernel(kernelsListAll[:-1])

                #test log likelihood obtained with this kernel for this region
                args = (sumKernel, C, y, reml)
                self.optimization_counter = 0
                hyp0_all = np.concatenate(
                    (hyp0_sig2e, hyp0_fixedEffects, hyp_kernels + hyp0))
                optObj = gpUtils.minimize(hyp0_all, funcToSolve, -maxiter,
                                          *args)
                if (not optObj.success):
                    print 'Optimization status:', optObj.status
                    print 'optimization message:', optObj.message
                    raise Exception('optimization failed')

                print 'final LL: %0.5e' % (-optObj.fun)
                if (kernelToTry == 'lin'):
                    linLL = -optObj.fun
                    pVal = 1.0
                else:
                    llDiff = -optObj.fun - linLL
                    if (llDiff < 0): pVal = 1.0
                    else: pVal = 0.5 * stats.chi2(df).sf(llDiff)
                    print 'llDiff: %0.5e' % llDiff, 'pVal:%0.5e' % pVal

                if (kernelToTry == 'lin'
                        or (pVal < bestPval and
                            (len(kernelsToTry) == 1 or pVal < 0.05 /
                             (len(kernelsToTry) - 1)))):
                    bestOptObj = optObj
                    bestPval = pVal
                    bestKernelName = kernelToTry
                    best_hyp0 = hyp0
                    best_sumKernel = sumKernel
                    bestKernel = kernel

            if (bestKernel is not None): kernelsListAll[-1] = bestKernel
            else: kernelsListAll = kernelsListAll[:-1]
            hyp_kernels += best_hyp0
            bestKernelNames.append(bestKernelName)

            if self.verbose: print 'selected kernel:', bestKernelName

        if self.verbose:
            print 'selected kernels:', bestKernelNames
            print

        return bestKernelNames