예제 #1
0
 def test_scipyopt_core_model(self, curve_fun, seed):
     np.random.seed(seed)
     params_set, params_true, _ = simulate_params(1)
     data = simulate_data(curve_fun, params_true)
     core_model = CoreModel(params_set, curve_fun, normal_loss)
     solver = ScipyOpt(core_model)
     solver.fit(data=data, options={'maxiter': 200})
     y_pred = solver.predict(t=data[0]['t'].to_numpy())
     y_true = data[0]['obs'].to_numpy()
     assert np.linalg.norm(y_pred - y_true) / np.linalg.norm(y_true) < 2e-2
예제 #2
0
    def test_gaussian_mixture_integration(self, curve_fun):
        gm_model = GaussianMixtures(stride=1.0, size=3)
        params_set, params_true, _ = simulate_params(1)
        data = simulate_data(curve_fun, params_true)
        core_model = CoreModel(params_set, curve_fun, normal_loss)
        y_true = data[0]['obs'].to_numpy()

        solver = GaussianMixturesIntegration(gm_model)
        solver.set_model_instance(core_model)
        solver.fit(data=data, options={'maxiter': 20})
        y_pred = solver.predict(t=data[0]['t'].to_numpy())
        core_model.erase_data()

        if curve_fun.__name__ == 'gaussian_pdf':
            solver_base = ScipyOpt(core_model)
            solver_base.fit(data=data, options={'maxiter': 20})
            y_pred_base = solver_base.predict(t=data[0]['t'].to_numpy())

            assert np.linalg.norm(y_pred -
                                  y_true) < np.linalg.norm(y_pred_base -
                                                           y_true)
예제 #3
0
beta = Parameter(param_name='beta',
                 link_fun=identity_fun,
                 variables=[b_intercept, b_social_distance])
p = Parameter(param_name='p', link_fun=exp_fun, variables=[phi_intercept])

parameters = ParameterSet([alpha, beta, p])

optimizer_options = {
    'ftol': 1e-12,
    'gtol': 1e-12,
}

model = CoreModel(param_set=parameters,
                  curve_fun=gaussian_cdf,
                  loss_fun=normal_loss)
solver = ScipyOpt(model)
solver.fit(data=data._get_df(copy=True, return_specs=True),
           options=optimizer_options)
params_estimate = model.get_params(solver.x_opt, expand=True)

# -------------------------------------------------------------------------
# check result

for i in range(3):
    assert numpy.allclose(params_estimate[i], params_value[i], rtol=rel_tol)

print('covariate.py: OK')
""" ```
{end_markdown covariate_xam}
"""
예제 #4
0
beta = Parameter('beta', link_fun=lambda x: x, variables=[beta_fe])
p = Parameter('p', link_fun=lambda x: x, variables=[p_fe])

params = ParameterSet([alpha, beta, p])
""" ```

### Models and Solvers
We now need a `Model` that contains our parameter set and also which curve function we want to fit, and the loss
function for the optimization. We also need to define a `Solver` that will actually perform the optimization.
Finally, we will fit the solver to our simulated data.

```python """
model = CoreModel(param_set=params,
                  curve_fun=ln_gaussian_pdf,
                  loss_fun=normal_loss)
solver = ScipyOpt()
""" ```

### Use the Prior Initializer
The purpose of using the prior initializer is that it gets smarter priors for later on. Therefore, there are many
different things that one could do to get good priors based on subsets of the data. Each of these types
is implemented in a separate [`PriorInitializerComponent`](PriorInitializerComponent.md). Here we will use
the `BetaPrior` prior initializer component; the goal is to estimate the mean and variance of the random effects
and then set that as the fixed effects prior variance for a **new** parameter set that can be used in later model fits.

Even though we only have 10 groups, our random effect variance should get pretty close to our simulated value.
The `BetaPrior()` will update the fixed effects Gaussian prior for the `beta` parameter to what is estimated based on
a joint model fit with random effects.

```python """
# Instantiate a PriorInitializer with one component
예제 #5
0
 def test_set_options(self, rb):
     options = {'maxiter': 20}
     solver = ScipyOpt(rb)
     solver.set_options(options)
     assert solver.options == options
예제 #6
0
 def test_scipyopt(self, rb):
     solver = ScipyOpt(rb)
     solver.fit(data=None, options={'maxiter': 20})
     assert np.abs(solver.fun_val_opt) < 1e-5