Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

******* Please write the code, I have posted this question before and no code wa

ID: 3603502 • Letter: #

Question

******* Please write the code, I have posted this question before and no code was written.

*******I would like to check the code I have already written with the code you come up with

*******Posted the question twice before and didn't have any code written. I would like to check mine with yours

******** Thank you SO much!!!

Write a Binomial class to implement a Binomial random variable. Private instance variables for the class should include: double P - probability of a success in any one trial. long N - number of independent trials. long X - the number of successes in the N trials.

The class methods should include: appropriate constructor(s). appropriate get and set methods for the instance variables. long randomX() - generates a new random value for X. double mean() - calculates the mean of the random variable [= N*P]. double variance() - calculates the variance of random variable [= N*P*(1-P)]. double probX() - calculates the Binomial probability for the current X value.

Write a program that uses the Binomial class to solve the following problems:

1. For the Binomial distribution defined by P = 0.36 and N = 22: a. Output P and N. b. Generate a random value for X, and then output that value. c. Output the mean of the distribution. d. Output the variance of the distribution

2. For the Binomial distribution defined by P = 0.73 and N = 6, output the probability distribution of X. That is, for each value of X from 0 to 6, show the X-value together with the probability for that value.

Output format should be:

X Pr(X)

0 ...

1 ...

... ...

6 ...

3. For the Binomial distribution defined by P = 0.32 and N = 12: a. Generate 100 random variable values. b. Calculate the average of these generated values [= (X1 + X2 + ... + X100)/100]. c. Calculate the theoretical mean of this probability distribution using the mean() method. d. Compare the values obtained in 3b and 3c. How close are they? Why aren't the values identical?

Explanation / Answer

Here is the java code, Part 1 is working fine. Part 2 is not working just because the long values generated by the random() function are too big and I used the below formula to calculate the probablity. I didn't know how to calculate the probability so used the below formula.

Change the probability function as per your formula and it will work fine.

import java.util.Random;

public class Binomial {

private double P; // probability of success in any one trial.

private long N; // No of independent trials.

public Binomial(double P, long N) {

this.N = N;

this.P = P;

}

public double getP() {

return P;

}

public void setP(double p) {

P = p;

}

public long getN() {

return N;

}

public void setN(long n) {

N = n;

}

//randomX() generates a new random value for X.

long randomX() {

return new Random().nextLong();

}

//mean() calculates the mean of the random variable [= N*P].

double mean() {

return N*P;

}

//variance() calculates the variance of random variable [= N*P*(1-P)].

double variance() {

return N*P*(1-P);

}

//probX() calculates the Binomial probability for the current X value.

double probX(long x) {

return fact(N)/(fact(x)*fact(N-x))*(Math.pow((Math.PI),x))*(Math.pow((1-Math.PI),(N-x)));

}

// return n!

public static double fact(long n) {

double ans = 0.0;

for (int i = 1; i <= n; i++)

ans *= i;

return ans;

}

public static void main(String...s) {

Binomial binomial1 = new Binomial(0.36, 22);

double valN = binomial1.getN();

double valP = binomial1.getP();

long rand = binomial1.randomX();

double valMean = binomial1.mean();

double valVariance = binomial1.variance();

// Part 1 results.

System.out.println("N:"+valN+" and P:"+valP);

System.out.println("Random No generated is:"+rand);

System.out.println("Mean of the distribution is:"+valMean);

System.out.println("Variance of the distribution is:"+valVariance);

// Part 2

Binomial binomial2 = new Binomial(0.73, 6);

System.out.println("XPr(X)");

for(int i=0; i<=binomial2.getN(); i++) {

long rand2 = binomial2.randomX();

double prob = binomial2.probX(rand2);

System.out.println(i+","+rand2+","+prob);

}

// Part 3

Binomial binomial3 = new Binomial(0.32, 12);

// generating 100 random nos

long[] randomNos= new long[100];

for(int i=0; i<100; i++)

randomNos[i]= binomial3.randomX();

// taking avg of random nos generated

long rand3=0;

for(int i=0; i<100; i++)

rand3 += randomNos[i];

rand3=rand3/100;

valMean=binomial3.mean();

System.out.println("Theoretical mean:"+valMean+", Mean from random no's"+rand3);

}

}