How do I model a yes-or-no outcome with logit in statsmodels?

Fit a logit model of whether a stock beats the market next month with smf.logit, read a coefficient as an odds ratio, predict probabilities, and summarise the effect with the average marginal effect.

After this lesson, you can fit a logit model of a yes-or-no outcome and read its coefficient as odds, probabilities and a marginal effect.

How do I fit a logit model?

smf.logit("beat ~ momentum", data=data).fit() estimates how a stock’s momentum changes the probability that it beats the market next month. On 2,000 made-up stock-months with a true slope of 0.4, the estimated slope is 0.448 with a standard error of 0.047.

\[ \log\frac{p}{1-p} = a + b\,x, \qquad p = \frac{1}{1 + e^{-(a + b x)}} \]

Here \(x\) is the momentum score, in standard deviations, \(p\) the probability of beating the market, \(p/(1-p)\) the odds, \(a\) the log odds at a score of zero and \(b\) the change in log odds per unit of momentum.

The example draws each score from a standard normal distribution, computes \(p\) with \(a = -0.1\) and \(b = 0.4\), and sets beat to 1 when a uniform random number falls below \(p\). rng.random(2000) draws the uniform numbers, and .astype(int) turns True and False into 1 and 0. .fit(disp=0) estimates the model by maximum likelihood without printing its progress.

The following box simulates the outcomes and fits the model.

import numpy as np
import pandas as pd
import statsmodels.formula.api as smf

rng = np.random.default_rng(8)
momentum = rng.normal(0, 1, 2000)
probability = 1 / (1 + np.exp(-(-0.1 + 0.4 * momentum)))
data = pd.DataFrame({"momentum": momentum, "beat": (rng.random(2000) < probability).astype(int)})

model = smf.logit("beat ~ momentum", data=data).fit(disp=0)
print(pd.DataFrame({"estimate": model.params, "standard_error": model.bse}).round(3))
           estimate  standard_error
Intercept    -0.125           0.046
momentum      0.448           0.047

How do I turn the slope into odds and probabilities?

\(e^{b}\) is the odds ratio, the factor by which one more unit of momentum multiplies the odds, here \(e^{0.448} = 1.565\). .predict() gives the probability for new scores, 26.5% at −2, 46.9% at 0 and 68.4% at 2.

The following box prints the odds ratio and three predicted probabilities.

print(round(np.exp(model.params["momentum"]), 3))
print(model.predict(pd.DataFrame({"momentum": [-2.0, 0.0, 2.0]})).round(3))
1.565
0    0.265
1    0.469
2    0.684
dtype: float64

The step from −2 to 0 adds 20.4 percentage points and the step from 0 to 2 adds 21.5, because the probability curve bends.

How do I express the effect in percentage points?

The average marginal effect is the slope of the probability curve, \(b\,p_i(1-p_i)\), averaged over the stock-months, here 0.1063. A rise of 0.1 in momentum raises the probability by about 1.06 percentage points on average.

.get_margeff(at="overall") computes the average of the slopes, and .summary_frame() returns it as a table with its standard error.

The following box prints the average marginal effect.

print(model.get_margeff(at="overall").summary_frame().round(4))
           dy/dx  Std. Err.        z  Pr(>|z|)  Conf. Int. Low  Cont. Int. Hi.
momentum  0.1063     0.0102  10.3901       0.0          0.0863          0.1264

Your turn

Fit an OLS regression of beat on momentum, a straight line through the zeros and ones, and print its slope. You should see 0.1064, close to the average marginal effect, because most scores lie in the middle of the curve, where it is close to straight.

smf.ols fits the same formula with a straight line.

print(round(smf.ols("beat ~ momentum", data=data).fit().params["momentum"], 4))
0.1064