Multinomial Logistic Regression › Example
Machine Learning from ScratchAll chapters 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.
| Age | Income | Risk Level |
|---|---|---|
| 25 | 30 | low |
| 30 | 40 | low |
| 35 | 50 | low |
| 40 | 60 | medium |
| 45 | 70 | medium |
| 50 | 80 | medium |
| 25 | 35 | medium |
| 30 | 45 | medium |
| 35 | 55 | high |
| 40 | 65 | high |
| 45 | 75 | high |
| 50 | 85 | high |
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):
- (intercept)
- (age coefficient)
- (income coefficient)
Coefficients for high vs low (reference):
- (intercept)
- (age coefficient)
- (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:
where:
- is the linear predictor for the medium risk category (the log-odds of medium risk relative to low risk before normalization)
- is the intercept for medium risk (the baseline log-odds when age and income are both zero)
- is the age coefficient for medium risk (the change in log-odds for each one-year increase in age)
- is the income coefficient for medium risk (the change in log-odds for each one-unit increase in income)
Substituting the values:
Similarly for high risk:
where:
- is the linear predictor for the high risk category (the log-odds of high risk relative to low risk before normalization)
- is the intercept for high risk (the baseline log-odds when age and income are both zero)
- is the age coefficient for high risk (the change in log-odds for each one-year increase in age)
- is the income coefficient for high risk (the change in log-odds for each one-unit increase in income)
Substituting the values:
For the reference category (low risk):
where:
- 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:
where:
- is Euler's number (approximately 2.71828)
- is the exponential of the linear predictor for medium risk (used in the softmax numerator)
where:
- is the exponential of the linear predictor for high risk (used in the softmax numerator)
where:
- is the exponential of the linear predictor for low risk (equals 1 because )
Step 3: Calculate Denominator
The denominator of the softmax function is the sum of all exponentials:
where:
- is the sum of exponentials across all categories (the normalization constant in the softmax function)
- is the total number of categories (low, medium, high)
Substituting the values:
Step 4: Calculate Probabilities
Using the softmax formula, we calculate the probability for each category:
where:
- 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)
where:
- 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)
where:
- 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:
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.