예제 #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.assertTrue(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.assertTrue(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.assertTrue(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.assertTrue(0 < 1)
예제 #5
0
 def regfun(self, var, type=0):
     """It gives reggersion function E(var | I) """
     var, c_var = self.prepare_var(var)
     var, c_var  = self.Vars[var[0]], self.Vars[c_var[0]]
     #print ">>>", var, c_var
     assert self.d == 2
     def _fun(x):
         #print ">>>>>>", x, self.condition([c_var], x).distr_pdf(0.2)
         #self.condition(c_var, [x]).distr_pdf.plot()
         #show()
         if isscalar(x):
             distr = FunDistr(fun=self.condition([c_var], x).distr_pdf, breakPoints=var.get_piecewise_pdf().getBreaks())
             if type==0:
                 return distr.mean()
             elif type==1:
                 return distr.median()
             elif type==2:
                 return distr.mode()
             elif type==3:
                 return distr.quantile(0.025)
             elif type==4:
                 return distr.quantile(0.975)
             else:
                 assert 1==0
             #return distr.median()
         else:
             y =  zeros_like(x)
             for i in range(len(x)):
                 print(i, "|||", _fun(x[i]))
                 y[i] = _fun(x[i])
             return y
         #distr = FunDistr(fun=self.condition([c_var], x).distr_pdf, breakPoints=var.get_piecewise_pdf().getBreaks())
         #return distr.mean()
     #print "+++", _fun(0.5)
     return PiecewiseFunction(fun=_fun, breakPoints=c_var.get_piecewise_pdf().getBreaks()).toInterpolated()
예제 #6
0
파일: iid_ops.py 프로젝트: dkasak/pacal
def iid_unknown(X, n, k, **kwargs):
    """It gives distribution of sample _unknown on level k based on sample size of n."""
    fun = PiecewiseFunction([])
    for i in xrange(k, n+1):
        fun += iid_order_stat(X, n, i).get_piecewise_pdf()
    fun2 = (1.0 / (1+n-k)) * fun
    return FunDistr(fun = fun2.toInterpolated(), breakPoints = X.get_piecewise_pdf().getBreaks(), **kwargs)
예제 #7
0
파일: regression.py 프로젝트: dkasak/pacal
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()
예제 #8
0
 def __init__(self, d, Vars, fun, safe = False, abs = False):
     self.d = d
     self.a = [-5] * d # FIX THIS!!!
     self.b = [5] * d # FIX THIS!!!
     self.safe = safe
     self.abs = abs
     if Vars is not None:
         self.a, self.b = getRanges(Vars)
     i=0
     if Vars is None:
         i+=-1
         Vars = [RV(sym=LETTERS[i]) for i in range(d)]
     self.Vars = Vars
     var_ids = [v.id() for v in self.Vars]
     self.id_to_idx = dict((id, idx) for idx, id in enumerate(var_ids))
     self.idx_to_id = [id_ for idx, id_ in enumerate(var_ids)]
     self.symVars = [v.getSymname() for v in Vars]
     self.sym_to_var = dict((v.getSymname(), v) for v in Vars)
     if len(Vars)==1:
         a, b = getRanges(Vars)
         self.distr_pdf = PiecewiseFunction(fun=fun, breakPoints=[a[0], b[0]])
         self.fun = self.distr_pdf
     else:
         self.fun = fun
예제 #9
0
 def mgf(self):
     return PiecewiseFunction(fun=partial(_mgf_fun, self), breakPoints=self.get_piecewise_pdf().getBreaks())
예제 #10
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()
예제 #11
0
#    def __init__(self, f, U):
#        vt = VarTransformAlgebraic_MInf(U)
#        super(ChebyshevInterpolator_MInf, self).__init__(f, vt)



if __name__ == "__main__":
    from pylab import *
    from pacal import *
#    B= BetaDistr(1,1) * UniformDistr(0,3)
#    B =(UniformDistr(0,3)+UniformDistr(0,1)+UniformDistr(0,1)+UniformDistr(0,1)) * (UniformDistr(0,1)+UniformDistr(0,1)+UniformDistr(0,1))
#    B = BetaDistr(4,4) *  (UniformDistr(-1,1)+UniformDistr(-1,2))
#    B.summary(show_moments=True)
#    print B.get_piecewise_pdf()
    from pacal.segments import PiecewiseFunction
    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()
예제 #12
0
#    def __init__(self, f, U):
#        vt = VarTransformAlgebraic_MInf(U)
#        super(ChebyshevInterpolator_MInf, self).__init__(f, vt)



if __name__ == "__main__":
    from pylab import *
    from pacal import *
#    B= BetaDistr(1,1) * UniformDistr(0,3)
#    B =(UniformDistr(0,3)+UniformDistr(0,1)+UniformDistr(0,1)+UniformDistr(0,1)) * (UniformDistr(0,1)+UniformDistr(0,1)+UniformDistr(0,1))
#    B = BetaDistr(4,4) *  (UniformDistr(-1,1)+UniformDistr(-1,2))
#    B.summary(show_moments=True)
#    print B.get_piecewise_pdf()
    from pacal.segments import PiecewiseFunction
    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()