Exemple #1
0
def hessian(phi, R, Delta, t, N, regularized=False):
    # Make sure phi is valid
    if not all(np.isreal(phi)):
        raise ControlledError('/hessian/ phi is not real: phi = %s' % phi)
    if not all(np.isfinite(phi)):
        raise ControlledError('/hessian/ phi is not finite: phi = %s' % phi)
    # Make sure t is valid
    if not np.isreal(t):
        raise ControlledError('/hessian/ t is not real: t = %s' % t)
    if not np.isfinite(t):
        raise ControlledError('/hessian/ t is not finite: t = %s' % t)
    # Make sure regularized is valid
    if not isinstance(regularized, bool):
        raise ControlledError(
            '/hessian/ regularized must be a boolean: regularized = %s' %
            type(regularized))

    G = 1. * len(R)
    quasiQ = utils.field_to_quasiprob(phi)
    Delta_sparse = Delta.get_sparse_matrix()
    H = sp.exp(-t) * Delta_sparse + G * diags(quasiQ, 0)

    if regularized:
        H += diags(np.ones(int(G)), 0) / (N * PHI_STD_REG**2)

    # Make sure H is valid ?
    return H
Exemple #2
0
def gradient_per_datum_from_coeffs(coeffs, R, kernel, phi0=False, 
    regularized=True):
    """ For optimizer. Computes gradient from coefficients. """
    if not isinstance(phi0,np.ndarray):
        phi0 = np.zeros(R.size)
    else:
        assert all(np.isreal(phi0))

    phi = coeffs_to_field(coeffs, kernel)
    quasiQ = utils.field_to_quasiprob(phi+phi0)
    G = len(phi)

    R_row = sp.mat(R) # 1 x G
    quasiQ_row = sp.mat(quasiQ) # 1 x G
    reg_row = (1./G)*sp.mat(phi)/(PHI_STD_REG**2) # 1 x G
    kernel_mat = sp.mat(kernel) # G x kernel_dim

    mu_R_row = R_row*kernel_mat # 1 x kernel_dim
    mu_quasiQ_row = quasiQ_row*kernel_mat # 1 x kernel_dim
    mu_reg_row = reg_row*kernel_mat

    if regularized:
        grad_row = mu_R_row - mu_quasiQ_row + mu_reg_row
    else:
        grad_row = mu_R_row - mu_quasiQ_row

    return sp.array(grad_row).ravel() # Returns an array
Exemple #3
0
def hessian_per_datum_from_coeffs(coeffs,
                                  R,
                                  kernel,
                                  phi0=False,
                                  regularized=False):
    """ For optimizer. Computes hessian from coefficients. """

    # Get number of gridpoints and dimension of kernel
    G = kernel.shape[0]
    kernel_dim = kernel.shape[1]

    # Make sure coeffs is valid
    if not (len(coeffs) == kernel_dim):
        raise ControlledError(
            '/hessian_per_datum_from_coeffs/ coeffs must have length %d: len(coeffs) = %d'
            % (kernel_dim, len(coeffs)))
    if not all(np.isreal(coeffs)):
        raise ControlledError(
            '/hessian_per_datum_from_coeffs/ coeffs is not real: coeffs = %s' %
            coeffs)
    if not all(np.isfinite(coeffs)):
        raise ControlledError(
            '/hessian_per_datum_from_coeffs/ coeffs is not finite: coeffs = %s'
            % coeffs)
    # Make sure phi0 is valid
    if not isinstance(phi0, np.ndarray):
        phi0 = np.zeros(G)
    else:
        if not all(np.isreal(phi0)):
            raise ControlledError(
                '/hessian_per_datum_from_coeffs/ phi0 is not real: phi0 = %s' %
                phi0)
        if not all(np.isfinite(phi0)):
            raise ControlledError(
                '/hessian_per_datum_from_coeffs/ phi0 is not finite: phi0 = %s'
                % phi0)
    # Make sure regularized is valid
    if not isinstance(regularized, bool):
        raise ControlledError(
            '/hessian_per_datum_from_coeffs/ regularized must be a boolean: regularized = %s'
            % type(regularized))

    phi = coeffs_to_field(coeffs, kernel)
    quasiQ = utils.field_to_quasiprob(phi + phi0)

    kernel_mat = sp.mat(kernel)  # G x kernel_dim
    H = sp.mat(sp.diag(quasiQ))  # G x G

    if regularized:
        H += (1. / G) * sp.diag(np.ones(G)) / (PHI_STD_REG**2)

    hessian_mat = kernel_mat.T * H * kernel_mat  # kernel_dim x kernel_dim

    # Make sure hessian_array is valid ?

    return sp.array(hessian_mat)  # Returns an array
