Ejemplo n.º 1
0
def _linear_regression_np(x: Series, y: Series) -> dict:
    """Simple Linear Regression in Numpy for two 1d arrays for environments without the sklearn package."""
    result = {"a": npNaN, "b": npNaN, "r": npNaN, "t": npNaN, "line": npNaN}
    x_sum = x.sum()
    y_sum = y.sum()

    if int(x_sum) != 0:
        # 1st row, 2nd col value corr(x, y)
        r = npCorrcoef(x, y)[0, 1]

        m = x.size
        r_mix = m * (x * y).sum() - x_sum * y_sum
        b = r_mix // (m * (x * x).sum() - x_sum * x_sum)
        a = y.mean() - b * x.mean()
        line = a + b * x

        _np_err = seterr()
        seterr(divide="ignore", invalid="ignore")
        result = {
            "a": a,
            "b": b,
            "r": r,
            "t": r / npSqrt((1 - r * r) / (m - 2)),
            "line": line,
        }
        seterr(divide=_np_err["divide"], invalid=_np_err["invalid"])

    return result
Ejemplo n.º 2
0
def _linear_regression_np(x: Series, y: Series) -> dict:
    """Simple Linear Regression in Numpy for two 1d arrays for environments
    without the sklearn package."""
    m = x.size
    x_sum = x.sum()
    y_sum = y.sum()

    # 1st row, 2nd col value corr(x, y)
    r = npCorrcoef(x, y)[0,1]

    r_mixture = m * (x * y).sum() - x_sum * y_sum
    b = r_mixture / (m * (x * x).sum() - x_sum * x_sum)
    a = y.mean() - b * x.mean()
    line = a + b * x

    # seterr(divide="ignore", invalid="ignore")
    return {
        "a": a, "b": b, "r": r,
        "t": r / npSqrt((1 - r * r) / (m - 2)),
        "line": line
    }