content: /*
   *
   *
   *    Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
   *    See the NOTICE file distributed with this work for additional information regarding copyright ownership.
   *    The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
   *    this file except in compliance with the License.  You may obtain a copy of the License at
   *
   *            http://www.apache.org/licenses/LICENSE-2.0
   *
   *    Unless required by applicable law or agreed to in writing, software distributed under the License is
   *    distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *    See the License for the specific language governing permissions and limitations under the License.
   *
   *
   */
  
  package eu.amidst.core.learning;
  
  import eu.amidst.core.Main;
  import eu.amidst.core.datastream.DataInstance;
  import eu.amidst.core.datastream.DataStream;
  import eu.amidst.core.exponentialfamily.EF_BayesianNetwork;
  import eu.amidst.core.io.BayesianNetworkLoader;
  import eu.amidst.core.learning.parametric.ParallelMaximumLikelihood;
  import eu.amidst.core.models.BayesianNetwork;
  import eu.amidst.core.utils.BayesianNetworkSampler;
  import eu.amidst.core.variables.Variable;
  import org.junit.Assert;
  import org.junit.Test;
  
  import java.io.IOException;
  
  /**
   * Created by ana@cs.aau.dk on 22/01/15.
   *
   */
  public class MLNormalsTest {
  
      //TODO: Test more than 1 parent
  
      @Test
      public void testingML_NormalNormal1Parent() throws IOException, ClassNotFoundException  {
  
  
          BayesianNetwork testnet = BayesianNetworkLoader.loadFromFile("../networks/simulated/Normal_1NormalParents.bn");
          if (Main.VERBOSE) System.out.println("\nNormal_withOneNormalParent network \n ");
  
          //Sampling
          BayesianNetworkSampler sampler = new BayesianNetworkSampler(testnet);
          sampler.setSeed(0);
          DataStream<DataInstance> data = sampler.sampleToDataStream(10000);
  
  
          //Parameter Learning
          ParallelMaximumLikelihood parallelMaximumLikelihood = new ParallelMaximumLikelihood();
          parallelMaximumLikelihood.setWindowsSize(1000);
          parallelMaximumLikelihood.setParallelMode(true);
          parallelMaximumLikelihood.setLaplace(false);
          parallelMaximumLikelihood.setDAG(testnet.getDAG());
          parallelMaximumLikelihood.initLearning();
          parallelMaximumLikelihood.updateModel(data);
          BayesianNetwork bnet = parallelMaximumLikelihood.getLearntBayesianNetwork();
  
  
  
          EF_BayesianNetwork ef_testnet = new EF_BayesianNetwork(testnet);
  
          EF_BayesianNetwork ef_bnet = new EF_BayesianNetwork(bnet);
  
          Assert.assertTrue(ef_bnet.equal_efBN(ef_testnet, 0.05));
      }
  
      @Test
      public void testingML_GaussiansTwoParents() throws  IOException, ClassNotFoundException {
  
          BayesianNetwork testnet = BayesianNetworkLoader.loadFromFile("../networks/simulated/Normal_NormalParents.bn");
          if (Main.VERBOSE) System.out.println("\nNormal_withTwoNormalParents network \n ");
  
          //Sampling
          BayesianNetworkSampler sampler = new BayesianNetworkSampler(testnet);
          sampler.setSeed(0);
          DataStream<DataInstance> data = sampler.sampleToDataStream(10000);
  
  
          //Parameter Learning
          ParallelMaximumLikelihood parallelMaximumLikelihood = new ParallelMaximumLikelihood();
          parallelMaximumLikelihood.setWindowsSize(1000);
          parallelMaximumLikelihood.setParallelMode(true);
          parallelMaximumLikelihood.setLaplace(false);
          parallelMaximumLikelihood.setDAG(testnet.getDAG());
          parallelMaximumLikelihood.initLearning();
          parallelMaximumLikelihood.updateModel(data);
          BayesianNetwork bnet = parallelMaximumLikelihood.getLearntBayesianNetwork();
  
  
          //Check the probability distributions of each node
          for (Variable var : testnet.getVariables()) {
              if (Main.VERBOSE) System.out.println("\n------ Variable " + var.getName() + " ------");
              if (Main.VERBOSE) System.out.println("\nTrue distribution:\n"+ testnet.getConditionalDistribution(var));
              if (Main.VERBOSE) System.out.println("\nLearned distribution:\n"+ bnet.getConditionalDistribution(var));
              Assert.assertTrue(bnet.getConditionalDistribution(var).equalDist(testnet.getConditionalDistribution(var), 0.05));
          }
  
          //Or check directly if the true and learned networks are equals
          Assert.assertTrue(bnet.equalBNs(testnet, 0.05));
      }
  
  }