def test_free_grads(self):
        k = 2
        mat = np.full(k**2, 0.2).reshape(k, k) + np.eye(k)

        lb = -0.1
        ub = 5.2
        val = 0.5 * (ub - lb) + lb
        vec = np.linspace(lb, ub, k)

        vp_scalar = ScalarParam('scalar', lb=lb - 0.1, ub=ub + 0.1)
        vp_mat = PosDefMatrixParam('matrix', k)
        vp_vec = VectorParam('vector', k, lb=lb - 0.1, ub=ub + 0.1)

        vp_scalar.set(val)
        vp_vec.set(vec)
        vp_mat.set(mat)

        mp = par_dict.ModelParamsDict()
        mp.push_param(vp_scalar)
        mp.push_param(vp_vec)
        mp.push_param(vp_mat)

        # To take advantage of the old quick_grad_check(), define scalar
        # functions of each parameter.  It would be nice to have an easy-to-use
        # test for Jacobians.
        def ScalarFun(val_free):
            vp_scalar_ad = copy.deepcopy(vp_scalar)
            vp_scalar_ad.set_free(val_free)
            return vp_scalar_ad.get()

        def VecFun(val_free):
            vp_vec_ad = copy.deepcopy(vp_vec)
            vp_vec_ad.set_free(val_free)
            return np.linalg.norm(vp_vec_ad.get())

        def MatFun(val_free):
            vp_mat_ad = copy.deepcopy(vp_mat)
            vp_mat_ad.set_free(val_free)
            return np.linalg.norm(vp_mat_ad.get())

        def ParamsFun(val_free):
            mp_ad = copy.deepcopy(mp)
            mp_ad.set_free(val_free)
            return mp_ad['scalar'].get() + \
                   np.linalg.norm(mp_ad['vector'].get()) + \
                   np.linalg.norm(mp_ad['matrix'].get())

        check_grads(ScalarFun, modes=['rev'], order=2)(vp_scalar.get_free())
        check_grads(VecFun, modes=['rev'], order=2)(vp_vec.get_free())
        check_grads(MatFun, modes=['rev'], order=2)(vp_mat.get_free())
        check_grads(ParamsFun, modes=['rev'], order=2)(mp.get_free())
    def test_LDMatrixParamDerivatives(self):
        # Test the LD matrix extra carefully since we define our own
        # autograd derivatives.
        k = 2
        mat = np.full(k**2, 0.2).reshape(k, k) + np.eye(k)
        diag_lb = 0.5
        vp = PosDefMatrixParam('test', k, diag_lb=diag_lb)
        vp.set(mat)
        mat_free = vp.get_free()

        def MatFun(mat_free):
            vp_ad = copy.deepcopy(vp)
            vp_ad.set_free(mat_free)
            return vp_ad.get()

        MatFunJac = jacobian(MatFun)
        MatFunHess = hessian(MatFun)

        # Test the jacobian
        eps = 1e-4
        for ind in range(len(mat_free)):
            mat_free_eps = copy.deepcopy(mat_free)
            mat_free_eps[ind] += eps
            num_grad = MatFun(mat_free_eps) - MatFun(mat_free)
            np_test.assert_array_almost_equal(
                num_grad,
                eps * MatFunJac(mat_free)[:, :, ind])

        # Test the hessian
        eps = 1e-4
        for ind1 in range(len(mat_free)):
            for ind2 in range(len(mat_free)):

                eps1_vec = np.zeros_like(mat_free)
                eps2_vec = np.zeros_like(mat_free)
                eps1_vec[ind1] = eps
                eps2_vec[ind2] = eps

                num_hess = MatFun(mat_free + eps1_vec + eps2_vec) - \
                           MatFun(mat_free + eps2_vec) - \
                           (MatFun(mat_free + eps1_vec) - MatFun(mat_free))
                np_test.assert_array_almost_equal(
                    num_hess,
                    (eps**2) * MatFunHess(mat_free)[:, :, ind1, ind2])
    def test_sparse_free_hessians(self):
        k = 2

        mat = np.full(k**2, 0.2).reshape(k, k) + np.eye(k)
        vp_array = ArrayParam('array', shape=(4, 5, 7))
        vp_mat = PosDefMatrixParam('mat', k, val=mat)
        vp_simplex = SimplexParam('simplex', shape=(5, 3))

        mp = par_dict.ModelParamsDict()
        mp.push_param(vp_mat)
        mp.push_param(vp_simplex)
        mp.push_param(vp_array)

        def model(mp):
            mat = mp['mat'].get()
            array = mp['array'].get()
            simplex = mp['simplex'].get()

            return np.sum(mat)**2 * np.sum(array)**2 * np.sum(simplex)**2

        def model_wrap_free(free_param, mp):
            mp.set_free(free_param)
            return model_wrap_vec(mp.get_vector(), mp)

        def model_wrap_vec(vec_param, mp):
            mp.set_vector(vec_param)
            return model(mp)

        free_vec = np.random.random(mp.free_size())
        mp.set_free(free_vec)
        mp_vec = mp.get_vector()

        model_wrap_vec_jac = jacobian(model_wrap_vec)
        model_wrap_free_hess = hessian(model_wrap_free)
        model_wrap_vec_hess = hessian(model_wrap_vec)

        vec_jac_model = model_wrap_vec_jac(mp_vec, mp)
        vec_hess_model = model_wrap_vec_hess(mp_vec, mp)
        free_hess_model = model_wrap_free_hess(free_vec, mp)

        mp.set_free(free_vec)
        free_hess_sparse = Parameters.convert_vector_to_free_hessian(
            mp, free_vec, vec_jac_model, vec_hess_model)

        np_test.assert_array_almost_equal(free_hess_model, free_hess_sparse)
    def test_model_arams_dict_values(self):
        k = 3
        mp = par_dict.ModelParamsDict(name='ModelParamsDict')
        mp.push_param(ScalarParam('scalar', lb=0))
        mp.push_param(PosDefMatrixParam('matrix', k))

        mp['scalar'].set(0.3)
        mp['matrix'].set(3 * np.eye(k))

        np_test.assert_array_almost_equal(mp['scalar'].get(),
                                          mp.values['scalar'])
        np_test.assert_array_almost_equal(mp['matrix'].get(),
                                          mp.values['matrix'])

        mp.values['scalar'] = 0.4
        mp.values['matrix'] = 4 * np.eye(k)

        np_test.assert_array_almost_equal(mp['scalar'].get(),
                                          mp.values['scalar'])
        np_test.assert_array_almost_equal(mp['matrix'].get(),
                                          mp.values['matrix'])
    def test_vector_grads(self):
        k = 2
        mat = np.full(k**2, 0.2).reshape(k, k) + np.eye(k)

        lb = -0.1
        ub = 5.2
        val = 0.5 * (ub - lb) + lb
        vec = np.linspace(lb, ub, k)

        vp_scalar = ScalarParam('scalar', lb=lb - 0.1, ub=ub + 0.1)
        vp_mat = PosDefMatrixParam('matrix', k)
        vp_vec = VectorParam('vector', k, lb=lb - 0.1, ub=ub + 0.1)

        vp_scalar.set(val)
        vp_vec.set(vec)
        vp_mat.set(mat)

        mp = par_dict.ModelParamsDict()
        mp.push_param(vp_scalar)
        mp.push_param(vp_vec)
        mp.push_param(vp_mat)

        # To take advantage of the old quick_grad_check(), define scalar
        # functions of each parameter.  It would be nice to have an easy-to-use
        # test for Jacobians.
        def ScalarFun(val_vec):
            vp_scalar_ad = copy.deepcopy(vp_scalar)
            vp_scalar_ad.set_vector(val_vec)
            return vp_scalar_ad.get()

        def VecFun(val_vec):
            vp_vec_ad = copy.deepcopy(vp_vec)
            vp_vec_ad.set_vector(val_vec)
            return np.linalg.norm(vp_vec_ad.get())

        def MatFun(val_vec):
            vp_mat_ad = copy.deepcopy(vp_mat)
            vp_mat_ad.set_vector(val_vec)
            return np.linalg.norm(vp_mat_ad.get())

        def ParamsFun(val_vec):
            mp_ad = copy.deepcopy(mp)
            mp_ad.set_vector(val_vec)
            return mp_ad['scalar'].get() + \
                   np.linalg.norm(mp_ad['vector'].get()) + \
                   np.linalg.norm(mp_ad['matrix'].get())

        # TODO: you could probably use the new functionality of check_grads
        # to do this more easily.

        par_vec = vp_scalar.get_vector()
        par_vec = np.array([float(x) for x in par_vec])
        #        check_grads(ScalarFun)(par_vec)

        par_vec = vp_vec.get_vector()
        par_vec = np.array([float(x) for x in par_vec])
        #        check_grads(VecFun)(par_vec)

        par_vec = vp_mat.get_vector()
        par_vec = np.array([float(x) for x in par_vec])
        #        check_grads(MatFun, par_vec)

        par_vec = mp.get_vector()
        par_vec = np.array([float(x) for x in par_vec])
    def test_model_params_dict(self):
        k = 2
        mat = np.full(k**2, 0.2).reshape(k, k) + np.eye(k)

        def clear_model_params(mp):
            mp['scalar'].set(0.)
            mp['vector'].set(np.full(k, 0.))
            mp['matrix'].set(np.full((k, k), 0.))
            mp['simplex'].set(np.full(simp.shape, 1. / np.prod(simp.shape)))

        lb = -0.1
        ub = 5.2
        val = 0.5 * (ub - lb) + lb
        vec = np.linspace(lb, ub, k)
        simp = np.linspace(2., 10., k * 2).reshape(k, 2)
        simp = simp / np.expand_dims(np.sum(simp, 1), axis=1)
        vp_scalar = ScalarParam('scalar', lb=lb - 0.1, ub=ub + 0.1)
        vp_mat = PosDefMatrixParam('matrix', k)
        vp_vec = VectorParam('vector', k, lb=lb - 0.1, ub=ub + 0.1)
        vp_simp = SimplexParam('simplex', shape=simp.shape)

        mp = par_dict.ModelParamsDict(name='ModelParamsDict')
        mp.push_param(vp_scalar)
        mp.push_param(vp_vec)
        mp.push_param(vp_mat)
        mp.push_param(vp_simp)

        execute_required_methods(self, mp, test_autograd=False)

        param_names = ['scalar', 'vector', 'matrix', 'simplex']
        param_vals = \
            { 'scalar': val, 'vector': vec, 'matrix': mat, 'simplex': simp }

        for param in param_names:
            mp[param].set(param_vals[param])
        for param in param_names:
            np_test.assert_array_almost_equal(param_vals[param],
                                              mp[param].get())

        free_vec = mp.get_free()
        clear_model_params(mp)
        mp.set_free(free_vec)
        for param in param_names:
            np_test.assert_array_almost_equal(param_vals[param],
                                              mp[param].get())
        self.assertEqual(len(free_vec), mp.free_size())

        param_vec = mp.get_vector()
        clear_model_params(mp)
        mp.set_vector(param_vec)
        for param in param_names:
            np_test.assert_array_almost_equal(param_vals[param],
                                              mp[param].get())
        self.assertEqual(len(param_vec), mp.vector_size())

        # Check the index dictionaries.
        free_vec = mp.get_free()
        mp.set_free(free_vec)
        for param_id in range(len(param_names)):
            param = param_names[param_id]
            free_vec_peturb = mp.get_free()
            free_vec_peturb[mp.free_indices_dict[param]] += 1.0
            mp.set_free(free_vec_peturb)

            # Check that only the parameter we're looking at has changed.
            self.assertTrue(
                np.max(np.abs(mp[param].get() - param_vals[param]) > 1e-6))
            for unchanged_param in set(param_names) - set([param]):
                np_test.assert_array_almost_equal(param_vals[unchanged_param],
                                                  mp[unchanged_param].get())
            mp.set_free(free_vec)

        param_vec = mp.get_vector()
        mp.set_vector(param_vec)
        for param_id in range(len(param_names)):
            param = param_names[param_id]
            param_vec_peturb = mp.get_vector()
            param_vec_peturb[mp.vector_indices_dict[param]] += 0.1
            mp.set_vector(param_vec_peturb)

            # Check that only the parameter we're looking at has changed.
            self.assertTrue(
                np.max(np.abs(mp[param].get() - param_vals[param]) > 1e-6))
            for unchanged_param in set(param_names) - set([param]):
                np_test.assert_array_almost_equal(param_vals[unchanged_param],
                                                  mp[unchanged_param].get())
            mp.set_vector(param_vec)

        # Check the sparse transforms.  Note that the Hessian of this test
        # gives the autograd warning that the output seems to be independent
        # of the input.
        check_sparse_transforms(self, mp)
    def test_pos_def_matrix_param(self):
        k = 2
        mat = np.full(k**2, 0.2).reshape(k, k) + np.eye(k)

        # not symmetric
        bad_mat = copy.deepcopy(mat)
        bad_mat[1, 0] += +1

        vp = PosDefMatrixParam('test', k)

        # Check setting.
        vp_init = PosDefMatrixParam('test', k, val=mat)
        np_test.assert_array_almost_equal(mat, vp_init.get())

        self.assertRaises(ValueError, vp.set, bad_mat)
        self.assertRaises(ValueError, vp.set, np.eye(k + 1))
        vp.set(mat)

        # Check size.
        self.assertEqual(k, vp.size())
        self.assertEqual(k * (k + 1) / 2, vp.free_size())

        # Check getting and free parameters.
        np_test.assert_array_almost_equal(mat, vp.get())
        mat_free = vp.get_free()
        vp.set(np.full((k, k), 0.))
        vp.set_free(mat_free)
        np_test.assert_array_almost_equal(mat, vp.get())

        mat_vectorized = vp.get_vector()
        vp.set(np.full((k, k), 0.))
        vp.set_vector(mat_vectorized)
        np_test.assert_array_almost_equal(mat, vp.get())
 def test_pos_def_matrix(self):
     single_mat = np.diag([1.0, 2.0]) + np.full((2, 2), 0.1)
     execute_required_methods(self,
                              PosDefMatrixParam(val=single_mat),
                              test_autograd=True,
                              test_sparse_transform=True)