def get_actionStepLength(self) -> float: actionStepLength = 0.0 if 17 <= self.age < 45: actionStepLength = distrs.Uniform( -0.1, 0.1)().item() # lower_bound = -0.1, upper_bound = 0.1 else: actionStepLength = distrs.Chi2(2)().item( ) / 16 - 0.025 # k = 2 -> mean = 0.125, sigma = 0.125 return actionStepLength
def guide(): # variational parameters var_mu = pyro.param('var_mu', torch.tensor(180.)) var_mu_sig = pyro.param('var_mu_sig', torch.tensor(5.), constraint=constraints.positive) var_sig = pyro.param('var_sig', torch.tensor(5.)) # factorized distribution pyro.sample('mu', dist.Normal(loc=var_mu, scale=var_mu_sig)) pyro.sample('sigma', dist.Chi2(var_sig))
def get_sigma(self) -> float: sigma = 0.0 if 17 <= self.age < 45: sigma = random_noise().item() elif 45 <= self.age < 55: sigma = distrs.Normal(0.1, 0.05)().item() # mean = 0.1, sigma = 0.05 else: sigma = distrs.Chi2( 2)().item() / 10 # k = 2 -> mean = 0.1 , sigma = 0.2 return sigma
def _params2probs(self, params): """Convert parameters to probability distributions. Args: params (dict[str -> np.array]): dictionary of distribution parameters Returns: dict[str -> Dist]: dictionary of prior probabilities """ M = dist.Normal(params["M_mu"], self.softplus(params["M_sigma"])).independent(2) beta = dist.Beta(self.softplus(params["beta_p"]), self.softplus(params["beta_q"])).independent(1) z_epsilon = dist.Beta( self.softplus(params["z_epsilon_p"]), self.softplus(params["z_epsilon_q"])) scale_factor = dist.Chi2(params["scale_factor"]) return {"M": M, "beta": beta, "z_epsilon": z_epsilon, "scale_factor": scale_factor}
def ChiSq(_name, df): return {'x': pyro.sample(_name, dist.Chi2(df))}
pyro.clear_param_store() pyro.enable_validation(True) svi = SVI(model, guide, optim=pyro.optim.ClippedAdam({"lr": 0.01}), loss=Trace_ELBO()) # do gradient steps c = 0 for step in range(1000): c += 1 loss = svi.step() if step % 100 == 0: print("[iteration {:>4}] loss: {:.4f}".format(c, loss)) sigma = dist.Chi2(pyro.param('var_sig')).sample((10000, )).numpy() mu = dist.Normal(pyro.param('var_mu'), pyro.param('var_mu_sig')).sample( (10000, )).numpy() plt.figure() plt.scatter(mu, sigma, alpha=0.01) plt.xlim([extent[0], extent[1]]) plt.ylim([extent[2], extent[3]]) plt.ylabel('$\sigma$') plt.xlabel('$\mu$') plt.title('VI samples') save_fig('bayes_unigauss_2d_pyro_post.pdf') plt.show()