Normal Distribution Practice Problem
This data science coding problem helps you practice Random Sampling & Generators, normal distribution, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Random Sampling & Generators.
- Problem ID: 113
- Problem key: 113-normal-distribution
- URL: https://datacrack.app/solve/113-normal-distribution
- Difficulty: easy
- Topic: Random Sampling & Generators
- Module: NumPy Foundations
Problem Statement
# 🧩 Normal Distribution
---
### 🎯 Goal
The **normal (Gaussian) distribution** is the most important distribution in statistics and ML. It appears everywhere: noise in data, weight initialization in neural networks, natural phenomena (heights, test scores), and the Central Limit Theorem. `np.random.normal` generates samples from $N(\mu, \sigma)$.
---
### 🔍 Normal Samples with `np.random.normal`
```python
np.random.seed(42)
np.random.normal(0, 1, size=5) # 5 values from standard normal N(0,1)
np.random.normal(100, 15, size=1000) # IQ scores: mean=100, std=15
```
**Normal distribution intuition:**
| Range | Approx. percentage of values |
|:------|:-----------------------------|
| $\mu \pm 1\sigma$ | ~68.3% |
| $\mu \pm 2\sigma$ | ~95.4% |
| $\mu \pm 3\sigma$ | ~99.7% |
This means most normal-distribution values stay close to the mean, and values far beyond 2–3 standard deviations are uncommon.
---
### 💻 Task
Implement `generate_normal_stats(mean, std, n_samples, seed)` that generates normal samples and returns `[round(sample_mean, 1), round(sample_std, 1)]`.
---
### 📥 Input
- `mean`: float — population mean ($\mu$)
- `std`: float — population standard deviation ($\sigma$)
- `n_samples`: int — number of samples
- `seed`: int — random seed
### 📤 Output
- List: `[rounded_sample_mean, rounded_sample_std]`
---
### 🧩 Starter Code
```python
import numpy as np
def generate_normal_stats(mean, std, n_samples, seed):
"""
Generate normal samples and return summary statistics.
Args:
mean (float): Population mean
std (float): Population standard deviation
n_samples (int): Number of samples
seed (int): Random seed
Returns:
list: [rounded_sample_mean, rounded_sample_std]
"""
# 🧠 TODO: Set seed, generate normal samples
# 🧠 TODO: Return [round(arr.mean(), 1), round(arr.std(), 1)]
pass
```
---
### 💡 Example
```python
generate_normal_stats(100, 15, 100000, 0)
# Expected: [100.0, 15.0] — sample stats match population params
```
---
### 🔑 Key Concepts
- `np.random.normal(mean, std, size)` draws from $N(\mu, \sigma)$
- With enough samples, the sample mean ≈ $\mu$ and sample std ≈ $\sigma$ (Law of Large Numbers)
- Standard normal ($\mu=0, \sigma=1$) can be generated with `np.random.randn(size)`Starter Code
import numpy as np
def generate_normal_stats(mean, std, n_samples, seed):
"""
Generate normal samples and return summary statistics.
Args:
mean (float): Population mean
std (float): Population standard deviation
n_samples (int): Number of samples
seed (int): Random seed
Returns:
list: [rounded_sample_mean, rounded_sample_std]
"""
# 🧠 TODO: Set seed, generate normal samples
# 🧠 TODO: Return [round(arr.mean(), 1), round(arr.std(), 1)]
pass