Ejemplo n.º 1
0
def test_formula_partial_weights():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=['y1', 'y2', 'x1', 'x2'])
    weights = DataFrame(np.random.chisquare(5, (500, 1)), columns=['eq2'])
    formula = OrderedDict()
    formula['eq1'] = 'y1 ~ 1 + x1'
    formula['eq2'] = 'y2 ~ 1 + x1'
    with warnings.catch_warnings(record=True) as w:
        mod = SUR.from_formula(formula, data, weights=weights)
        assert len(w) == 1
        assert 'Weights' in w[0].message.args[0]
        assert 'eq1' in w[0].message.args[0]
        assert 'eq2' not in w[0].message.args[0]
    mod.fit()
    expected = np.ones((500, 1))
    assert_allclose(mod._w[0], expected / expected.mean())
    expected = weights.values[:, [0]]
    assert_allclose(mod._w[1], expected / expected.mean())

    formula = '{y1 ~ 1 + x1} {y2 ~ 1 + x2}'
    weights = DataFrame(np.random.chisquare(5, (500, 1)), columns=['y2'])
    with warnings.catch_warnings(record=True) as w:
        mod = SUR.from_formula(formula, data, weights=weights)
        assert len(w) == 1
        assert 'y1' in w[0].message.args[0]
        assert 'y2' not in w[0].message.args[0]

    expected = np.ones((500, 1))
    assert_allclose(mod._w[0], expected / expected.mean())
    expected = weights.values[:, [0]]
    assert_allclose(mod._w[1], expected / expected.mean())
Ejemplo n.º 2
0
def test_formula_partial_weights():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=["y1", "y2", "x1", "x2"])
    weights = DataFrame(np.random.chisquare(5, (500, 1)), columns=["eq2"])
    formula = {"eq1": "y1 ~ 1 + x1", "eq2": "y2 ~ 1 + x1"}
    with warnings.catch_warnings(record=True) as w:
        mod = SUR.from_formula(formula, data, weights=weights)
        assert len(w) == 1
        assert "Weights" in w[0].message.args[0]
        assert "eq1" in w[0].message.args[0]
        assert "eq2" not in w[0].message.args[0]
    mod.fit()
    expected = np.ones((500, 1))
    assert_allclose(mod._w[0], expected / expected.mean())
    expected = weights.values[:, [0]]
    assert_allclose(mod._w[1], expected / expected.mean())

    formula = "{y1 ~ 1 + x1} {y2 ~ 1 + x2}"
    weights = DataFrame(np.random.chisquare(5, (500, 1)), columns=["y2"])
    with warnings.catch_warnings(record=True) as w:
        mod = SUR.from_formula(formula, data, weights=weights)
        assert len(w) == 1
        assert "y1" in w[0].message.args[0]
        assert "y2" not in w[0].message.args[0]

    expected = np.ones((500, 1))
    assert_allclose(mod._w[0], expected / expected.mean())
    expected = weights.values[:, [0]]
    assert_allclose(mod._w[1], expected / expected.mean())
Ejemplo n.º 3
0
def test_formula():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=['y1', 'y2', 'x1', 'x2'])
    formula = {'eq1': 'y1 ~ 1 + x1', 'eq2': 'y2 ~ 1 + x2'}
    mod = SUR.from_formula(formula, data)
    mod.fit()

    formula = '{y1 ~ 1 + x1} {y2 ~ 1 + x2}'
    mod = SUR.from_formula(formula, data)
    mod.fit(cov_type='heteroskedastic')

    formula = '''
    {y1 ~ 1 + x1}
    {y2 ~ 1 + x2}
    '''
    mod = SUR.from_formula(formula, data)
    mod.fit(cov_type='heteroskedastic')

    formula = '''
    {eq.a:y1 ~ 1 + x1}
    {second: y2 ~ 1 + x2}
    '''
    mod = SUR.from_formula(formula, data)
    res = mod.fit(cov_type='heteroskedastic')
    assert 'eq.a' in res.equation_labels
    assert 'second' in res.equation_labels
