Exemplo n.º 1
0
    def interp_f(self, *X):
        ndfun = self.ndfun
        arg = self.arg
        c_var = self.c_var
        v1 = self.v1
#        print "======="
#        print ndfun, arg, c_var, v1, X[0]
#        print self.ndfun.a
#        print self.ndfun.b
#        print self.ndfun.a[v1]
#        print self.ndfun.b[v1]
        if isscalar(X[0]):
            # TODO: fix integration bounds!!!!
            if hasattr(self.ndfun, "f"):
                y = integrate_fejer2(partial(self.integ_f, X), ndfun.f.a[v1], ndfun.f.b[v1])
            else:
                y = integrate_fejer2(partial(self.integ_f, X), ndfun.a[v1], ndfun.b[v1])
            return y[0]
        y = asfarray(zeros_like(X[0]))
        for i in range(len(X[0])):
            # TODO: fix integration bounds!!!!
            if hasattr(self.ndfun, "f"):
                y[i] = integrate_fejer2(partial(self.integ_f, [x[i] for x in X]), ndfun.f.a[v1], ndfun.f.b[v1])[0]
            else:
                y[i] = integrate_fejer2(partial(self.integ_f, [x[i] for x in X]), ndfun.a[v1], ndfun.b[v1])[0]
        return y
Exemplo n.º 2
0
    def eliminate(self, var, a=None, b=None):
        """Integrate out var, i.e. return the marginal on all
        remaining variables.

        Optional integration limits are allowed, default being full
        real line.  var is an RV instance or dimension number"""
        var, c_var = self.prepare_var(var)
        # assume below var are dimension numbers
        if len(var) == 0:
            return self
        v1 = var[-1]
        c_var = list(sorted(set(range(self.d)) - set([v1])))
        arg = zeros(self.d)
#        def integ_f(f, arg, c_var, v1, X, x1):
#            if isscalar(x1):
#                arg[c_var] = X
#                arg[v1] = x1
#                y = f(*arg)
#            else:
#                Xcol = [None] * self.d
#                for i, j in enumerate(c_var):
#                    Xcol[j] = zeros(len(x1)) + X[i]
#                Xcol[v1] = x1
#                return f(*Xcol)
#        def interp_f(f, arg, c_var, v1, *X):
#            if isscalar(X[0]):
#                # TODO: fix integration bounds!!!!
#                if hasattr(self, "f"):
#                    y = integrate_fejer2(partial(integ_f, self, arg, c_var, v1, X, self.d), self.f.a[v1], self.f.b[v1])
#                else:
#                    y = integrate_fejer2(partial(integ_f, self, arg, c_var, v1, X, self.d), self.a[v1], self.b[v1])
#                return y[0]
#            y = asfarray(zeros_like(X[0]))
#            for i in xrange(len(X[0])):
#                # TODO: fix integration bounds!!!!
#                if hasattr(self, "f"):
#                    y[i] = integrate_fejer2(partial(integ_f, self, arg, c_var, v1, [x[i] for x in X], self.d), self.f.a[v1], self.f.b[v1])[0]
#                else:
#                    y[i] = integrate_fejer2(partial(integ_f, self, arg, c_var, v1, [x[i] for x in X], self.d), self.a[v1], self.b[v1])[0]
#            return y
        if self.d == 1:
            X = []
#            if hasattr(self, "f"):
#                y = integrate_fejer2(partial(integ_f, self.fun, arg, c_var, v1, X), self.f.a[v1], self.f.b[v1])
#            else:
#                y = integrate_fejer2(partial(integ_f, self, arg, c_var, v1, X), self.a[v1], self.b[v1])
#            return NDConstFactor(y[0])
            if hasattr(self, "f"):
                y = integrate_fejer2(partial(InterpRunner(self.fun, arg, c_var, v1).integ_f, X), self.f.a[v1], self.f.b[v1])
            else:
                y = integrate_fejer2(partial(InterpRunner(self, arg, c_var, v1).integ_f, X), self.a[v1], self.b[v1])
            return NDConstFactor(y[0])
        #m = NDInterpolatedDistr(self.d - 1, partial(self.interp_f, self, arg, c_var, v1), [self.Vars[i] for i in c_var])
        m = NDInterpolatedDistr(self.d - 1, InterpRunner(self, arg, c_var, v1).interpxx, [self.Vars[i] for i in c_var])
        if len(var) == 1:
            return m
        else:
            # TODO: eliminate all vars at once using sparse grid integration
            return m.eliminate(var[:-1])
