Data Science and Visualization

Chapter 2
Mathematical and Statistical Foundations

Bachelors Program in Computer Applications
Data Science and Visualization   •   Bachelor

Mathematical and Statistical Foundations

Introduction

Data science converts observations into evidence for decisions. That conversion depends on mathematical and statistical reasoning: mathematics provides structures for representing data and models, while statistics provides methods for describing uncertainty, estimating unknown quantities, and evaluating evidence. These foundations support data visualization, predictive modelling, machine learning, experimentation, and responsible interpretation.

A data analyst should therefore be able to distinguish a population from a sample, summarize data appropriately, interpret probability, recognize common distributions, quantify relationships among variables, represent data as vectors and matrices, and formulate optimization problems. Equally important is knowing when a method is inappropriate. A numerical result is not automatically meaningful merely because it has been calculated correctly.

Learning Objectives

By the end of this chapter, you should be able to:

  • distinguish descriptive and inferential statistics;
  • calculate and interpret measures of central tendency, dispersion, and position;
  • apply basic probability rules and conditional probability;
  • explain important discrete and continuous probability distributions;
  • describe sampling methods and sources of sampling error;
  • represent data using vectors, matrices, and geometric operations;
  • calculate covariance and correlation and interpret their limitations;
  • explain objective functions, gradients, and gradient-based optimization;
  • apply these ideas to an analytics problem and communicate results responsibly.

1. Data, Populations, and Statistical Reasoning

An observation is a recorded value, such as a transaction amount, examination score, or temperature. A variable is a characteristic that may take different values. Variables may be quantitative, such as income, or qualitative, such as product category. Quantitative variables can be discrete, taking countable values, or continuous, taking values on an interval.

A population is the complete set of entities relevant to a question. A sample is a subset observed from that population. A parameter describes a population; a statistic describes a sample. For example, the population mean is commonly denoted by μ, whereas the sample mean is denoted by x̄.

Descriptive statistics summarize observed data. Inferential statistics use sample evidence to make statements about a broader population. Inference always involves uncertainty because different samples can produce different results.

Measurement scales

The measurement scale affects which analyses are valid.

ScaleMeaningExampleSuitable summaries
NominalCategories without orderBrowser typeCounts, proportions, mode
OrdinalOrdered categoriesSatisfaction levelMedian, rank comparisons
IntervalEqual differences, no true zeroTemperature in °CMean and standard deviation
RatioEqual differences and meaningful zeroWeight, revenueAll arithmetic summaries

Treating nominal codes such as 1, 2, and 3 as numerical measurements can produce misleading averages.

2. Descriptive Statistics

Frequency and proportion

For category i, its relative frequency is:

pi = fi / n

where f_i is the category frequency and n is the total number of observations. The proportions satisfy ∑p_i = 1, apart from rounding.

Measures of central tendency

For observations x₁, x₂, …, xₙ, the arithmetic mean is:

x̄ = (1/n) ∑ᵢ₌₁ⁿ xᵢ

The mean uses every observation and is sensitive to extreme values. The median is the middle ordered value; for an even sample size it is usually the average of the two middle observations. The mode is the most frequent value or category.

The weighted mean is:

x̄w = (∑wᵢxᵢ) / (∑wᵢ)

where wᵢ is a non-negative weight. The weights must be interpreted consistently—for example, as frequencies or importance weights.

The geometric mean, useful for positive multiplicative growth rates, is:

G = (∏ᵢ₌₁ⁿ xᵢ)^(1/n), xᵢ > 0

It should not be used for zero or negative observations without an appropriate transformation.

Measures of dispersion

The range is maximum minus minimum, but it depends only on two observations. The interquartile range is:

IQR = Q₃ − Q₁

where Q₁ and Q₃ are the first and third quartiles. The IQR is relatively resistant to outliers.

Population variance and standard deviation are:

σ² = (1/N) ∑ᵢ₌₁ᴺ (xᵢ − μ)²

σ = √σ²

For a sample used to estimate population variability, the commonly used unbiased variance estimator is:

s² = [1/(n − 1)] ∑ᵢ₌₁ⁿ (xᵢ − x̄)²

