UDST / choicemodels

Python library for discrete choice modeling
https://udst.github.io/choicemodels
BSD 3-Clause "New" or "Revised" License
74 stars 33 forks source link

Bug: fitted MNL choice probabilities only available with sampling of alternatives #69

Open smmaurer opened 3 years ago

smmaurer commented 3 years ago

It looks like the MultinomialLogitResults class can only provide fitted probabilities for MergedChoiceTables that are generated with sampling of alternatives. This is a bug; there's no reason we can't calculate probabilities for tables that include all the alternatives. Thanks to @msoltadeo for reporting this.

Diagnosis

What's happening is that mergedchoicetable.py#L316 uses the sample size as a count of the number of alternatives, raising an error if the sample size is None. This should be easy to fix.

Workaround

As a temporary workaround, you can edit the table's sample size property after generating it:

results = choicemodels.MultinomialLogit(...).fit()
mct = choicemodels.MergedChoiceTable(..., sample_size=None)
mct.sample_size = numalts
results.predict(mct)

Or you can do it like this, although the table generation may be substantially slower:

results = choicemodels.MultinomialLogit(...).fit()
mct = choicemodels.MergedChoiceTable(..., sample_size=numalts, replace=False)
results.predict(mct)