def brute_force_marginal_likelihood(self, X, n_samples=10000, gen_seed=0): """ Calculates the log marginal likelihood via brute force method in which parameters (mu and kappa) are repeatedly drawn from the prior, the likelihood is calculated for each set of parameters, then the average is taken. Inputs: X: A column of data (numpy) n_samples: the number of draws gen_Seed: seed for the rng """ check_data_type_column_data(X) if type(n_samples) is not int: raise TypeError("n_samples should be an int") if n_samples <= 0: raise ValueError("n_samples should be greater than 0") if type(gen_seed) is not int: raise TypeError("gen_seed should be an int") N = float(len(X)) rng = random.Random(gen_seed) log_likelihoods = [0]*n_samples for i in range(n_samples): params = self.sample_parameters_given_hyper(gen_seed=next_seed(rng)) log_likelihoods[i] = self.log_likelihood(X, params) log_marginal_likelihood = logmeanexp(log_likelihoods) return log_marginal_likelihood
def brute_force_marginal_likelihood(self, X, n_samples=10000, gen_seed=0): """ Calculates the log marginal likelihood via brute force method in which parameters (weights) are repeatedly drawn from the prior, the likelihood is calculated for each set of parameters, then the average is taken. Inputs: X: A column of data (numpy) n_samples: the number of draws gen_Seed: seed for the rng """ check_data_type_column_data(X) if type(n_samples) is not int: raise TypeError("n_samples should be an int") if n_samples <= 0: raise ValueError("n_samples should be greater than 0") if type(gen_seed) is not int: raise TypeError("gen_seed should be an int") hypers = self.get_hypers() K = hypers[b'K'] check_data_vs_k(X,K) rng = numpy.random.RandomState(gen_seed) log_likelihoods = [0]*n_samples for i in range(n_samples): params = self.sample_parameters_given_hyper(gen_seed=next_seed(rng)) log_likelihoods[i] = self.log_likelihood(X, params) log_marginal_likelihood = logmeanexp(log_likelihoods) return log_marginal_likelihood
def predictive_probability_multistate(M_c, X_L_list, X_D_list, Y, Q): """ Returns the predictive probability, averaged over each sample. """ logprobs = [float(predictive_probability(M_c, X_L, X_D, Y, Q)) for X_L, X_D in zip(X_L_list, X_D_list)] return logmeanexp(logprobs)
def test_logmeanexp(): inf = float('inf') nan = float('nan') assert gu.logmeanexp([]) == -inf assert relerr(992.550919866405, gu.logmeanexp(range(1000))) < 1e-15 assert gu.logmeanexp([-1000., -1000.]) == -1000. assert relerr(math.log(0.5 * (1 + math.exp(-1.))), gu.logmeanexp([0., -1.])) \ < 1e-15 assert relerr(math.log(0.5), gu.logmeanexp([0., -1000.])) < 1e-15 assert relerr(-3 - math.log(2.), gu.logmeanexp([-inf, -3])) < 1e-15 assert relerr(-3 - math.log(2.), gu.logmeanexp([-3, -inf])) < 1e-15 assert gu.logmeanexp([+inf, -3]) == +inf assert gu.logmeanexp([-3, +inf]) == +inf assert gu.logmeanexp([-inf, 0, +inf]) == +inf assert math.isnan(gu.logmeanexp([nan, inf])) assert math.isnan(gu.logmeanexp([nan, -3])) assert math.isnan(gu.logmeanexp([nan]))