The n − 1 denominator, called Bessel’s correction, compensates for estimating μ with x̄. Standard deviation has the same units as the original variable; variance has squared units.

The coefficient of variation is:

CV = s / x̄

and is often expressed as a percentage. It is meaningful primarily for ratio-scale data with a positive mean.

Position and standardization

A z-score expresses an observation’s distance from the mean in standard-deviation units:

z = (x − μ) / σ

For sample standardization, x̄ and s are substituted. A z-score has no units. It is useful for comparing variables measured on different scales, provided the mean and standard deviation are appropriate summaries.

Worked example: delivery times

Suppose delivery times, in minutes, are 20, 22, 22, 25, and 41. The mean is:

x̄ = (20 + 22 + 22 + 25 + 41)/5 = 130/5 = 26 minutes.

The median is 22 minutes. The mean is higher because the 41-minute delivery is unusually long. Deviations from the mean are −6, −4, −4, −1, and 15; their squared sum is 294. Thus the sample variance is 294/4 = 73.5 minutes² and the sample standard deviation is approximately 8.57 minutes. Reporting both mean and median gives a clearer picture than reporting either alone.

Shape and outliers

A distribution may be symmetric, positively skewed, or negatively skewed. In a positively skewed distribution, a long right tail often makes the mean greater than the median. Outliers may result from data-entry errors, unusual but genuine cases, or changes in the data-generating process. They should be investigated rather than automatically deleted.

The box plot summarizes Q₁, median, Q₃, and potential outliers. The conventional 1.5-IQR rule flags values below Q₁ − 1.5IQR or above Q₃ + 1.5IQR. This is a screening convention, not proof that an observation is erroneous.

3. Probability and Uncertainty

A sample space Ω contains all possible outcomes. An event A is a subset of Ω. Probability satisfies 0 ≤ P(A) ≤ 1, P(Ω) = 1, and for mutually exclusive events A and B, P(A ∪ B) = P(A) + P(B).

The complement rule is:

P(Aᶜ) = 1 − P(A)

The general addition rule is:

P(A ∪ B) = P(A) + P(B) − P(A ∩ B)

The subtraction prevents the intersection from being counted twice.

Conditional probability is:

P(A | B) = P(A ∩ B) / P(B), provided P(B) > 0

Events A and B are independent when P(A ∩ B) = P(A)P(B), equivalently, when P(A|B) = P(A) when P(B) > 0. Mutually exclusive events with positive probability are not independent, because observing one makes the other impossible.

The multiplication rule is:

P(A ∩ B) = P(A | B)P(B)

Bayes’ theorem reverses conditional probabilities:

P(A | B) = [P(B | A)P(A)] / P(B)

If A₁, …, Aₖ partition the sample space, then:

P(B) = ∑ⱼ₌₁ᵏ P(B | Aⱼ)P(Aⱼ)

Bayes example

Assume 2% of devices are defective. A test detects a defective device with probability 0.95 and incorrectly signals positive for a good device with probability 0.05. The probability of a positive result is:

P(positive) = 0.95(0.02) + 0.05(0.98) = 0.068.

Therefore:

P(defective | positive) = 0.95(0.02)/0.068 ≈ 0.279.

Thus a positive result implies approximately 27.9% probability of defect, not 95%. The base rate is essential.

Random variables and expectation

A random variable assigns a numerical value to each outcome. For a discrete random variable X with possible values xᵢ and probabilities pᵢ:

E[X] = ∑ᵢ xᵢpᵢ

Its variance is:

Var(X) = E[(X − E[X])²] = E[X²] − [E[X]]²

For random variables X and Y, linearity of expectation gives E[aX + b] = aE[X] + b, without requiring independence. If X and Y are independent, Var(X + Y) = Var(X) + Var(Y); otherwise covariance terms must be included.

4. Probability Distributions

A probability mass function describes a discrete random variable. A probability density function describes a continuous variable; probabilities are areas under the density, and P(X = a) = 0 for an individual point in an ideal continuous model. A cumulative distribution function is F(x) = P(X ≤ x).

Important discrete distributions

