Package

Source Code of Hybrid_Test

import unbbayes.util.Debug;
import edu.gmu.seor.prognos.unbbayesplugin.cps.CPSCompilerMain;
import edu.gmu.seor.prognos.unbbayesplugin.cps.datastructure.EDB;


public class Hybrid_Test  extends Test{
  public Hybrid_Test() { 
    Debug.setDebug(true);
    EDB.This().printSet(true);
  }
 
  /*  % mk_hybridTest_1d2c_bnet creates an example CLG of polytree, in which one
    % continuous node W has one discrete parent A, and two continuous
    % parents X, and Y, shown as below:
    %
   
    %             A  (X)  (Y)
    %              \  |   /
    %               \ |  /
    %                \| /
    %                (W)
    %               
    %
    % A is the discrete nodes.
    % X, Y, W, are continuous nodes.
    % W has one discrete parents and two continuous parents
    % */
  String mk_hybridTest_1d2c_bnet = new String(
      "defineNode(A, DescriptionA);"+
      "{ defineState(Discrete, a1, a2);" +
      "p( A ) = { a1:0.8; a2:0.2; } }" +
      
      "defineNode(X, DescriptionX);"+
        "{ defineState(Continuous);" +
        "p( X ) = NormalDist( -3, 1 ); }" +
     
        "defineNode(Y, DescriptionY);"+
        "{ defineState(Continuous);" +
        "p( Y ) = NormalDist( 3, 0.5 ); }" +
      
      "defineNode(W, DescriptionW);"+
      "{ defineState(Continuous);" +
      "p( W | A, X, Y ) = if( A == a1  ){ X - 0.5*Y + NormalDist( 2, 1 ); } " +
      " else if( A == a2  ){ X+Y + NormalDist( -1, 0.5 ); } " +
      "}" );
 
 
    String mk_hybridTest_1d2c_bnet_2 = new String(
      "defineNode(A, DescriptionA);"+
      "{ defineState(Discrete, a1, a2, a3);" +
      "p( A ) = { a1:0.8; a2:0.1; a3:0.1;  } }" +
      
      "defineNode(X, DescriptionX);"+
        "{ defineState(Continuous);" +
        "p( X ) = NormalDist( -3, 1 ); }" +
     
        "defineNode(Y, DescriptionY);"+
        "{ defineState(Continuous);" +
        "p( Y ) = NormalDist( 3, 0.5 ); }" +
      
      "defineNode(W, DescriptionW);"+
      "{ defineState(Continuous);" +
      "p( W | A, X, Y ) = if( A == a1  ){ X - 0.5*Y + NormalDist( 2, 1 ); } " +
      " else if( A == a2  ){ X+Y + NormalDist( -1, 0.5 ); } " +
      " else if( A == a3  ){ X+Y + NormalDist( 10, 0.5 ); } " +
      "}" );
 
 
  /* mk_hybridTest_2d2c_bnet
  % mk_CLG_polytree_bnet creates an example CLG of polytree, in which one
  % continuous node W has two discrete parents A, and B, and two continuous
  % parents X, and Y, shown as below:
  %
  %           A  B (X)  (Y)
  %            \ \  |   /
  %             \ \ |  /
  %              \ \| /
  %                (W)
  %               
  %
  % A, B, are discrete nodes.
  % X, Y, W, are continuous nodes.
  % W has two discrete parents and two continuous parents
 
    bnet.CPD{A} = tabular_CPD(bnet, A, [0.8 0.2]);
      bnet.CPD{B} = tabular_CPD(bnet, B, [0.3 0.7]);    
      bnet.CPD{X} = gaussian_CPD(bnet, X, 'mean', -3, 'cov', 1) ;
      bnet.CPD{Y} = gaussian_CPD(bnet, Y, 'mean', 3, 'cov', 0.5) ;
      bnet.CPD{W} = gaussian_CPD(bnet, W, 'mean', [2 1 -2 0], ...
            'cov', [1 1 0.5 0.5], 'weights', [1.0 -0.5 1.0 1.0 -1.0 1.0 0.5 1.0], ...
            'function', {sym('X - 0.5*Y'),sym('X+Y'),sym('-X+Y'),sym('0.5*X+Y')}) ;         
  */
  String mk_hybridTest_2d2c_bnet = new String(
      "defineNode(A, DescriptionA);"+
      "{ defineState(Discrete, 1, 2);" +
      "p( A ) = { 1:0.8; 2:0.2; }; }" +
     
      "defineNode(B, DescriptionB);"+
      "{ defineState(Discrete, 1, 2);" +
      "p( B ) = { 1:0.3; 2:0.7; } }" +
     
      "defineNode(X, DescriptionX);"+
        "{ defineState(Continuous);" +
        "p( X ) = NormalDist( -3, 1 ); }" +
     
        "defineNode(Y, DescriptionY);"+
        "{ defineState(Continuous);" +
        "p( Y ) = NormalDist( 3, 0.5 ); }" +
      
      "defineNode(W, DescriptionW);"+
      "{ defineState(Continuous);" +
      "p( W | A, B, X, Y ) = if( A == 1 && B == 1 ){ X - 0.5*Y + NormalDist( 2, 1 ); } " +
      " else if( A == 2 && B == 1 ){ X+Y + NormalDist( 1, 1 ); } " +
      " else if( A == 1 && B == 2 ){ -1*X+Y + NormalDist( -2, 0.5 ); } " +
      " else if( A == 2 && B == 2  ){ 0.5*X+Y + NormalDist( 0, 0.5 ); } " +
      "}" );
 
