/*  confidence.java */

/*  confidence.java illustrates hypothesis testing for 
*   proportions
* @author Charles Stanton
* @version 0.0 May 30 1997
*/
import java.applet.*;
import java.awt.*;
import java.util.*;  //Need gaussian random variables


public class confidence extends playApplet {
	confidenceCanvas cc;
	confidenceControlPanel ccp;
	Panel upperPanel;
	FlowLayout fl;
	int n=20;    //number of datapoints
	int nTrials=20;
	double[][] normalData;
	double[] xBar;
	double[] s;
	Random random;
	int cvIndex=1;

/** initialization method
*/

	public void init() {
		int hypothesis=0; // initially, Ha: mu>mu0
		setLayout(new BorderLayout());
		cc = new confidenceCanvas(n,nTrials);
		add("South",cc);
		ccp = new confidenceControlPanel(this);
		fl = new FlowLayout();
		upperPanel = new Panel();
		upperPanel.setLayout(fl);
		upperPanel.add(ccp);
		add("North",upperPanel);
		cc.setBackground(Color.white);
		random = new Random();
		
	}

			

/**
* play generates sample dat
* @param nTrials is the number of tests to do
*/
	public void play(int nTrials){
		cvIndex = ccp.getAlphaIndex();
		int sumx=0;
		normalData = new double[nTrials][n];
		xBar = new double[nTrials];
		s = new double[nTrials];
		generateNormalData( nTrials, n, normalData, xBar,s);
		cc.update(cvIndex, normalData, xBar,  s);
		}

private void generateNormalData( int nTrials, int n, 
				double[][] normalData, double[] xBar,
				double[] s){
	double sumx=0;
	double sumxx=0;
	for (int trial =0; trial < nTrials; trial++) {
		for (int i=0; i<n; i++) {
			normalData[trial][i] = random.nextGaussian();	
			sumx += normalData[trial][i];
			sumxx += normalData[trial][i]*normalData[trial][i];
				}
			
		xBar[trial] = sumx/n;
		s[trial] = Math.sqrt((sumxx -sumx*sumx/n)/(n-1));
		sumx=0;
		sumxx=0;
		}
	}


}
