Esempio n. 1
0
    def convmodel(self):
        """Probabilistic operation defined by model
        """
        op = self.symop  #d.getSym()
        x = self.symvars[0]
        y = self.symvars[1]
        lop = my_lambdify([x, y], op, "numpy")
        F = self.vars[0]
        G = self.vars[1]
        #self.nddistr.setMarginals(F, G)
        f = self.vars[0].get_piecewise_pdf()
        g = self.vars[1].get_piecewise_pdf()
        bf = f.getBreaks()
        bg = g.getBreaks()

        bi = zeros(len(bf) * len(bg))
        k = 0
        for xi in bf:
            for yi in bg:
                if not isnan(lop(xi, yi)):
                    bi[k] = lop(xi, yi)
                else:
                    pass
                    #print "not a number, xi=", xi, "yi=", yi, "result=", lop(xi,yi)
                k += 1
        ub = array(unique(bi))

        fun = lambda x: self.convmodelx(segList, x)
        fg = PiecewiseDistribution([])

        if isinf(ub[0]):
            segList = _findSegList(f, g, ub[1] - 1, lop)
            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, lop)
            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, lop)
            seg = Segment(ub[i], ub[i + 1], partial(self.convmodelx, segList))
            #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
Esempio n. 2
0
    def convmodel(self):
        """Probabilistic operation defined by model
        """
        op = self.symop#d.getSym()
        x = self.symvars[0]
        y = self.symvars[1]
        lop = my_lambdify([x, y], op, "numpy")
        F = self.vars[0]
        G = self.vars[1]
        #self.nddistr.setMarginals(F, G)
        f = self.vars[0].get_piecewise_pdf()
        g = self.vars[1].get_piecewise_pdf()
        bf = f.getBreaks()
        bg = g.getBreaks()

        bi = zeros(len(bf) * len(bg))
        k = 0;
        for xi in bf:
            for yi in bg:
                if not isnan(lop(xi, yi)):
                    bi[k] = lop(xi, yi)
                else:
                    pass
                    #print "not a number, xi=", xi, "yi=", yi, "result=", lop(xi,yi)
                k += 1
        ub = array(unique(bi))

        fun = lambda x : self.convmodelx(segList, x)
        fg = PiecewiseDistribution([]);

        if isinf(ub[0]):
            segList = _findSegList(f, g, ub[1] -1, lop)
            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, lop)
            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, lop)
            seg = Segment(ub[i], ub[i + 1], partial(self.convmodelx, segList))
            #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
Esempio n. 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
Esempio n. 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