Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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