Courses / Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course

4.1 Statistical Models: Correlation, Linear Regression, K-means and Smoothing

Unit 4 – Data and Analysis · 85 min read · 12 MCQs to check yourself

StatQuest – Linear regression, clearly explained

1. What is a statistical model?

A model is a simplified mathematical description of a real-world relationship that we can use to explain and predict. Weather forecasts, crop-yield estimates, electricity demand, house prices, exam-result predictions and recommendations on YouTube or Daraz are all based on models.

Term Meaning
Variable A quantity that changes (hours studied, marks, temperature)
Independent (predictor, x) The variable used to predict – plotted on the x-axis
Dependent (response, y) The variable being predicted – plotted on the y-axis
Model An equation linking them, e.g. y = mx + c
Prediction Using the model to estimate y for a new x

2. Correlation

Correlation measures how strongly two variables move together in a straight-line pattern. Pearson's correlation coefficient r is always between −1 and +1:

Value of r Meaning Example
close to +1 Strong positive – as x increases, y increases Study hours and marks; temperature and ice-cream sales
close to −1 Strong negative – as x increases, y decreases Car speed and journey time; absences and marks
close to 0 No linear relationship Shoe size and intelligence

r = (Σ (x - x)(y - y))/(√(Σ (x - x)² Σ (y - y)²)) Rough guide: |r| ≥ 0.7 strong, 0.4–0.7 moderate, below 0.4 weak. Correlation is not causation (Lesson 4.2).

3. Linear regression: the line y = mx + c

Linear regression fits the best straight line through the data (the line that makes the squared vertical distances from the points as small as possible – least squares).

Worked example – hours studied per week (x) vs test marks (y):

x 1 2 3 4 5 6 7 8
y 52 55 61 64 70 72 78 83

x = 4.5, y = 66.875, Σ (x-x)(y-y) = 185.5, Σ (x-x)² = 42 m = 185.5/42 ≈ 4.42 and c = 66.875 - 4.42 × 4.5 = 47.0 Model: y = 4.42x + 47 → each extra weekly study hour adds about 4.4 marks; a student who studies 0 hours is predicted to get 47. Prediction for 10 hours: y = 4.42 × 10 + 47 ≈ 91. Correlation r ≈ 0.997 (very strong positive).

Build the model in Python:

x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [52, 55, 61, 64, 70, 72, 78, 83]
n = len(x)
mx, my = sum(x) / n, sum(y) / n
sxy = sum((a - mx) * (b - my) for a, b in zip(x, y))
sxx = sum((a - mx) ** 2 for a in x)
m = sxy / sxx
c = my - m * mx
print(f"y = {m:.2f}x + {c:.2f}")
print(f"Prediction for 10 hours: {m * 10 + c:.1f}")

Output:

y = 4.42x + 47.00
Prediction for 10 hours: 91.2

In Excel / Google Sheets: put x in column A and y in column B, then =SLOPE(B2:B9, A2:A9), =INTERCEPT(B2:B9, A2:A9), =CORREL(A2:A9, B2:B9); or insert a scatter chart → add trendline → tick "display equation and R²". In Python, statistics.linear_regression(x, y) and statistics.correlation(x, y) do the same.

Daily-life example of slope and intercept (model paper): a taxi/ride app charges a base fare of Rs 150 plus Rs 40 per km: fare = 40 × km + 150. Slope = 40 (cost per extra km); intercept = 150 (fixed charge even for 0 km). A mobile package: bill = 2 × minutes + 100.

Multiple linear regression uses several predictors: y = b₀ + b₁x₁ + b₂x₂ + … (e.g. house price from area, number of rooms and location). The coefficients b₁, b₂ (beta values) show the effect of each predictor; statistical significance tells whether an effect is likely real or due to chance (advanced).

Limits of linear models: they assume a straight-line relation; predictions far outside the data range (extrapolation) are unreliable; outliers pull the line.

4. K-means clustering

K-means is an unsupervised machine-learning method that groups data points into k clusters of similar items (no labels are given).

  1. Choose k (number of clusters).
  2. Place k starting centroids (centres) randomly.
  3. Assign every point to its nearest centroid.
  4. Move each centroid to the mean of its points.
  5. Repeat steps 3–4 until the clusters stop changing.

Uses: customer segmentation (budget, regular, premium shoppers), grouping students by performance to plan extra classes, grouping areas by crime or disease rates, image compression (grouping similar colours).

Supervised learning Unsupervised learning
Trained on labelled data (inputs with correct answers) Finds patterns in unlabelled data
Predicts a label or value Groups or organises data
Examples: linear regression (predict marks), spam filter, face recognition Examples: K-means clustering of customers, grouping news articles

5. Smoothing

Real data is noisy (it jumps up and down). Smoothing removes short-term noise to show the underlying trend. The simplest method is the moving average: each point is replaced by the average of itself and its neighbours.

