0% found this document useful (0 votes)
10 views56 pages

Assignment Mridul

The document contains solutions to various assignments in Probability and Statistics, including simulations and plotting of empirical densities, confidence intervals, and sums of random variables. It utilizes Python libraries such as NumPy and Matplotlib for data generation and visualization. Each section details the methodology, parameters, and results of the statistical analyses performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views56 pages

Assignment Mridul

The document contains solutions to various assignments in Probability and Statistics, including simulations and plotting of empirical densities, confidence intervals, and sums of random variables. It utilizes Python libraries such as NumPy and Matplotlib for data generation and visualization. Each section details the methodology, parameters, and results of the statistical analyses performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Assignment

Probability & Statistics

Mridul Sarmah

Indian Institute of Management, Udaipur

Roll No: 2421007

01-10-2024
6.(b) Solution
In [ ]: import numpy as np
import matplotlib.pyplot as plt

# Step 1: Generate 1000 random samples using the inverse CDF method
n = 1000
u = np.random.uniform(0, 1, n)
x = np.sqrt(u) # Apply the inverse CDF: X = sqrt(U)

# Step 2: Plot the histogram of the generated random variable


plt.figure(figsize=(8, 6))
plt.hist(x, bins=30, density=True, alpha=0.6, color='b', label="Empirical Densit

# Step 3: Plot the theoretical PDF


# The PDF is derived by differentiating the CDF F(x) = x^2 for 0 ≤ x ≤ 1.
# PDF: f(x) = 2x for 0 ≤ x ≤ 1
x_vals = np.linspace(0, 1, 100)
pdf_vals = 2 * x_vals # Theoretical PDF

plt.plot(x_vals, pdf_vals, 'r-', lw=2, label="Theoretical PDF")

# Step 4: Add labels and legends


plt.xlabel('x')
plt.ylabel('Density')
plt.title('Empirical Density vs Theoretical PDF')
plt.legend()

# Show the plot


plt.show()
8.(b) Solution
In [ ]: import numpy as np
import matplotlib.pyplot as plt

# Parameters
P = 0.48 # True population proportion
n = 1500 # Sample size
num_samples = 100 # Number of samples
confidence_level = 0.98 # 98% confidence level
Z = 2.326 # Z-value for 98% confidence level

# Function to calculate a confidence interval for each sample


def confidence_interval(sample_proportion, Z, n):
se = np.sqrt(sample_proportion * (1 - sample_proportion) / n)
return sample_proportion - Z * se, sample_proportion + Z * se

# Simulating 100 samples and calculating confidence intervals


intervals = []
proportions = []
contains_P = []
for _ in range(num_samples):
# Generate a random sample of size 1500 from a binomial distribution
sample = np.random.binomial(n, P) / n
proportions.append(sample)
# Calculate the confidence interval for this sample
ci_lower, ci_upper = confidence_interval(sample, Z, n)
intervals.append((ci_lower, ci_upper))
# Check if the true population proportion P is within the interval
contains_P.append(ci_lower <= P <= ci_upper)

# Plotting the confidence intervals and sample proportions with flipped axes
plt.figure(figsize=(10, 8))
for i, (ci_lower, ci_upper) in enumerate(intervals):
if contains_P[i]:
plt.plot([ci_lower, ci_upper], [i, i], color='blue') # Interval contain
else:
plt.plot([ci_lower, ci_upper], [i, i], color='red') # Interval doesn't

# Plot the sample proportion estimates as markers (on the y-axis)


plt.scatter(proportions, range(num_samples), color='black', zorder=5, label='Sam

# Plot a vertical line for the true population proportion P


plt.axvline(x=P, color='green', linestyle='--', label='True P (0.48)')

# Add labels and legend


plt.xlabel('Proportion')
plt.ylabel('Sample Index')
plt.title(f'98% Confidence Intervals and Sample Proportions for {num_samples} Sa
plt.legend()

# Show the plot


plt.show()
9.(a) Solution
In [ ]: import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

# Parameters
n_samples = 25 # Number of independent samples in each trial
num_trials = 1000 # Number of trials
a, b = 0, 1 # Uniform distribution range [0, 1]

# Step 1: Simulate 1000 sums of 25 independent uniform random variables


sums = [np.sum(np.random.uniform(a, b, n_samples)) for _ in range(num_trials)]

# Step 2: Compute mean and standard deviation of the sums


mean_s25 = np.mean(sums)
std_s25 = np.std(sums)

# Step 3: Plot the histogram of the sums


plt.figure(figsize=(10, 6))
plt.hist(sums, bins=30, density=True, alpha=0.6, color='b', label="Histogram of

# Step 4: Plot the normal distribution with same mean and std
x = np.linspace(min(sums), max(sums), 100)
plt.plot(x, norm.pdf(x, mean_s25, std_s25), 'r', lw=2, label=f'Normal Density\nM

# Step 5: Add labels and legend


plt.title('Sums of 25 Uniform Random Variables repeated 1000 times\nAnd\n Corres
plt.xlabel('Sum of 25 Uniform Variables')
plt.ylabel('Density')
plt.legend()

# Show the plot


plt.show()
9.(b) Solution
In [ ]: import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

# Parameters
n_samples = 25 # Number of independent samples in each trial
num_trials = 1000 # Number of trials

# Step 1: Inverse transform sampling for f(x) = 2x


def sample_from_fx_2x(size):
u = np.random.uniform(0, 1, size) # Generate uniform random numbers
return np.sqrt(u) # Apply the inverse CDF transformation

# Step 2: Simulate 1000 sums of 25 independent random variables


sums = [np.sum(sample_from_fx_2x(n_samples)) for _ in range(num_trials)]

# Step 3: Compute mean and standard deviation of the sums


mean_s25 = np.mean(sums)
std_s25 = np.std(sums)

# Step 4: Plot the histogram of the sums


plt.figure(figsize=(10, 6))
plt.hist(sums, bins=30, density=True, alpha=0.6, color='b', label="Histogram of

# Step 5: Plot the normal distribution with the same mean and standard deviation
x = np.linspace(min(sums), max(sums), 100)
plt.plot(x, norm.pdf(x, mean_s25, std_s25), 'r', lw=2, label=f'Normal Density\nM

# Step 6: Add labels and legend


plt.title('Sums of 25 Random Variables (f(x)=2x) repeated 1000 times\n And\n Cor
plt.xlabel('Sum of 25 Variables')
plt.ylabel('Density')
plt.legend()

# Show the plot


plt.show()
9.(c) Solution
In [ ]: import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

# Parameters
n_samples = 25 # Number of independent samples in each trial
num_trials = 1000 # Number of trials

# Step 1: Inverse transform sampling for f(x) = 3x^2


def sample_from_fx_3x2(size):
u = np.random.uniform(0, 1, size) # Generate uniform random numbers
return u**(1/3) # Apply the inverse CDF transformation

# Step 2: Simulate 1000 sums of 25 independent random variables


sums = [np.sum(sample_from_fx_3x2(n_samples)) for _ in range(num_trials)]

# Step 3: Compute mean and standard deviation of the sums


mean_s25 = np.mean(sums)
std_s25 = np.std(sums)

# Step 4: Plot the histogram of the sums


plt.figure(figsize=(10, 6))
plt.hist(sums, bins=30, density=True, alpha=0.6, color='b', label="Histogram of

# Step 5: Plot the normal distribution with the same mean and standard deviation
x = np.linspace(min(sums), max(sums), 100)
plt.plot(x, norm.pdf(x, mean_s25, std_s25), 'r', lw=2, label=f'Normal Density\nM

# Step 6: Add labels and legend


plt.title('Sums of 25 Random Variables (f(x)=3x^2) repeated 1000 times\nAnd\n Co
plt.xlabel('Sum of 25 Variables')
plt.ylabel('Density')
plt.legend()

# Show the plot


plt.show()
9.(d) Solution
In [ ]: import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

# Parameters
n_samples = 25 # Number of independent samples in each trial
num_trials = 1000 # Number of trials

# Step 1: Inverse transform sampling for f(x) = 4|x - 1/2|


def sample_from_fx_4_abs_x_minus_1_2(size):
u = np.random.uniform(0, 1, size) # Generate uniform random numbers
# Apply inverse CDF transformation
samples = np.where(u <= 0.5, 1-np.sqrt(1-u), (1 - np.sqrt(2*u)) / 2)
return samples

# Step 2: Simulate 1000 sums of 25 independent random variables


sums = [np.sum(sample_from_fx_4_abs_x_minus_1_2(n_samples)) for _ in range(num_t

# Step 3: Compute mean and standard deviation of the sums


mean_s25 = np.mean(sums)
std_s25 = np.std(sums)

# Step 4: Plot the histogram of the sums


plt.figure(figsize=(10, 6))
plt.hist(sums, bins=30, density=True, alpha=0.6, color='b', label="Histogram of

# Step 5: Plot the normal distribution with the same mean and standard deviation
x = np.linspace(min(sums), max(sums), 100)
plt.plot(x, norm.pdf(x, mean_s25, std_s25), 'r', lw=2, label=f'Normal Density\nM

# Step 6: Add labels and legend


plt.title('Sums of 25 Random Variables (f(x) = 4|x - 1/2|) repeated 1000 times\n
plt.xlabel('Sum of 25 Variables')
plt.ylabel('Density')
plt.legend()

# Show the plot


plt.show()
6.
The e
ypoesia

Po.8

Ptm (outicod vale)


As the Lineny
Populatiou hottoy

inomia Dici kniou,


he beeple to bollotd
fe
ino i ol henee the ean

and Vauane Wold de

Ho

Since Aize
C>30)

M.

(-e./

np(0-P)
M- 100Xo.z 2o.01
NJooxo.1X02

P2< 4
tu 2-Atehe
tale

m- 80 -2.93
4
80-9.32
W
= 70. 63

71-100X0.7

V]o0 x 0.9 2x0.22)

= P(22 -l-69)

=1- (I- (22149))


: P(241-69 )
2- uble)
= 0.9 545
7)- J0oX 0.7C
r (?2 NJoox0.7s025

(?2 -0.92)

0.81

71- )00X 0.72


P(? 160x0.9Lx 0.27

= P(2 -0.1)
P(2< O. 22)

D.527

(Y) P 0.70

Nj00x 0.90o.3
= P(22 0.22)

) 1 -.ET1
0 129
(v) = 0.48
7- J00X 0.61
P(2 2 )

|-r(2 20.64)

P( 22 ?|-)0 x0.45
Nlo0 xo-6sX0.3S

|- 0.39%2

b.103

(i) P= 0.63
-100x o. 63
Noxo.63 20.27

)-1(22|.·36)
| 0.95)S
,04 85
- P(?2 2-24)
- )-P(2can2u)
o.9 B75
o.o125

ot the
Aee thot the pokabi ity
he ml hypo fheas
reases
Cie. Pe tr eoh) dese de
We decheuse
igibi canty dhen
ole e u
to 0.6. So, TyPe I! eoh

we move

menKes

Cen Ahy that fhe test in vnoat ponbul.


Hence, ve

You might also like