def rejection_sampling_z(N, y, W1, W2): """ A rejection sampling method for sampling the inactive variables from a polytope. **See Also** domains.sample_z """ m, n = W1.shape s = np.dot(W1, y).reshape((m, 1)) # Build a box around z for uniform sampling qps = QPSolver() A = np.vstack((W2, -W2)) b = np.vstack((-1-s, -1+s)).reshape((2*m, 1)) lbox, ubox = np.zeros((1,m-n)), np.zeros((1,m-n)) for i in range(m-n): clb = np.zeros((m-n,1)) clb[i,0] = 1.0 lbox[0,i] = qps.linear_program_ineq(clb, A, b)[i,0] cub = np.zeros((m-n,1)) cub[i,0] = -1.0 ubox[0,i] = qps.linear_program_ineq(cub, A, b)[i,0] bn = BoundedNormalizer(lbox, ubox) Zbox = bn.unnormalize(np.random.uniform(-1.0,1.0,size=(50*N,m-n))) ind = np.all(np.dot(A, Zbox.T) >= b, axis=0) if np.sum(ind) >= N: Z = Zbox[ind,:] return Z[:N,:].reshape((N,m-n)) else: return None
def rejection_sampling_z(N, y, W1, W2): """A rejection sampling method for sampling the from a polytope. Parameters ---------- N : int the number of inactive variable samples y : ndarray the value of the active variables W1 : ndarray m-by-n matrix that contains the eigenvector bases of the n-dimensional active subspace W2 : ndarray m-by-(m-n) matrix that contains the eigenvector bases of the (m-n)-dimensional inactive subspace Returns ------- Z : ndarray N-by-(m-n) matrix that contains values of the inactive variable that correspond to the given `y` See Also -------- domains.sample_z Notes ----- The interface for this implementation is written specifically for `domains.sample_z`. """ m, n = W1.shape s = np.dot(W1, y).reshape((m, 1)) # Build a box around z for uniform sampling qps = QPSolver() A = np.vstack((W2, -W2)) b = np.vstack((-1 - s, -1 + s)).reshape((2 * m, 1)) lbox, ubox = np.zeros((1, m - n)), np.zeros((1, m - n)) for i in range(m - n): clb = np.zeros((m - n, 1)) clb[i, 0] = 1.0 lbox[0, i] = qps.linear_program_ineq(clb, A, b)[i, 0] cub = np.zeros((m - n, 1)) cub[i, 0] = -1.0 ubox[0, i] = qps.linear_program_ineq(cub, A, b)[i, 0] bn = BoundedNormalizer(lbox, ubox) Zbox = bn.unnormalize(np.random.uniform(-1.0, 1.0, size=(50 * N, m - n))) ind = np.all(np.dot(A, Zbox.T) >= b, axis=0) if np.sum(ind) >= N: Z = Zbox[ind, :] return Z[:N, :].reshape((N, m - n)) else: return None
def rejection_sampling_z(N, y, W1, W2): """A rejection sampling method for sampling the from a polytope. Parameters ---------- N : int the number of inactive variable samples y : ndarray the value of the active variables W1 : ndarray m-by-n matrix that contains the eigenvector bases of the n-dimensional active subspace W2 : ndarray m-by-(m-n) matrix that contains the eigenvector bases of the (m-n)-dimensional inactive subspace Returns ------- Z : ndarray N-by-(m-n) matrix that contains values of the inactive variable that correspond to the given `y` See Also -------- domains.sample_z Notes ----- The interface for this implementation is written specifically for `domains.sample_z`. """ m, n = W1.shape s = np.dot(W1, y).reshape((m, 1)) # Build a box around z for uniform sampling qps = QPSolver() A = np.vstack((W2, -W2)) b = np.vstack((-1-s, -1+s)).reshape((2*m, 1)) lbox, ubox = np.zeros((1,m-n)), np.zeros((1,m-n)) for i in range(m-n): clb = np.zeros((m-n,1)) clb[i,0] = 1.0 lbox[0,i] = qps.linear_program_ineq(clb, A, b)[i,0] cub = np.zeros((m-n,1)) cub[i,0] = -1.0 ubox[0,i] = qps.linear_program_ineq(cub, A, b)[i,0] bn = BoundedNormalizer(lbox, ubox) Zbox = bn.unnormalize(np.random.uniform(-1.0,1.0,size=(50*N,m-n))) ind = np.all(np.dot(A, Zbox.T) >= b, axis=0) if np.sum(ind) >= N: Z = Zbox[ind,:] return Z[:N,:].reshape((N,m-n)) else: return None
def hit_and_run_z(N, y, W1, W2): """ A hit and run method for sampling the inactive variables from a polytope. **See Also** domains.sample_z """ m, n = W1.shape # get an initial feasible point using the Chebyshev center. huge props to # David Gleich for showing me the Chebyshev center. s = np.dot(W1, y).reshape((m, 1)) normW2 = np.sqrt( np.sum( np.power(W2, 2), axis=1 ) ).reshape((m,1)) A = np.hstack(( np.vstack((W2, -W2.copy())), np.vstack((normW2, normW2.copy())) )) b = np.vstack((1-s, 1+s)).reshape((2*m, 1)) c = np.zeros((m-n+1,1)) c[-1] = -1.0 qps = QPSolver() zc = qps.linear_program_ineq(c, -A, -b) z0 = zc[:-1].reshape((m-n, 1)) # define the polytope A >= b s = np.dot(W1, y).reshape((m, 1)) A = np.vstack((W2, -W2)) b = np.vstack((-1-s, -1+s)).reshape((2*m, 1)) # tolerance ztol = 1e-6 eps0 = ztol/4.0 Z = np.zeros((N, m-n)) for i in range(N): # random direction bad_dir = True count, maxcount = 0, 50 while bad_dir: d = np.random.normal(size=(m-n,1)) bad_dir = np.any(np.dot(A, z0 + eps0*d) <= b) count += 1 if count >= maxcount: logging.getLogger(__name__).warn('There are no more directions worth pursuing in hit and run. Got {:d} samples.'.format(i)) Z[i:,:] = np.tile(z0, (1,N-i)).transpose() return Z # find constraints that impose lower and upper bounds on eps f, g = b - np.dot(A,z0), np.dot(A, d) # find an upper bound on the step min_ind = np.logical_and(g<=0, f < -np.sqrt(np.finfo(np.float).eps)) eps_max = np.amin(f[min_ind]/g[min_ind]) # find a lower bound on the step max_ind = np.logical_and(g>0, f < -np.sqrt(np.finfo(np.float).eps)) eps_min = np.amax(f[max_ind]/g[max_ind]) # randomly sample eps eps1 = np.random.uniform(eps_min, eps_max) # take a step along d z1 = z0 + eps1*d Z[i,:] = z1.reshape((m-n, )) # update temp var z0 = z1.copy() return Z
def hit_and_run_z(N, y, W1, W2): """A hit and run method for sampling the inactive variables from a polytope. Parameters ---------- N : int the number of inactive variable samples y : ndarray the value of the active variables W1 : ndarray m-by-n matrix that contains the eigenvector bases of the n-dimensional active subspace W2 : ndarray m-by-(m-n) matrix that contains the eigenvector bases of the (m-n)-dimensional inactive subspace Returns ------- Z : ndarray N-by-(m-n) matrix that contains values of the inactive variable that correspond to the given `y` See Also -------- domains.sample_z Notes ----- The interface for this implementation is written specifically for `domains.sample_z`. """ m, n = W1.shape # get an initial feasible point using the Chebyshev center. huge props to # David Gleich for showing me the Chebyshev center. s = np.dot(W1, y).reshape((m, 1)) normW2 = np.sqrt(np.sum(np.power(W2, 2), axis=1)).reshape((m, 1)) A = np.hstack((np.vstack( (W2, -W2.copy())), np.vstack((normW2, normW2.copy())))) b = np.vstack((1 - s, 1 + s)).reshape((2 * m, 1)) c = np.zeros((m - n + 1, 1)) c[-1] = -1.0 qps = QPSolver() zc = qps.linear_program_ineq(c, -A, -b) z0 = zc[:-1].reshape((m - n, 1)) # define the polytope A >= b s = np.dot(W1, y).reshape((m, 1)) A = np.vstack((W2, -W2)) b = np.vstack((-1 - s, -1 + s)).reshape((2 * m, 1)) # tolerance ztol = 1e-6 eps0 = ztol / 4.0 Z = np.zeros((N, m - n)) for i in range(N): # random direction bad_dir = True count, maxcount = 0, 50 while bad_dir: d = np.random.normal(size=(m - n, 1)) bad_dir = np.any(np.dot(A, z0 + eps0 * d) <= b) count += 1 if count >= maxcount: Z[i:, :] = np.tile(z0, (1, N - i)).transpose() return Z # find constraints that impose lower and upper bounds on eps f, g = b - np.dot(A, z0), np.dot(A, d) # find an upper bound on the step min_ind = np.logical_and(g <= 0, f < -np.sqrt(np.finfo(np.float).eps)) eps_max = np.amin(f[min_ind] / g[min_ind]) # find a lower bound on the step max_ind = np.logical_and(g > 0, f < -np.sqrt(np.finfo(np.float).eps)) eps_min = np.amax(f[max_ind] / g[max_ind]) # randomly sample eps eps1 = np.random.uniform(eps_min, eps_max) # take a step along d z1 = z0 + eps1 * d Z[i, :] = z1.reshape((m - n, )) # update temp var z0 = z1.copy() return Z
def hit_and_run_z(N, y, W1, W2): """A hit and run method for sampling the inactive variables from a polytope. Parameters ---------- N : int the number of inactive variable samples y : ndarray the value of the active variables W1 : ndarray m-by-n matrix that contains the eigenvector bases of the n-dimensional active subspace W2 : ndarray m-by-(m-n) matrix that contains the eigenvector bases of the (m-n)-dimensional inactive subspace Returns ------- Z : ndarray N-by-(m-n) matrix that contains values of the inactive variable that correspond to the given `y` See Also -------- domains.sample_z Notes ----- The interface for this implementation is written specifically for `domains.sample_z`. """ m, n = W1.shape # get an initial feasible point using the Chebyshev center. huge props to # David Gleich for showing me the Chebyshev center. s = np.dot(W1, y).reshape((m, 1)) normW2 = np.sqrt( np.sum( np.power(W2, 2), axis=1 ) ).reshape((m,1)) A = np.hstack(( np.vstack((W2, -W2.copy())), np.vstack((normW2, normW2.copy())) )) b = np.vstack((1-s, 1+s)).reshape((2*m, 1)) c = np.zeros((m-n+1,1)) c[-1] = -1.0 qps = QPSolver() zc = qps.linear_program_ineq(c, -A, -b) z0 = zc[:-1].reshape((m-n, 1)) # define the polytope A >= b s = np.dot(W1, y).reshape((m, 1)) A = np.vstack((W2, -W2)) b = np.vstack((-1-s, -1+s)).reshape((2*m, 1)) # tolerance ztol = 1e-6 eps0 = ztol/4.0 Z = np.zeros((N, m-n)) for i in range(N): # random direction bad_dir = True count, maxcount = 0, 50 while bad_dir: d = np.random.normal(size=(m-n,1)) bad_dir = np.any(np.dot(A, z0 + eps0*d) <= b) count += 1 if count >= maxcount: Z[i:,:] = np.tile(z0, (1,N-i)).transpose() return Z # find constraints that impose lower and upper bounds on eps f, g = b - np.dot(A,z0), np.dot(A, d) # find an upper bound on the step min_ind = np.logical_and(g<=0, f < -np.sqrt(np.finfo(np.float).eps)) eps_max = np.amin(f[min_ind]/g[min_ind]) # find a lower bound on the step max_ind = np.logical_and(g>0, f < -np.sqrt(np.finfo(np.float).eps)) eps_min = np.amax(f[max_ind]/g[max_ind]) # randomly sample eps eps1 = np.random.uniform(eps_min, eps_max) # take a step along d z1 = z0 + eps1*d Z[i,:] = z1.reshape((m-n, )) # update temp var z0 = z1.copy() return Z