{% extends "base.html" %} {% block title %}Probability & Statistics - Data Science Lab{% endblock %} {% block content %}

πŸ“– Descriptive Statistics

Python
import numpy as np
import pandas as pd

# property prices for Downtown
prices = np.array([480000, 520000, 510000, 650000, 490000, 530000, 1200000, 500000])

# Measures of Central Tendency
mean = np.mean(prices)          # 610,000 β€” pulled up by outlier
median = np.median(prices)      # 515,000 β€” robust to outliers
# Mode: most frequent value

# Measures of Spread
std = np.std(prices)           # Standard deviation
variance = np.var(prices)      # Variance = stdΒ²
iqr = np.percentile(prices, 75) - np.percentile(prices, 25)  # IQR

# Skewness: positive = right tail (common in prices)
from scipy.stats import skew
price_skew = skew(prices)      # > 0 means right-skewed

print(f"Mean: ${mean:,.0f}, Median: ${median:,.0f}")
print(f"Std Dev: ${std:,.0f}, IQR: ${iqr:,.0f}")
print(f"Skewness: {price_skew:.2f} (right-skewed = expensive outliers)")

πŸ“– Probability Distributions

DistributionTypeExample
NormalContinuousStudent test scores follow a bell curve
PoissonDiscreteNumber of transit delays per month
ExponentialContinuousTime between bus arrivals
BinomialDiscreteProbability of k housing ballots succeeding out of n
Log-NormalContinuousproperty prices (right-skewed)
Python
from scipy import stats

# Poisson: transit incidents per month (avg = 2.5)
poisson_dist = stats.poisson(mu=2.5)
print(f"P(0 incidents) = {poisson_dist.pmf(0):.4f}")
print(f"P(≀3 incidents) = {poisson_dist.cdf(3):.4f}")

# Normal: housing prices ~ N(500000, 80000Β²)
normal_dist = stats.norm(loc=500000, scale=80000)
print(f"P(price < 400000) = {normal_dist.cdf(400000):.4f}")
print(f"P(400000 < price < 600000) = {normal_dist.cdf(600000) - normal_dist.cdf(400000):.4f}")

# Central Limit Theorem: sample means are normally distributed
sample_means = [np.mean(np.random.choice(prices, size=10)) for _ in range(1000)]
print(f"Sample mean distribution: ΞΌ={np.mean(sample_means):,.0f}, Οƒ={np.std(sample_means):,.0f}")

πŸ“– Hypothesis Testing

Python
# Question: Are Downtown housing prices significantly higher than Midtown?
downtown_prices = [520000, 530000, 540000, 550000, 560000, 580000]
midtown_prices = [420000, 430000, 440000, 450000, 460000, 470000]

# Hβ‚€: ΞΌ_downtown = ΞΌ_midtown (no difference)
# H₁: ΞΌ_downtown > ΞΌ_midtown (Downtown is higher)

t_stat, p_value = stats.ttest_ind(downtown_prices, midtown_prices)
alpha = 0.05

print(f"t-statistic: {t_stat:.4f}")
print(f"p-value: {p_value:.6f}")
print(f"Reject Hβ‚€? {'Yes' if p_value < alpha else 'No'} (Ξ±={alpha})")
# Result: Yes β€” Downtown prices are statistically significantly higher

# Chi-squared test: Is flat type distribution independent of town?
# contingency_table = pd.crosstab(df['town'], df['property_type'])
# chi2, p, dof, expected = stats.chi2_contingency(contingency_table)

πŸ“– Correlation & Causation

Python
# Pearson correlation: linear relationship between two variables
# Spearman: monotonic relationship (robust to outliers)

# area vs price
r_pearson, p_val = stats.pearsonr(floor_areas, prices)
r_spearman, p_val = stats.spearmanr(floor_areas, prices)

print(f"Pearson r = {r_pearson:.3f}")     # e.g., 0.85 β€” strong positive
print(f"Spearman ρ = {r_spearman:.3f}")

# ⚠️ Correlation β‰  Causation
# Observation: Ice cream sales ↑ and illness cases ↑ in the area
# Correlation: r = 0.7
# Causation? NO β€” both are caused by hot weather (confounding variable)
⚠️ Common Mistakes
πŸ§ͺ Exercises

1. Compute mean, median, and std for resale prices by flat type. Which metric is most useful?

2. Test whether 3-ROOM and 4-ROOM flat prices have equal variance (Levene's test).

3. What distribution would you use to model the number of housing applications per launch?

{% endblock %}