def test_logsumexp(): from galpy.util import logsumexp sumthis= numpy.array([[0.,1.]]) sum= numpy.log(numpy.exp(0.)+numpy.exp(1.)) assert numpy.all(numpy.fabs(logsumexp(sumthis,axis=0)-sumthis) < 10.**-10.), 'galpy.util.logsumexp did not work as expected' assert numpy.fabs(logsumexp(sumthis,axis=1)-sum) < 10.**-10., 'galpy.util.logsumexp did not work as expected' assert numpy.fabs(logsumexp(sumthis,axis=None)-sum) < 10.**-10., 'galpy.util.logsumexp did not work as expected' return None
def test_logsumexp(): from galpy.util import logsumexp sumthis = numpy.array([[0., 1.]]) sum = numpy.log(numpy.exp(0.) + numpy.exp(1.)) assert numpy.all( numpy.fabs(logsumexp(sumthis, axis=0) - sumthis) < 10.**-10. ), 'galpy.util.logsumexp did not work as expected' assert numpy.fabs( logsumexp(sumthis, axis=1) - sum) < 10.**-10., 'galpy.util.logsumexp did not work as expected' assert numpy.fabs( logsumexp(sumthis, axis=None) - sum) < 10.**-10., 'galpy.util.logsumexp did not work as expected' return None
def __call__(self, x, h=None, log=False, scale=True, sx2=None): """ NAME: __call__ PURPOSE: return the density INPUT: x - evaluate at this x [nx,dim] log= (False) if True, return the log h= (None) if set, use this bandwidth scale= (True), if False, don't rescale first sx2= uncertainty variance of x OUTPUT: density (or log) HISTORY: 2012-11-12 - Written - Bovy (IAS) """ if h is None: thish = self._h else: thish = h x = self._prepare_x(x, scale) divh = numpy.tile(thish * self._lambda.T, (x.shape[0], 1, 1)) if not sx2 is None: divh = numpy.sqrt(divh**2. + numpy.tile(sx2.T, (self._ndata, 1, 1)).T) thiskernel = self._kernel(numpy.tile(x.T, (self._ndata, 1, 1)).T / divh, numpy.tile(self._data.T, (x.shape[0], 1, 1)) / divh, log=log) if log: return logsumexp(thiskernel+numpy.tile(numpy.log(self._w),(x.shape[0],1))\ # return -self._dim*numpy.log(thish)\ -numpy.sum(numpy.log(divh),axis=1),axis=1) #latter assumes that lambda are spherical # -self._dim*numpy.log(self._lambda[:,0]),axis=1) #latter assumes that lambda are spherical else: return 1./thish**self._dim\ *numpy.sum(numpy.tile(self._w,(x.shape[0],1))\ *thiskernel/self._lambda[:,0]**self._dim,axis=1)
def __call__(self,x,h=None,log=False,scale=True,sx2=None): """ NAME: __call__ PURPOSE: return the density INPUT: x - evaluate at this x [nx,dim] log= (False) if True, return the log h= (None) if set, use this bandwidth scale= (True), if False, don't rescale first sx2= uncertainty variance of x OUTPUT: density (or log) HISTORY: 2012-11-12 - Written - Bovy (IAS) """ if h is None: thish= self._h else: thish= h x= self._prepare_x(x,scale) divh= numpy.tile(thish*self._lambda.T,(x.shape[0],1,1)) if not sx2 is None: divh= numpy.sqrt(divh**2. +numpy.tile(sx2.T,(self._ndata,1,1)).T) thiskernel= self._kernel(numpy.tile(x.T,(self._ndata,1,1)).T/divh, numpy.tile(self._data.T,(x.shape[0],1,1))/divh, log=log) if log: return logsumexp(thiskernel+numpy.tile(numpy.log(self._w),(x.shape[0],1))\ # return -self._dim*numpy.log(thish)\ -numpy.sum(numpy.log(divh),axis=1),axis=1) #latter assumes that lambda are spherical # -self._dim*numpy.log(self._lambda[:,0]),axis=1) #latter assumes that lambda are spherical else: return 1./thish**self._dim\ *numpy.sum(numpy.tile(self._w,(x.shape[0],1))\ *thiskernel/self._lambda[:,0]**self._dim,axis=1)