Spaces:
Sleeping

capstone / templates /probability.html
Heng
Remove Singapore context - generalize for any dataset
8625b3d
Raw History Blame Contribute Delete
5.68 kB
{% extends "base.html" %}
{% block title %}Probability & Statistics - Data Science Lab{% endblock %}
{% block content %}
<div class="page-header">
<h1>📊 Probability & Statistics for ML</h1>
<p class="breadcrumb">Home / Probability — The mathematical foundation of data science</p>
</div>
<div class="concept-section">
<div class="concept-header"><h3>📖 Descriptive Statistics</h3></div>
<div class="concept-body">
<div class="code-block"><span class="lang-label">Python</span><pre>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)")</pre></div>
</div>
</div>
<div class="concept-section">
<div class="concept-header"><h3>📖 Probability Distributions</h3></div>
<div class="concept-body">
<table class="table">
<tr><th>Distribution</th><th>Type</th><th>Example</th></tr>
<tr><td>Normal</td><td>Continuous</td><td>Student test scores follow a bell curve</td></tr>
<tr><td>Poisson</td><td>Discrete</td><td>Number of transit delays per month</td></tr>
<tr><td>Exponential</td><td>Continuous</td><td>Time between bus arrivals</td></tr>
<tr><td>Binomial</td><td>Discrete</td><td>Probability of k housing ballots succeeding out of n</td></tr>
<tr><td>Log-Normal</td><td>Continuous</td><td>property prices (right-skewed)</td></tr>
</table>
<div class="code-block"><span class="lang-label">Python</span><pre>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}")</pre></div>
</div>
</div>
<div class="concept-section">
<div class="concept-header"><h3>📖 Hypothesis Testing</h3></div>
<div class="concept-body">
<div class="code-block"><span class="lang-label">Python</span><pre># 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)</pre></div>
</div>
</div>
<div class="concept-section">
<div class="concept-header"><h3>📖 Correlation & Causation</h3></div>
<div class="concept-body">
<div class="code-block"><span class="lang-label">Python</span><pre># 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)</pre></div>
</div>
</div>
<div class="common-mistake">
<h5>⚠️ Common Mistakes</h5>
<ul>
<li><strong>Using mean for skewed data</strong>: housing prices are right-skewed — use median instead</li>
<li><strong>p-value ≠ probability H₀ is true</strong>: It's P(data | H₀), not P(H₀ | data)</li>
<li><strong>Multiple testing problem</strong>: Testing 20 hypotheses at α=0.05 ⇒ expect 1 false positive</li>
</ul>
</div>
<div class="exercise-box">
<h5>🧪 Exercises</h5>
<p>1. Compute mean, median, and std for resale prices by flat type. Which metric is most useful?</p>
<p>2. Test whether 3-ROOM and 4-ROOM flat prices have equal variance (Levene's test).</p>
<p>3. What distribution would you use to model the number of housing applications per launch?</p>
</div>
{% endblock %}