0% found this document useful (0 votes)
3 views6 pages

Python CLT and Confidence Interval Simulations

data science coding tutorial series 6

Uploaded by

hpmfsl2sys3
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)
3 views6 pages

Python CLT and Confidence Interval Simulations

data science coding tutorial series 6

Uploaded by

hpmfsl2sys3
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

Q11.

Write Python code to demonstrate the Central Limit Theorem (CLT) using
simulation. (Hint: Draw many samples, compute sample means, plot histogram.)

In [ ]: # Central Limit Theorem (CLT)


import numpy as np
import [Link] as plt

# Try changing these values-

n = 30 # Sample size in each draw (try 5, 30, 100)


B = 1000 # Number of repeated samples (try 500, 2000, 5000)
dist = "exponential" # Try for: "exponential", "uniform", "poisson"

# Generate samples and compute means

[Link](0) # Reproducibility
means = [Link](B) # Store sample means

for i in range(B):
if dist == "exponential":
sample = [Link](scale=1.0, size=n)
elif dist == "uniform":
sample = [Link](0, 10, size=n)
elif dist == "poisson":
sample = [Link](lam=3, size=n)
means[i] = [Link](sample)

# Plot results

[Link](means, bins=30, density=True, edgecolor='black', color='lightgreen')


[Link](f"CLT: Distribution of sample means (n={n}, B={B}, dist={dist})")
[Link]("Sample Mean")
[Link]("Density")
[Link]()

# Check mean and std

print("Average of sample means:", [Link](means))


print("Std. deviation of sample means:", [Link](means))

# What you should observe / interpret:


#The original population (exponential) is skewed.
#The histogram of sample means should look approximately normal (bell-shaped).

# Small exercises you can try:


# Try n as (5,30,100) and see how the shape changes (larger n → more normal).
# Try [Link](...) with [Link](...) or [Link](...)
# to test CLT for different population shapes.
Average of sample means: 0.9954889043544146
Std. deviation of sample means: 0.18313997891553105

Q12. Write Python code to construct a 95% confidence interval for the mean of a
dataset. (Hint: Use the following predefined
dataset:([65,70,68,72,74,69,71,73,67,66,70,75])

In [ ]: # Confidence Interval

import numpy as np
import [Link] as plt
from [Link] import norm

# Try changing these values!


scores = [65,70,68,72,74,69,71,73,67,66,70,75] # Your dataset
conf = 95 # Confidence level (try 90, 95, 99)

# Step 1: Basic statistics


n = len(scores)
mean = [Link](scores)
s = [Link](scores, ddof=1) # sample standard deviation

# Step 2: Standard error


SE = s / [Link](n)

# Step 3: Critical z-value


alpha = 1 - (conf / 100)
z = [Link](1 - alpha/2) # two-tailed

# Step 4: Confidence interval


lower = mean - z * SE
upper = mean + z * SE

print(f"{conf}% CI for the mean: ({lower:.3f}, {upper:.3f})")


print("Sample mean is:", mean)

# Step 5: Plot
[Link](scores, bins=5, edgecolor='black', color='skyblue')
[Link](lower, color='red', linestyle='--', label=f'Lower {conf}% CI')
[Link](upper, color='blue', linestyle='--', label=f'Upper {conf}% CI')
[Link](mean, color='green', linestyle='-', label='Sample Mean')
[Link](f"{conf}% Confidence Interval for the Mean")
[Link]("Scores")
[Link]("Frequency")
[Link]()
[Link]()
#Try for different CI 90%,99%. And also try for different scores.

95% CI for the mean: (68.211, 71.789)


Sample mean is: 70.0

[Link] random numbers from the normal distribution and approximate the
shape of: a) Chi square distribution with 3 degrees of freedom b) t-distribution with
5 degrees of freedom (Hint: Use only [Link], generate 100,000
samples, construct them using definitions, then plot histograms.

In [ ]: import numpy as np
import [Link] as plt

# Step 1: Generate 100,000 Chi-square samples with df=3


# Each sample = sum of squares of df independent standard normal variables
df = 3
chi_sq_samples = [sum([Link](0,1,df)**2) for _ in range(100000)]

# Step 2: Basic statistics


mean = [Link](chi_sq_samples)
std = [Link](chi_sq_samples)
print(f"Chi-square Mean: {mean:.2f}, Std Dev: {std:.2f}")

# Step 3: Plot histogram


[Link](chi_sq_samples, bins=50, edgecolor='black', color='lightcoral')
[Link](mean, color='green', linestyle='-', label='Mean')
[Link]('Chi-square Distribution (df=3)')
[Link]('Value')
[Link]('Frequency')
[Link]()
[Link]()

# Exploration / Try & Observe


# Change df to (5,10,15) and observe how distribution becomes more symmetric.
# Observe that distribution is always positive and skewed to the right.
# Try plotting samples (e.g., 10,000 or 200,000) and see histogram smoothing.

Chi-square Mean: 2.99, Std Dev: 2.44


[Link] random numbers from the normal distribution and approximate the
shape of: b) t-distribution with 5 degrees of freedom (Hint: Use only
[Link], generate 100,000 samples, construct them using
definitions,then plot histograms.)

In [ ]: import numpy as np
import [Link] as plt

# Step 1: Generate 100,000 t-distribution samples


# t = Z / sqrt(V/df), where Z~N(0,1) and V=sum of squares of df standard normal variab
df = 5
t_samples = []
for _ in range(100000):
Z = [Link](0,1) # numerator (standard normal)
V = sum([Link](0,1,df)**2) # denominator chi-square component
t_samples.append(Z / [Link](V/df))

# Step 2: Basic statistics


mean = [Link](t_samples)
std = [Link](t_samples)
print(f"t-Distribution Mean: {mean:.2f}, Std Dev: {std:.2f}")
# Step 3: Plot histogram
[Link](t_samples, bins=50, edgecolor='black', color='skyblue')
[Link](mean, color='green', linestyle='-', label='Mean')
[Link]('t-Distribution (df=5)')
[Link]('Value')
[Link]('Frequency')
[Link]()
[Link]()

# Step 4: Exploration / Try & Observe


# Change df to (2,10,20) to see effect on tail thickness.
# Observe symmetry around 0 and heavier tails compared to normal distribution.
# Try different samples (50,000 or 200,000) to see smoother histograms.

t-Distribution Mean: 0.00, Std Dev: 1.29

You might also like