  /*  % mk_hybridTest_3pdp_bnet creates an example CLG of polytree, consisting of
    % one continuous node with 3 pure discrete parents, shown as below:
    %
    %             A    B    C 
    %              \   |   /
    %               \  |  /
    %                \ | /
    %                 (Z)
    %               
    %
    % A, B, and C are discrete nodes.
    % Z is continuous nodes.
    %*/
  String mk_hybridTest_3pdp_bnet = new String(
      "defineNode(A, DescriptionA);"+
      "{ defineState(Discrete, 1, 2);" +
      "p( A ) = { 1:0.8; 2:0.2; } }" +
     
      "defineNode(B, DescriptionB);"+
      "{ defineState(Discrete, 1, 2);" +
      "p( B ) = { 1:0.3; 2:0.7; } }" +
     
      "defineNode(C, DescriptionB);"+
      "{ defineState(Discrete, 1, 2);" +
      "p( C ) = { 1:0.6; 2:0.4; } }" +
      
      "defineNode(Z, DescriptionZ);"+
      "{ defineState(Continuous);" +
      "p( Z | A, B, C ) = if( A == 1 && B == 1 && C == 1 ){ NormalDist( 3, 1 ); } " +
      " else if( A == 2 && B == 1 && C == 1 ){ NormalDist( -1, 1 ); } " +
      " else if( A == 1 && B == 2 && C == 1 ){ NormalDist( 0, 1 ); } " +
      " else if( A == 2 && B == 2 && C == 1 ){ NormalDist( 1, 0.5 ); } " +
      " else if( A == 1 && B == 1 && C == 2 ){ NormalDist( 2, 1 ); } " +
      " else if( A == 2 && B == 1 && C == 2 ){ NormalDist( -2, 1 ); } " +
      " else if( A == 1 && B == 2 && C == 2 ){ NormalDist( 0, 1 ); } " +
      " else if( A == 2 && B == 2 && C == 2 ){ NormalDist( 1, 0.5 ); } " +
      "}" );
 
