Package

Source Code of LW_Test

import java.util.Map;

import edu.gmu.seor.prognos.unbbayesplugin.cps.CPSCompilerMain;
import edu.gmu.seor.prognos.unbbayesplugin.cps.lw.LikelihoodWeighting;
import edu.gmu.seor.prognos.unbbayesplugin.jt.util.Debug;


public class LW_Test {

  public LW_Test() {
    Debug.setDebug(true);
  }

  /*
  %             B
  %             |
  %             A  (X)  (Y)
  %              \  |   /
  %               \ |  /
  %                \| /
  %                (W)
  % */
  String mk_hybridTest_1d1d2c_bnet = new String(
    "defineNode(B, DescriptionA);"+
    "{ defineState(Discrete, b1, b2);" +
    "p( B ) = { b1:0.8; b2:0.2; } }" +
    
    "defineNode(A, DescriptionA);"+
    "{ defineState(Discrete, a1, a2);" +
    "p( A | B ) = if( B == b1  ){ a1:0.8; a2:0.2; } " +
    " else if( B == b2  ){ a1:0.3; a2: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, X, Y ) = if( A == a1  ){ X - 0.5*Y + NormalDist( 2, 1 ); } " +
    " else if( A == a2  ){ X+Y + NormalDist( -1, 0.5 ); } " +
    "}" );
 
  /*
  %             B C
  %             |/
  %             A  (X)  (Y)
  %              \  |   /
  %               \ |  /
  %                \| /
  %                (W)
  % */
  String mk_hybridTest_2d1d2c_bnet = new String(
    "defineNode(C, DescriptionC);"+
    "{ defineState(Discrete, c1, c2);" +
    "p( C ) = { c1:0.5; c2:0.5; } }"+
   
    "defineNode(B, DescriptionA);"+
    "{ defineState(Discrete, b1, b2);" +
    "p( B ) = { b1:0.8; b2:0.2; } }" +
    
    "defineNode(A, DescriptionA);"+
    "{ defineState(Discrete, a1, a2);" +
    "p( A | B, C ) = if( B == b1 && C == c1  ){ a1:0.8; a2:0.2; } " +
    " else if( B == b1 && C == c2 ){ a1:0.3; a2:0.7; } " +
    " else if( B == b2 && C == c1 ){ a1:0.4; a2:0.6; } " +
    " else if( B == b2 && C == c2 ){ a1:0.2; a2:0.8; } " +
    "}" +
   
    "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 ); } " +
    "}" );

