コード例 #1
0
    def __call__(self, N):
        """
Sample generator

Parameters
----------
N : int
    Upper quadrature order

Returns
-------
samples : np.ndarray
    The quadrature nodes with `samples.shape=(D,K)` where
    `D=len(dist)` and `K` is the number of nodes.
        """
        dim = len(self.dist)

        if self.sparse == 0 or self.scheme in (3, 4, 5, 6):
            X = self.segment(N)
            if self.scheme in (1, 2):
                X = combine((X, ) * dim)
            out = self.trans(X)

        else:
            out = []
            for i in xrange(be.terms(N - self.sparse, dim), be.terms(N, dim)):
                I = be.multi_index(i, dim)
                out.append(combine([self.segment(n) for n in I]))
            out = self.trans(np.concatenate(out, 0))

        return out.T
コード例 #2
0
ファイル: sampler.py プロジェクト: xantares/chaospy
    def __call__(self, N):
        """
Sample generator

Parameters
----------
N : int
    Upper quadrature order

Returns
-------
samples : np.ndarray
    The quadrature nodes with `samples.shape=(D,K)` where
    `D=len(dist)` and `K` is the number of nodes.
        """
        dim = len(self.dist)

        if self.sparse==0 or self.scheme in (3,4,5,6):
            X = self.segment(N)
            if self.scheme in (1,2):
                X = combine((X,)*dim)
            out = self.trans(X)

        else:
            out = []
            for i in xrange(be.terms(N-self.sparse, dim),
                    be.terms(N, dim)):
                I = be.multi_index(i, dim)
                out.append(combine([self.segment(n) for n in I]))
            out = self.trans(np.concatenate(out, 0))

        return out.T
