Exemplo n.º 1
0
def hes_fixed_obj():
    import cppad_py
    import numpy
    ok = True
    #
    n_fixed = 5
    n_random = 0
    #
    # value of theta and we will record fix_likelihood( theta )
    theta = numpy.ones(n_fixed, dtype=float)
    #
    # independent variables during the recording
    atheta = cppad_py.independent(theta)
    #
    a_sum = 0.0
    for i in range(n_fixed):
        j = (i + 1) % n_fixed
        a_sum += (i + j) * atheta[i] * atheta[j]
    #
    av = numpy.array([a_sum], dtype=cppad_py.a_double)
    f = cppad_py.d_fun(atheta, av)
    #
    # mixed_obj
    empty = numpy.array([], dtype=float)
    mixed_obj = cppad_py.mixed(
        fixed_init=theta,
        random_init=empty,
        fix_likelihood=f,
    )
    #
    # hes_fixed_obj_rcv
    hes_fixed_obj_rcv = cppad_py.sparse_rcv()
    mixed_obj.hes_fixed_obj(
        hes_fixed_obj_rcv,
        fixed_vec=theta,
        random_opt=empty,
    )
    # check lower triangle of the Hessian
    ok = ok and hes_fixed_obj_rcv.nr() == n_fixed
    ok = ok and hes_fixed_obj_rcv.nc() == n_fixed
    ok = ok and hes_fixed_obj_rcv.nnz() == n_fixed
    row = hes_fixed_obj_rcv.row()
    col = hes_fixed_obj_rcv.col()
    val = hes_fixed_obj_rcv.val()
    for k in range(hes_fixed_obj_rcv.nnz()):
        i = row[k]
        j = col[k]
        ok = ok and j < i
        ok = ok and ((j + 1 == i) or (j == 0 and i == n_fixed - 1))
        ok = ok and val[k] == (i + j)
    return ok
Exemplo n.º 2
0
def sparse_rcv_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # create an empty matrix
    matrix = cppad_py.sparse_rcv()
    ok = ok and matrix.nr() == 0
    ok = ok and matrix.nc() == 0
    ok = ok and matrix.nnz() == 0
    #
    # create sparsity pattern for n by n identity matrix
    pattern = cppad_py.sparse_rc()
    n = 5
    pattern.resize(n, n, n)
    for k in range(n):
        pattern.put(k, k, k)
    #
    # create n by n sparse representation of identity matrix
    matrix.pat(pattern)
    for k in range(n):
        matrix.put(k, 1.0)
    #
    # row, column indices
    row = matrix.row()
    col = matrix.col()
    val = matrix.val()
    #
    # check results
    for k in range(n):
        ok = ok and row[k] == k
        ok = ok and col[k] == k
        ok = ok and val[k] == 1.0
    #
    # For this case, row_major and col_major order are same as original order
    row_major = matrix.row_major()
    col_major = matrix.col_major()
    for k in range(n):
        ok = ok and row_major[k] == k
        ok = ok and col_major[k] == k
    #
    #
    return (ok)
Exemplo n.º 3
0
def sparse_jac_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # number of dependent and independent variables
    n = 3
    # one
    aone = cppad_py.a_double(1.0)
    #
    # create the independent variables ax
    x = numpy.empty(n, dtype=float)
    for i in range(n):
        x[i] = i + 2.0
    #
    ax = cppad_py.independent(x)
    #
    # create dependent variables ay with ay[i] = (j+1) * ax[j]
    # where i = mod(j + 1, n)
    ay = numpy.empty(n, dtype=cppad_py.a_double)
    for j in range(n):
        i = j + 1
        if i >= n:
            i = i - n
        #
        aj = cppad_py.a_double(j)
        ay_i = (aj + aone) * ax[j]
        ay[i] = ay_i
    #
    #
    # define af corresponding to f(x)
    f = cppad_py.d_fun(ax, ay)
    #
    # sparsity pattern for identity matrix
    pat_eye = cppad_py.sparse_rc()
    pat_eye.resize(n, n, n)
    for k in range(n):
        pat_eye.put(k, k, k)
    #
    #
    # sparsity pattern for the Jacobian
    pat_jac = cppad_py.sparse_rc()
    f.for_jac_sparsity(pat_eye, pat_jac)
    #
    # loop over forward and reverse mode
    for mode in range(2):
        # compute all possibly non-zero entries in Jacobian
        subset = cppad_py.sparse_rcv(pat_jac)
        # work space used to save time for multiple calls
        work = cppad_py.sparse_jac_work()
        if mode == 0:
            f.sparse_jac_for(subset, x, pat_jac, work)
        #
        if mode == 1:
            f.sparse_jac_rev(subset, x, pat_jac, work)
        #
        #
        # check result
        ok = ok and n == subset.nnz()
        col_major = subset.col_major()
        row = subset.row()
        col = subset.col()
        val = subset.val()
        for k in range(n):
            ell = col_major[k]
            r = row[ell]
            c = col[ell]
            v = val[ell]
            i = c + 1
            if i >= n:
                i = i - n
            #
            ok = ok and c == k
            ok = ok and r == i
            ok = ok and v == c + 1.0
        #
    #
    #
    return (ok)
