Code Examples

Contents

Getting Started

You can download all of the code examples for the book here.

Notice! You will need the appropriate software (GAMS, Python, Stata, SAS, Matlab, Excel or NLOGIT) to run the code examples. See below to see how to set up the software.
Notice! The code is provided as-is and no support will be provided by the author or publisher of the book.

The table below contains a brief description of what each example does. For details on how to set up your computer to run the code examples, see the requirements section below.

Filter by language:

Chapters

File Name Language Keywords Relevant Chapters Description
Classical ME Example1.gms GAMS classical MaxEnt 4 Classical Maximum Entropy First Example
Classical Dual ME Example2.gms GAMS dual, concentrated, basic examples 4 Concentrated (Dual) Maximum Entropy Example
Classical Dual ME Example2 Surprisal GAMS surprisal, dual, concentrated 6 Classical concentrated Maximum Entropy example 2 with surprisal analysis.
Classical ME Dice Example.gms GAMS classical MaxEnt, dice 4 Classical Maximum Entropy Dice Example
Classical ME Dice Example.ipynb Python classical MaxEnt, dice 4 Classical Maximum Entropy Dice Example (IPython/Jupyter notebook)
Classical ME Dice Example surprisal.gms GAMS surprisal, dual, dice, classical 6 Classical Maximum Entropy Dice Example surprisal analysis.
Dual-Info.gms GAMS 4
Dice mata optimize.do Stata dice, classical, dual, cocentrated, additional examples 4 Stata dice example using Stata's Matrix Language
Dice NR.do Stata 9
Example 2 Dual ME STATA.do Stata 4
Inequality portfolio.gms GAMS portfolio, empirical examples 5
MV.gms GAMS portfolio, empirical examples 5,14 Minimum Variance portfolio example
Short sale portfolio.gms GAMS portfolio, empirical examples 5,13,14 Portfolio example with short sales
Simple case portfolio.gms GAMS portfolio, empirical examples 5,14 Simple portfolio example
Utility portfolio.gms GAMS portfolio, empirical examples 5,14
Smoker limdep.lim Limdep discrete choice, binomial, logit, empirical examples 12 Binomial Discrete Choice Model example (Limdep).
sas smoker.sas SAS discrete choice, binomial, logit, empirical examples 12 Binomial Discrete Choice Model example (SAS).
Smoker stata.do Stata discrete choice, binomial, logit, empirical examples 2 Binomial Discrete Choice Example (Stata).
Random multinomial.do Stata discrete choice, multinomial, empirical examples 12 Multinomial Discrete Choice example. Generates random data and presents estimates of the MSE for GME multinomial logit as well as for the multinomial logit.
gmemultinomial.ado Stata discrete choice, multinomial, empirical examples, ADO files 12 ADO file for multinomial discrete choice example. Needs to be installed for the multinomial model using MaxEnt approach to work.
gmentropylogit.ado Stata ADO files, logit, binomial, discrete choice 12 ADO file for binomial discrete choice (logit) model. Needs to be installed for the multinomial model using MaxEnt approach to work.
Linear auto.gms GAMS linear regression,empirical examples 13 Linear regression example
Linear random.gms GAMS linear regression,empirical examples 13 Linear regression example. Similar to linear auto.gms but generates it's own data.
GME auto.sas SAS linear regression,empirical examples 13 Linear regression in SAS using the example Automobile data
GME random.sas SAS linear regression, empirical examples 13 Linear regression in SAS. Similar to GME random.sas but generates it's own data.
GME auto example.do Stata linear regression, empirical examples 13 Linear regression example in Stata.
gmentropylinear.ado Stata ADO files, linear regression 13 ADO file for linear regression.
Matrix1.gms GAMS matrix, markov 12 Matrix balancing example
Matrix2.gms GAMS matrix 9 Matrix balancing example 2
Matrix3.gms GAMS matrix, markov 9 Matrix balancing example 3
dice two mean.do Stata noise, dice, two means, stochastic moments 9 Dice example with two means (noisy/stochastic moments) in Stata.
two mean dice loop.gms GAMS noise, dice, two means, stochastic moments 9 Dice example with two means (programmed using GAMS loop functionality)
two mean dice.gms GAMS noise, dice, two means, stochastic moments 9 Dice example with two means in GAMS.
Weighted.gms GAMS 5,14
dual_classical.py Python basic examples, classical, concentrated, dual 4 Optimize the dual concentrated maximum entropy objective.
matrix balancing.py Python matrix balancing, markov process 9 Classical ME CE and Dual formulations for the Matrix Balancing problem: y=Ax where A is a K by K matrix and coefficients of each one of the K columns sum up to 1 (i.e., proper distribution). Example 1: an 11 by 11 SAM of the US Comparing the primal and concentrated (dual) models Try with the given priors and with uniform priors
Primal Taylor.gms GAMS curvature 4
primal classical.py Python basic examples, classical 4 Maximize the classical maximum entropy objective function.
centropy hessian.gms GAMS hessian, curvature, classical MaxEnt, CrossEnt 2

