(self, x, previous=False)
return res
return log-likelihood of `x` regarding the current sample distribution
return log-likelihood of `x` regarding the current sample distribution
return log - likelihood of x regarding the current sample distribution
def loglikelihood(self, x, previous=False):
        """return log-likelihood of `x` regarding the current sample distribution"""
        # testing of original fct: MC integrate must be one: mean(p(x_i)) * volume(where x_i are uniformely sampled)
        # for i in range(3): print mean([cma.likelihood(20*r-10, dim * [0], None, 3) for r in rand(10000,dim)]) * 20**dim
        # TODO: test this!!
        # c=cma.fmin...
        # c[3]['cma'].loglikelihood(...)

        if previous and hasattr(self, 'lastiter'):
            sigma = self.lastiter.sigma
            Crootinv = self.lastiter._Crootinv
            xmean = self.lastiter.mean
            D = self.lastiter.D
        elif previous and self.countiter > 1:
            raise _Error('no previous distribution parameters stored, check options importance_mixing')
        else:
            sigma = self.sigma
            Crootinv = self._Crootinv
            xmean = self.mean
            D = self.D

        dx = array(x) - xmean  # array(x) - array(m)
        n = self.N
        logs2pi = n * log(2*np.pi) / 2.
        logdetC = 2 * sum(log(D))
        dx = np.dot(Crootinv, dx)
        res = -sum(dx**2) / sigma**2 / 2 - logs2pi - logdetC/2 - n*log(sigma)
        if 1 < 3: # testing
            s2pi = (2*np.pi)**(n/2.)
            detC = np.prod(D)**2
            res2 = -sum(dx**2) / sigma**2 / 2 - log(s2pi * abs(detC)**0.5 * sigma**n)
            assert res2 < res + 1e-8 or res2 > res - 1e-8
        return res
def loglikelihood ( self , x , previous = False ) : # testing of original fct: MC integrate must be one: mean(p(x_i)) * volume(where x_i are uniformely sampled) # for i in range(3): print mean([cma.likelihood(20*r-10, dim * [0], None, 3) for r in rand(10000,dim)]) * 20**dim # TODO: test this!! # c=cma.fmin... # c[3]['cma'].loglikelihood(...) if previous and hasattr ( self , 'lastiter' ) : sigma = self . lastiter . sigma Crootinv = self . lastiter . _Crootinv xmean = self . lastiter . mean D = self . lastiter . D elif previous and self . countiter > 1 : raise _Error ( 'no previous distribution parameters stored, check options importance_mixing' ) else : sigma = self . sigma Crootinv = self . _Crootinv xmean = self . mean D = self . D dx = array ( x ) - xmean # array(x) - array(m) n = self . N logs2pi = n * log ( 2 * np . pi ) / 2. logdetC = 2 * sum ( log ( D ) ) dx = np . dot ( Crootinv , dx ) res = - sum ( dx ** 2 ) / sigma ** 2 / 2 - logs2pi - logdetC / 2 - n * log ( sigma ) if 1 < 3 : # testing s2pi = ( 2 * np . pi ) ** ( n / 2. ) detC = np . prod ( D ) ** 2 res2 = - sum ( dx ** 2 ) / sigma ** 2 / 2 - log ( s2pi * abs ( detC ) ** 0.5 * sigma ** n ) assert res2 < res + 1e-8 or res2 > res - 1e-8 return res