MDM Py
This package is a Python implementation of
Marginal Distribution Models
(MDMs), which can be used in Discrete Choice Modelling.
Documentation
Documentation is kindly hosted by Read The Docs.
Install
This package is uploaded to PyPI. Hence,
pip install mdmpy
should work.
How to use
Simplest Case
Gradient Descent
In the simplest case, we will use the Multinomial Logit (MNL) model,
which is used as a default. Assuming numpy, scipy and pandas are
installed, we generate choice data assuming a random utility model:
from string import ascii_uppercase as letters
import pandas as pd
import scipy.stats as stats
import numpy as np
NUM_INDIV = 57
NUM_CHOICES = 3
NUM_ATTR = 4
np.random.seed(2019)
X = np.random.random((NUM_ATTR, NUM_INDIV * NUM_CHOICES))
true_beta = np.random.random(NUM_ATTR)
V = np.dot(true_beta.T, X)
V = np.reshape(V, (NUM_INDIV,NUM_CHOICES))
eps = stats.gumbel_r.rvs(size=NUM_INDIV * NUM_CHOICES)
eps = np.reshape(eps, (NUM_INDIV, NUM_CHOICES))
U = V + eps
highest_util = np.argmax(U, 1)
df = pd.DataFrame(X.T)
df['choice'] = [1 if idx == x else 0 for idx in highest_util for x in range(NUM_CHOICES)]
df['individual'] = [indiv for indiv in range(NUM_INDIV) for _ in range(NUM_CHOICES)]
df['altvar'] = [altlvl for _ in range(NUM_INDIV) for altlvl in letters[:NUM_CHOICES]]
With this package, we will assume that df is the dataframe which is
simply given to us. Instead of having the code itself find out how
many individuals, choices and coefficients or attributes there are, we
will simply feed them into the class. To perform a gradient descent
with this class, we will use the grad_desc method, using the df
from above as input,
import mdmpy
# In a typical case one would load df before this line
mdm = mdmpy.MDM(df, 4, 3, [0, 1, 2, 3])
np.random.seed(4)
init_beta = np.random.random(4)
grad_beta = mdm.grad_desc(init_beta)
print(grad_beta)
# expected output [0.30238122 0.07955214 0.86779824 0.50951981]
Solver
The MDM class acts as a wrapper and adds the necessary pyomo
variables and sets to model the problem, but requires a solver.
IPOPT, an
interior point solver, is recommended. If you have such a solver, it
can be called. Assuming IPOPT is being used:
import mdmpy
ipopt_exec_path = /path/to/ipopt # Replace with proper path
mdm = mdmpy.MDM(df, 4, 3, [0, 1, 2, 3])
mdm.model_init()
mdm.model_solve("ipopt",ipopt_exec_path)
print([mdm.m.beta[idx].value for idx in mdm.m.beta])
# expected output [0.30238834989235025, 0.07953888508425154, 0.8678050334295714, 0.5095096796373667]
Metadata
Release files for mdmpy 0.0.15.18
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mdmpy-0.0.15.18.tar.gz | 10.2 kB | Without provenance | Details for mdmpy-0.0.15.18.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mdmpy-0.0.15.18-py3-none-any.whl | Python 3 | none | any | Details for mdmpy-0.0.15.18-py3-none-any.whl |
Total release size: 20.1 kB
Release files / mdmpy-0.0.15.18.tar.gz
| Download URL | mdmpy-0.0.15.18.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f5dee41477f30898d1aba0862787c5cc4d58f8d7f9feed562e93bc63f4b3a171
|
|
BLAKE2b-256 checksum How to use checksums |
5899d5ed5964216881be8a483126388aab2f579fbc664f37a614b0517fe2f001
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / mdmpy-0.0.15.18-py3-none-any.whl
| Download URL | mdmpy-0.0.15.18-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0d6a60a6f8383da27cca66cdb6f2aefcfff3ec13c3fde6e391c4472385ee5e2e
|
|
BLAKE2b-256 checksum How to use checksums |
fbf8c68121f7b0853751996dff12e976ee44c77ed256b387316b69427f84cf24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Security for mdmpy 0.0.15.18
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.