├── .gitignore ├── Classification ├── LogitKNN.ipynb └── images │ ├── Fig2_14.png │ ├── Fig2_15.png │ ├── Fig2_16.png │ └── sigmoid.png ├── DynProg └── PS3ans.ipynb ├── GMM ├── GMM_SMM_notes.bib ├── GMM_SMM_notes.pdf ├── GMM_SMM_notes.tex ├── GMMest.ipynb ├── data │ └── Econ381totpts.txt └── images │ └── MLEplots.png ├── Interp └── Interp.ipynb ├── KDE ├── 05.13-Kernel-Density-Estimation.ipynb ├── KDE.ipynb ├── KDE_probset.pdf ├── data │ ├── BQ_mat_raw_ITA.txt │ └── geyser.csv └── images │ ├── PDSH-cover-small.png │ └── SixKernelOptions.png ├── MLE ├── MLest.ipynb ├── data │ └── Econ381totpts.txt └── images │ ├── GBtree.png │ └── MLEplots.png ├── NumIntegr ├── NumIntegr.ipynb └── images │ ├── Integration_num_trapezes_notation.png │ ├── MidRiemann2.png │ ├── MonteCarloCircle.png │ ├── NumInt_ComparisonWHNB1000.png │ ├── NumInt_Discrepancy.png │ ├── NumInt_TrapezoidGrid.png │ ├── Riemann_sum_convergence.png │ ├── Simpsons_method_illustration.png │ └── Simpsonsrule2.gif ├── README.md └── SMM ├── SMMest.ipynb ├── data ├── Econ381totpts.txt ├── MacroSeries.txt ├── clms.txt └── usincmoms.txt └── images ├── GBtree.png ├── MLEplots.png ├── critvals_1.png └── hist_pdf_1.png /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | MANIFEST 3 | build/* 4 | dist/* 5 | *.aux 6 | *.bbl 7 | *.blg 8 | *.fdb_latexmk 9 | *.idx 10 | *.ilg 11 | *.ind 12 | *.lof 13 | *.log 14 | *.lot 15 | *.out 16 | *.pdfsync 17 | *.synctex.gz 18 | *.toc 19 | *.swp 20 | *.asv 21 | *.nav 22 | *.snm 23 | *.gz 24 | *.bib.bak 25 | *.fls 26 | *.m~ 27 | *.sublime* 28 | .DS_Store 29 | *puf.csv 30 | */OUTPUT/* 31 | */Python/*/images/* 32 | */__pycache__/* 33 | *.dta 34 | */.ipynb_checkpoints/* 35 | -------------------------------------------------------------------------------- /Classification/images/Fig2_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/Classification/images/Fig2_14.png -------------------------------------------------------------------------------- /Classification/images/Fig2_15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/Classification/images/Fig2_15.png -------------------------------------------------------------------------------- /Classification/images/Fig2_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/Classification/images/Fig2_16.png -------------------------------------------------------------------------------- /Classification/images/sigmoid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/Classification/images/sigmoid.png -------------------------------------------------------------------------------- /GMM/GMM_SMM_notes.bib: -------------------------------------------------------------------------------- 1 | @BOOK{AddaCooper:2003, 2 | AUTHOR = {J\'{e}r\^{o}me Adda and Russell Cooper}, 3 | TITLE = {Dynamic Economics: Quantitative Methods and Applications}, 4 | PUBLISHER = {MIT Press}, 5 | YEAR = {2003}, 6 | } 7 | 8 | @BOOK{DavidsonMacKinnon:2004, 9 | AUTHOR = {Russell Davidson and James G. MacKinnon}, 10 | TITLE = {Econometric Theory and Methods}, 11 | PUBLISHER = {Oxford University Press}, 12 | YEAR = {2004}, 13 | } 14 | 15 | @ARTICLE{DuffieSingleton:1993, 16 | AUTHOR = {Darrell Duffie and Kenneth J. Singleton}, 17 | TITLE = {Simulated Moment Estimation of Markov Models of Asset Prices}, 18 | JOURNAL = {Econometrica}, 19 | YEAR = {1993}, 20 | volume = {61}, 21 | number = {4}, 22 | month = {July}, 23 | pages = {929-952}, 24 | } 25 | 26 | @BOOK{GourierouxMonfort:1996, 27 | AUTHOR = {Christian Gourieroux and Alain Monfort}, 28 | TITLE = {Simulation-based Econometric Methods}, 29 | PUBLISHER = {Oxford University Press}, 30 | YEAR = {1996}, 31 | } 32 | 33 | @ARTICLE{LaroqueSalanie:1993, 34 | AUTHOR = {G. Laroque and B. Salanie}, 35 | TITLE = {Simulation Based Estimation Models with Lagged Latent Variables}, 36 | JOURNAL = {Journal of Applied Econometrics}, 37 | YEAR = {1993}, 38 | volume = {8}, 39 | number = {Supplement}, 40 | month = {December}, 41 | pages = {119-133}, 42 | } 43 | 44 | @ARTICLE{LeeIngram:1991, 45 | AUTHOR = {{Bong-Soo} Lee and Beth Fisher Ingram}, 46 | TITLE = {Simulation Based Estimation of Time-series Models}, 47 | JOURNAL = {Journal of Econometrics}, 48 | YEAR = {1991}, 49 | volume = {47}, 50 | number = {2-3}, 51 | month = {February}, 52 | pages = {197-205}, 53 | } 54 | 55 | @ARTICLE{McFadden:1989, 56 | AUTHOR = {Daniel McFadden}, 57 | TITLE = {A Method of Simulated Moments for Estimation of Discrete Response Models without Numerical Integration}, 58 | JOURNAL = {Econometrica}, 59 | YEAR = {1989}, 60 | volume = {57}, 61 | number = {5}, 62 | month = {September}, 63 | pages = {995-1026}, 64 | } 65 | 66 | @BOOK{Varian:1992, 67 | AUTHOR = {Hal R. Varian}, 68 | TITLE = {Microeconomic Analysis}, 69 | PUBLISHER = {W. W. Norton \& Company}, 70 | YEAR = {1992}, 71 | edition = {third edition}, 72 | } 73 | -------------------------------------------------------------------------------- /GMM/GMM_SMM_notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/GMM/GMM_SMM_notes.pdf -------------------------------------------------------------------------------- /GMM/GMM_SMM_notes.tex: -------------------------------------------------------------------------------- 1 | \documentclass[letterpaper,12pt]{article} 2 | 3 | \usepackage{threeparttable} 4 | \usepackage{geometry} 5 | \geometry{letterpaper,tmargin=1in,bmargin=1in,lmargin=1.25in,rmargin=1.25in} 6 | \usepackage[format=hang,font=normalsize,labelfont=bf]{caption} 7 | \usepackage{amsmath} 8 | \usepackage{multirow} 9 | \usepackage{array} 10 | \usepackage{delarray} 11 | \usepackage{amssymb} 12 | \usepackage{amsthm} 13 | \usepackage{lscape} 14 | \usepackage{natbib} 15 | \usepackage{setspace} 16 | \usepackage{float,color} 17 | \usepackage[pdftex]{graphicx} 18 | \usepackage{listings} 19 | \lstset{basicstyle=\footnotesize\ttfamily, language=Python, showstringspaces=false} 20 | \usepackage{pdfsync} 21 | \usepackage{verbatim} 22 | \usepackage{placeins} 23 | \usepackage{geometry} 24 | \usepackage{pdflscape} 25 | \synctex=1 26 | \usepackage{hyperref} 27 | \hypersetup{colorlinks,linkcolor=red,urlcolor=blue,citecolor=red} 28 | \usepackage{bm} 29 | 30 | 31 | \theoremstyle{definition} 32 | \newtheorem{theorem}{Theorem} 33 | \newtheorem{acknowledgement}[theorem]{Acknowledgement} 34 | \newtheorem{algorithm}[theorem]{Algorithm} 35 | \newtheorem{axiom}[theorem]{Axiom} 36 | \newtheorem{case}[theorem]{Case} 37 | \newtheorem{claim}[theorem]{Claim} 38 | \newtheorem{conclusion}[theorem]{Conclusion} 39 | \newtheorem{condition}[theorem]{Condition} 40 | \newtheorem{conjecture}[theorem]{Conjecture} 41 | \newtheorem{corollary}[theorem]{Corollary} 42 | \newtheorem{criterion}[theorem]{Criterion} 43 | \newtheorem{definition}{Definition} % Number definitions on their own 44 | \newtheorem{derivation}{Derivation} % Number derivations on their own 45 | \newtheorem{example}[theorem]{Example} 46 | \newtheorem{exercise}[theorem]{Exercise} 47 | \newtheorem{lemma}[theorem]{Lemma} 48 | \newtheorem{notation}[theorem]{Notation} 49 | \newtheorem{problem}[theorem]{Problem} 50 | \newtheorem{proposition}{Proposition} % Number propositions on their own 51 | \newtheorem{remark}[theorem]{Remark} 52 | \newtheorem{solution}[theorem]{Solution} 53 | \newtheorem{summary}[theorem]{Summary} 54 | \bibliographystyle{aer} 55 | \newcommand\ve{\varepsilon} 56 | %\renewcommand\theenumi{\roman{enumi}} 57 | \newcommand\norm[1]{\left\lVert#1\right\rVert} 58 | \newcommand\abs[1]{\left\lvert#1\right\rvert} 59 | 60 | \begin{document} 61 | 62 | \title{GMM and SMM notes and comments for Tyler Ransom} 63 | \author{Richard W. Evans} 64 | \maketitle 65 | 66 | \pagenumbering{arabic} 67 | 68 | 69 | \section{GMM}\label{SecGMM} 70 | 71 | \setcounter{subsection}{-1} 72 | 73 | \subsection{GMM General Specification}\label{SecGMMgen} 74 | 75 | My most general definition of GMM is the following. Let $\bm{x}$ be an $N\times M$ matrix of data with $N$ observations (rows) and $M$ variables (columns) and typical element $x_{n,m}$. Let $\bm{\theta}\in\mathbb{R}^K$ be the $K$-dimensional parameter vector to be estimated, and let $\bm{\hat{\theta}}_{GMM}$ be the GMM estimator of that parameter vector. The GMM estimator is the one that minimizes some general norm (distance measure) on an $R$-dimensional vector of model moments $\bm{m}\bigl(\bm{x}|\bm{\theta}\bigr)$ and a corresponding $R$-dimensional vector of data moments $\bm{m}\bigl(\bm{x}\bigr)$ such that $R\geq K$. 76 | \begin{equation}\label{EqGMMdefgen} 77 | \bm{\hat{\theta}}_{GMM}:\quad \min_{\bm{\theta}}\norm{\bm{m}\bigl(\bm{x}|\bm{\theta}\bigr) - \bm{m}\bigl(\bm{x}\bigr)} 78 | \end{equation} 79 | 80 | The most common GMM criterion function or choice of norm is a weighted sum of squared moment errors. We can first define an $R\times 1$ error vector $\bm{e}\bigl(\bm{x},\bm{\theta}\bigr)$ as the following function of the data $\bm{x}$ and the parameter vector $\bm{\theta}$ which can either be given in levels or in percentage differences (there are advantages to each). 81 | \begin{equation}\label{EqGMMerrvec} 82 | \bm{e}\bigl(\bm{x},\bm{\theta}\bigr)\equiv 83 | \begin{cases} 84 | \bm{m}\bigl(\bm{x}|\bm{\theta}\bigr) - \bm{m}\bigl(\bm{x}\bigr) \\ 85 | \quad\text{or} \\ 86 | \frac{\bm{m}\bigl(\bm{x}|\bm{\theta}\bigr) - \bm{m}\bigl(\bm{x}\bigr)}{\bm{m}\bigl(\bm{x}\bigr)} 87 | \end{cases} 88 | \end{equation} 89 | 90 | The most common weighted sum of squared moment errors GMM specification takes the following form, 91 | \begin{equation}\label{EqGMMdef} 92 | \bm{\hat{\theta}}_{GMM}:\quad \min_{\bm{\theta}}\:\bm{e}\bigl(\bm{x},\bm{\theta}\bigr)^T\:\bm{W}\:\bm{e}\bigl(\bm{x},\bm{\theta}\bigr) 93 | \end{equation} 94 | where $\bm{W}$ is an $R\times R$ weighting matrix. The standard arguments regarding asymptotic biasedness and efficiency apply to the weighting matrix. An optimal weighting matrix is most efficient (inverse variance estimators, take care of heteroscedasticity and autocorrelation), but identity matrix is unbiased. 95 | 96 | 97 | \subsection{GMM Linear Regression: Method 1}\label{SecGMM1} 98 | 99 | Define a specific linear regression model as the following equation with the following assumptions on the error terms, 100 | \begin{equation}\label{EqLinRegGen} 101 | \begin{split} 102 | y_i = &\beta_0 + \beta_1 x_{1,i} + \beta_2 x_{2,i} + ... \beta_K x_{K,i} + \ve_i \\ 103 | &\text{where}\quad \ve_i\sim f(\ve), \quad E[\ve_i]=0,\quad\text{and}\quad E[x_{k,i}\ve_i]=0 \quad\forall k 104 | \end{split} 105 | \end{equation} 106 | where the data are the matrix of $i=1,2,...N$ observations on $K + 1$ variables $\bigl(y_i,x_{1,i},x_{2,i},...x_{K,i}\bigr)$, the parameter vector to be estimated is the $K+1$ vector $\bm{\beta}=(\beta_0,\beta_1,\beta_2,...\beta_K)$, and the error terms $\ve_i$ are i.i.d. according to the general pdf $f(\ve)$ with the moment conditions $E[\ve_i]=0$ and $E[x_{k,i}\ve_i]=0$ for all $k$. 107 | 108 | The most standard way of teaching GMM using linear regression is to use the $K+1$ moments implied by the unconditional expected value condition $E[\ve_i]=0$ and the $K$ conditional expected value conditions $E[x_{k,i}\ve_i]=0$ for all $k$. To map this into the notation in Section \ref{SecGMMgen}, let the vector of data moments be the vector of $K+1$ zeros assumed by the $K+1$ zero-expected values on $\ve_i$. 109 | \begin{equation}\label{EqLinRegDataMoms1} 110 | \bm{m}\bigl(\bm{y},\bm{x}\bigr) = 111 | \begin{bmatrix} 112 | E[\ve_i] \\ E[x_{1,i}\ve_i] \\ E[x_{2,i}\ve_i] \\ \vdots \\ E[x_{K,i}\ve_i] 113 | \end{bmatrix} = 114 | \begin{bmatrix} 0 \\ 0 \\ 0 \\ \vdots \\ 0 115 | \end{bmatrix} 116 | \end{equation} 117 | These $K\times 1$ data moments are zero by assumption and do not actually use the data $\bm{y}$ or $\bm{x}$ in this case. 118 | 119 | The model moments from Section \ref{SecGMMgen} were specified as a function of data and parameters $\bm{m}\bigl(\bm{x}|\bm{\theta}\bigr)$. In this linear regression case, the $K\times 1$ model moments $m_k$ are defined by solving the linear regression model \eqref{EqLinRegGen} for $\ve_i$ and taking the sample averages that correspond to the assumed unconditional and conditional expectations on $\ve_i$. 120 | \begin{equation}\label{EqLinRegModelMoms1} 121 | \bm{m}\bigl(\bm{y},\bm{x}|\bm{\beta}\bigr) = 122 | \begin{bmatrix} 123 | \frac{1}{N}\sum_{i=1}^N\bigl(y_i - \beta_0 - \beta_1 x_{1,i} - \beta_2 x_{2,i} - ... \beta_K x_{K,i}\bigr) \\ \frac{1}{N}\sum_{i=1}^N\bigl(y_i - \beta_0 - \beta_1 x_{1,i} - \beta_2 x_{2,i} - ... \beta_K x_{K,i}\bigr)x_{1,i} \\ \frac{1}{N}\sum_{i=1}^N\bigl(y_i - \beta_0 - \beta_1 x_{1,i} - \beta_2 x_{2,i} - ... \beta_K x_{K,i}\bigr)x_{2,i} \\ \vdots \\ \frac{1}{N}\sum_{i=1}^N\bigl(y_i - \beta_0 - \beta_1 x_{1,i} - \beta_2 x_{2,i} - ... \beta_K x_{K,i}\bigr)x_{K,i} 124 | \end{bmatrix} 125 | \end{equation} 126 | 127 | Define the moment error vector, analogous to \eqref{EqGMMerrvec} above, as the following function of data and parameters. 128 | \begin{equation}\label{EqGMMerrvec1} 129 | \bm{e}\bigl(\bm{y},\bm{x},\bm{\beta}\bigr)\equiv \bm{m}\bigl(\bm{y},\bm{x}|\bm{\beta}\bigr) - \bm{m}\bigl(\bm{y},\bm{x}\bigr) = \bm{m}\bigl(\bm{y},\bm{x}|\bm{\beta}\bigr) 130 | \end{equation} 131 | Note that we cannot use the percent deviation form of the moment errors shown in the second specification of \eqref{EqGMMerrvec} because the denominator is zero. 132 | 133 | The GMM estimator in this case $\bm{\hat{\beta}}_{GMM1}$ is the following exactly identified problem of choosing $K+1$ parameters to minimize the weighted sum of squared errors in $K+1$ moment conditions, 134 | \begin{equation}\label{EqGMMdef1} 135 | \bm{\hat{\beta}}_{GMM1}:\quad \min_{\bm{\beta}}\:\bm{e}\bigl(\bm{y},\bm{x},\bm{\beta}\bigr)^T\:\bm{W}\:\bm{e}\bigl(\bm{y},\bm{x},\bm{\beta}\bigr) 136 | \end{equation} 137 | where $\bm{W}$ is a $\bigl(K+1\times K+1\bigr)$ weighting matrix. 138 | 139 | 140 | \subsection{GMM Linear Regression: Method 2}\label{SecGMM2} 141 | 142 | The second method for estimating the parameter vector $\bm{\beta}$ by GMM is just weighted linear least squares. For nonlinear model cases, this is just weighted nonlinear least squares. This ends up being an overidentified GMM estimation in which each error term is treated as a model moment. This approach does not really use any of the expected value of $\ve_i$ conditions in \eqref{EqLinRegGen}. It just uses the idea that we want to choose the parameter vector $\bm{\beta}$ to maximize the fit of the predicted values of the endogenous variable $\hat{y}_i$ to the data values of the endogenous variable $y_i$. This is equivalent to minimizing the absolute value of the error terms $\ve_i$. 143 | 144 | Similar to Method 1 in Section \ref{SecGMM1}, we assume the data moments are a vector of zeros. However, in this case, the data moment vector is length $N$, a zero for each observation of $\ve_i$. 145 | \begin{equation}\label{EqLinRegDataMoms2} 146 | \bm{m}\bigl(\bm{y},\bm{x}\bigr) = 147 | \begin{bmatrix} 148 | \min\abs{\ve_1} \\ \min\abs{\ve_2} \\ \vdots \\ \min\abs{\ve_N} 149 | \end{bmatrix} = 150 | \begin{bmatrix} 0 \\ 0 \\ \vdots \\ 0 151 | \end{bmatrix} 152 | \end{equation} 153 | Similar to \eqref{EqLinRegDataMoms1}, these $N\times 1$ data moments are zero by assumption and do not actually use the data $\bm{y}$ or $\bm{x}$ in this case. 154 | 155 | The model moments are simply the model error terms found by solving linear regression equation \eqref{EqLinRegGen} for $\ve_i$ for each of the $i=1,2,...N$ observations. Note that each model error term is a function of data $\bm{y}$ and $\bm{x}$ and paramters $\bm{\beta}$. 156 | \begin{equation}\label{EqLinRegModelMoms2} 157 | \bm{m}\bigl(\bm{y},\bm{x}|\bm{\beta}\bigr) = 158 | \begin{bmatrix} 159 | y_1 - \beta_0 - \beta_1 x_{1,1} - \beta_2 x_{2,1} - ... \beta_K x_{K,1} \\ 160 | y_2 - \beta_0 - \beta_1 x_{1,2} - \beta_2 x_{2,2} - ... \beta_K x_{K,2} \\ \vdots \\ 161 | y_N - \beta_0 - \beta_1 x_{1,N} - \beta_2 x_{2,N} - ... \beta_K x_{K,N} 162 | \end{bmatrix} 163 | \end{equation} 164 | 165 | Define the $N\times 1$ moment error vector, analogous to \eqref{EqGMMerrvec}, as the following function of data and parameters. 166 | \begin{equation}\label{EqGMMerrvec2} 167 | \bm{e}\bigl(\bm{y},\bm{x},\bm{\beta}\bigr)\equiv \bm{m}\bigl(\bm{y},\bm{x}|\bm{\beta}\bigr) - \bm{m}\bigl(\bm{y},\bm{x}\bigr) = \bm{m}\bigl(\bm{y},\bm{x}|\bm{\beta}\bigr) 168 | \end{equation} 169 | As with \eqref{EqGMMerrvec1}, we cannot use the percent deviation form of the moment errors shown in the second specification of \eqref{EqGMMerrvec} because the denominator is zero. 170 | 171 | The GMM estimator in this case $\bm{\hat{\beta}}_{GMM2}$ is the following overidentified problem of choosing $K+1$ parameters to minimize the weighted sum of squared errors in $N$ moment conditions, 172 | \begin{equation}\label{EqGMMdef2} 173 | \bm{\hat{\beta}}_{GMM2}:\quad \min_{\bm{\beta}}\:\bm{e}\bigl(\bm{y},\bm{x},\bm{\beta}\bigr)^T\:\bm{W}\:\bm{e}\bigl(\bm{y},\bm{x},\bm{\beta}\bigr) 174 | \end{equation} 175 | where $\bm{W}$ is a $\bigl(N\times N\bigr)$ weighting matrix. 176 | 177 | 178 | \subsection{GMM Comparison of Methods}\label{SecGMMcomp} 179 | 180 | 181 | 182 | \section{SMM}\label{SecSMM} 183 | 184 | \setcounter{subsection}{-1} 185 | 186 | \subsection{SMM General Specification}\label{SecSMMgen} 187 | 188 | Simulated method of moments (SMM) is analogous to the generalized method of moments (GMM) estimator. SMM could really be thought of as a particular type of GMM estimator. The SMM estimator chooses model parameters $\bm{\theta}$ to make simulated model moments match data moments. Seminal papers developing SMM are \citet{McFadden:1989}, \citet{LeeIngram:1991}, and \citet{DuffieSingleton:1993}. Good textbook treatments of SMM are found in \citet[pp.87-100]{AddaCooper:2003} and \citet[pp. 383-394]{DavidsonMacKinnon:2004}. 189 | 190 | In the general specification of GMM estimation from Section \ref{SecGMMgen}, we use data $\bm{x}$ and model parameters $\bm{\theta}$ to minimize the distance between model moments $\bm{m}(\bm{x}|\bm{\theta})$ and data moments $\bm{m}(\bm{x})$. 191 | \begin{equation}\tag{\ref{EqGMMdefgen}} 192 | \bm{\hat{\theta}}_{GMM}:\quad \min_{\bm{\theta}}\norm{\bm{m}\bigl(\bm{x}|\bm{\theta}\bigr) - \bm{m}\bigl(\bm{x}\bigr)} 193 | \end{equation} 194 | The following difficulties can arise with GMM making it not possible or very difficult. 195 | \begin{itemize} 196 | \item The model moment function $\bm{m}(\bm{x}|\bm{\theta})$ is not known analytically. 197 | \item The data moments you are trying to match come from another model (indirect inference). 198 | \item The model moments $\bm{m}(\bm{x}|\bm{\theta})$ are derived from latent variables that are not observed by the modeler. You only have moments, not the underlying data. See \citet{LaroqueSalanie:1993}. 199 | \item The model moments $\bm{m}(\bm{x}|\bm{\theta})$ are derived from censored variables that are only partially observed by the modeler. 200 | \item The model moments $\bm{m}(\bm{x}|\bm{\theta})$ are just difficult to derive analytically. Examples include moments that include multiple integrals over nonlinear functions as in \citet{McFadden:1989}. 201 | \end{itemize} 202 | 203 | Let $\bm{x}$ be the $N\times M$ vector of data as in Section \ref{SecGMMgen}, and let $\bm{\tilde{x}}_s$ be the $s$th simulation of the dataset $\bm{x}$. SMM estimation is simply to simulate the model data $S$ times, and use the average values of the moments from the simulated data as the estimator for the model moments. Let $\bm{\tilde{x}}=\{\bm{\tilde{x}}_1,\bm{\tilde{x}}_2,...\bm{\tilde{x}}_s,...\bm{\tilde{x}}_S\}$ be the $S$ simulations of the model data. 204 | 205 | It is important to note a major difference between SMM and GMM. In the GMM examples in Section \ref{SecGMM}, the parameter vector $\bm{\theta}$ could be estimated without strong distributional assumptions about the random variable error term $\ve_i$. In the first linear regression example in Section \ref{SecGMM1} with the $K+1$ moments, all that was needed was that the errors be distributed i.i.d. according to general pdf $\ve_i\sim f(\ve)$ and the two sets of moment conditions on the distribution of the errors $E[\ve_i]=0$ and $E[\ve_i x_{k,i}]=0$. In SMM, we need to know the exact distribution $f(\ve)$ in order to simulate the data. For example, if we assume the errors are distributed normally, we need to know the parameters of the distribution (variance $\sigma^2$ in the case of the normal distribution). We need to be able 206 | 207 | The $R\times 1$ data moment vector $\bm{m}\bigl(\bm{x}\bigr)$ is the same as in the GMM specification of Section \ref{SecGMMgen}. We can compute a set of $R$ model moments $\bm{m}\bigl(\bm{\tilde{x}}_s|\bm{\theta}\bigr)$ from each of the simulated datasets $\bm{\tilde{x}}_s$. One conceptual advantage of SMM relative to GMM is that the model moments are computed in the same way as the data moments because the model moments are computed from simulated datasets $\bm{\tilde{x}}_s$ that are supposed to be representative of the real world dataset $\bm{x}$. The final model moment vector $\bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr)$ to be used in SMM is an average across each of the moment vectors for each simulation $\bm{m}\bigl(\bm{\tilde{x}}_s|\bm{\theta}\bigr)$. 208 | \begin{equation}\label{EqSMMmodmomgen} 209 | \bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr) \equiv \frac{1}{S}\sum_{s=1}^S\bm{m}\bigl(\bm{\tilde{x}}_s|\bm{\theta}\bigr) 210 | \end{equation} 211 | 212 | Once we have an estimate of the model moments $\bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr)$ from our $S$ simulations, SMM estimation is very similar to our presentation of GMM. The SMM approach of estimating the parameter vector $\bm{\hat{\theta}}_{SMM}$ is to choose $\bm{\theta}$ to minimize some distance measure of the simulated model moments $\bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr)$ from the data moments $\bm{m}\bigl(\bm{x}\bigr)$. 213 | \begin{equation}\label{EqSMMdefgen} 214 | \bm{\hat{\theta}}_{SMM}:\quad \min_{\bm{\theta}}\norm{\bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr) - \bm{m}\bigl(\bm{x}\bigr)} 215 | \end{equation} 216 | 217 | The distance measure $||\bm{\hat{m}}(\bm{\tilde{x}}|\bm{\theta})-\bm{m}(\bm{x})||$ can be any kind of norm. But it is important to recognize that your estimates $\bm{\hat{\theta}}_{SMM}$ will be dependent on what distance measure (norm) you choose. The most widely studied and used distance metric in GMM and SMM estimation is the $L^2$ norm or the weighted sum of squared errors in moments. Define the moment error function $\bm{e}(\bm{\tilde{x}},\bm{x}|\bm{\theta})$ as the simple difference or percent difference in the vector of simulated model moments from the data moments. 218 | \begin{equation}\label{EqSMMerrvec} 219 | \bm{e}\bigl(\bm{\tilde{x}},\bm{x},\bm{\theta}\bigr)\equiv 220 | \begin{cases} 221 | \bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr) - \bm{m}\bigl(\bm{x}\bigr) \\ 222 | \quad\text{or} \\ 223 | \frac{\bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr) - \bm{m}\bigl(\bm{x}\bigr)}{\bm{m}\bigl(\bm{x}\bigr)} 224 | \end{cases} 225 | \end{equation} 226 | The weighted sum of squared simulated moment errors SMM specification takes the following form, 227 | \begin{equation}\label{EqSMMdef} 228 | \bm{\hat{\theta}}_{SMM}:\quad \min_{\bm{\theta}}\:\bm{e}\bigl(\bm{\tilde{x}},\bm{x},\bm{\theta}\bigr)^T\:\bm{W}\:\bm{e}\bigl(\bm{\tilde{x}},\bm{x},\bm{\theta}\bigr) 229 | \end{equation} 230 | where $W$ is a $R\times R$ weighting matrix in the criterion function. We call the quadratic form expression $e(\tilde{x},x|\theta)^T \, W \, e(\tilde{x},x|\theta)$ the criterion function because it is a strictly positive scalar that is the object of the minimization in the SMM problem statement. The $R\times R$ weighting matrix $W$ in the criterion function allows the econometrician to control how each moment is weighted in the minimization problem. For example, an $R\times R$ identity matrix for $W$ would give each moment equal weighting, and the criterion function would be a simply sum of squared percent deviations (errors). Other weighting strategies can be dictated by the nature of the problem or model. 231 | 232 | One last item to emphasize with SMM is that the errors that are drawn for the $S$ simulations of the model must be drawn only once so that the minimization problem for $\bm{\hat{\theta}}_{SMM}$ does not have the underlying sampling changing for each guess of a value of $\bm{\theta}$. Put more simply, you want the random draws for all the simulations to be held constant so that the only thing changing in the minimization problem is the value of the vector of parameters $\bm{\theta}$. 233 | 234 | 235 | \subsection{SMM Linear Regression: Method 1}\label{SecSMM1} 236 | 237 | The SMM approach analogous to the GMM approach in Section \ref{SecGMM1} does not work. In most cases, the parameters are not identified. Loss of identification comes first because we have a maximum of $K+1$ moments with potentially $K+2$ parameters to estimate. A second characteristic that takes away a degree of freedom in this case is that one of the moment conditions $E[\ve_i]=0$ might be true for many parameter values in the assumed random variable distribution 238 | 239 | In SMM, we need to modify the regression equation \eqref{EqLinRegGen} to include a specific parameterized pdf for the i.i.d. random shocks $\ve_i$. We assume here a normal distribution, which could add another parameter $\sigma$ to our vector of parameters to be estimated $\bm{\theta}$. 240 | \begin{equation}\label{EqLinRegGen2} 241 | \begin{split} 242 | y_i = &\beta_0 + \beta_1 x_{1,i} + \beta_2 x_{2,i} + ... \beta_K x_{K,i} + \ve_i \\ 243 | &\text{where}\quad \ve_i\sim N(0,\sigma^2), \quad E[\ve_i]=0,\quad\text{and}\quad E[x_{k,i}\ve_i]=0 \quad\forall k 244 | \end{split} 245 | \end{equation} 246 | The $K+1$ model moment conditions are either not functions of the parameters 247 | 248 | 249 | \subsection{SMM Linear Regression: Method 2}\label{SecSMM2} 250 | 251 | Only the SMM method analogous to the second GMM method in Section \ref{SecGMM2} will work. The data $\bm{y}$ and $\bm{x}$ in linear regression model \eqref{EqLinRegGen2} has $N$ observations on one dependent variable $y_i$ and $K$ independent variables $x_{k,i}$. The parameter vector to be estimated is now $\bm{\theta}=\bigl(\beta_0,\beta_1,...\beta_K, \sigma\bigr)$, which has length $K+2$ and includes the standard deviation $\sigma$ of the mean-zero, normally distributed random shocks $\ve_i$. 252 | 253 | In this case, let the vector of data moments be the vector of $N$ dependent variable values $y_i$. 254 | \begin{equation}\label{EqLinRegSMMDataMoms2} 255 | \bm{m}\bigl(\bm{y},\bm{x}\bigr) = \bm{m}\bigl(\bm{y}\bigr) = 256 | \begin{bmatrix} 257 | y_1 \\ y_2 \\ \vdots \\ y_N 258 | \end{bmatrix} = \bm{y} 259 | \end{equation} 260 | Then let the data moments for the $s$th simulation be the vector of predicted values including the simulated error terms $\ve_{s,i}$, 261 | \begin{equation}\label{EqSMMlinregsimmodmoms2} 262 | \bm{m}\bigl(\bm{\tilde{y}}_s,\bm{\tilde{x}}_s|\bm{\theta}\bigr) = \bm{m}\bigl(\bm{\tilde{x}}_s|\bm{\theta}\bigr) = 263 | \begin{bmatrix} 264 | \beta_0 + \beta_1 x_{1,1} + \beta_2 x_{2,1} + ... \beta_K x_{K,1} + \ve_{1,s} \\ 265 | \beta_0 + \beta_1 x_{1,2} + \beta_2 x_{2,2} + ... \beta_K x_{K,2} + \ve_{2,s} \\ 266 | \vdots \\ 267 | \beta_0 + \beta_1 x_{1,N} + \beta_2 x_{2,N} + ... \beta_K x_{K,N} + \ve_{N,s} 268 | \end{bmatrix} = 269 | \begin{bmatrix} 270 | \hat{y}_{1,s} \\ \hat{y}_{2,s} \\ \vdots \\ \hat{y}_{N,s} 271 | \end{bmatrix} = \bm{\hat{y}}_s 272 | \end{equation} 273 | where $\ve_{i,s}$ is the $i$th observation error term in the $s$th simulation of the errors, and $\hat{y}_{i,s}$ is the $i$th observation predicted value of the dependent variable in the $s$th simulation of the errors. 274 | 275 | The final model moment vector $\bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr)$ to be used in SMM is an average across each of the moment vectors for each simulation $\bm{m}\bigl(\bm{\tilde{x}}_s|\bm{\theta}\bigr)$. 276 | \begin{equation}\label{EqSMMmodmomgenlinreg2} 277 | \bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr) \equiv \frac{1}{S}\sum_{s=1}^S\bm{m}\bigl(\bm{\tilde{x}}_s|\bm{\theta}\bigr) = \frac{1}{S}\sum_{s=1}^S\bm{\hat{y}_s} = \bm{\hat{y}} 278 | \end{equation} 279 | 280 | Define the moment error function as the following. 281 | \begin{equation} 282 | \bm{e}\bigl(\bm{\tilde{x}},\bm{y},\bm{\theta}\bigr) = \bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr) - \bm{m}\bigl(\bm{y}\bigr) = \bm{\hat{y}} - \bm{y} 283 | \end{equation} 284 | The SMM approach of estimating the parameter vector $\bm{\hat{\theta}}_{SMM}$ is to choose $\bm{\theta}$ to minimize some distance measure of the simulated model moments $\bm{\hat{m}}\bigl(\bm{\tilde{x}}|\bm{\theta}\bigr)$ from the data moments $\bm{m}\bigl(\bm{x}\bigr)$. 285 | \begin{equation}\label{EqSMMdeflinreg2} 286 | \bm{\hat{\theta}}_{SMM}:\quad \min_{\bm{\theta}}\:\bm{e}\bigl(\bm{\tilde{x}},\bm{y},\bm{\theta}\bigr)^T \: \bm{W} \: \bm{e}\bigl(\bm{\tilde{x}},\bm{y},\bm{\theta}\bigr) 287 | \end{equation} 288 | 289 | \begin{enumerate} 290 | \item Draw $S$ samples of $N$ uniformly distributed random errors $u_{s,i}\sim U(0,1)$ such that the sample of errors for the $s$th simulation is $\bm{u}_s=(u_{s,1}, u_{s,2},...u_{s,i},...u_{s,N})$. 291 | \item Guess values for the parameter vector to be estimated $\bm{\theta}_v = (\beta_{0,v},\beta_{1,v},...\beta_{K,v}, \sigma_v)$, where $v$ indexes the iteration of the guess. 292 | \item Compute $S$ samples of $N$ normally distributed random shocks $\ve_{i,s}$ that correspond to the uniformly distributed random errors $u_{s,i}$ in step (1) and the current guess of the standard deviation $\sigma_v$ of the normally distributed errors. 293 | \item Use the simulated values of $\ve_{i,s}$ and the guessed values of the coefficients $(\beta_{0,v},\beta_{1,v},...\beta_{K,v})$ to compute average predicted values $\bm{\hat{y}}$ using \eqref{EqSMMlinregsimmodmoms2} and \eqref{EqSMMmodmomgenlinreg2}. 294 | \item Compute the value of the criterion function from \eqref{EqSMMdeflinreg2} given the guess for the parameter vector $\bm{\theta}_v$. 295 | \item Update guess for parameter vector to $\bm{\theta}_{v+1}$ to minimize criterion function from \eqref{EqSMMdeflinreg2}. This should be done with a minimizer function. 296 | \end{enumerate} 297 | 298 | 299 | \section{Linking SMM to Cross Validation in Machine Learning} 300 | 301 | 302 | \bibliography{GMM_SMM_notes.bib} 303 | 304 | 305 | \end{document} 306 | -------------------------------------------------------------------------------- /GMM/GMMest.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Generalized Method of Moments (GMM) Estimation\n", 8 | "### by [Richard W. Evans](https://sites.google.com/site/rickecon/), February 2020\n", 9 | "The code in this Jupyter notebook was written using Python 3.7. It also uses data file `Econ381totpts.txt` and the image file `MLEplots.png`. This data file and image file are stored along with the Jupyter notebook file `GMMest.ipynb` in the public GitHub repository [https://github.com/rickecon/Notebooks/tree/master/GMM](https://github.com/rickecon/Notebooks/tree/master/GMM).\n", 10 | "\n", 11 | "For teaching and learning purposes, it is best to clear all cell output by selecting `Cell` then `All Output` then `Clear` from the menu across the top of this notebook. However, this notebook comes with all the cells' output displayed." 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "## 1. MLE vs. GMM: Strengths and weaknesses" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "### 1.1. Fuhrer, Moore, and Shuh (1995) paper\n", 26 | "A paper by Fuhrer, et al (1995) studies the accuracy and efficiency of the maximum likelihood (ML) estimator versus the generalized method of moments (GMM) estimator in the context of a simple linear-quadratic inventory model. They find that ML has some very nice properties over GMM in small samples when the model is simple. In the spirit of the Fuhrer, et al (1995) paper, we list the strengths and weaknesses of MLE vs. GMM more generally. I recommend you read the introduction to Fuhrer, et al (1995). Big support for maximum likelihood estimation over generalized method of moments.\n", 27 | "* GMM rejects the model (Hansen J-test)\n", 28 | "* MLE supports the model\n", 29 | "* \"Monte Carlo experiments reveal that the GMM estimates are often biased (apparently due to poor instruments), statistically insignificant, economically implausible, and dynamically unstable.\"\n", 30 | "* \"The ML estimates are generally unbiased (even in misspecifipd models), statistically significant, economically plausible, and dynamically stable.\"\n", 31 | "* \"Asymptotic standard errors for ML are 3 to 15 times sma!ler than for GMM.\"" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "### 1.2. MLE strengths\n", 39 | "* More statistical significance. In general, MLE provides more statistical significance for parameter estimates than does GMM. This comes from the strong distributional assumptions that are necessary for the ML estimates.\n", 40 | "* ML estimates are less sensitive to parameter or model normalizations than are GMM estimates.\n", 41 | "* ML estimates have nice small sample properties. ML estimates have less bias and more efficiency with small data samples than GMM estimates in many cases." 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "### 1.3. MLE weaknesses\n", 49 | "* MLE requires strong distributional assumptions. For MLE, the data generating process (DGP) must be completely specified. This assumes a lot of knowledge about the DGP. This assumption is likely almost always wrong.\n", 50 | "* MLE is very difficult in rational expectations models. This is because the consistency of beliefs induces a nonlinearity in the likelihood function that makes it difficult to find the global optimum.\n", 51 | "* MLE is very difficult in nonlinear models. The likelihood function can become highly nonlinear in MLE even if the model is linear when the data are irregular. This difficulty is multiplied when the model itself is more complicated and nonlinear." 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "### 1.4. GMM strengths\n", 59 | "* GMM allows for most flexible identification. GMM estimates can be identified by any set of moments from the data as long as you have at least as many moments as you have parameters to estimate and that those moments are independent enough to identify the parameters. (And the parameters are independent enough of each other to be separately identified.)\n", 60 | "* Good large sample properties. The GMM estimator is strongly consistent and asymptotically normal. GMM will likely be the best estimator if you have a lot of data.\n", 61 | "* GMM requires minimal assumptions about the DGP. In GMM, you need not specify the distributions of the error terms in your model of the DGP. This is often a strength, given that most error are not observed and most models are gross approximations of the true DGP." 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "### 1.5. GMM weaknesses\n", 69 | "* GMM estimates are usually less statistically significant than ML estimates. This comes from the minimal distributional assumptions. GMM parameter estimates usually are measured with more error.\n", 70 | "* GMM estimates can be sensitive to normalizations of the model or parameters.\n", 71 | "* GMM estimates have bad small sample properties. GMM estimates can have large bias and inefficiency in small samples." 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "### 1.6. Key questions to answer when deciding between MLE and GMM\n", 79 | "1. How much data is available for the estimation? Large data samples will make GMM relatively more attractive than MLE because of the nice large sample properties of GMM and fewer required assumptions on the model\n", 80 | "2. How complex is the model? Linear models or quadratic models are much easier to do using MLE than are more highly nonlinear models. Rational expectations models (macroeconomics) create an even more difficult level of nonlinearity that pushes you toward GMM estimation.\n", 81 | "3. How comfortable are you making strong distributional assumptions? MLE requires a complete specification of all distributional assumptions of the model DGP. If you think these assumptions are too strong, you should use GMM." 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## 2. The GMM estimator\n", 89 | "GMM was first formalized by Hansen (1982). A strength of GMM estimation is that the econometrician can remain completely agnostic as to the distribution of the random variables in the DGP. For identification, the econometrician simply needs at least as many moment conditions from the data as he has parameters to estimate.\n", 90 | "\n", 91 | "A *moment* of the data is broadly defined as any statistic that summarizes the data to some degree. A data moment could be as narrow as an individual observation from the data or as broad as the sample average. GMM estimates the parameters of a model or data generating process to make the model moments as close as possible to the corresponding data moments. See Davidson and MacKinnon (2004, ch. 9) for a more detailed treatment of GMM. The estimation methods of linear least squares, nonlinear least squares, generalized least squares, and instrumental variables estimation are all specific cases of the more general GMM estimation method.\n", 92 | "\n", 93 | "Let $m(x)$ be an $R\\times 1$ vector of moments from the real world data $x$. And let $x$ be an $N\\times K$ matrix of data with $K$ columns representing $K$ variables and $N$ observations. Let the model DGP be characterized as $F(x,\\theta)$, where $F$ is a vector of equations, each of which is a function of the data $x$ and the $K\\times 1$ parameter vector $\\theta$. Then define $m(x|\\theta)$ as a vector of $R$ moments from the model that correspond to the real-world moment vector $m(x)$. Note that GMM requires both real world data $x$ and deterministic moments from the model $m(x|\\theta)$ in order to estimate $\\hat{\\theta}_{GMM}$. There is also a stochastic way to generate moments from the model, which we discuss later in our section on Simulated Method of Moments (SMM).\n", 94 | "\n", 95 | "The GMM approach of estimating the parameter vector $\\hat{\\theta}_{GMM}$ is to choose $\\theta$ to minimize some distance measure of the model moments $m(x|\\theta)$ from the data moments $m(x)$.\n", 96 | "\n", 97 | "$$ \\hat{\\theta}_{GMM}=\\theta:\\quad \\min_{\\theta}\\: ||m(x|\\theta) - m(x)|| $$\n", 98 | "\n", 99 | "The distance measure $||m(x|\\theta) - m(x)||$ can be any kind of norm. But it is important to recognize that your estimates $\\hat{\\theta}_{GMM}$ will be dependent on what distance measure (norm) you choose. The most widely studied and used distance metric in GMM estimation is the $L^2$ norm or the sum of squared errors in moments. Define the moment error function $e(x|\\theta)$ as either the percent difference in the vector of model moments from the data moments or the simple difference.\n", 100 | "\n", 101 | "$$ e(x|\\theta) \\equiv \\frac{m(x|\\theta) - m(x)}{m(x)} \\quad\\text{or}\\quad m(x|\\theta) - m(x)$$\n", 102 | "\n", 103 | "It is important when possible that the error function $e(x|\\theta)$ be a percent deviation of the moments (given that none of the data moments are 0). This puts all the moments in the same units, which helps make sure that no moments receive unintended weighting simply due to its units. This ensures that the problem is scaled properly and does not suffer from ill conditioning. However, percent deviations become computationally problematic when the data moments are zero or close to zero. In that case, you would use a simple difference.\n", 104 | "\n", 105 | "The GMM estimator is the following,\n", 106 | "\n", 107 | "$$ \\hat{\\theta}_{GMM}=\\theta:\\quad \\min_{\\theta}\\:e(x|\\theta)^T \\, W \\, e(x|\\theta) $$\n", 108 | "\n", 109 | "where $W$ is a $R\\times R$ weighting matrix in the criterion function. For now, think of this weighting matrix as the identity matrix. But we will show in Section 3 a more optimal weighting matrix. We call the quadratic form expression $e(x|\\theta)^T \\, W \\, e(x|\\theta)$ the *criterion function* because it is a strictly positive scalar that is the object of the minimization in the GMM problem statement. The $R\\times R$ weighting matrix $W$ in the criterion function allows the econometrician to control how each moment is weighted in the minimization problem. For example, an $R\\times R$ identity matrix for $W$ would give each moment equal weighting, and the criterion function would be a simply sum of squared percent deviations (errors). Other weighting strategies can be dictated by the nature of the problem or model." 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "## 3. The Weighting Matrix (W)\n", 117 | "In the GMM criterion function in the problem statement above, some weighting matrices $W$ produce precise estimates while others produce poor estimates with large variances. We want to choose the optimal weighting matrix $W$ with the smallest possible asymptotic variance. This is an efficient of optimal GMM estimator. The optimal weighting matrix is the inverse variance covariance matrix of the moments at the optimal parameter values,\n", 118 | "\n", 119 | "$$ W^{opt} \\equiv \\Omega^{-1}(x|\\hat{\\theta}_{GMM}) $$\n", 120 | "\n", 121 | "where $\\Omega(x|\\theta)$ is the variance covariance matrix of the moment condition errors $E(x|\\theta)$ from each observation in the data (to be defined below). The intuition for using the inverse variance covariance matrix $\\Omega^{-1}$ as the optimal weighting matrix is the following. You want to downweight moments that have a high variance, and you want to weight more heavily the moments that are generated more precisely.\n", 122 | "\n", 123 | "Notice that this definition of the optimal weighting matrix is circular. $W^{opt}$ is a function of the GMM estimates $\\hat{\\theta}_{GMM}$, but the optimal weighting matrix is used in the estimation of $\\hat{\\theta}_{GMM}$. This means that one has to use some kind of iterative fixed point method to find the true optimal weighting matrix $W^{opt}$. Below are some examples of weighting matrices to use." 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "### 3.1. The identity matrix (W = I)\n", 131 | "Many times, you can get away with just using the identity matrix as your weighting matrix $W = I$. This changes the criterion function to a simple sum of squared error functions such that each moment has the same weight.\n", 132 | "\n", 133 | "$$ \\hat{\\theta}_{GMM}=\\theta:\\quad \\min_{\\theta}\\:e(x|\\theta)^T \\, e(x|\\theta) $$\n", 134 | "\n", 135 | "If the problem is well conditioned and well identified, then your GMM estimates $\\hat{\\theta}_{GMM}$ will not be greatly affected by this simplest of weighting matrices." 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "### 3.2. Two-step variance covariance estimator of W\n", 143 | "The most common method of estimating the optimal weighting matrix for GMM estimates is the two-step variance covariance estimator. The name \"two-step\" refers to the two steps used to get the weighting matrix.\n", 144 | "\n", 145 | "The first step is to estimate the GMM parameter vector $\\hat{\\theta}_{1,GMM}$ using the simple identity matrix as the weighting matrix $W = I$.\n", 146 | "\n", 147 | "$$ \\hat{\\theta}_{1, GMM}=\\theta:\\quad \\min_{\\theta}\\:e(x|\\theta)^T \\, I \\, e(x|\\theta) $$\n", 148 | "\n", 149 | "You then use the errors from each observation in the data $E(x|\\theta)$ that generated the error vector $e(x|\\theta)$, which data matrix is $R\\times N$ and if summed across the rows gives the error vector. When the errors are simple differences the $E(x|\\theta)$ matrix is the following,\n", 150 | "\n", 151 | "$$ E(x|\\theta) =\n", 152 | " \\begin{bmatrix}\n", 153 | " m_1(x|\\theta) - m_1(x_1) & m_1(x|\\theta) - m_1(x_2) & ... & m_1(x|\\theta) - m_1(x_N) \\\\\n", 154 | " m_2(x|\\theta) - m_2(x_1) & m_2(x|\\theta) - m_2(x_2) & ... & m_2(x|\\theta) - m_2(x_N) \\\\\n", 155 | " \\vdots & \\vdots & \\ddots & \\vdots \\\\\n", 156 | " m_R(x|\\theta) - m_R(x_1) & m_R(x|\\theta) - m_R(x_2) & ... & m_R(x|\\theta) - m_R(x_N) \\\\\n", 157 | " \\end{bmatrix} $$\n", 158 | "\n", 159 | "where $m_r(x_i)$ is a function associated with the $r$th moment and the $i$th data observation. When the errors are percent deviations, the $E(x|\\theta)$ matrix is the following,\n", 160 | "\n", 161 | "$$ E(x|\\theta) =\n", 162 | " \\begin{bmatrix}\n", 163 | " \\frac{m_1(x|\\theta) - m_1(x_1)}{m_1(x_1)} & \\frac{m_1(x|\\theta) - m_1(x_2)}{m_1(x_2)} & ... & \\frac{m_1(x|\\theta) - m_1(x_N)}{m_1(x_N)} \\\\\n", 164 | " \\frac{m_2(x|\\theta) - m_2(x_1)}{m_2(x_1)} & \\frac{m_2(x|\\theta) - m_2(x_2)}{m_2(x_2)} & ... & \\frac{m_2(x|\\theta) - m_2(x_N)}{m_2(x_N)} \\\\\n", 165 | " \\vdots & \\vdots & \\ddots & \\vdots \\\\\n", 166 | " \\frac{m_R(x|\\theta) - m_R(x_1)}{m_R(x_1)} & \\frac{m_R(x|\\theta) - m_R(x_2)}{m_R(x_2)} & ... & \\frac{m_R(x|\\theta) - m_R(x_N)}{m_R(x_N)} \\\\\n", 167 | " \\end{bmatrix} $$\n", 168 | "\n", 169 | "where the denominator of the percentage deviation or baseline is the model moment that does not change. We use the $E(x|\\theta)$ data matrix and the Step 1 GMM estimate $e(x|\\hat{\\theta}_{1,GMM})$ to get a new estimate of the variance covariance matrix.\n", 170 | "\n", 171 | "$$ \\hat{\\Omega}_2 = \\frac{1}{N}E(x|\\hat{\\theta}_{1,GMM})\\,E(x|\\hat{\\theta}_{1,GMM})^T $$\n", 172 | "\n", 173 | "This is simply saying that the $(r,s)$-element of the estimator of the variance-covariance matrix of the moment vector is the following.\n", 174 | "\n", 175 | "$$\\hat{\\Omega}_{r,s} = \\frac{1}{N}\\sum_{i=1}^N\\Bigl[m_r(x|\\theta) - m_{r}(x_i)\\Bigr]\\Bigl[ m_s(x|\\theta) - m_s(x_i)\\Bigr] $$\n", 176 | "\n", 177 | "The optimal weighting matrix is the inverse of the two-step variance covariance matrix.\n", 178 | "\n", 179 | "$$ \\hat{W}^{two-step} \\equiv \\hat{\\Omega}_2^{-1} $$\n", 180 | "\n", 181 | "Lastly, re-estimate the GMM estimator using the optimal two-step weighting matrix.\n", 182 | "\n", 183 | "$$ \\hat{\\theta}_{2, GMM}=\\theta:\\quad \\min_{\\theta}\\:e(x|\\theta)^T \\, \\hat{W}^{two-step} \\, e(x|\\theta) $$\n", 184 | "\n", 185 | "$\\hat{\\theta}_{2, GMM}$ is called the two-step GMM estimator." 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "### 3.3. Iterated variance covariance estimator of W\n", 193 | "The truly optimal weighting matrix $W^{opt}$ is the iterated variance-covariance estimator of $W$. This procedure is to just repeat the process described in the two-step GMM estimator until the estimated weighting matrix no longer changes between iterations. Let $i$ index the $i$th iterated GMM estimator,\n", 194 | "\n", 195 | "$$ \\hat{\\theta}_{i, GMM}=\\theta:\\quad \\min_{\\theta}\\:e(x|\\theta)^T \\, \\hat{W}_{i} \\, e(x|\\theta) $$\n", 196 | "\n", 197 | "and the $i+1$th estimate of the optimal weighting matrix is defined as the following.\n", 198 | "\n", 199 | "$$ \\hat{W}_{i+1} \\equiv \\hat{\\Omega}_{i+1} = \\frac{1}{N}E(x|\\hat{\\theta}_{i,GMM})\\,E(x|\\hat{\\theta}_{i,GMM})^T $$\n", 200 | "\n", 201 | "The iterated GMM estimator is the $\\hat{\\theta}_{i,GMM}$ such that $\\hat{W}_{i+1}$ is very close to $\\hat{W}_{i}$ for some distance metric (norm).\n", 202 | "\n", 203 | "$$ \\hat{\\theta}_{it,GMM} = \\hat{\\theta}_{i,GMM}: \\quad || \\hat{W}_{i+1} - \\hat{W}_{i} || < \\varepsilon $$" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "### 3.4. Newey-West consistent estimator of $\\Omega$ and W\n", 211 | "The Newey-West estimator of the optimal weighting matrix and variance covariance matrix is consistent in the presence of heteroskedasticity and autocorrelation in the data (See Newey and West, 1987). Adda and Cooper (2003, p. 82) have a nice exposition of how to compute the Newey-West weighting matrix $\\hat{W}_{nw}$. The asymptotic representation of the optimal weighting matrix $\\hat{W}^{opt}$ is the following:\n", 212 | "\n", 213 | "$$ \\hat{W}^{opt} = \\lim_{N\\rightarrow\\infty}\\frac{1}{N}\\sum_{i=1}^N \\sum_{l=-\\infty}^\\infty E(x_i|\\theta)E(x_{i-l}|\\theta)^T $$\n", 214 | "\n", 215 | "The Newey-West consistend estimator of $\\hat{W}^{opt}$ is:\n", 216 | "\n", 217 | "$$ \\hat{W}_{nw} = \\Gamma_{0,N} + \\sum_{v=1}^q \\left(1 - \\left[\\frac{v}{q+1}\\right]\\right)\\left(\\Gamma_{v,N} + \\Gamma^T_{v,N}\\right) $$\n", 218 | "\n", 219 | "where\n", 220 | "\n", 221 | "$$ \\Gamma_{v,N} = \\frac{1}{N}\\sum_{i=v+1}^N E(x_i|\\theta)E(x_{i-v}|\\theta)^T $$\n", 222 | "\n", 223 | "Of course, for autocorrelation, the subscript $i$ and the sample size $N$ can be changed to $t$ and $T$, respectively." 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "## 4. The GMM Variance-Covariance Estimator of the Estimated Parameters\n", 231 | "The estimated variance-covariance matrix $\\hat{\\Sigma}$ of the estimated parameter vector $\\hat{\\theta}_{GMM}$ is different from the variance-covariance matrix $\\hat{\\Omega}$ of the moment vector $e(x|\\theta)$ from the previous section. $\\hat{\\Omega}$ from the previous section is the $R\\times R$ variance-covariance matrix of the $R$ moment errors used to identify the $K$ parameters $\\theta$ to be estimated. The estimated variance-covariance matrix of the estimated parameter vector $\\hat{\\Sigma}$ is a $K\\times K$ matrix. We say the model is exactly identified if $K = R$. We say the model is overidentified if $K" 328 | ] 329 | }, 330 | "execution_count": 10, 331 | "metadata": {}, 332 | "output_type": "execute_result" 333 | } 334 | ], 335 | "source": [ 336 | "# Download and save the data file MLEplots.png\n", 337 | "url = ('https://raw.githubusercontent.com/rickecon/Notebooks/' +\n", 338 | " 'master/GMM/images/MLEplots.png')\n", 339 | "image_file = requests.get(url, allow_redirects=True)\n", 340 | "open('images/MLEplots.png', 'wb').write(image_file.content)\n", 341 | "Image(\"images/MLEplots.png\")" 342 | ] 343 | }, 344 | { 345 | "cell_type": "markdown", 346 | "metadata": {}, 347 | "source": [ 348 | "#### 5.1.1. Two moments, identity weighting matrix\n", 349 | "Let's try estimating the parameters $\\mu$ and $\\sigma$ by GMM. What moments should we use? Let's try the mean and variance of the data. These two statistics of the data are defined by:\n", 350 | "\n", 351 | "$$ mean(scores_i) = \\frac{1}{N}\\sum_{i=1}^N scores_i $$\n", 352 | "\n", 353 | "$$ var(scores_i) = \\frac{1}{N}\\sum_{i=1}^{N} \\left(scores_i - mean(scores_i)\\right)^2 $$\n", 354 | "\n", 355 | "So the data moment vector $m(x)$ for GMM is the following.\n", 356 | "\n", 357 | "$$ m(scores_i) \\equiv \\begin{bmatrix} mean(scores_i) \\\\ var(scores_i) \\end{bmatrix} $$\n", 358 | "\n", 359 | "And the model moment vector $m(x|\\theta)$ for GMM is the following.\n", 360 | "\n", 361 | "$$ m(scores_i|\\mu,\\sigma) \\equiv \\begin{bmatrix} mean(scores_i|\\mu,\\sigma) \\\\ var(scores_i|\\mu,\\sigma) \\end{bmatrix} $$\n", 362 | "\n", 363 | "Define the error vector as the vector of percent deviations of the model moments from the data moments.\n", 364 | "\n", 365 | "$$ e(scores_i|\\mu,\\sigma) \\equiv \\frac{m(scores_i|\\mu,\\sigma) - m(scores_i)}{m(scores_i)} $$\n", 366 | "\n", 367 | "The GMM estimator for this moment vector is the following.\n", 368 | "\n", 369 | "$$ (\\hat{\\mu}_{GMM},\\hat{\\sigma}_{GMM}) = (\\mu,\\sigma):\\quad \\min_{\\mu,\\sigma} e(scores_i|\\mu,\\sigma)^T \\, W \\, e(scores_i|\\mu,\\sigma) $$" 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "Something interesting to note here is the $1/N$ weighting on our variance estimator. There is less bias in the estimator of the variance by using the weighting $1/(N-1)$ because one degree of freedom is used in calculating the mean used in the variance calculation. However, in GMM when many moments are used that might have differing degrees of freedom restrictions, it is important to have the same weighting for each moment. So we use $1/N$ in all cases." 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": null, 382 | "metadata": {}, 383 | "outputs": [], 384 | "source": [ 385 | "# Download and save the data file Econ381totpts.txt\n", 386 | "url = ('https://raw.githubusercontent.com/rickecon/Notebooks/' +\n", 387 | " 'master/GMM/data/Econ381totpts.txt')\n", 388 | "data_file = requests.get(url, allow_redirects=True)\n", 389 | "open('data/Econ381totpts.txt', 'wb').write(data_file.content)\n", 390 | "\n", 391 | "# Load the data as a NumPy array\n", 392 | "pts = np.loadtxt('data/Econ381totpts.txt')\n", 393 | "#pts" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": null, 399 | "metadata": {}, 400 | "outputs": [], 401 | "source": [ 402 | "# Define function that generates values of a normal pdf\n", 403 | "def trunc_norm_pdf(xvals, mu, sigma, cut_lb, cut_ub):\n", 404 | " '''\n", 405 | " --------------------------------------------------------------------\n", 406 | " Generate pdf values from the normal pdf with mean mu and standard\n", 407 | " deviation sigma. If the cutoff is given, then the PDF values are\n", 408 | " inflated upward to reflect the zero probability on values above the\n", 409 | " cutoff. If there is no cutoff given, this function does the same\n", 410 | " thing as sp.stats.norm.pdf(x, loc=mu, scale=sigma).\n", 411 | " --------------------------------------------------------------------\n", 412 | " INPUTS:\n", 413 | " xvals = (N,) vector, values of the normally distributed random\n", 414 | " variable\n", 415 | " mu = scalar, mean of the normally distributed random variable\n", 416 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 417 | " random variable\n", 418 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 419 | " is scalar lower bound value of distribution. Values below\n", 420 | " this value have zero probability\n", 421 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 422 | " is scalar upper bound value of distribution. Values above\n", 423 | " this value have zero probability\n", 424 | " \n", 425 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION: None\n", 426 | " \n", 427 | " OBJECTS CREATED WITHIN FUNCTION:\n", 428 | " prob_notcut = scalar \n", 429 | " pdf_vals = (N,) vector, normal PDF values for mu and sigma\n", 430 | " corresponding to xvals data\n", 431 | " \n", 432 | " FILES CREATED BY THIS FUNCTION: None\n", 433 | " \n", 434 | " RETURNS: pdf_vals\n", 435 | " --------------------------------------------------------------------\n", 436 | " '''\n", 437 | " if cut_ub == 'None' and cut_lb == 'None':\n", 438 | " prob_notcut = 1.0\n", 439 | " elif cut_ub == 'None' and cut_lb != 'None':\n", 440 | " prob_notcut = 1.0 - sts.norm.cdf(cut_lb, loc=mu, scale=sigma)\n", 441 | " elif cut_ub != 'None' and cut_lb == 'None':\n", 442 | " prob_notcut = sts.norm.cdf(cut_ub, loc=mu, scale=sigma)\n", 443 | " elif cut_ub != 'None' and cut_lb != 'None':\n", 444 | " prob_notcut = (sts.norm.cdf(cut_ub, loc=mu, scale=sigma) -\n", 445 | " sts.norm.cdf(cut_lb, loc=mu, scale=sigma))\n", 446 | " \n", 447 | " pdf_vals = ((1/(sigma * np.sqrt(2 * np.pi)) *\n", 448 | " np.exp( - (xvals - mu)**2 / (2 * sigma**2))) /\n", 449 | " prob_notcut)\n", 450 | " \n", 451 | " return pdf_vals" 452 | ] 453 | }, 454 | { 455 | "cell_type": "markdown", 456 | "metadata": {}, 457 | "source": [ 458 | "Now let's define a criterion function that takes as inputs the parameters and the estimator for the weighting matrix $\\hat{W}$." 459 | ] 460 | }, 461 | { 462 | "cell_type": "code", 463 | "execution_count": null, 464 | "metadata": {}, 465 | "outputs": [], 466 | "source": [ 467 | "def data_moments(xvals):\n", 468 | " '''\n", 469 | " --------------------------------------------------------------------\n", 470 | " This function computes the two data moments for GMM\n", 471 | " (mean(data), variance(data)).\n", 472 | " --------------------------------------------------------------------\n", 473 | " INPUTS:\n", 474 | " xvals = (N,) vector, test scores data\n", 475 | " \n", 476 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION: None\n", 477 | " \n", 478 | " OBJECTS CREATED WITHIN FUNCTION:\n", 479 | " mean_data = scalar, mean value of test scores data\n", 480 | " var_data = scalar > 0, variance of test scores data\n", 481 | " \n", 482 | " FILES CREATED BY THIS FUNCTION: None\n", 483 | " \n", 484 | " RETURNS: mean_data, var_data\n", 485 | " --------------------------------------------------------------------\n", 486 | " '''\n", 487 | " mean_data = xvals.mean()\n", 488 | " var_data = xvals.var()\n", 489 | " \n", 490 | " return mean_data, var_data\n", 491 | "\n", 492 | "\n", 493 | "def model_moments(mu, sigma, cut_lb, cut_ub):\n", 494 | " '''\n", 495 | " --------------------------------------------------------------------\n", 496 | " This function computes the two model moments for GMM\n", 497 | " (mean(model data), variance(model data)).\n", 498 | " --------------------------------------------------------------------\n", 499 | " INPUTS:\n", 500 | " mu = scalar, mean of the normally distributed random variable\n", 501 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 502 | " random variable\n", 503 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 504 | " is scalar lower bound value of distribution. Values below\n", 505 | " this value have zero probability\n", 506 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 507 | " is scalar upper bound value of distribution. Values above\n", 508 | " this value have zero probability\n", 509 | " \n", 510 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:\n", 511 | " trunc_norm_pdf()\n", 512 | " xfx()\n", 513 | " x2fx()\n", 514 | " \n", 515 | " OBJECTS CREATED WITHIN FUNCTION:\n", 516 | " mean_model = scalar, mean value of test scores from model\n", 517 | " m_m_err = scalar > 0, estimated error in the computation of the\n", 518 | " integral for the mean of the distribution\n", 519 | " var_model = scalar > 0, variance of test scores from model\n", 520 | " v_m_err = scalar > 0, estimated error in the computation of the\n", 521 | " integral for the variance of the distribution\n", 522 | " \n", 523 | " FILES CREATED BY THIS FUNCTION: None\n", 524 | " \n", 525 | " RETURNS: mean_model, var_model\n", 526 | " --------------------------------------------------------------------\n", 527 | " '''\n", 528 | " xfx = lambda x: x * trunc_norm_pdf(x, mu, sigma, cut_lb, cut_ub)\n", 529 | " (mean_model, m_m_err) = intgr.quad(xfx, cut_lb, cut_ub)\n", 530 | " x2fx = lambda x: ((x - mean_model) ** 2) * trunc_norm_pdf(x, mu, sigma, cut_lb, cut_ub) \n", 531 | " (var_model, v_m_err) = intgr.quad(x2fx, cut_lb, cut_ub)\n", 532 | " \n", 533 | " return mean_model, var_model\n", 534 | "\n", 535 | "\n", 536 | "def err_vec(xvals, mu, sigma, cut_lb, cut_ub, simple):\n", 537 | " '''\n", 538 | " --------------------------------------------------------------------\n", 539 | " This function computes the vector of moment errors (in percent\n", 540 | " deviation from the data moment vector) for GMM.\n", 541 | " --------------------------------------------------------------------\n", 542 | " INPUTS:\n", 543 | " xvals = (N,) vector, test scores data\n", 544 | " mu = scalar, mean of the normally distributed random variable\n", 545 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 546 | " random variable\n", 547 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 548 | " is scalar lower bound value of distribution. Values below\n", 549 | " this value have zero probability\n", 550 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 551 | " is scalar upper bound value of distribution. Values above\n", 552 | " this value have zero probability\n", 553 | " simple = boolean, =True if errors are simple difference, =False if\n", 554 | " errors are percent deviation from data moments\n", 555 | " \n", 556 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:\n", 557 | " data_moments()\n", 558 | " model_moments()\n", 559 | " \n", 560 | " OBJECTS CREATED WITHIN FUNCTION:\n", 561 | " mean_data = scalar, mean value of data\n", 562 | " var_data = scalar > 0, variance of data\n", 563 | " moms_data = (2, 1) matrix, column vector of two data moments\n", 564 | " mean_model = scalar, mean value from model\n", 565 | " var_model = scalar > 0, variance from model\n", 566 | " moms_model = (2, 1) matrix, column vector of two model moments\n", 567 | " err_vec = (2, 1) matrix, column vector of two moment error\n", 568 | " functions\n", 569 | " \n", 570 | " FILES CREATED BY THIS FUNCTION: None\n", 571 | " \n", 572 | " RETURNS: err_vec\n", 573 | " --------------------------------------------------------------------\n", 574 | " '''\n", 575 | " mean_data, var_data = data_moments(xvals)\n", 576 | " moms_data = np.array([[mean_data], [var_data]])\n", 577 | " mean_model, var_model = model_moments(mu, sigma, cut_lb, cut_ub)\n", 578 | " moms_model = np.array([[mean_model], [var_model]])\n", 579 | " if simple:\n", 580 | " err_vec = moms_model - moms_data\n", 581 | " else:\n", 582 | " err_vec = (moms_model - moms_data) / moms_data\n", 583 | " \n", 584 | " return err_vec\n", 585 | "\n", 586 | "\n", 587 | "def criterion(params, *args):\n", 588 | " '''\n", 589 | " --------------------------------------------------------------------\n", 590 | " This function computes the GMM weighted sum of squared moment errors\n", 591 | " criterion function value given parameter values and an estimate of\n", 592 | " the weighting matrix.\n", 593 | " --------------------------------------------------------------------\n", 594 | " INPUTS:\n", 595 | " params = (2,) vector, ([mu, sigma])\n", 596 | " mu = scalar, mean of the normally distributed random variable\n", 597 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 598 | " random variable\n", 599 | " args = length 3 tuple, (xvals, cutoff, W_hat)\n", 600 | " xvals = (N,) vector, values of the truncated normally distributed\n", 601 | " random variable\n", 602 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 603 | " is scalar lower bound value of distribution. Values below\n", 604 | " this value have zero probability\n", 605 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 606 | " is scalar upper bound value of distribution. Values above\n", 607 | " this value have zero probability\n", 608 | " W_hat = (R, R) matrix, estimate of optimal weighting matrix\n", 609 | " \n", 610 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:\n", 611 | " norm_pdf()\n", 612 | " \n", 613 | " OBJECTS CREATED WITHIN FUNCTION:\n", 614 | " err = (2, 1) matrix, column vector of two moment error\n", 615 | " functions\n", 616 | " crit_val = scalar > 0, GMM criterion function value\n", 617 | " \n", 618 | " FILES CREATED BY THIS FUNCTION: None\n", 619 | " \n", 620 | " RETURNS: crit_val\n", 621 | " --------------------------------------------------------------------\n", 622 | " '''\n", 623 | " mu, sigma = params\n", 624 | " xvals, cut_lb, cut_ub, W = args\n", 625 | " err = err_vec(xvals, mu, sigma, cut_lb, cut_ub, simple=False)\n", 626 | " crit_val = err.T @ W @ err\n", 627 | " \n", 628 | " return crit_val" 629 | ] 630 | }, 631 | { 632 | "cell_type": "markdown", 633 | "metadata": {}, 634 | "source": [ 635 | "Now we can perform the GMM estimation. Let's start with the identity matrix as our estimate for the optimal weighting matrix $W = I$." 636 | ] 637 | }, 638 | { 639 | "cell_type": "code", 640 | "execution_count": null, 641 | "metadata": {}, 642 | "outputs": [], 643 | "source": [ 644 | "# Note that this takes a little time because the intgr.quad() commands\n", 645 | "# are a little slow\n", 646 | "mu_init = 400\n", 647 | "sig_init = 60\n", 648 | "params_init = np.array([mu_init, sig_init])\n", 649 | "W_hat = np.eye(2)\n", 650 | "gmm_args = (pts, 0.0, 450.0, W_hat)\n", 651 | "results = opt.minimize(criterion, params_init, args=(gmm_args))\n", 652 | "results = opt.minimize(criterion, params_init, args=(gmm_args),\n", 653 | " tol=1e-14, method='L-BFGS-B',\n", 654 | " bounds=((1e-10, None), (1e-10, None)))\n", 655 | "mu_GMM1, sig_GMM1 = results.x\n", 656 | "print('mu_GMM1=', mu_GMM1, ' sig_GMM1=', sig_GMM1)\n", 657 | "results" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": null, 663 | "metadata": {}, 664 | "outputs": [], 665 | "source": [ 666 | "mean_data, var_data = data_moments(pts)\n", 667 | "mean_model, var_model = model_moments(mu_GMM1, sig_GMM1, 0.0, 450.0)\n", 668 | "err1 = err_vec(pts, mu_GMM1, sig_GMM1, 0.0, 450.0, False).reshape(2,)\n", 669 | "print('Mean of points =', mean_data, ', Variance of points =', var_data)\n", 670 | "print('Mean of model =', mean_model, ', Variance of model =', var_model)\n", 671 | "print('Error vector=', err1)\n", 672 | "results" 673 | ] 674 | }, 675 | { 676 | "cell_type": "markdown", 677 | "metadata": {}, 678 | "source": [ 679 | "Let's plot the PDF implied by these results against the histogram." 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "execution_count": null, 685 | "metadata": {}, 686 | "outputs": [], 687 | "source": [ 688 | "# Plot the histogram of the data\n", 689 | "num_bins = 30\n", 690 | "count, bins, ignored = plt.hist(pts, num_bins, density=True,\n", 691 | " edgecolor='k')\n", 692 | "plt.title('Econ 381 scores: 2011-2012', fontsize=20)\n", 693 | "plt.xlabel(r'Total points')\n", 694 | "plt.ylabel(r'Percent of scores')\n", 695 | "plt.xlim([0, 550]) # This gives the xmin and xmax to be plotted\"\n", 696 | "\n", 697 | "# Plot the estimated GMM PDF\n", 698 | "dist_pts = np.linspace(0, 450, 500)\n", 699 | "plt.plot(dist_pts, trunc_norm_pdf(dist_pts, mu_GMM1, sig_GMM1, 0.0, 450),\n", 700 | " linewidth=2, color='k', label='1: $\\mu_{GMM1}$,$\\sigma_{GMM1}$')\n", 701 | "plt.legend(loc='upper left')" 702 | ] 703 | }, 704 | { 705 | "cell_type": "markdown", 706 | "metadata": {}, 707 | "source": [ 708 | "That looks just like the ML estimate from the previous notebook. Let's see what the criterion function looks like for different values of $\\mu$ and $\\sigma$." 709 | ] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "execution_count": null, 714 | "metadata": {}, 715 | "outputs": [], 716 | "source": [ 717 | "# Note that this will take a few minutes because the intgr.quad() commands\n", 718 | "# are a little slow\n", 719 | "mu_vals = np.linspace(60, 700, 50)\n", 720 | "sig_vals = np.linspace(20, 200, 50)\n", 721 | "# mu_vals = np.linspace(540, 572, 50)\n", 722 | "# sig_vals = np.linspace(170, 182, 50)\n", 723 | "crit_vals = np.zeros((50, 50))\n", 724 | "for mu_ind in range(50):\n", 725 | " for sig_ind in range(50):\n", 726 | " crit_vals[mu_ind, sig_ind] = \\\n", 727 | " criterion(np.array([mu_vals[mu_ind], sig_vals[sig_ind]]),\n", 728 | " pts, 0.0, 450.0, W_hat)\n", 729 | "\n", 730 | "mu_mesh, sig_mesh = np.meshgrid(mu_vals, sig_vals)\n", 731 | "\n", 732 | "fig = plt.figure()\n", 733 | "ax = fig.gca(projection='3d')\n", 734 | "ax.plot_surface(sig_mesh, mu_mesh, crit_vals, rstride=8,\n", 735 | " cstride=1, cmap=cmap1)\n", 736 | "ax.set_title('Criterion function for values of mu and sigma')\n", 737 | "ax.set_xlabel(r'$\\sigma$')\n", 738 | "ax.set_ylabel(r'$\\mu$')\n", 739 | "ax.set_zlabel(r'Crit. func.')" 740 | ] 741 | }, 742 | { 743 | "cell_type": "markdown", 744 | "metadata": {}, 745 | "source": [ 746 | "Let's compute the GMM estimator for the variance-covariance matrix $\\hat{\\Sigma}_{GMM}$ of our GMM estimates $\\hat{\\theta}_{GMM}$ using the equation in Section 4 based on the Jacobian $d(x|\\hat{\\theta}_{GMM})$ of the moment error vector $e(x|\\hat{\\theta}_{GMM})$ from the criterion function at the estimated (optimal) parameter values $\\hat{\\theta}_{GMM}$. We first write a function that computes the Jacobian $d(x|\\hat{\\theta}_{GMM})$." 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": null, 752 | "metadata": {}, 753 | "outputs": [], 754 | "source": [ 755 | "def Jac_err2(xvals, mu, sigma, cut_lb, cut_ub, simple=False):\n", 756 | " '''\n", 757 | " This function computes the Jacobian matrix of partial derivatives of the R x 1 moment\n", 758 | " error vector e(x|theta) with respect to the K parameters theta_i in the K x 1 parameter vector\n", 759 | " theta. The resulting matrix is R x K Jacobian.\n", 760 | " '''\n", 761 | " Jac_err = np.zeros((2, 2))\n", 762 | " h_mu = 1e-8 * mu\n", 763 | " h_sig = 1e-8 * sigma\n", 764 | " Jac_err[:, 0] = \\\n", 765 | " ((err_vec(xvals, mu + h_mu, sigma, cut_lb, cut_ub, simple) -\n", 766 | " err_vec(xvals, mu - h_mu, sigma, cut_lb, cut_ub, simple)) / (2 * h_mu)).flatten()\n", 767 | " Jac_err[:, 1] = \\\n", 768 | " ((err_vec(xvals, mu, sigma + h_sig, cut_lb, cut_ub, simple) -\n", 769 | " err_vec(xvals, mu, sigma - h_sig, cut_lb, cut_ub, simple)) / (2 * h_sig)).flatten()\n", 770 | " \n", 771 | " return Jac_err" 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "execution_count": null, 777 | "metadata": {}, 778 | "outputs": [], 779 | "source": [ 780 | "N = pts.shape[0]\n", 781 | "d_err2 = Jac_err2(pts, mu_GMM1, sig_GMM1, 0.0, 450.0, False)\n", 782 | "print(d_err2)\n", 783 | "print(W_hat)\n", 784 | "SigHat2 = (1 / N) * lin.inv(d_err2.T @ W_hat @ d_err2)\n", 785 | "print(SigHat2)\n", 786 | "print('Std. err. mu_hat=', np.sqrt(SigHat2[0, 0]))\n", 787 | "print('Std. err. sig_hat=', np.sqrt(SigHat2[1, 1]))" 788 | ] 789 | }, 790 | { 791 | "cell_type": "markdown", 792 | "metadata": {}, 793 | "source": [ 794 | "#### 5.1.2. Two moments, two-step optimal weighting matrix\n", 795 | "Similar to the ML problem, it looks like the criterion function is roughly equal for a specific portion increase of $\\mu$ and $\\sigma$ together. That is, with these two moments probably have a correspondence of values of $\\mu$ and $\\sigma$ that give roughly the same criterion function value. This issue has two possible solutions.\n", 796 | "\n", 797 | "1. Maybe we need the two-step variance covariance estimator to calculate a \"more\" optimal weighting matrix $W$.\n", 798 | "2. Maybe our two moments aren't very good moments for fitting the data.\n", 799 | "\n", 800 | "Let's first try the two-step weighting matrix." 801 | ] 802 | }, 803 | { 804 | "cell_type": "code", 805 | "execution_count": null, 806 | "metadata": {}, 807 | "outputs": [], 808 | "source": [ 809 | "def get_Err_mat2(pts, mu, sigma, cut_lb, cut_ub, simple=False):\n", 810 | " '''\n", 811 | " --------------------------------------------------------------------\n", 812 | " This function computes the R x N matrix of errors from each\n", 813 | " observation for each moment. In this function, we have hard coded\n", 814 | " R = 2.\n", 815 | " --------------------------------------------------------------------\n", 816 | " INPUTS:\n", 817 | " xvals = (N,) vector, test scores data\n", 818 | " mu = scalar, mean of the normally distributed random variable\n", 819 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 820 | " random variable\n", 821 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 822 | " is scalar lower bound value of distribution. Values below\n", 823 | " this value have zero probability\n", 824 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 825 | " is scalar upper bound value of distribution. Values above\n", 826 | " this value have zero probability\n", 827 | " simple = boolean, =True if errors are simple difference, =False if\n", 828 | " errors are percent deviation from data moments\n", 829 | " \n", 830 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:\n", 831 | " model_moments()\n", 832 | " \n", 833 | " OBJECTS CREATED WITHIN FUNCTION:\n", 834 | " R = integer = 2, hard coded number of moments\n", 835 | " N = integer >= R, number of data observations\n", 836 | " Err_mat = (R, N) matrix, error by moment and observation data\n", 837 | " mean_model = scalar, mean value from model\n", 838 | " var_model = scalar > 0, variance from model\n", 839 | " \n", 840 | " FILES CREATED BY THIS FUNCTION: None\n", 841 | " \n", 842 | " RETURNS: Err_mat\n", 843 | " --------------------------------------------------------------------\n", 844 | " '''\n", 845 | " R = 2\n", 846 | " N = len(pts)\n", 847 | " Err_mat = np.zeros((R, N))\n", 848 | " mean_model, var_model = model_moments(mu, sigma, cut_lb, cut_ub)\n", 849 | " if simple:\n", 850 | " Err_mat[0, :] = pts - mean_model\n", 851 | " Err_mat[1, :] = ((mean_data - pts) ** 2) - var_model\n", 852 | " else:\n", 853 | " Err_mat[0, :] = (pts - mean_model) / mean_model\n", 854 | " Err_mat[1, :] = (((mean_data - pts) ** 2) - var_model) / var_model\n", 855 | " \n", 856 | " return Err_mat" 857 | ] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": null, 862 | "metadata": {}, 863 | "outputs": [], 864 | "source": [ 865 | "Err_mat = get_Err_mat2(pts, mu_GMM1, sig_GMM1, 0.0, 450.0, False)\n", 866 | "VCV2 = (1 / pts.shape[0]) * (Err_mat @ Err_mat.T)\n", 867 | "print(VCV2)\n", 868 | "W_hat2 = lin.inv(VCV2)\n", 869 | "print(W_hat2)" 870 | ] 871 | }, 872 | { 873 | "cell_type": "code", 874 | "execution_count": null, 875 | "metadata": {}, 876 | "outputs": [], 877 | "source": [ 878 | "# Note that this takes a little time because the intgr.quad() commands\n", 879 | "# are a little slow\n", 880 | "mu_init = 400\n", 881 | "sig_init = 60\n", 882 | "params_init = np.array([mu_GMM1, sig_GMM1])\n", 883 | "# W_hat3 = np.array([[1. / VCV2[0, 0], 0.], [0., 1. / VCV2[1, 1]]])\n", 884 | "gmm_args = (pts, 0.0, 450.0, W_hat2)\n", 885 | "results = opt.minimize(criterion, params_init, args=(gmm_args),\n", 886 | " method='L-BFGS-B', bounds=((1e-10, None), (1e-10, None)))\n", 887 | "mu_GMM2, sig_GMM2 = results.x\n", 888 | "print('mu_GMM2=', mu_GMM2, ' sig_GMM2=', sig_GMM2)" 889 | ] 890 | }, 891 | { 892 | "cell_type": "markdown", 893 | "metadata": {}, 894 | "source": [ 895 | "Look at how much smaller (more efficient) the estimated standard errors are in this case with the two-step optimal weighting matrix $\\hat{W}_{2step}$." 896 | ] 897 | }, 898 | { 899 | "cell_type": "code", 900 | "execution_count": null, 901 | "metadata": {}, 902 | "outputs": [], 903 | "source": [ 904 | "d_err2_2 = Jac_err2(pts, mu_GMM2, sig_GMM2, 0.0, 450.0, False)\n", 905 | "print(d_err2_2)\n", 906 | "print(W_hat2)\n", 907 | "SigHat2_2 = (1 / N) * lin.inv(d_err2.T @ W_hat2 @ d_err2)\n", 908 | "print(SigHat2_2)\n", 909 | "print('Std. err. mu_hat=', np.sqrt(SigHat2_2[0, 0]))\n", 910 | "print('Std. err. sig_hat=', np.sqrt(SigHat2_2[1, 1]))" 911 | ] 912 | }, 913 | { 914 | "cell_type": "markdown", 915 | "metadata": {}, 916 | "source": [ 917 | "#### 5.1.3. Four moments, identity weighting matrix\n", 918 | "Using a better weighting matrix didn't improve our estimates or fit very much it did improve the standard errors of our estimates. To get the right fit, we might need to choose different moments. Let's try an overidentified model $K=220) & (xvals < 320)].shape[0] /\n", 960 | " xvals.shape[0])\n", 961 | " bpct_3_dat = (xvals[(xvals >=320) & (xvals < 430)].shape[0] /\n", 962 | " xvals.shape[0])\n", 963 | " bpct_4_dat = xvals[xvals >= 430].shape[0] / xvals.shape[0]\n", 964 | "\n", 965 | " return bpct_1_dat, bpct_2_dat, bpct_3_dat, bpct_4_dat\n", 966 | "\n", 967 | "\n", 968 | "def model_moments4(mu, sigma, cut_lb, cut_ub):\n", 969 | " '''\n", 970 | " --------------------------------------------------------------------\n", 971 | " This function computes the four model moments for GMM\n", 972 | " (binpct_1, binpct_2, binpct_3, binpct_4).\n", 973 | " --------------------------------------------------------------------\n", 974 | " INPUTS:\n", 975 | " mu = scalar, mean of the normally distributed random variable\n", 976 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 977 | " random variable\n", 978 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 979 | " is scalar lower bound value of distribution. Values below\n", 980 | " this value have zero probability\n", 981 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 982 | " is scalar upper bound value of distribution. Values above\n", 983 | " this value have zero probability\n", 984 | " \n", 985 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:\n", 986 | " trunc_norm_pdf()\n", 987 | " xfx()\n", 988 | " \n", 989 | " OBJECTS CREATED WITHIN FUNCTION:\n", 990 | " bpct_1_mod = scalar in [0, 1], percent of model observations in\n", 991 | " bin 1\n", 992 | " bp_1_err = scalar > 0, estimated error in the computation of the\n", 993 | " integral for bpct_1_mod\n", 994 | " bpct_2_mod = scalar in [0, 1], percent of model observations in\n", 995 | " bin 2\n", 996 | " bp_2_err = scalar > 0, estimated error in the computation of the\n", 997 | " integral for bpct_2_mod\n", 998 | " bpct_3_mod = scalar in [0, 1], percent of model observations in\n", 999 | " bin 3\n", 1000 | " bp_3_err = scalar > 0, estimated error in the computation of the\n", 1001 | " integral for bpct_3_mod\n", 1002 | " bpct_4_mod = scalar in [0, 1], percent of model observations in\n", 1003 | " bin 4\n", 1004 | " bp_4_err = scalar > 0, estimated error in the computation of the\n", 1005 | " integral for bpct_4_mod\n", 1006 | " \n", 1007 | " FILES CREATED BY THIS FUNCTION: None\n", 1008 | " \n", 1009 | " RETURNS: bpct_1_mod, bpct_2_mod, bpct_3_mod, bpct_4_mod\n", 1010 | " --------------------------------------------------------------------\n", 1011 | " '''\n", 1012 | " xfx = lambda x: trunc_norm_pdf(x, mu, sigma, cut_lb, cut_ub)\n", 1013 | " (bpct_1_mod, bp_1_err) = intgr.quad(xfx, 0.0, 220)\n", 1014 | " (bpct_2_mod, bp_2_err) = intgr.quad(xfx, 220, 320)\n", 1015 | " (bpct_3_mod, bp_3_err) = intgr.quad(xfx, 320, 430)\n", 1016 | " (bpct_4_mod, bp_4_err) = intgr.quad(xfx, 430, 450)\n", 1017 | " \n", 1018 | " return bpct_1_mod, bpct_2_mod, bpct_3_mod, bpct_4_mod\n", 1019 | "\n", 1020 | "\n", 1021 | "def err_vec4(xvals, mu, sigma, cut_lb, cut_ub, simple):\n", 1022 | " '''\n", 1023 | " --------------------------------------------------------------------\n", 1024 | " This function computes the vector of moment errors (in percent\n", 1025 | " deviation from the data moment vector) for GMM.\n", 1026 | " --------------------------------------------------------------------\n", 1027 | " INPUTS:\n", 1028 | " xvals = (N,) vector, test scores data\n", 1029 | " mu = scalar, mean of the normally distributed random variable\n", 1030 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 1031 | " random variable\n", 1032 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 1033 | " is scalar lower bound value of distribution. Values below\n", 1034 | " this value have zero probability\n", 1035 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 1036 | " is scalar upper bound value of distribution. Values above\n", 1037 | " this value have zero probability\n", 1038 | " simple = boolean, =True if errors are simple difference, =False if\n", 1039 | " errors are percent deviation from data moments\n", 1040 | " \n", 1041 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:\n", 1042 | " data_moments4()\n", 1043 | " model_moments4()\n", 1044 | " \n", 1045 | " OBJECTS CREATED WITHIN FUNCTION:\n", 1046 | " mean_data = scalar, mean value of data\n", 1047 | " var_data = scalar > 0, variance of data\n", 1048 | " moms_data = (2, 1) matrix, column vector of two data moments\n", 1049 | " mean_model = scalar, mean value from model\n", 1050 | " var_model = scalar > 0, variance from model\n", 1051 | " moms_model = (2, 1) matrix, column vector of two model moments\n", 1052 | " err_vec = (2, 1) matrix, column vector of two moment error\n", 1053 | " functions\n", 1054 | " \n", 1055 | " FILES CREATED BY THIS FUNCTION: None\n", 1056 | " \n", 1057 | " RETURNS: err_vec\n", 1058 | " --------------------------------------------------------------------\n", 1059 | " '''\n", 1060 | " bpct_1_dat, bpct_2_dat, bpct_3_dat, bpct_4_dat = \\\n", 1061 | " data_moments4(xvals)\n", 1062 | " moms_data = np.array([[bpct_1_dat], [bpct_2_dat], [bpct_3_dat],\n", 1063 | " [bpct_4_dat]])\n", 1064 | " bpct_1_mod, bpct_2_mod, bpct_3_mod, bpct_4_mod = \\\n", 1065 | " model_moments4(mu, sigma, cut_lb, cut_ub)\n", 1066 | " moms_model = np.array([[bpct_1_mod], [bpct_2_mod], [bpct_3_mod],\n", 1067 | " [bpct_4_mod]])\n", 1068 | " if simple:\n", 1069 | " err_vec = moms_model - moms_data\n", 1070 | " else:\n", 1071 | " err_vec = (moms_model - moms_data) / moms_data\n", 1072 | " \n", 1073 | " return err_vec\n", 1074 | "\n", 1075 | "\n", 1076 | "def criterion4(params, *args):\n", 1077 | " '''\n", 1078 | " --------------------------------------------------------------------\n", 1079 | " This function computes the GMM weighted sum of squared moment errors\n", 1080 | " criterion function value given parameter values and an estimate of\n", 1081 | " the weighting matrix.\n", 1082 | " --------------------------------------------------------------------\n", 1083 | " INPUTS:\n", 1084 | " params = (2,) vector, ([mu, sigma])\n", 1085 | " mu = scalar, mean of the normally distributed random variable\n", 1086 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 1087 | " random variable\n", 1088 | " args = length 3 tuple, (xvals, cutoff, W_hat)\n", 1089 | " xvals = (N,) vector, values of the truncated normally distributed\n", 1090 | " random variable\n", 1091 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 1092 | " is scalar lower bound value of distribution. Values below\n", 1093 | " this value have zero probability\n", 1094 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 1095 | " is scalar upper bound value of distribution. Values above\n", 1096 | " this value have zero probability\n", 1097 | " W_hat = (R, R) matrix, estimate of optimal weighting matrix\n", 1098 | " \n", 1099 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:\n", 1100 | " err_vec4()\n", 1101 | " \n", 1102 | " OBJECTS CREATED WITHIN FUNCTION:\n", 1103 | " err = (4, 1) matrix, column vector of four moment error\n", 1104 | " functions\n", 1105 | " crit_val = scalar > 0, GMM criterion function value\n", 1106 | " \n", 1107 | " FILES CREATED BY THIS FUNCTION: None\n", 1108 | " \n", 1109 | " RETURNS: crit_val\n", 1110 | " --------------------------------------------------------------------\n", 1111 | " '''\n", 1112 | " mu, sigma = params\n", 1113 | " xvals, cut_lb, cut_ub, W = args\n", 1114 | " err = err_vec4(xvals, mu, sigma, cut_lb, cut_ub, simple=False)\n", 1115 | " crit_val = err.T @ W @ err \n", 1116 | " \n", 1117 | " return crit_val" 1118 | ] 1119 | }, 1120 | { 1121 | "cell_type": "markdown", 1122 | "metadata": {}, 1123 | "source": [ 1124 | "Let's see what the model moments would be relative to the data moments with the first GMM estimates $mu\\approx 620$ and $\\sigma\\approx 198$ from the previous 2-moment estimation. Let's also look at the resulting criterion function at those values." 1125 | ] 1126 | }, 1127 | { 1128 | "cell_type": "code", 1129 | "execution_count": null, 1130 | "metadata": {}, 1131 | "outputs": [], 1132 | "source": [ 1133 | "params = np.array([mu_GMM1, sig_GMM1])\n", 1134 | "print('params=', params)\n", 1135 | "print(data_moments4(pts))\n", 1136 | "print(model_moments4(mu_GMM1, sig_GMM1, 0.0, 450))\n", 1137 | "print(criterion4(params, pts, 0.0, 450.0, np.eye(4)))" 1138 | ] 1139 | }, 1140 | { 1141 | "cell_type": "code", 1142 | "execution_count": null, 1143 | "metadata": {}, 1144 | "outputs": [], 1145 | "source": [ 1146 | "# Note that this takes a little time because the intgr.quad() commands\n", 1147 | "# are a little slow\n", 1148 | "mu_init = 400\n", 1149 | "sig_init = 70\n", 1150 | "params_init = np.array([mu_init, sig_init])\n", 1151 | "W_hat1_4 = np.eye(4)\n", 1152 | "gmm_args = (pts, 0.0, 450.0, W_hat1_4)\n", 1153 | "results_4 = opt.minimize(criterion4, params_init, args=(gmm_args),\n", 1154 | " method='L-BFGS-B', bounds=((1e-10, None), (1e-10, None)))\n", 1155 | "mu_GMM1_4, sig_GMM1_4 = results_4.x\n", 1156 | "print('mu_GMM1_4=', mu_GMM1_4, ' sig_GMM1_4=', sig_GMM1_4)" 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "code", 1161 | "execution_count": null, 1162 | "metadata": {}, 1163 | "outputs": [], 1164 | "source": [ 1165 | "params = np.array([mu_GMM1_4, sig_GMM1_4])\n", 1166 | "print('params=', params)\n", 1167 | "print(data_moments4(pts))\n", 1168 | "print(model_moments4(mu_GMM1_4, sig_GMM1_4, 0.0, 450))\n", 1169 | "print(criterion4(params, pts, 0.0, 450.0, W_hat1_4))" 1170 | ] 1171 | }, 1172 | { 1173 | "cell_type": "code", 1174 | "execution_count": null, 1175 | "metadata": {}, 1176 | "outputs": [], 1177 | "source": [ 1178 | "params2 = np.array([400, 70])\n", 1179 | "print('params=', params)\n", 1180 | "print(data_moments4(pts))\n", 1181 | "print(model_moments4(400, 70, 0.0, 450))\n", 1182 | "print(criterion4(params2, pts, 0.0, 450.0, W_hat1_4))" 1183 | ] 1184 | }, 1185 | { 1186 | "cell_type": "code", 1187 | "execution_count": null, 1188 | "metadata": {}, 1189 | "outputs": [], 1190 | "source": [ 1191 | "# Plot the histogram of the data\n", 1192 | "count, bins, ignored = plt.hist(pts, num_bins, density=True,\n", 1193 | " edgecolor='k')\n", 1194 | "plt.title('Econ 381 scores: 2011-2012', fontsize=20)\n", 1195 | "plt.xlabel(r'Total points')\n", 1196 | "plt.ylabel(r'Percent of scores')\n", 1197 | "plt.xlim([0, 550]) # This gives the xmin and xmax to be plotted\"\n", 1198 | "\n", 1199 | "# Plot the estimated GMM PDF\n", 1200 | "dist_pts = np.linspace(0, 450, 500)\n", 1201 | "plt.plot(dist_pts, trunc_norm_pdf(dist_pts, mu_GMM1_4, sig_GMM1_4, 0.0, 450),\n", 1202 | " linewidth=2, color='k', label='1: $\\mu_{GMM1}$,$\\sigma_{GMM1}$')\n", 1203 | "\n", 1204 | "# Plot the estimated GMM PDF\n", 1205 | "dist_pts = np.linspace(0, 450, 500)\n", 1206 | "plt.plot(dist_pts, trunc_norm_pdf(dist_pts, 400, 70, 0.0, 450),\n", 1207 | " linewidth=2, color='g', label='1: $\\mu=400$,$\\sigma=70$')\n", 1208 | "plt.legend(loc='upper left')" 1209 | ] 1210 | }, 1211 | { 1212 | "cell_type": "markdown", 1213 | "metadata": {}, 1214 | "source": [ 1215 | "We can compute the estimator of the variance-covariance matrix $\\hat{\\Sigma}$ of the GMM parameter estimator by computing the Jacobian of the error vector. In this case, the Jacobian $d(x|\\theta)$ is $R\\times K = 4\\times 2$." 1216 | ] 1217 | }, 1218 | { 1219 | "cell_type": "code", 1220 | "execution_count": null, 1221 | "metadata": {}, 1222 | "outputs": [], 1223 | "source": [ 1224 | "def Jac_err4(xvals, mu, sigma, cut_lb, cut_ub, simple=False):\n", 1225 | " '''\n", 1226 | " This function computes the Jacobian matrix of partial derivatives of the R x 1 moment\n", 1227 | " error vector e(x|theta) with respect to the K parameters theta_i in the K x 1 parameter vector\n", 1228 | " theta. The resulting matrix is R x K Jacobian.\n", 1229 | " '''\n", 1230 | " Jac_err = np.zeros((4, 2))\n", 1231 | " h_mu = 1e-8 * mu\n", 1232 | " h_sig = 1e-8 * sigma\n", 1233 | " Jac_err[:, 0] = \\\n", 1234 | " ((err_vec4(xvals, mu + h_mu, sigma, cut_lb, cut_ub, simple) -\n", 1235 | " err_vec4(xvals, mu - h_mu, sigma, cut_lb, cut_ub, simple)) / (2 * h_mu)).flatten()\n", 1236 | " Jac_err[:, 1] = \\\n", 1237 | " ((err_vec4(xvals, mu, sigma + h_sig, cut_lb, cut_ub, simple) -\n", 1238 | " err_vec4(xvals, mu, sigma - h_sig, cut_lb, cut_ub, simple)) / (2 * h_sig)).flatten()\n", 1239 | " \n", 1240 | " return Jac_err" 1241 | ] 1242 | }, 1243 | { 1244 | "cell_type": "code", 1245 | "execution_count": null, 1246 | "metadata": {}, 1247 | "outputs": [], 1248 | "source": [ 1249 | "d_err4 = Jac_err4(pts, mu_GMM1_4, sig_GMM1_4, 0.0, 450.0, False)\n", 1250 | "print(d_err4)\n", 1251 | "print(W_hat1_4)\n", 1252 | "SigHat4 = (1 / N) * lin.inv(d_err4.T @ W_hat1_4 @ d_err4)\n", 1253 | "print(SigHat4)\n", 1254 | "print('Std. err. mu_hat=', np.sqrt(SigHat4[0, 0]))\n", 1255 | "print('Std. err. sigma_hat=', np.sqrt(SigHat4[1, 1]))" 1256 | ] 1257 | }, 1258 | { 1259 | "cell_type": "markdown", 1260 | "metadata": {}, 1261 | "source": [ 1262 | "Note how much tighter the standard errors are here with these four moments than they were in the econometric models of Sections 5.1.1 and 5.1.2 with two moments." 1263 | ] 1264 | }, 1265 | { 1266 | "cell_type": "markdown", 1267 | "metadata": {}, 1268 | "source": [ 1269 | "#### 5.1.4. Four moments, two-step optimal weighting matrix\n", 1270 | "Let's see how much things change if we use the two-step estimator for the optimal weighting matrix $W$ instead of the identity matrix." 1271 | ] 1272 | }, 1273 | { 1274 | "cell_type": "code", 1275 | "execution_count": null, 1276 | "metadata": {}, 1277 | "outputs": [], 1278 | "source": [ 1279 | "def get_Err_mat4(pts, mu, sigma, cut_lb, cut_ub, simple=False):\n", 1280 | " '''\n", 1281 | " --------------------------------------------------------------------\n", 1282 | " This function computes the R x N matrix of errors from each\n", 1283 | " observation for each moment. In this function, we have hard coded\n", 1284 | " R = 4.\n", 1285 | " --------------------------------------------------------------------\n", 1286 | " INPUTS:\n", 1287 | " xvals = (N,) vector, test scores data\n", 1288 | " mu = scalar, mean of the normally distributed random variable\n", 1289 | " sigma = scalar > 0, standard deviation of the normally distributed\n", 1290 | " random variable\n", 1291 | " cut_lb = scalar or string, ='None' if no cutoff is given, otherwise\n", 1292 | " is scalar lower bound value of distribution. Values below\n", 1293 | " this value have zero probability\n", 1294 | " cut_ub = scalar or string, ='None' if no cutoff is given, otherwise\n", 1295 | " is scalar upper bound value of distribution. Values above\n", 1296 | " this value have zero probability\n", 1297 | " simple = boolean, =True if errors are simple difference, =False if\n", 1298 | " errors are percent deviation from data moments\n", 1299 | " \n", 1300 | " OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:\n", 1301 | " model_moments()\n", 1302 | " \n", 1303 | " OBJECTS CREATED WITHIN FUNCTION:\n", 1304 | " R = 2, hard coded number of moments\n", 1305 | " N = integer >= R, number of data observations\n", 1306 | " Err_mat = (R, N) matrix, error by moment and observation data\n", 1307 | " mean_model = scalar, mean value from model\n", 1308 | " var_model = scalar > 0, variance from model\n", 1309 | " \n", 1310 | " FILES CREATED BY THIS FUNCTION: None\n", 1311 | " \n", 1312 | " RETURNS: Err_mat\n", 1313 | " --------------------------------------------------------------------\n", 1314 | " '''\n", 1315 | " R = 4\n", 1316 | " N = len(pts)\n", 1317 | " Err_mat = np.zeros((R, N))\n", 1318 | " pct_1_mod, pct_2_mod, pct_3_mod, pct_4_mod = \\\n", 1319 | " model_moments4(mu, sigma, cut_lb, cut_ub)\n", 1320 | " if simple:\n", 1321 | " pts_in_grp1 = pts < 220\n", 1322 | " Err_mat[0, :] = pts_in_grp1 - pct_1_mod\n", 1323 | " pts_in_grp2 = (pts >= 220) & (pts < 320)\n", 1324 | " Err_mat[1, :] = pts_in_grp2 - pct_2_mod\n", 1325 | " pts_in_grp3 = (pts >= 320) & (pts < 430)\n", 1326 | " Err_mat[2, :] = pts_in_grp3 - pct_3_mod\n", 1327 | " pts_in_grp4 = pts >= 430\n", 1328 | " Err_mat[3, :] = pts_in_grp4 - pct_4_mod\n", 1329 | " else:\n", 1330 | " pts_in_grp1 = pts < 220\n", 1331 | " Err_mat[0, :] = (pts_in_grp1 - pct_1_mod) / pct_1_mod\n", 1332 | " pts_in_grp2 = (pts >= 220) & (pts < 320)\n", 1333 | " Err_mat[1, :] = (pts_in_grp2 - pct_2_mod) / pct_2_mod\n", 1334 | " pts_in_grp3 = (pts >= 320) & (pts < 430)\n", 1335 | " Err_mat[2, :] = (pts_in_grp3 - pct_3_mod) / pct_3_mod\n", 1336 | " pts_in_grp4 = pts >= 430\n", 1337 | " Err_mat[3, :] = (pts_in_grp4 - pct_4_mod) / pct_4_mod\n", 1338 | " \n", 1339 | " return Err_mat" 1340 | ] 1341 | }, 1342 | { 1343 | "cell_type": "code", 1344 | "execution_count": null, 1345 | "metadata": {}, 1346 | "outputs": [], 1347 | "source": [ 1348 | "Err_mat4 = get_Err_mat4(pts, mu_GMM1_4, sig_GMM1_4, 0.0, 450.0, True)\n", 1349 | "VCV2_4 = (1 / pts.shape[0]) * (Err_mat4 @ Err_mat4.T)\n", 1350 | "print(VCV2_4)\n", 1351 | "# We use the pseudo-inverse command here because the VCV matrix is poorly conditioned\n", 1352 | "W_hat2_4 = lin.pinv(VCV2_4)\n", 1353 | "print(W_hat2_4)" 1354 | ] 1355 | }, 1356 | { 1357 | "cell_type": "code", 1358 | "execution_count": null, 1359 | "metadata": {}, 1360 | "outputs": [], 1361 | "source": [ 1362 | "# Note that this takes a little time because the intgr.quad() commands\n", 1363 | "# are a little slow\n", 1364 | "mu_init = 600\n", 1365 | "sig_init = 196\n", 1366 | "params_init = np.array([mu_GMM1_4, sig_GMM1_4])\n", 1367 | "gmm_args = (pts, 0.0, 450.0, W_hat2_4)\n", 1368 | "results2_4 = opt.minimize(criterion4, params_init, args=(gmm_args),\n", 1369 | " method='L-BFGS-B', bounds=((1e-10, None), (1e-10, None)))\n", 1370 | "mu_GMM2_4, sig_GMM2_4 = results2_4.x\n", 1371 | "print('mu_GMM2_4=', mu_GMM2_4, ' sig_GMM2_4=', sig_GMM2_4)\n", 1372 | "results2_4" 1373 | ] 1374 | }, 1375 | { 1376 | "cell_type": "markdown", 1377 | "metadata": {}, 1378 | "source": [ 1379 | "In this case, the two-step estimator of the optimal weighting matrix creates a small change in the estimated $\\mu$ and $\\sigma$ paramters to $(\\mu=364,\\sigma=111)$ from $(\\mu=362,\\sigma=92)$ in the identity matrix estimation. The criterion function for different values of $\\mu$ and $\\sigma$ here has a clear minimum in a certain area. But it also has some really interesting nonlinearities." 1380 | ] 1381 | }, 1382 | { 1383 | "cell_type": "code", 1384 | "execution_count": null, 1385 | "metadata": {}, 1386 | "outputs": [], 1387 | "source": [ 1388 | "%matplotlib notebook\n", 1389 | "# Note that this will take a few minutes because the intgr.quad() commands\n", 1390 | "# are a little slow\n", 1391 | "mu_vals = np.linspace(355, 370, 50)\n", 1392 | "sig_vals = np.linspace(85, 120, 50)\n", 1393 | "# mu_vals = np.linspace(60, 700, 50)\n", 1394 | "# sig_vals = np.linspace(20, 200, 50)\n", 1395 | "crit_vals4 = np.zeros((50, 50))\n", 1396 | "for mu_ind in range(50):\n", 1397 | " for sig_ind in range(50):\n", 1398 | " crit_vals4[mu_ind, sig_ind] = \\\n", 1399 | " criterion4(np.array([mu_vals[mu_ind], sig_vals[sig_ind]]),\n", 1400 | " pts, 0.0, 450.0, W_hat2_4)\n", 1401 | "\n", 1402 | "mu_mesh, sig_mesh = np.meshgrid(mu_vals, sig_vals)\n", 1403 | "\n", 1404 | "fig = plt.figure()\n", 1405 | "ax = fig.gca(projection='3d')\n", 1406 | "ax.plot_surface(sig_mesh, mu_mesh, crit_vals4, rstride=8,\n", 1407 | " cstride=1, cmap=cmap1)\n", 1408 | "ax.set_title('Criterion function for values of mu and sigma')\n", 1409 | "ax.set_xlabel(r'$\\sigma$')\n", 1410 | "ax.set_ylabel(r'$\\mu$')\n", 1411 | "ax.set_zlabel(r'Crit. func.')" 1412 | ] 1413 | }, 1414 | { 1415 | "cell_type": "markdown", 1416 | "metadata": {}, 1417 | "source": [ 1418 | "We can compute the estimator of the variance-covariance matrix $\\hat{\\Sigma}$ of the GMM parameter estimator by computing the Jacobian of the error vector." 1419 | ] 1420 | }, 1421 | { 1422 | "cell_type": "code", 1423 | "execution_count": null, 1424 | "metadata": {}, 1425 | "outputs": [], 1426 | "source": [ 1427 | "d_err4_2 = Jac_err4(pts, mu_GMM2_4, sig_GMM2_4, 0.0, 450.0, False)\n", 1428 | "print(d_err4_2)\n", 1429 | "print(W_hat2_4)\n", 1430 | "SigHat4_2 = (1 / N) * lin.inv(d_err4_2.T @ W_hat2_4 @ d_err4_2)\n", 1431 | "print(SigHat4_2)\n", 1432 | "print('Std. err. mu_hat=', np.sqrt(SigHat4_2[0, 0]))\n", 1433 | "print('Std. err. sigma_hat=', np.sqrt(SigHat4_2[1, 1]))" 1434 | ] 1435 | }, 1436 | { 1437 | "cell_type": "markdown", 1438 | "metadata": {}, 1439 | "source": [ 1440 | "Note how much smaller (more efficient) the estimated standard errors are in this case with the two-step optimal weighting matrix $\\hat{W}_{2step}$: $(1.65, 1.49)$ versus $(3.78, 3.24)$." 1441 | ] 1442 | }, 1443 | { 1444 | "cell_type": "markdown", 1445 | "metadata": {}, 1446 | "source": [ 1447 | "### 5.2. Unconditional and conditional expectations, instruments, and moments\n", 1448 | "Most standard treatments of the generalized method of moments estimator in econometrics textbooks start with this principle and this selection of moments. However, this notebook follows the progression of starting with the most general treatment of GMM and then covering these special cases.\n", 1449 | "\n", 1450 | "In stochastic models, the assumed data generating process might have one or more characterizing equations that involve an unconditional expectation. The unconditional expectation is a strong assumption with many implications on conditional expectations that can create moments for identifying parameters using GMM. In econometric models, these unconditional expectations often show up as an assumption on the error term of one or more of the equations. Note that this is a minimal assumption and does not require knowledge of the distribution of the error term.\n", 1451 | "\n", 1452 | "$$ y_i = \\beta_0 + \\beta_1 x_{1,i} + \\beta_2 x_{2,i} + \\varepsilon_i \\quad\\text{where}\\quad E\\left[\\varepsilon_i\\right] = 0 $$\n", 1453 | "\n", 1454 | "In a macroeconomic model like the Brock and Mirman (1972) model, unconditional expectations show up in two places. The first is in the Euler equation, which is the first equation below. And the second is on the error term in the law of motion for the productivity shock, which is the last equation below.\n", 1455 | "\n", 1456 | "$$ \\quad\\left(c_t\\right)^{-1} = \\beta E\\left[r_{t+1}\\left(c_{t+1}\\right)^{-1}\\right] $$\n", 1457 | "$$ c_t + k_{t+1} = r_{t+1}k_t + w_t $$\n", 1458 | "$$ \\quad w_t = (1 - \\alpha)e^{z_t}k_{t}^\\alpha $$\n", 1459 | "$$ r_t = \\alpha e^{z_t}k_{t}^{\\alpha-1} $$\n", 1460 | "$$ \\qquad\\qquad\\qquad\\qquad\\qquad\\qquad z_{t} = \\rho z_{t-1} + (1 - \\rho)\\mu + \\varepsilon_t \\quad\\text{where}\\quad E[\\varepsilon_t]=0 $$\n", 1461 | "\n", 1462 | "It is valuable to note first that these unconditional expectations imply minimal restrictions on the stochastic distributions in the model. They only imply a restriction on the first moments of those particular parts of the distributions. Furthermore, because they are unconditional distributions (which is a strong assumption), they also imply restrictions on conditional distributions." 1463 | ] 1464 | }, 1465 | { 1466 | "cell_type": "markdown", 1467 | "metadata": {}, 1468 | "source": [ 1469 | "Let $\\mathcal{I}$ be the set of variables that are in the information set of the model at the time the expectations operator in the model is formed. Let $w\\in\\mathcal{I}$ be the typical element (variable) in the information set. In a cross sectional econometric model, the variables in the information set are $w\\in\\mathcal{I}$ that could possibly be related to the dependent variable $y$ and were determined at the time the expectation was formed. In dynamic models or time series models, variables in the information set include any variables that were determined on or before the period in which the expectation was formed.\n", 1470 | "\n", 1471 | "The following sequence shows how an unconditional expectation can lead to moments that can identify parameters.\n", 1472 | "\n", 1473 | "$$ E[x] = 0 \\Rightarrow E[x|\\mathcal{I}] = 0 \\Rightarrow Cov[x,w] = 0 \\Rightarrow E[xw] = 0 $$\n", 1474 | "\n", 1475 | "The first equation states that the unconditional expectation of $x$ is zero. This implies that the conditional expectation of $x$ given anything else in the information set is also zero. This, in turn, implies that the covariance of $x$ and any element $w$ of the information set is zero so that the expectation of $x$ times $w$ is zero. It is this last equation that generates many of the moments used to identify parameters in GMM. Any variable in the instrument set $w\\in\\mathcal{I}$ can generate a moment condition." 1476 | ] 1477 | }, 1478 | { 1479 | "cell_type": "markdown", 1480 | "metadata": {}, 1481 | "source": [ 1482 | "#### 5.2.1. Econometric (linear regression) GMM example using unconditional expectation for moments\n", 1483 | "In the econometric model example above, there are three parameters to be estimated $(\\beta_0, \\beta_1, \\beta_2)$. We need at least three independent moments to identify these parameters. The first moment we can use is the assumption that the unconditional expectation of the error terms is zero. This has an empirical counterpart that the sample mean of the error terms should be zero.\n", 1484 | "\n", 1485 | "$$ \\text{(Theoretical moment 1)}\\quad E[\\varepsilon_i] = 0 \\quad\\Rightarrow\\quad E\\left[y_i - \\beta_0 - \\beta_1 x_{1,i} - \\beta_2 x_{2,i}\\right]=0 $$\n", 1486 | "$$ \\text{(Empirical moment 1)}\\qquad\\qquad\\qquad\\:\\:\\: \\frac{1}{N}\\sum_{i=1}^N\\left[y_i - \\beta_0 - \\beta_1 x_{1,i} - \\beta_2 x_{2,i}\\right]=0 $$\n", 1487 | "\n", 1488 | "The next two moments are formed from the implications that the unconditional expectation on the errors implies that the expected value of the product of the error and any other variable in the information set is also zero.\n", 1489 | "\n", 1490 | "$$ \\text{(Theoretical moment 2)}\\quad E[\\varepsilon_i x_{1,i}] = 0 \\quad\\Rightarrow\\quad E\\Bigl[\\left(y_i - \\beta_0 - \\beta_1 x_{1,i} - \\beta_2 x_{2,i}\\right)x_{1,i}\\Bigr]=0 $$\n", 1491 | "$$ \\text{(Empirical moment 2)}\\qquad\\qquad\\qquad\\qquad \\frac{1}{N}\\sum_{i=1}^N\\Bigl[\\left(y_i - \\beta_0 - \\beta_1 x_{1,i} - \\beta_2 x_{2,i}\\right)x_{1,i}\\Bigr]=0 $$\n", 1492 | "\n", 1493 | "$$ \\text{(Theoretical moment 3)}\\quad E[\\varepsilon_i x_{2,i}] = 0 \\quad\\Rightarrow\\quad E\\Bigl[\\left(y_i - \\beta_0 - \\beta_1 x_{1,i} - \\beta_2 x_{2,i}\\right)x_{2,i}\\Bigr]=0 $$\n", 1494 | "$$ \\text{(Empirical moment 3)}\\qquad\\qquad\\qquad\\qquad \\frac{1}{N}\\sum_{i=1}^N\\Bigl[\\left(y_i - \\beta_0 - \\beta_1 x_{1,i} - \\beta_2 x_{2,i}\\right)x_{2,i}\\Bigr]=0 $$" 1495 | ] 1496 | }, 1497 | { 1498 | "cell_type": "markdown", 1499 | "metadata": {}, 1500 | "source": [ 1501 | "#### 5.2.2. Brock and Mirman (1972) estimation by GMM using unconditional expectations for moments\n", 1502 | "Give Brock and Mirman (1972) example. This is currently a problem set.\n", 1503 | "\n", 1504 | "Cite Hansen and Singleton (1982) example." 1505 | ] 1506 | }, 1507 | { 1508 | "cell_type": "markdown", 1509 | "metadata": {}, 1510 | "source": [ 1511 | "## 6. Identification\n", 1512 | "An issue that we saw in the examples from Section 5 is that there is some science as well as some art in choosing moments to identify the parameters in a GMM estimation.\n", 1513 | "\n", 1514 | "* The $\\mu$ and $\\sigma$ parameters were identified more precisely when using the two-step estimator of the optimal weighting matrix instead of the identity matrix.\n", 1515 | "* The overidentified four-moment model of total scores produced much smaller standard errors for both $\\mu$ and $\\sigma$ than did the two-moment model.\n", 1516 | "\n", 1517 | "Suppose the parameter vector $\\theta$ has $K$ elements, or rather, $K$ parameters to be estimated. In order to estimate $\\theta$ by GMM, you must have at least as many moments as parameters to estimate $R\\geq K$. If you have exactly as many moments as parameters to be estimated $R=K$, the model is said to be *exactly identified*. If you have more moments than parameters to be estimated $R>K$, the model is said to be *overidentified*. If you have fewer moments than parameters to be estimated $RK$ the model in GMM estimation as we saw in the previous example. The main reason is that not all moments are orthogonal. That is, some moments convey roughly the same information about the data and, therefore, do not separately identify any extra parameters. So a good GMM model often is overidentified $R>K$.\n", 1518 | "\n", 1519 | "One last point about GMM regards moment selection and verification of results. The real world has an infinite supply of potential moments that describe some part of the data. Choosing moments to estimate parameters by GMM requires understanding of the model, intuition about its connections to the real world, and artistry. A good GMM estimation will include moments that have some relation to or story about their connection to particular parameters of the model to be estimated. In addition, a good verification of a GMM estimation is to take some moment from the data that was not used in the estimation and see how well the corresponding moment from the estimated model matches that *outside moment*." 1520 | ] 1521 | }, 1522 | { 1523 | "cell_type": "markdown", 1524 | "metadata": {}, 1525 | "source": [ 1526 | "## 7. Linear regression by GMM and relation to OLS\n", 1527 | "The most common method of estimating the parameters of a linear regression is using the ordinary least squares (OLS) estimator. This estimator is just special type of generalized method of moments (GMM) estimator. A simple regression specification in which the dependent variable $y_i$ is a linear function of two independent variables $x_{1,i}$ and $x_{2,i}$ is the following:\n", 1528 | "\n", 1529 | "$$ y_i = \\beta_0 + \\beta_1 x_{1,i} + \\beta_2 x_{2,i} + \\varepsilon_i \\quad\\text{where}\\quad \\varepsilon_i\\sim N\\left(0,\\sigma^2\\right) $$\n", 1530 | "\n", 1531 | "Note that we can solve for the parameters $(\\beta_0,\\beta_1,\\beta_2)$ in a number of ways. And we can do it without making any assumptions about the distribution of the error terms $\\varepsilon_i$.\n", 1532 | "\n", 1533 | "One way we might choose the parameters is to choose $(\\beta_0,\\beta_1,\\beta_2)$ to minimize the distance between the $N$ observations of $y_i$ and the $N$ predicted values for $y_i$ given by $\\beta_0 + \\beta_1 x_{1,i} + \\beta_2 x_{2,i}$. You can think of the $N$ observations of $y_i$ as $N$ data moments. And you can think of the $N$ observations of $\\beta_0 + \\beta_1 x_{1,i} + \\beta_2 x_{2,i}$ as $N$ model moments. The least squares estimator minimizes the sum of squared errors, which is the sum of squared deviations between the $N$ values of $y_i$ and $\\beta_0 + \\beta_1 x_{1,i} + \\beta_2 x_{2,i}$.\n", 1534 | "\n", 1535 | "$$ \\varepsilon_i = y_i - \\beta_0 - \\beta_1 x_{1,i} - \\beta_2 x_{2,i} $$\n", 1536 | "\n", 1537 | "$$ \\hat{\\theta}_{OLS} = \\theta:\\quad \\min_{\\theta} \\varepsilon^T\\, I \\, \\varepsilon $$\n", 1538 | "\n", 1539 | "Let the $N\\times 1$ vector of $y_i$'s be $Y$. Let the $N\\times 3$ vector of data $(1, x_{1,i}, x_{2,i})$ be $X$. And let the vector of three parameters $(\\beta_0, \\beta_1, \\beta_2)$ be $\\beta$. It can be shown that the OLS estimator for the vector of parameters $\\beta$ is the following.\n", 1540 | "\n", 1541 | "$$ \\hat{\\beta}_{OLS} = (X^T X)^{-1}(X^T Y) $$\n", 1542 | "\n", 1543 | "But you could also just estimate the coefficients using the criterion function in the GMM statement of the problem above. This method is called nonlinear least squares or generalized least squares. Many applications of regression use a weighting matrix in the criterion function that adjusts for issues like heteroskedasticity and autocorrelation. \n", 1544 | "\n", 1545 | "Lastly, many applications use a different distance metric than the weighted sum of squared errors for the difference in moments. Sum of squared errors puts a large penalty on big differences. Sometimes you might want to maximize the sum of absolute errors, which is sometimes called median regression. You could also minimize the maximum absolute difference in the errors, which is even more extreme than the sum of squared errors on penalizing large differences." 1546 | ] 1547 | }, 1548 | { 1549 | "cell_type": "markdown", 1550 | "metadata": {}, 1551 | "source": [ 1552 | "## 8. References\n", 1553 | "* Adda, Jerome and Russell Cooper, *Dynamic Economics: Quantitative Methods and Applications*, MIT Press (2003).\n", 1554 | "* Brock, William A. and Leonard J. Mirman, \"Optimal Economic Growth and Uncertainty: The Discounted Case,\" *Journal of Economic Theory*, 4:3, pp. 479-513 (June 1972).\n", 1555 | "* Davidson, Russell and James G. MacKinnon, *Econometric Theory and Methods*, Oxford University Press (2004).\n", 1556 | "* Fuhrer, Jeffrey C., George R. Moore, and Scott D. Schuh, \"Estimating the Linear-quadratic Inventory Model: Maximum Likelihood versus Generalized Method of Moments,\" *Journal of Monetary Economics*, 35:1, pp. 115-157 (February 1995).\n", 1557 | "* Hansen, Lars Peter, \"Large Sample Properties of Generalized Method of Moments Estimators,\" *Econometrica*, 50:4, pp. 1029-1054 (July 1982).\n", 1558 | "* Hansen, Lars Peter and Kenneth J. Singleton, \"Generalized Instrumental Variables Estimation of Nonlinear Rational Expectations Models\", *Econometrica*, 50:5, pp. 1269-1286 (September 1982).\n", 1559 | "* Newey, Whitney K. and Kenneth D. West, \"A Simple, Positive, Semi-definite, Heteroskedasticy and Autocorrelation Consistent Covariance Matrix,\" *Econometrica*, 55:3, pp. 703-708 (May 1987)." 1560 | ] 1561 | } 1562 | ], 1563 | "metadata": { 1564 | "anaconda-cloud": {}, 1565 | "kernelspec": { 1566 | "display_name": "Python 3", 1567 | "language": "python", 1568 | "name": "python3" 1569 | }, 1570 | "language_info": { 1571 | "codemirror_mode": { 1572 | "name": "ipython", 1573 | "version": 3 1574 | }, 1575 | "file_extension": ".py", 1576 | "mimetype": "text/x-python", 1577 | "name": "python", 1578 | "nbconvert_exporter": "python", 1579 | "pygments_lexer": "ipython3", 1580 | "version": "3.7.4" 1581 | } 1582 | }, 1583 | "nbformat": 4, 1584 | "nbformat_minor": 1 1585 | } 1586 | -------------------------------------------------------------------------------- /GMM/data/Econ381totpts.txt: -------------------------------------------------------------------------------- 1 | 275.5 2 | 351.5 3 | 346.25 4 | 228.25 5 | 108.25 6 | 380.75 7 | 346.25 8 | 360.75 9 | 196 10 | 414.75 11 | 370.5 12 | 371.75 13 | 143.75 14 | 333.5 15 | 397.5 16 | 405.75 17 | 154.75 18 | 321 19 | 279 20 | 326.5 21 | 49.5 22 | 402.75 23 | 389.75 24 | 382.25 25 | 337.75 26 | 311 27 | 105.5 28 | 380.5 29 | 236 30 | 326.5 31 | 343.75 32 | 328.75 33 | 316.25 34 | 348.25 35 | 338.75 36 | 375.75 37 | 410 38 | 17 39 | 414.25 40 | 21.25 41 | 369.625 42 | 318.875 43 | 336.125 44 | 429.875 45 | 407.5 46 | 415.75 47 | 332.375 48 | 397 49 | 375.875 50 | 419.125 51 | 270.125 52 | 299.25 53 | 384.125 54 | 335 55 | 408.5 56 | 414.25 57 | 253.5 58 | 339.25 59 | 338.75 60 | 355.375 61 | 326.375 62 | 240.375 63 | 385 64 | 435 65 | 317.25 66 | 365.625 67 | 372.75 68 | 365.125 69 | 349.625 70 | 366.75 71 | 386.5 72 | 391.75 73 | 403 74 | 258.5 75 | 386 76 | 411 77 | 350.25 78 | 402.25 79 | 294.625 80 | 291.125 81 | 378.125 82 | 442.0 83 | 428.1 84 | 347.3 85 | 431.8 86 | 430.4 87 | 426.0 88 | 433.5 89 | 331.1 90 | 405.7 91 | 415.5 92 | 406.4 93 | 418.6 94 | 400.7 95 | 408.8 96 | 404.8 97 | 409.4 98 | 410.8 99 | 402.5 100 | 401.0 101 | 415.3 102 | 390.8 103 | 394.6 104 | 399.0 105 | 380.0 106 | 397.5 107 | 368.7 108 | 394.7 109 | 304.3 110 | 391.1 111 | 388.4 112 | 370.3 113 | 384.6 114 | 383.5 115 | 305.6 116 | 286.5 117 | 367.9 118 | 329.8 119 | 288.2 120 | 338.5 121 | 333.6 122 | 268.6 123 | 335.2 124 | 296.3 125 | 269.1 126 | 243.2 127 | 159.4 128 | 448.4 129 | 449.8 130 | 435.9 131 | 429.4 132 | 428.3 133 | 427.5 134 | 422.5 135 | 409.8 136 | 415.8 137 | 413.4 138 | 416.8 139 | 406.7 140 | 383.9 141 | 389.0 142 | 387.2 143 | 368.6 144 | 399.5 145 | 382.6 146 | 355.9 147 | 389.9 148 | 342.5 149 | 365.2 150 | 320.3 151 | 341.5 152 | 248.1 153 | 305.0 154 | 279.2 155 | 275.7 156 | 204.5 157 | 235.0 158 | 102.2 159 | 112.3 160 | 130.6 161 | 60.2 -------------------------------------------------------------------------------- /GMM/images/MLEplots.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/GMM/images/MLEplots.png -------------------------------------------------------------------------------- /KDE/KDE_probset.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/KDE/KDE_probset.pdf -------------------------------------------------------------------------------- /KDE/data/BQ_mat_raw_ITA.txt: -------------------------------------------------------------------------------- 1 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 2 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 3 | 4.40227E-06,1.14361E-06,3.31989E-06,3.49422E-06,6.06806E-06,3.53256E-06,4.57352E-06,6.23564E-06,1.68415E-05,4.05543E-06,8.73003E-06,1.11871E-05,1.74949E-05,2.08129E-05,2.51389E-05,2.03126E-05,2.56779E-05,3.08143E-05,3.28582E-05,2.25082E-05,4.72711E-05,2.83967E-05,3.78819E-05,2.82244E-05,2.32537E-05,3.67646E-05,2.62243E-05,2.58321E-05,2.2255E-05,2.54171E-05,2.69271E-05,2.53012E-05,3.63832E-05,3.5056E-05,4.57789E-05,3.27994E-05,5.07395E-05,4.42413E-05,5.70184E-05,2.36847E-05,4.31972E-05,4.78118E-05,3.98565E-05,3.66566E-05,5.69835E-05,6.33806E-05,5.57632E-05,5.98053E-05,4.57256E-05,5.77131E-05,4.43761E-05 4 | 1.01967E-05,0,0.000137913,7.97226E-05,0.000116127,0.000127526,0.000175608,0.000261162,8.79135E-05,0.000369589,0.000419157,0.00046336,0.000109627,0.000181204,0.00061489,0.000344891,0.000503343,0.000418594,0.000375547,0.000552006,0.000885624,0.000337144,0.000806012,0.000557125,0.000305367,0.000675076,0.000754112,0.000445625,0.000564363,0.000680144,0.000511569,0.000611491,0.000615179,0.000866396,0.000928943,0.001018827,0.001188168,0.001038365,0.001006771,0.001095911,0.000716556,0.000907901,0.000790047,0.000756036,0.00119508,0.001035074,0.001551555,0.000755463,0.000746949,0.000771044,0.001016874 5 | 0.000362354,7.14008E-05,0,8.03644E-05,0,0.000266842,0.000813817,0.00023318,0.00025168,0.000980904,0.00146289,0.001212241,0.001554103,0.00081565,0.001707895,0.00137016,0.001712293,0.00165709,0.00508769,0.002433042,0.002993362,0.002471363,0.002947146,0.002601887,0.00220222,0.003122599,0.00197883,0.001997649,0.002536775,0.002560403,0.002114621,0.004044303,0.003483889,0.002444064,0.004225653,0.002411519,0.004195531,0.003061232,0.004202622,0.003304409,0.004567344,0.004019395,0.003816758,0.004617068,0.003401402,0.00449409,0.005301573,0.003788769,0.002732716,0.002569613,0.0021944 6 | 0.000687324,0,0,0,0.001377772,0.001524855,0,0.001630919,0.001941149,0.001810466,0.003678639,0.004603721,0.002492423,0.005045566,0.002487443,0.002708386,0.000538893,0.007797562,0.017395843,0.012066315,0.010861142,0.007870002,0.010947978,0.024790679,0.023602579,0.009066285,0.014568873,0.010418967,0.017173959,0.007652413,0.023432263,0.016050854,0.016567831,0.025282353,0.028096625,0.02502763,0.017785312,0.026524809,0.02773421,0.01851133,0.029624393,0.013602479,0.01831391,0.041464153,0.01810032,0.020879533,0.046580695,0.019128793,0.015346558,0.015805246,0.013804186 7 | 0.001049746,0,0,0,0,0,0,0,0,0.002627751,0,0.002571572,0,0.004371755,0,0.003850452,0.003613017,0.003673207,0,0.006584352,0.001726277,0.010708746,0.009921059,0.000840006,0.005052461,0.003930283,0.003317128,0.006166279,0.006650835,0.003096097,0.002586563,0.006240828,0.006184288,0.0031153,0,0.002456804,0.006662545,0.007799945,0.003502294,0.0089357,0.002188581,0.001537249,0.000621813,0.00771827,0.002947926,0.012096282,0.00442768,0,0.004363377,0,0 -------------------------------------------------------------------------------- /KDE/data/geyser.csv: -------------------------------------------------------------------------------- 1 | 4.37 2 | 3.87 3 | 4 4 | 4.03 5 | 3.5 6 | 4.08 7 | 2.25 8 | 4.7 9 | 1.73 10 | 4.93 11 | 1.73 12 | 4.62 13 | 3.43 14 | 4.25 15 | 1.68 16 | 3.92 17 | 3.68 18 | 3.1 19 | 4.03 20 | 1.77 21 | 4.08 22 | 1.75 23 | 3.2 24 | 1.85 25 | 4.62 26 | 1.97 27 | 4.5 28 | 3.92 29 | 4.35 30 | 2.33 31 | 3.83 32 | 1.88 33 | 4.6 34 | 1.8 35 | 4.73 36 | 1.77 37 | 4.57 38 | 1.85 39 | 3.52 40 | 4 41 | 3.7 42 | 3.72 43 | 4.25 44 | 3.58 45 | 3.8 46 | 3.77 47 | 3.75 48 | 2.5 49 | 4.5 50 | 4.1 51 | 3.7 52 | 3.8 53 | 3.43 54 | 4 55 | 2.27 56 | 4.4 57 | 4.05 58 | 4.25 59 | 3.33 60 | 2 61 | 4.33 62 | 2.93 63 | 4.58 64 | 1.9 65 | 3.58 66 | 3.73 67 | 3.73 68 | 1.82 69 | 4.63 70 | 3.5 71 | 4 72 | 3.67 73 | 1.67 74 | 4.6 75 | 1.67 76 | 4 77 | 1.8 78 | 4.42 79 | 1.9 80 | 4.63 81 | 2.93 82 | 3.5 83 | 1.97 84 | 4.28 85 | 1.83 86 | 4.13 87 | 1.83 88 | 4.65 89 | 4.2 90 | 3.93 91 | 4.33 92 | 1.83 93 | 4.53 94 | 2.03 95 | 4.18 96 | 4.43 97 | 4.07 98 | 4.13 99 | 3.95 100 | 4.1 101 | 2.72 102 | 4.58 103 | 1.9 104 | 4.5 105 | 1.95 106 | 4.83 107 | 4.12 -------------------------------------------------------------------------------- /KDE/images/PDSH-cover-small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/KDE/images/PDSH-cover-small.png -------------------------------------------------------------------------------- /KDE/images/SixKernelOptions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/KDE/images/SixKernelOptions.png -------------------------------------------------------------------------------- /MLE/data/Econ381totpts.txt: -------------------------------------------------------------------------------- 1 | 275.5 2 | 351.5 3 | 346.25 4 | 228.25 5 | 108.25 6 | 380.75 7 | 346.25 8 | 360.75 9 | 196 10 | 414.75 11 | 370.5 12 | 371.75 13 | 143.75 14 | 333.5 15 | 397.5 16 | 405.75 17 | 154.75 18 | 321 19 | 279 20 | 326.5 21 | 49.5 22 | 402.75 23 | 389.75 24 | 382.25 25 | 337.75 26 | 311 27 | 105.5 28 | 380.5 29 | 236 30 | 326.5 31 | 343.75 32 | 328.75 33 | 316.25 34 | 348.25 35 | 338.75 36 | 375.75 37 | 410 38 | 17 39 | 414.25 40 | 21.25 41 | 369.625 42 | 318.875 43 | 336.125 44 | 429.875 45 | 407.5 46 | 415.75 47 | 332.375 48 | 397 49 | 375.875 50 | 419.125 51 | 270.125 52 | 299.25 53 | 384.125 54 | 335 55 | 408.5 56 | 414.25 57 | 253.5 58 | 339.25 59 | 338.75 60 | 355.375 61 | 326.375 62 | 240.375 63 | 385 64 | 435 65 | 317.25 66 | 365.625 67 | 372.75 68 | 365.125 69 | 349.625 70 | 366.75 71 | 386.5 72 | 391.75 73 | 403 74 | 258.5 75 | 386 76 | 411 77 | 350.25 78 | 402.25 79 | 294.625 80 | 291.125 81 | 378.125 82 | 442.0 83 | 428.1 84 | 347.3 85 | 431.8 86 | 430.4 87 | 426.0 88 | 433.5 89 | 331.1 90 | 405.7 91 | 415.5 92 | 406.4 93 | 418.6 94 | 400.7 95 | 408.8 96 | 404.8 97 | 409.4 98 | 410.8 99 | 402.5 100 | 401.0 101 | 415.3 102 | 390.8 103 | 394.6 104 | 399.0 105 | 380.0 106 | 397.5 107 | 368.7 108 | 394.7 109 | 304.3 110 | 391.1 111 | 388.4 112 | 370.3 113 | 384.6 114 | 383.5 115 | 305.6 116 | 286.5 117 | 367.9 118 | 329.8 119 | 288.2 120 | 338.5 121 | 333.6 122 | 268.6 123 | 335.2 124 | 296.3 125 | 269.1 126 | 243.2 127 | 159.4 128 | 448.4 129 | 449.8 130 | 435.9 131 | 429.4 132 | 428.3 133 | 427.5 134 | 422.5 135 | 409.8 136 | 415.8 137 | 413.4 138 | 416.8 139 | 406.7 140 | 383.9 141 | 389.0 142 | 387.2 143 | 368.6 144 | 399.5 145 | 382.6 146 | 355.9 147 | 389.9 148 | 342.5 149 | 365.2 150 | 320.3 151 | 341.5 152 | 248.1 153 | 305.0 154 | 279.2 155 | 275.7 156 | 204.5 157 | 235.0 158 | 102.2 159 | 112.3 160 | 130.6 161 | 60.2 -------------------------------------------------------------------------------- /MLE/images/GBtree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/MLE/images/GBtree.png -------------------------------------------------------------------------------- /MLE/images/MLEplots.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/MLE/images/MLEplots.png -------------------------------------------------------------------------------- /NumIntegr/images/Integration_num_trapezes_notation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/Integration_num_trapezes_notation.png -------------------------------------------------------------------------------- /NumIntegr/images/MidRiemann2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/MidRiemann2.png -------------------------------------------------------------------------------- /NumIntegr/images/MonteCarloCircle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/MonteCarloCircle.png -------------------------------------------------------------------------------- /NumIntegr/images/NumInt_ComparisonWHNB1000.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/NumInt_ComparisonWHNB1000.png -------------------------------------------------------------------------------- /NumIntegr/images/NumInt_Discrepancy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/NumInt_Discrepancy.png -------------------------------------------------------------------------------- /NumIntegr/images/NumInt_TrapezoidGrid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/NumInt_TrapezoidGrid.png -------------------------------------------------------------------------------- /NumIntegr/images/Riemann_sum_convergence.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/Riemann_sum_convergence.png -------------------------------------------------------------------------------- /NumIntegr/images/Simpsons_method_illustration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/Simpsons_method_illustration.png -------------------------------------------------------------------------------- /NumIntegr/images/Simpsonsrule2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/NumIntegr/images/Simpsonsrule2.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Notebooks 2 | This repository contains Jupyter notebooks authored by [Richard Evans](https://sites.google.com/site/rickecon/). 3 | -------------------------------------------------------------------------------- /SMM/data/Econ381totpts.txt: -------------------------------------------------------------------------------- 1 | 275.5 2 | 351.5 3 | 346.25 4 | 228.25 5 | 108.25 6 | 380.75 7 | 346.25 8 | 360.75 9 | 196 10 | 414.75 11 | 370.5 12 | 371.75 13 | 143.75 14 | 333.5 15 | 397.5 16 | 405.75 17 | 154.75 18 | 321 19 | 279 20 | 326.5 21 | 49.5 22 | 402.75 23 | 389.75 24 | 382.25 25 | 337.75 26 | 311 27 | 105.5 28 | 380.5 29 | 236 30 | 326.5 31 | 343.75 32 | 328.75 33 | 316.25 34 | 348.25 35 | 338.75 36 | 375.75 37 | 410 38 | 17 39 | 414.25 40 | 21.25 41 | 369.625 42 | 318.875 43 | 336.125 44 | 429.875 45 | 407.5 46 | 415.75 47 | 332.375 48 | 397 49 | 375.875 50 | 419.125 51 | 270.125 52 | 299.25 53 | 384.125 54 | 335 55 | 408.5 56 | 414.25 57 | 253.5 58 | 339.25 59 | 338.75 60 | 355.375 61 | 326.375 62 | 240.375 63 | 385 64 | 435 65 | 317.25 66 | 365.625 67 | 372.75 68 | 365.125 69 | 349.625 70 | 366.75 71 | 386.5 72 | 391.75 73 | 403 74 | 258.5 75 | 386 76 | 411 77 | 350.25 78 | 402.25 79 | 294.625 80 | 291.125 81 | 378.125 82 | 442.0 83 | 428.1 84 | 347.3 85 | 431.8 86 | 430.4 87 | 426.0 88 | 433.5 89 | 331.1 90 | 405.7 91 | 415.5 92 | 406.4 93 | 418.6 94 | 400.7 95 | 408.8 96 | 404.8 97 | 409.4 98 | 410.8 99 | 402.5 100 | 401.0 101 | 415.3 102 | 390.8 103 | 394.6 104 | 399.0 105 | 380.0 106 | 397.5 107 | 368.7 108 | 394.7 109 | 304.3 110 | 391.1 111 | 388.4 112 | 370.3 113 | 384.6 114 | 383.5 115 | 305.6 116 | 286.5 117 | 367.9 118 | 329.8 119 | 288.2 120 | 338.5 121 | 333.6 122 | 268.6 123 | 335.2 124 | 296.3 125 | 269.1 126 | 243.2 127 | 159.4 128 | 448.4 129 | 449.8 130 | 435.9 131 | 429.4 132 | 428.3 133 | 427.5 134 | 422.5 135 | 409.8 136 | 415.8 137 | 413.4 138 | 416.8 139 | 406.7 140 | 383.9 141 | 389.0 142 | 387.2 143 | 368.6 144 | 399.5 145 | 382.6 146 | 355.9 147 | 389.9 148 | 342.5 149 | 365.2 150 | 320.3 151 | 341.5 152 | 248.1 153 | 305.0 154 | 279.2 155 | 275.7 156 | 204.5 157 | 235.0 158 | 102.2 159 | 112.3 160 | 130.6 161 | 60.2 -------------------------------------------------------------------------------- /SMM/data/MacroSeries.txt: -------------------------------------------------------------------------------- 1 | 1.067109409061767906e+07,8.040697000000000000e+06,1.059437619404014759e+07,9.541199796178368997e-01 2 | 1.169486784030517004e+07,7.595071761175669730e+06,1.161078970793734863e+07,1.107009058131760426e+00 3 | 9.292394242981897667e+06,8.323735104414386675e+06,9.225588259037146345e+06,8.025962276156768382e-01 4 | 7.913164544811055996e+06,6.613792410530416295e+06,7.856274282763459720e+06,8.601761064804100965e-01 5 | 8.140994183307196945e+06,5.632135942712142132e+06,8.082465981373116374e+06,1.039183047593432274e+00 6 | 8.274413563723327592e+06,5.794291991474032402e+06,8.214926167339148000e+06,1.026655137016088570e+00 7 | 8.625915781319057569e+06,5.889252242033818737e+06,8.563901323459522799e+06,1.053010727183899320e+00 8 | 9.012820397565513849e+06,6.139431328093906865e+06,8.948024359103044495e+06,1.055407821991020123e+00 9 | 8.788319891282491386e+06,6.414807807784557343e+06,8.725137858513941988e+06,9.849403857723674216e-01 10 | 9.720971689242739230e+06,6.255021244086366147e+06,9.651084525437844917e+06,1.117296985537299614e+00 11 | 1.005004728544992767e+07,6.918829216684572399e+06,9.977794292298797518e+06,1.044295080689273014e+00 12 | 9.723085890949297696e+06,7.153046321961784735e+06,9.653183527474483475e+06,9.772390716972715330e-01 13 | 1.097813965906196646e+07,6.920333984006702900e+06,1.089921431402933970e+07,1.140484624225178711e+00 14 | 1.340418607718102634e+07,7.813609158229993656e+06,1.330781911120334454e+07,1.233322067001226596e+00 15 | 1.165034979168639891e+07,9.540329631790250540e+06,1.156659171375917830e+07,8.779369388676381281e-01 16 | 1.068305571870938502e+07,8.292049714794931933e+06,1.060625182617501542e+07,9.262353118473811708e-01 17 | 1.095902638558835164e+07,7.603585360902702436e+06,1.088023845197063498e+07,1.036194504014413376e+00 18 | 1.014599496625789441e+07,7.800005428154114634e+06,1.007305217464837246e+07,9.351633441976291428e-01 19 | 8.230279394137594849e+06,7.221336369342738762e+06,8.171109292365293950e+06,8.193788344149076641e-01 20 | 9.116554734116766602e+06,5.857840075457733124e+06,9.051012916445951909e+06,1.118873455514983295e+00 21 | 8.926962183038588613e+06,6.488639949410729110e+06,8.862783406645638868e+06,9.890944310876667034e-01 22 | 8.170313044474149123e+06,6.353698862902160734e+06,8.111574059902443551e+06,9.244848684074593592e-01 23 | 7.313353164122148417e+06,5.815159472599023953e+06,7.260775137266082689e+06,9.041545137981487068e-01 24 | 7.771116212360399775e+06,5.205224658750404604e+06,7.715247181049351580e+06,1.073326032472519564e+00 25 | 8.270994704985442571e+06,5.531034099793647416e+06,8.211531887866410427e+06,1.075075945043459491e+00 26 | 7.792057251047311351e+06,5.886818894784224220e+06,7.736037667934680358e+06,9.516104387424634270e-01 27 | 7.198780378778908402e+06,5.545938728150408715e+06,7.147026052194055170e+06,9.331932630657409078e-01 28 | 8.208012195392226800e+06,5.123678331900492311e+06,8.149002179608852603e+06,1.151711953031333113e+00 29 | 8.903863600585354492e+06,5.841991562554065138e+06,8.839850887263787910e+06,1.095734436384216970e+00 30 | 8.777845693588284776e+06,6.337258618834963068e+06,8.714738963165363297e+06,9.958048774490921851e-01 31 | 9.239356644044132903e+06,6.247566311869231984e+06,9.172931964302629232e+06,1.063208878876675589e+00 32 | 1.037910934288493544e+07,6.576043294408669695e+06,1.030449061772212200e+07,1.134705503326908227e+00 33 | 1.149978250872196816e+07,7.387253791118714958e+06,1.141710690698175691e+07,1.119165580037454699e+00 34 | 1.131660389668392576e+07,8.184884572281049564e+06,1.123524522436952591e+07,9.940112361502251304e-01 35 | 1.494471982994309627e+07,8.054508559125592932e+06,1.483727747580793500e+07,1.333940529660631036e+00 36 | 1.803503596873445064e+07,1.063679305938092060e+07,1.790537634691198170e+07,1.218972871791606849e+00 37 | 1.773586408417364582e+07,1.283630256042412296e+07,1.760835530438328162e+07,9.933450788507168783e-01 38 | 1.856367822774336115e+07,1.262336919924579561e+07,1.843021802822860330e+07,1.057247058279278562e+00 39 | 1.794332526121625304e+07,1.321255975196111202e+07,1.781432497690076753e+07,9.763458916152938372e-01 40 | 1.353859981713458337e+07,1.277102814723332971e+07,1.344126650793916360e+07,7.621415290397538511e-01 41 | 1.084368829800117947e+07,9.635997610346725211e+06,1.076572956665642746e+07,8.090364329381378727e-01 42 | 9.325827922413226217e+06,7.717914403130581602e+06,9.258781573090843856e+06,8.687107140652687853e-01 43 | 9.265801012619467452e+06,6.637588582915809005e+06,9.199186215883757919e+06,1.003599362985026389e+00 44 | 8.578808872826991603e+06,6.594864876835281029e+06,8.517133081546826288e+06,9.352093247096725515e-01 45 | 8.453526674622744322e+06,6.105903336736494675e+06,8.392751576996220276e+06,9.953498159865660222e-01 46 | 9.036591108828714117e+06,6.016734665025904775e+06,8.971624175146618858e+06,1.079770628073997374e+00 47 | 9.301513267203677446e+06,6.431726434527518228e+06,9.234641723687322810e+06,1.039713740891855087e+00 48 | 8.580454046207016334e+06,6.620282808119283989e+06,8.518766427251061425e+06,9.317973377255216461e-01 49 | 8.639362411512780935e+06,6.107074276639634743e+06,8.577251281542986631e+06,1.017035771242823294e+00 50 | 8.932467127214476466e+06,6.149001867009602487e+06,8.868248774023273960e+06,1.044370364168287324e+00 51 | 9.517887844292486086e+06,6.357616965929094702e+06,9.449460714977135882e+06,1.076301540059070527e+00 52 | 8.566070335414798930e+06,6.774285802219809964e+06,8.504486125540198758e+06,9.090878606630584668e-01 53 | 7.551059668105320074e+06,6.096836777585538104e+06,7.496772693428767845e+06,8.904121375880387212e-01 54 | 7.248837365314833820e+06,5.374410492978756316e+06,7.196723163099287078e+06,9.696729024258494700e-01 55 | 7.728539677774420939e+06,5.159306019339103252e+06,7.672976742740781046e+06,1.076945907557496973e+00 56 | 8.377893946162032895e+06,5.500730568330367096e+06,8.317662596326565370e+06,1.094970006025039133e+00 57 | 8.885703970236858353e+06,5.962903633711350150e+06,8.821821812285821885e+06,1.071326351643126173e+00 58 | 1.006819459333958849e+07,6.324333637152483687e+06,9.995811133408015594e+06,1.144523108432427438e+00 59 | 1.015180658716110699e+07,7.165962533225946128e+06,1.007882201395659335e+07,1.018489461340474911e+00 60 | 9.286491385895080864e+06,7.225472747246803716e+06,9.219727839471323416e+06,9.240024668171006317e-01 61 | 9.383838529778419062e+06,6.609591095952022821e+06,9.316375123710174114e+06,1.020689556870822079e+00 62 | 9.495230029729578644e+06,6.678877200756356120e+06,9.426965794664766639e+06,1.022091483536829992e+00 63 | 9.999462335053941235e+06,6.758159271416563541e+06,9.927573013233970851e+06,1.063741159875053910e+00 64 | 1.113815108849541284e+07,7.117042860177034512e+06,1.105807537029671483e+07,1.125126260609710549e+00 65 | 1.092330020929127000e+07,7.927496101671328768e+06,1.084476912254182808e+07,9.906165293841555641e-01 66 | 1.245453877191625349e+07,7.774577588194632903e+06,1.236499912309384905e+07,1.151697925792982025e+00 67 | 1.328882534400501847e+07,8.864425233417959884e+06,1.319328774310665950e+07,1.077764190939212385e+00 68 | 1.249206150678228587e+07,9.458222488937489688e+06,1.240225209505944327e+07,9.495379478320251643e-01 69 | 1.106214614410276525e+07,8.891131760561570525e+06,1.098261684967409074e+07,8.944788646755048589e-01 70 | 9.312323456334056333e+06,7.873400148438761011e+06,9.245374194922549650e+06,8.503220990842641491e-01 71 | 9.965429895271671936e+06,6.627976879739297554e+06,9.893785243508335203e+06,1.080942994571130633e+00 72 | 1.009363420817955770e+07,7.092820524570276961e+06,1.002106785474861972e+07,1.023095864043940484e+00 73 | 1.169392730921449326e+07,7.184068989662883803e+06,1.160985593862445466e+07,1.170247261141407424e+00 74 | 1.227386870626889728e+07,8.323065688413867727e+06,1.218562795213276334e+07,1.060195334742693785e+00 75 | 1.181903628822179511e+07,8.735834659477235749e+06,1.173406546930613182e+07,9.726697245063217201e-01 76 | 1.019596270829312876e+07,8.412111072650838643e+06,1.012266068266007304e+07,8.713868017193997506e-01 77 | 9.388309083937661722e+06,7.256900537672508508e+06,9.320813537630682811e+06,9.300877965268138370e-01 78 | 8.414021979866554961e+06,6.682059084391094744e+06,8.353530894081825390e+06,9.052761285220312093e-01 79 | 8.926475153239617124e+06,5.988617492688309401e+06,8.862299878259120509e+06,1.071620871742945846e+00 80 | 9.431544200004776940e+06,6.353352223069204018e+06,9.363737822668215260e+06,1.067253553019710921e+00 81 | 9.418620401307521388e+06,6.712831356319725513e+06,9.350906937278950587e+06,1.008716895067287789e+00 82 | 1.027266482097715512e+07,6.703632938828593120e+06,1.019881135940901935e+07,1.101693100473360998e+00 83 | 9.590623076372442767e+06,7.311492695245284587e+06,9.521673030291026458e+06,9.430365173756707353e-01 84 | 1.052809809608503617e+07,6.826054562060354277e+06,1.045240824328880757e+07,1.108837500610063254e+00 85 | 1.083601238664177805e+07,7.493295426826694980e+06,1.075810883987030387e+07,1.039643338931661498e+00 86 | 1.192356906173364632e+07,7.712451130375981331e+06,1.183784672339184396e+07,1.111479825190463844e+00 87 | 1.268884541692265868e+07,8.486511495838487521e+06,1.259762126294957660e+07,1.074931130627896358e+00 88 | 1.389568729577491246e+07,9.031191243335224688e+06,1.379578677088231966e+07,1.106172178186569655e+00 89 | 1.195106905936053768e+07,9.890151964367004111e+06,1.186514901477081887e+07,8.687434217318290575e-01 90 | 1.160835007837390713e+07,8.506084414382245392e+06,1.152489394977210835e+07,9.811344978035811959e-01 91 | 1.102588897818392515e+07,8.262156731577994302e+06,1.094662034807716124e+07,9.594181359910609608e-01 92 | 1.083286196217405796e+07,7.847594380569793284e+06,1.075498106480820477e+07,9.924174669205790833e-01 93 | 1.207973255563328415e+07,7.710208839219391346e+06,1.199288750816039741e+07,1.126364399251188919e+00 94 | 1.263172160153367743e+07,8.597659699815668166e+06,1.254090812887629867e+07,1.056258049610023564e+00 95 | 1.201259491846160591e+07,8.990533793080620468e+06,1.192623254486089014e+07,9.605922805961057964e-01 96 | 1.220885534363994934e+07,8.549874986470952630e+06,1.212108199128923565e+07,1.026603926836413105e+00 97 | 1.525028173818298802e+07,8.689561882720788941e+06,1.514064260209882446e+07,1.261733762460238939e+00 98 | 1.471566191623893753e+07,1.085427447233221307e+07,1.460986633245221525e+07,9.746905153025023916e-01 99 | 1.342830691876499727e+07,1.047376279488555714e+07,1.333176654036922008e+07,9.217353904837203293e-01 100 | 1.348767324779560789e+07,9.557497461181923747e+06,1.339070606593880989e+07,1.014566650429502337e+00 101 | -------------------------------------------------------------------------------- /SMM/data/usincmoms.txt: -------------------------------------------------------------------------------- 1 | 0.035190156 2500 0.041062064 7500 0.058859479 12500 0.056836107 17500 0.0585952 22500 0.054432836 27500 0.055060495 32500 0.050675146 37500 0.047858942 42500 0.04083908 47500 0.042697279 52500 0.03509931 57500 0.036602387 62500 0.031680225 67500 0.029780733 72500 0.028508899 77500 0.025073296 82500 0.021191725 87500 0.021422967 92500 0.018590247 97500 0.020869637 102500 0.014626089 107500 0.014229673 112500 0.01295784 117500 0.012718338 122500 0.010389396 127500 0.010001239 132500 0.007581451 137500 0.00851468 142500 0.007374985 147500 0.009629599 152500 0.006111409 157500 0.005756287 162500 0.005037783 167500 0.005095594 172500 0.00437709 177500 0.003798984 182500 0.002997894 187500 0.003138291 192500 0.002576702 197500 0.018970145 225000 0.023190321 300000 -------------------------------------------------------------------------------- /SMM/images/GBtree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/SMM/images/GBtree.png -------------------------------------------------------------------------------- /SMM/images/MLEplots.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/SMM/images/MLEplots.png -------------------------------------------------------------------------------- /SMM/images/critvals_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/SMM/images/critvals_1.png -------------------------------------------------------------------------------- /SMM/images/hist_pdf_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickecon/Notebooks/132aeb502ca1fe5e36b06e1b2b7068f57c972978/SMM/images/hist_pdf_1.png --------------------------------------------------------------------------------