Exemplo n.º 3
0
 def interp_fx(self, X):
     ndfun = self.ndfun
     arg = self.arg
     c_var = self.c_var
     v1 = self.v1
     if hasattr(self.ndfun, "f"):
         y = integrate_fejer2(partial(self.integ_f, X), ndfun.f.a[v1], ndfun.f.b[v1])[0]
     else:
         y = integrate_fejer2(partial(self.integ_f, X), ndfun.a[v1], ndfun.b[v1])[0]
     return y
Exemplo n.º 4
0
 def interpxx(self, *X):
     """convolution of f and g
     """
     ndfun = self.ndfun
     v1 = self.v1
     if isscalar(X[0]):
         # TODO: fix integration bounds!!!!
         if hasattr(self.ndfun, "f"):
             y = integrate_fejer2(partial(self.integ_f, X), ndfun.f.a[v1], ndfun.f.b[v1])
         else:
             y = integrate_fejer2(partial(self.integ_f, X), ndfun.a[v1], ndfun.b[v1])
         return y[0]
     xx = transpose(array(X))
     if isscalar(xx):
         xx=asfarray([xx])
     #print ">>>>>>>>", xx
     p_map = get_parmap()
     res = p_map(self.interp_fx, xx)
     res = array(res)
     return res
Exemplo n.º 5
0
 def cov(self, i=None, j=None):
     if i is not None and j is not None:
         var, c_var = self.prepare_var([i, j])
         dij = self.eliminate(c_var)
         f, g = dij.marginals[0], self.marginals[1]
         fmean = f.mean()
         gmean = g.mean()
         f0, f1 = f.get_piecewise_pdf().range()
         g0, g1 = g.get_piecewise_pdf().range()
         print(fmean, gmean, var, c_var, f0, f1, g0, g1)
         if i == j:
             c, e = c, e = integrate_fejer2(lambda x: (x - fmean) ** 2 * f.pdf(x), f0, f1)                  
         else:
             c, e = integrate_iter(lambda x, y: (x - fmean) * (y - gmean) * dij.pdf(x, y), f0, f1, g0, g1)
         return c
     else:
         c = zeros((self.d, self.d))
         for i in range(self.d):
             for j in range(self.d):
                 c[i, j] = self.cov(i, j)                                          
         return c
Exemplo n.º 6
0
 def cov(self, i=None, j=None):
     if i is not None and j is not None:
         var, c_var = self.prepare_var([i, j])
         dij = self.eliminate(c_var)
         f, g = dij.marginals[0], self.marginals[1]
         fmean = f.mean()
         gmean = g.mean()
         f0, f1 = f.get_piecewise_pdf().range()
         g0, g1 = g.get_piecewise_pdf().range()
         print fmean, gmean, var, c_var, f0, f1, g0, g1
         if i == j:
             c, e = c, e = integrate_fejer2(
                 lambda x: (x - fmean)**2 * f.pdf(x), f0, f1)
         else:
             c, e = integrate_iter(
                 lambda x, y: (x - fmean) * (y - gmean) * dij.pdf(x, y), f0,
                 f1, g0, g1)
         return c
     else:
         c = zeros((self.d, self.d))
         for i in range(self.d):
             for j in range(self.d):
                 c[i, j] = self.cov(i, j)
         return c
Exemplo n.º 7
0
 def tau_c(self):
     return 1 + 4 * integrate_fejer2(
         lambda t: self.fi(t) / self.fi_deriv(t), 0, 1)[0]
Exemplo n.º 8
0
 def tau_c(self):
     return 1 + 4 * integrate_fejer2(lambda t : self.fi(t) / self.fi_deriv(t), 0, 1)[0]