Exemple #4
0
def hessian(phi, R, Delta, t, regularized=False):
    G = 1. * len(R)
    quasiQ = utils.field_to_quasiprob(phi)
    Delta_sparse = Delta.get_sparse_matrix()
    H = sp.exp(-t) * Delta_sparse + G * diags(quasiQ, 0)

    if regularized:
        H += diags(np.ones(G), 0) / (PHI_STD_REG**2)

    return H
Exemple #5
0
def action(phi, R, Delta, t, N, phi_in_kernel=False, regularized=False):
    # Make sure phi is valid
    if not all(np.isreal(phi)):
        raise ControlledError('/action/ phi is not real: phi = %s' % phi)
    if not all(np.isfinite(phi)):
        raise ControlledError('/action/ phi is not finite: phi = %s' % phi)
    # Make sure t is valid
    if not np.isreal(t):
        raise ControlledError('/action/ t is not real: t = %s' % t)
    # if not np.isfinite(t):
    #    raise ControlledError('/action/ t is not finite: t = %s' % t)
    # Make sure phi_in_kernel is valid
    if not isinstance(phi_in_kernel, bool):
        raise ControlledError(
            '/action/ phi_in_kernel must be a boolean: phi_in_kernel = %s' %
            type(phi_in_kernel))
    # Make sure regularized is valid
    if not isinstance(regularized, bool):
        raise ControlledError(
            '/action/ regularized must be a boolean: regularized = %s' %
            type(regularized))

    G = 1. * len(R)
    quasiQ = utils.field_to_quasiprob(phi)
    quasiQ_col = sp.mat(quasiQ).T
    Delta_sparse = Delta.get_sparse_matrix()
    phi_col = sp.mat(phi).T
    R_col = sp.mat(R).T
    ones_col = sp.mat(sp.ones(int(G))).T

    if phi_in_kernel:
        S_mat = G * R_col.T * phi_col + G * ones_col.T * quasiQ_col
    else:
        S_mat = 0.5 * sp.exp(
            -t
        ) * phi_col.T * Delta_sparse * phi_col + G * R_col.T * phi_col + G * ones_col.T * quasiQ_col

    if regularized:
        S_mat += 0.5 * (phi_col.T * phi_col) / (N * PHI_STD_REG**2)

    S = S_mat[0, 0]

    # Make sure S is valid
    if not np.isreal(S):
        raise ControlledError('/action/ S is not real at t = %s: S = %s' %
                              (t, S))
    if not np.isfinite(S):
        raise ControlledError('/action/ S is not finite at t = %s: S = %s' %
                              (t, S))

    return S
Exemple #6
0
def gradient(phi, R, Delta, t, regularized=False):
    G = 1. * len(R)
    quasiQ = utils.field_to_quasiprob(phi)
    quasiQ_col = sp.mat(quasiQ).T
    Delta_sparse = Delta.get_sparse_matrix()
    phi_col = sp.mat(phi).T
    R_col = sp.mat(R).T
    grad_col = sp.exp(-t) * Delta_sparse * phi_col + G * R_col - G * quasiQ_col

    if regularized:
        grad_col += phi_col / (PHI_STD_REG**2)

    grad = sp.array(grad_col).ravel()

    assert all(np.isreal(grad))
    return grad
Exemple #7
0
def action_per_datum_from_coeffs(coeffs, R, kernel, phi0=False, 
    regularized=True):
    """ For optimizer. Computes action from coefficients. """
    if not isinstance(phi0,np.ndarray):
        phi0 = np.zeros(R.size)
    else:
        assert all(np.isreal(phi0))

    phi = coeffs_to_field(coeffs, kernel)
    quasiQ = utils.field_to_quasiprob(phi+phi0)
    current_term = sp.sum(R*phi)
    nonlinear_term = sp.sum(quasiQ)
    s = current_term + nonlinear_term
    if regularized:
        G = len(phi)
        s += (.5/G)*sum(phi**2)/(PHI_STD_REG**2)
    return s
