def __init__(self, random_state=None, mu=0.0, sigma=1.0): super(Normal, self).__init__(mu=mu, sigma=sigma, random_state=random_state, optimizer=None) # pdf self.pdf_ = ( (1. / np.sqrt(2. * np.pi)) / self.sigma * T.exp(-(self.X - self.mu) ** 2 / (2. * self.sigma ** 2))).ravel() self.make_(self.pdf_, "pdf") # -log pdf self.nnlf_ = bound( T.log(self.sigma) + T.log(np.sqrt(2. * np.pi)) + (self.X - self.mu) ** 2 / (2. * self.sigma ** 2), np.inf, self.sigma > 0.).ravel() self.make_(self.nnlf_, "nnlf") # cdf self.cdf_ = 0.5 * (1. + T.erf((self.X - self.mu) / (self.sigma * np.sqrt(2.)))).ravel() self.make_(self.cdf_, "cdf") # ppf self.ppf_ = (self.mu + np.sqrt(2.) * self.sigma * T.erfinv(2. * self.p - 1.)) self.make_(self.ppf_, "ppf", args=[self.p])
def __init__(self, mu=0.0, sigma=1.0): super(Normal, self).__init__(mu=mu, sigma=sigma) # pdf self.pdf_ = ((1. / np.sqrt(2. * np.pi)) / self.sigma * T.exp(-(self.X - self.mu)**2 / (2. * self.sigma**2))).ravel() self.make_(self.pdf_, "pdf") # -log pdf self.nll_ = bound( T.log(self.sigma) + T.log(np.sqrt(2. * np.pi)) + (self.X - self.mu)**2 / (2. * self.sigma**2), np.inf, self.sigma > 0.).ravel() self.make_(self.nll_, "nll") # cdf self.cdf_ = 0.5 * (1. + T.erf( (self.X - self.mu) / (self.sigma * np.sqrt(2.)))).ravel() self.make_(self.cdf_, "cdf") # ppf self.ppf_ = (self.mu + np.sqrt(2.) * self.sigma * T.erfinv(2. * self.p - 1.)) self.make_(self.ppf_, "ppf", args=[self.p])
def get_output_for(self, inputs, **kwargs): """ :param inputs[0]: (batch, 600) :param inputs[1]: (batch, 1) :return: """ result = (T.erf( T.erfinv( T.clip(inputs[1].dimshuffle(0,'x',1)*2-1, -1+3e-8, 1-3e-8) ) * inputs[0].dimshuffle(0,1,'x') )+1)/2 return result[:,0,:]
def get_output_for(self, inputs, **kwargs): """ :param inputs[0]: (batch, 600) :param inputs[1]: (batch, 1) :return: """ result = (T.erf( T.erfinv( T.clip(inputs[1].dimshuffle(0, 'x', 1) * 2 - 1, -1 + 3e-8, 1 - 3e-8)) * inputs[0].dimshuffle(0, 1, 'x')) + 1) / 2 return result[:, 0, :]
def __init__(self, mu=0.0, sigma=1.0): """Constructor. Parameters ---------- * `mu` [float]: The distribution mean. * `sigma` [float]: The distribution standard deviation. """ super(Normal, self).__init__(mu=mu, sigma=sigma) # pdf self.pdf_ = ( (1. / np.sqrt(2. * np.pi)) / self.sigma * T.exp(-(self.X - self.mu) ** 2 / (2. * self.sigma ** 2))).ravel() self._make(self.pdf_, "pdf") # -log pdf self.nll_ = bound( T.log(self.sigma) + T.log(np.sqrt(2. * np.pi)) + (self.X - self.mu) ** 2 / (2. * self.sigma ** 2), np.inf, self.sigma > 0.).ravel() self._make(self.nll_, "nll") # cdf self.cdf_ = 0.5 * (1. + T.erf((self.X - self.mu) / (self.sigma * np.sqrt(2.)))).ravel() self._make(self.cdf_, "cdf") # ppf self.ppf_ = (self.mu + np.sqrt(2.) * self.sigma * T.erfinv(2. * self.p - 1.)) self._make(self.ppf_, "ppf", args=[self.p])
def theano(self, x): return tt.erfinv(x)
def Phi_inv(y, eps=3e-8): """ eps was calibrated for cublas.erfinv using float32 """ temp = 2. * y - 1. erfinv_input = T.clip(temp, -1+eps, 1-eps) rval = avg + std * SQRT2 * T.erfinv(erfinv_input) return rval.astype(dtype)
def phi_inv(phi_x): erfinv_input = T.clip(2. * phi_x - 1., -1. + 1e-6, 1. - 1e-6) rval = avg + std * SQRT2 * T.erfinv(erfinv_input) return rval.astype(dtype)
def inverse_theano(self, y): x = tt.switch(tt.abs_(y) < self.b, tt.erfinv(y) * 2.**0.5, (self.alpha * y**2 + self.gamma) * tt.sgn(y) + self.beta * y) return x
def phi_inv(phi_x): erfinv_input = T.clip(2. * phi_x - 1., -1.+1e-6, 1.-1e-6) rval = avg + std * SQRT2 * T.erfinv(erfinv_input) return rval.astype(dtype)
#%% M = np.stack([[[1,0],[0,1]], [[-1,0],[-1,1]], [[0,-1],[1,-1]]]) MS = T.dot(M, S).dimshuffle((0,2,1,3)).reshape((ngroup*nchoice, nchoice-1, nchoice-1)) Sigma = T.batched_dot(MS, MS.dimshuffle((0,2,1))).reshape((nchoice, ngroup, nchoice-1, nchoice-1)) #%% c00 = T.sqrt(Sigma[:,:,0,0]) c10 = Sigma[:,:,1,0]/c00 c11 = T.sqrt(Sigma[:,:,1,1] - c10**2) iii = (choice-1, groupid) normcdf = lambda x: 0.5 + 0.5*T.erf(x/np.sqrt(2)) norminv = lambda p: np.sqrt(2)*T.erfinv(2*p-1) ndraws = 10 #draws = np.random.random((ndraws,nobs)) draws = (np.tile(np.arange(ndraws), (nobs,1)).transpose() + 0.5)/ndraws prob0 = normcdf(-Vnonchoice[0,:]/c00[iii]) draws1 = norminv(draws*prob0) prob1 = normcdf(-(Vnonchoice[1,:] + c10[iii]*draws1)/c11[iii]) nlogl_i = -T.log(prob0) - T.log(prob1.sum(axis=0)/ndraws) nlogl = nlogl_i.sum() obj = nlogl eval_f = theano.function([theta], outputs = obj)