Exemple #1
0
int GibbsForSingleGaussian(float eps)
{
    std::cout<<std::endl<<"Using Gibbs for testing samples from gaussian"<<std::endl;

    int nnodes = 1;
    int numnt = 1;
    CNodeType *nodeTypes = new CNodeType[numnt];
    nodeTypes[0] = CNodeType(0,2);
   
    intVector nodeAssociation = intVector(nnodes,0);
   
   
    CGraph *graph;
    graph = CGraph::Create(nnodes, 0, NULL, NULL);

    CBNet *pBnet = CBNet::Create( nnodes, numnt, nodeTypes,
	&nodeAssociation.front(),graph );
    pBnet->AllocFactors();
	pBnet->AllocFactor(0);


    float mean[2] = {0.0f, 0.0f};
    intVector ranges(2,1);
    ranges[0] = 2;

    ///////////////////////////////////////////////////////////////////
    CNumericDenseMatrix<float> *mean0 =	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), mean);

    ranges[1] = 2;
    float cov[4] = {1.0f, 0.3f, 0.3f, 1.0f};
    CNumericDenseMatrix<float> *cov0 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), cov);

    pBnet->GetFactor(0)->AttachMatrix( mean0, matMean );
    pBnet->GetFactor(0)->AttachMatrix( cov0, matCovariance );
    /////////////////////////////////////////////////////////////////////
    CGibbsSamplingInfEngine *pGibbsInf = CGibbsSamplingInfEngine::Create( pBnet );
    pGibbsInf->SetBurnIn( 100 );
    pGibbsInf->SetMaxTime( 5000 );

    pEvidencesVector evidences;
    pBnet->GenerateSamples(&evidences, 1 );
    
    const int ndsToToggle[] = { 0 };
    evidences[0]->ToggleNodeState( 1, ndsToToggle );
    
    intVector query(1,0);
    
    
    intVecVector queryes(1);
    queryes[0].push_back(0);
    pGibbsInf->SetQueries( queryes);
    pGibbsInf->EnterEvidence( evidences[0] );
    pGibbsInf->MarginalNodes( &query.front(),query.size() );

    const CPotential *pQueryPot1 = pGibbsInf->GetQueryJPD();
  
    std::cout<<"result of gibbs"<<std::endl<<std::endl;
    pQueryPot1->Dump();
    
    delete evidences[0];
   
    delete pGibbsInf;
    delete pBnet;
    delete []nodeTypes;

    return 1;

}
Exemple #2
0
int GibbsForSimplestGaussianBNet( float eps)
{
    std::cout<<std::endl<<"Gibbs for simplest gaussian BNet (3 nodes) "<<std::endl;

    int nnodes = 3;
    int numnt = 2;
    CNodeType *nodeTypes = new CNodeType[numnt];
    nodeTypes[0] = CNodeType(0,1);
    nodeTypes[1] = CNodeType(0,2);
    intVector nodeAssociation = intVector(nnodes,1);
    nodeAssociation[0] = 0;
    int nbs0[] = { 1 };
    int nbs1[] = { 0, 2 };
    int nbs2[] = { 1 };
    ENeighborType ori0[] = { ntChild };
    ENeighborType ori1[] = { ntParent, ntChild  };
    ENeighborType ori2[] = { ntParent };
    int *nbrs[] = { nbs0, nbs1, nbs2 };
    ENeighborType *orient[] = { ori0, ori1, ori2 };

    intVector numNeighb = intVector(3);
    numNeighb[0] = 1;
    numNeighb[1] = 2;
    numNeighb[2] = 1;

    CGraph *graph;
    graph = CGraph::Create(nnodes, &numNeighb.front(), nbrs, orient);

    CBNet *pBnet = CBNet::Create( nnodes, numnt, nodeTypes,
	&nodeAssociation.front(),graph );
    pBnet->AllocFactors();

    for(int i = 0; i < nnodes; i++ )
    {
	pBnet->AllocFactor(i);

    }

    floatVector data(1,0.0f);
    intVector ranges(2,1);

    ///////////////////////////////////////////////////////////////////
    CNumericDenseMatrix<float> *mean0 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    data[0] = 0.3f;
    CNumericDenseMatrix<float> *cov0 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    pBnet->GetFactor(0)->AttachMatrix( mean0, matMean );
    pBnet->GetFactor(0)->AttachMatrix( cov0, matCovariance );
    /////////////////////////////////////////////////////////////////////

    ranges[0] = 2;
    data.resize(2);
    data[0] = -1.0f;
    data[1] = 0.0f;
    CNumericDenseMatrix<float> *mean1 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    ranges[1] = 2;
    data.resize(4);
    data[0] = 1.0f;
    data[1] = 0.1f;
    data[3] = 3.0f;
    data[2] = 0.1f;
    CNumericDenseMatrix<float> *cov1 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    ranges[1] =1;
    data.resize(2);
    data[0] = 1.0f;
    data[1] = 0.5f;
    CNumericDenseMatrix<float> *weight1 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());


    pBnet->GetFactor(1)->AttachMatrix( mean1, matMean );
    pBnet->GetFactor(1)->AttachMatrix( cov1, matCovariance );
    pBnet->GetFactor(1)->AttachMatrix( weight1, matWeights,0 );
    ///////////////////////////////////////////////////////////////////////////


    ranges[0] = 2;
    data.resize(2);
    data[0] = 1.0f;
    data[1] = 20.5f;
    CNumericDenseMatrix<float> *mean2 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    ranges[1] = 2;
    data.resize(4);
    data[0] = 1.0f;
    data[1] = 0.0f;
    data[3] = 9.0f;
    data[2] = 0.0f;
    CNumericDenseMatrix<float> *cov2 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());


    data.resize(2);
    data[0] = 1.0f;
    data[1] = 3.5f;
    data[2] = 1.0f;
    data[3] = 0.5f;
    CNumericDenseMatrix<float> *weight2 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());


    pBnet->GetFactor(2)->AttachMatrix( mean2, matMean );
    pBnet->GetFactor(2)->AttachMatrix( cov2, matCovariance );
    pBnet->GetFactor(2)->AttachMatrix( weight2, matWeights,0 );
    ///////////////////////////////////////////////////////////////////////////

    pEvidencesVector evidences;

    pBnet->GenerateSamples( &evidences, 1 );

    const int ndsToToggle[] = { 0, 1 };
    evidences[0]->ToggleNodeState( 2, ndsToToggle );

    intVector query(1,1);



    CNaiveInfEngine *pNaiveInf = CNaiveInfEngine::Create(pBnet);
    pNaiveInf->EnterEvidence( evidences[0] );
    pNaiveInf->MarginalNodes( &query.front(),query.size() );

    CGibbsSamplingInfEngine *pGibbsInf = CGibbsSamplingInfEngine::Create( pBnet );
    intVecVector queryes(1);
    queryes[0].push_back(1);
    pGibbsInf->SetQueries( queryes);
    pGibbsInf->EnterEvidence( evidences[0] );
    pGibbsInf->MarginalNodes( &query.front(),query.size() );

    const CPotential *pQueryPot1 = pGibbsInf->GetQueryJPD();
    const CPotential *pQueryPot2 = pNaiveInf->GetQueryJPD();
    std::cout<<"result of gibbs"<<std::endl<<std::endl;
    pQueryPot1->Dump();
    std::cout<<"result of naive"<<std::endl;
    pQueryPot2->Dump();

    int ret = pQueryPot1->IsFactorsDistribFunEqual( pQueryPot2, eps, 0 );

    delete evidences[0];
    delete pNaiveInf;
    delete pGibbsInf;
    delete pBnet;

    return ret;

}
Exemple #3
0
int GibbsForScalarGaussianBNet( float eps)
{
    std::cout<<std::endl<<" Scalar gaussian BNet (5 nodes)"<< std::endl;
    CBNet *pBnet;
    pEvidencesVector evidences;

    CGibbsSamplingInfEngine *pGibbsInf;
    const CPotential *pQueryPot1, *pQueryPot2;
    int i, ret;

    ////////////////////////////////////////////////////////////////////////
    //Do the example from Satnam Alag's PhD thesis, UCB ME dept 1996 p46
    //Make the following polytree, where all arcs point down
    //
    // 0   1
    //  \ /
    //   2
    //  / \
    // 3   4
    //
    //////////////////////////////////////////////////////////////////////

    int nnodes = 5;
    int numnt = 1;
    CNodeType *nodeTypes = new CNodeType[numnt];
    nodeTypes[0] = CNodeType(0,1);

    intVector nodeAssociation = intVector(nnodes,0);

    int nbs0[] = { 2 };
    int nbs1[] = { 2 };
    int nbs2[] = { 0, 1, 3, 4 };
    int nbs3[] = { 2 };
    int nbs4[] = { 2 };
    ENeighborType ori0[] = { ntChild };
    ENeighborType ori1[] = { ntChild };
    ENeighborType ori2[] = { ntParent, ntParent, ntChild, ntChild };
    ENeighborType ori3[] = { ntParent };
    ENeighborType ori4[] = { ntParent };
    int *nbrs[] = { nbs0, nbs1, nbs2, nbs3, nbs4 };
    ENeighborType *orient[] = { ori0, ori1, ori2, ori3, ori4 };
    intVector numNeighb = intVector(5,1);
    numNeighb[2] = 4;
    CGraph *graph;
    graph = CGraph::Create(nnodes, &numNeighb.front(), nbrs, orient);

    pBnet = CBNet::Create( nnodes, numnt, nodeTypes, &nodeAssociation.front(),graph );
    pBnet->AllocFactors();

    for( i = 0; i < nnodes; i++ )
    {
	pBnet->AllocFactor(i);
    }
    //now we need to create data for factors - we'll create matrices
    floatVector smData = floatVector(1,0.0f);
    floatVector bigData = floatVector(1,1.0f);
    intVector ranges = intVector(2, 1);
    ranges[0] = 1;
    smData[0] = 1.0f;
    CNumericDenseMatrix<float> *mean0 = CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &smData.front());
    bigData[0] = 4.0f;

    CNumericDenseMatrix<float> *cov0 = CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &bigData.front());
    pBnet->GetFactor(0)->AttachMatrix(mean0, matMean);
    pBnet->GetFactor(0)->AttachMatrix(cov0, matCovariance);

    float val = 1.0f;

    CNumericDenseMatrix<float> *mean1 = CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &val );
    CNumericDenseMatrix<float> *cov1 = CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &val );
    pBnet->GetFactor(1)->AttachMatrix(mean1, matMean);
    pBnet->GetFactor(1)->AttachMatrix(cov1, matCovariance);
    smData[0] = 0.0f;

    CNumericDenseMatrix<float> *mean2 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &smData.front());
    smData[0] = 2.0f;

    CNumericDenseMatrix<float> *w21 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &smData.front());
    bigData[0] = 2.0f;

    CNumericDenseMatrix<float> *cov2 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &bigData.front());
    bigData[0] = 1.0f;

    CNumericDenseMatrix<float> *w20 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &bigData.front());
    pBnet->GetFactor(2)->AttachMatrix( mean2, matMean );
    pBnet->GetFactor(2)->AttachMatrix( cov2, matCovariance );
    pBnet->GetFactor(2)->AttachMatrix( w20, matWeights,0 );
    pBnet->GetFactor(2)->AttachMatrix( w21, matWeights,1 );

    val = 0.0f;

    CNumericDenseMatrix<float> *mean3 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &val);
    val = 4.0f;
    CNumericDenseMatrix<float> *cov3 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &val);

    smData[0] = 1.1f;

    CNumericDenseMatrix<float> *w30 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &smData.front());
    pBnet->GetFactor(3)->AttachMatrix( mean3, matMean );
    pBnet->GetFactor(3)->AttachMatrix( cov3, matCovariance );
    pBnet->GetFactor(3)->AttachMatrix( w30, matWeights,0 );


    smData[0] = -0.8f;

    CNumericDenseMatrix<float> *mean4 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &smData.front());

    bigData[0] = 1.2f;

    CNumericDenseMatrix<float> *cov4 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &bigData.front());
    bigData[0] = 2.0f;

    CNumericDenseMatrix<float> *w40 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &bigData.front());
    pBnet->GetFactor(4)->AttachMatrix( mean4, matMean );
    pBnet->GetFactor(4)->AttachMatrix( cov4, matCovariance );
    pBnet->GetFactor(4)->AttachMatrix( w40, matWeights,0 );


    evidences.clear();
    pBnet->GenerateSamples( &evidences, 1 );

    const int ndsToToggle2[] = { 0, 1, 2 };
    evidences[0]->ToggleNodeState( 3, ndsToToggle2 );
    const int *flags1 = evidences[0]->GetObsNodesFlags();
    std::cout<<"observed nodes"<<std::endl;
    for( i = 0; i < pBnet->GetNumberOfNodes(); i++ )
    {
	if ( flags1[i] )
	{
	    std::cout<<"node "<<i<<"; ";
	}
    }
    std::cout<<std::endl<<std::endl;

    const int querySz2 = 1;
    const int query2[] = { 0 };

    CNaiveInfEngine *pNaiveInf = CNaiveInfEngine::Create(pBnet);
    pNaiveInf->EnterEvidence( evidences[0] );
    pNaiveInf->MarginalNodes( query2,querySz2 );

    pGibbsInf = CGibbsSamplingInfEngine::Create( pBnet );
    pGibbsInf->SetNumStreams( 1 );
    pGibbsInf->SetMaxTime( 10000 );
    pGibbsInf->SetBurnIn( 1000 );
    


    intVecVector queries(1);
    queries[0].clear();
    queries[0].push_back( 0 );
    //queries[0].push_back( 2 );
    pGibbsInf->SetQueries( queries );

    pGibbsInf->EnterEvidence( evidences[0] );
    pGibbsInf->MarginalNodes( query2, querySz2 );

    pQueryPot1 = pGibbsInf->GetQueryJPD();
    pQueryPot2 = pNaiveInf->GetQueryJPD();
    std::cout<<"result of gibbs"<<std::endl<<std::endl;
    pQueryPot1->Dump();
    std::cout<<"result of naive"<<std::endl;
    pQueryPot2->Dump();

    ret = pQueryPot1->IsFactorsDistribFunEqual( pQueryPot2, eps, 0 );

    delete evidences[0];
    delete pNaiveInf;
    delete pGibbsInf;
    delete pBnet;

    return ret;

    ////////////////////////////////////////////////////////////////////////////////////////
}
CBNet* CreateBNet()
{
	// Creation Water-Sprinkler Bayesian network

	const int numOfNds = 4;//*<-

	// 1 STEP:
	// need to specify the graph structure of the model;
	// there are two way to do it

	CGraph *pGraph;

		// Graph creation using neighbors list

		int numOfNbrs[numOfNds] = { 2, 2, 2, 2 };//*<-
		int nbrs0[] = { 1, 2 };//*<-
		int nbrs1[] = { 0, 3 };//*<-
		int nbrs2[] = { 0, 3 };//*<-
		int nbrs3[] = { 1, 2 };//*<-

		// number of neighbors for every node
		int *nbrs[] = { nbrs0, nbrs1, nbrs2, nbrs3 };//*<-

		// neighbors can be of either one of the three following types:
		// a parent, a child (for directed arcs) or just a neighbor (for undirected graphs).
		// Accordingly, the types are ntParent, ntChild or ntNeighbor.

		ENeighborType nbrsTypes0[] = { ntChild, ntChild };//*<-
		ENeighborType nbrsTypes1[] = { ntParent, ntChild };//*<-
		ENeighborType nbrsTypes2[] = { ntParent, ntChild };//*<-
		ENeighborType nbrsTypes3[] = { ntParent, ntParent };//*<-

		ENeighborType *nbrsTypes[] = { nbrsTypes0, nbrsTypes1,nbrsTypes2, nbrsTypes3 };//*<-

		// this is creation of a directed graph for the BNet model using neighbors list
		pGraph = CGraph::Create( numOfNds, numOfNbrs, nbrs, nbrsTypes );

	// 2 STEP:
	// Creation NodeType objects and specify node types for all nodes of the model.

	nodeTypeVector  nodeTypes;

	// number of node types is 1, because all nodes are of the same type
	// all four are discrete and binary
	CNodeType nt(1,2);//*<-
	nodeTypes.push_back(nt);

	intVector nodeAssociation;
	// reflects association between node numbers and node types
	// nodeAssociation[k] is a number of node type object in the
	// node types array for the k-th node
	nodeAssociation.assign(numOfNds, 0);

	// 2 STEP:
	// Creation base for BNet using Graph, types of nodes and nodes association

	CBNet* pBNet = CBNet::Create( numOfNds, nodeTypes, nodeAssociation, pGraph );

	// 3 STEP:
	// Allocation space for all factors of the model
	pBNet->AllocFactors();

	// 4 STEP:
	// Creation factors and attach their to model

	//create raw data tables for CPDs
	float table0[] = { 0.5f, 0.5f };//*<-
	float table1[] = { 0.5f, 0.5f, 0.9f, 0.1f };
	float table2[] = { 0.8f, 0.2f, 0.2f, 0.8f };
	float table3[] = { 1.0f, 0.0f, 0.1f, 0.9f, 0.1f, 0.9f, 0.01f, 0.99f };

	float* table[] = { table0, table1, table2, table3 };//*<-

	int i;
	for( i = 0; i < numOfNds; ++i )
	{
		pBNet->AllocFactor(i);

		CFactor* pFactor = pBNet->GetFactor(i);

		pFactor->AllocMatrix( table[i], matTable );
	}

	return pBNet;
}
void CBICLearningEngine::Learn()
{
    CEMLearningEngine *pLearn = NULL;

    float resultBIC = -FLT_MAX;
    CBNet *pResultBNet = NULL;
    intVector resultOrder;
    
    
    pEvidencesVector pEv(m_Vector_pEvidences.size(), NULL );
    
    CModelDomain *pMD = m_pGrModel->GetModelDomain();
    
    int nnodes = m_pGrModel->GetNumberOfNodes();
    
    nodeTypeVector varTypes;
    pMD->GetVariableTypes(&varTypes);

    intVector varAss( pMD->GetVariableAssociations(), pMD->GetVariableAssociations() + nnodes );
       
    intVector currentAssociation(nnodes);
    intVector currentObsNodes(nnodes);
    int i;
    for( i = 0; i < nnodes; i++ )
    {
	currentObsNodes[i] = i;
    }

    CGraph *pGraph = CGraph::Create(nnodes, NULL, NULL, NULL);
    CBNet *pBNet;
    int lineSz = int( nnodes * ( nnodes - 1 ) / 2 );
    intVecVector connect;
    intVector indexes(lineSz, 0);
    int startNode, endNode;
    int ind;
    for( ind = 0; ind < lineSz ; )
    {
	if( indexes[ind] == 1 )
	{
	    FindNodesByNumber(&startNode, &endNode, nnodes, ind);
	    pGraph->RemoveEdge(startNode, endNode );
	    indexes[ind] = 0;
	    ind++;
	}
	else
	{
	    FindNodesByNumber(&startNode, &endNode, nnodes, ind);
	    pGraph->AddEdge(startNode, endNode, 1 );
	    indexes[ind] = 1;
	    ind = 0;
	    connect.clear();
	    pGraph->GetConnectivityComponents(&connect);
	    if( connect.size() == 1 )
	    {
		
		do
		{
		    CGraph *pCopyGraph = CGraph::Copy(pGraph);
		    int j;
		    for( j = 0; j < nnodes; j++ )
		    {
			currentAssociation[j] = varAss[currentObsNodes[j]];
		    }
		    
		    pBNet = CBNet::Create(nnodes, varTypes, currentAssociation, pCopyGraph);
		    pBNet->AllocFactors();
		    for( j = 0; j < nnodes; j++ )
		    {
			pBNet->AllocFactor( j );
			pBNet->GetFactor(j)->CreateAllNecessaryMatrices();
		    }

		    int dimOfModel = DimOfModel(pBNet);
		    int k;
		    for( k = 0; k < pEv.size(); k++ )
		    {
			valueVector vls; 
			m_Vector_pEvidences[k]->GetRawData(&vls);
			pEv[k] = CEvidence::Create( pBNet->GetModelDomain(),currentObsNodes, vls );
		    }
		    
		    
		    pLearn = CEMLearningEngine::Create(pBNet);
		    pLearn->SetData(pEv.size(), &pEv.front());
		    pLearn->Learn();
		    int nsteps;
		    const float *score;
		    pLearn->GetCriterionValue(&nsteps, &score);
		    float log_lik = score[nsteps-1];
		    float BIC = log_lik - 0.5f*float( dimOfModel*log(float(pEv.size())) );
		    
		    if( BIC >= resultBIC )
		    {
			delete pResultBNet;
			resultBIC = BIC;
			m_critValue.push_back(BIC);
			pResultBNet = pBNet;
			resultOrder.assign( currentObsNodes.begin(), currentObsNodes.end() );
		    }
		    else
		    {
			delete pBNet;
		    }
		    for( k = 0; k < pEv.size(); k++ )
		    {
			delete pEv[k];
		    }

		    delete pLearn;
		}while(std::next_permutation(currentObsNodes.begin(), currentObsNodes.end()));
		
	    }
	    
	}
    }
    
    delete pGraph;
    m_pResultGrModel = pResultBNet;
    m_resultRenaming.assign(resultOrder.begin(), resultOrder.end());
    
}
int testBayesLearningEngine()
{
    
    int ret = TRS_OK;
    int i, j;
    const int nnodes = 4;//Number of nodes
    int numNt = 1;//number of Node Types

    float eps = -1.0f;
    while( eps <= 0)
    {
	trssRead( &eps, "0.01f", "accuracy in test");
    }
    CNodeType *nodeTypes = new CNodeType [numNt];
    for( i=0; i < numNt; i++ )
    {
	nodeTypes[i] = CNodeType(1,2);//all nodes are discrete and binary
    }

    int nodeAssociation[] = {0, 0, 0, 0};
    int obs_nodes[] = { 0, 1, 2, 3 };
    int numOfNeigh[] = { 2, 2, 2, 2};

    int neigh0[] = { 1, 2 };
    int neigh1[] = { 0, 3 };
    int neigh2[] = { 0, 3 };
    int neigh3[] = { 1, 2 };

    ENeighborType orient0[] = { ntChild, ntChild };
    ENeighborType orient1[] = { ntParent, ntChild };
    ENeighborType orient2[] = { ntParent, ntChild };
    ENeighborType orient3[] = { ntParent, ntParent };

    int *neigh[] = { neigh0, neigh1, neigh2, neigh3 };
    ENeighborType *orient[] = { orient0, orient1, orient2, orient3 };

    float prior0[] = { 1.f, 1.f };
    float prior1[] = { 1.f, 1.f, 1.f, 1.f };
    float prior2[] = { 1.f, 1.f, 1.f, 1.f };
    float prior3[] = { 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f };

    float* priors[] = { prior0,prior1,prior2,prior3 };

    float zero_array[] = { 1,1,1,1, 1,1,1,1 };

    float test_data0[] = { 0.636364f, 0.363636f };
    float test_data1[] = { 0.6f, 0.4f, 0.777778f, 0.222222f };
    float test_data2[] = { 0.866667f, 0.133333f, 0.111111f, 0.888889f };
    float test_data3[] = { 0.888889f, 0.111111f, 0.111111f, 0.888889f,
	0.142857f, 0.857143f, 0.333333f, 0.666667f };
    float* test_data_first[] = { test_data0, test_data1, test_data2, test_data3 };

    float test_data4[] = { 0.519231f, 0.480769f };
    float test_data5[] = { 0.571429f, 0.428571f, 0.884615f, 0.115385f };
    float test_data6[] = { 0.857143f, 0.142857f, 0.0769231f, 0.923077f };
    float test_data7[] = { 0.937500f, 0.0625000f, 0.12f, 0.88f,
	0.166667f, 0.833333f, 0.2f, 0.8f };
    float* test_data_second[] = { test_data4, test_data5, test_data6, test_data7 };

    CGraph* Graph = CGraph::Create( nnodes, numOfNeigh, neigh,
	orient);

    CBNet *myBNet = CBNet::Create(nnodes, numNt, nodeTypes,
	nodeAssociation, Graph );

    myBNet->AllocFactors();
    for ( int node = 0; node < nnodes; node++ )
    {
	myBNet->AllocFactor( node );
	//allocate empty CPT matrix, it needs to be allocated even if
	//we are going to learn train it.
	(myBNet->GetFactor( node ))->AllocMatrix( zero_array, matTable );
	//allocate prior matrices
	(myBNet->GetFactor( node ))->AllocMatrix( priors[node], matDirichlet );
	static_cast<CCPD *>(myBNet->GetFactor( node ))->NormalizeCPD();
    }

    ///////////////////////////////////////////////////////////////
    //Reading cases from disk

    FILE *fp;
    const int nEv = 50;
    CEvidence **m_pEv;
    m_pEv = new CEvidence *[nEv];
    std::vector<valueVector> Evidence(nEv);
    for( int ev = 0; ev < nEv; ev++)
    {
	Evidence[ev].resize(nnodes);
    }

    int simbol;
    char *argv = "../c_pgmtk/tests/testdata/cases1";
    fp = fopen(argv, "r");
    if(!fp)
    {
        argv = "../testdata/cases1";
        if ((fp = fopen(argv, "r")) == NULL)
	{
            printf( "can't open file %s\n", argv );
	    ret = TRS_FAIL;
	    return trsResult( ret, ret == TRS_OK ? "No errors" :
	        "Bad test: no file with cases");
	}
    }
    if (fp)
    {
	i = 0;
	j = 0;
	while( i < nEv)
	{
	    simbol = getc( fp );
	    if( isdigit( simbol ) )
	    {
		(Evidence[i])[j].SetInt(simbol - '0');
		j++;
		if( ( j - nnodes ) == 0 )
		{
		    i++;
		    j = 0;
		}
	    }
	}
    }

    for( i = 0; i < nEv; i++ )
    {
	m_pEv[i] = CEvidence::Create(myBNet->GetModelDomain(),
	    nnodes, obs_nodes, Evidence[i]);
    }

    //create bayesin learning engine
    CBayesLearningEngine *pLearn = CBayesLearningEngine::Create(myBNet);

    //Learn only portion of evidences
    pLearn->SetData(20, m_pEv);
    pLearn ->Learn();

    ///////////////////////////////////////////////////////////////////////////////
    CNumericDenseMatrix<float> *pMatrix;
    int length = 0;
    const float *output;

    for ( i = 0; i < nnodes; i++)
    {
	pMatrix = static_cast<CNumericDenseMatrix<float>*>(myBNet->
	    GetFactor(i)->GetMatrix(matTable));
	pMatrix->GetRawData(&length, &output);
	for (j = 0; j < length; j++)
	{
	    if( fabs(output[j] - test_data_first[i][j] ) > eps )
	    {
		ret = TRS_FAIL;
		return trsResult( ret, ret == TRS_OK ? "No errors" :
		"Bad test on BayesLearningEngine");
		break;
	    }

	}
    }

    //Learn second portion of evidences
    pLearn->AppendData(20, m_pEv+20);
    pLearn->AppendData(10, m_pEv+40);
    pLearn ->Learn();

    //check second portion
    for ( i = 0; i < nnodes; i++)
    {
	pMatrix = static_cast<CNumericDenseMatrix<float>*>(myBNet->
	    GetFactor(i)->GetMatrix(matTable));
	pMatrix->GetRawData(&length, &output);
	for (j = 0; j < length; j++)
	{
	    if( fabs(output[j] - test_data_second[i][j] ) > eps )
	    {
		ret = TRS_FAIL;
		return trsResult( ret, ret == TRS_OK ? "No errors" :
		"Bad test on BayesLearningEngine");
		break;
	    }

	}
    }

    for( i = 0; i < nEv; i++ )
    {
	delete (m_pEv[i]);

    }
    delete []m_pEv;
    delete (pLearn);
    delete (myBNet);
    delete [](nodeTypes);
    fclose(fp);
    return trsResult( ret, ret == TRS_OK ? "No errors"
	: "Bad test on BayesLearningEngine");
}
Exemple #7
0
int main()
{
    PNL_USING
	//we create very small model to start inference on it
	// the model is from Kevin Murphy's BNT\examples\static\belprop_polytree_gaussain
	/*
	Do the example from Satnam Alag's PhD thesis, UCB ME dept 1996 p46
	Make the following polytree, where all arcs point down
	
	 0   1
	  \ /
	   2
	  / \
	 3   4


	*/
	int i;
	//create this model
	int nnodes = 5;
	int numnt = 2;
	CNodeType *nodeTypes = new CNodeType[numnt];
 	nodeTypes[0] = CNodeType(0,2);
	nodeTypes[1] = CNodeType(0,1);
	
	intVector nodeAssociation = intVector(nnodes,0);
	nodeAssociation[1] = 1;
	nodeAssociation[3] = 1;
	int nbs0[] = { 2 };
	int nbs1[] = { 2 };
	int nbs2[] = { 0, 1, 3, 4 };
	int nbs3[] = { 2 };
	int nbs4[] = { 2 };
	int *nbrs[] = { nbs0, nbs1, nbs2, nbs3, nbs4 };
	int numNeighb[] = {1, 1, 4, 1, 1};

	
	ENeighborType ori0[] = { ntChild };
	ENeighborType ori1[] = { ntChild };
	ENeighborType ori2[] = { ntParent, ntParent, ntChild, ntChild };
	ENeighborType ori3[] = { ntParent };
	ENeighborType ori4[] = { ntParent };
	ENeighborType *orient[] = { ori0, ori1, ori2, ori3, ori4 }; 
	
	
	CGraph *pGraph;
	pGraph = CGraph::Create(nnodes, numNeighb, nbrs, orient);
	
	CBNet *pBNet;
	
	pBNet = CBNet::Create( nnodes, numnt, nodeTypes, &nodeAssociation.front(), pGraph );
	//Allocation space for all factors of the model
	pBNet->AllocFactors();
	
	for( i = 0; i < nnodes; i++ )
	{
	    //Allocation space for all matrices of CPD
	    pBNet->AllocFactor(i);
	}
	
	//now we need to create data for CPDs - we'll create matrices
	CFactor *pCPD;
	floatVector smData = floatVector(2,0.0f);
	floatVector bigData = floatVector(4,1.0f);
	intVector ranges = intVector(2, 1);
	ranges[0] = 2;
	smData[0] = 1.0f;
	CNumericDenseMatrix<float> *mean0 = CNumericDenseMatrix<float>::
        Create( 2, &ranges.front(), &smData.front());
	bigData[0] = 4.0f;
	bigData[3] = 4.0f;
	ranges[1] = 2;
	CNumericDenseMatrix<float> *cov0 = CNumericDenseMatrix<float>::
        Create( 2, &ranges.front(), &bigData.front());
	pCPD = pBNet->GetFactor(0);
	pCPD->AttachMatrix(mean0, matMean);
	pCPD->AttachMatrix(cov0, matCovariance);
	ranges[0] = 1;
	ranges[1] = 1;
	float val = 1.0f;
	CNumericDenseMatrix<float> *mean1 = CNumericDenseMatrix<float>::
        Create( 2, &ranges.front(), &val );
	CNumericDenseMatrix<float> *cov1 = CNumericDenseMatrix<float>::
        Create( 2, &ranges.front(), &val );
	pCPD = pBNet->GetFactor(1);
	pCPD->AttachMatrix(mean1, matMean);
	pCPD->AttachMatrix(cov1, matCovariance);
	smData[0] = 0.0f;
	smData[1] = 0.0f;
	ranges[0] = 2;
	CNumericDenseMatrix<float> *mean2 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &smData.front());
	smData[0] = 2.0f;
	smData[1] = 1.0f;
	CNumericDenseMatrix<float> *w21 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &smData.front());
	bigData[0] = 2.0f;
	bigData[1] = 1.0f;
	bigData[2] = 1.0f;
	bigData[3] = 1.0f;
	ranges[1] = 2;
	CNumericDenseMatrix<float> *cov2 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &bigData.front());
	bigData[0] = 1.0f;
	bigData[1] = 2.0f;
	bigData[2] = 1.0f;
	bigData[3] = 0.0f;
	CNumericDenseMatrix<float> *w20 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &bigData.front());
	pCPD = pBNet->GetFactor(2);
	pCPD->AttachMatrix( mean2, matMean );
	pCPD->AttachMatrix( cov2, matCovariance );
	pCPD->AttachMatrix( w20, matWeights,0 );
	pCPD->AttachMatrix( w21, matWeights,1 );
	
	val = 0.0f;
	ranges[0] = 1;
	ranges[1] = 1;
	CNumericDenseMatrix<float> *mean3 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &val);
	val = 1.0f;
	CNumericDenseMatrix<float> *cov3 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &val);
	ranges[1] = 2;
	smData[0] = 1.0f;
	smData[1] = 1.0f;
	CNumericDenseMatrix<float> *w30 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &smData.front());
	pCPD = pBNet->GetFactor(3);
	pCPD->AttachMatrix( mean3, matMean );
	pCPD->AttachMatrix( cov3, matCovariance );
	pCPD->AttachMatrix( w30, matWeights,0 );

	ranges[0] = 2; 
	ranges[1] = 1;
	smData[0] = 0.0f;
	smData[1] = 0.0f;
	CNumericDenseMatrix<float> *mean4 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &smData.front());
	ranges[1] = 2;
	bigData[0] = 1.0f;
	bigData[1] = 0.0f;
	bigData[2] = 0.0f;
	bigData[3] = 1.0f;
	CNumericDenseMatrix<float> *cov4 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &bigData.front());
	bigData[2] = 1.0f;
	CNumericDenseMatrix<float> *w40 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &bigData.front());
	pCPD = pBNet->GetFactor(4);
	pCPD->AttachMatrix( mean4, matMean );
	pCPD->AttachMatrix( cov4, matCovariance );
	pCPD->AttachMatrix( w40, matWeights,0 );

	//Generate random evidences for the modes
	int nEv = 1000;
	pEvidencesVector evid;
	pBNet->GenerateSamples( &evid, nEv );
	/////////////////////////////////////////////////////////////////////
		
	//Create copy of initial model with random matrices 
	CGraph *pGraphCopy = CGraph::Copy(pGraph); 
	CBNet *pLearnBNet = CBNet::CreateWithRandomMatrices(pGraphCopy, pBNet->GetModelDomain() );
	
	// Creating learning process	
	CEMLearningEngine *pLearn = CEMLearningEngine::Create(pLearnBNet);

	pLearn->SetData(nEv, &evid.front());
	pLearn->Learn();
	CNumericDenseMatrix<float> *pMatrix;
	int length = 0;
	const float *output;
	
	///////////////////////////////////////////////////////////////////////
	std::cout<<" results of learning (number of evidences = "<<nEv<<std::endl;
	for (i = 0; i < nnodes; i++ )
	{
	    int j;
	    std::cout<<"\n matrix mean for node "<<i;
	    std::cout<<"\n initial BNet \n";
	    pMatrix = static_cast<CNumericDenseMatrix<float>*>
		(pBNet->GetFactor(i)->GetMatrix(matMean));
	    pMatrix->GetRawData(&length, &output);
	    for ( j = 0; j < length; j++ )
	    {
		std::cout<<" "<<output[j];
	    }
	    std::cout<<"\n BNet with random matrices after learning \n ";
	    pMatrix = static_cast<CNumericDenseMatrix<float>*>
		(pLearnBNet->GetFactor(i)->GetMatrix(matMean));
	    pMatrix->GetRawData(&length, &output);
	    for ( j = 0; j < length; j++)
	    {
		std::cout<<" "<<output[j];
	    }
	    
    	    std::cout<<"\n \n matrix covariance for node "<<i<<'\n';
	    std::cout<<"\n initial BNet \n";

	    pMatrix = static_cast<CNumericDenseMatrix<float>*>
		(pBNet->GetFactor(i)->GetMatrix(matCovariance));
	    pMatrix->GetRawData(&length, &output);
	    for (j = 0; j < length; j++ )
	    {
		std::cout<<" "<<output[j];
	    }
    	    std::cout<<"\n BNet with random matrices after learning \n ";
	    pMatrix = static_cast<CNumericDenseMatrix<float>*>
		(pLearnBNet->GetFactor(i)->GetMatrix(matCovariance));
	    pMatrix->GetRawData(&length, &output);
	    for ( j = 0; j < length; j++ )
	    {
		std::cout<<" "<<output[j];
	    }

	    std::cout<<"\n ___________________________\n";
	    
	}
	
	
	for( i = 0; i < nEv; i++)
	{
	    delete evid[i];
	}
	delete pLearn;
	delete pLearnBNet;
	delete pBNet;
	
	