The Bernoulli distribution models one trial with success probability p. If X is 1 for success and 0 otherwise, E[X] = p and Var(X) = p(1 − p).

The binomial distribution models the number of successes in n independent Bernoulli trials with constant success probability p:

P(X = k) = C(n,k)pᵏ(1 − p)ⁿ⁻ᵏ, k = 0,1,…,n

Its mean is np and variance is np(1 − p). Independence and constant p are important assumptions.

The Poisson distribution models counts in a fixed interval when events occur independently at a constant average rate λ:

P(X = k) = e^(−λ) λᵏ / k!, k = 0,1,2,…

Both mean and variance equal λ. Real processes may violate the constant-rate or independence assumptions.

Important continuous distributions

A uniform distribution on [a,b] assigns equal density across the interval. Its mean is (a+b)/2 and variance is (b−a)²/12.

The normal distribution has density:

f(x) = [1/(σ√(2π))] exp[−(x − μ)²/(2σ²)], σ > 0

It is symmetric with mean μ and standard deviation σ. The empirical 68–95–99.7 rule states that approximately 68%, 95%, and 99.7% of observations lie within one, two, and three standard deviations of μ when the normal model is appropriate.

The central limit theorem states, under broad conditions such as independent observations with finite variance, that the standardized sample mean tends toward a normal distribution as sample size increases. It does not say that the raw data must be normal, nor does it guarantee good approximation for small samples or highly dependent data.

5. Sampling and Estimation

A simple random sample gives each sample of a specified size an equal chance of selection. Stratified sampling divides a population into relevant subgroups and samples within each. Cluster sampling selects groups, such as schools or stores, and observes units within selected groups. Systematic sampling selects every kth unit after a random starting point.

Convenience sampling is easy but often biased. Voluntary-response samples may overrepresent people with strong opinions. Coverage error occurs when parts of the target population cannot be selected. Nonresponse bias occurs when respondents differ systematically from nonrespondents.

The standard error of the sample mean, under independent observations, is:

SE(x̄) = σ/√n

Since σ is usually unknown, s/√n is used. Increasing n reduces sampling variability at a square-root rate; quadrupling the sample size approximately halves the standard error.

A confidence interval has the general form:

estimate ± critical value × standard error

For a large-sample mean with known σ, a 95% interval is x̄ ± 1.96σ/√n. When σ is estimated, a t critical value is generally used, particularly for small samples. A 95% confidence procedure means that, over repeated samples, approximately 95% of intervals constructed by the procedure would contain the fixed population parameter. It does not mean there is a 95% probability that a particular fixed parameter lies in the already calculated interval.

6. Linear Algebra for Data Science

A scalar is a single number. A vector is an ordered list, represented as x = [x₁, x₂, …, xₚ]ᵀ. A matrix is a rectangular array. A dataset with n observations and p features is commonly represented by an n × p matrix X.

Vector addition requires equal dimensions. Scalar multiplication multiplies every component. The dot product is:

x · y = xᵀy = ∑ⱼ₌₁ᵖ xⱼyⱼ

The Euclidean norm is:

||x||₂ = √(xᵀx)

The distance between x and y is ||x − y||₂. The cosine similarity is:

cos(θ) = (xᵀy)/(||x||₂||y||₂)

provided neither vector is zero. It measures orientation rather than magnitude and is widely used in text analysis.

Matrix multiplication AB is defined when the number of columns of A equals the number of rows of B. If A is m × r and B is r × n, AB is m × n. Matrix multiplication is generally not commutative: AB may differ from BA or BA may not exist.

A linear model can be written:

y = Xβ + ε

where y is an n × 1 response vector, X is an n × p design matrix, β is a p × 1 coefficient vector, and ε is an error vector. An intercept is included by adding a column of ones to X.

The ordinary least squares objective is:

J(β) = ||y − Xβ||₂²

When XᵀX is invertible, the minimizer is:

β̂ = (XᵀX)⁻¹Xᵀy

This closed-form expression assumes a well-defined inverse and is not always numerically preferable. In practice, QR or singular-value decompositions are often more stable. If features are highly collinear, XᵀX can be ill-conditioned.

