示例#1
0
def test_grad_objective_function_infinite_end_time_two_features():
    df = pd.DataFrame(
        [[0, np.nan, 1, random()], [random(), np.nan, 1,
                                    random()],
         [0, random(), 1, random()], [0, random(), 1, random()]],
        columns=['click_time', 'conv_time', 'intercept', 'random'])
    features = ['intercept', 'random']
    end_time = 1000000

    y = df[['click_time', 'conv_time']].values
    X = csr_matrix(df[features].values)

    grad_objective_function = grad_objective_function_factory(
        fit_intercept=False)

    input_ = prepare_input(y, X, end_time=end_time)
    input_['Jacobian'] = np.zeros(4)

    y1 = input_['y1']
    x0, x1 = input_['x0'], input_['x1']

    w_zero = np.zeros(4)
    input_['mu'] = 0

    assert np.allclose(
        grad_objective_function(w_zero, **input_),
        np.array([
            0, 1 / 2 * x0[:, 1].sum() - 1 / 2 * x1[:, 1].sum(),
            (y1[:, 1] - y1[:, 0] - 1).sum(),
            ((y1[:, 1] - y1[:, 0] - 1) * x1[:, 1]).sum()
        ]))
示例#2
0
def test_grad_objective_function_infinite_end_time():
    df = pd.DataFrame([[0, np.nan, 1], [random(), np.nan, 1],
                       [0, random(), 1], [0, random(), 1]],
                      columns=['click_time', 'conv_time', 'intercept'])

    end_time = 1000000

    y = df[['click_time', 'conv_time']].values
    X = csr_matrix(df[['intercept']].values)

    grad_objective_function = grad_objective_function_factory(
        fit_intercept=False)

    input_ = prepare_input(y, X, end_time=end_time)
    input_['Jacobian'] = np.zeros(2)

    y1 = input_['y1']

    w_zero = np.zeros(2)
    w_one = np.ones(2)

    assert np.allclose(grad_objective_function(w_zero, **input_),
                       np.array([0, (y1[:, 1] - y1[:, 0]).sum() - 2]))
    assert np.allclose(
        grad_objective_function(w_one, **input_),
        np.array([
            df.shape[0] * sigmoid(1) - y1.shape[0],
            np.exp(1) * (y1[:, 1] - y1[:, 0]).sum() - 2
        ]) + w_one)
示例#3
0
def test_grad_objective_function_output_type(test_data_input):
    p = test_data_input['X'].shape[1]

    grad_objective_function = grad_objective_function_factory(
        fit_intercept=False)

    input_ = prepare_input(**test_data_input)

    w = np.zeros(2 * p)
    input_['Jacobian'] = np.zeros(2 * p)

    assert isinstance(grad_objective_function(w, **input_), np.ndarray)
示例#4
0
    def _get_optimization_result(self, method='L-BFGS-B', **kwargs):
        p = kwargs['x0'].shape[1]
        kwargs['Jacobian'] = np.zeros(2 * p)
        # todo use better initialization

        objective_function = objective_function_factory(self.fit_intercept)
        grad_objective_function = grad_objective_function_factory(
            self.fit_intercept)

        def L(w):
            return partial(objective_function, **kwargs)(w)

        def DL(w):
            return partial(grad_objective_function, **kwargs)(w)

        w0 = np.zeros(2 * p)

        info = minimize(L, x0=w0, method=method, jac=DL)

        return info
示例#5
0
def test_scipy_check_grad(test_dummied_matrix):

    y, X = test_dummied_matrix

    end_time = 1.1 * y[:, 1][~np.isnan(y[:, 1])].max()

    for fit_intercept in [True, False]:
        objective_function = objective_function_factory(
            fit_intercept=fit_intercept)
        grad_objective_function = grad_objective_function_factory(
            fit_intercept=fit_intercept)

        input_ = prepare_input(y, X, end_time=end_time)
        p = input_['x0'].shape[1]
        input_['Jacobian'] = np.zeros(2 * p)
        input_['mu'] = 0.
        for i in range(20):
            w0 = (5. - i) * np.ones(2 * p)
            L = partial(objective_function, **input_)
            DL = partial(grad_objective_function, **input_)

        assert isclose(check_grad(L, DL, w0), 0.)