def boom(self): """ Return the boom.GaussianModel corresponding to this object's parameters. """ import BayesBoom.boom as boom return boom.GaussianModel(self.mu, self.sigma)
def test_parameters(self): """When parameters are modified outside the object, the object properties should change. This is testing that pointers are being stored. """ model = boom.GaussianModel(0, 1) mu_prm = model.mean_parameter mu_prm.set(2.0) self.assertAlmostEqual(model.mean, 2.0)
def __init__(self, y, sigma_prior=None, initial_state_prior=None, sdy=None, initial_y=None): """ Args: y: The data to be modeled. If sdy and initial_y are supplied this is not used. sigma_prior: An object of class boom.GammaModelBase serving as the prior on the precision (reciprocal variance) of the innovation terms. If None then 'sdy' will be used to choose a defalt. initial_state_prior: An object of class boom.GaussianModel serving as the prior distribution on the value of the state at time 0 (the time of the first observation). If None then initial_y and sdy will be used to choose a defalt. sdy: The standard deviation of y. If None then this will be computed from y. This argument is primarily intended to handle unusual cases where 'y' is unavailable. initial_y: The first element of y. If None then this will be computed from y. This argument is primarily intended to handle unusual cases where 'y' is unavailable. Returns: A StateModel object representing a local level model. """ if sigma_prior is None: if sdy is None: sdy = np.std(y) sigma_prior = R.SdPrior(sigma_guess=.01 * sdy, sample_size=.01, upper_limit=sdy) if not isinstance(sigma_prior, R.SdPrior): raise Exception("sigma_prior should be an R.SdPrior.") if initial_state_prior is None: if initial_y is None: initial_y = y[0] if sdy is None: sdy = np.std(y) initial_y = float(initial_y) sdy = float(sdy) initial_state_prior = boom.GaussianModel(initial_y, sdy**2) if not isinstance(initial_state_prior, boom.GaussianModel): raise Exception( "initial_state_prior should be a boom.GaussianModel.") self._state_model = boom.LocalLevelStateModel() self._state_model.set_initial_state_mean(initial_state_prior.mu) self._state_model.set_initial_state_variance( initial_state_prior.sigsq) innovation_precision_prior = boom.ChisqModel( sigma_prior.sigma_guess, sigma_prior.sample_size) state_model_sampler = self._state_model.set_posterior_sampler( innovation_precision_prior) state_model_sampler.set_sigma_upper_limit(sigma_prior.upper_limit) self._state_contribution = None
def test_data(self): model = boom.GaussianModel(0, 1) mu = -16 sigma = 7 data = np.random.randn(10000) * sigma + mu model.set_data(boom.Vector(data)) model.mle() self.assertLess(np.abs(model.mean - mu), 4 * sigma / np.sqrt(len(data))) self.assertLess(np.abs(model.sd - sigma), .1)
def _set_initial_distribution(self, y, initial_level_prior, initial_slope_prior, sdy, initial_y): """ A utility called by the constructor. See the __init__ method for argument documentation. """ if initial_level_prior is None: sdy = self._compute_sdy(sdy, y, "initial_level_prior") if initial_y is None: if y is None: raise Exception( "One of initial_y, y, or initial_level_prior must be " "specified.") else: initial_y = y[0] initial_level_prior = boom.GaussianModel(initial_y, sdy) if not isinstance(initial_level_prior, boom.GaussianModel): raise Exception("Unexpected type for initial_level_prior.") if initial_slope_prior is None: sdy = self._compute_sdy(sdy, y, "initial_slope_prior") initial_slope_prior = boom.GaussianModel(0, sdy) if not isinstance(initial_slope_prior, boom.GaussianModel): raise Exception("Unexpected type for initial_slope_prior.")
def test_mcmc(): model = boom.GaussianModel() mu = -16 sigma = 7 data = np.random.randn(10000) * sigma + mu model.set_data(boom.Vector(data)) mean_prior = boom.GaussianModelGivenSigma( model.sigsq_parameter, mu, 1.0) sigsq_prior = boom.ChisqModel(1.0, sigma) sampler = boom.GaussianConjugateSampler( model, mean_prior, sigsq_prior) model.set_method(sampler) for _ in range(100): model.sample_posterior()
def test_moments(self): model = boom.GaussianModel(1, 2) self.assertEqual(1.0, model.mean) self.assertEqual(2.0, model.sd) self.assertEqual(4.0, model.variance)