Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Classification for BI: Bayesian methods and logistic regression

Clear study notes on Bayesian classification (including Naive Bayes) and logistic regression for BI classification tasks, with definitions, derivations, formulas, worked examples, common mistakes and exam guidance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Classification is the task of assigning categorical labels (for example "fraud" vs "not fraud") to records. Engineers use Bayesian methods and logistic regression in Business Intelligence to score records, build risk models, and produce interpretable probability estimates that feed dashboards, KPIs and automated decision rules.

Key ideas

  • Bayes' theorem — an exact identity that relates conditional probabilities. Exact statement: for events A and B with P(B) > 0, P(A|B) = P(B|A) · P(A) / P(B). Assumption: probabilities are defined; for continuous variables replace probabilities by densities.

  • Posterior, likelihood, prior — when using Bayes' theorem: the prior P(C) is belief about class before seeing x; the likelihood P(x|C) is probability/density of seeing x if class is C; the posterior P(C|x) is updated belief after observing x.

  • Naive Bayes classifier — a practical classifier that applies Bayes' theorem with the naive assumption that features are conditionally independent given the class. Exact decision rule: choose class C that maximises the posterior P(C|x) ∝ P(C) ∏_j P(x_j | C). Assumptions: conditional independence; feature distributions known or estimated (categorical: multinomial/Bernoulli; real-valued: Gaussian). For categorical features, apply Laplace/add-α smoothing to avoid zero probabilities in P(x_j|C); in practice compute log-posteriors (sum of log-likelihoods plus log-prior) to avoid underflow when multiplying many probabilities.

  • Gaussian Naive Bayes — assume each continuous feature x_j | C ~ N(μ_{j,C}, σ_{j,C}^2). Likelihood for feature j: (1/(√(2π)σ)) exp(- (x_j - μ)^2/(2σ^2)). Assumption: normality within each class and independence between features given class.

  • Decision boundary — the set of x where two classes have equal posterior. For Gaussian Naive Bayes with equal variances, the boundary is linear in x; with unequal variances it is quadratic. Diagram to draw for exam: 2D feature space with x1 horizontal, x2 vertical; plot a few training points labelled Class A and Class B; sketch contour lines of equal posterior and draw the boundary curve; label axes, classes and boundary.

  • Logistic regression — a discriminative model for binary classification that models the log-odds (logit) of class 1 as a linear function of features. Define logit: logit(p) = log(p/(1-p)). Logistic model: logit(P(y=1|x)) = β0 + β1 x1 + ... + βd xd. The inverse of logit is the sigmoid function σ(z) = 1 / (1 + e^{-z}). Assumption: log-odds is linear in features; observations independent. Interpretation: a one‑unit increase in x_j multiplies the odds by exp(β_j), holding other features fixed.

  • Maximum likelihood estimation (MLE) — choose parameters β that maximise the probability (likelihood) of observed labels given features under the model. For logistic regression this means maximizing a concave log-likelihood; equivalently, minimizing a convex negative log-likelihood (no closed-form solution in general).

  • Cross-entropy (log loss) — the negative log-likelihood for logistic regression. Minimising cross-entropy is equivalent to maximizing likelihood. Regularization (e.g., L2/ridge or L1/lasso) is often added to control overfitting, e.g., minimise −ℓ(β) + (λ/2)||β||_2^2 or −ℓ(β) + λ||β||_1.

  • Optimization methods — for logistic regression: gradient descent, stochastic gradient descent (SGD), and second-order methods (Newton-Raphson / IRLS). State that Newton-Raphson uses the Hessian and converges faster when small-d to medium-d.

  • Calibration and thresholding — both Naive Bayes and logistic regression output probabilities; choose a decision threshold (often 0.5) for class label, but in BI tasks the threshold may be set by business constraints (precision/recall trade-off).

Formulas

P(C|x) = P(x|C) · P(C) / P(x)

  • P(C|x) = posterior probability of class C given features x (unitless, applies for discrete or continuous with densities). P(x) = ∑_k P(x|C_k) P(C_k).

P(C | x) ∝ P(C) · ∏_j P(x_j | C)

  • Naive Bayes decision proportionality using conditional independence of features x_j given class. Note: for continuous features the P(x_j|C) are densities (with units), but proportionality is unaffected for comparing classes. Applies when features are conditionally independent given class.

P(x_j | C) = (1 / (√(2π) σ_{j,C})) · exp( - (x_j - μ_{j,C})^2 / (2 σ_{j,C}^2) )

  • Gaussian likelihood for feature j given class C; μ in same units as x_j; σ in same units as x_j; applies for Gaussian Naive Bayes.

σ(z) = 1 / (1 + e^{-z})

  • Sigmoid function mapping real z to probability in (0,1). Unitless.

logit(p) = log( p / (1 - p) ) = β0 + Σ_{j=1}^d β_j x_j

  • Relationship defining logistic regression. p is probability P(y=1|x) (unitless); β0 is unitless and each β_j has units inverse to x_j so that β^T x is unitless.

p_i = σ(β^T x_i) = 1 / (1 + e^{-β^T x_i})

  • Predicted probability for observation i; x_i includes a constant 1 for intercept if needed.

ℓ(β) = Σ_{i=1}^n [ y_i log p_i + (1 - y_i) log(1 - p_i) ]

  • Log-likelihood of data under logistic model. Unitless; maximised to obtain MLE.