Exemple #8
0
def hessian_per_datum_from_coeffs(coeffs, R, kernel, phi0=False, 
    regularized=True):
    """ For optimizer. Computes hessian from coefficients. """
    if not isinstance(phi0,np.ndarray):
        phi0 = np.zeros(R.size)
    else:
        assert all(np.isreal(phi0))

    phi = coeffs_to_field(coeffs, kernel)
    quasiQ = utils.field_to_quasiprob(phi+phi0)
    
    kernel_mat = sp.mat(kernel) # G x kernel_dim matrix
    H = sp.mat(sp.diag(quasiQ)) # G x G matrix

    if regularized:
        G = len(phi)
        H += (1./G)*sp.diag(np.ones(G))/(PHI_STD_REG**2)

    hessian_mat = kernel_mat.T*H*kernel_mat # kernel_dim^2 matrix

    return sp.array(hessian_mat) # Returns an array
Exemple #9
0
def gradient(phi, R, Delta, t, N, regularized=False):
    # Make sure phi is valid
    if not all(np.isreal(phi)):
        raise ControlledError('/gradient/ phi is not real: phi = %s' % phi)
    if not all(np.isfinite(phi)):
        raise ControlledError('/gradient/ phi is not finite: phi = %s' % phi)
    # Make sure t is valid
    if not np.isreal(t):
        raise ControlledError('/gradient/ t is not real: t = %s' % t)
    if not np.isfinite(t):
        raise ControlledError('/gradient/ t is not finite: t = %s' % t)
    # Make sure regularized is valid
    if not isinstance(regularized, bool):
        raise ControlledError(
            '/gradient/ regularized must be a boolean: regularized = %s' %
            type(regularized))

    G = 1. * len(R)
    quasiQ = utils.field_to_quasiprob(phi)
    quasiQ_col = sp.mat(quasiQ).T
    Delta_sparse = Delta.get_sparse_matrix()
    phi_col = sp.mat(phi).T
    R_col = sp.mat(R).T
    grad_col = sp.exp(-t) * Delta_sparse * phi_col + G * R_col - G * quasiQ_col

    if regularized:
        grad_col += phi_col / (N * PHI_STD_REG**2)

    grad = sp.array(grad_col).ravel()

    # Make sure grad is valid
    if not all(np.isreal(grad)):
        raise ControlledError(
            '/gradient/ grad is not real at t = %s: grad = %s' % (t, grad))
    if not all(np.isfinite(grad)):
        raise ControlledError(
            '/gradient/ grad is not finite at t = %s: grad = %s' % (t, grad))

    return grad
Exemple #10
0
def action(phi, R, Delta, t, phi_in_kernel=False, regularized=False):
    G = 1. * len(R)
    quasiQ = utils.field_to_quasiprob(phi)
    quasiQ_col = sp.mat(quasiQ).T
    Delta_sparse = Delta.get_sparse_matrix()
    phi_col = sp.mat(phi).T
    R_col = sp.mat(R).T
    ones_col = sp.mat(sp.ones(G)).T

    if phi_in_kernel:
        S_mat = G * R_col.T * phi_col + G * ones_col.T * quasiQ_col
    else:
        S_mat = 0.5*sp.exp(-t)*phi_col.T*Delta_sparse*phi_col \
           + G*R_col.T*phi_col \
           + G*ones_col.T*quasiQ_col

    if regularized:
        S_mat += 0.5 * (phi_col.T * phi_col) / (PHI_STD_REG**2)

    S = S_mat[0, 0]
    assert np.isreal(S)
    return S
