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
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
def samplegen(order, domain, rule="S", antithetic=None, verbose=False): """ Sample generator Parameters ---------- order : int Sample order domain : Dist, int, array_like Defines the space where the samples are generated. Dist Mapped to distribution domain using inverse Rosenblatt. int No mapping, but sets the number of dimension. array_like Stretch samples such that they are in [domain[0], domain[1]] rule : str rule for generating samples, where d is the number of dimensions. Key Name Nested ---- ---------------- ------ "C" Chebyshev nodes no "NC" Nested Chebyshev yes "G" Gaussian quadrature no "K" Korobov no "R" (Pseudo-)Random no "RG" Regular grid no "NG" Nested grid yes "L" Latin hypercube no "S" Sobol yes "H" Halton yes "M" Hammersley yes antithetic : array_like, optional List of bool. Represents the axes to mirror using antithetic variable. """ rule = rule.upper() if isinstance(domain, int): dim = domain trans = lambda x, verbose: x elif isinstance(domain, (tuple, list, np.ndarray)): domain = np.asfarray(domain) if len(domain.shape) < 2: dim = 1 else: dim = len(domain[0]) lo, up = domain trans = lambda x, verbose: ((up - lo) * x.T + lo).T else: dist = domain dim = len(dist) trans = dist.inv if not (antithetic is None): antithetic = np.array(antithetic, dtype=bool).flatten() if antithetic.size == 1 and dim > 1: antithetic = np.repeat(antithetic, dim) N = np.sum(1 * np.array(antithetic)) order_, order = order, int(order * 2**-N + 1 * (order % 2 != 0)) trans_ = trans trans = lambda X, verbose: \ trans_(antithetic_gen(X, antithetic)[:,:order_]) if rule == "C": X = chebyshev(dim, order) elif rule == "NC": X = chebyshev_nested(dim, order) elif rule == "G": X = _gw(order - 1, dist)[0] trans = lambda x, verbose: x elif rule == "K": X = korobov(dim, order) elif rule == "R": X = np.random.random((dim, order)) elif rule == "RG": X = regular_grid(dim, order) elif rule == "NG": X = regular_grid_nested(dim, order) elif rule == "L": X = latin_hypercube(dim, order) elif rule == "S": X = sobol(dim, order) elif rule == "H": X = halton(dim, order) elif rule == "M": X = hammersley(dim, order) else: raise KeyError, "rule not recognised" X = trans(X, verbose=verbose) return X
def samplegen(order, domain, rule="S", antithetic=None, verbose=False): """ Sample generator Parameters ---------- order : int Sample order domain : Dist, int, array_like Defines the space where the samples are generated. Dist Mapped to distribution domain using inverse Rosenblatt. int No mapping, but sets the number of dimension. array_like Stretch samples such that they are in [domain[0], domain[1]] rule : str rule for generating samples, where d is the number of dimensions. Key Name Nested ---- ---------------- ------ "C" Chebyshev nodes no "NC" Nested Chebyshev yes "G" Gaussian quadrature no "K" Korobov no "R" (Pseudo-)Random no "RG" Regular grid no "NG" Nested grid yes "LH" Latin hypercube no "S" Sobol yes "H" Halton yes "M" Hammersley yes antithetic : array_like, optional List of bool. Represents the axes to mirror using antithetic variable. """ rule = rule.upper() if isinstance(domain, int): dim = domain trans = lambda x, verbose:x elif isinstance(domain, (tuple, list, np.ndarray)): domain = np.asfarray(domain) if len(domain.shape)<2: dim = 1 else: dim = len(domain[0]) lo,up = domain trans = lambda x, verbose: ((up-lo)*x.T + lo).T else: dist = domain dim = len(dist) trans = dist.inv if not (antithetic is None): antithetic = np.array(antithetic, dtype=bool).flatten() if antithetic.size==1 and dim>1: antithetic = np.repeat(antithetic, dim) N = np.sum(1*np.array(antithetic)) order_,order = order,int(order*2**-N+1*(order%2!=0)) trans_ = trans trans = lambda X, verbose: \ trans_(antithetic_gen(X, antithetic)[:,:order_]) if rule=="C": X = chebyshev(dim, order) elif rule=="NC": X = chebyshev_nested(dim, order) elif rule=="G": X = _gw(order-1,dist)[0] trans = lambda x, verbose:x elif rule=="K": X = korobov(dim, order) elif rule=="R": X = np.random.random((dim,order)) elif rule=="RG": X = regular_grid(dim, order) elif rule=="NG": X = regular_grid_nested(dim, order) elif rule=="LH": X = latin_hypercube(dim, order) elif rule=="S": X = sobol(dim, order) elif rule=="H": X = halton(dim, order) elif rule=="M": X = hammersley(dim,order) else: raise KeyError, "rule not recognised" X = trans(X, verbose=verbose) return X