Exemplo n.º 4
0
def sparse_hes_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # number of dependent and independent variables
    n = 3
    #
    # create the independent variables ax
    x = numpy.empty(n, dtype=float)
    for i in range(n):
        x[i] = i + 2.0
    #
    ax = cppad_py.independent(x)
    #
    # ay[i] = j * ax[j] * ax[i]
    # where i = mod(j + 1, n)
    ay = numpy.empty(n, dtype=cppad_py.a_double)
    for j in range(n):
        i = j + 1
        if i >= n:
            i = i - n
        #
        aj = cppad_py.a_double(j)
        ax_j = ax[j]
        ax_i = ax[i]
        ay[i] = aj * ax_j * ax_i
    #
    #
    # define af corresponding to f(x)
    f = cppad_py.d_fun(ax, ay)
    #
    # Set select_d (domain) to all true,
    # initial select_r (range) to all false
    # initialize r to all zeros
    select_d = numpy.empty(n, dtype=bool)
    select_r = numpy.empty(n, dtype=bool)
    r = numpy.empty(n, dtype=float)
    for i in range(n):
        select_d[i] = True
        select_r[i] = False
        r[i] = 0.0
    #
    #
    # only select component n-1 of the range function
    # f_0 (x) = (n-1) * x_{n-1} * x_0
    select_r[0] = True
    r[0] = 1.0
    #
    # sparisty pattern for Hessian
    pattern = cppad_py.sparse_rc()
    f.for_hes_sparsity(select_d, select_r, pattern)
    #
    # compute all possibly non-zero entries in Hessian
    # (should only compute lower triangle becuase matrix is symmetric)
    subset = cppad_py.sparse_rcv(pattern)
    #
    # work space used to save time for multiple calls
    work = cppad_py.sparse_hes_work()
    #
    f.sparse_hes(subset, x, r, pattern, work)
    #
    # check that result is sparsity pattern for Hessian of f_0 (x)
    ok = ok and subset.nnz() == 2
    row = subset.row()
    col = subset.col()
    val = subset.val()
    for k in range(2):
        i = row[k]
        j = col[k]
        v = val[k]
        if i <= j:
            ok = ok and i == 0
            ok = ok and j == n - 1
        #
        if i >= j:
            ok = ok and i == n - 1
            ok = ok and j == 0
        #
        ok = ok and v == n - 1
    #
    #
    return (ok)
Exemplo n.º 5
0
    def __init__(
        self,
        # BEGIN_MIXED_CTOR
        # mixed_obj = cppad_py.mixed(
        fixed_init=None,
        random_init=None,
        quasi_fixed=False,
        bool_sparsity=False,
        A_rcv=None,
        warning=None,
        fix_likelihood=None,
        fix_constraint=None,
        ran_likelihood=None,
        # )
        # END_MIXED_CTOR
    ):
        def numpy2std(vec, name):
            # numpy2vec will check the length and report errors
            dtype = float
            shape = len(vec)
            context = 'cppad_py.mixed: ' + name
            vec = cppad_py.utility.numpy2vec(vec, dtype, shape, context, name)
            return vec

        #
        def ignore_warning():
            return

        #
        if fixed_init is None:
            raise RuntimeError('cppad_py.mixed: fixed_init is None')
        if random_init is None:
            random_init = numpy.array([], dtype='float')
        #
        self.n_fixed = len(fixed_init)
        self.n_random = len(random_init)
        self.fixed_init = fixed_init
        self.random_init = random_init
        self.A_rcv = A_rcv
        if fix_constraint is None:
            self.n_fix_con = 0
        else:
            self.n_fix_con = fix_constraint.size_range()
        #
        fixed_init = numpy2std(fixed_init, 'fixed_init')
        random_init = numpy2std(random_init, 'random_init')
        #
        if self.n_fixed == 0:
            raise RuntimeError('cppad_py.mixed: n_fixed is zero')
        if self.n_random < 0:
            raise RuntimeError('cppad_py.mixed: n_random is less than zero')
        if A_rcv is None:
            A_rcv = cppad_py.sparse_rcv()
            self.A_rcv = A_rcv
        if warning is None:
            warning = ignore_warning
        if fix_likelihood is None:
            fix_likelihood = cppad_py.d_fun()
        if fix_constraint is None:
            fix_constraint = cppad_py.d_fun()
        if ran_likelihood is None:
            ran_likelihood = cppad_py.d_fun()
        #
        self.obj = cppad_py.cppad_swig.mixed(
            fixed_init,
            random_init,
            quasi_fixed,
            bool_sparsity,
            A_rcv.rcv,
            warning,
            fix_likelihood.f,
            fix_constraint.f,
            ran_likelihood.f,
        )
