{ "metadata": { "name": "generic_mle" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from scipy import stats\n", "import statsmodels.api as sm\n", "from statsmodels.base.model import GenericLikelihoodModel" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print sm.datasets.spector.NOTE" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "data = sm.datasets.spector.load_pandas()\n", "exog = sm.add_constant(data.exog, prepend=True)\n", "endog = data.endog" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "sm_probit = sm.Probit(endog, exog).fit()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "raw", "metadata": {}, "source": [ "* To create your own Likelihood Model, you just need to overwrite the loglike method." ] }, { "cell_type": "code", "collapsed": false, "input": [ "class MyProbit(GenericLikelihoodModel):\n", " def loglike(self, params):\n", " exog = self.exog\n", " endog = self.endog\n", " q = 2 * endog - 1\n", " return stats.norm.logcdf(q*np.dot(exog, params)).sum()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "my_probit = MyProbit(endog, exog).fit()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print sm_probit.params" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print sm_probit.cov_params()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print my_probit.params" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "raw", "metadata": {}, "source": [ "You can get the variance-covariance of the parameters. Notice that we didn't have to provide Hessian or Score functions." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print my_probit.cov_params()" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }