Eigen::LDLT<Eigen::MatrixXd> CKLInference::update_init_helper() { Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); eigen_K=eigen_K+m_noise_factor*MatrixXd::Identity(m_ktrtr.num_rows, m_ktrtr.num_cols); Eigen::LDLT<Eigen::MatrixXd> ldlt; ldlt.compute(eigen_K*CMath::exp(m_log_scale*2.0)); float64_t attempt_count=0; MatrixXd Kernel_D=ldlt.vectorD(); float64_t noise_factor=m_noise_factor; while (Kernel_D.minCoeff()<=m_min_coeff_kernel) { if (m_max_attempt>0 && attempt_count>m_max_attempt) SG_ERROR("The Kernel matrix is highly non-positive definite since the min_coeff_kernel is less than %.20f", " even when adding %.20f noise to the diagonal elements at max %d attempts\n", m_min_coeff_kernel, noise_factor, m_max_attempt); attempt_count++; float64_t pre_noise_factor=noise_factor; noise_factor*=m_exp_factor; //updat the noise eigen_K=eigen_K+noise_factor*(m_exp_factor^attempt_count)*Identity() eigen_K=eigen_K+(noise_factor-pre_noise_factor)*MatrixXd::Identity(m_ktrtr.num_rows, m_ktrtr.num_cols); ldlt.compute(eigen_K*CMath::exp(m_log_scale*2.0)); Kernel_D=ldlt.vectorD(); } return ldlt; }
inline void check_pos_definite(const char* function, const char* name, const Eigen::LDLT<Derived>& cholesky) { if (cholesky.info() != Eigen::Success || !cholesky.isPositive() || !(cholesky.vectorD().array() > 0.0).all()) domain_error(function, "LDLT decomposition of", " failed", name); }
inline bool check_pos_definite(const char* function, const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y, const char* name, T_result* result) { typedef typename Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>::size_type size_type; if (y.rows() == 1 && y(0,0) <= CONSTRAINT_TOLERANCE) { std::ostringstream message; message << name << " is not positive definite. " << name << "(0,0) is %1%."; std::string msg(message.str()); return dom_err(function,y(0,0),name,msg.c_str(),"",result); } Eigen::LDLT< Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> > cholesky = y.ldlt(); if(cholesky.info() != Eigen::Success || cholesky.isNegative() || (cholesky.vectorD().array() <= CONSTRAINT_TOLERANCE).any()) { std::ostringstream message; message << name << " is not positive definite. " << name << "(0,0) is %1%."; std::string msg(message.str()); return dom_err(function,y(0,0),name,msg.c_str(),"",result); } return true; }
Eigen::Matrix<double, N, N> cholesky(Eigen::Matrix<double, N, N> x) { Eigen::LDLT<Eigen::Matrix<double, N, N> > ldlt = x.ldlt(); Eigen::Array<double, N, 1> d(ldlt.vectorD().array()); for(int i = 0; i < N; i++) { if(d[i] < 0) d[i] = 0; } Eigen::Matrix<double, N, 1> sqrtd(d.sqrt()); return ldlt.transpositionsP().transpose() * Eigen::Matrix<double, N, N>(ldlt.matrixL()) * sqrtd.asDiagonal(); }
inline void check_pos_definite(const char* function, const char* name, const Eigen::Matrix<T_y, -1, -1>& y) { check_symmetric(function, name, y); check_positive_size(function, name, "rows", y.rows()); if (y.rows() == 1 && !(y(0, 0) > CONSTRAINT_TOLERANCE)) domain_error(function, name, "is not positive definite.", ""); Eigen::LDLT<Eigen::MatrixXd> cholesky = value_of_rec(y).ldlt(); if (cholesky.info() != Eigen::Success || !cholesky.isPositive() || (cholesky.vectorD().array() <= 0.0).any()) domain_error(function, name, "is not positive definite.", ""); check_not_nan(function, name, y); }