def uncollapsed_likelihood(self, X, parameters): """ Calculates the score of the data X under this component model with mean mu and precision kappa. Inputs: X: A column of data (numpy) parameters: a dict with the following keys mu: the Von Mises mean kappa: the precision of the Von Mises """ check_data_type_column_data(X) check_model_params_dict(parameters) mu = parameters['mu'] kappa = parameters['kappa'] N = float(len(X)) hypers = self.get_hypers() a = hypers['a'] b = hypers['b'] kappa = hypers['kappa'] sum_err = numpy.sum((mu-X)**2.0) log_likelihood = self.log_likelihood(X, {'mu':mu, 'kappa':rho}) log_prior_mu = vonmises.logpdf(b, a) log_p = log_likelihood + log_prior_mu + log_prior_rho return log_p
def uncollapsed_likelihood(self, X, parameters): """ Calculates the score of the data X under this component model with mean mu and precision kappa. Inputs: X: A column of data (numpy) parameters: a dict with the following keys mu: the Von Mises mean kappa: the precision of the Von Mises """ check_data_type_column_data(X) check_model_params_dict(parameters) mu = parameters['mu'] kappa = parameters['kappa'] N = float(len(X)) hypers = self.get_hypers() a = hypers['a'] b = hypers['b'] kappa = hypers['kappa'] sum_err = numpy.sum((mu - X)**2.0) log_likelihood = self.log_likelihood(X, {'mu': mu, 'kappa': rho}) log_prior_mu = vonmises.logpdf(b, a) log_p = log_likelihood + log_prior_mu + log_prior_rho return log_p
def neg_log_likelihood(self, coeff, obsTime, shiftStart, shiftEnd): location, kappa = np.abs(coeff) return ( -np.sum(vonmises.logpdf(obsTime, kappa, location, 12 / np.pi)) + np.sum( np.log( self.interval_probability(shiftStart, shiftEnd, location, kappa))))
def test_vonmises_logpdf(T=100, K=4, D=10): # Test single datapoint log pdf x = npr.vonmises(0, 1, size=(T, D)) mus = npr.randn(K, D) kappas = np.exp(npr.randn(K, D)) ll1 = vonmises_logpdf(x[:, None, :], mus, kappas) ll2 = np.sum(vonmises.logpdf(x[:, None, :], kappas[None, :, :], loc=mus[None, :, :]), axis=-1) assert np.allclose(ll1, ll2)
def log_pdf(X, parameters): """ Calculates the pdf for each point in the data X given mean mu and precision kappa. Inputs: X: a column of data (numpy) parameters: a dict with the following keys mu: the Von Mises mean kappa: the precision of the Von Mises """ check_data_type_column_data(X) check_model_params_dict(parameters) return vonmises.logpdf(X--math.pi, parameters['kappa'],loc=parameters['mu']-math.pi)
def test_vonmises_logpdf(T=100, K=4, D=10): """ NOTE: Skipping this test until the scipy special functions make it into the release version of autograd. """ # Test single datapoint log pdf x = npr.vonmises(0, 1, size=(T, D)) mus = npr.randn(K, D) kappas = np.exp(npr.randn(K, D)) ll1 = vonmises_logpdf(x[:, None, :], mus, kappas) ll2 = np.sum(vonmises.logpdf(x[:, None, :], kappas[None, :, :], loc=mus[None, :, :]), axis=-1) assert np.allclose(ll1, ll2)
def log_likelihood(X, parameters): """ Calculates the log likelihood of the data X given mean mu and precision kappa. Inputs: X: a column of data (numpy) parameters: a dict with the following keys mu: the Von Mises mean kappa: the precision of the Von Mises """ check_data_type_column_data(X) check_model_params_dict(parameters) log_likelihood = numpy.sum(vonmises.logpdf(X-math.pi, parameters['mu']-math.pi, parameters['kappa'])) return log_likelihood
def log_pdf(X, parameters): """ Calculates the pdf for each point in the data X given mean mu and precision kappa. Inputs: X: a column of data (numpy) parameters: a dict with the following keys mu: the Von Mises mean kappa: the precision of the Von Mises """ check_data_type_column_data(X) check_model_params_dict(parameters) return vonmises.logpdf(X - -math.pi, parameters['kappa'], loc=parameters['mu'] - math.pi)
def log_likelihood(X, parameters): """ Calculates the log likelihood of the data X given mean mu and precision kappa. Inputs: X: a column of data (numpy) parameters: a dict with the following keys mu: the Von Mises mean kappa: the precision of the Von Mises """ check_data_type_column_data(X) check_model_params_dict(parameters) log_likelihood = numpy.sum( vonmises.logpdf(X - math.pi, parameters['mu'] - math.pi, parameters['kappa'])) return log_likelihood
print( timeit("lp(a, kappa, loc, scale)", number=1000, globals={ **globals(), **locals() })) print( timeit("vonmises_logpdf(a, kappa, loc, scale)", number=1000, globals={ **globals(), **locals() })) b1 = vonmises.logpdf(a, kappa, loc, scale) b2 = vonmises_logpdf(a, kappa, loc, scale) print(a) print(b1) print(b2) print(np.abs(b1 - b2)) sys.exit() np.random.seed() x = np.arange(1, 10 + 1) d = np.random.choice(x, 25, replace=True) y = np.cumsum(np.ones(x.size) / x.size) x = np.arange(16)