  /*  % mk_CLG_polytree_bnet creates an example CLG of polytree.
    % shown as following:
    %
    %                (U)
    %                 |
    %                 |
    %                 |
    %           A  B (X)  (Y)
    %          /|\ \  |   /
    %         / | \ \ |  /
    %        /  |  \ \| /
    %       C  (Z)   (W)
    %                 |
    %                 |
    %                (E)
    %               
    %
    % A, B, and C are discrete nodes.
    % U, X, Y, W, Z and E are continuous nodes.
    % W has two discrete parents and two continuous parents
   */
 
  String mk_hybridTest_polyCLG_bnet = new String(
      "defineNode(A, DescriptionA);"+
      "{ defineState(Discrete, a1, a2);" +
      "p( A ) = { a1:0.8; a2:0.2; } }" +
     
      "defineNode(B, DescriptionB);"+
      "{ defineState(Discrete, b1, b2);" +
      "p( B ) = { b1:0.5; b2:0.5; } }" +
     
      "defineNode(C, DescriptionC);"+
      "{ defineState(Discrete, c1, c2);" +
      "p( C | A ) = if( A == a1 ) { c1:0.5; c2:0.5; } " +
      "else if( A ==  a2 ){ c1:0.3; c2:0.7; } " +
      "}" +
     
      "defineNode(U, DescriptionU);"+
        "{ defineState(Continuous);" +
        "p( U ) = NormalDist( 30, 1 ); }" +
     
        "defineNode(X, DescriptionX);"+
        "{ defineState(Continuous);" +
        "p( X | U ) = 0.5 * U + NormalDist( -5, 1 ); }" +
      
        "defineNode(Y, DescriptionY);"+
        "{ defineState(Continuous);" +
        "p( Y ) = NormalDist( 3, 1 ); }" +
       
      "defineNode(W, DescriptionW);"+
      "{ defineState(Continuous);" +
      "p( W | A, B, X, Y ) = if( A == a1 && B == b1 ){ X - 0.5*Y + NormalDist( 5, 1 ); } " +
      " else if( A == a2 && B == b1 ){ X+Y + NormalDist( 20, 2 ); } " +
      " else if( A == a1 && B == b2 ){ -1*X+Y + NormalDist( 10, 1 ); } " +
      " else if( A == a2 && B == b2  ){ 0.5*X+Y + NormalDist( -5, 0.5 ); } " +
     
      "defineNode(Z, DescriptionZ);"+
      "{ defineState(Continuous);" +
      "p( Z | A ) = if( A == a1 ) { NormalDist( 3, 1 ); } " +
      " else if( A == a2 ){ NormalDist( -1, 1 ); } " +
      "}" +
     
        "defineNode(E, DescriptionE);"+
        "{ defineState(Continuous);" +
        "p( E | W ) = 0.5 * W + NormalDist( 0, 1 ); }" +
 
  "}" );
 
  /*  % mk_CLG_polytree_bnet creates an example CLG of polytree.
  % shown as following:
  %
  %                (U)
  %                 |
  %                 |
  %                 |
  %           A  B (X)  (Y)
  %          /|\ \  |   /
  %         / | \ \ |  /
  %        /  |  \ \| /
  %       C  (Z)   (W)
  %                 |
  %                 |
  %                (E)
  %               
  %
  % A, B, and C are discrete nodes. !!! B has 3 states
  % U, X, Y, W, Z and E are continuous nodes.
  % W has two discrete parents and two continuous parents
*/

