def make_2d_data(n_examples, noise="uniform"): cov = [[2, 1], [1, 2]] dist = 100 x1 = mvnormal([-dist, 0], cov, n_examples / 4) x2 = mvnormal([0, dist], cov, n_examples / 4) x3 = mvnormal([dist, 0], cov, n_examples / 4) x4 = mvnormal([0, -dist], cov, n_examples / 4) x = np.concatenate([x1, x2, x3, x4]) if noise == "uniform": x += np.random.uniform(-4, 4, size=x.shape) else: x += np.random.normal(0, 3, size=x.shape) y = np.zeros((n_examples, 4)) y[0 : n_examples / 4] = 1 y[n_examples / 4 : n_examples / 2, 1] = 1 y[n_examples / 2 : 3 * n_examples / 4, 2] = 1 y[3 * n_examples / 4 :, 3] = 1 idx = np.arange(len(x)) perm = np.random.permutation(idx) x = x[perm] y = y[perm] return x, y
def make_2d_data(n_examples, noise='uniform'): cov = [[2, 1], [1, 2]] dist = 100 x1 = mvnormal([-dist, 0], cov, n_examples / 4) x2 = mvnormal([0, dist], cov, n_examples / 4) x3 = mvnormal([dist, 0], cov, n_examples / 4) x4 = mvnormal([0, -dist], cov, n_examples / 4) x = np.concatenate([x1, x2, x3, x4]) if noise == 'uniform': x += np.random.uniform(-4, 4, size=x.shape) else: x += np.random.normal(0, 3, size=x.shape) y = np.zeros((n_examples, 4)) y[0:n_examples / 4] = 1 y[n_examples / 4:n_examples / 2, 1] = 1 y[n_examples / 2:3 * n_examples / 4, 2] = 1 y[3 * n_examples / 4:, 3] = 1 idx = np.arange(len(x)) perm = np.random.permutation(idx) x = x[perm] y = y[perm] return x, y
def reset(self): """ Reset priors and draw parameter estimates from prior. """ # priors self.lbd_phi0 = self.h["lbd_phi0"] self.alpha_s20 = self.h["alpha_s20"] self.beta_s20 = self.h["beta_s20"] self.alpha_w = self.h["alpha_w0"] self.beta_w = self.h["beta_w0"] self.sigma_phi0 = eye(self.pdata) * self.h["lbd_phi0"] self.sigma_phi0_inv = eye(self.pdata) / self.h["lbd_phi0"] self.mu_phi0 = ones(self.pdata) * self.h["mu_phi0"] # initial parameter estimates drawn from prior self.p = dict() self.p["sigma2_1"] = 1.0 / gamma(self.alpha_s20 , (1.0 / self.beta_s20) ) # inverse gamma self.p["phi_1"] = mvnormal(self.mu_phi0 , self.p["sigma2_1"] * self.sigma_phi0) self.p["sigma2_2"] = 1.0 / gamma(self.alpha_s20 , (1.0 / self.beta_s20)) # inverse gamma self.p["phi_2"] = mvnormal(self.mu_phi0, self.p["sigma2_2"] * self.sigma_phi0) self.p["w"] = beta(self.alpha_w, self.beta_w) # beta distribution
def reset(self): """ Reset priors and draw parameter estimates from prior. """ # priors self.lbd_phi0 = self.h["lbd_phi0"] #lambda0 = 1 self.alpha_s20 = self.h["alpha_s20"] #alpha(sigma^2) = 5 self.beta_s20 = self.h["beta_s20"] #beta(sigma^2) = 1 self.sigma_phi0 = eye(self.pdata) * self.h["lbd_phi0"] #covariate matrix of sigma square self.sigma_phi0_inv = eye(self.pdata) / self.h["lbd_phi0"] #inverse self.mu_phi0 = ones(self.pdata) * self.h["mu_phi0"] #0 self.alpha_w0 = self.h["alpha_w0"] self.beta_w0 = self.h["beta_w0"] # initial parameter estimates drawn from prior self.p = dict() self.p["sigma_square_1"] = 1.0 / gamma(self.alpha_s20, 1.0 / self.beta_s20) self.p["sigma_square_2"] = 1.0 / gamma(self.alpha_s20, 1.0 / self.beta_s20) self.p["phi_1"] = mvnormal(self.mu_phi0, self.p["sigma_square_1"] * self.sigma_phi0) self.p["phi_2"] = mvnormal(self.mu_phi0, self.p["sigma_square_2"] * self.sigma_phi0) self.p["w"] = beta(self.alpha_w0, self.beta_w0) self.p["z"] = binomial(1, self.p["w"], self.ndata)
def reset(self): """ Reset priors and draw parameter estimates from prior. """ # priors self.lbd_phi0 = self.h["lbd_phi0"] self.alpha_s20 = self.h["alpha_s20"] self.beta_s20 = self.h["beta_s20"] self.sigma_phi0 = eye(self.pdata) * self.h["lbd_phi0"] self.sigma_phi0_inv = eye(self.pdata) / self.h["lbd_phi0"] self.mu_phi0 = ones(self.pdata) * self.h["mu_phi0"] # initial parameter estimates drawn from prior self.p = dict() self.p["sigma2"] = 1.0 / gamma(self.alpha_s20, 1.0 / self.beta_s20) # inverse gamma self.p["phi"] = mvnormal(self.mu_phi0, self.p["sigma2"] * self.sigma_phi0)
def reset(self): """ Reset priors and draw parameter estimates from prior. """ # priors self.lbd_phi0 = self.h["lbd_phi0"] # lambda self.alpha_s20 = self.h["alpha_s20"] # alpha sigma sq self.beta_s20 = self.h["beta_s20"] # beta sigma sq self.sigma_phi0 = eye(self.pdata) * self.h["lbd_phi0"] self.sigma_phi0_inv = eye(self.pdata) / self.h["lbd_phi0"] self.mu_phi0 = ones(self.pdata) * self.h["mu_phi0"] # initial parameter estimates drawn from prior self.p = dict() self.p["sigma2"] = 1.0 / gamma( self.alpha_s20, 1.0 / self.beta_s20) # inverse gamma # possible numerical problems ? self.p["phi"] = mvnormal(self.mu_phi0, self.p["sigma2"] * self.sigma_phi0)
def generate(self): return mvnormal(self.means, self.cov, self.size) '''