Repository: scipy/scipy
File: scipy/stats/tests/test_mstats_basic.py (Python)

Code before:
                     0.838, 1.0, 0.962, 0.666, 0.981, 0.978]),
              array([0.771, 0.590, 0.968, 0.654, 0.886, 0.916, 0.609, 0.563,
                     0.866, 1.0, 0.965, 0.614, 0.9751, 0.946]),
              array([0.798, 0.569, 0.967, 0.657, 0.898, 0.931, 0.685, 0.625,
                     0.875, 1.0, 0.962, 0.669, 0.975, 0.970])]

        # From "Bioestadistica para las ciencias de la salud" Xf=18.95 p<0.001:
        x2 = [array([4, 3, 5, 3, 5, 3, 2, 5, 4, 4, 4, 3]),
              array([2, 2, 1, 2, 3, 1, 2, 3, 2, 1, 1, 3]),
              array([2, 4, 3, 3, 4, 3, 3, 4, 4, 1, 2, 1]),
              array([3, 5, 4, 3, 4, 4, 3, 3, 3, 4, 4, 4])]

        # From Jerrorl H. Zar, "Biostatistical Analysis"(example 12.6),
        # Xf=10.68, 0.005 < p < 0.01:
        # Probability from this example is inexact
        # using Chisquare approximation of Friedman Chisquare.
        x3 = [array([7.0, 9.9, 8.5, 5.1, 10.3]),
              array([5.3, 5.7, 4.7, 3.5, 7.7]),
              array([4.9, 7.6, 5.5, 2.8, 8.4]),
              array([8.8, 8.9, 8.1, 3.3, 9.1])]

        # test using mstats
        assert_array_almost_equal(mstats.friedmanchisquare(x1[0], x1[1],
                                                           x1[2], x1[3]),
                                  (10.2283464566929, 0.0167215803284414))
        # the following fails
        # assert_array_almost_equal(mstats.friedmanchisquare(x2[0],x2[1],x2[2],x2[3]),
        #                           (18.9428571428571, 0.000280938375189499))

        assert_array_almost_equal(mstats.friedmanchisquare(x3[0], x3[1],
                                                           x3[2], x3[3]),
                                  (10.68, 0.0135882729582176))
        assert_raises(ValueError, mstats.friedmanchisquare,x3[0],x3[1])


def test_regress_simple():
    # Regress a line with sinusoidal noise. Test for #1273.
    x = np.linspace(0, 100, 100)
    y = 0.2 * np.linspace(0, 100, 100) + 10
    y += np.sin(np.linspace(0, 20, 100))

    result = mstats.linregress(x, y)

    # Result is of a correct class and with correct fields
    lr = _stats_py.LinregressResult
    assert_(isinstance(result, lr))
    attributes = ('slope', 'intercept', 'rvalue', 'pvalue', 'stderr')
    check_named_results(result, attributes, ma=True)
    assert 'intercept_stderr' in dir(result)

    # Slope and intercept are estimated correctly

Review comment (bug):
```suggestion

        # the following fails
```

Code after:
                     0.583, 0.838, 1.0, 0.962, 0.666, 0.981, 0.978]),
              array([0.771, 0.590, 0.968, 0.654, 0.886, 0.916, 0.609,
                     0.563, 0.866, 1.0, 0.965, 0.614, 0.9751, 0.946]),
              array([0.798, 0.569, 0.967, 0.657, 0.898, 0.931, 0.685,
                     0.625, 0.875, 1.0, 0.962, 0.669, 0.975, 0.970])]

        # From "Bioestadistica para las ciencias de la salud" Xf=18.95 p<0.001:
        # x2 = [array([4, 3, 5, 3, 5, 3, 2, 5, 4, 4, 4, 3]),
        #       array([2, 2, 1, 2, 3, 1, 2, 3, 2, 1, 1, 3]),
        #       array([2, 4, 3, 3, 4, 3, 3, 4, 4, 1, 2, 1]),
        #       array([3, 5, 4, 3, 4, 4, 3, 3, 3, 4, 4, 4])]

        # From Jerrorl H. Zar, "Biostatistical Analysis"(example 12.6),
        # Xf=10.68, 0.005 < p < 0.01:
        # Probability from this example is inexact
        # using Chisquare approximation of Friedman Chisquare.
        x3 = [array([7.0, 9.9, 8.5, 5.1, 10.3]),
              array([5.3, 5.7, 4.7, 3.5, 7.7]),
              array([4.9, 7.6, 5.5, 2.8, 8.4]),
              array([8.8, 8.9, 8.1, 3.3, 9.1])]

        # test using mstats
        assert_array_almost_equal(mstats.friedmanchisquare(*x1),
                                  (10.2283464566929, 0.0167215803284414))

        # the following fails
        # assert_array_almost_equal(mstats.friedmanchisquare(*x2),
        #                           (18.9428571428571, 0.000280938375189499))

        assert_array_almost_equal(mstats.friedmanchisquare(*x3),
                                  (10.68, 0.0135882729582176))
        assert_raises(ValueError, mstats.friedmanchisquare, x3[0], x3[1])


def test_regress_simple():
    # Regress a line with sinusoidal noise. Test for #1273.
    x = np.linspace(0, 100, 100)
    y = 0.2 * np.linspace(0, 100, 100) + 10
    y += np.sin(np.linspace(0, 20, 100))

    result = mstats.linregress(x, y)

    # Result is of a correct class and with correct fields
    lr = _stats_py.LinregressResult
    assert_(isinstance(result, lr))
    attributes = ('slope', 'intercept', 'rvalue', 'pvalue', 'stderr')
    check_named_results(result, attributes, ma=True)
    assert 'intercept_stderr' in dir(result)

    # Slope and intercept are estimated correctly
    assert_almost_equal(result.slope, 0.19644990055858422)