示例#1
0
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')
    ])
示例#2
0
 def test_intercept(self, df):
     with pytest.raises(TypeError):
         i = Intercept(covariate='foo')
     i = Intercept()
     i.build_design_matrix_fe(df)
     assert i.covariate == 'intercept'
     assert i.num_fe == 1
     np.testing.assert_allclose(
         i.design_matrix_fe,
         np.ones((5, 1)),
     )
     i.build_bounds_fe()
     assert i.lb_fe[0] == -np.inf
     assert i.ub_fe[0] == np.inf
示例#3
0
    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])
示例#4
0
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])
示例#5
0
    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))
示例#6
0
def param2():
    return Parameter(
        param_name='beta',
        variables=[Intercept()],
        link_fun=lambda x: x,
    )
示例#7
0
def param1():
    return Parameter(
        param_name='alpha',
        variables=[Intercept(), Intercept(add_re=True, col_group='group')],
        link_fun=lambda x: x,
    )