Exemple #11
0
def action_per_datum_from_coeffs(coeffs,
                                 R,
                                 kernel,
                                 phi0=False,
                                 regularized=False):
    """ For optimizer. Computes action from coefficients. """

    # Get number of gridpoints and dimension of kernel
    G = kernel.shape[0]
    kernel_dim = kernel.shape[1]

    # Make sure coeffs is valid
    if not (len(coeffs) == kernel_dim):
        raise ControlledError(
            '/action_per_datum_from_coeffs/ coeffs must have length %d: len(coeffs) = %d'
            % (kernel_dim, len(coeffs)))
    if not all(np.isreal(coeffs)):
        raise ControlledError(
            '/action_per_datum_from_coeffs/ coeffs is not real: coeffs = %s' %
            coeffs)
    if not all(np.isfinite(coeffs)):
        raise ControlledError(
            '/action_per_datum_from_coeffs/ coeffs is not finite: coeffs = %s'
            % coeffs)
    # Make sure phi0 is valid
    if not isinstance(phi0, np.ndarray):
        phi0 = np.zeros(G)
    else:
        if not all(np.isreal(phi0)):
            raise ControlledError(
                '/action_per_datum_from_coeffs/ phi0 is not real: phi0 = %s' %
                phi0)
        if not all(np.isfinite(phi0)):
            raise ControlledError(
                '/action_per_datum_from_coeffs/ phi0 is not finite: phi0 = %s'
                % phi0)
    # Make sure regularized is valid
    if not isinstance(regularized, bool):
        raise ControlledError(
            '/action_per_datum_from_coeffs/ regularized must be a boolean: regularized = %s'
            % type(regularized))

    phi = coeffs_to_field(coeffs, kernel)
    quasiQ = utils.field_to_quasiprob(phi + phi0)

    current_term = sp.sum(R * phi)
    nonlinear_term = sp.sum(quasiQ)
    s = current_term + nonlinear_term

    if regularized:
        s += (.5 / G) * sum(phi**2) / (PHI_STD_REG**2)

    # Make sure s is valid
    if not np.isreal(s):
        raise ControlledError(
            '/action_per_datum_from_coeffs/ s is not real: s = %s' % s)
    if not np.isfinite(s):
        raise ControlledError(
            '/action_per_datum_from_coeffs/ s is not finite: s = %s' % s)

    return s
Exemple #12
0
def gradient_per_datum_from_coeffs(coeffs,
                                   R,
                                   kernel,
                                   phi0=False,
                                   regularized=False):
    """ For optimizer. Computes gradient from coefficients. """

    # Get number of gridpoints and dimension of kernel
    G = kernel.shape[0]
    kernel_dim = kernel.shape[1]

    # Make sure coeffs is valid
    if not (len(coeffs) == kernel_dim):
        raise ControlledError(
            '/gradient_per_datum_from_coeffs/ coeffs must have length %d: len(coeffs) = %d'
            % (kernel_dim, len(coeffs)))
    if not all(np.isreal(coeffs)):
        raise ControlledError(
            '/gradient_per_datum_from_coeffs/ coeffs is not real: coeffs = %s'
            % coeffs)
    if not all(np.isfinite(coeffs)):
        raise ControlledError(
            '/gradient_per_datum_from_coeffs/ coeffs is not finite: coeffs = %s'
            % coeffs)
    # Make sure phi0 is valid
    if not isinstance(phi0, np.ndarray):
        phi0 = np.zeros(G)
    else:
        if not all(np.isreal(phi0)):
            raise ControlledError(
                '/gradient_per_datum_from_coeffs/ phi0 is not real: phi0 = %s'
                % phi0)
        if not all(np.isfinite(phi0)):
            raise ControlledError(
                '/gradient_per_datum_from_coeffs/ phi0 is not finite: phi0 = %s'
                % phi0)
    # Make sure regularized is valid
    if not isinstance(regularized, bool):
        raise ControlledError(
            '/gradient_per_datum_from_coeffs/ regularized must be a boolean: regularized = %s'
            % type(regularized))

    phi = coeffs_to_field(coeffs, kernel)
    quasiQ = utils.field_to_quasiprob(phi + phi0)

    R_row = sp.mat(R)  # 1 x G
    quasiQ_row = sp.mat(quasiQ)  # 1 x G
    kernel_mat = sp.mat(kernel)  # G x kernel_dim

    mu_R_row = R_row * kernel_mat  # 1 x kernel_dim
    mu_quasiQ_row = quasiQ_row * kernel_mat  # 1 x kernel_dim
    grad_row = mu_R_row - mu_quasiQ_row  # 1 x kernel_dim

    if regularized:
        reg_row = (1. / G) * sp.mat(phi) / (PHI_STD_REG**2)  # 1 x G
        mu_reg_row = reg_row * kernel_mat  # 1 x kernel_dim
        grad_row += mu_reg_row  # 1 x kernel_dim

    # Make sure grad_array is valid
    grad_array = sp.array(grad_row).ravel()
    if not all(np.isreal(grad_array)):
        raise ControlledError(
            '/gradient_per_datum_from_coeffs/ grad_array is not real: grad_array = %s'
            % grad_array)
    if not all(np.isfinite(grad_array)):
        raise ControlledError(
            '/gradient_per_datum_from_coeffs/ grad_array is not finite: grad_array = %s'
            % grad_array)

    return sp.array(grad_row).ravel()  # Returns an array