Пример #1
0
 def testTrim(self):
     S = PiecewiseFunction(fun=lambda x: sin(4*(x-0.5)), breakPoints=[-1, 1]).toInterpolated()
     I = S.trimInterpolators(abstol=1e-15)
     figure()
     subplot(211)
     S.plot(color="r")
     I.plot(color="k")
     r = S-I
     subplot(212)
     r.plot()
     #show()
     self.assert_(0 < 1)
Пример #2
0
 def testTrim(self):
     S = PiecewiseFunction(fun=lambda x: sin(4 * (x - 0.5)),
                           breakPoints=[-1, 1]).toInterpolated()
     I = S.trimInterpolators(abstol=1e-15)
     figure()
     subplot(211)
     S.plot(color="r")
     I.plot(color="k")
     r = S - I
     subplot(212)
     r.plot()
     #show()
     self.assert_(0 < 1)
Пример #3
0
 def testChebcoef(self):
     S = PiecewiseFunction(fun=lambda x: sin(4*(x-0.5)), breakPoints=[-1, 1]).toInterpolated()
     seg = S.segments[0]
     Xs, Ys = seg.f.Xs, seg.f.Ys
     print "Xs=", Xs
     print "Ys=", Ys
     print "Cs=", chebt2(Ys)
     print "Xs=", ichebt2(chebt2(Ys))
     figure()
     S.plot(color="r")
     D = S.diff()
     D.plot(color="k")
     #show()
     self.assert_(0 < 1)
Пример #4
0
 def testChebcoef(self):
     S = PiecewiseFunction(fun=lambda x: sin(4 * (x - 0.5)),
                           breakPoints=[-1, 1]).toInterpolated()
     seg = S.segments[0]
     Xs, Ys = seg.f.Xs, seg.f.Ys
     print "Xs=", Xs
     print "Ys=", Ys
     print "Cs=", chebt2(Ys)
     print "Xs=", ichebt2(chebt2(Ys))
     figure()
     S.plot(color="r")
     D = S.diff()
     D.plot(color="k")
     #show()
     self.assert_(0 < 1)
Пример #5
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()
Пример #6
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()
Пример #7
0
    B = PiecewiseFunction(fun=lambda x:sin(3*x), breakPoints=[-1,0,1])

    B = B.toInterpolated()
    print(B.segments[0].f.__class__)
    #B = B.trimInterpolators(abstol=1e-15)
    print(B.segments[0].f.Ys, B.segments[0].f.__class__)
    D = B.diff()
    D2 = D.diff()
    D3 = D2.diff()
    D4 = D3.diff()
    D5 = D4.diff()
    print(D.segments[0].f.Ys, D.segments[0].f.__class__)
    print(D2.segments[0].f.Ys)
    print(D.roots())
    figure()
    B.plot()
    D.plot()
    D2.plot()
    D3.plot()
    D4.plot()
    D5.plot()
    show()
    0/0
    #Xs = [-1,0,1]
    #Ys = [0,1,0]
    #i = Interpolator(Xs, Ys)
    #X = linspace(-1,1,100)
    #Y = [i.interp_at(x) for x in X]

    #ci = ChebyshevInterpolator(cos, -2, 8)
    #print ci.err, len(ci.Xs)
Пример #8
0
    B = PiecewiseFunction(fun=lambda x:sin(3*x), breakPoints=[-1,0,1])
    
    B = B.toInterpolated()
    print B.segments[0].f.__class__
    #B = B.trimInterpolators(abstol=1e-15)
    print B.segments[0].f.Ys, B.segments[0].f.__class__
    D = B.diff()
    D2 = D.diff()
    D3 = D2.diff()
    D4 = D3.diff()
    D5 = D4.diff()
    print D.segments[0].f.Ys, D.segments[0].f.__class__ 
    print D2.segments[0].f.Ys
    print D.roots()
    figure()
    B.plot()
    D.plot()
    D2.plot()
    D3.plot()
    D4.plot()
    D5.plot()
    show()
    0/0
    #Xs = [-1,0,1]
    #Ys = [0,1,0]
    #i = Interpolator(Xs, Ys)
    #X = linspace(-1,1,100)
    #Y = [i.interp_at(x) for x in X]

    #ci = ChebyshevInterpolator(cos, -2, 8)
    #print ci.err, len(ci.Xs)