∂ℓ/∂β = Σ_{i=1}^n x_i (y_i - p_i) = X^T (y - p)

  • Gradient of log-likelihood (column vector). Use in gradient ascent; for minimising negative log-likelihood use negative of this gradient.

H(β) = X^T W X, where W is diagonal with w_i = p_i (1 - p_i)

  • Hessian (second derivative matrix) of the negative log-likelihood used in Newton-Raphson / IRLS. For the log-likelihood, the Hessian is - X^T W X. For a convex loss (negative log-likelihood), H is positive semidefinite (positive definite if X has full column rank).

Update (gradient ascent): β ← β + α · X^T (y - p)

  • α is learning rate (unitless). For descent on negative log-likelihood change sign.

Worked examples

  1. Gaussian Naive Bayes (numeric posterior)

Data (feature x1 only), two classes A and B:

  • Prior: P(A) = 0.6, P(B) = 0.4.
  • Class-conditional Gaussian parameters: A: μ_A = 5.0 (units), σ_A = 1.0 (units). B: μ_B = 7.0, σ_B = 1.5.

Question: For x = 6.0, compute P(A|x) and P(B|x).

Steps:

  1. Compute likelihoods:
    • P(x|A) = (1/(√(2π)·1.0)) · exp(- (6-5)^2 / (2·1^2)) = (1/2.5066) · exp(-0.5) = 0.39894 · 0.60653 = 0.24197.
    • P(x|B) = (1/(√(2π)·1.5)) · exp(- (6-7)^2 / (2·1.5^2)) = (1/ (2.5066·1.5)) · exp(-0.2222) = 0.26596 · 0.80074 = 0.21299.
  2. Compute unnormalised posteriors:
    • P(A)P(x|A) = 0.6 · 0.24197 = 0.14518.
    • P(B)P(x|B) = 0.4 · 0.21299 = 0.08520.
  3. Normalise: P(A|x) = 0.14518 / (0.14518 + 0.08520) = 0.14518 / 0.23038 = 0.6305. P(B|x) = 0.3695.

Answer: P(A|x=6.0) = 0.6305, P(B|x=6.0) = 0.3695 (unitless probabilities).

  1. Logistic regression: prediction and one gradient computation

Data: three observations with one feature x (plus intercept):

  • x_1 = 0, y_1 = 0
  • x_2 = 1, y_2 = 0
  • x_3 = 2, y_3 = 1 Model parameters initial guess: β0 = -1.0 (intercept), β1 = 1.0.

(a) Predict probability for x = 1.5.

  1. Compute z = β0 + β1·x = -1.0 + 1.0·1.5 = 0.5.
  2. p = σ(z) = 1 / (1 + e^{-0.5}) = 0.62246. Answer: P(y=1|x=1.5) = 0.6225.

(b) Compute gradient ∂ℓ/∂β at current β (using training data).

  1. Compute p_i for each i:
    • i=1: z=-1+1·0=-1 → p1=0.26894
    • i=2: z=0 → p2=0.5
    • i=3: z=1 → p3=0.73106
  2. Residuals y_i - p_i: r = [ -0.26894, -0.5, 0.26894 ].
  3. Gradient for β0 = Σ (y_i - p_i) = -0.26894 -0.5 +0.26894 = -0.5. Gradient for β1 = Σ x_i (y_i - p_i) = 0·(-0.26894) + 1·(-0.5) + 2·0.26894 = -0.5 + 0.53788 = 0.03788. Answer: Gradient = [∂ℓ/∂β0, ∂ℓ/∂β1] = [-0.5, 0.03788]. Use in ascent; for descent on negative log-likelihood take negative.

Common mistakes

  • Mixing up likelihood P(x|C) and posterior P(C|x).
  • For logistic regression: using linear regression (predicting p by β^T x directly) which can yield values outside [0,1].
  • Gradient sign errors: remember gradient of log-likelihood is X^T (y - p), so ascent uses plus; descent on negative log-likelihood uses minus.
  • Not including intercept term (bias) in logistic model and forgetting to add column of ones to X.
  • Interpreting probabilities as calibrated without checking (Naive Bayes often over/under-confident; check calibration).

For your exam

Typical question types: definitions (Bayes theorem, logit, likelihood), derivations (derive logistic log-likelihood and its gradient; show Naive Bayes decision rule), numericals (compute posterior with naive Bayes; compute logistic probability and gradient), algorithm trace (one SGD step), diagram (decision boundary).

What full-marks answers include:

  • Clear definitions of terms used (e.g., prior, likelihood, posterior, logit).
  • Statement of assumptions (conditional independence for Naive Bayes; linearity of log-odds for logistic regression).
  • Step-by-step algebra for derivations (show substitution into Bayes' theorem; show derivation of gradient from log-likelihood).
  • For numericals, show every calculation step, state intermediate values and final probabilities with correct rounding and units (probabilities unitless).

Quick check

  1. State Bayes' theorem formula and name its three parts for classification.
  2. What assumption does Naive Bayes make about features?
  3. Write the formula for the gradient of the logistic log-likelihood.

Answers: 1) P(C|x) = P(x|C)P(C)/P(x) — prior P(C), likelihood P(x|C), posterior P(C|x). 2) Conditional independence of features given the class. 3) ∂ℓ/∂β = X^T (y - p).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?