Saturday, January 31, 2015

Bernoulli.java in programe



Bernoulli.java

public class Bernoulli 
{ 
              public static int binomial(int N, double p) 
{
        int heads = 0;
        for (int i = 0; i < N; i++) 
{
            if (StdRandom.bernoulli(p)) 
{
                heads++;
            }
        }
        return heads;
    } 
              public static int binomial(int N) 
{
        int heads = 0;
        for (int i = 0; i < N; i++) 
         {
            if (StdRandom.bernoulli(0.5)) 
           {
                heads++;
            }
        }
        return heads;
    } 
                public static void main(String[] args) 
    { 
        int N = Integer.parseInt(args[0]);   
        int T = Integer.parseInt(args[1]);   
        StdDraw.setYscale(0, 0.2);
        int[] freq = new int[N+1];
        for (int t = 0; t < T; t++) 
        {
            freq[binomial(N)]++;
        }
                     double[] normalized = new double[N+1];
        for (int i = 0; i <= N; i++)
       {
            normalized[i] = (double) freq[i] / T;
        }
        StdStats.plotBars(normalized);
        double mean = N / 2.0;
        double stddev = Math.sqrt(N) / 2.0;
        double[] phi  = new double[N+1];
        for (int i = 0; i <= N; i++) 
        {
            phi[i] = Gaussian.phi(i, mean, stddev);
        }
        StdStats.plotLines(phi);
    } 
} 

No comments:

Post a Comment