7. Covariance, Correlation, and Regression

Sample covariance between variables X and Y is:

sₓᵧ = [1/(n − 1)] ∑ᵢ₌₁ⁿ (xᵢ − x̄)(yᵢ − ȳ)

Positive covariance indicates that above-average values of one variable tend to occur with above-average values of the other. Its magnitude depends on measurement units, so covariance values are difficult to compare across datasets.

Pearson correlation is:

r = sₓᵧ/(sₓsᵧ)

where sₓ and sᵧ are sample standard deviations. It lies between −1 and 1 and measures linear association. Correlation is unchanged by positive changes of measurement units but changes sign if one variable is multiplied by a negative number.

Correlation does not establish causation. A third variable may confound the relationship, reverse causality may operate, or the association may be coincidental. Pearson correlation can also be distorted by outliers and nonlinear relationships. Spearman’s rank correlation may be more suitable for monotonic relationships involving ranks.

In simple linear regression, the fitted model is ŷ = β̂₀ + β̂₁x. The slope estimates the average change in predicted response for a one-unit increase in x, within the observed and model-relevant range. R² is the proportion of variation in the response explained by the fitted model under the usual decomposition conditions; it is not a universal measure of predictive quality and does not establish causation.

8. Optimization and Learning

Optimization chooses parameter values that minimize a loss or maximize an objective. A general minimization problem is:

minimize_θ J(θ)

where θ is a parameter vector and J is the objective function.

For a differentiable function of one variable, a necessary interior condition for a local optimum is J′(θ) = 0. It is not sufficient by itself: the point may be a maximum, saddle-like point in higher dimensions, or part of a flat region. Constraints and boundary points must also be considered.

For differentiable multivariable J, the gradient is:

∇J(θ) = [∂J/∂θ₁, …, ∂J/∂θₚ]ᵀ

Gradient descent updates parameters by:

θₜ₊₁ = θₜ − η∇J(θₜ)

where η > 0 is the learning rate. The negative gradient is the direction of steepest local decrease under the Euclidean metric. If η is too large, optimization may diverge; if too small, it may be slow.

For a dataset of m examples, mean squared error is:

J(β) = (1/m) ∑ᵢ₌₁ᵐ (yᵢ − ŷᵢ)²

For linear predictions ŷ = Xβ, the gradient is:

∇J(β) = −(2/m)Xᵀ(y − Xβ)

This formula assumes the objective is exactly the stated unregularized mean squared error. Batch gradient descent uses all observations; stochastic and mini-batch methods use one or a subset at each update. Feature scaling often improves convergence because differently scaled coordinates produce an elongated objective surface.

Regularization adds a penalty. Ridge regression uses:

J(β) = ||y − Xβ||₂² + λ||β||₂²

where λ ≥ 0 controls shrinkage. Typically the intercept is excluded from the penalty. Regularization can reduce variance and overfitting, but λ must be selected using training-validation procedures rather than the test set.

9. Applied Case: Predicting Customer Churn

The following is an illustrative case. A subscription company wants to identify customers at risk of cancellation so that retention resources can be allocated responsibly.

Background and problem

The company has monthly records containing tenure, monthly charge, support contacts, contract type, and churn status. Management asks whether a model can identify high-risk customers. The analytical problem is not simply to maximize accuracy; it must also account for class imbalance, unequal costs of errors, privacy, and the risk of unfair treatment.

Analysis and approach

The team first defines the population as active customers during the study period and checks whether records are complete. It separates a training set from a time-ordered test set to reduce leakage from future information. Categorical variables are encoded without pretending that category labels have numerical order. Numeric features are standardized using training-set means and standard deviations only.

Descriptive analysis shows that churn is relatively uncommon and that monthly charges are right-skewed. The median and IQR are therefore reported alongside the mean and standard deviation. A covariance matrix identifies scale-dependent co-movement, while a correlation matrix helps compare linear associations. Analysts avoid interpreting a strong association between support contacts and churn as proof that support contacts cause churn; dissatisfaction may cause both.

A logistic model estimates the probability of churn:

P(Y = 1 | x) = 1/[1 + exp(−(β₀ + βᵀx))]

