Multinomial Logistic Regression › Example

Michael BrenndoerferJune 15, 20256 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 38Multinomial Logistic Regression › Example
Reading section 4 of 8 · Example · 6 minSection 4 of 8 · 6 min

Book contents

Section 4 of 8 · 6 min read

Multinomial Logistic Regression › ExampleOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through a concrete example to understand how multinomial logistic regression works with actual numbers. We'll use a simple dataset with two predictor variables and three classes to make the calculations manageable.

Dataset Setup

Suppose we have a dataset with two features (age and income) and three possible outcomes (low, medium, high risk). We'll use "low" as our reference category and calculate the probabilities for "medium" and "high" risk.

AgeIncomeRisk Level
2530low
3040low
3550low
4060medium
4570medium
5080medium
2535medium
3045medium
3555high
4065high
4575high
5085high

Coefficient Estimation

As mentioned in the formula section earlier, the coefficients in multinomial logistic regression are found by solving an optimization problem that maximizes the likelihood of the observed data. In practice, this is done using numerical algorithms such as Newton-Raphson or gradient-based methods, which iteratively adjust the coefficients to best fit the data.

For this example, let's assume we have already obtained the estimated coefficients. We'll now use these given coefficients to demonstrate how to calculate the predicted probabilities for a new observation.

Manual Calculation

Now let's manually calculate the multinomial logistic regression for an observation with age = 25 and income = 30 using these estimated coefficients:

Coefficients for medium vs low (reference):

  • βmedium,0=0.5\beta_{\text{medium},0} = 0.5 (intercept)
  • βmedium,age=0.1\beta_{\text{medium},\text{age}} = 0.1 (age coefficient)
  • βmedium,income=−0.05\beta_{\text{medium},\text{income}} = -0.05 (income coefficient)

Coefficients for high vs low (reference):

  • βhigh,0=1.2\beta_{\text{high},0} = 1.2 (intercept)
  • βhigh,age=0.15\beta_{\text{high},\text{age}} = 0.15 (age coefficient)
  • βhigh,income=−0.08\beta_{\text{high},\text{income}} = -0.08 (income coefficient)

Step-by-Step Calculation

Step 1: Calculate Linear Predictors

For our observation with age = 25 and income = 30, we calculate the linear predictor for each category:

ηmedium=βmedium,0+βmedium,age×age+βmedium,income×income\eta_{\text{medium}} = \beta_{\text{medium},0} + \beta_{\text{medium},\text{age}} \times \text{age} + \beta_{\text{medium},\text{income}} \times \text{income}

where:

  • ηmedium\eta_{\text{medium}} is the linear predictor for the medium risk category (the log-odds of medium risk relative to low risk before normalization)
  • βmedium,0=0.5\beta_{\text{medium},0} = 0.5 is the intercept for medium risk (the baseline log-odds when age and income are both zero)
  • βmedium,age=0.1\beta_{\text{medium},\text{age}} = 0.1 is the age coefficient for medium risk (the change in log-odds for each one-year increase in age)
  • βmedium,income=−0.05\beta_{\text{medium},\text{income}} = -0.05 is the income coefficient for medium risk (the change in log-odds for each one-unit increase in income)

Substituting the values:

ηmedium=0.5+0.1×25+(−0.05)×30=0.5+2.5−1.5=1.5\eta_{\text{medium}} = 0.5 + 0.1 \times 25 + (-0.05) \times 30 = 0.5 + 2.5 - 1.5 = 1.5

Similarly for high risk:

ηhigh=βhigh,0+βhigh,age×age+βhigh,income×income\eta_{\text{high}} = \beta_{\text{high},0} + \beta_{\text{high},\text{age}} \times \text{age} + \beta_{\text{high},\text{income}} \times \text{income}

where:

  • ηhigh\eta_{\text{high}} is the linear predictor for the high risk category (the log-odds of high risk relative to low risk before normalization)
  • βhigh,0=1.2\beta_{\text{high},0} = 1.2 is the intercept for high risk (the baseline log-odds when age and income are both zero)
  • βhigh,age=0.15\beta_{\text{high},\text{age}} = 0.15 is the age coefficient for high risk (the change in log-odds for each one-year increase in age)
  • βhigh,income=−0.08\beta_{\text{high},\text{income}} = -0.08 is the income coefficient for high risk (the change in log-odds for each one-unit increase in income)