sales = [45, 52, 61, 58, 70]
smooth = []
for i in range(len(sales) - 2):
    window = sales[i:i + 3]
    smooth.append(round(sum(window) / 3, 2))
print(smooth)

Output:

[52.67, 57.0, 63.0]

Uses: stock-price charts (50-day moving average), COVID case trends (7-day average), weather and sensor data.

Practical (PBA) tasks

  1. Collect height and arm-span (or study time and marks) of 10 classmates; plot a scatter chart in Excel/Sheets, add a trendline and write the equation.
  2. Use the equation to predict a value and compare it with a real measurement.
  3. Write a Python program that computes m, c and r for any two lists.

StatQuest – K-means clustering

Short and long questions

1. What is slope and intercept for a linear relationship? Give an example from daily life.

Model answer

In the model y = mx + c, the slope (m) is the change in y for each one-unit increase in x (steepness and direction of the line); the intercept (c) is the value of y when x = 0 (where the line meets the y-axis). Example: a ride-hailing fare = 40 × km + 150. Slope = Rs 40 per km; intercept = Rs 150 base fare charged even for 0 km.

2. Define correlation. Explain positive, negative and zero correlation with examples.

Model answer

Correlation measures the strength and direction of a linear relationship between two variables (coefficient r from −1 to +1). Positive (r near +1): both increase together – study hours and marks. Negative (r near −1): one increases as the other decreases – absences and marks. Zero (r near 0): no linear relation – shoe size and exam marks.

3. Write the steps of the K-means clustering algorithm.

Model answer

(1) Choose the number of clusters k. (2) Place k initial centroids randomly. (3) Assign each data point to the nearest centroid. (4) Recalculate each centroid as the mean of the points assigned to it. (5) Repeat steps 3–4 until assignments no longer change. Use: customer segmentation.

4. What is smoothing? Calculate the 3-point moving averages of 45, 52, 61, 58, 70.

Model answer

Smoothing removes short-term random fluctuations (noise) from data to show the underlying trend. 3-point moving averages: (45 + 52 + 61)/3 = 52.67; (52 + 61 + 58)/3 = 57; (61 + 58 + 70)/3 = 63.

5. Differentiate between supervised and unsupervised learning with daily-life examples.

Model answer

Supervised learning trains on labelled data (inputs with known answers) to predict labels/values – e.g. a spam filter learns from emails marked spam/not spam; predicting house prices with regression. Unsupervised learning finds patterns in unlabelled data – e.g. K-means grouping shoppers into budget/regular/premium customers without being told the groups.

6. Why is model building important? Give three real-world applications of statistical models.

Model answer

Models summarise relationships in data so we can understand them, predict future values and make data-driven decisions/recommendations. Applications: (1) weather and rainfall forecasting; (2) predicting crop yield from rainfall and fertiliser; (3) recommendation systems (YouTube, Daraz) and predicting sales/electricity demand.

7. The table shows hours studied (x) and marks (y) of 8 students: x = 1, 2, 3, 4, 5, 6, 7, 8; y = 52, 55, 61, 64, 70, 72, 78, 83. Find the regression line y = mx + c, interpret slope and intercept, and predict the marks for 10 hours of study.

Model answer

x̄ = 36/8 = 4.5; ȳ = 535/8 = 66.875. Σ(x − x̄)(y − ȳ) = 185.5; Σ(x − x̄)² = 42. m = 185.5 / 42 ≈ 4.42; c = ȳ − m x̄ = 66.875 − 4.4167 × 4.5 = 47.0. Model: y = 4.42x + 47. Interpretation: slope 4.42 → each extra hour of weekly study raises marks by about 4.4; intercept 47 → predicted marks with zero study hours. Prediction: y = 4.42 × 10 + 47 ≈ 91 marks. (The correlation r ≈ 0.997 shows a very strong positive linear relation, so the prediction is reasonable within the data range; predicting far beyond is extrapolation and less reliable.)

8. Explain linear models, K-means clustering and smoothing with one real-world application of each.

Model answer

Linear model (regression): fits the best straight line y = mx + c through data using least squares; slope shows the rate of change and intercept the starting value; used to predict one variable from another, e.g. predicting electricity bill from units used or crop yield from rainfall. Multiple regression uses several predictors. K-means clustering: unsupervised method that groups data into k clusters: choose k, place centroids, assign points to the nearest centroid, move centroids to the mean, repeat until stable; application: grouping customers of an online store into segments for targeted offers. Smoothing: removes noise to reveal trends, typically with a moving average (each point replaced by the average of a window of neighbouring points); application: 7-day average of daily COVID cases or 50-day moving average of share prices.

This lesson ends with a quiz of 12 MCQs. Open the lesson on PrepOasis to take it and save your progress.

Previous: Unit 3 Review, SLO Checklist and Unit Test · Next: 4.2 Experimental Design: Correlation vs Causation, Population and Sample

All lessons in Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course