Exemplo n.º 6
0
def hes_fixed_obj_xam():
    import cppad_py
    import numpy
    ok = True
    #
    y_bar = 1.0  # mean of the data y
    y = 2.0  # data that depends on random effects
    sigma = 0.5  # standard deviation for the random effects
    #
    # value of theta and u at which we will record ran_likelihood( theta , u)
    theta_u = numpy.array([sigma * sigma, 0.0], dtype=float)
    #
    # independent variables during the recording
    atheta_u = cppad_py.independent(theta_u)
    #
    # split out theta and u
    atheta = atheta_u[0]
    au = atheta_u[1]
    #
    # - log[ p(y|theta,u) ] (dropping terms that are constant w.r.t. theta,u)
    atmp = (y - y_bar - au)
    ap_y_theta_u = 0.5 * atmp * atmp / atheta
    ap_y_theta_u += 0.5 * numpy.log(atheta)
    #
    # - log[ p(u|theta) ] (dropping terms that are constant w.r.t. theta,u)
    atmp = au / sigma
    ap_u_theta = 0.5 * atmp * atmp
    #
    # - log[ p(y|theta, u) p(u|theta) ]
    av_0 = ap_y_theta_u + ap_u_theta
    #
    # function mapping (theta,u) -> v
    av = numpy.array([av_0])
    r = cppad_py.d_fun(atheta_u, av)
    #
    # mixed_obj
    theta = theta_u[0:1]
    u = theta_u[1:2]
    mixed_obj = cppad_py.mixed(
        fixed_init=theta,
        random_init=u,
        ran_likelihood=r,
    )
    #
    # optimal random effects
    options = 'String  sb    yes\n'  # suppress optimizer banner
    options += 'Integer print_level 0\n'  # suppress optimizer trace
    random_opt = mixed_obj.optimize_random(
        random_ipopt_options=options,
        fixed_vec=theta,
    )
    #
    # hes_fixed_obj_rcv
    hes_fixed_obj_rcv = cppad_py.sparse_rcv()
    mixed_obj.hes_fixed_obj(
        hes_fixed_obj_rcv,
        fixed_vec=theta,
        random_opt=random_opt,
    )
    #
    theta = theta[0]
    term1 = (theta + sigma * sigma)
    term2 = (y - y_bar) * (y - y_bar)
    check = term2 / (term1 * term1 * term1) - 0.5 / (term1 * term1)
    #
    # check solution
    ok = ok and hes_fixed_obj_rcv.nr() == 1
    ok = ok and hes_fixed_obj_rcv.nc() == 1
    ok = ok and hes_fixed_obj_rcv.nnz() == 1
    ok = ok and hes_fixed_obj_rcv.row()[0] == 0
    ok = ok and hes_fixed_obj_rcv.col()[0] == 0
    ok = ok and abs(hes_fixed_obj_rcv.val()[0] / check - 1.0) < 1e-8
    return ok
Exemplo n.º 7
0
def hes_random_obj_xam():
    import cppad_py
    import numpy
    ok = True
    #
    n_fixed = 5
    n_random = n_fixed
    #
    # value of theta and u at which we will record ran_likelihood( theta , u)
    theta_u = numpy.ones(n_fixed + n_random, dtype=float)
    for i in range(n_fixed):
        theta_u[i] = float(i + 1)
    #
    # independent variables during the recording
    atheta_u = cppad_py.independent(theta_u)
    #
    # split out theta and u
    atheta = atheta_u[0:n_fixed]
    au = atheta_u[n_fixed:n_fixed + n_random]
    #
    # - log[ p(u|theta) ] (dropping terms that are constant w.r.t. theta,u)
    asum = 0.0
    for i in range(n_fixed):
        ip = (i + 1) % n_fixed
        asum += atheta[i] * au[i] * au[ip]
    #
    # function mapping (theta,u) -> sum
    av = numpy.array([asum])
    r = cppad_py.d_fun(atheta_u, av)
    #
    # mixed_obj
    theta = theta_u[0:n_fixed]
    u = theta_u[n_fixed:n_fixed + n_random]
    mixed_obj = cppad_py.mixed(
        fixed_init=theta,
        random_init=u,
        ran_likelihood=r,
    )
    #
    # hes_random_obj_rcv
    hes_random_obj_rcv = cppad_py.sparse_rcv()
    mixed_obj.hes_random_obj(
        hes_random_obj_rcv,
        fixed_vec=theta,
        random_vec=u,
    )
    #
    # check lower triangle of the Hessian
    ok = ok and hes_random_obj_rcv.nr() == n_random
    ok = ok and hes_random_obj_rcv.nc() == n_random
    ok = ok and hes_random_obj_rcv.nnz() == n_random
    row = hes_random_obj_rcv.row()
    col = hes_random_obj_rcv.col()
    val = hes_random_obj_rcv.val()
    for k in range(hes_random_obj_rcv.nnz()):
        i = row[k]
        j = col[k]
        ok = ok and j < i
        if j + 1 == i:
            ok = ok and val[k] == theta[(i - 1) % n_random]
        elif j == 0 and i == n_random - 1:
            ok = ok and val[k] == theta[i]
        else:
            ok = False
    return ok