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; }
/** * Computes the structure and the expected return of the tangency portfolio; */ void EfficientPortfolioWithRisklessAsset::_computeTangencyPortfolio() { Eigen::LDLT<Eigen::MatrixXd> ldlt; ldlt.compute(variance); Eigen::VectorXd one = Eigen::VectorXd::Constant(dim - 1, 1.0); Eigen::VectorXd excessReturn = mean - _risklessRate * one; _inverseTimesExcessReturn = ldlt.solve(excessReturn); _tangencyPortfolio = Eigen::VectorXd::Constant(dim, 0.0); _tangencyPortfolio.head(dim - 1) = 1.0 / (alpha - gamma * _risklessRate) * _inverseTimesExcessReturn; _tangencyPortfolioReturn = portfolioReturn(_tangencyPortfolio); }
void ctms_decompositions() { const int maxSize = 16; const int size = 12; typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, 0, maxSize, maxSize> Matrix; typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, 0, maxSize, 1> Vector; typedef Eigen::Matrix<std::complex<Scalar>, Eigen::Dynamic, Eigen::Dynamic, 0, maxSize, maxSize> ComplexMatrix; const Matrix A(Matrix::Random(size, size)); const ComplexMatrix complexA(ComplexMatrix::Random(size, size)); const Matrix saA = A.adjoint() * A; // Cholesky module Eigen::LLT<Matrix> LLT; LLT.compute(A); Eigen::LDLT<Matrix> LDLT; LDLT.compute(A); // Eigenvalues module Eigen::HessenbergDecomposition<ComplexMatrix> hessDecomp; hessDecomp.compute(complexA); Eigen::ComplexSchur<ComplexMatrix> cSchur(size); cSchur.compute(complexA); Eigen::ComplexEigenSolver<ComplexMatrix> cEigSolver; cEigSolver.compute(complexA); Eigen::EigenSolver<Matrix> eigSolver; eigSolver.compute(A); Eigen::SelfAdjointEigenSolver<Matrix> saEigSolver(size); saEigSolver.compute(saA); Eigen::Tridiagonalization<Matrix> tridiag; tridiag.compute(saA); // LU module Eigen::PartialPivLU<Matrix> ppLU; ppLU.compute(A); Eigen::FullPivLU<Matrix> fpLU; fpLU.compute(A); // QR module Eigen::HouseholderQR<Matrix> hQR; hQR.compute(A); Eigen::ColPivHouseholderQR<Matrix> cpQR; cpQR.compute(A); Eigen::FullPivHouseholderQR<Matrix> fpQR; fpQR.compute(A); // SVD module Eigen::JacobiSVD<Matrix> jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV); }
void LinearRegression::Learn(const std::vector<double>& input, const std::vector<double>& output, int dataSize) { Reset(); mInputDim = input.size() / dataSize; mOutputDim = output.size() / dataSize; Eigen::MatrixXd matA(dataSize, mInputDim + 1); Eigen::MatrixXd matB(dataSize, mOutputDim); for (int dataId = 0; dataId < dataSize; dataId++) { int inputBase = dataId * mInputDim; for (int dimId = 0; dimId < mInputDim; dimId++) { matA(dataId, dimId) = input.at(inputBase + dimId); } matA(dataId, mInputDim) = 1.0; int outputBase = dataId * mOutputDim; for (int dimId = 0; dimId < mOutputDim; dimId++) { matB(dataId, dimId) = output.at(outputBase + dimId); } } Eigen::MatrixXd matAT = matA.transpose(); Eigen::MatrixXd matCoefA = matAT * matA; Eigen::MatrixXd matCoefB = matAT * matB; Eigen::LDLT<Eigen::MatrixXd> solver; solver.compute(matCoefA); Eigen::MatrixXd matRes = solver.solve(matCoefB); //copy result mRegMat.resize((mInputDim + 1) * mOutputDim); for (int colId = 0; colId < mOutputDim; colId++) { int baseIndex = colId * (mInputDim + 1); for (int rowId = 0; rowId <= mInputDim; rowId++) { mRegMat.at(baseIndex + rowId) = matRes(rowId, colId); } } }