Here Y = 1 denotes churn, x is the feature vector, and β contains coefficients. The probability threshold is selected using the relative costs of false negatives and false positives, not automatically set to 0.5. Performance is evaluated on untouched test data using a confusion matrix, precision, recall, calibration, and an appropriate baseline. Because the positive class is uncommon, accuracy alone could be misleading.

Outcome and governance

Suppose the model identifies a subgroup for proactive contact. Before deployment, the company checks whether contact rates and error rates differ substantially across protected groups, investigates proxy variables, documents feature sources, and provides an appeal or human-review process. Customers are not denied essential services solely because of an opaque risk score. Monitoring is established because customer behavior, pricing, and the data-generating process may change.

Learning points

The case illustrates that statistical foundations support the entire workflow: descriptive statistics reveal data shape, sampling determines generalizability, probability supports risk interpretation, linear algebra represents the model, optimization estimates parameters, and correlation analysis prevents simplistic causal claims. Governance is part of analytical quality rather than an afterthought.

10. Limitations, Ethics, and Best Practices

Statistical models simplify reality. Important limitations include measurement error, missing data, selection bias, dependence among observations, distribution shift, confounding, and model misspecification. A mathematically elegant method can still produce poor decisions when the data do not represent the intended population.

Best practice includes:

  1. define the population, unit of analysis, target variable, and time period before modelling;
  2. inspect distributions and missingness before selecting summaries or algorithms;
  3. preserve units and document transformations;
  4. separate training, validation, and test information to prevent leakage;
  5. report uncertainty and practical effect sizes, not only p-values or accuracy;
  6. investigate outliers and data-quality problems rather than hiding them;
  7. test robustness to alternative specifications and reasonable perturbations;
  8. document assumptions, data provenance, code, and version changes;
  9. protect personal data through minimization, access control, retention limits, and appropriate anonymization;
  10. monitor deployed systems for drift, discrimination, calibration failure, and unexpected consequences.

Contemporary analytics increasingly combines classical statistical reasoning with automated machine learning. Automated tools can search models and tune parameters, but they do not remove the need to define the estimand, assess sampling, interpret uncertainty, or establish governance. Generative AI may assist with code or explanations, yet its output must be independently verified, especially for formulas, data handling, and claims about people.

Chapter Summary

Mathematical and statistical foundations provide the language and discipline of data science. Descriptive statistics summarize distributions using measures such as the mean, median, variance, standard deviation, quartiles, and z-scores. Probability formalizes uncertainty through complements, unions, conditional probability, independence, Bayes’ theorem, random variables, expectation, and variance. Probability distributions such as the Bernoulli, binomial, Poisson, uniform, and normal models are useful only when their assumptions are reasonably appropriate.

Sampling determines how well a statistic represents a population. Standard errors quantify sampling variation, while confidence procedures communicate uncertainty under repeated-sampling interpretations. Linear algebra represents datasets and models with vectors and matrices; dot products, norms, matrix multiplication, and least squares are central to machine learning. Covariance and correlation describe association but do not establish causation. Optimization formulates learning as objective minimization, with gradients and learning rates governing iterative updates. Finally, sound analytics requires attention to data quality, leakage, fairness, privacy, interpretability, and monitoring.

Key Terms

Population: The complete set of entities relevant to a statistical question.

Sample: A subset of a population that is observed.

Parameter: A numerical characteristic of a population.

Statistic: A numerical quantity calculated from a sample.

Mean: The arithmetic average of numerical observations.

Median: The middle ordered observation or midpoint of the two middle observations.

Variance: The average squared deviation from a mean, with the denominator depending on population or sample use.

Standard deviation: The square root of variance, expressed in the original units.

Interquartile range: The difference between the third and first quartiles.

Conditional probability: The probability of one event given that another event has occurred.

Independence: A relationship in which occurrence of one event does not change the probability of another.

Random variable: A numerical mapping from outcomes to values.

Expected value: The probability-weighted average value of a random variable.

Normal distribution: A symmetric continuous probability model defined by a mean and standard deviation.

