Example #1
0
def plot_regression(F, Ybreaks=None):
    assert F.d == 2
    X = F.marginals[0]
    Y = F.marginals[1]
    if Ybreaks is None:
        Ybreaks = Y.get_piecewise_pdf().getBreaks()

    def cond_pdf(F, Xpdf, x, y):
        if not isscalar(y):
            x = zeros_like(y) + x
        return F.pdf(x, y) / Xpdf

    def regfun(type, x):
        # value of regression functions at point x
        if isscalar(x):
            Xpdf = float(X.pdf(x))
            if Xpdf == 0:
                y = NaN
            else:
                distr = FunDistr(fun=partial(cond_pdf, F, Xpdf, x),
                                 breakPoints=Ybreaks)
                if type == 1: y = distr.mean()
                if type == 2: y = distr.median()
                if type == 3: y = distr.mode()
                if y is None:
                    y = NaN
        else:
            y = zeros_like(x)
            for i in range(len(x)):
                y[i] = regfun(type, x[i])
        return y

    F.contour()

    Xbreaks = X.get_piecewise_pdf().getBreaks()
    Xbreaks = concatenate([Xbreaks, [F.a[0], F.b[0]]])
    Xbreaks.sort()
    Xbreaks = epsunique(Xbreaks)
    mreg = PiecewiseFunction(fun=partial(regfun, 1),
                             breakPoints=Xbreaks).toInterpolated()
    mreg.plot(label="mean")
    mreg = PiecewiseFunction(fun=partial(regfun, 2),
                             breakPoints=Xbreaks).toInterpolated()
    mreg.plot(label="median", color="g")
    mreg = PiecewiseFunction(fun=partial(regfun, 3),
                             breakPoints=Xbreaks).toInterpolated()
    mreg.plot(label="mode", color="r")
    legend()
Example #2
0
def plot_regression(F, Ybreaks = None):
    assert F.d == 2
    X = F.marginals[0]
    Y = F.marginals[1]
    if Ybreaks is None:
        Ybreaks = Y.get_piecewise_pdf().getBreaks()

    def cond_pdf(F, Xpdf, x, y):
        if not isscalar(y):
            x = zeros_like(y) + x
        return F.pdf(x, y) / Xpdf
    def regfun(type, x):
        # value of regression functions at point x
        if isscalar(x):
            Xpdf = float(X.pdf(x))
            if Xpdf == 0:
                y = NaN
            else:
                distr = FunDistr(fun = partial(cond_pdf, F, Xpdf, x),
                                 breakPoints = Ybreaks)
                if type==1: y = distr.mean()
                if type==2: y = distr.median()
                if type==3: y = distr.mode()  
                if y is None:
                    y = NaN
        else:
            y = zeros_like(x)
            for i in range(len(x)):
                y[i] = regfun(type, x[i])
        return y 

    F.contour()

    Xbreaks = X.get_piecewise_pdf().getBreaks()
    Xbreaks = concatenate([Xbreaks, [F.a[0], F.b[0]]])
    Xbreaks.sort()
    Xbreaks = epsunique(Xbreaks)
    mreg = PiecewiseFunction(fun=partial(regfun, 1), breakPoints=Xbreaks).toInterpolated()
    mreg.plot(label = "mean")
    mreg = PiecewiseFunction(fun=partial(regfun, 2), breakPoints=Xbreaks).toInterpolated()
    mreg.plot(label = "median", color = "g")
    mreg = PiecewiseFunction(fun=partial(regfun, 3), breakPoints=Xbreaks).toInterpolated()
    mreg.plot(label = "mode", color = "r")
    legend()
Example #3
0
def convmean(F, G, p=0.5, q=0.5, theta=1.0):
    """Probabilistic weighted mean of f and g
    """
    f = F.get_piecewise_pdf()
    g = G.get_piecewise_pdf()
    if p + q <> 1.0:
        p1 = abs(p) / (abs(p) + abs(q))
        q = abs(q) / (abs(p) + abs(q))
        p = p1
    if q == 0:
        return f
    bf = f.getBreaks()
    bg = g.getBreaks()
    b = add.outer(bf * p, bg * q)
    fun = lambda x: convmeanx(F, G, segList, x, p, q, theta=theta)
    ub = epsunique(b)
    fg = PiecewiseDistribution([])
    op = lambda x, y: p * x + q * y
    if isinf(ub[0]):
        segList = _findSegList(f, g, ub[1] - 1, op)
        seg = MInfSegment(ub[1], fun)
        segint = seg.toInterpolatedSegment()
        fg.addSegment(segint)
        ub = ub[1:]
    if isinf(ub[-1]):
        segList = _findSegList(f, g, ub[-2] + 1, op)
        seg = PInfSegment(ub[-2], fun)
        segint = seg.toInterpolatedSegment()
        fg.addSegment(segint)
        ub = ub[0:-1]
    for i in range(len(ub) - 1):
        segList = _findSegList(f, g, (ub[i] + ub[i + 1]) / 2, op)
        seg = Segment(ub[i], ub[i + 1], fun)
        segint = seg.toInterpolatedSegment()
        fg.addSegment(segint)

    # Discrete parts of distributions
    fg_discr = convdiracs(f, g, fun=lambda x, y: x * p + y * q)
    for seg in fg_discr.getDiracs():
        fg.addSegment(seg)
    return fg
Example #4
0
def convmean(F, G, p=0.5, q=0.5, theta=1.0):
    """Probabilistic weighted mean of f and g
    """
    f = F.get_piecewise_pdf()
    g = G.get_piecewise_pdf()
    if  p + q != 1.0 :
        p1 = abs(p) / (abs(p) + abs(q))
        q = abs(q) / (abs(p) + abs(q))
        p = p1;
    if q == 0:
        return f;
    bf = f.getBreaks()
    bg = g.getBreaks()
    b = add.outer(bf * p, bg * q)
    fun = lambda x : convmeanx(F, G, segList, x, p, q, theta=theta)
    ub = epsunique(b)
    fg = PiecewiseDistribution([]);
    op = lambda x, y : p * x + q * y;
    if isinf(ub[0]):
        segList = _findSegList(f, g, ub[1] - 1, op)
        seg = MInfSegment(ub[1], fun)
        segint = seg.toInterpolatedSegment()
        fg.addSegment(segint)
        ub = ub[1:]
    if isinf(ub[-1]):
        segList = _findSegList(f, g, ub[-2] + 1, op)
        seg = PInfSegment(ub[-2], fun)
        segint = seg.toInterpolatedSegment()
        fg.addSegment(segint)
        ub = ub[0:-1]
    for i in range(len(ub) - 1) :
        segList = _findSegList(f, g, (ub[i] + ub[i + 1]) / 2, op)
        seg = Segment(ub[i], ub[i + 1], fun)
        segint = seg.toInterpolatedSegment()
        fg.addSegment(segint)

    # Discrete parts of distributions
    fg_discr = convdiracs(f, g, fun=lambda x, y : x * p + y * q)
    for seg in fg_discr.getDiracs():
        fg.addSegment(seg)
    return fg