コード例 #3
0
    def __init__(self,
                 dist,
                 scheme=1,
                 box=True,
                 edge=False,
                 growth=0,
                 sparse=0):
        """
Parameters
----------
dist : Dist
    The domain where the samples are to be generated

Optional parameters

box : int
    0 : The samples are mapped onto the domain using an inverse
        Rosenblatt transform.
    1 : Will only use distribution bounds and evenly
        distribute samples inbetween.
    2 : Non transformation will be performed.
edge: bool
    True : Will include the bounds of the domain in the samples.
        If infinite domain, a resonable trunkation will be used.
growth : int
    0 : Linear growth rule. Minimizes the number of samples.
    1 : Exponential growth rule. Nested for schemes 1 and 2.
    2 : Double the number of polynomial terms. Recommended for
        schemes 4, 5 and 6.
sparse : int
    Defines the sparseness of the samples.
    sparse=len(dist) is equivalent to Smolyak sparse grid nodes.
    0 : A full tensor product nodes will be used
scheme : int
    0 : Gaussian quadrature will be used.
        box and edge argument will be ignored.
    1 : uniform distributed samples will be used.
    2 : The roots of Chebishev polynomials will be used
        (Clenshaw-Curtis and Fejer).
    3 : Stroud's cubature rule.
        Only order 2 and 3 valid,
        edge, growth and sparse are ignored.
    4 : Latin Hypercube sampling, no edge or sparse.
    5 : Classical random sampling, no edge or sparse.
    6 : Halton sampling, no edge or sparse
    7 : Hammersley sampling, no edge or sparse
    8 : Sobol sampling, no edge or sparse
    9 : Korobov samples, no edge or sparse
        """
        print "Warning: Sampler is depricated. Use samplegen instead"

        self.dist = dist
        self.scheme = scheme
        self.sparse = sparse

        # Gausian Quadrature
        if scheme == 0:
            segment = lambda n: _gw(n, dist)[0]
            self.trans = lambda x: x

        else:

            if box == 0:
                self.trans = lambda x: self.dist.inv(x.T).T
            elif box == 1:
                lo, up = dist.range().reshape(2, len(dist))
                self.trans = lambda x: np.array(x) * (up - lo) + lo
            elif box == 2:
                self.trans = lambda x: x

            if scheme == 1:
                _segment = lambda n: np.arange(0, n + 1) * 1. / n

            elif scheme == 2:
                _segment = lambda n: \
                    .5*np.cos(np.arange(n,-1,-1)*np.pi/n) + .5

        if scheme in (1, 2):
            if edge:

                if growth == 0:
                    segment = lambda n: _segment(n + 1)

                elif growth == 1:
                    segment = lambda n: _segment(2**n)

                elif growth == 2:
                    segment = lambda n: _segment(2 * be.terms(n, len(dist)))

            else:

                if growth == 0:
                    segment = lambda n: _segment(n + 2)[1:-1]

                elif growth == 1:
                    segment = lambda n: _segment(2**(n + 1))[1:-1]

                elif growth == 2:
                    segment = lambda n: _segment(2*be.terms(n, \
                                len(dist)))[1:-1]

        elif scheme == 4:
            if growth == 0:
                segment = lambda n: latin_hypercube(n + 1, len(dist))
            elif growth == 1:
                segment = lambda n: latin_hypercube(2**n, len(dist))
            elif growth == 2:
                segment = lambda n: latin_hypercube(2*be.terms(n, len(dist)), \
                        len(dist))

        elif scheme == 5:
            if growth == 0:
                segment = lambda n: np.random.random((n + 1, len(dist)))
            elif growth == 1:
                segment = lambda n: np.random.random((2**n, len(dist)))
            elif growth == 2:
                segment = lambda n: np.random.random(
                    (2 * be.terms(n + 1, len(dist)), len(dist)))

        elif scheme == 6:
            if growth == 0:
                segment = lambda n: halton(n + 1, len(dist))
            elif growth == 1:
                segment = lambda n: halton(2**n, len(dist))
            elif growth == 2:
                segment = lambda n: halton(2*be.terms(n, len(dist)), \
                        len(dist))

        elif scheme == 7:
            if growth == 0:
                segment = lambda n: hammersley(n + 1, len(dist))
            elif growth == 1:
                segment = lambda n: hammersley(2**n, len(dist))
            elif growth == 2:
                segment = lambda n: hammersley(2*be.terms(n, len(dist)), \
                        len(dist))

        elif scheme == 8:
            if growth == 0:
                segment = lambda n: sobol(n + 1, len(dist))
            elif growth == 1:
                segment = lambda n: sobol(2**n, len(dist))
            elif growth == 2:
                segment = lambda n: sobol(2*be.terms(n, len(dist)), \
                        len(dist))

        elif scheme == 9:
            if growth == 0:
                segment = lambda n: korobov(n + 1, len(dist))
            elif growth == 1:
                segment = lambda n: korobov(2**n, len(dist))
            elif growth == 2:
                segment = lambda n: korobov(2*be.terms(n, len(dist)), \
                        len(dist))

        self.segment = segment
