Exemplo n.º 1
0
def statsmodels_fixture():
    """These fixtures are taken from the statsmodels test battery and adapted towards
     a random test."""
    fix = {}
    num_obs = 100
    num_params = 3
    max_range = 10
    x = np.linspace(0, max_range, num_obs)
    x = sm.add_constant(np.column_stack((x, x**2)), prepend=False)
    beta = np.random.rand(num_params) * max_range
    y = np.dot(x, beta) + np.random.normal(size=num_obs)

    results = sm.OLS(y, x).fit()

    fix["stats_cov"] = results.cov_HC0

    params_df = pd.DataFrame({"value": results.params})

    moment_cond = np.zeros((num_obs, num_params))
    moment_jac = np.zeros((num_obs, num_params, num_params))
    for i in range(num_obs):
        moment_cond[i, :] = calc_moment_condition(params_df, x[i, :], y[i])
        moment_jac[i, :, :] = jacobian(calc_moment_condition,
                                       params_df,
                                       func_kwargs={
                                           "x_t": x[i, :],
                                           "y_t": y[i]
                                       })
    fix["mom_cond"] = moment_cond
    fix["mom_cond_jacob"] = moment_jac
    fix["weighting_matrix"] = np.eye(num_params)
    return fix
Exemplo n.º 2
0
def np_jac(log_like_obs, params, method, log_like_kwargs):
    """We wrote this function because we did not want to touch the
    differentiation files.

    """
    numpy_jacobian = jacobian(log_like_obs,
                              params,
                              method,
                              func_kwargs=log_like_kwargs)
    return numpy_jacobian.to_numpy()
def test_jacobian(statsmodels_fixtures, method, extrapolation):
    fix = statsmodels_fixtures
    func_kwargs = {"y": fix["y"], "x": fix["x"]}
    calculated = jacobian(
        logit_loglikeobs,
        params=fix["params"],
        method=method,
        extrapolation=extrapolation,
        func_kwargs=func_kwargs,
    )

    expected = fix["jacobian"]
    assert_frame_equal(calculated, expected)