double InvLogitGaussianJointPdf<V,M>::lnValue( const V& domainVector, const V* domainDirection, V* gradVector, M* hessianMatrix, V* hessianEffect) const { double returnValue; double lnDeterminant = 0.0; V transformedDomainVector(domainVector); V min_domain_bounds(this->m_domainBoxSubset.minValues()); V max_domain_bounds(this->m_domainBoxSubset.maxValues()); double lnjacobian = 0.0; for (unsigned int i = 0; i < domainVector.sizeLocal(); i++) { double min_val = min_domain_bounds[i]; double max_val = max_domain_bounds[i]; if (queso_isfinite(min_val) && queso_isfinite(max_val)) { if (domainVector[i] == min_val || domainVector[i] == max_val) { // Exit early if we can return -INFINITY; } // Left- and right-hand sides are finite. Do full transform. transformedDomainVector[i] = std::log(domainVector[i] - min_val) - std::log(max_val - domainVector[i]); lnjacobian += std::log(max_val - min_val) - std::log(domainVector[i] - min_val) - std::log(max_val - domainVector[i]); } else if (queso_isfinite(min_val) && !queso_isfinite(max_val)) { if (domainVector[i] == min_val) { // Exit early if we can return -INFINITY; } // Left-hand side finite, but right-hand side is not. // Do only left-hand transform. transformedDomainVector[i] = std::log(domainVector[i] - min_val); lnjacobian += -std::log(domainVector[i] - min_val); } else if (!queso_isfinite(min_val) && queso_isfinite(max_val)) { if (domainVector[i] == max_val) { // Exit early if we can return -INFINITY; } // Right-hand side is finite, but left-hand side is not. // Do only right-hand transform. transformedDomainVector[i] = -std::log(max_val - domainVector[i]); lnjacobian += -std::log(max_val - domainVector[i]); } else { // No transform. transformedDomainVector[i] = domainVector[i]; } } V diffVec(transformedDomainVector - this->lawExpVector()); if (m_diagonalCovMatrix) { returnValue = ((diffVec * diffVec) / this->lawVarVector()).sumOfComponents(); if (m_normalizationStyle == 0) { unsigned int iMax = this->lawVarVector().sizeLocal(); for (unsigned int i = 0; i < iMax; ++i) { lnDeterminant += log(this->lawVarVector()[i]); } } } else { V tmpVec = this->m_lawCovMatrix->invertMultiply(diffVec); returnValue = (diffVec * tmpVec).sumOfComponents(); if (m_normalizationStyle == 0) { lnDeterminant = this->m_lawCovMatrix->lnDeterminant(); } } if (m_normalizationStyle == 0) { returnValue += ((double) this->lawVarVector().sizeLocal()) * log(2 * M_PI); returnValue += lnDeterminant; } returnValue *= -0.5; returnValue += m_logOfNormalizationFactor; returnValue += lnjacobian; return returnValue; }
double GaussianJointPdf<V,M>::lnValue( const V& domainVector, const V* domainDirection, V* gradVector, M* hessianMatrix, V* hessianEffect) const { if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) { *m_env.subDisplayFile() << "Entering GaussianJointPdf<V,M>::lnValue()" << ", meanVector = " << *m_lawExpVector << ", lawCovMatrix = " << *m_lawCovMatrix << ": domainVector = " << domainVector << std::endl; } queso_require_msg(!(domainDirection || hessianMatrix || hessianEffect), "incomplete code for gradVector, hessianMatrix and hessianEffect calculations"); double returnValue = 0.; double lnDeterminant = 0.; if (this->m_domainSet.contains(domainVector) == false) { // What should the gradient be here? returnValue = -INFINITY; } else { V diffVec(domainVector - this->lawExpVector()); if (m_diagonalCovMatrix) { returnValue = ((diffVec*diffVec)/this->lawVarVector()).sumOfComponents(); // Compute the gradient of log of the pdf. // The log of a Gaussian pdf is: // f(x) = - 1/2 (x - \mu)^T \Sigma^{-1} (x - \mu) // Therefore // \frac{df}{dx}(x) = - (x - \mu)^T \Sigma^{-1} // = - (\Sigma^{-1}^T (x - \mu))^T // = - (\Sigma^{-1} (x - \mu))^T // = - \Sigma^{-1} (x - \mu) (row/column vector doesn't matter) // // So if \Sigma is diagonal we have a component-wise product of two // vectors (x - \mu) and the diagonal elements of \Sigma^{-1} if (gradVector) { (*gradVector) = diffVec; // Copy (*gradVector) /= this->lawVarVector(); (*gradVector) *= -1.0; } if (m_normalizationStyle == 0) { unsigned int iMax = this->lawVarVector().sizeLocal(); for (unsigned int i = 0; i < iMax; ++i) { lnDeterminant += std::log(this->lawVarVector()[i]); } } } else { V tmpVec = this->m_lawCovMatrix->invertMultiply(diffVec); returnValue = (diffVec*tmpVec).sumOfComponents(); // Compute the gradient of log of the pdf. // The log of a Gaussian pdf is: // f(x) = - 1/2 (x - \mu)^T \Sigma^{-1} (x - \mu) // Therefore // \frac{df}{dx}(x) = - (x - \mu)^T \Sigma^{-1} // = - (\Sigma^{-1}^T (x - \mu))^T // = - (\Sigma^{-1} (x - \mu))^T // = - \Sigma^{-1} (x - \mu) (row/column vector doesn't matter) if (gradVector) { (*gradVector) = tmpVec; (*gradVector) *= -1.0; } if (m_normalizationStyle == 0) { lnDeterminant = this->m_lawCovMatrix->lnDeterminant(); } } if (m_normalizationStyle == 0) { returnValue += ((double) this->lawVarVector().sizeLocal()) * std::log(2*M_PI); // normalization of pdf returnValue += lnDeterminant; // normalization of pdf } returnValue *= -0.5; } returnValue += m_logOfNormalizationFactor; if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) { *m_env.subDisplayFile() << "Leaving GaussianJointPdf<V,M>::lnValue()" << ", m_normalizationStyle = " << m_normalizationStyle << ", m_diagonalCovMatrix = " << m_diagonalCovMatrix << ", m_logOfNormalizationFactor = " << m_logOfNormalizationFactor << ", lnDeterminant = " << lnDeterminant << ", meanVector = " << *m_lawExpVector << ", lawCovMatrix = " << *m_lawCovMatrix << ": domainVector = " << domainVector << ", returnValue = " << returnValue << std::endl; } return returnValue; }
double LogNormalJointPdf<V,M>::lnValue( const V& domainVector, const V* domainDirection, V* gradVector, M* hessianMatrix, V* hessianEffect) const { if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) { *m_env.subDisplayFile() << "Entering LogNormalJointPdf<V,M>::lnValue()" << ", meanVector = " << *m_lawExpVector << ": domainVector = " << domainVector << std::endl; } UQ_FATAL_TEST_MACRO((gradVector || hessianMatrix || hessianEffect), m_env.worldRank(), "LogNormalJointPdf<V,M>::lnValue()", "incomplete code for gradVector, hessianMatrix and hessianEffect calculations"); if (domainDirection) {}; // just to remove compiler warning double returnValue = 0.; V zeroVector(domainVector); zeroVector.cwSet(0.); if (domainVector.atLeastOneComponentSmallerOrEqualThan(zeroVector)) { returnValue = -INFINITY; } else if (this->m_domainSet.contains(domainVector) == false) { // prudenci 2011-Oct-04 returnValue = -INFINITY; } else { if (m_diagonalCovMatrix) { V diffVec(zeroVector); for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) { diffVec[i] = std::log(domainVector[i]) - this->lawExpVector()[i]; } returnValue = ((diffVec*diffVec)/this->lawVarVector()).sumOfComponents(); returnValue *= -0.5; if (m_normalizationStyle == 0) { for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) { returnValue -= std::log(domainVector[i] * std::sqrt(2. * M_PI * this->lawVarVector()[i])); // Contribution of 1/(x\sqrt{2\pi\sigma^2}) } } } else { UQ_FATAL_TEST_MACRO(true, m_env.worldRank(), "LogNormalJointPdf<V,M>::lnValue()", "situation with a non-diagonal covariance matrix makes no sense"); } returnValue += m_logOfNormalizationFactor; // [PDF-10] } if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) { *m_env.subDisplayFile() << "Leaving LogNormalJointPdf<V,M>::lnValue()" << ", meanVector = " << *m_lawExpVector << ": domainVector = " << domainVector << ", returnValue = " << returnValue << std::endl; } return returnValue; }
void MakeGraph( Energy<float,float,float> *e , int *vars , vector< set<int> > &neighbors, vector<int> &Disparity, vector< vector<double> > Colors_L, vector<int> FLabels_L, vector<int> BLabels_L , vector<double> FDistVec_L , vector<double> BDistVec_L , vector< vector<double> > Colors_R, vector<int> FLabels_R, vector<int> BLabels_R , vector<double> FDistVec_R , vector<double> BDistVec_R , int numLabels , int num_high , int numRows , int numCols , double lambda_p , double lambda_c ) { int i; set<int>::iterator pIter; const int K = 10000; // Edge weight for infinity -- All other edge weights less than 1 -- so it suffices to have this weight as more than sum of all other edges, i.e. >8 double beta; double countCand = 0; double SumColors = 0; for( i=0 ; i<numLabels ; i++ ) { for ( pIter = neighbors[i].begin() ; pIter != neighbors[i].end() ; pIter++ ) { SumColors += pow( diffVec(Colors_L[i],Colors_L[*pIter]) , 2 ); countCand++; } } for( i=numLabels ; i<2*numLabels ; i++ ) { for ( pIter = neighbors[i].begin() ; pIter != neighbors[i].end() ; pIter++ ) { SumColors += pow( diffVec(Colors_R[i-numLabels],Colors_R[*pIter-numLabels]) , 2 ); countCand++; } } beta = countCand/(2*SumColors); /////////////////////////////////////////////////////////////////////////////////////////////////// //when reducing the order of high-order term, every high-order clique needs 5 new auxiliary variables //calulate the factors for data term vector<double> ForeEdges, BackEdges; ForeEdges.clear(); BackEdges.clear(); printf("Graph Making code starts... \n"); /****** Making Terminal Edge Weights ******/ int count=0 ; for( i=0; i<numLabels; i++) { if(findValVec(FLabels_L,i)) { count++ ; ForeEdges.push_back(K); BackEdges.push_back(0); } else if(findValVec(BLabels_L,i)) { count++ ; ForeEdges.push_back(0); BackEdges.push_back(K); } else { double temp; temp = BDistVec_L[i]+FDistVec_L[i] ; ForeEdges.push_back(FDistVec_L[i]/temp); BackEdges.push_back(BDistVec_L[i]/temp); } } for( i=0; i<numLabels; i++) { if(findValVec(FLabels_R,i)) { count++ ; ForeEdges.push_back(K); BackEdges.push_back(0); } else if(findValVec(BLabels_R,i)) { count++ ; ForeEdges.push_back(0); BackEdges.push_back(K); } else { double temp; temp = BDistVec_R[i]+FDistVec_R[i] ; ForeEdges.push_back(FDistVec_R[i]/temp); BackEdges.push_back(BDistVec_R[i]/temp); } } printf("count=%d \n Terminal Edge Weights Made... \n",count); /////////////////////////////////////////////// /******* Start making the graph **************/ /////////////////////////////////////////////// // Add Nodes for( i=0 ; i<2*numLabels+4*num_high*5 ; i++ ) vars[i] = e -> add_variable(); printf("Nodes Added to Graph... \n"); //data term for( i=0 ; i<2*numLabels ; i++ ) { e -> add_term1(vars[i], ForeEdges[i], BackEdges[i]); } printf("Terminal Edge Weights Set... \n"); //smooth term for( i=0 ; i<numLabels ; i++ ) for (pIter = neighbors[i].begin(); pIter != neighbors[i].end(); pIter++) { int tmpN = *pIter; double Energ = exp( -beta * pow(diffVec( Colors_L[i], Colors_L[tmpN]) ,2 ) ); e -> add_term2( vars[i], vars[tmpN] , 0 , lambda_p*Energ, lambda_p*Energ , 0); } for( i=numLabels ; i<2*numLabels ; i++ ) for (pIter = neighbors[i].begin(); pIter != neighbors[i].end(); pIter++) { int tmpN = *pIter; double Energ = exp( -beta * pow(diffVec( Colors_R[i-numLabels], Colors_R[tmpN-numLabels]) ,2 ) ); e -> add_term2( vars[i], vars[tmpN] , 0 , lambda_p*Energ, lambda_p*Energ , 0); } //correspondence term: the low-order terms decomposed from the high-order term int count_high =0 ; int loc_y[4] = { -1 , 1, 0, 0 } ; int loc_x[4] = { 0 , 0, -1, 1 } ; //up down left right float f1 , f2 , a ; f2 = 2 ; float theta1 , theta2 , gamma1, gamma2 ; theta1=0.5 ; theta2=1.5 ; //////////////////////////////// //////////////////////////////////////// gamma1 = 200 ; gamma2 = 1000 ; for( int c=1 ; c<numCols-1 ; c++ ) for( int h=1 ; h<numRows-1 ; h++ ) { int this_ind , this_disp , cr ; this_ind = c*numRows + h ; this_disp = Disparity[this_ind] ; cr = c- this_disp ; if( cr >= 1 ) { vector<double> Meanwin_l(3) ; vector<double> Meanwin_r(3) ; get_win_mean( Colors_L , h , c , numRows , numCols , Meanwin_l ) ; get_win_mean( Colors_R , h , cr, numRows , numCols , Meanwin_r ) ; double C_rl = exp( -sqrt( 1/(2*16*16) * pow(diffVec( Meanwin_l, Meanwin_r) ,2) ) ); C_rl *= lambda_c ; float local_var=0; local_var = get_var( Colors_L , Colors_R , h, c, cr , numRows , numCols , Meanwin_l , Meanwin_r); if ( local_var<theta1 ) f1 = gamma1 ; else if ( local_var>theta2 ) f1 = gamma2 ; else { f1 = -(gamma2-gamma1)/(theta2-theta1)*local_var + gamma2*theta2-gamma1*theta1 ; } a = f2 - 2*f1 ; if(a>0) { //one order terms transformed from the high-order term e -> add_term1(vars[2*numLabels + count_high*5] , 0, -C_rl*2*a); e -> add_term1(vars[2*numLabels + count_high*5 +1], 0, -C_rl*2*a); e -> add_term1(vars[2*numLabels + count_high*5 +2], 0, -C_rl*2*a); e -> add_term1(vars[2*numLabels + count_high*5 +3], 0, -C_rl*2*a); e -> add_term1(vars[2*numLabels + count_high*5 +4], 0, -C_rl*12*a); // w1 w2 w3 w4 w //two order terms transformed from the high-order term for( int u=0 ; u<4 ; u++ ) { //find the corresponding pixels and their neighbours int neigh_y , neigh_xl , neigh_xr , ind_l , ind_r , neigh_ind_l , neigh_ind_r ; neigh_y = h + loc_y[u] ; neigh_xl = c + loc_x[u] ; neigh_xr = cr + loc_x[u] ; ind_l = c * numRows + h ; ind_r = cr * numRows + h ; neigh_ind_l = neigh_xl * numRows + neigh_y ; neigh_ind_r = neigh_xr * numRows + neigh_y ; e -> add_term1(vars[ind_l] , 0, C_rl*(f1+6*a) ); e -> add_term1(vars[ind_r+numLabels] , 0, C_rl*(f1+6*a) ); e -> add_term1(vars[neigh_ind_l] , 0, C_rl*(f1+6*a) ); e -> add_term1(vars[neigh_ind_r+numLabels] , 0, C_rl*(f1+6*a) ); //x0 y0 x1 y1 e -> add_term2( vars[ind_l] , vars[neigh_ind_l] , 0 , 0 , 0 , -C_rl*3*a); //x0x1 y0y1 x0y1 x1y0 e -> add_term2( vars[ind_r+numLabels], vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*3*a); e -> add_term2( vars[ind_l] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*3*a); e -> add_term2( vars[neigh_ind_l] , vars[ind_r+numLabels] , 0 , 0 , 0 , -C_rl*3*a); e -> add_term2( vars[ind_l] , vars[ind_r+numLabels] , 0 , 0 , 0 , C_rl*(-2*f1-4*a) ); e -> add_term2( vars[neigh_ind_l], vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , C_rl*(-2*f1-4*a) ); //x0y0 x1y1 e -> add_term2( vars[2*numLabels + count_high*5] , vars[ind_l] , 0 , 0 , 0 , -C_rl*2*a ); //w1 *( x0 + x1 + y0 ) e -> add_term2( vars[2*numLabels + count_high*5] , vars[neigh_ind_l] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5] , vars[ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+1] , vars[ind_l] , 0 , 0 , 0 , -C_rl*2*a ); //w2 *( x0 + x1 + y1 ) e -> add_term2( vars[2*numLabels + count_high*5+1] , vars[neigh_ind_l] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+1] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+2] , vars[ind_l] , 0 , 0 , 0 , -C_rl*2*a ); //w3 *( x0 + y0 + y1 ) e -> add_term2( vars[2*numLabels + count_high*5+2] , vars[ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+2] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+3] , vars[neigh_ind_l] , 0 , 0 , 0 , -C_rl*2*a ); //w4 *( x1 + y0 + y1 ) e -> add_term2( vars[2*numLabels + count_high*5+3] , vars[ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+3] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+4] , vars[ind_l] , 0 , 0 , 0 , C_rl*4*a ); //w*(x0+x1+y0+y1) e -> add_term2( vars[2*numLabels + count_high*5+4] , vars[neigh_ind_l] , 0 , 0 , 0 , C_rl*4*a ); e -> add_term2( vars[2*numLabels + count_high*5+4] , vars[ind_r+numLabels] , 0 , 0 , 0 , C_rl*4*a ); e -> add_term2( vars[2*numLabels + count_high*5+4] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , C_rl*4*a ); } } else { e -> add_term1(vars[2*numLabels + count_high*5] , 0, C_rl*4*a); e -> add_term1(vars[2*numLabels + count_high*5 +1], 0, C_rl*4*a); e -> add_term1(vars[2*numLabels + count_high*5 +2], 0, C_rl*4*a); e -> add_term1(vars[2*numLabels + count_high*5 +3], 0, C_rl*4*a); e -> add_term1(vars[2*numLabels + count_high*5 +4], 0, C_rl*12*a); // w1 w2 w3 w4 w for( int u=0 ; u<4 ; u++ ) { int neigh_y , neigh_xl , neigh_xr , ind_l , ind_r , neigh_ind_l , neigh_ind_r ; neigh_y = h + loc_y[u] ; neigh_xl = c + loc_x[u] ; neigh_xr = cr + loc_x[u] ; ind_l = c * numRows + h ; ind_r = cr * numRows + h ; neigh_ind_l = neigh_xl * numRows + neigh_y ; neigh_ind_r = neigh_xr * numRows + neigh_y ; e -> add_term1(vars[ind_l] , 0, C_rl*f1 ); e -> add_term1(vars[ind_r+numLabels] , 0, C_rl*f1 ); e -> add_term1(vars[neigh_ind_l] , 0, C_rl*f1 ); e -> add_term1(vars[neigh_ind_r+numLabels] , 0, C_rl*f1 ); //x0 y0 x1 y1 e -> add_term2( vars[ind_l] , vars[neigh_ind_l] , 0 , 0 , 0 , C_rl*5*a); //x0x1 y0y1 x0y1 x1y0 e -> add_term2( vars[ind_r+numLabels], vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , C_rl*5*a); e -> add_term2( vars[ind_l] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , C_rl*5*a); e -> add_term2( vars[neigh_ind_l] , vars[ind_r+numLabels] , 0 , 0 , 0 , C_rl*5*a); e -> add_term2( vars[ind_l] , vars[ind_r+numLabels] , 0 , 0 , 0 , C_rl*(-2*f1+4*a) ); e -> add_term2( vars[neigh_ind_l], vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , C_rl*(-2*f1+4*a) ); //x0y0 x1y1 e -> add_term2( vars[2*numLabels + count_high*5] , vars[ind_l] , 0 , 0 , 0 , -C_rl*2*a ); //w1 *( x0 + x1 + y0 ) e -> add_term2( vars[2*numLabels + count_high*5] , vars[neigh_ind_l] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5] , vars[ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+1] , vars[ind_l] , 0 , 0 , 0 , -C_rl*2*a ); //w2 *( x0 + x1 + y1 ) e -> add_term2( vars[2*numLabels + count_high*5+1] , vars[neigh_ind_l] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+1] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+2] , vars[ind_l] , 0 , 0 , 0 , -C_rl*2*a ); //w3 *( x0 + y0 + y1 ) e -> add_term2( vars[2*numLabels + count_high*5+2] , vars[ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+2] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+3] , vars[neigh_ind_l] , 0 , 0 , 0 , -C_rl*2*a ); //w4 *( x1 + y0 + y1 ) e -> add_term2( vars[2*numLabels + count_high*5+3] , vars[ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+3] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*2*a ); e -> add_term2( vars[2*numLabels + count_high*5+4] , vars[ind_l] , 0 , 0 , 0 , -C_rl*8*a ); //w*(x0+x1+y0+y1) e -> add_term2( vars[2*numLabels + count_high*5+4] , vars[neigh_ind_l] , 0 , 0 , 0 , -C_rl*8*a ); e -> add_term2( vars[2*numLabels + count_high*5+4] , vars[ind_r+numLabels] , 0 , 0 , 0 , -C_rl*8*a ); e -> add_term2( vars[2*numLabels + count_high*5+4] , vars[neigh_ind_r+numLabels] , 0 , 0 , 0 , -C_rl*8*a ); } } count_high ++ ; } } printf("Graph Made... \n"); }