Skip to main content

A package that implements Marginal Distribution Models (MDMs)

Project description

MDM Py

Codacy Badge PyPI version

This package is a Python implementation of Marginal Distribution Models (MDMs), which can be used in Discrete Choice Modelling.

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]

Todo

  1. Add documentation.

    • Add more type hints, especially those involving Python builtins
  2. Add tests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mdmpy-0.0.15.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mdmpy-0.0.15.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file mdmpy-0.0.15.1.tar.gz.

File metadata

  • Download URL: mdmpy-0.0.15.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.1

File hashes

Hashes for mdmpy-0.0.15.1.tar.gz
Algorithm Hash digest
SHA256 7dd0a681683c4c660412adf0d1526d69ba4ec1c0cbd53f73169d7a13c32a4a5b
MD5 a3eaaf3853d012265d396ed9b3a19c11
BLAKE2b-256 dc1a46ae4c7eaf7589e138c2302affcfc5f67ff9831f2610d56258efcfa8f461

See more details on using hashes here.

File details

Details for the file mdmpy-0.0.15.1-py3-none-any.whl.

File metadata

  • Download URL: mdmpy-0.0.15.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.1

File hashes

Hashes for mdmpy-0.0.15.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ec30eb350100c3648939a735837dfd7e149a7a24d506a3fc96d7257d08aa37d1
MD5 77868310d4d22475f045be7e0be90c4d
BLAKE2b-256 4bc82096fa98a238efd02cbe0bba721b89680aca82b6fa948ef4bed3722cb75e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page