/**
 * This applet demonstrates how a beta distribution is updated by positive and negative examples. It isn't designed to be general or reusable.
 * Copyright 2008 David Poole. This web page and applet is released under a <a
href="http://creativecommons.org/licenses/by-nc-sa/2.5/ca/">Creative Commons
Attribution-Noncommercial-Share Alike</a> license.
 * @author David Poole
 * @version 0.1 2002-03-11 */

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.*;
import javax.swing.*;

public class beta extends JApplet implements ActionListener 
{  int numVals = 580; // this is the width in pixels of the plot
   double previous[] = new double[numVals];
    double current[] = new double[numVals];
    int displayHeight = 290;
    int unitHeight = 50;
    int width = 1;

    int numPos = 0;
    int remPos=0;

    int numNeg = 0;
    int remNeg = 0;

//      Font smallFont = new Font("Serif", Font.PLAIN, 12);
      Font bigFont = new Font("Serif", Font.BOLD, 24);
    public void init()
    {
	setFont(bigFont);

     //setLayout(new FlowLayout());
	Panel pan = new Panel();
// 	pan.setLayout(new BoxLayout(pan,BoxLayout.Y_AXIS));
		pan.setLayout(new GridLayout(4,1));

    Button door = new Button("Positive Example");
    pan.add(door);
    door.addActionListener(this);

    Button nodoor = new Button("Negative Example");
    pan.add(nodoor);
    nodoor.addActionListener(this);

    Button reset = new Button("Reset");
    pan.add(reset);
    reset.addActionListener(this);

    Button remember = new Button("Remember");
    pan.add(remember);
    remember.addActionListener(this);


    getContentPane().add(pan,"East");
    doReset();
    doRemember();
    //repaint();
    }

public void paint(Graphics g)
    {  
	g.setColor(Color.white);
	g.fillRect(0,0,numVals*width,displayHeight*2+30);
	g.setColor(Color.black);
	drawDist(g,displayHeight,current,"Current (pos:"+numPos+", neg:"+numNeg+")");
	g.setColor(Color.green);
	drawDist(g,2*displayHeight+14,previous,"Remembered (pos:"+remPos+", neg:"+remNeg+")");
  }

    public void actionPerformed ( ActionEvent e)
    {
	String arg = e.getActionCommand();
	if (arg.equals("Reset") )
		doReset();
	if (arg.equals("Remember") )
		doRemember();
	else if (arg.equals("Positive Example"))
		positive();
	else if (arg.equals("Negative Example"))
		negative();
	repaint();
    }

    public void doReset()
    {     
      for (int i=0 ; i <numVals; i++) {
	  current[i]=1.0;
      }
      numPos=0;
      numNeg=0;
     }

    public void doRemember()
    {     
      for (int i=0 ; i <numVals; i++) {
	  previous[i]=current[i];
      }
      remPos=numPos;
      remNeg=numNeg;
    }

    public void positive() {
	for (int i=0; i<numVals; i++) {
	    current[i]*=i;
	}
	normalizeCurrent();
	numPos++;
    }

    public void negative() {
	for (int i=0; i<numVals; i++) {
	    current[i]*=(numVals-i);
	}
	normalizeCurrent();
        numNeg++;
    }

 

   public void normalizeCurrent() {
	double sum=0.0;
	for (int i=0; i<numVals; i++) {
	    sum+=current[i];
	}
        double multFactor=numVals/sum;
	for (int i=0; i<numVals; i++) {
	    current[i] *= multFactor;
	}
    }

    public void drawDist(Graphics g, int offset, double dist[], String title ) {
//   	setFont(smallFont);
	DecimalFormat df = new DecimalFormat("0.###");
	for (int i=0; i<numVals; i++) {
	    g.fillRect(i*width,offset-(int)(1.0+Math.floor(unitHeight*dist[i])),width,(int)(1+Math.floor(unitHeight*dist[i])));
	    //	    g.drawString(df.format(dist[i]),i*width+10,offset+12);
	}
	g.setColor(Color.red);
//   	setFont(bigFont);
	g.drawString(title,20,offset-unitHeight*2/3);
// 	for (int i=0; i<numVals; i++) {
// 	    g.drawString(Integer.toString(i),i*width+10,offset-40);
// 	}
//  	setFont(smallFont);
    }
}

