Professional Documents
Culture Documents
CsLab4 Pritam Das 17ETCS002130
CsLab4 Pritam Das 17ETCS002130
4: Discrete Distributions AND Continuous Distributions
1. Introduction and Purpose of Experiment
2. Aim and Objectives
3. Experimental Procedure
Design and Implement a Java program for the following Discrete Probability Distribution
1. Binomial distribution
i. To find the number of successes in n independent Bernoulli trials, given that
X has a binomial distribution
ii. Calculate the
1. Mean, E(X)
2. Variance, V(X)
2. Geometric distribution
i. To identify the number of Bernoulli trials, X, to achieve the 1st success
ii. Calculate the
1. Mean, E(X)
2. Variance V(X)
3. Negative binomial distribution
i. To identify the number of Bernoulli trials, X, until the kth success
ii. Calculate the Mean, E(X) and Variance V(X)
4. Develop and implement a Java program by selecting suitable distribution function for
given scenario:If 40% of the assembled ink‐jet printers are rejected at the inspection
station. Your program should identify:
i. Probability that the first acceptable ink‐jet printer is the third one inspected.
Considering each inspection as a Bernoulli trial with q=0.4 and p=0.6.
ii. Probability that the third printer inspected is the second acceptable printer
4. Design and Implement a Java program for the following Continuous Distribution
A computer repair person is “beeped” each time there is a call for service. If the number of
beeps per hour is Poisson distributed (α = 2 beeps per hour). Then design and implement a
Java program to determine the following.
i. The probability of exactly three beeps in the next hour:
ii. The probability of two or more beeps in a 1‐hour period:
5. Algorithms
Algorithm:
procedure bernoullidistribution Var p
begin
Var q := 1 ‐ p, x, pxx, mean, var : Double
Write "Enter the Position of Success : "
Read x
pxx := Math.pow (p, x) * Math.pow (q, 1 – x)
mean := p
var := p * q
Write "Binomial distribution P x := x := " + df.format
pxx + "\nMean := " + df.format mean + "\nVariance := " + df.format var
end
procedure geometricdistribution Var p
begin
Var q := 1 ‐ p, x, pxx, mean, var : Double
Scanner s := new Scanner System.in
Write "Enter the Position of Success : "
Read x
pxx := Math.pow (q, x – 1) * p
mean := 1 / p
var := p / Math.pow q, 2
Write "Binomial distribution P x := x := " + df.format
pxx + "\nMean := " + df.format mean + "\nVariance := " + df.format var
end
procedure binomialdistribution Var p
begin
Var q := 1 ‐ p, x, n, pxx, mean, var : Double
Scanner s := new Scanner System.in
Write "Enter the Position of Success : "
Read x
Write "Enter the number of Trials : "
Read n
pxx := combination n, x * Math.pow (p, x) * Math.pow (q, 1 – x)
mean := n * p
var := n * p * q
Write "Binomial distribution P x := x := " + df.format
pxx + "\nMean := " + df.format mean + "\nVariance := " + df.format var
end
procedure negativebinomialdistribution Var p
begin
Var q := 1 ‐ p, r, x, pxx, mean, var : Double
Scanner s := new Scanner System.in
Write "Enter the Position of Success : "
Read x
Write "Enter the No of Success : "
Read r
pxx := combination x ‐ 1 , r ‐ 1 * Math.pow (p, r) * Math.pow
(q, x – r)
mean := r / p
var := r * p / Math.pow (q, 2)
Write "Binomial distribution P x := x := " + df.format
pxx + "\nMean := " + df.format mean + "\nVariance := " + df.format var
end
6. Presentation of Results
Code:
/**
* csLab4
*/
import java.lang.Math;
import java.text.DecimalFormat;
import java.util.*;
public class csLab4 {
public static void main(String[] args) {
int n;
double p;
Scanner s = new Scanner(System.in);
while (true) {
System.out.print(
"Enter the type of Distribution:\n1. Bernoulli's Distribution\n2. Binom
ial Distribution\n3. Geometric Distribution\n4. Negative Binomial Distribution\n5. EXIT\n")
;
n = s.nextInt();
switch (n) {
case 1:
System.out.println("Enter the Value of P : ");
p = s.nextDouble();
bernoullidistribution(p);
break;
case 2:
System.out.println("Enter the Value of P : ");
p = s.nextDouble();
binomialdistribution(p);
break;
case 3:
System.out.println("Enter the Value of P : ");
p = s.nextDouble();
geometricdistribution(p);
break;
case 4:
System.out.println("Enter the Value of P : ");
p = s.nextDouble();
negativebinomialdistribution(p);
break;
case 5:
return;
}
}
}
private static DecimalFormat df = new DecimalFormat("0.00");
static double factCal(double x) {
double fact = 1;
for (int i = 1; i <= x; i++) {
fact = fact * i;
}
return fact;
}
static double combination(double n, double r) {
double ncr;
ncr = factCal(n) / (factCal(r) * factCal(n ‐ r));
return ncr;
}
static void bernoullidistribution(double p) {
double q = 1 ‐ p, x, pxx, mean, var;
Scanner s = new Scanner(System.in);
System.out.println("Enter the Position of Success : ");
x = s.nextDouble();
pxx = Math.pow(p, x) * Math.pow(q, (1 ‐ x));
mean = p;
var = p * q;
System.out.println("Binomial distribution P(x = x) = " + df.format(pxx) + "\nMean =
" + df.format(mean) + "\nVariance = " + df.format(var));
}
static void geometricdistribution(double p) {
double q = 1 ‐ p, x, pxx, mean, var;
Scanner s = new Scanner(System.in);
System.out.println("Enter the Position of Success : ");
x = s.nextDouble();
pxx = Math.pow(q, (x ‐ 1)) * p;
mean = 1 / p;
var = p / Math.pow(q, 2);
System.out.println("Binomial distribution P(x = x) = " + df.format(pxx) + "\nMean =
" + df.format(mean) + "\nVariance = " + df.format(var));
}
static void binomialdistribution(double p) {
double q = 1 ‐ p, x, n, pxx, mean, var;
Scanner s = new Scanner(System.in);
System.out.println("Enter the Position of Success : ");
x = s.nextDouble();
System.out.println("Enter the number of Trials : ");
n = s.nextDouble();
pxx = combination(n, x) * Math.pow(p, x) * Math.pow(q, (1 ‐ x));
mean = n * p;
var = n * p * q;
System.out.println("Binomial distribution P(x = x) = " + df.format(pxx) + "\nMean =
" + df.format(mean) + "\nVariance = " + df.format(var));
}
static void negativebinomialdistribution(double p) {
double q = 1 ‐ p, r, x, pxx, mean, var;
Scanner s = new Scanner(System.in);
System.out.println("Enter the Position of Success : ");
x = s.nextDouble();
System.out.println("Enter the No of Success : ");
r = s.nextDouble();
pxx = combination((x ‐ 1), (r ‐ 1)) * Math.pow(p, r) * Math.pow(q, (x ‐ r));
mean = r / p;
var = (r * p) / Math.pow(q, 2);
System.out.println("Binomial distribution P(x = x) = " + df.format(pxx) + "\nMean =
" + df.format(mean) + "\nVariance = " + df.format(var));
}
}
7. Analysis and Discussions
a. Binomial distribution
A frequency distribution of the possible number of successful outcomes in a given number of trials in
each of which there is the same probability of success. A binomial random variable is the number of
successes x in n repeated trials of a binomial experiment. The probability distribution of a binomial
random variable is called a binomial distribution. Suppose we flip a coin two times and count the
number of heads (successes).
b. Geometric distribution
It is the discrete equivalent of the exponential distribution. The geometric distribution is a special case
of the negative binomial distribution. It deals with the number of trials required for a single success.
Thus, the geometric distribution is negative binomial distribution where the number of successes (r)
is equal to 1.
c. Negative binomial distribution
The negative binomial distribution is a discrete probability distribution of the number of successes in
a sequence of independent and identically distributed Bernoulli trials before a specified (non‐random)
number of failures (denoted r) occurs.
8. Conclusions
Here in the above code we are showing three different distributions and finding Mean and
variance for the generated random numbers. In all the distribution the result is discrete.