示例#1
0
 def proceed(self, y=None, mu=None, s2=None, inffunc=None, der=None, nargout=1):
     sn2 = np.exp(2. * self.hyp[0])
     if inffunc == None:                                  # prediction mode 
         if y == None:
             y = np.zeros_like(mu)
         s2zero = True
         if (not s2==None) and np.linalg.norm(s2) > 0:
             s2zero = False     
         if s2zero:                                       # log probability
             lp = -(y-mu)**2 /sn2/2 - np.log(2.*np.pi*sn2)/2. 
             s2 = 0.
         else:
             inf_func = inf.infEP()                       # prediction
             lp = self.proceed(y, mu, s2, inf_func)
         if nargout>1:
             ymu = mu                                     # first y moment
             if nargout>2:
                 ys2 = s2 + sn2                           # second y moment
                 varargout = [lp,ymu,ys2]
             else:
                 varargout = [lp,ymu]
         else:
             varargout = lp
                    
     else:
         if isinstance(inffunc, inf.infEP):
             if der == None:                                                 # no derivative mode
                 lZ = -(y-mu)**2/(sn2+s2)/2. - np.log(2*np.pi*(sn2+s2))/2.   # log part function
                 if nargout>1:
                     dlZ  = (y-mu)/(sn2+s2)                          # 1st derivative w.r.t. mean
                     if nargout>2:
                         d2lZ = -1/(sn2+s2)                          # 2nd derivative w.r.t. mean
                         varargout = [lZ,dlZ,d2lZ]
                     else:
                        varargout = [lZ,dlZ]
                 else:
                     varargout = lZ
             else:                                                      # derivative mode
                 dlZhyp = ((y-mu)**2/(sn2+s2)-1) / (1+s2/sn2)           # deriv. w.r.t. hyp.lik
                 varargout = dlZhyp
         '''
         elif isinstance(inffunc, infLaplace):
             if der == None:                                                 # no derivative mode
                 if not y: 
                     y=0 
                 ymmu = y-mu
                 lp = -ymmu**2/(2*sn2) - np.log(2*np.pi*sn2)/2. 
                 if nargout>1:
                     dlp = ymmu/sn2                          # dlp, derivative of log likelihood
                     if nargout>2:                           # d2lp, 2nd derivative of log likelihood
                         d2lp = -np.ones_like(ymmu)/sn2
                         if nargout>3:                       # d3lp, 3rd derivative of log likelihood
                             d3lp = np.zeros_like(ymmu)
                             varargout = [lp,dlp,d2lp,d3lp]
                         else:
                             varargout = [lp,dlp,d2lp]
                     else:
                         varargout = [lp,dlp]
                 else:
                     varargout = lp
             else:                                          # derivative mode
                 lp_dhyp   = (y-mu)**2/sn2 - 1              # derivative of log likelihood w.r.t. hypers
                 dlp_dhyp  = 2*(mu-y)/sn2                   # first derivative,
                 d2lp_dhyp = 2*np.ones_like(mu)/sn2         # and also of the second mu derivative
                 varargout = [lp_dhyp,dlp_dhyp,d2lp_dhyp]
         
         elif isinstance(inffunc, infVB):
             if der == None:
                 # variational lower site bound
                 # t(s) = exp(-(y-s)^2/2sn2)/sqrt(2*pi*sn2)
                 # the bound has the form: b*s - s.^2/(2*ga) - h(ga)/2 with b=y/ga
                 ga  = s2
                 n   = len(ga)
                 b   = y/ga
                 y   = y*np.ones((n,1))
                 db  = -y/ga**2 
                 d2b = 2*y/ga**3
                 h   = np.zeros((n,1))
                 dh  = h
                 d2h = h                             # allocate memory for return args
                 id  = (ga <= sn2 + 1e-8)            # OK below noise variance
                 h[id]   = y[id]**2/ga[id] + np.log(2*np.pi*sn2)
                 h[np.logical_not(id)] = np.inf
                 dh[id]  = -y[id]**2/ga[id]**2
                 d2h[id] = 2*y[id]**2/ga[id]**3
                 id = ga < 0
                 h[id] = np.inf
                 dh[id] = 0
                 d2h[id] = 0                        # neg. var. treatment
                 varargout = [h,b,dh,db,d2h,d2b]
             else:
                 ga = s2 
                 n  = len(ga)
                 dhhyp = np.zeros((n,1))
                 dhhyp[ga<=sn2] = 2
                 dhhyp[ga<0] = 0                   # negative variances get a special treatment
                 varargout = dhhyp                 # deriv. w.r.t. hyp.lik
         else:
             raise Exception('Incorrect inference in lik.Gauss\n')
     '''
     return varargout