  /* 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 ); } " +
      "}" );
 
  public void test() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_2d1d2c_bnet  +
                  "defineEvidence( C, c1 );"+
                  "run(LW, 100);" );
       
       
        //for printing all information
      forint i = 0; i < LikelihoodWeighting.This().sampleList.size(); i++ ){
         Map<String, String> mapSample = LikelihoodWeighting.This().sampleList.get(i);
        System.out.println( mapSample.toString());
      }      
    }
 
  /*
        *+++ Results by JunctionTree for original model:
    posterior marginal of B: [0.3 0.7]
    X.bel - mu: -3, Sigma: 1.
    Y.bel - mu: 3, Sigma: 0.5.
    W.bel - mu: 1.35, Sigma: 1.6775.
   
       *+++ Results by Likelihood Weighting for original model:
    posterior marginal of B: [0.3 0.7]
    X.bel - mu: -3.0148, Sigma: 0.970234.
    Y.bel - mu: 3.01022, Sigma: 0.506678.
    W.bel - mu: 1.3579, Sigma: 1.78179.
   */
  public void test2() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_2d2c_bnet  +
                  "defineEvidence( A, 2 );"+
                         "run(LW, 1000);" );
               
    }
 
  //evids{5} = 2.1 ; % for bnet = mk_hybridTest_2d2c_bnet('orig') ;
  /*
        *+++ Results by JunctionTree for original model:
    posterior marginal of A: [0.539187 0.460813]
    posterior marginal of B: [0.1031 0.8969]
    X.bel - mu: -2.35272, Sigma: 0.740484.
    Y.bel - mu: 2.85225, Sigma: 0.476438.
   
       *+++ Results by Likelihood Weighting for original model:
    posterior marginal of A: [0.547134 0.452866]
    posterior marginal of B: [0.0651447 0.934855]
    X.bel - mu: -2.38967, Sigma: 0.612017.
    Y.bel - mu: 2.79354, Sigma: 0.554956. 
   */
 
  public void test1() {
    CPSCompilerMain cpsCompiler = new CPSCompilerMain();
        cpsCompiler.InitCompiler();
        cpsCompiler.compile( mk_hybridTest_2d2c_bnet  +
                  "defineEvidence( W, 2.1 );"+
                         "run(LW, 1000);" );
               
    }
 
  //evids{5} = 2.1 ; % for bnet = mk_hybridTest_2d2c_bnet('orig') ;
  //evids{1} = 2 ; % for bnet = mk_hybridTest_2d2c_bnet('orig') ;
  /*
       *+++ Results by JunctionTree for original model:
    posterior marginal of B: [0.215536 0.784464]
    X.bel - mu: -2.71689, Sigma: 0.763656.
    Y.bel - mu: 3.23569, Sigma: 0.321621.
   
       *+++ Results by Likelihood Weighting for original model:
    posterior marginal of B: [0.228588 0.771412]
    X.bel - mu: -2.66079, Sigma: 0.74823.
    Y.bel - mu: 3.20887, Sigma: 0.334082.
   */

  //evids{3} = -2 ; % for bnet = mk_hybridTest_2d2c_bnet('orig') ;
  //evids{1} = 2 ; % for bnet = mk_hybridTest_2d2c_bnet('orig') ;
  /*
      *+++ Results by JunctionTree for original model:
    posterior marginal of B: [0.3 0.7]
    Y.bel - mu: 3, Sigma: 0.5.
    W.bel - mu: 2, Sigma: 1.15.
   
       *+++ Results by Likelihood Weighting for original model:
    posterior marginal of B: [0.281 0.719]
    Y.bel - mu: 2.997, Sigma: 0.495642.
    W.bel - mu: 1.9833, Sigma: 1.06181.
   */
 
  //evids{5} = 2.1 ; % for bnet = mk_hybridTest_2d2c_bnet('orig') ;
  //evids{3} = -2 ; % for bnet = mk_hybridTest_2d2c_bnet('orig') ;
  //evids{1} = 2 ; % for bnet = mk_hybridTest_2d2c_bnet('orig') ;
  /*
      *+++ Results by JunctionTree for original model:
  posterior marginal of B: [0.259539 0.740461]
  Y.bel - mu: 3.04567, Sigma: 0.271682.
 
     *+++ Results by Likelihood Weighting for original model:
  posterior marginal of B: [0.270936 0.729064]
  Y.bel - mu: 3.04538, Sigma: 0.260848.
   */
 
  //evids(8:9) = {0.7 2.2}  % for bnet = mk_hybridTest_polyCLG_bnet('orig').
  /*
 
       *+++ Results by JunctionTree for original model:
    posterior marginal of A: [0.527795 0.472205]
    posterior marginal of B: [0.00109026 0.99891]
    posterior marginal of C: [0.405559 0.594441]
    U.bel - mu: 29.9768, Sigma: 0.983337.
    X.bel - mu: 9.94204, Sigma: 1.14586.
    Y.bel - mu: 3.21621, Sigma: 0.846893.
    W.bel - mu: 3.54433, Sigma: 1.58967.
   
       *+++ Results by Likelihood Weighting for original model:
    posterior marginal of A: [0.56631 0.43369]
    posterior marginal of B: [0.000587292 0.999413]
    posterior marginal of C: [0.37836 0.62164]
    U.bel - mu: 29.9825, Sigma: 0.908088.
    X.bel - mu: 9.963, Sigma: 1.20749.
    Y.bel - mu: 3.17019, Sigma: 0.863995.
    W.bel - mu: 3.54458, Sigma: 1.60773.
   */
  /**
   * @param args
   */
  public static void main(String[] args) {
    LW_Test t = new LW_Test();
    t.test();
    //t.test1(); 
    //t.test2(); 
  }

}
TOP

Related Classes of LW_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.