return 1;
}
Exemple #8
0
PNL_USING
int main()
{
    int nnodes = 16;
    int nodeslice = 8;
    CBNet* pKjaerulf = pnlExCreateKjaerulfsBNet();
    CDBN*  pKj = CDBN::Create(pKjaerulf);

    int nSeries = 50;
    int	nslices = 101;
    int i;

    intVector nS(nSeries);
    for(i = 0; i < nSeries; i++)
    {
        nS[i] = nslices;
    }

    valueVector vValues;
    vValues.resize(nodeslice);
    intVector obsNodes(nodeslice);
    for( i=0; i<nodeslice; i++)obsNodes[i] = i;
    CEvidence ***pEv;
    pEv = new CEvidence **[nSeries];

    int series, slice, node, val;

    FILE * fp;
    fp = fopen("../Data/kjaerulff.dat", "r");

    if( !fp )
    {
        std::cout<<"can't open cases file"<<std::endl;
        exit(1);
    }

    for( series = 0; series < nSeries; series++ )
    {
        pEv[series] = new CEvidence*[ nslices ];
        for( slice = 0;  slice < nslices; slice++ )
        {
            for( node = 0; node < nodeslice; node++)
            {
                fscanf(fp, "%d,", &val);
		vValues[node].SetFlt(val);
            }
            (pEv[series])[slice] = CEvidence::Create(pKj->GetModelDomain(), obsNodes,  vValues );
        }

    }
    fclose(fp);

    CGraph *pGraph = CGraph::Create(nnodes, NULL, NULL, NULL);
    for(i=0; i<nnodes-1; i++)
    {
	pGraph->AddEdge(i, i+1,1);
    }
    CNodeType *nodeTypes = new CNodeType[1];

    nodeTypes[0].SetType(1, 2);
    int *nodeAssociation = new int[nnodes];

    for ( i = 0; i < nnodes; i++ )
    {
	nodeAssociation[i] = 0;
    }
    CBNet *pBnet = CBNet::Create( nnodes, 1, nodeTypes, nodeAssociation, pGraph );
    pBnet -> AllocFactors();
    floatVector data;
    data.assign(64, 0.0f);

    for ( node = 0; node < nnodes; node++ )
    {
	pBnet->AllocFactor( node );
	(pBnet->GetFactor( node )) ->AllocMatrix( &data.front(), matTable );
    }

    CDBN*  pDBN = CDBN::Create(pBnet);

    CMlDynamicStructLearn *pLearn = CMlDynamicStructLearn::Create(pDBN, itDBNStructLearnML,
	StructLearnHC, BIC, 4, 1, 30);

    pLearn -> SetData(nSeries, &nS.front(), pEv);
    //	pLearn->SetLearnPriorSlice(true);
    //	pLearn->SetMinProgress((float)1e-4);
    pLearn ->Learn();
    const CDAG* pDAG = pLearn->GetResultDag();
    pDAG->Dump();
    ////////////////////////////////////////////////////////////////////////////
    delete pLearn;
    delete pDBN;
    delete pKj;
    for(series = 0; series < nSeries; series++ )
    {
        for( slice = 0;  slice < nslices; slice++ )
        {
            delete (pEv[series])[slice];
        }
	delete[] pEv[series];
    }
    delete[] pEv;
    return 0;
}