def bootstrap(factor_names, model, run_count): """Create a minimal starting design that is non-singular.""" md = ModelDesc.from_formula(model) model_size = len(md.rhs_termlist) if run_count == 0: run_count = model_size if model_size > run_count: raise ValueError("Can't build a design of size {} " "for a model of rank {}. " "Model: '{}'".format(run_count, model_size, model)) factor_count = len(factor_names) x0 = np.zeros(factor_count) # add high/low bounds to constraint matrix constraint_matrix = np.zeros((factor_count * 2, factor_count)) bounds = np.zeros(factor_count * 2) c = 0 for f in range(factor_count): constraint_matrix[c][f] = -1 bounds[c] = 1 c += 1 constraint_matrix[c][f] = 1 bounds[c] = 1 c += 1 start_points = hit_and_run(x0, constraint_matrix, bounds, run_count) d = pd.DataFrame(start_points, columns=factor_names) X = dmatrix(model, d) return (d, X)
def test_hit_and_run_one_dim(cls): """Tests generation of 2 points with thin = 2.""" x0 = np.array([0.5]) A = np.array([[1], [-1]]) bounds = np.array([1, 0]) np.random.seed(300) result = hit_and_run(x0, A, bounds, 2, 2) answer = np.array([[0.70922430], [0.59920946]]) np.testing.assert_almost_equal(result, answer)
def test_hit_and_run_constrained(cls): """Tests generation of 4 points.""" x0 = np.array([0.5, 0.5]) A = np.array([[1, 0], [0, 1], [-1, 0], [0, -1], [-1, -1]]) bounds = np.array([1, 1, 0, 0, -0.9]) np.random.seed(200) result = hit_and_run(x0, A, bounds, 4, 1) answer = np.array([[0.28429647, 0.78408962], [0.26194012, 0.99263893], [0.30624997, 0.89710025], [0.41753565, 0.74949495]]) np.testing.assert_almost_equal(result, answer)
def test_hit_and_run_4(self): """Tests generation of 3 points with thin = 3.""" result = hit_and_run(self.x0, self.A, self.bounds, 2, 3) answer = np.array([[0.32222719, 0.10867805], [0.30750317, 0.05997018]]) np.testing.assert_almost_equal(result, answer)
def test_hit_and_run_3(self): """Tests generation of 3 points.""" result = hit_and_run(self.x0, self.A, self.bounds, 2, 1) answer = np.array([[0.57548241, 0.62646417], [0.77956909, 0.17414724]]) np.testing.assert_almost_equal(result, answer)
def test_hit_and_run_2(self): """Tests generation of a single point with thin = 3.""" result = hit_and_run(self.x0, self.A, self.bounds, 1, 3) answer = np.array([[0.32222719, 0.10867805]]) np.testing.assert_almost_equal(result, answer)
def test_hit_and_run_1(self): """Tests generation of a single point.""" result = hit_and_run(self.x0, self.A, self.bounds, 1, 1) answer = np.array([[0.57548241, 0.62646417]]) np.testing.assert_almost_equal(result, answer)