def SimpleParam(): return ParameterSet(parameters=[ Parameter(variables=[ Intercept(add_re=True, col_group='group', fe_prior=GaussianPrior()) ], link_fun=lambda x: x, param_name='foo') ])
def SplineParam(): constr_cvx = SplineLinearConstr(order=2, y_bounds=[0.0, np.inf], grid_size=5) spline_var = Spline( covariate='cov1', derivative_constr=[constr_cvx], degree=2, ) parameter = Parameter( variables=[spline_var, Intercept(add_re=True, col_group='group')], param_name='foo') return ParameterSet([parameter])
def make_parameter_set(self, coefficient_priors: Optional[List[float]] = None, coefficient_prior_var: Optional[float] = None): if coefficient_priors is not None: intercept = [ Intercept( fe_prior=GaussianPrior(mean=[coefficient_priors.pop(0)], std=[coefficient_prior_var**0.5])) ] else: intercept = [Intercept()] if self.covariates is not None: if coefficient_priors is None: covariate_variables = [ Variable(covariate=cov) for cov in self.covariates ] else: covariate_variables = [ Variable(covariate=cov, fe_prior=GaussianPrior( mean=[coefficient_priors.pop(0)], std=[coefficient_prior_var**0.5])) for cov in self.covariates ] else: covariate_variables = list() spline_variables = list() if self.splines is not None: spline_variables = make_spline_variables(self.splines, coefficient_priors, coefficient_prior_var) parameter = Parameter(param_name='p', variables=intercept + covariate_variables + spline_variables, link_fun=lambda x: expit(x)) self.parameter_set = ParameterSet(parameters=[parameter])
def __init__(self, df: pd.DataFrame, col_output: str, col_se: str, col_input: str, increasing: bool = False, decreasing: bool = False, concave: bool = False, convex: bool = False, knots_type: str = 'domain', knots_num: int = 4, knots_degree: int = 3, r_linear: bool = False, l_linear: bool = False, include_intercept: bool = True): self.col_output = col_output self.col_se = col_se self.col_input = col_input self.increasing = increasing self.decreasing = decreasing self.concave = concave self.convex = convex self.knots_num = knots_num self.knots_type = knots_type self.knots_degree = knots_degree self.r_linear = r_linear self.l_linear = l_linear self.include_intercept = include_intercept data = df.copy().reset_index(drop=True) data['group'] = data.index self.data_spec = DataSpecs(col_obs=self.col_output, col_obs_se=self.col_se) # Create derivative constraints including monotone constraints # and shape constraints derivative_constraints = [] if self.increasing or self.decreasing: if self.increasing and self.decreasing: raise RuntimeError( "Cannot have both monotone increasing and decreasing.") if self.increasing: y_bounds = [0.0, np.inf] else: y_bounds = [-np.inf, 0.0] derivative_constraints.append( SplineLinearConstr(order=1, y_bounds=y_bounds, grid_size=20)) if self.convex or self.concave: if self.convex and self.concave: raise RuntimeError("Cannot have both convex and concave.") if self.convex: y_bounds = [0.0, np.inf] else: y_bounds = [-np.inf, 0.0] derivative_constraints.append( SplineLinearConstr(order=2, y_bounds=y_bounds, grid_size=20)) # Create the spline variable for the input spline = Spline(covariate=self.col_input, knots_type=self.knots_type, knots_num=self.knots_num, degree=self.knots_degree, r_linear=self.r_linear, l_linear=self.l_linear, derivative_constr=derivative_constraints, include_intercept=include_intercept) # Create the parameter set to solve # the marginal likelihood problem param_set_marginal = ParameterSet( parameters=[Parameter(param_name='beta', variables=[spline])]) process_all(param_set_marginal, data) # Create the parameter set to solve # for the inefficiencies intercept = Intercept(add_re=True, col_group='group', re_prior=GaussianPrior(lower_bound=[0.0], upper_bound=[np.inf])) param_set_v = ParameterSet( parameters=[Parameter(param_name='v', variables=[intercept])]) process_all(param_set_v, data) # Process the data set self.data = Data(data_specs=self.data_spec) self.data.process(data) # Build the two models self.marginal_model = SimpleBetaEtaModel(param_set_marginal) self.v_model = VModel(param_set_v) # Create the solvers for the models marginal_solver = IPOPTSolver(self.marginal_model) v_solver = ClosedFormSolver(self.v_model) self.solver = SimpleSolver([marginal_solver, v_solver]) # Randomly initialize parameter values self.x_init = np.random.randn(marginal_solver.model.x_dim) # Placeholder for inefficiencies self.inefficiencies = np.zeros(len(data))
def test_parameter_set_duplicates(self, param1): with pytest.raises(ParameterSetError): ParameterSet( parameters=[param1, param1] )
def parameter_set(param1, param2, parameter_function): return ParameterSet( parameters=[param1, param2], parameter_functions=[parameter_function], )
def test_beta_gamma_eta_model(sfa_inputs): data, X, Z, beta_true, gamma_true, eta_true = sfa_inputs n_beta = len(beta_true) with patch.object(ParameterSet, '__init__', lambda x: None): param_set = ParameterSet() param_set.reset() param_set.num_fe = n_beta param_set.num_re_var = 1 param_set.design_matrix_fe = X param_set.design_matrix_re = Z param_set.lb_fe = [-10.0] * n_beta param_set.ub_fe = [10.0] * n_beta param_set.lb_re_var = [0.0] * 1 param_set.ub_re_var = [10.0] * 1 param_set.constr_matrix_fe = np.zeros((1, n_beta)) param_set.constr_lb_fe = [0.0] param_set.constr_ub_fe = [0.0] param_set.constr_matrix_re_var = np.zeros((1, 1)) param_set.constr_lb_re_var = [0.0] param_set.constr_ub_re_var = [0.0] param_set.fe_priors = [] param_set.re_var_priors = [] model = SimpleBetaGammaEtaModel(param_set) solver = IPOPTSolver(model) x_init = np.random.rand(len(beta_true) + 2) solver.fit(x_init, data, options=dict(solver_options=dict(max_iter=100))) assert np.linalg.norm(solver.x_opt[:n_beta] - beta_true) / np.linalg.norm(beta_true) < 2e-2
def test_u_model(re_inputs): data, Z, u_true, eta = re_inputs n_groups = len(u_true) with patch.object(ParameterSet, '__init__', lambda x: None): with patch.object(ParameterSet, 'num_re', new_callable=PropertyMock) as mock_num_re: param_set = ParameterSet() param_set.reset() param_set.num_fe = 1 mock_num_re.return_value = n_groups param_set.design_matrix_re = Z param_set.constr_matrix_re = np.zeros((1, n_groups)) param_set.constr_lb_re = [0.0] param_set.constr_ub_re = [0.0] param_set.lb_re = [-2.0] * n_groups param_set.ub_re = [2.0] * n_groups param_set.re_priors = [GaussianPrior(mean=[0.0], std=[eta])] param_set.re_var_padding = np.ones((n_groups, 1)) model = UModel(param_set) solver = ScipyOpt(model) x_init = np.random.rand(n_groups) solver.fit(x_init, data, options=dict(solver_options=dict(maxiter=100))) assert np.linalg.norm(solver.x_opt - u_true) / np.linalg.norm(u_true) < 2e-2 u_model = UModel(param_set) cf_solver = ClosedFormSolver(u_model) cf_solver.fit(x_init, data) assert np.linalg.norm(cf_solver.x_opt - u_true) / np.linalg.norm(u_true) < 2e-2
def param_set(variable1, variable2, spline_variable): return ParameterSet([ Parameter(param_name='foo', variables=[variable1, variable2, spline_variable]) ])