def initialize_population(self): """Create an initial population from the prior distribution.""" population = [] if self.init_samples is None: init_rnd = sample_prior_predictive( self.N, var_names=[v.name for v in self.model.unobserved_RVs], model=self.model, ) for i in range(self.N): point = Point( {v.name: init_rnd[v.name][i] for v in self.variables}, model=self.model) population.append(self.model.dict_to_array(point)) self.prior_samples = np.array(floatX(population)) elif self.init_samples is not None: self.prior_samples = np.copy(self.init_samples) self.samples = np.copy(self.prior_samples) self.nf_samples = np.copy(self.samples) self.get_posterior_logp() self.get_prior_logp() self.log_weight = self.posterior_logp - self.prior_logp self.log_evidence = logsumexp(self.log_weight) - np.log( len(self.log_weight)) self.evidence = np.exp(self.log_evidence) self.log_weight = self.log_weight - self.log_evidence self.regularize_weights() #same as in fitnf but prior~q self.log_weight_pq_num = self.posterior_logp + 2 * self.prior_logp self.log_weight_pq_den = 3 * self.prior_logp self.log_evidence_pq = logsumexp(self.log_weight_pq_num) - logsumexp( self.log_weight_pq_den) self.evidence_pq = np.exp(self.log_evidence_pq) self.log_weight_pq = self.posterior_logp - self.prior_logp - self.log_evidence_pq self.pq_bw_loss = np.log( (np.exp(self.posterior_logp) - np.exp(self.log_evidence_pq + self.prior_logp))**2) #not actually used yet I think self.regularize_weights_pq() #sum of mean loss (p - q*Z_pq)^2 /N for diagnostic purposes self.log_mean_loss = np.log( np.mean((np.exp(self.posterior_logp) - np.exp(self.prior_logp + self.log_evidence_pq))**2)) self.init_weights_cleanup(lambda x: self.prior_logp(x), lambda x: self.prior_dlogp(x)) self.q_ess = self.calculate_ess(self.log_weight) self.total_ess = self.calculate_ess(self.sinf_logw) self.all_logq = np.array([]) self.nf_models = [] self.nf_models_uw = []
def initialize_population(self): """Create an initial population from the prior distribution.""" population = [] var_info = OrderedDict() if self.start is None: init_rnd = sample_prior_predictive( self.draws, var_names=[v.name for v in self.model.unobserved_RVs], model=self.model, ) else: init_rnd = self.start init = self.model.initial_point for v in self.variables: var_info[v.name] = (init[v.name].shape, init[v.name].size) for i in range(self.draws): point = Point( {v.name: init_rnd[v.name][i] for v in self.variables}, model=self.model) population.append(DictToArrayBijection.map(point).data) self.posterior = np.array(floatX(population)) self.var_info = var_info
def initialize_population(self): """Create an initial population from the prior distribution.""" population = [] var_info = OrderedDict() init_rnd = sample_prior_predictive( self.draws, var_names=[v.name for v in self.model.unobserved_RVs], model=self.model, ) init = self.model.test_point for v in self.variables: var_info[v.name] = (init[v.name].shape, init[v.name].size) for i in range(self.draws): point = Point({v.name: init_rnd[v.name][i] for v in self.variables}, model=self.model) population.append(self.model.dict_to_array(point)) self.nf_samples = np.array(floatX(population)) self.live_points = np.array(floatX(population)) self.var_info = var_info self.posterior = np.empty((0, np.shape(self.nf_samples)[1]))
def test_interval_missing_observations(): with Model() as model: obs1 = ma.masked_values([1, 2, -1, 4, -1], value=-1) obs2 = ma.masked_values([-1, -1, 6, -1, 8], value=-1) rng = aesara.shared(np.random.RandomState(2323), borrow=True) with pytest.warns(ImputationWarning): theta1 = Uniform("theta1", 0, 5, observed=obs1, rng=rng) with pytest.warns(ImputationWarning): theta2 = Normal("theta2", mu=theta1, observed=obs2, rng=rng) assert "theta1_observed_interval__" in model.named_vars assert "theta1_missing_interval__" in model.named_vars assert isinstance( model.rvs_to_values[model.named_vars["theta1_observed"]].tag.transform, Interval ) prior_trace = sample_prior_predictive() # Make sure the observed + missing combined deterministics have the # same shape as the original observations vectors assert prior_trace["theta1"].shape[-1] == obs1.shape[0] assert prior_trace["theta2"].shape[-1] == obs2.shape[0] # Make sure that the observed values are newly generated samples assert np.all(np.var(prior_trace["theta1_observed"], 0) > 0.0) assert np.all(np.var(prior_trace["theta2_observed"], 0) > 0.0) # Make sure the missing parts of the combined deterministic matches the # sampled missing and observed variable values assert np.mean(prior_trace["theta1"][:, obs1.mask] - prior_trace["theta1_missing"]) == 0.0 assert np.mean(prior_trace["theta1"][:, ~obs1.mask] - prior_trace["theta1_observed"]) == 0.0 assert np.mean(prior_trace["theta2"][:, obs2.mask] - prior_trace["theta2_missing"]) == 0.0 assert np.mean(prior_trace["theta2"][:, ~obs2.mask] - prior_trace["theta2_observed"]) == 0.0 assert {"theta1", "theta2"} <= set(prior_trace.keys()) trace = sample(chains=1, draws=50, compute_convergence_checks=False) assert np.all(0 < trace["theta1_missing"].mean(0)) assert np.all(0 < trace["theta2_missing"].mean(0)) assert "theta1" not in trace.varnames assert "theta2" not in trace.varnames # Make sure that the observed values are newly generated samples and that # the observed and deterministic matche pp_trace = sample_posterior_predictive(trace) assert np.all(np.var(pp_trace["theta1"], 0) > 0.0) assert np.all(np.var(pp_trace["theta2"], 0) > 0.0) assert np.mean(pp_trace["theta1"][:, ~obs1.mask] - pp_trace["theta1_observed"]) == 0.0 assert np.mean(pp_trace["theta2"][:, ~obs2.mask] - pp_trace["theta2_observed"]) == 0.0
def test_missing(data): with Model() as model: x = Normal("x", 1, 1) with pytest.warns(ImputationWarning): y = Normal("y", x, 1, observed=data) assert "y_missing" in model.named_vars test_point = model.initial_point assert not np.isnan(model.logp(test_point)) with model: prior_trace = sample_prior_predictive() assert {"x", "y"} <= set(prior_trace.keys())
def test_missing_dual_observations(): with Model() as model: obs1 = ma.masked_values([1, 2, -1, 4, -1], value=-1) obs2 = ma.masked_values([-1, -1, 6, -1, 8], value=-1) beta1 = Normal("beta1", 1, 1) beta2 = Normal("beta2", 2, 1) latent = Normal("theta", size=5) with pytest.warns(ImputationWarning): ovar1 = Normal("o1", mu=beta1 * latent, observed=obs1) with pytest.warns(ImputationWarning): ovar2 = Normal("o2", mu=beta2 * latent, observed=obs2) prior_trace = sample_prior_predictive() assert {"beta1", "beta2", "theta", "o1", "o2"} <= set(prior_trace.keys()) # TODO: Assert something trace = sample(chains=1, draws=50)
def test_missing_with_predictors(): predictors = array([0.5, 1, 0.5, 2, 0.3]) data = ma.masked_values([1, 2, -1, 4, -1], value=-1) with Model() as model: x = Normal("x", 1, 1) with pytest.warns(ImputationWarning): y = Normal("y", x * predictors, 1, observed=data) assert "y_missing" in model.named_vars test_point = model.initial_point assert not np.isnan(model.logp(test_point)) with model: prior_trace = sample_prior_predictive() assert {"x", "y"} <= set(prior_trace.keys())