示例#2
0
    def proceed(self, y=None, mu=None, s2=None, inffunc=None, der=None, nargout=1):
        if not y == None:
            y = np.sign(y)
            y[y==0] = 1
        else:
            y = 1; # allow only +/- 1 values
        if inffunc == None:                              # prediction mode if inf is not present
            y = y*np.ones_like(mu)                                       # make y a vector
            s2zero = True; 
            if not s2 == None: 
                if np.linalg.norm(s2)>0:
                    s2zero = False                        # s2==0?       
            if s2zero:                                         # log probability evaluation
                [p,lp] = self.cumGauss(y,mu,2)
            else:                                                              # prediction
                lp = self.proceed(y, mu, s2, inf.infEP())
                p = np.exp(lp)
            if nargout>1:
                ymu = 2*p-1                                                # first y moment
                if nargout>2:
                    ys2 = 4*p*(1-p)                                        # second y moment
                    varargout = [lp,ymu,ys2]
                else:
                    varargout = [lp,ymu]
            else:
                varargout = lp
        else:                                                   # inference mode
            if isinstance(inffunc, inf.infLaplace):
                if der == None:                                  # no derivative mode
                    f = mu; yf = y*f                             # product latents and labels
                    [p,lp] = self.cumGauss(y,f,2)
                    if nargout>1:                                # derivative of log likelihood
                        n_p = self.gauOverCumGauss(yf,p)
                        dlp = y*n_p                              # derivative of log likelihood
                        if nargout>2:                            # 2nd derivative of log likelihood
                            d2lp = -n_p**2 - yf*n_p
                            if nargout>3:                        # 3rd derivative of log likelihood
                                d3lp = 2*y*n_p**3 + 3*f*n_p**2 + y*(f**2-1)*n_p 
                                varargout = [lp,dlp,d2lp,d3lp]
                            else:
                                varargout = [lp,dlp,d2lp]
                        else:
                            varargout = [lp,dlp]
                    else:
                        varargout = lp
                else:                                              # derivative mode
                    varargout = nargout*[]                         # derivative w.r.t. hypers

            elif isinstance(inffunc, inf.infEP):
                if der == None:                                            # no derivative mode
                    z = mu/np.sqrt(1+s2) 
                    [junk,lZ] = self.cumGauss(y,z,2)                            # log part function
                    if not y == None:
                         z = z*y
                    if nargout>1:
                        if y == None: y = 1
                        n_p = self.gauOverCumGauss(z,np.exp(lZ))
                        dlZ = y*n_p/np.sqrt(1.+s2)                      # 1st derivative wrt mean
                        if nargout>2:
                            d2lZ = -n_p*(z+n_p)/(1.+s2)                 # 2nd derivative wrt mean
                            varargout = [lZ,dlZ,d2lZ]
                        else:
                            varargout = [lZ,dlZ]
                    else:
                        varargout = lZ
                else:                                                   # derivative mode
                    varargout = []                                     # deriv. wrt hyp.lik
        '''
        if inffunc == 'inf.infVB':
            if der == None:                                           # no derivative mode
                # naive variational lower bound based on asymptotical properties of lik
                # normcdf(t) -> -(t*A_hat^2-2dt+c)/2 for t->-np.inf (tight lower bound)
                d =  0.158482605320942;
                c = -1.785873318175113;
                ga = s2; n = len(ga); b = d*y*np.ones((n,1)); db = np.zeros((n,1)); d2b = db
                h = -2.*c*np.ones((n,1)); h[ga>1] = np.inf; dh = np.zeros((n,1)); d2h = dh   
                varargout = [h,b,dh,db,d2h,d2b]
            else:                                                  # derivative mode
                varargout = []                                     # deriv. wrt hyp.lik
        '''
        return varargout