def derivMarginalLikelihood(params,trainingSet,PackParams,thisCovarClass, useDerivs): """ NAME: derivMarginalLikelihood PURPOSE: evaluate the derivative of the marginal (log) likelihood INPUT: params - ndarray consisting of covarHyper - a set of covariance Hyperparameters These can be unpacked by using the GPparams trainingSet - trainingSet object PackParams - parameters describing the GP (to unpack the parameters) thisCovarClass - covariance class OUTPUT: derivative of the log of the marginal likelihood HISTORY: 2010-07-25 - Written - Bovy (NYU) """ hyperParamsDict= unpack_params(params,PackParams) covarFunc= PackParams.covarFunc(**hyperParamsDict) N= trainingSet.nTraining #Calculate Ky Ky= _calc_ky(covarFunc,trainingSet,N) try: (Kyinv,Kylogdet)= fast_cholesky_invert(Ky,logdet=True) except (numpy.linalg.linalg.LinAlgError,ValueError): print "Warning: " return -numpy.finfo(numpy.dtype(numpy.float64)).max #All the derivatives return dictionaries with keys corresponding to the #hyper-parameters Kydothyper= _calc_kydot_hyper(covarFunc,trainingSet,N,hyperParamsDict) alpha= numpy.dot(Kyinv,trainingSet.listy) aat= numpy.outer(alpha,alpha) Ldothyper= {} for key in hyperParamsDict: Ldothyper[key]= 0.5*numpy.trace(numpy.dot(Kyinv-aat,Kydothyper[key])) #Now pack the derivatives, by cleverly re-using the packing routine fakeCovar= thisCovarClass.covarFunc(**Ldothyper) (derivs,derivspacking)= pack_params(fakeCovar,None,None) #print "derivs", derivs return derivs
def marginalLikelihood(params,trainingSet,PackParams,thisCovarClass, thisMeanClass=None, useDerivs=False): """ NAME: marginalLikelihood PURPOSE: evaluate minus the marginal (log) likelihood INPUT: params - ndarray consisting of covarHyper - a set of covariance Hyperparameters These can be unpacked by using the PackParams trainingSet - trainingSet object PackParams - parameters describing the GP (to unpack the parameters) thisCovarClass - covariance class (not used in this function) thisMeanClass - same as previous two, but for the mean function OUTPUT: log of the marginal likelihood HISTORY: 2010-07-25 - Written - Bovy (NYU) """ hyperParamsDict= unpack_params(params,PackParams) covarFunc= PackParams.covarFunc(**hyperParamsDict) meanFunc= PackParams.meanFunc(**hyperParamsDict) N= trainingSet.nTraining #Calculate Ky Ky= _calc_ky(covarFunc,trainingSet,N) try: (Kyinv,Kylogdet)= fast_cholesky_invert(Ky,logdet=True) except (numpy.linalg.linalg.LinAlgError,ValueError): if useDerivs: print "Warning: parameter space includes non-positive definite regions; consider using useDerivs=False" return numpy.finfo(numpy.dtype(numpy.float64)).max datamean= _calc_mean(meanFunc,trainingSet,N) chi2= numpy.dot((trainingSet.listy-datamean), numpy.dot(Kyinv,(trainingSet.listy-datamean))) return 0.5*(chi2+Kylogdet+N*_LOGTWOPI)