示例#1
0
 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);
 }
示例#2
0
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;
}
示例#3
0
 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;
 }
示例#4
0
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();
}
/**
 * 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);
}
示例#6
0
    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);
    }
示例#7
0
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);
}
示例#8
0
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);
        }
    }
}