{
 "metadata": {
  "name": "",
  "signature": "sha256:db4cb100feb256e662d3fa7f3ff2347df004518535f3116bea8e9207c6b0d378"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Discrete Choice Models Overview"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import print_function\n",
      "import numpy as np\n",
      "import statsmodels.api as sm"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Data\n",
      "\n",
      "Load data from Spector and Mazzeo (1980). Examples follow Greene's Econometric Analysis Ch. 21 (5th Edition)."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "spector_data = sm.datasets.spector.load()\n",
      "spector_data.exog = sm.add_constant(spector_data.exog, prepend=False)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Inspect the data:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(spector_data.exog[:5,:])\n",
      "print(spector_data.endog[:5])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Linear Probability Model (OLS)"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "lpm_mod = sm.OLS(spector_data.endog, spector_data.exog)\n",
      "lpm_res = lpm_mod.fit()\n",
      "print('Parameters: ', lpm_res.params[:-1])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Logit Model"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "logit_mod = sm.Logit(spector_data.endog, spector_data.exog)\n",
      "logit_res = logit_mod.fit(disp=0)\n",
      "print('Parameters: ', logit_res.params)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Marginal Effects"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "margeff = logit_res.get_margeff()\n",
      "print(margeff.summary())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "As in all the discrete data models presented below, we can print a nice summary of results:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(logit_res.summary())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Probit Model "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "probit_mod = sm.Probit(spector_data.endog, spector_data.exog)\n",
      "probit_res = probit_mod.fit()\n",
      "probit_margeff = probit_res.get_margeff()\n",
      "print('Parameters: ', probit_res.params)\n",
      "print('Marginal effects: ')\n",
      "print(probit_margeff.summary())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Multinomial Logit"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Load data from the American National Election Studies:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "anes_data = sm.datasets.anes96.load()\n",
      "anes_exog = anes_data.exog\n",
      "anes_exog = sm.add_constant(anes_exog, prepend=False)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Inspect the data:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(anes_data.exog[:5,:])\n",
      "print(anes_data.endog[:5])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Fit MNL model:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mlogit_mod = sm.MNLogit(anes_data.endog, anes_exog)\n",
      "mlogit_res = mlogit_mod.fit()\n",
      "print(mlogit_res.params)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Poisson\n",
      "\n",
      "Load the Rand data. Note that this example is similar to Cameron and Trivedi's `Microeconometrics` Table 20.5, but it is slightly different because of minor changes in the data. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "rand_data = sm.datasets.randhie.load()\n",
      "rand_exog = rand_data.exog.view(float).reshape(len(rand_data.exog), -1)\n",
      "rand_exog = sm.add_constant(rand_exog, prepend=False)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Fit Poisson model: "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "poisson_mod = sm.Poisson(rand_data.endog, rand_exog)\n",
      "poisson_res = poisson_mod.fit(method=\"newton\")\n",
      "print(poisson_res.summary())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Negative Binomial\n",
      "\n",
      "The negative binomial model gives slightly different results. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mod_nbin = sm.NegativeBinomial(rand_data.endog, rand_exog)\n",
      "res_nbin = mod_nbin.fit(disp=False)\n",
      "print(res_nbin.summary())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Alternative solvers\n",
      "\n",
      "The default method for fitting discrete data MLE models is Newton-Raphson. You can use other solvers by using the ``method`` argument: "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mlogit_res = mlogit_mod.fit(method='bfgs', maxiter=100)\n",
      "print(mlogit_res.summary())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}