コード例 #4
0
ファイル: sampler.py プロジェクト: xantares/chaospy
    def __init__(self, dist, scheme=1, box=True, edge=False,
            growth=0, sparse=0):
        """
Parameters
----------
dist : Dist
    The domain where the samples are to be generated

Optional parameters

box : int
    0 : The samples are mapped onto the domain using an inverse
        Rosenblatt transform.
    1 : Will only use distribution bounds and evenly
        distribute samples inbetween.
    2 : Non transformation will be performed.
edge: bool
    True : Will include the bounds of the domain in the samples.
        If infinite domain, a resonable trunkation will be used.
growth : int
    0 : Linear growth rule. Minimizes the number of samples.
    1 : Exponential growth rule. Nested for schemes 1 and 2.
    2 : Double the number of polynomial terms. Recommended for
        schemes 4, 5 and 6.
sparse : int
    Defines the sparseness of the samples.
    sparse=len(dist) is equivalent to Smolyak sparse grid nodes.
    0 : A full tensor product nodes will be used
scheme : int
    0 : Gaussian quadrature will be used.
        box and edge argument will be ignored.
    1 : uniform distributed samples will be used.
    2 : The roots of Chebishev polynomials will be used
        (Clenshaw-Curtis and Fejer).
    3 : Stroud's cubature rule.
        Only order 2 and 3 valid,
        edge, growth and sparse are ignored.
    4 : Latin Hypercube sampling, no edge or sparse.
    5 : Classical random sampling, no edge or sparse.
    6 : Halton sampling, no edge or sparse
    7 : Hammersley sampling, no edge or sparse
    8 : Sobol sampling, no edge or sparse
    9 : Korobov samples, no edge or sparse
        """
        print "Warning: Sampler is depricated. Use samplegen instead"

        self.dist = dist
        self.scheme = scheme
        self.sparse = sparse

        # Gausian Quadrature
        if scheme==0:
            segment = lambda n: _gw(n,dist)[0]
            self.trans = lambda x:x

        else:

            if box==0:
                self.trans = lambda x: self.dist.inv(x.T).T
            elif box==1:
                lo, up = dist.range().reshape(2, len(dist))
                self.trans = lambda x: np.array(x)*(up-lo) + lo
            elif box==2:
                self.trans = lambda x: x

            if scheme==1:
                _segment = lambda n: np.arange(0, n+1)*1./n

            elif scheme==2:
                _segment = lambda n: \
                    .5*np.cos(np.arange(n,-1,-1)*np.pi/n) + .5

        if scheme in (1,2):
            if edge:

                if growth==0:
                    segment = lambda n: _segment(n+1)

                elif growth==1:
                    segment = lambda n: _segment(2**n)

                elif growth==2:
                    segment = lambda n: _segment(2*be.terms(n, len(dist)))

            else:

                if growth==0:
                    segment = lambda n: _segment(n+2)[1:-1]

                elif growth==1:
                    segment = lambda n: _segment(2**(n+1))[1:-1]

                elif growth==2:
                    segment = lambda n: _segment(2*be.terms(n, \
                                len(dist)))[1:-1]


        elif scheme==4:
            if growth==0:
                segment = lambda n: latin_hypercube(n+1, len(dist))
            elif growth==1:
                segment = lambda n: latin_hypercube(2**n, len(dist))
            elif growth==2:
                segment = lambda n: latin_hypercube(2*be.terms(n, len(dist)), \
                        len(dist))

        elif scheme==5:
            if growth==0:
                segment = lambda n: np.random.random((n+1, len(dist)))
            elif growth==1:
                segment = lambda n: np.random.random((2**n, len(dist)))
            elif growth==2:
                segment = lambda n: np.random.random((2*be.terms(n+1,
                    len(dist)), len(dist)))


        elif scheme==6:
            if growth==0:
                segment = lambda n: halton(n+1, len(dist))
            elif growth==1:
                segment = lambda n: halton(2**n, len(dist))
            elif growth==2:
                segment = lambda n: halton(2*be.terms(n, len(dist)), \
                        len(dist))

        elif scheme==7:
            if growth==0:
                segment = lambda n: hammersley(n+1, len(dist))
            elif growth==1:
                segment = lambda n: hammersley(2**n, len(dist))
            elif growth==2:
                segment = lambda n: hammersley(2*be.terms(n, len(dist)), \
                        len(dist))

        elif scheme==8:
            if growth==0:
                segment = lambda n: sobol(n+1, len(dist))
            elif growth==1:
                segment = lambda n: sobol(2**n, len(dist))
            elif growth==2:
                segment = lambda n: sobol(2*be.terms(n, len(dist)), \
                        len(dist))

        elif scheme==9:
            if growth==0:
                segment = lambda n: korobov(n+1, len(dist))
            elif growth==1:
                segment = lambda n: korobov(2**n, len(dist))
            elif growth==2:
                segment = lambda n: korobov(2*be.terms(n, len(dist)), \
                        len(dist))


        self.segment = segment