def _moments_match_analytical(self,data_i,tau_i,v_i): """ Moments match of the marginal approximation in EP algorithm :param i: number of observation (int) :param tau_i: precision of the cavity distribution (float) :param v_i: mean/variance of the cavity distribution (float) """ if data_i == 1: sign = 1. elif data_i == 0: sign = -1 else: raise ValueError("bad value for Bernouilli observation (0,1)") if isinstance(self.gp_link,gp_transformations.Probit): z = sign*v_i/np.sqrt(tau_i**2 + tau_i) Z_hat = std_norm_cdf(z) phi = std_norm_pdf(z) mu_hat = v_i/tau_i + sign*phi/(Z_hat*np.sqrt(tau_i**2 + tau_i)) sigma2_hat = 1./tau_i - (phi/((tau_i**2+tau_i)*Z_hat))*(z+phi/Z_hat) elif isinstance(self.gp_link,gp_transformations.Heaviside): a = sign*v_i/np.sqrt(tau_i) Z_hat = std_norm_cdf(a) N = std_norm_pdf(a) mu_hat = v_i/tau_i + sign*N/Z_hat/np.sqrt(tau_i) sigma2_hat = (1. - a*N/Z_hat - np.square(N/Z_hat))/tau_i if np.any(np.isnan([Z_hat, mu_hat, sigma2_hat])): stop else: raise ValueError("Exact moment matching not available for link {}".format(self.gp_link.gp_transformations.__name__)) return Z_hat, mu_hat, sigma2_hat
def copula(self, z): if type(z) == type(np.ones(1)): z_shape = np.shape(z) z = np.reshape(z, (1, -1))[0] output = np.array(map(lambda z_in: self.marginal.quantile(std_norm_cdf(z_in/np.sqrt(self.k))), z)) output = np.reshape(output, z_shape) else: output = self.marginal.quantile(std_norm_cdf(z/np.sqrt(self.k))) assert not(np.isnan(output).any()), 'function returns NaN' assert not(np.isinf(output).any()), 'function returns inf' return output
def transf(self, z): if type(z) == type(np.ones(1)): z_shape = np.shape(z) z = np.reshape(z, (1, -1))[0] output = np.array(map(lambda z_in: std_norm_cdf(self.copula(z_in)) , z)) output = np.reshape(output, z_shape) else: output = std_norm_cdf(self.copula(z)) assert not(np.isnan(output).any()), 'function returns NaN' assert not(np.isinf(output).any()), 'function returns inf' return output
def transf(self, z): if type(z) == type(np.ones(1)): z_shape = np.shape(z) z = np.reshape(z, (1, -1))[0] output = np.array( map(lambda z_in: std_norm_cdf(self.copula(z_in)), z)) output = np.reshape(output, z_shape) else: output = std_norm_cdf(self.copula(z)) assert not (np.isnan(output).any()), 'function returns NaN' assert not (np.isinf(output).any()), 'function returns inf' return output
def copula(self, z): if type(z) == type(np.ones(1)): z_shape = np.shape(z) z = np.reshape(z, (1, -1))[0] output = np.array( map( lambda z_in: self.marginal.quantile( std_norm_cdf(z_in / np.sqrt(self.k))), z)) output = np.reshape(output, z_shape) else: output = self.marginal.quantile(std_norm_cdf(z / np.sqrt(self.k))) assert not (np.isnan(output).any()), 'function returns NaN' assert not (np.isinf(output).any()), 'function returns inf' return output
def dcopula_dk(self, z): output = self.marginal.dquantile_dcdf(std_norm_cdf( z / np.sqrt(self.k))) * (std_norm_pdf( z / np.sqrt(self.k))) * -0.5 * z * (self.k**(-1.5)) assert not (np.isnan(output).any()), 'function returns NaN' assert not (np.isinf(output).any()), 'function returns inf' return output
def prediction_combine(self): self.comm.barrier() if self.IS: self.proposal_prob[np.where(np.isnan(self.proposal_prob))] = -np.inf self.gather_weights = np.hstack(self.comm.allgather((self.proposal_prob))) self.proposal_prob = np.array_split(np.exp(self.gather_weights-logsumexp(self.gather_weights)), self.P) self.proposal_prob =self.comm.scatter(self.proposal_prob) else: self.proposal_prob[:] = 1./self.total_J self.gather_weights = np.copy(self.proposal_prob) self.gather_Y_predict = self.comm.gather(self.Y_star_predict) self.gather_Y_cov = self.comm.gather([self.Y_star_cov[j].toarray() for j in self.Y_star_cov.keys()]) self.avg_Y_predict = self.comm.reduce(np.dot(self.proposal_prob, self.Y_star_predict)) self.avg_cov = self.comm.reduce( np.sum([self.proposal_prob[j]*self.Y_star_cov[j].toarray() for j in self.Y_star_cov.keys()],axis=0)) self.cluster_sizes = np.array(self.comm.gather([np.unique(self.Z_proposal[j]).size for j in xrange(self.J) ])).flatten() if self.rank ==0: end_time = time.time()- self.start_time if self.classification: self.avg_score = roc_auc_score(self.Y_star.flatten(), std_norm_cdf(self.avg_Y_predict.flatten())) pred_LL = np.clip(np.where(self.Y_star.flatten()==1,self.avg_Y_predict,1.-self.avg_Y_predict), 1e-9 ,np.inf) self.avg_LL = np.log(pred_LL).sum() else: self.avg_score = np.mean((self.avg_Y_predict.flatten() - self.Y_star.flatten())**2) if self.full_cov: self.avg_cov += (1e-6)*np.eye(self.N_star) self.avg_LL = stats.multivariate_normal.logpdf(self.Y_star.flatten(), self.avg_Y_predict.flatten(), self.avg_cov, allow_singular=True) else: self.avg_LL = stats.norm.logpdf(self.Y_star.flatten(), self.avg_Y_predict.flatten(), np.sqrt(np.diag(self.avg_cov))).sum() print("%i %i %i %.2f %.2f %.2f %i %i %i " %(self.total_J, self.N_minibatch, self.K, self.avg_score, self.avg_LL, end_time, int(self.partition == "random"), int(self.IS), int(self.mb_weight == 1.)))
def cdf(self, f): input = (f - self.mu_vec) / (self.sigma_vec) output = np.dot(self.k_vec, np.reshape(std_norm_cdf(input), (1, -1))[0]) assert not (np.isnan(output).any()), 'function returns NaN' assert not (np.isinf(output).any()), 'function returns inf' return output
def bernoulli_d2logpdf_dlink2(f, y): inv_link_f= std_norm_cdf(f) arg = np.where(y==1, inv_link_f, 1.-inv_link_f) ret = -1./np.square(np.clip(arg, 1e-9, 1e9)) if np.any(np.isinf(ret)): raise return ret
def predictive(self, m, v, Y_metadata=None): # This can be checked in eq 4.152 Pattern recognition Bishop # with lambda = 1 mean_pred = std_norm_cdf( m / np.sqrt(1 + v) ) #Here the mean prediction is already influenced by the variance v var_pred = np.ones_like( mean_pred ) * np.nan #Since the prediction is already a probability informing the uncertainty return mean_pred, var_pred #of the prediciton, so with don't need any confidence variance
def dcopula_dz(self, z): output = self.marginal.dquantile_dcdf(std_norm_cdf(z)) * std_norm_pdf(z) assert not(np.isnan(output).any()), 'function returns NaN' assert not(np.isinf(output).any()), 'function returns inf' return output
def bernoulli_logpdf(f, y): inv_link_f= std_norm_cdf(f) p = np.where(y==1, inv_link_f, 1.-inv_link_f) return np.log(np.clip(p, 1e-9 ,np.inf))
def bernoulli_dlogpdf_dlink(f, y): inv_link_f= std_norm_cdf(f) ff = np.clip(inv_link_f, 1e-9, 1-1e-9) denom = np.where(y==1, ff, -(1-ff)) return 1./denom
def copula(self, z): output = self.marginal.quantile(std_norm_cdf(z)) output = np.clip(output, -1.e50, 1.e50) assert not (np.isnan(output).any()), 'function returns NaN' assert not (np.isinf(output).any()), 'function returns inf' return output
def transf(self,f): shiftedf = (f-self.offset)*self.scale return std_norm_cdf(shiftedf)
def transf(self,f): return std_norm_cdf(f)
def dcopula_dz(self, z): output = self.marginal.dquantile_dcdf( std_norm_cdf(z)) * std_norm_pdf(z) assert not (np.isnan(output).any()), 'function returns NaN' assert not (np.isinf(output).any()), 'function returns inf' return output
def dcopula_dk(self, z): output = self.marginal.dquantile_dcdf(std_norm_cdf(z/np.sqrt(self.k))) * (std_norm_pdf(z/np.sqrt(self.k))) * -0.5 * z * (self.k ** (-1.5)) assert not(np.isnan(output).any()), 'function returns NaN' assert not(np.isinf(output).any()), 'function returns inf' return output
def dcopula_dtheta(self, z): output = self.marginal.dquantile_dtheta(std_norm_cdf(z/np.sqrt(self.k))) assert not(np.isnan(output).any()), 'function returns NaN' assert not(np.isinf(output).any()), 'function returns inf' return output
def dcopula_dtheta(self, z): output = self.marginal.dquantile_dtheta( std_norm_cdf(z / np.sqrt(self.k))) assert not (np.isnan(output).any()), 'function returns NaN' assert not (np.isinf(output).any()), 'function returns inf' return output
def cdf(self, f): output = std_norm_cdf((f - self.mu) / self.sigma) assert not (np.isnan(output).any()), 'function returns NaN' assert not (np.isinf(output).any()), 'function returns inf' return output
def copula(self, z): output = self.marginal.quantile(std_norm_cdf(z)) output = np.clip(output, -1.e50, 1.e50) assert not(np.isnan(output).any()), 'function returns NaN' assert not(np.isinf(output).any()), 'function returns inf' return output
def cdf(self, f): output = std_norm_cdf((f - self.mu)/self.sigma) assert not(np.isnan(output).any()), 'function returns NaN' assert not(np.isinf(output).any()), 'function returns inf' return output
def cdf(self, f): input = (f - self.mu_vec)/(self.sigma_vec) output = np.dot(self.k_vec, np.reshape(std_norm_cdf(input), (1, -1))[0]) assert not(np.isnan(output).any()), 'function returns NaN' assert not(np.isinf(output).any()), 'function returns inf' return output