Esempio n. 1
0
int CBICLearningEngine::DimOfModel(const CStaticGraphicalModel *pModel)
{
/*
compute dimension of the model in (d)
it using in BIC criterion:
BIC = LogLic - 0.5*d*log(N)
    */
    int nParam = pModel->GetNumberOfFactors();
    CFactor *param = NULL;
    int dimOfModel = 0;
    int dim = 1;
    CMatrix<float> *matrix;;
    for (int  i = 0; i < nParam; i++)
    {
	dim = 1;
	param = pModel->GetFactor(i);
	switch (param->GetFactorType())
	{
	case ftCPD:
	    {
		switch (param->GetDistributionType())
		{
		case dtTabular:
		    {

			matrix = param->GetMatrix(matTable);
			int size;
			const int *ranges;
			static_cast<CNumericDenseMatrix<float>*>(matrix)->
			    GetRanges(&size, &ranges);
			for(int j=0; j < size - 1; j++)
			{
			    dim *= ranges[j];

			}
			dim *= ranges[size-1]-1;
			break;

		    }//case dtTabular
		case dtGaussian:
		    {
			PNL_THROW(CNotImplemented,"Gaussian")
			    break;
		    }//case dtGaussian
		case dtCondGaussian:
		    {
			PNL_THROW(CNotImplemented,"CondGaussian")
			    break;
		    }//case dtCondGaussian
		default:
		    {
			PNL_THROW(CBadConst,"distribution type")
			    break;
		    }
		}//swith(param->GetFactorType)
		break;
	    }//end case ftCPD
	case ftPotential:
	    {
		PNL_THROW(CNotImplemented,"Factor")
		    break;
	    }
	default:
	    {
		PNL_THROW(CBadConst,"FactorType")
		    break;
	    }
	}//end switch(param->GetFactor)

	dimOfModel += dim;
    }//end for(i)
    return dimOfModel;
}
void CSoftMaxCPD::CreateMeanAndCovMatrixForNode(int Node, const CEvidence* pEvidence, 
    const CBNet *pBNet, floatVector &Mean, 
    C2DNumericDenseMatrix<float>**CovContParents) const
{
    int i, j, k;
    int *multiindex = new int [2];
    intVector contParents;
    pBNet->GetContinuousParents(Node, &contParents);
                   
    intVector lineSizes;
    lineSizes.assign(2, contParents.size());
    floatVector zerodata;
    zerodata.assign(contParents.size()*contParents.size(), 0.0f);
    
    *CovContParents = 
        C2DNumericDenseMatrix<float>::Create( &lineSizes.front(), &zerodata.front() );
    for (i = 0; i < contParents.size(); i++ )    
    {
        for (j = 0; j < contParents.size(); j++ )
        {
            multiindex[0] = i;
            multiindex[1] = j;
            (*CovContParents)->SetElementByIndexes(0.0f, multiindex);
        }
    }
 
    intVector pObsNodes;
    pConstValueVector pObsValues;
    pConstNodeTypeVector pNodeTypes;
    pEvidence->GetObsNodesWithValues(&pObsNodes, &pObsValues, &pNodeTypes);

    Mean.resize(0);
    for ( i = 0; i < contParents.size(); i++)
    { 
       int CurNode = contParents[i];  
       CFactor *param;
       param = pBNet->GetFactor(CurNode);
       intVector parentOut;
       intVector DiscrParents;
       pBNet->GetDiscreteParents(CurNode, &DiscrParents);
          
       int *parentComb = new int [DiscrParents.size()];
    
       for ( j = 0; j < DiscrParents.size(); j++)
       {
            for ( k = 0; k < pObsNodes.size(); k++)
            {
                if (DiscrParents[j] == pObsNodes[k])
                {
                    parentComb[j] = pObsValues[k]->GetInt();
                    break;
                }
            }
       }
                    
       const float *data;
       int datasize;
                    
       static_cast<CDenseMatrix<float>*>(param->GetMatrix(matMean, -1, parentComb))->
           GetRawData(&datasize, &data);
       Mean.push_back(data[0]);
                    
       static_cast<CDenseMatrix<float>*>(param->GetMatrix(matCovariance, -1, parentComb))->
           GetRawData(&datasize, &data);
       multiindex[0] = i;
       multiindex[1] = i;
       (*CovContParents)->SetElementByIndexes(data[0], multiindex);
       delete [] parentComb;
    }
    delete [] multiindex;
}