Пример #1
0
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
Пример #2
0
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