Ejemplo n.º 1
0
void eigenTest()
{
    e::Matrix<double, 3, 3> Q;
    e::Matrix<double, 3, 3> corrMatrix;
    corrMatrix(0, 0) = 17.25905;
    corrMatrix(0, 1) = -23.88775;
    corrMatrix(0, 2) = 6.448684;
    corrMatrix(1, 0) = -23.88775;
    corrMatrix(1, 1) = 37.74094;
    corrMatrix(1, 2) = -0.7966833;
    corrMatrix(2, 0) = 6.448684;
    corrMatrix(2, 1) = -0.7966833;
    corrMatrix(2, 2) = 17.4851;
    std::cout << "UNITTEST" << std::endl;
    std::cout << "CorrMatrix: " << std::endl;
    std::cout << corrMatrix << std::endl;
    std::cout << "******" << std::endl;

    std::vector<double> eigenValues(3);
    //jacobi_sweep(corrMatrix, Q, eigenValues);
    eigenSolver(corrMatrix, Q, eigenValues);


    std::cout << "EigenValues: " << eigenValues[0] << " " << eigenValues[1] << " " << eigenValues[2] << std::endl;
    std::cout << "EigenVec1:" << Q(0, 0) << " " << Q(1, 0) << " " << Q(2, 0) << std::endl;
    std::cout << "EigenVec2:" << Q(0, 1) << " " << Q(1, 1) << " " << Q(2, 1) << std::endl;
    std::cout << "EigenVec3:" << Q(0, 2) << " " << Q(1, 2) << " " << Q(2, 2) << std::endl;
}
void qlProcessFactory::makeMatrix(boost::shared_ptr<FpmlSerialized::Excel_correlationInfo_para> e_corrPara)
{
	Size dimension = e_corrPara->getDimension()->IValue();

	QuantLib::Matrix corrMatrix(dimension,dimension,1.0);
	std::vector<boost::shared_ptr<FpmlSerialized::Excel_correlation_para>> xml_corrListpara
		= e_corrPara->getExcel_correlation_para();

	Size chechCount = 0;

	for( Size row = 0 ; row < dimension ; ++row )
	{
		for( Size col = 0 ; col < dimension ; ++col )
		{
			for ( Size i = 0 ; i < xml_corrListpara.size() ; ++ i )
			{
				if ( boost::to_upper_copy(xml_corrListpara[i]->getFirst()->SValue()) == boost::to_upper_copy(underListCode_[row]) &&
					 boost::to_upper_copy(xml_corrListpara[i]->getSecond()->SValue()) == boost::to_upper_copy(underListCode_[col]) )				
				{
					corrMatrix[row][col] = xml_corrListpara[i]->getValue()->DValue();
					chechCount += 1;
				}
			}
		}
	}

	QL_REQUIRE(chechCount == dimension * dimension , "matrix chechsum error, chesumNum : " << chechCount );

	/*if ( chechCount != dimension * dimension )
	{
		this->matrix_ = QuantLib::Matrix();
	}*/


		for ( Size col=0 ;col<dimension; col++)	
		{
			for ( Size row=0 ;row<dimension; row++)
			{
				std::cout << " " <<	corrMatrix[row][col];
			}
			std::cout << std::endl;
		}

	this->matrix_ = corrMatrix;
			
}
Ejemplo n.º 3
0
void umdFitLine(int lineCount, e::Vector2d **points, Line &l)
{
    e::Vector2d mean(0, 0);
    for(int i = 0; i < lineCount; ++i)
    {
        mean += *(points[i]);
    }
    mean = mean*(1.0/lineCount);

    //move it to mean;
    for(int i = 0; i < lineCount; ++i)
    {
        *(points[i]) -= mean;
    }

    //first compute the correlation matrix
    //should be row major
    e::Matrix<double, 2, 2> corrMatrix;
    for(int row = 0; row < 2; row++)
    {
        for(int col = 0; col < 2; col++)
        {
            //normal matrix multiplication m * m^T, this could be probably optimized since it's symmetrical
            double sum = 0;
            for(int i = 0; i < lineCount; i++)
            {
                sum += (*(points[i]))[row]* (*(points[i]))[col];
            }
            corrMatrix(row, col) = sum;
        }
    }


    e::Matrix<double, 2, 2> V;

    std::vector<double> eigenValues(2);
    eigenSolver(corrMatrix, V, eigenValues);

    //we want the normal
    l.a = V(1, 0);
    l.b = V(1, 1); //the second eigenvector - it is sorted for 2x2
    //compute mean

    l.c = -l.a*mean[0] - l.b*mean[1];
}
Ejemplo n.º 4
0
void umdFitHyperplane(int lineCount, e::Vector3d **lines, e::Hyperplane<double, 3> &hyperplane)
{
	//first compute the correlation matrix
	//should be row major
    e::Matrix<double, 3, 3> corrMatrix;
    //std::cout << "CORRELATION*******************" << std::endl;
	for(int row = 0; row < 3; row++)
	{
		for(int col = 0; col < 3; col++)
		{
			//normal matrix multiplication m * m^T, this could be probably optimized since it's symmetrical
            double sum = 0;
			for(int i = 0; i < lineCount; i++)
			{
				sum += (*(lines[i]))[row]* (*(lines[i]))[col];
			}
			corrMatrix(row, col) = sum;
            //std::cout << sum << " ";
		}
       //std::cout << std::endl;
	}
    //std::cout << "CORRELATION*******************" << std::endl;



    e::Matrix<double, 3, 3> D;
    e::Matrix<double, 3, 3> Q;
	//std::cout << "correlation matrix: " << corrMatrix << std::endl;
	//std::cout << "**********" << std::endl;
	/*
	diagonalizer(corrMatrix, Q, D); //this does basically the eigen decomposition, the quaternions rot matrix should give the eigenVectors

	//get the eigenvalues A = Q * D * QT
    std::vector<double> eigenValues(3);
    eigenValues[0] = (const double)(D(0,0));
    eigenValues[1] = (const double)(D(1,1));
    eigenValues[2] = (const double)(D(2,2));

	//find the smallest
	int k = (int)(std::min_element(eigenValues.begin(), eigenValues.end()) - eigenValues.begin());

	//get the k's row for our hyperplane normal
	hyperplane.coeffs().data()[0] = Q(k , 0);
	hyperplane.coeffs().data()[1] = Q(k, 1);
	hyperplane.coeffs().data()[2] = Q(k, 2);
	hyperplane.coeffs().data()[3] = 0; //non linear part - should go through 0s
	std::cout << "Coeffs: " << Q(k, 0) << " " << Q(k, 1) << " " << Q(k, 2) << std::endl;
	std::cout << "Q: " << Q << std::endl;
	std::cout << "******" << std::endl;
	std::cout << "D: " << D << std::endl;
	std::cout << "******" << std::endl;
	*/

    std::vector<double> eigenValues(3);
    //jacobi_sweep(corrMatrix, Q, eigenValues);
    eigenSolver(corrMatrix, Q, eigenValues);

    //eigenTest();

    //e::SelfAdjointEigenSolver< e::Matrix<double, 3, 3> > slvr(corrMatrix);
    //e::Vector3d ev = slvr.eigenvalues();
    //Q = slvr.eigenvectors();
    //eigenValues[0] = ev[0];
    //eigenValues[1] = ev[1];
    //eigenValues[2] = ev[2];

	//std::cout << "Q: " << Q << std::endl;
	//std::cout << "******" << std::endl;


	//find the smallest
	int k = (int)(std::min_element(eigenValues.begin(), eigenValues.end()) - eigenValues.begin());

	hyperplane.coeffs().data()[0] = Q(0, k);
	hyperplane.coeffs().data()[1] = Q(1, k);
	hyperplane.coeffs().data()[2] = Q(2, k);
	hyperplane.coeffs().data()[3] = 0; //non linear part - should go through 0s

	e::Vector3d hyperplaneNormal = hyperplane.normal();
	//std::cout << "Normal: " << hyperplaneNormal << std::endl;
}