  String mk_hybridTest_polyCLG_bnet_2 = new String(
    "defineNode(A, DescriptionA);"+
    "{ defineState(Discrete, a1, a2);" +
    "p( A ) = { a1:0.8; a2:0.2; } }" +
   
    "defineNode(B, DescriptionB);"+
    "{ defineState(Discrete, b1, b2, b3);" +
    "p( B ) = { b1:0.1; b2:0.2; b3:0.7; } }" +
   
    "defineNode(C, DescriptionC);"+
    "{ defineState(Discrete, c1, c2);" +
    "p( C | A ) = if( A == a1 ) { c1:0.5; c2:0.5; } " +
    "else if( A ==  a2 ){ c1:0.3; c2:0.7; } " +
    "}" +
   
    "defineNode(U, DescriptionU);"+
      "{ defineState(Continuous);" +
      "p( U ) = NormalDist( 30, 1 ); }" +
   
      "defineNode(X, DescriptionX);"+
      "{ defineState(Continuous);" +
      "p( X | U ) = 0.5 * U + NormalDist( -5, 1 ); }" +
    
      "defineNode(Y, DescriptionY);"+
      "{ defineState(Continuous);" +
      "p( Y ) = NormalDist( 3, 1 ); }" +
     
    "defineNode(W, DescriptionW);"+
    "{ defineState(Continuous);" +
    "p( W | A, B, X, Y ) = if( A == a1 && B == b1 ){ X - 0.5*Y + NormalDist( 5, 1 ); } " +
    " else if( A == a2 && B == b1 ){ X+Y + NormalDist( 20, 2 ); } " +
    " else if( A == a1 && B == b2 ){ -1*X+Y + NormalDist( 10, 1 ); } " +
    " else if( A == a2 && B == b2  ){ 0.5*X+Y + NormalDist( -5, 0.5 );" +
    " else if( A == a1 && B == b3 ){ -1*X+Y + NormalDist( 10, 1 ); } " +
    " else if( A == a2 && B == b3  ){ 0.5*X+Y + NormalDist( -5, 0.5 ); } " +
   
    "defineNode(Z, DescriptionZ);"+
    "{ defineState(Continuous);" +
    "p( Z | A ) = if( A == a1 ) { NormalDist( 3, 1 ); } " +
    " else if( A == a2 ){ NormalDist( -1, 1 ); } " +
    "}" +
   
      "defineNode(E, DescriptionE);"+
      "{ defineState(Continuous);" +
      "p( E | W ) = 0.5 * W + NormalDist( 0, 1 ); }" +

  "}" );

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Y_12_20_2011:
  // this test works well
  public void test_mk_hybridTest_2d2c_bnet() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_2d2c_bnet +
                   "defineEvidence( W, 2.1 );"+
                   "run(DMP);" );
       
        //Comparison
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.A.BEL").getNext("1").getData()0.539187 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.B.BEL").getNext("1").getData()0.1031 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.X.BEL.MU").getMatrixData(),  -2.35272 );
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.X.BEL.SIGMA").getMatrixData()0.740484 );
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.Y.BEL.MU").getMatrixData()2.85225 );
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.Y.BEL.SIGMA").getMatrixData(), 0.476438 );
    }
 
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Y_12_20_2011:
  // this test works well
  public void test_mk_hybridTest_2d2c_bnet_2() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_2d2c_bnet +
                   "defineEvidence( W, 2.1 );"+
                   "defineEvidence( Y, 1 );"+
                   "run(DMP);" );
       
        //Comparison
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.A.BEL").getNext("1").getData()0.987984 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.B.BEL").getNext("1").getData()0.0224057 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.X.BEL.MU").getMatrixData(),  -3.02003 );
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.X.BEL.SIGMA").getMatrixData()0.419442 );
    }
 
  public void test_mk_hybridTest_3pdp_bnet() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_3pdp_bnet +
                   "defineEvidence( Z, 1.2 );"+
                   "run(DMP);" );
       
        //Comparison
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.A.BEL").getNext("1").getData()0.657008 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.B.BEL").getNext("1").getData()0.179942 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.C.BEL").getNext("1").getData()0.548202 );   
    }
  
 
  public void test_mk_hybridTest_3pdp_bnet_2() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_3pdp_bnet +
                   "defineEvidence( C, 1 );"+
                   "run(DMP);");
       
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.Z.BEL.MU").getMatrixData()0.800000000000000 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.Z.BEL.SIGMA").getMatrixData()2.650000000000000 );       
    }
 
  public void test_mk_hybridTest_polyCLG_bnet() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile(
                   mk_hybridTest_polyCLG_bnet +
                   "defineEvidence( Z, 0.5 );"+
                   "defineEvidence( E, 3.0 );"+
                       "run(DMP);" );
       
        check( EDB.This().get("ROOT.ENGINES.DMP.NODES.A.BEL").getNext().getData()0.367007 );   
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.B.BEL").getNext().getData()0.00928248 );   
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.C.BEL").getNext().getData()0.373401 );   
    
        check( EDB.This().get("ROOT.ENGINES.DMP.NODES.U.BEL.MU").getMatrixData()30.0023 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.U.BEL.SIGMA").getMatrixData(), 1.00944 );
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.X.BEL.MU").getMatrixData()10.0058 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.X.BEL.SIGMA").getMatrixData()1.30898 );
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.Y.BEL.MU").getMatrixData()3.48009 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.Y.BEL.SIGMA").getMatrixData()0.843881 );
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.W.BEL.MU").getMatrixData()4.14392 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.W.BEL.SIGMA").getMatrixData()1.87595 );       
    }
 
  public void test_mk_hybridTest_polyCLG_bnet_2() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile
                   mk_hybridTest_polyCLG_bnet +
                   "defineEvidence( Y, 4 );"+
                   "run(DMP);" );
               
        check( EDB.This().get("ROOT.ENGINES.DMP.NODES.A.BEL").getNext().getData()0.8 );   
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.B.BEL").getNext().getData()0.5 );   
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.C.BEL").getNext().getData()0.46 );   
    
        check( EDB.This().get("ROOT.ENGINES.DMP.NODES.U.BEL.MU").getMatrixData()30.0  );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.U.BEL.SIGMA").getMatrixData(), 1.0  );
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.X.BEL.MU").getMatrixData()10.0  );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.X.BEL.SIGMA").getMatrixData()1.25 );
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.W.BEL.MU").getMatrixData()10.6 );   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.W.BEL.SIGMA").getMatrixData()81.0463 );
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.Z.BEL.MU").getMatrixData()2.2);   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.Z.BEL.SIGMA").getMatrixData()3.56 );  
      check( EDB.This().get("ROOT.ENGINES.DMP.NODES.E.BEL.MU").getMatrixData()5.3);   
    check( EDB.This().get("ROOT.ENGINES.DMP.NODES.E.BEL.SIGMA").getMatrixData()21.2616 );  
    }
 
  public void test7() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile
                   mk_hybridTest_polyCLG_bnet +
                   "defineEvidence( A, a1 );"+
                   "defineEvidence( Z, 2 );"+
                   "run(DMP);" );
               
    }
 
  public void test8() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile
                  mk_hybridTest_polyCLG_bnet_2 +
                   "defineEvidence( B, b1 );"+
                   "defineEvidence( Z, 2 );"+
                   "run(DMP);" );
               
     }
 
 
  public void test6() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_1d2c_bnet +
                   "defineEvidence( W, -2 );"+
                         "run(DMP);" );
    }
 
  public void test62() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_1d2c_bnet_2 +
                   "defineEvidence( W, -2 );"+
                         "run(DMP);" );
    }
 
  public void test_final() {
    test_mk_hybridTest_2d2c_bnet();
    test_mk_hybridTest_2d2c_bnet_2();
    test_mk_hybridTest_3pdp_bnet();
    test_mk_hybridTest_3pdp_bnet_2();
    test_mk_hybridTest_polyCLG_bnet();    
  }
 
  public void test_All() {
    test_final();
  }
 
  /**
   * @param args
   */
  public static void main(String[] args) { 
    Hybrid_Test t = new Hybrid_Test();
    t.test_mk_hybridTest_polyCLG_bnet();
  }

}
TOP

Related Classes of Hybrid_Test

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.