Package org.encog.ml.bayesian

Examples of org.encog.ml.bayesian.BayesianEvent


 
  public double probabilitySpam(String m) {
    List<String> words = separateSpaces(m);
   
    BayesianNetwork network = new BayesianNetwork();
    BayesianEvent spamEvent = network.createEvent("spam");
   
    int index = 0;
    for( String word: words) {
      BayesianEvent event = network.createEvent(word+index);
      network.createDependency(spamEvent, event);
      index++;
    }
   
    network.finalizeStructure();
   
    //SamplingQuery query = new SamplingQuery(network);
    EnumerationQuery query = new EnumerationQuery(network);
   
    CalcProbability messageProbability = new CalcProbability(this.k);
    messageProbability.addClass(SPAM_DATA.length);
    messageProbability.addClass(HAM_DATA.length);
    double probSpam = messageProbability.calculate(0);

    spamEvent.getTable().addLine(probSpam, true);
    query.defineEventType(spamEvent, EventType.Outcome);
    query.setEventValue(spamEvent, true);
       
    index = 0;
    for( String word: words) {
      String word2 = word+index;
      BayesianEvent event = network.getEvent(word2);
      event.getTable().addLine(this.spamBag.probability(word), true, true); // spam
      event.getTable().addLine(this.hamBag.probability(word), true, false); // ham
      query.defineEventType(event, EventType.Evidence);
      query.setEventValue(event, true);
      index++;
    }
View Full Code Here


      this.network.createEvent(v.getName(), c);
    }

    // define relations
    for (BIFDefinition d : this.bifDefinitions) {
      BayesianEvent childEvent = this.network.requireEvent(d
          .getForDefinition());
      for (String s : d.getGivenDefinitions()) {
        BayesianEvent parentEvent = this.network.requireEvent(s);
        this.network.createDependency(parentEvent, childEvent);
      }
    }

    this.network.finalizeStructure();
   
    // define probabilities
   
    for (BIFDefinition d : this.bifDefinitions) {
      double[] t = d.getTable();
      BayesianEvent childEvent = this.network.requireEvent(d.getForDefinition());
     
      int tableIndex = 0;
      int[] args = new int[childEvent.getParents().size()];
      do {
        for(int result = 0; result<childEvent.getChoices().size();result++) {
          childEvent.getTable().addLine(t[tableIndex++], result, args)
        }
      } while(BIFUtil.rollArgs(childEvent,args));
    }
  }
View Full Code Here

  public int[] getArgs(BayesianNetwork network) {
    int[] result = new int[givenEvents.size()];
       
    for(int i=0;i<givenEvents.size();i++) {
      ParsedEvent givenEvent = this.givenEvents.get(i);
      BayesianEvent actualEvent = network.getEvent(givenEvent.getLabel());
      result[i] = givenEvent.resolveValue(actualEvent);
    }
   
    return result;
  }
View Full Code Here

   * @param result The resulting probability.
   */
  public void defineTruthTable(BayesianNetwork network, double result) {
   
    ParsedEvent childParsed = getChildEvent();
    BayesianEvent childEvent = network.requireEvent(childParsed.getLabel());
   
    // define truth table line
    int[] args = getArgs(network);
    childEvent.getTable().addLine(result, childParsed.resolveValue(childEvent), args);
   
  }
View Full Code Here

   * @param network The network.
   */
  public void defineRelationships(BayesianNetwork network) {
    // define event relations, if they are not there already
    ParsedEvent childParsed = getChildEvent();
    BayesianEvent childEvent = network.requireEvent(childParsed.getLabel());
    for( ParsedEvent event : this.givenEvents) {
      BayesianEvent parentEvent = network.requireEvent(event.getLabel());
      network.createDependency(parentEvent, childEvent);
    }
   
  }
View Full Code Here

  public final File SERIAL_FILENAME = TEMP_DIR.createFile("encogtest.ser");
   
  public BayesianNetwork create()
  {
    BayesianNetwork network = new BayesianNetwork();
    BayesianEvent a = network.createEvent("a");
    BayesianEvent b = network.createEvent("b");

    network.createDependency(a, b);
    network.finalizeStructure();
    a.getTable().addLine(0.5, true); // P(A) = 0.5
    b.getTable().addLine(0.2, true, true); // p(b|a) = 0.2
    b.getTable().addLine(0.8, true, false);// p(b|~a) = 0.8   
    network.validate();
    return network;
  }
View Full Code Here

      done = true;
      eof = true;
    }

    while (!done) {
      BayesianEvent event = enumerationEvents.get(currentIndex);
      int v = (int) args[currentIndex];
      v++;
      if (v >= event.getChoices().size()) {
        args[currentIndex] = 0;
      } else {
        args[currentIndex] = v;
        done = true;
        break;
View Full Code Here

   * @param n The value for n.
   * @param old The old value.
   * @return The new value for z.
   */
  private BayesianEvent findZ(BayesianEvent event, int n, double old) {
    BayesianEvent result = null;
    double maxChildP = Double.NEGATIVE_INFINITY;
    //System.out.println("Finding parent for: " + event.toString());
    for(int i=0;i<n;i++) {
      BayesianEvent trialParent = this.nodeOrdering.get(i);
      List<BayesianEvent> parents = new ArrayList<BayesianEvent>();
      parents.addAll(event.getParents());
      parents.add(trialParent);
      //System.out.println("Calculating adding " + trialParent.toString() + " to " + event.toString());
      this.lastCalculatedP = this.calculateG(network, event, parents);
View Full Code Here

      if ( d[eventIndex] == desiredValue) {
        boolean reject = false;

        for (int i = 0; i < parentInstance.length; i++) {
          BayesianEvent parentEvent = parents.get(i);
          int parentIndex = network.getEventIndex(parentEvent);
          if (parentInstance[i] != d[parentIndex]) {
            reject = true;
            break;
          }
View Full Code Here

      int[] d = this.network.determineClasses( pair.getInput());

      boolean reject = false;

      for (int i = 0; i < parentInstance.length; i++) {
        BayesianEvent parentEvent = parents.get(i);
        int parentIndex = network.getEventIndex(parentEvent);
        if (parentInstance[i] != ((int) d[parentIndex])) {
          reject = true;
          break;
        }
View Full Code Here

TOP

Related Classes of org.encog.ml.bayesian.BayesianEvent

Copyright © 2018 www.massapicom. 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.