GAMS code for classical ME and CE Including the ME dual case. Compare ME and CE for uniform and for correct priors as well as for incorrect priors. Compare dual and primal (look at shadow "prices" - moments.m, as well as speed of convergence and resources used.

NOTE: Can generate monotonically increasing/decreasing prob dist and X's

NOTE: Plots up to 20 observations

beer example.py Python 14
entropy function.py Python visualization, basic examples 2,3 Explore the properties of the Entropy function graphically. Contributed by Skipper Seabold and Alan Isaac
Firm Size Distribution Matlab,Excel empirical 5 Distribution of firm sizes based on industry input/output.
Entropy function simplex visualization Matlab visualization, theoretical 4 Visualization of the entropy function on a 3D simplex
Portfolio optimization toolbox Matlab empirical 5 A portfolio optimization toolbox. Includes Traditional Portfolio Optimization, Generalized Cross-entropy based Portfolio Optimization, Visualization, Performance Comparison
Income Distribution Python empirical,grouping property 5,8 Estimate the income distribution of the population based on tax brackets.
Simple Matrix Balancing Excel simmple matrix balancing,markov 4 Use Excel to explore the grouping property for a 2 dice example. (Please see the included README file to see how to set up Excel to run the example.)
R package for info-metrics R R package,ols,linear regression 4,5,6,7,8,9 R package for info-metrics

Requirements

This section describes how to set up your computer to run the code examples. The examples are written in several languages. Please select the language below to get details about how to set up your computer to run the code examples written in that language.

GAMS

The General Algebraic Modeling System (GAMS) is a proprietary software package that provides a high-level modeling system for mathematical programming and optimization. GAMS code is provided for many of the examples in the text.

Information about GAMS, including installation instructions and a users guide can be found here.

LIMDEP/NLOGIT

LIMDEP is a complete econometrics and statistics software package. LIMDEP code is provided for some examples in the text.

Python

Python is a general purpose, high-level programming language. We support Python 2.7 - 3.4. For more information about Python language visit the Official Python Site

You will need the python laguage interpreter and several packages in order to run the examples. If you are new to python, the easiest way to get started is by installing one of several Python Distributions.

Python Distributions

New to Python? Try a Python distribution already set up for scientific computing.

Python Package Resources

Already have Python installed? You can install additional packages from these resources.

IPython Notebooks

The IPython notebook is a web-based interactive computational environment that allows you to combine code, text, math markup, and plots provided as part of the IPython project. The Python code examples from the text are provided as IPython notebooks.

Stata

Stata code for binary, multinomial and linear regression are available through the Statistical Software Components (SSC) Archive. The relevant programs can be installed by issuing the following commands:

                                    ssc install gmentropylogit
                                    ssc install gmemultinomial
                                    ssc install gmentropylinear
                                
SAS

The Statistical Analysis System (SAS) provides the ENTROPY procedure for fitting many types of generalized maximum entropy models. Some SAS code is provided for examples in the text.

Detailed guidance about the entropy procedure can be found here

Excel

In order to run the examples, you will need to enable up the solver. You can find instructions on how to set up the solver here.

Matlab
Instructions on how to install Matlab on your system can be found on the Mathworks website: https://www.mathworks.com/. Some individual Matlab examples have corresponding Readme files. Please be sure to read these first before executing the code.

Additional Resources and Codes

Ximing Wu maintains a list of software resources for Info-Metrics here. These include R packages and scripts for MATLAB.

Contributing

Once you have a GitHub account, you can submit, correct, or improve any of the code distributed through a Pull Request. We welcome any and all contributions.