Standard error: The standard deviation of a sampling distribution, often of an estimator.

Vector: An ordered numerical array with direction and magnitude or coordinate values.

Matrix: A rectangular array of numbers.

Covariance: A unit-dependent measure of joint variation between two variables.

Correlation: A standardized measure of linear association between two variables.

Gradient descent: An iterative optimization method that moves opposite the gradient.

Review Questions

Short Answer Questions

  1. Distinguish a population parameter from a sample statistic and give one example of each.
  2. Why can the median be more informative than the mean for a strongly skewed variable?
  3. State the multiplication rule for conditional probability and identify its condition.
  4. What is the difference between covariance and correlation?
  5. What role does the learning rate play in gradient descent?

Long Answer Questions

  1. Explain the principal measurement scales and discuss how scale affects the choice of statistical summaries.
  2. Describe the assumptions of the binomial and Poisson distributions and compare their applications.
  3. Explain simple random, stratified, cluster, systematic, and convenience sampling, including one limitation of each.
  4. Develop the interpretation of ordinary least squares using vector and matrix notation. State when the closed-form estimator may be problematic.
  5. Explain why correlation does not establish causation and discuss confounding, reverse causality, outliers, and nonlinear relationships.

Analytical / Application Questions

  1. A fraud detector has a 1% fraud rate, 90% sensitivity, and a 4% false-positive rate. Calculate the probability that a transaction is actually fraudulent given a positive alert, and explain the role of the base rate.
  2. A dataset contains customer spending values with a few extremely large purchases. Recommend descriptive summaries, visualizations, and transformations, and justify each choice.
  3. Design an evaluation plan for a churn model. Specify the data split, performance measures, leakage controls, fairness checks, privacy safeguards, and monitoring procedures.

Multiple Choice Questions with Answers

Question 1

Which measure is generally most resistant to an extreme outlier?

A. Arithmetic mean

B. Median

C. Variance

D. Range

Correct Answer: B. Median

Question 2

If P(A) = 0.4 and P(B|A) = 0.5, what is P(A ∩ B)?

A. 0.10

B. 0.20

C. 0.45

D. 0.90

Correct Answer: B. 0.20

Question 3

A binomial model requires which condition?

A. A continuously measured outcome

B. Trials with changing success probabilities

C. A fixed number of independent trials with constant success probability

D. Events occurring at an unknown rate over unequal intervals

Correct Answer: C. A fixed number of independent trials with constant success probability

Question 4

If a sample standard deviation is 10 and the sample size is 100, the estimated standard error of the sample mean is:

A. 0.1

B. 1

C. 10

D. 100

Correct Answer: B. 1

Question 5

Which statement about Pearson correlation is correct?

A. It proves that one variable causes another

B. It measures only linear association and lies between −1 and 1

C. It is unaffected by outliers

D. It can exceed 1 when variables have large units

Correct Answer: B. It measures only linear association and lies between −1 and 1

Question 6

If x = [1, 2] and y = [3, 4], their dot product is:

A. 5

B. 7

C. 10

D. 14

Correct Answer: C. 10

Question 7

In gradient descent, the usual update is made in which direction?

A. The positive gradient direction

B. The negative gradient direction

C. A randomly selected direction only

D. The direction of the largest parameter value

Correct Answer: B. The negative gradient direction

Question 8

What is the principal purpose of a test set?

A. To fit model parameters repeatedly

B. To replace missing observations

C. To provide an approximately unbiased final evaluation after model development

D. To guarantee causal conclusions

Correct Answer: C. To provide an approximately unbiased final evaluation after model development

Question 9

A positive covariance means that, on average:

A. Both variables are always positive

B. Higher values of one tend to occur with lower values of the other

C. Above-average values tend to occur together with above-average values

D. The variables have identical units

Correct Answer: C. Above-average values tend to occur together with above-average values

Question 10

For a 2% defect rate, sensitivity 0.95, and false-positive rate 0.05, the probability of a positive test being truly defective is approximately:

A. 2.0%

B. 5.0%

C. 27.9%

D. 95.0%

Correct Answer: C. 27.9%

Data Science and Visualization
Chapter 2 of 15