Substituting the values:

ηhigh=1.2+0.15×25+(−0.08)×30=1.2+3.75−2.4=2.55\eta_{\text{high}} = 1.2 + 0.15 \times 25 + (-0.08) \times 30 = 1.2 + 3.75 - 2.4 = 2.55

For the reference category (low risk):

ηlow=0\eta_{\text{low}} = 0

where:

  • ηlow\eta_{\text{low}} is the linear predictor for the low risk category (set to 0 because low risk is the reference category)

Step 2: Calculate Exponentials

We apply the exponential function to each linear predictor:

eηmedium=e1.5≈4.482e^{\eta_{\text{medium}}} = e^{1.5} \approx 4.482

where:

  • ee is Euler's number (approximately 2.71828)
  • eηmediume^{\eta_{\text{medium}}} is the exponential of the linear predictor for medium risk (used in the softmax numerator)
eηhigh=e2.55≈12.807e^{\eta_{\text{high}}} = e^{2.55} \approx 12.807

where:

  • eηhighe^{\eta_{\text{high}}} is the exponential of the linear predictor for high risk (used in the softmax numerator)
eηlow=e0=1.000e^{\eta_{\text{low}}} = e^{0} = 1.000

where:

  • eηlowe^{\eta_{\text{low}}} is the exponential of the linear predictor for low risk (equals 1 because e0=1e^0 = 1)

Step 3: Calculate Denominator

The denominator of the softmax function is the sum of all exponentials:

∑k=1Keηk=eηlow+eηmedium+eηhigh\sum_{k=1}^{K} e^{\eta_k} = e^{\eta_{\text{low}}} + e^{\eta_{\text{medium}}} + e^{\eta_{\text{high}}}

where:

  • ∑k=1Keηk\sum_{k=1}^{K} e^{\eta_k} is the sum of exponentials across all KK categories (the normalization constant in the softmax function)
  • K=3K = 3 is the total number of categories (low, medium, high)

Substituting the values:

∑k=1Keηk=1.000+4.482+12.807=18.289\sum_{k=1}^{K} e^{\eta_k} = 1.000 + 4.482 + 12.807 = 18.289

Step 4: Calculate Probabilities

Using the softmax formula, we calculate the probability for each category:

P(low)=eηlow∑k=1Keηk=1.00018.289≈0.055P(\text{low}) = \frac{e^{\eta_{\text{low}}}}{\sum_{k=1}^{K} e^{\eta_k}} = \frac{1.000}{18.289} \approx 0.055

where:

  • P(low)P(\text{low}) is the predicted probability of low risk for this observation (computed by dividing the exponential of the low risk linear predictor by the sum of all exponentials)
P(medium)=eηmedium∑k=1Keηk=4.48218.289≈0.245P(\text{medium}) = \frac{e^{\eta_{\text{medium}}}}{\sum_{k=1}^{K} e^{\eta_k}} = \frac{4.482}{18.289} \approx 0.245

where:

  • P(medium)P(\text{medium}) is the predicted probability of medium risk for this observation (computed by dividing the exponential of the medium risk linear predictor by the sum of all exponentials)
P(high)=eηhigh∑k=1Keηk=12.80718.289≈0.700P(\text{high}) = \frac{e^{\eta_{\text{high}}}}{\sum_{k=1}^{K} e^{\eta_k}} = \frac{12.807}{18.289} \approx 0.700

where:

  • P(high)P(\text{high}) is the predicted probability of high risk for this observation (computed by dividing the exponential of the high risk linear predictor by the sum of all exponentials)

Step 5: Verification

We verify that all probabilities sum to 1, as required by the softmax function:

P(low)+P(medium)+P(high)=0.055+0.245+0.700=1.000 ✓P(\text{low}) + P(\text{medium}) + P(\text{high}) = 0.055 + 0.245 + 0.700 = 1.000 \,\checkmark

Interpretation

The calculations show that for a 25-year-old with income 30, the model predicts:

  • 5.5% probability of low risk
  • 24.5% probability of medium risk
  • 70.0% probability of high risk

The predicted class would be "high" since it has the highest probability. Notice how the probabilities sum to exactly 1, which is a fundamental property of the multinomial logistic regression model.