Beispiel #1
0
def iid_order_stat(X, n, k, **kwargs):
    """It gives distribution of $k$-th observation in ordered sample size of n."""
    pdf = X.get_piecewise_pdf()
    cdf = X.get_piecewise_cdf()
    ccdf = 1 - X.get_piecewise_cdf()
    fun = (k * binomial_coeff(n, k)) * pdf * (pow(cdf, k-1) * pow(ccdf, n-k))
    #return PDistr(fun.toInterpolated())
    return FunDistr(fun = fun.toInterpolated(), breakPoints = X.get_piecewise_pdf().getBreaks(), **kwargs)
Beispiel #2
0
def iid_order_stat(X, n, k, **kwargs):
    """It gives distribution of $k$-th observation in ordered sample size of n."""
    pdf = X.get_piecewise_pdf()
    cdf = X.get_piecewise_cdf()
    ccdf = 1 - X.get_piecewise_cdf()
    fun = (k * binomial_coeff(n, k)) * pdf * (pow(cdf, k-1) * pow(ccdf, n-k))
    #return PDistr(fun.toInterpolated())
    return FunDistr(fun = fun.toInterpolated(), breakPoints = X.get_piecewise_pdf().getBreaks(), **kwargs)
Beispiel #3
0
    def get_nodes(self, q):
        """Compute nodes for current ni."""
        nodes = []
        node_inds_map = {}
        subgrid_map = {}  # maps subgrids to nodes

        ps = gen_Q(
            q, self.d
        )  # compute partitions.  Each partition corresponds to a subgrid
        for p in ps:
            # each new node is described with a set of indices.  Each
            # index is the number of interpolation root for given node
            # and dimension
            max_q = q - self.d + 1
            max_d = self.exps[max_q]
            if self.init_ni == 1:
                new_nodes = itertools.product(*[
                    list(range(0, max_d, 1 << (max_q -
                                               e))) if e > 1 else [max_d / 2]
                    for e in p
                ])
            else:
                new_nodes = itertools.product(
                    *[list(range(0, max_d, 1 << (max_q - e))) for e in p])

            # node indices for this subgrid
            subgrid_indices = []
            for ni in new_nodes:
                if ni in node_inds_map:
                    idx = node_inds_map[ni]
                else:
                    idx = len(nodes)
                    node_inds_map[ni] = idx
                    nodes.append(ni)
                subgrid_indices.append(idx)
            # subgrid's coefficient
            si = sum(p)
            c = (-1)**(q - si) * binomial_coeff(self.d - 1, q - si)
            subgrid_map[p] = (c, array(subgrid_indices))
        if params.interpolation_nd.debug_info:
            print(len(subgrid_map),
                  "rect. subgrids,",
                  sum(len(subgrid_map[k]) for k in subgrid_map),
                  "ops per X,",
                  len(nodes),
                  "nodes,",
                  end=' ')
            print("full grid size", self.exps[q - self.d + 1]**self.d, "dim",
                  self.d)
        return nodes, subgrid_map
Beispiel #4
0
    def get_nodes(self, q):
        """Compute nodes for current ni."""
        nodes = []
        node_inds_map = {}
        subgrid_map = {} # maps subgrids to nodes

        ps = gen_Q(q, self.d)  # compute partitions.  Each partition corresponds to a subgrid
        for p in ps:
            # each new node is described with a set of indices.  Each
            # index is the number of interpolation root for given node
            # and dimension
            max_q = q-self.d+1
            max_d = self.exps[max_q]
            if self.init_ni == 1:
                new_nodes = itertools.product(*[list(range(0, max_d, 1 << (max_q - e))) if e > 1 else [max_d/2] for e in p])
            else:
                new_nodes = itertools.product(*[list(range(0, max_d, 1 << (max_q - e))) for e in p])

            # node indices for this subgrid
            subgrid_indices = []
            for ni in new_nodes:
                if ni in node_inds_map:
                    idx = node_inds_map[ni]
                else:
                    idx = len(nodes)
                    node_inds_map[ni] = idx
                    nodes.append(ni)
                subgrid_indices.append(idx)
            # subgrid's coefficient
            si = sum(p)
            c = (-1)**(q-si) * binomial_coeff(self.d - 1, q-si)
            subgrid_map[p] = (c, array(subgrid_indices))
        if params.interpolation_nd.debug_info:
            print(len(subgrid_map), "rect. subgrids,", sum(len(subgrid_map[k]) for k in subgrid_map), "ops per X,", len(nodes), "nodes,", end=' ')
            print("full grid size", self.exps[q-self.d+1]**self.d, "dim", self.d)
        return nodes, subgrid_map
Beispiel #5
0
        D = UniformDistr(0, 1)
        for i in range(m - 1):
            D += UniformDistr(0, 1)
        d = N / D
        figure()
        demo_distr(d, xmax=10)

        a = 0.9
        nmfact = 1
        for i in range(2, n + m + 1):
            nmfact *= i
        nrm = a**m * nmfact
        s = 0
        for i in range(int(numpy.floor(m * a) + 1)):
            for j in range(int(numpy.floor((m * a - i) / a) + 1)):
                s += (-1)**(i + j) * binomial_coeff(n, i) * binomial_coeff(
                    m, j) * ((m - j) * a - i)**(n + m)
        cdf_formula = s / nrm
        cdf_pacal = d.cdf(a)
        print("n={0},m={1}  cdf({2})={3}, err = {4}".format(
            n, m, a, cdf_pacal, abs(cdf_pacal - cdf_formula)))

    #! Exercise 4.32
    for l1, l2 in [(1, 1), (10, 1), (1, 10)]:
        d = ExponentialDistr(l1) / ExponentialDistr(l2)
        figure()
        alpha = float(l2) / l1
        demo_distr(d, xmax=10, theoretical=lambda x: alpha / (x + alpha)**2)
    print("time=", time.time() - tic)
    show()
Beispiel #6
0
        for i in xrange(n-1):
            N += UniformDistr(0,1)
        D = UniformDistr(0,1)
        for i in xrange(m-1):
            D += UniformDistr(0,1)
        d = N/D
        figure()
        demo_distr(d, xmax = 10)
    
        a = 0.9
        nmfact = 1
        for i in xrange(2, n+m+1):
            nmfact *= i
        nrm = a**m * nmfact
        s = 0
        for i in xrange(int(numpy.floor(m*a)+1)):
            for j in xrange(int(numpy.floor((m*a-i)/a)+1)):
                s += (-1)**(i+j) * binomial_coeff(n, i) * binomial_coeff(m, j) * ((m-j)*a-i)**(n+m)
        cdf_formula = s / nrm
        cdf_pacal = d.cdf(a)
        print "n={0},m={1}  cdf({2})={3}, err = {4}".format(n, m, a, cdf_pacal, abs(cdf_pacal - cdf_formula))
    
    #! Exercise 4.32
    for l1, l2 in [(1,1), (10,1), (1,10)]:
        d = ExponentialDistr(l1) / ExponentialDistr(l2)
        figure()
        alpha = float(l2) / l1
        demo_distr(d, xmax = 10, theoretical = lambda x: alpha / (x+alpha)**2)
    print "time=", time.time() - tic
    show()