# Implementing a Conditional Logit in Python StatsModels

I have a dataframe with some horseracing data, and each row contains a predicted speed rating for each of the runners. I am now trying to convert that information into a winning probability for each runner in a given race. I have tried implementing a softmax by group, but it tends to produce extreme results for the favored horse and is not well-calibrated to actual observed results. Much of the academic literature on the topic suggests using a conditional logit model for such a problem, but my attempts to implement it have thrown a variety of errors.

A simplified example of the dataframe that would be used to fit the conditional logit is below. The goal is to create a new column that provides a winning probability based on just the speed rating, conditional on the speed ratings of the other runners in the race.

Race Runner Proj. Speed Rating Winner?
1012 Horse 1 87.25 0
1012 Horse 2 86.52 0
1012 Horse 3 77.56 0
1012 Horse 4 84.25 1
1013 Horse 1 77.55 0
1013 Horse 3 74.06 0
1013 Horse 5 74.59 1
1013 Horse 7 74.46 0

My code thus far is as follows:

from statsmodels.discrete.conditional_models import ConditionalLogit

labels =  df['Winner?']
pred = df['Proj. Speed Rating']
groups = df['Race']

cl_model = ConditionalLogit(endog = labels, exog = pred,groups = group)
cl_fit = cl_model.fit()
cl_pred = cl_fit.predict(pred_external,race_external)



However no matter which adjustments I make the 'predict' line continues to throw a value error - in the format above it's 'Truth Value is ambiguous', and if I add the 'exog =' and 'groups =' it's a "NotImplemented" error. So my question is twofold: 1.) am I correct in thinking a conditional logit is the best way to approach this problem, even with only 1 independent variable? 2.) If so, how can I fix my code above to generate race-level probability estimates?

I don't have a statistics or computer science background, so any assistance with this is greatly appreciated.

• Is the problem not here: cl_model = ConditionalLogit(endog = labels, exog = pred,groups = group) I believe you should have "group=groups" instead of "groups= group" Commented Jan 10, 2023 at 22:56
• I have tried to use .predict() function on conditional logit models, but it has not been working, while it works on unconditional logit models. Did you @AMJ manage to produce predictions with conditional logit models? Commented Jul 22 at 16:52

I don't see anything obviously wrong and the approach appears sound. The simple simulation below emulates what you are doing and runs fine for me. Note that if you construct and fit the model in separate steps, you can see whether the ValueError occurs during setup or during optimization.

from statsmodels.discrete.conditional_models import ConditionalLogit
import numpy as np

g = np.kron(np.arange(100), np.ones(5)).astype(int)
x = np.random.normal(size=500)
pr = 1 / (1 + np.exp(-x))
y = (np.random.uniform(size=500) < pr).astype(int)

m = ConditionalLogit(endog=y, exog=x, groups=g)
r = m.fit()


Can you send the complete text of the ValueError? Also, check the dtypes of the three variables, perhaps one of them is a string or object?

• Thank you very much for your answer. Following your suggestion I fixed the dtype of one of the inputs (it was previously a DF object), and can now get the 'fit' command to work. I'm now getting an error when I try to run 'predict' though - "ValueError: The truth value of a Series is ambiguous.." This time the dtypes are all floats/integers so I'm not sure if my parameters are correct, and I can't find any examples along these lines on the StatsModels site. I updated the code above, any help greatly appreciated.
– AMJ
Commented Jul 10, 2022 at 21:14
• Conditional logit models aren't very useful for prediction. Each group (race for you) has a fixed effect and the model cannot extrapolate the results to new groups. Even for the groups in your dataset the fixed effects are not explicitly estimated. Commented Jul 11, 2022 at 0:51
• Thanks for the reply. I guess I'm a bit confused on that point - what would be a typical use case for SM CL's 'predict' then? Or are you saying 'fit' model can be directly used on out-of-sample data? Since empirically the winning speed ratings are normally distributed, could we use random effects instead? Finally, if a CL model isn't ideal for this as you say, what would be your suggested method for converting a group of ratings into probabilities, besides a softmax? Sorry for all the questions; any thoughts on any of the above appreciated again.
– AMJ
Commented Jul 11, 2022 at 19:06
• The results of predict can be used to compare two horses that will run in the same race. If you subtract the linear predictors for two observations in the same group, then this is the log-odds ratio comparing the success probabilities for those two observations. Because you are subtracting, the common fixed effect (which is not estimated) cancels out. But you cannot use these results to obtain an absolute probability for a single horse winning its race. Commented Jul 13, 2022 at 23:12
• Would it be possible to compare more than two horses with this method? I'm not looking for absolute probabilities, just ones relative to the other group members. Thanks again for your response; I appreciate it.
– AMJ
Commented Jul 14, 2022 at 19:53