IIT Madras Zanzibar · Teaching

← Z3003 Data Mining

Tutorial 1: Simulating Random Variables

numpy · scipy.stats  ·  Submit as dm01t[RR].py

Submission

Name your file dm01t[RR].py where RR = last two digits of your roll number. Example: roll ending in 10 → dm01t10.py. All five tasks must be in one file, with # Task N comments separating them. Submit by the deadline announced in class.

Background

In data mining we frequently simulate data, understand probability distributions, and reason about random processes. This tutorial builds the numpy/scipy foundation used throughout the course. We focus on the Bernoulli distribution (a single yes/no trial) and the Binomial distribution (count of successes across N independent trials).

Tasks

Task 1 Set Up the Random Number Generator

  1. Import numpy as np
  2. Create a generator: rng = np.random.default_rng(seed=5)
  3. Print the generator object to confirm creation
Generator(PCG64) at 0x...
A fixed seed means every run of your program produces the same numbers: essential for reproducible experiments in data science.

Task 2 Bernoulli Random Variable X with p = 0.8

A Bernoulli variable models one trial: success (1) with probability p, failure (0) with probability 1−p.

  1. Set p = 0.8
  2. Sample 25 Bernoulli outcomes and store in a numpy array samples:
    samples = rng.binomial(n=1, p=0.8, size=25)
  3. Print the numpy vector samples
  4. Compute and print the average of the 25 samples using np.mean()
samples: [1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0] mean: 0.84
The mean should be close to p = 0.8 but not exact: 25 samples is small. Try size=10000 and observe how much closer you get to 0.8.

Task 3 Compute the Average using numpy's Statistical Method

  1. Compute the sample mean: sample_mean = np.mean(samples)
  2. Print it formatted to 4 decimal places: f"Sample mean: {sample_mean:.4f}"
  3. Compute the theoretical mean of Bernoulli(p=0.8) and store as theoretical_mean
  4. Print the theoretical mean and the absolute difference between sample and theoretical means
Sample mean: 0.8400 Theoretical mean: 0.8000 Difference: 0.0400

Task 4 Binomial Random Variable Y with n = 10, p = 0.8

Y ~ Binomial(n=10, p=0.8) counts the number of successes across 10 independent Bernoulli(0.8) trials. Y takes values 0, 1, 2, …, 10.

  1. Set n = 10, p = 0.8
  2. Sample 25 outcomes from Y: Y_samples = rng.binomial(n=10, p=0.8, size=25)
  3. Print the 25 sampled outcomes
  4. Compute and print the expectation (mean) of Y_samples using a numpy statistical method
  5. Compute and print the theoretical expectation E[Y] = n × p
Y samples: [ 8 9 10 7 9 8 8 9 8 6 10 9 8 9 8 9 6 9 9 9 10 8 9 8 7] Sample E[Y]: 8.5200 Theoretical E[Y]: 8.0000
Bernoulli(p) = Binomial(n=1, p). Tasks 2 and 4 use the same function with only n changing.

Task 5 Extension: Variance (discuss in tutorial session)

  1. Compute sample variance of samples using np.var(samples) and print it
  2. Print the theoretical variance of Bernoulli(p=0.8) = p(1−p)
  3. Compute and print sample variance of Y_samples
  4. Print the theoretical variance of Binomial(n=10, p=0.8) = n·p·(1−p)
  5. Try n=100 and n=1000 with size=25. Write a comment in your code about what happens to the sample mean relative to the theoretical mean as n increases.