Marginal calculation

61 views
Skip to first unread message

Julian Kreis

unread,
Jul 24, 2017, 11:03:32 AM7/24/17
to libDAI
I am a beginner in probabilistic graphical models and I wanted to validate my settings in libDAI with a frequently used example for the prediction of student intelligence. For this, I build the graph within a framework software called SAMIAM and within a libDAI script. When I compare the marginals of libDAI with marginals of SAMIAM I don't get the same results. Could someone give me a hint what I am doing wrong within my libDAI script? After clamping evidence data I also don't get the same results between the two approaches.

My factor graph output:

    5
    
    1
    1 
    2 
    2
    0 0.7
    1 0.3
    
    1
    0 
    2 
    2
    0 0.6
    1 0.4
    
    3
    0 1 2 
    2 2 3 
    12
    0 0.3
    1 0.9
    2 0.05
    3 0.5
    4 0.4
    5 0.08
    6 0.25
    7 0.3
    8 0.3
    9 0.02
    10 0.7
    11 0.2
    
    2
    1 3 
    2 2 
    4
    0 0.95
    1 0.05
    2 0.2
    3 0.8
    
    2
    2 4 
    3 2 
    6
    0 0.1
    1 0.9
    2 0.4
    3 0.6
    4 0.99
    5 0.01

My script to build the graph and calculate marginals:

    #include <dai/factorgraph.h>
    #include <dai/util.h>
    #include <dai/alldai.h>
    #include <dai/varset.h>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    using namespace dai;
    
    int StudentTest() {
      
      Var difficulty(0, 2);  
      Var intelligence(1, 2);  
      Var grade(2, 3);  
      Var sat(3, 2);  
      Var letter(4, 2);  
      
      
      Factor P_difficulty( difficulty );
      P_difficulty.set(0, 0.6);   
      P_difficulty.set(1, 0.4); 
      
      Factor P_intelligence( intelligence );
      P_intelligence.set(0, 0.7);   
      P_intelligence.set(1, 0.3); 
      
      
      Factor P_sat_given_intelligence( VarSet( sat, intelligence ) );
      P_sat_given_intelligence.set(0, 0.95);     //  0  0
      P_sat_given_intelligence.set(1, 0.05);     //  1  0
      P_sat_given_intelligence.set(2, 0.20);     //  0  1
      P_sat_given_intelligence.set(3, 0.80);     //  1  1
      
      Factor P_letter_given_grade( VarSet( letter, grade ) );
      P_letter_given_grade.set(0, 0.1);     //  0  0
      P_letter_given_grade.set(1, 0.9);     //  1  0
      P_letter_given_grade.set(2, 0.4);     //  0  1
      P_letter_given_grade.set(3, 0.6);     //  1  1
      P_letter_given_grade.set(4, 0.99);    //  0  2
      P_letter_given_grade.set(5, 0.01);    //  1  2
      
      Factor P_grade_given_intelligence_difficulty( VarSet( intelligence, difficulty ) | grade );
      P_grade_given_intelligence_difficulty.set(0, 0.3);     //  0  0  0
      P_grade_given_intelligence_difficulty.set(1, 0.9);     //  1  0  0
      P_grade_given_intelligence_difficulty.set(2, 0.05);    //  0  1  0
      P_grade_given_intelligence_difficulty.set(3, 0.5);     //  1  1  0
      P_grade_given_intelligence_difficulty.set(4, 0.4);     //  0  0  1
      P_grade_given_intelligence_difficulty.set(5, 0.08);    //  1  0  1
      P_grade_given_intelligence_difficulty.set(6, 0.25);    //  0  1  1
      P_grade_given_intelligence_difficulty.set(7, 0.3);     //  1  1  1
      P_grade_given_intelligence_difficulty.set(8, 0.3);     //  0  0  2
      P_grade_given_intelligence_difficulty.set(9, 0.02);    //  1  0  2
      P_grade_given_intelligence_difficulty.set(10, 0.7);    //  0  1  2
      P_grade_given_intelligence_difficulty.set(11, 0.2);    //  1  1  2
      
      // Build factor graph consisting of those four factors
      vector<Factor> student;
      student.push_back( P_intelligence );
      student.push_back( P_difficulty );
      student.push_back( P_grade_given_intelligence_difficulty );
      student.push_back( P_sat_given_intelligence );
      student.push_back( P_letter_given_grade );
      FactorGraph studentTest( student );
      
      // Write factorgraph to a file
      studentTest.WriteToFile( "student.fg" );
      cout << "Student network written to student.fg" << endl;
      
      // Output some information about the factorgraph
      cout << studentTest.nrVars() << " variables" << endl;
      cout << studentTest.nrFactors() << " factors" << endl;
      
      // Prepare junction-tree object for doing exact inference for E-step
      PropertySet infprops;
      
      infprops.set( "verbose", (size_t)1 );
      infprops.set( "updates", string("HUGIN") );
      infprops.set( "inference", string("SUMPROD") );
      InfAlg* inf = newInfAlg( "JTREE", studentTest, infprops );
      inf->init();
      inf->run();
      
      // Report variable marginals for fg, calculated by the junction tree algorithm
      cout << "Exact variable marginals after run (JT, HUGIN, SUMPROD):" << endl;
      for( size_t i = 0; i < studentTest.nrVars(); i++ ) {// iterate over all variables in fg
        cout << inf->belief(studentTest.var(i)) << endl; // display the "belief" of jt for that variable
      }
      return 0;
    }


Marginals from libDAI script:

    ({x0}, (0.645203, 0.354797))
    ({x1}, (0.777352, 0.222648))
    ({x2}, (0.344096, 0.541993, 0.113911))
    ({x3}, (0.655257, 0.344743))
    ({x4}, (0.418381, 0.581619))

The Graph within SAMIAM gives me:
see attached picture



I also posted the question on stack overflow, because I didn't know which platform is the better one for this question.
Bildschirmfoto 2017-07-24 um 12.10.05.png
Reply all
Reply to author
Forward
0 new messages