Ejemplo n.º 4
0
def test_formula():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=["y1", "y2", "x1", "x2"])
    formula = {"eq1": "y1 ~ 1 + x1", "eq2": "y2 ~ 1 + x2"}
    mod = SUR.from_formula(formula, data)
    mod.fit()

    formula = "{y1 ~ 1 + x1} {y2 ~ 1 + x2}"
    mod = SUR.from_formula(formula, data)
    mod.fit(cov_type="heteroskedastic")

    formula = """
    {y1 ~ 1 + x1}
    {y2 ~ 1 + x2}
    """
    mod = SUR.from_formula(formula, data)
    mod.fit(cov_type="heteroskedastic")

    formula = """
    {eq.a:y1 ~ 1 + x1}
    {second: y2 ~ 1 + x2}
    """
    mod = SUR.from_formula(formula, data)
    res = mod.fit(cov_type="heteroskedastic")
    assert "eq.a" in res.equation_labels
    assert "second" in res.equation_labels
Ejemplo n.º 5
0
def test_formula_repeated_key():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=['y1', 'y2', 'x1', 'x2'])

    formula = '''
    {first:y1 ~ 1 + x1}
    {first: y2 ~ 1 + x2}
    '''
    mod = SUR.from_formula(formula, data)
    res = mod.fit()
    assert 'first' in res.equation_labels
    assert 'first.0' in res.equation_labels
Ejemplo n.º 6
0
def test_formula_weights():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=["y1", "y2", "x1", "x2"])
    weights = DataFrame(np.random.chisquare(5, (500, 2)),
                        columns=["eq1", "eq2"])
    formula = {"eq1": "y1 ~ 1 + x1", "eq2": "y2 ~ 1 + x1"}
    mod = SUR.from_formula(formula, data, weights=weights)
    mod.fit()
    expected = weights.values[:, [0]]
    assert_allclose(mod._w[0], expected / expected.mean())
    expected = weights.values[:, [1]]
    assert_allclose(mod._w[1], expected / expected.mean())

    formula = "{y1 ~ 1 + x1} {y2 ~ 1 + x2}"
    weights = DataFrame(np.random.chisquare(5, (500, 2)), columns=["y1", "y2"])
    mod = SUR.from_formula(formula, data, weights=weights)
    mod.fit()
    expected = weights.values[:, [0]]
    assert_allclose(mod._w[0], expected / expected.mean())
    expected = weights.values[:, [1]]
    assert_allclose(mod._w[1], expected / expected.mean())
Ejemplo n.º 7
0
def test_formula_repeated_key():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=["y1", "y2", "x1", "x2"])

    formula = """
    {first:y1 ~ 1 + x1}
    {first: y2 ~ 1 + x2}
    """
    mod = SUR.from_formula(formula, data)
    res = mod.fit()
    assert "first" in res.equation_labels
    assert "first.0" in res.equation_labels
Ejemplo n.º 8
0
def test_formula_weights():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=['y1', 'y2', 'x1', 'x2'])
    weights = DataFrame(np.random.chisquare(5, (500, 2)), columns=['eq1', 'eq2'])
    formula = OrderedDict()
    formula['eq1'] = 'y1 ~ 1 + x1'
    formula['eq2'] = 'y2 ~ 1 + x1'
    mod = SUR.from_formula(formula, data, weights=weights)
    mod.fit()
    expected = weights.values[:, [0]]
    assert_allclose(mod._w[0], expected / expected.mean())
    expected = weights.values[:, [1]]
    assert_allclose(mod._w[1], expected / expected.mean())

    formula = '{y1 ~ 1 + x1} {y2 ~ 1 + x2}'
    weights = DataFrame(np.random.chisquare(5, (500, 2)), columns=['y1', 'y2'])
    mod = SUR.from_formula(formula, data, weights=weights)
    mod.fit()
    expected = weights.values[:, [0]]
    assert_allclose(mod._w[0], expected / expected.mean())
    expected = weights.values[:, [1]]
    assert_allclose(mod._w[1], expected / expected.mean())
Ejemplo n.º 9
0
def test_formula_errors():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=['y1', 'y2', 'x1', 'x2'])
    with pytest.raises(TypeError):
        SUR.from_formula(np.ones(10), data)
Ejemplo n.º 10
0
def test_formula_errors():
    data = DataFrame(np.random.standard_normal((500, 4)),
                     columns=["y1", "y2", "x1", "x2"])
    with pytest.raises(TypeError):
        SUR.from_formula(np.ones(10), data)