├── .gitignore ├── LICENSE ├── README.md ├── examples ├── Example Fama-MacBeth regression.ipynb ├── Example GJR-GARCH.ipynb ├── Example GMM.ipynb ├── Example LaTeX Output.ipynb ├── FTSE_1984_2012.csv ├── FTSE_1984_2012.zip ├── FamaFrench.csv ├── FamaFrench.zip ├── example-fama-macbeth-regression.py ├── example-gjr-garch.py ├── example-gmm.py ├── example-latex-output.py ├── examples_gjrgarch_lineprofiler.py ├── examples_gjrgarch_lineprofiler.py.prof ├── latex.pdf └── latex.tex └── solutions ├── chapter02 └── builtin_datatypes.ipynb ├── chapter03 └── arrays_and_matrices.ipynb ├── chapter04 └── basic_math.ipynb ├── chapter05 └── basic_functions.ipynb ├── chapter06 └── special_matrices.ipynb ├── chapter07 ├── Special_Arrays.ipynb └── array_and_matrix_functions_solutions.ipynb ├── chapter08 ├── exercise3 ├── exercise3.mat ├── exercise3.xls ├── exercise3.xlsx ├── exercise3_new.csv ├── exercise3_numeric.csv ├── exercise3_numeric.txt ├── exercise3_numeric.xls └── importing_and_exporting_data_solutions.ipynb ├── chapter09 └── inf_nan_and_numerics_solutions.ipynb ├── chapter10 └── logical_operators_and_find_solutions.ipynb ├── chapter11 └── advanced_selection.ipynb ├── chapter12 └── flow_control_and_loops_solutions.ipynb ├── chapter13 └── no-exercises-in-chapter.txt ├── chapter14 └── dates_and_times.ipynb ├── chapter15 ├── ftse.csv ├── graphics_solutions.ipynb └── sp500.csv ├── chapter16 ├── pandas.ipynb └── problem8.csv ├── chapter17 └── no-exercises-in-chapter.txt ├── chapter18 └── custom_functions_and_modules_solutions.ipynb ├── chapter19 └── probability_and_statistics_functions.ipynb ├── chapter20 └── simulation.ipynb ├── chapter21 └── optimization_solutions.ipynb ├── chapter22 └── string_manipulation.ipynb ├── chapter23 └── file_system_and_navigation_solutions.ipynb ├── chapter25 └── improving_performance_using_numba.ipynb └── chapter26 ├── cython_arma.c ├── cython_arma.pyx ├── improving_performance_using_cython.ipynb └── setup_cython_arma.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | **/__pycache__/ 3 | **/*.aux 4 | **/*.toc 5 | **/*-lyxformat-413.lyx 6 | **/*-lyxformat-474.lyx 7 | **/*-lyxformat-508.lyx 8 | **/*.lyx\# 9 | **/*.npz 10 | **/*.h5 11 | **/*.pyd 12 | **/*.pyc 13 | **/.ipynb_checkpoints 14 | **/*.lprof 15 | **/rets.* 16 | 17 | ## Core latex/pdflatex auxiliary files: 18 | **/*.lof 19 | **/*.log 20 | **/*.lot 21 | **/*.fls 22 | **/*.out 23 | **/*.fmt 24 | **/*.fot 25 | **/*.cb 26 | **/*.cb2 27 | **/*./.*.lb 28 | 29 | ## Intermediate documents: 30 | **/*.dvi 31 | **/*.xdv 32 | **/*-converted-to.* 33 | # these rules might exclude image files for figures etc. 34 | # *.ps 35 | # *.eps 36 | # *.pdf 37 | 38 | ## Bibliography auxiliary files (bibtex/biblatex/biber): 39 | **/*.bbl 40 | **/*.bcf 41 | **/*.blg 42 | **/*-blx.aux 43 | **/*-blx.bib 44 | **/*.run.xml 45 | 46 | ## Build tool auxiliary files: 47 | **/*.fdb_latexmk 48 | **/*.synctex 49 | **/*.synctex(busy) 50 | **/*.synctex.gz 51 | **/*.synctex.gz(busy) 52 | **/*.pdfsync 53 | 54 | ## Build tool directories for auxiliary files 55 | # latexrun 56 | **/latex.out/ 57 | 58 | ## Auxiliary and intermediate files from other packages: 59 | # algorithms 60 | **/*.alg 61 | **/*.loa 62 | 63 | # achemso 64 | **/*./acs-*.bib 65 | 66 | # amsthm 67 | **/*.thm 68 | 69 | # beamer 70 | **/*.nav 71 | **/*.pre 72 | **/*.snm 73 | **/*.vrb 74 | 75 | # changes 76 | **/*.soc 77 | 78 | # comment 79 | **/*.cut 80 | 81 | # cprotect 82 | **/*.cpt 83 | 84 | # elsarticle (documentclass of Elsevier journals) 85 | **/*.spl 86 | 87 | # endnotes 88 | **/*.ent 89 | 90 | # fixme 91 | **/*.lox 92 | 93 | # feynmf/feynmp 94 | **/*.mf 95 | **/*.mp 96 | **/*.t[1-9] 97 | **/*.t[1-9][0-9] 98 | **/*.tfm 99 | 100 | #(r)(e)ledmac/(r)(e)ledpar 101 | **/*.end 102 | **/*.?end 103 | **/*.[1-9] 104 | **/*.[1-9][0-9] 105 | **/*.[1-9][0-9][0-9] 106 | **/*.[1-9]R 107 | **/*.[1-9][0-9]R 108 | **/*.[1-9][0-9][0-9]R 109 | **/*.eledsec[1-9] 110 | **/*.eledsec[1-9]R 111 | **/*.eledsec[1-9][0-9] 112 | **/*.eledsec[1-9][0-9]R 113 | **/*.eledsec[1-9][0-9][0-9] 114 | **/*.eledsec[1-9][0-9][0-9]R 115 | 116 | # glossaries 117 | **/*.acn 118 | **/*.acr 119 | **/*.glg 120 | **/*.glo 121 | **/*.gls 122 | **/*.glsdefs 123 | **/*.lzo 124 | **/*.lzs 125 | 126 | # uncomment this for glossaries-extra (will ignore makeindex's style files!) 127 | # *.ist 128 | 129 | # gnuplottex 130 | **/*.gnuplottex-* 131 | 132 | # gregoriotex 133 | **/*.gaux 134 | **/*.gtex 135 | 136 | # htlatex 137 | **/*.4ct 138 | **/*.4tc 139 | **/*.idv 140 | **/*.lg 141 | **/*.trc 142 | **/*.xref 143 | 144 | # hyperref 145 | **/*.brf 146 | 147 | # knitr 148 | **/*.concordance.tex 149 | **/*.tikzDictionary 150 | 151 | # listings 152 | **/*.lol 153 | 154 | # luatexja-ruby 155 | **/*.ltjruby 156 | 157 | # makeidx 158 | **/*.idx 159 | **/*.ilg 160 | **/*.ind 161 | 162 | # minitoc 163 | **/*.maf 164 | **/*.mlf 165 | **/*.mlt 166 | **/*.mtc[0-9]* 167 | **/*.slf[0-9]* 168 | **/*.slt[0-9]* 169 | **/*.stc[0-9]* 170 | 171 | # minted 172 | _minted* 173 | **/*.pyg 174 | 175 | # morewrites 176 | **/*.mw 177 | 178 | # nomencl 179 | **/*.nlg 180 | **/*.nlo 181 | **/*.nls 182 | 183 | # pax 184 | **/*.pax 185 | 186 | # pdfpcnotes 187 | **/*.pdfpc 188 | 189 | # sagetex 190 | **/*.sagetex.sage 191 | **/*.sagetex.py 192 | **/*.sagetex.scmd 193 | 194 | # scrwfile 195 | **/*.wrt 196 | 197 | # sympy 198 | **/*.sout 199 | **/*.sympy 200 | **/sympy-plots-for-*.tex/ 201 | 202 | # pdfcomment 203 | **/*.upa 204 | **/*.upb 205 | 206 | # pythontex 207 | **/*.pytxcode 208 | pythontex-files-*/ 209 | 210 | # tcolorbox 211 | **/*.listing 212 | 213 | # thmtools 214 | **/*.loe 215 | 216 | # TikZ & PGF 217 | **/*.dpth 218 | **/*.md5 219 | **/*.auxlock 220 | 221 | # todonotes 222 | **/*.tdo 223 | 224 | # vhistory 225 | **/*.hst 226 | **/*.ver 227 | 228 | # easy-todo 229 | **/*.lod 230 | 231 | # xcolor 232 | **/*.xcp 233 | 234 | # xmpincl 235 | **/*.xmpi 236 | 237 | # xindy 238 | **/*.xdy 239 | 240 | # xypic precompiled matrices and outlines 241 | **/*.xyc 242 | **/*.xyd 243 | 244 | # endfloat 245 | **/*.ttt 246 | **/*.fff 247 | 248 | # Latexian 249 | TSWLatexianTemp* 250 | 251 | ## Editors: 252 | # WinEdt 253 | **/*.bak 254 | **/*.sav 255 | 256 | # Texpad 257 | .texpadtmp 258 | 259 | # LyX 260 | **/*.lyx~ 261 | 262 | # Kile 263 | **/*.backup 264 | 265 | # gummi 266 | .*.swp 267 | 268 | # KBibTeX 269 | **/*.[0-9]* 270 | 271 | # TeXnicCenter 272 | **/*.tps 273 | 274 | # auto folder when using emacs and auctex 275 | ./auto/* 276 | **/*.el 277 | 278 | # expex forward references with /gathertags 279 | **/*.tags.tex 280 | 281 | # standalone packages 282 | **/*.sta 283 | 284 | # Makeindex log files 285 | **/*.lpz 286 | 287 | # xwatermark package 288 | **/*.xwm 289 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python python-for-econometrics-statistics-data-analysis 2 | Code from _Introduction to Python for Econometrics, Statistics and Numerical Analysis: Fourth Edition_ by Kevin Sheppard 3 | 4 | The main text is available [on my website](https://www.kevinsheppard.com/teaching/python/notes/). 5 | 6 | All code is licensed CC0 1.0 Universal. 7 | -------------------------------------------------------------------------------- /examples/Example GMM.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Risk Premia Estimation using GMM" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Start by importing the modules and functions needed" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "from numpy import hstack, ones, array, mat, tile, reshape, squeeze, eye, asmatrix\n", 24 | "from numpy.linalg import inv\n", 25 | "from pandas import read_csv, Series \n", 26 | "from scipy.linalg import kron\n", 27 | "from scipy.optimize import minimize\n", 28 | "import numpy as np\n", 29 | "import statsmodels.api as sm" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "Next a callable function is used to produce iteration-by-iteration output when using the non-linear optimizer." 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "iteration = 0\n", 46 | "last_value = 0\n", 47 | "function_count = 0\n", 48 | "\n", 49 | "def iter_print(params):\n", 50 | " global iteration, last_value, function_count\n", 51 | " iteration += 1\n", 52 | " print(f'Func value: {last_value:6.6g}, Iteration: {iteration}, Function Count: {function_count}')" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "The GMM objective, which is minimized, is defined next." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "def gmm_objective(params, p_rets, f_rets, Winv, out=False):\n", 69 | " global last_value, function_count\n", 70 | " t,n = p_rets.shape\n", 71 | " t,k = f_rets.shape\n", 72 | " beta = squeeze(array(params[:(n*k)]))\n", 73 | " lam = squeeze(array(params[(n*k):]))\n", 74 | " beta = reshape(beta,(n,k))\n", 75 | " lam = reshape(lam,(k,1))\n", 76 | " betalam = beta @ lam\n", 77 | " expected_ret = f_rets @ beta.T\n", 78 | " e = p_rets - expected_ret\n", 79 | " instr = tile(f_rets,n)\n", 80 | " moments1 = kron(e,ones((1,k)))\n", 81 | " moments1 = moments1 * instr\n", 82 | " moments2 = p_rets - betalam.T\n", 83 | " moments = hstack((moments1,moments2))\n", 84 | "\n", 85 | " avg_moment = moments.mean(axis=0)\n", 86 | " \n", 87 | " J = t * mat(avg_moment) * mat(Winv) * mat(avg_moment).T\n", 88 | " J = J[0,0]\n", 89 | " last_value = J\n", 90 | " function_count += 1\n", 91 | " if not out:\n", 92 | " return J\n", 93 | " else:\n", 94 | " return J, moments" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "The `G` matrix, which is the derivative of the GMM moments with respect to the parameters, is defined." 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "def gmm_G(params, p_rets, f_rets):\n", 111 | " t,n = p_rets.shape\n", 112 | " t,k = f_rets.shape\n", 113 | " beta = squeeze(array(params[:(n*k)]))\n", 114 | " lam = squeeze(array(params[(n*k):]))\n", 115 | " beta = reshape(beta,(n,k))\n", 116 | " lam = reshape(lam,(k,1))\n", 117 | " G = np.zeros((n*k+k,n*k+n))\n", 118 | " ffp = (f_rets.T @ f_rets) / t\n", 119 | " G[:(n*k),:(n*k)]=kron(eye(n),ffp)\n", 120 | " G[:(n*k),(n*k):] = kron(eye(n),-lam)\n", 121 | " G[(n*k):,(n*k):] = -beta.T\n", 122 | " \n", 123 | " return G" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "Next, the data is imported and a subset of the test portfolios is selected to make the estimation faster." 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "data = read_csv('FamaFrench.csv')\n", 140 | "\n", 141 | "# Split using both named colums and ix for larger blocks\n", 142 | "dates = data['date'].values\n", 143 | "factors = data[['VWMe','SMB','HML']].values\n", 144 | "riskfree = data['RF'].values\n", 145 | "portfolios = data.iloc[:,5:].values\n", 146 | "\n", 147 | "t,n = portfolios.shape\n", 148 | "portfolios = portfolios[:,np.arange(0,n,2)]\n", 149 | "t,n = portfolios.shape\n", 150 | "excess_ret = portfolios - np.reshape(riskfree,(t,1))\n", 151 | "k = np.size(factors,1)" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "Starting values for the factor loadings and rick premia are estimated using OLS and simple means." 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "betas = []\n", 168 | "for i in range(n):\n", 169 | " res = sm.OLS(excess_ret[:,i],sm.add_constant(factors)).fit()\n", 170 | " betas.append(res.params[1:])\n", 171 | "\n", 172 | "avg_return = excess_ret.mean(axis=0)\n", 173 | "avg_return.shape = n,1\n", 174 | "betas = array(betas)\n", 175 | "res = sm.OLS(avg_return, betas).fit()\n", 176 | "risk_premia = res.params" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "The starting values are computed the first step estimates are found using the non-linear optimizer. The initial weighting matrix is just the identify matrix." 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "risk_premia.shape = 3\n", 193 | "starting_vals = np.concatenate((betas.flatten(),risk_premia))\n", 194 | "\n", 195 | "Winv = np.eye(n*(k+1))\n", 196 | "args = (excess_ret, factors, Winv)\n", 197 | "iteration = 0\n", 198 | "function_count = 0\n", 199 | "opt = minimize(gmm_objective, starting_vals, args=args, callback=iter_print)\n", 200 | "step1opt = opt.x" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "Here we look at the risk premia estimates from the first step (inefficient) estimates." 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [ 216 | "premia = step1opt[-3:]\n", 217 | "premia = Series(premia,index=['VWMe', 'SMB', 'HML'])\n", 218 | "print('Annualized Risk Premia (First step)')\n", 219 | "print(12 * premia)" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "Next the first step estimates are used to estimate the moment conditions which are in-turn used to estimate the optimal weighting matrix for the moment conditions. This is then used as an input for the 2nd-step estimates." 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": null, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "out = gmm_objective(step1opt, excess_ret, factors, Winv, out=True)\n", 236 | "S = np.cov(out[1].T)\n", 237 | "Winv2 = inv(S)\n", 238 | "args = (excess_ret, factors, Winv2)\n", 239 | "\n", 240 | "iteration = 0\n", 241 | "function_count = 0\n", 242 | "opt = minimize(gmm_objective, step1opt, args=args, callback=iter_print)\n", 243 | "step2opt = opt.x" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "Finally the VCV of the parameter estimates is computed." 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [ 259 | "out = gmm_objective(step2opt, excess_ret, factors, Winv2, out=True)\n", 260 | "G = gmm_G(step2opt, excess_ret, factors)\n", 261 | "S = np.cov(out[1].T)\n", 262 | "vcv = inv(G @ inv(S) @ G.T)/t" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "The annualized risk premia and their associated t-stats." 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [ 278 | "premia = step2opt[-3:]\n", 279 | "premia = Series(premia,index=['VWMe', 'SMB', 'HML'])\n", 280 | "premia_vcv = vcv[-3:,-3:]\n", 281 | "print('Annualized Risk Premia')\n", 282 | "print(12 * premia)\n", 283 | "\n", 284 | "premia_stderr = np.diag(premia_vcv)\n", 285 | "premia_stderr = Series(premia_stderr,index=['VWMe', 'SMB', 'HML'])\n", 286 | "print('T-stats')\n", 287 | "print(premia / premia_stderr)" 288 | ] 289 | } 290 | ], 291 | "metadata": { 292 | "kernelspec": { 293 | "display_name": "Python 3 (ipykernel)", 294 | "language": "python", 295 | "name": "python3" 296 | }, 297 | "language_info": { 298 | "codemirror_mode": { 299 | "name": "ipython", 300 | "version": 3 301 | }, 302 | "file_extension": ".py", 303 | "mimetype": "text/x-python", 304 | "name": "python", 305 | "nbconvert_exporter": "python", 306 | "pygments_lexer": "ipython3", 307 | "version": "3.9.6" 308 | } 309 | }, 310 | "nbformat": 4, 311 | "nbformat_minor": 4 312 | } 313 | -------------------------------------------------------------------------------- /examples/Example LaTeX Output.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "pycharm": { 7 | "name": "#%% md\n" 8 | } 9 | }, 10 | "source": [ 11 | "# Example: Exporting to $\\LaTeX$\n", 12 | "\n", 13 | "The first code block contains the imports needed and defines a flag which determines whether the \n", 14 | "output $\\LaTeX$ should be compiled." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "# imports\n", 24 | "import subprocess\n", 25 | "\n", 26 | "import numpy as np\n", 27 | "\n", 28 | "# Flag to compile output tables\n", 29 | "compile_latex = False" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": { 35 | "pycharm": { 36 | "name": "#%% md\n" 37 | } 38 | }, 39 | "source": [ 40 | "The next code block loads the npz file created using the output from the Fama-MacBeth example.\n", 41 | "The second part shows a generic method to restore all variables. The loaded data is in a dictionary,\n", 42 | "and so iterating over the keys and using `globals()` (a dictionary) in the main program." 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": { 49 | "pycharm": { 50 | "name": "#%%\n" 51 | } 52 | }, 53 | "outputs": [], 54 | "source": [ 55 | "# Load variables\n", 56 | "f = np.load(\"fama-macBeth-results.npz\")\n", 57 | "data = f.items()\n", 58 | "# Manually load parameters and std errors\n", 59 | "arp = f[\"arp\"]\n", 60 | "arp_se = f[\"arp_se\"]\n", 61 | "beta = f[\"beta\"]\n", 62 | "beta_se = f[\"beta_se\"]\n", 63 | "J = f[\"J\"]\n", 64 | "Jpval = f[\"Jpval\"]\n", 65 | "\n", 66 | "# Generic restore of all data in a npz file\n", 67 | "for key in f.keys():\n", 68 | " globals()[key] = f[key]\n", 69 | "f.close()" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": { 75 | "pycharm": { 76 | "name": "#%% md\n" 77 | } 78 | }, 79 | "source": [ 80 | "The document is be stored in a list. The first few lines contain the required header for a\n", 81 | "$\\LaTeX$ document, including some packages used to improve table display and to select a custom font.\n", 82 | " " 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": { 89 | "pycharm": { 90 | "name": "#%%\n" 91 | } 92 | }, 93 | "outputs": [], 94 | "source": [ 95 | "# List to hold table\n", 96 | "latex = []\n", 97 | "# Initializd LaTeX document\n", 98 | "latex.append(r\"\\documentclass[a4paper]{article}\")\n", 99 | "latex.append(r\"\\usepackage{amsmath}\")\n", 100 | "latex.append(r\"\\usepackage{booktabs}\")\n", 101 | "latex.append(r\"\\usepackage[adobe-utopia]{mathdesign}\")\n", 102 | "latex.append(r\"\\usepackage[T1]{fontenc}\")\n", 103 | "latex.append(r\"\\begin{document}\")" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": { 109 | "pycharm": { 110 | "name": "#%% md\n" 111 | } 112 | }, 113 | "source": [ 114 | "Table 1 is stored in its own list, and then extend will be used to add it to the main list. " 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": { 121 | "pycharm": { 122 | "name": "#%%\n" 123 | } 124 | }, 125 | "outputs": [], 126 | "source": [ 127 | "# Table 1\n", 128 | "table1 = []\n", 129 | "table1.append(r\"\\begin{center}\")\n", 130 | "table1.append(r\"\\begin{tabular}{lrrr} \\toprule\")\n", 131 | "# Header\n", 132 | "colNames = [r\"VWM$^e$\", \"SMB\", \"HML\"]\n", 133 | "header = \"\"\n", 134 | "for cName in colNames:\n", 135 | " header += \" & \" + cName\n", 136 | "\n", 137 | "header += r\"\\\\ \\cmidrule{2-4}\"\n", 138 | "table1.append(header)\n", 139 | "# Main row\n", 140 | "row = \"\"\n", 141 | "for a, se in zip(arp, arp_se):\n", 142 | " row += r\" & $\\underset{{({0:0.3f})}}{{{1:0.3f}}}$\".format(se, a)\n", 143 | "table1.append(row)\n", 144 | "# Blank row\n", 145 | "row = r\"\\\\\"\n", 146 | "table1.append(row)\n", 147 | "# J-stat row\n", 148 | "row = r\"J-stat: $\\underset{{({0:0.3f})}}{{{1:0.1f}}}$ \\\\\".format(float(Jpval), float(J))\n", 149 | "table1.append(row)\n", 150 | "table1.append(r\"\\bottomrule \\end{tabular}\")\n", 151 | "table1.append(r\"\\end{center}\")\n", 152 | "# Extend latex with table 1\n", 153 | "latex.extend(table1)\n", 154 | "latex.append(r\"\\newpage\")" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": { 160 | "pycharm": { 161 | "name": "#%% md\n" 162 | } 163 | }, 164 | "source": [ 165 | "Table 2 is a bit more complex, and uses loops to iterate over the rows of the arrays containing\n", 166 | "the $\\beta$s and their standard errors." 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": { 173 | "pycharm": { 174 | "name": "#%%\n" 175 | } 176 | }, 177 | "outputs": [], 178 | "source": [ 179 | "# Format information for table 2\n", 180 | "sizes = [\"S\", \"2\", \"3\", \"4\", \"B\"]\n", 181 | "values = [\"L\", \"2\", \"3\", \"4\", \"H\"]\n", 182 | "# Table 2 has the same header as table 1, copy with a slice\n", 183 | "table2 = table1[:3]\n", 184 | "m = 0\n", 185 | "for i in range(len(sizes)):\n", 186 | " for j in range(len(values)):\n", 187 | " row = \"Size: {:}, Value: {:} \".format(sizes[i], values[j])\n", 188 | " b = beta[:, m]\n", 189 | " s = beta_se[m, 1:]\n", 190 | " for k in range(len(b)):\n", 191 | " row += r\" & $\\underset{{({0:0.3f})}}{{{1: .3f}}}$\".format(s[k], b[k])\n", 192 | " row += r\"\\\\ \"\n", 193 | " table2.append(row)\n", 194 | " m += 1\n", 195 | " if i < (len(sizes) - 1):\n", 196 | " table2.append(r\"\\cmidrule{2-4}\")\n", 197 | "\n", 198 | "table2.append(r\"\\bottomrule \\end{tabular}\")\n", 199 | "table2.append(r\"\\end{center}\")\n", 200 | "# Extend with table 2\n", 201 | "latex.extend(table2)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "The penultimate block finished the document, and uses write to write the lines to the $\\LaTeX$ file." 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "pycharm": { 216 | "name": "#%%\n" 217 | } 218 | }, 219 | "outputs": [], 220 | "source": [ 221 | "# Finish document\n", 222 | "latex.append(r\"\\end{document}\")\n", 223 | "# Write to table\n", 224 | "fid = open(\"latex.tex\", \"w\")\n", 225 | "for line in latex:\n", 226 | " fid.write(line + \"\\n\")\n", 227 | "fid.close()" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "Finally, if the flag is set, subprocess is used to compile the LaTeX.\n", 235 | " " 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": { 242 | "pycharm": { 243 | "name": "#%%\n" 244 | } 245 | }, 246 | "outputs": [], 247 | "source": [ 248 | "# Compile if needed\n", 249 | "if compile_latex:\n", 250 | " exit_status = subprocess.call(r\"pdflatex latex.tex\", shell=True)\n", 251 | "else:\n", 252 | " print(\"\\n\".join(latex))" 253 | ] 254 | } 255 | ], 256 | "metadata": { 257 | "kernelspec": { 258 | "display_name": "Python 3 (ipykernel)", 259 | "language": "python", 260 | "name": "python3" 261 | }, 262 | "language_info": { 263 | "codemirror_mode": { 264 | "name": "ipython", 265 | "version": 3 266 | }, 267 | "file_extension": ".py", 268 | "mimetype": "text/x-python", 269 | "name": "python", 270 | "nbconvert_exporter": "python", 271 | "pygments_lexer": "ipython3", 272 | "version": "3.9.6" 273 | } 274 | }, 275 | "nbformat": 4, 276 | "nbformat_minor": 4 277 | } 278 | -------------------------------------------------------------------------------- /examples/FTSE_1984_2012.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/examples/FTSE_1984_2012.zip -------------------------------------------------------------------------------- /examples/FamaFrench.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/examples/FamaFrench.zip -------------------------------------------------------------------------------- /examples/example-fama-macbeth-regression.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # # Estimating the Risk Premia using Fama-MacBeth Regressions 5 | 6 | # This example highlights how to implement a Fama-MacBeth 2-stage regression to estimate factor risk premia, make inference on the risk premia, and test whether a linear factor model can explain a cross-section of portfolio returns. This example closely follows [Cochrane::2001] (See also [JagannathanSkoulakisWang::2010]). As in the previous example, the first segment contains the imports. 7 | 8 | # In[ ]: 9 | 10 | 11 | from numpy import mat, cov, mean, hstack, multiply,sqrt,diag, squeeze, ones, array, vstack, kron, zeros, eye, savez_compressed 12 | from numpy.linalg import inv 13 | from scipy.stats import chi2 14 | from pandas import read_csv 15 | import statsmodels.api as sm 16 | 17 | 18 | # Next, the data are imported. I formatted the data downloaded from Ken French's website into an easy-to-import CSV which can be read by `pandas.read_csv`. The data is split using named columns for the small sets of variables and `ix` for the portfolios. The code uses pure NumPy arrays, and so `values` is used to retrieve the array from the DataFrame. The dimensions are determined using `shape`. Finally the risk free rate is forced to have 2 dimensions so that it will be broadcastable with the portfolio returns in the construction of the excess returns to the Size and Value-weighted portfolios. `asmatrix` is used to return matrix views of all of the arrays. This code is linear algebra-heavy and so matrices are easier to use than arrays. 19 | 20 | # In[ ]: 21 | 22 | 23 | data = read_csv('FamaFrench.csv') 24 | 25 | # Split using both named colums and ix for larger blocks 26 | dates = data['date'].values 27 | factors = data[['VWMe', 'SMB', 'HML']].values 28 | riskfree = data['RF'].values 29 | portfolios = data.iloc[:, 5:].values 30 | 31 | # Use mat for easier linear algebra 32 | factors = mat(factors) 33 | riskfree = mat(riskfree) 34 | portfolios = mat(portfolios) 35 | 36 | # Shape information 37 | t,k = factors.shape 38 | t,n = portfolios.shape 39 | # Reshape rf and compute excess returns 40 | riskfree.shape = t,1 41 | excess_returns = portfolios - riskfree 42 | 43 | 44 | # 45 | # The next block does 2 things: 46 | # 47 | # 1. Compute the time-series $\beta$s. This is done be regressing the full array of excess returns on the factors (augmented with a constant) using lstsq. 48 | # 2. Compute the risk premia using a cross-sectional regression of average excess returns on the estimates $\beta$s. This is a standard regression where the step 1 $\beta$ estimates are used as regressors, and the dependent variable is the average excess return. 49 | 50 | # In[ ]: 51 | 52 | 53 | # Time series regressions 54 | x = sm.add_constant(factors) 55 | ts_res = sm.OLS(excess_returns, x).fit() 56 | alpha = ts_res.params[0] 57 | beta = ts_res.params[1:] 58 | avgexcess_returns = mean(excess_returns, 0) 59 | # Cross-section regression 60 | cs_res = sm.OLS(avgexcess_returns.T, beta.T).fit() 61 | risk_premia = cs_res.params 62 | 63 | 64 | # 65 | # The asymptotic variance requires computing the covariance of the demeaned returns and the weighted pricing errors. The problem is formulated using 2-step GMM where the moment conditions are 66 | # \begin{equation} 67 | # g_{t}\left(\theta\right)=\left[\begin{array}{c} 68 | # \epsilon_{1t}\\ 69 | # \epsilon_{1t}f_{t}\\ 70 | # \epsilon_{2t}\\ 71 | # \epsilon_{2t}f_{t}\\ 72 | # \vdots\\ 73 | # \epsilon_{Nt}\\ 74 | # \epsilon_{Nt}f_{t}\\ 75 | # \beta u_{t} 76 | # \end{array}\right] 77 | # \end{equation} 78 | # 79 | # where $\epsilon_{it}=r_{it}^{e}-\alpha_{i}-\beta_{i}^{\prime}f_{t}$, $\beta_{i}$ is a $K$ by 1 vector of factor loadings, $f_{t}$ is a $K$ by 1 set of factors, $\beta=\left[\beta_{1}\,\beta_{2}\ldots\beta_{N}\right]$ is a $K$ by $N$ matrix of all factor loadings, $u_{t}=r_{t}^{e}-\beta'\lambda$ are the $N$ by 1 vector of pricing errors and $\lambda$ is a $K$ by 1 vector of risk premia. 80 | # The vector of parameters is then $\theta= \left[\alpha_{1}\:\beta_{1}^{\prime}\:\alpha_{2}\:\beta_{2}^{\prime}\:\ldots\:\alpha_{N}\,\beta_{N}^{\prime}\:\lambda'\right]'$ 81 | # To make inference on this problem, the derivative of the moments with respect to the parameters, $\partial g_{t}\left(\theta\right)/\partial\theta^{\prime}$ is needed. With some work, the estimator of this matrix can be seen to be 82 | # 83 | # \begin{equation} 84 | # G=E\left[\frac{\partial g_{t}\left(\theta\right)}{\partial\theta^{\prime}}\right]=\left[\begin{array}{cc} 85 | # -I_{n}\otimes\Sigma_{X} & 0\\ 86 | # G_{21} & -\beta\beta^{\prime} 87 | # \end{array}\right]. 88 | # \end{equation} 89 | # 90 | # where $X_{t}=\left[1\: f_{t}^{\prime}\right]'$ and $\Sigma_{X}=E\left[X_{t}X_{t}^{\prime}\right]$. $G_{21}$ is a matrix with the structure 91 | # 92 | # \begin{equation} 93 | # G_{21}=\left[G_{21,1}\, G_{21,2}\,\ldots G_{21,N}\right] 94 | # \end{equation} 95 | # 96 | # where 97 | # 98 | # \begin{equation} 99 | # G_{21,i}=\left[\begin{array}{cc} 100 | # 0_{K,1} & \textrm{diag}\left(E\left[u_{i}\right]-\beta_{i}\odot\lambda\right)\end{array}\right]\end{equation} 101 | # 102 | # and where $E\left[u_{i}\right]$ is the expected pricing error. In estimation, all expectations are replaced with their sample analogues. 103 | 104 | # In[ ]: 105 | 106 | 107 | # Moment conditions 108 | X = sm.add_constant(factors) 109 | p = vstack((alpha, beta)) 110 | epsilon = excess_returns - x @ p 111 | moments1 = kron(epsilon, ones((1, k + 1))) 112 | moments1 = multiply(moments1, kron(ones((1, n)), x)) 113 | u = excess_returns - risk_premia[None,:] @ beta 114 | moments2 = u * beta.T 115 | # Score covariance 116 | S = mat(cov(hstack((moments1, moments2)).T)) 117 | # Jacobian 118 | G = mat(zeros((n * k + n + k, n * k + n + k))) 119 | sigma_x = (x.T @ x) / t 120 | G[:n * k + n, :n * k + n] = kron(eye(n), sigma_x) 121 | G[n * k + n:, n * k + n:] = -beta @ beta.T 122 | for i in range(n): 123 | temp = zeros((k, k + 1)) 124 | values = mean(u[:, i]) - multiply(beta[:, i], risk_premia) 125 | temp[:, 1:] = diag(values) 126 | G[n * k + n:, i * (k + 1):(i + 1) * (k + 1)] = temp 127 | 128 | vcv = inv(G.T) * S * inv(G) / t 129 | 130 | 131 | # The $J$-test examines whether the average pricing errors, $\hat{\alpha}$, are zero. The $J$ statistic has an asymptotic $\chi_{N}^{2}$ distribution, and the model is badly rejected. 132 | 133 | # In[ ]: 134 | 135 | 136 | vcv_alpha = vcv[0:n * k + n:4, 0:n * k + n:4] 137 | J = alpha @ inv(vcv_alpha) @ alpha.T 138 | J = J[0, 0] 139 | Jpval = 1 - chi2(25).cdf(J) 140 | 141 | 142 | # The final block using formatted output to present all of the results in a readable manner. 143 | 144 | # In[ ]: 145 | 146 | 147 | vcvrisk_premia = vcv[n * k + n:, n * k + n:] 148 | annualized_rp = 12 * risk_premia 149 | arp = list(squeeze(annualized_rp)) 150 | arp_se = list(sqrt(12 * diag(vcvrisk_premia))) 151 | print(' Annualized Risk Premia') 152 | print(' Market SMB HML') 153 | print('--------------------------------------') 154 | print(f'Premia {arp[0]:0.4f} {arp[1]:0.4f} {arp[2]:0.4f}') 155 | print(f'Std. Err. {arp_se[0]:0.4f} {arp_se[1]:0.4f} {arp_se[2]:0.4f}') 156 | print('\n\n') 157 | 158 | print(f'J-test: {J:0.4f}') 159 | print(f'P-value: {Jpval:0.4f}') 160 | 161 | i = 0 162 | beta_se = [] 163 | for j in range(5): 164 | for m in range(5): 165 | a = alpha[i] 166 | b = beta[:, i] 167 | variances = diag(vcv[(k + 1) * i:(k + 1) * (i + 1), (k + 1) * i:(k + 1) * (i + 1)]) 168 | beta_se.append(sqrt(variances)) 169 | s = sqrt(variances) 170 | c = hstack((a, b)) 171 | t = c / s 172 | print(f'Size: {j+1}, Value:{m+1} Alpha Beta(VWM) Beta(SMB) Beta(HML)') 173 | print(f'Coefficients: {a:>10,.4f} {b[0]:>10,.4f} {b[1]:>10,.4f} {b[2]:>10,.4f}') 174 | print(f'Std Err. {s[0]:>10,.4f} {s[1]:>10,.4f} {s[2]:>10,.4f} {s[3]:>10,.4f}') 175 | print(f'T-stat {t[0]:>10,.4f} {t[1]:>10,.4f} {t[2]:>10,.4f} {t[3]:>10,.4f}') 176 | print('') 177 | i += 1 178 | 179 | 180 | # The final block converts the standard errors of $\beta$ to be an array and saves the results. 181 | 182 | # In[ ]: 183 | 184 | 185 | beta_se = array(beta_se) 186 | savez_compressed('fama-macbeth-results', alpha=alpha, beta=beta, 187 | beta_se=beta_se, arp_se=arp_se, arp=arp, J=J, Jpval=Jpval) 188 | 189 | 190 | # ## Save Results 191 | # 192 | # Save the estimated values for use in the $\LaTeX$ notebook. 193 | 194 | # In[ ]: 195 | 196 | 197 | from numpy import savez 198 | savez('fama-macBeth-results.npz', arp=arp, beta=beta, arp_se=arp_se, 199 | beta_se=beta_se, J=J, Jpval=Jpval) 200 | 201 | -------------------------------------------------------------------------------- /examples/example-gjr-garch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### IPython Notebook Setup 5 | # 6 | # This commands are used needed for plots to appear in the notebook. 7 | # 8 | 9 | # # Estimating the Parameters of a GJR-GARCH Model 10 | # 11 | # This example will highlight the steps needed to estimate the parameters of a GJR-GARCH(1,1,1) model with a constant mean. The volatility dynamics in a GJR-GARCH model are given by 12 | # $$\sigma_{t}^{2}=\omega+\sum_{i=1}^{p}\alpha_{i}\epsilon_{t-i}^{2}+\sum_{j=1}^{o}\gamma_{j}r_{t-j}^{2}I_{\left[\epsilon_{t-j}<0\right]}+\sum_{k=1}^{q}\beta_{k}\sigma_{t-k}^{2}.$$ 13 | # 14 | # Returns are assumed to be conditionally normal, $r_{t}|\mathcal{F}_{t-1}\sim N\left(\mu,\sigma_{t}^{2}\right)$, $\epsilon_{t}=r_{t}-\mu$ and parameters are estimated by maximum likelihood. To estimate the parameters, it is necessary to: 15 | # 16 | # 1. Produce some starting values 17 | # 2. Estimate the parameters using (quasi-) maximum likelihood 18 | # 3. Compute standard errors using a “sandwich” covariance estimator (also known as the [BollerslevWooldridge::1992] covariance estimator) 19 | # 20 | # The first task is to write the log-likelihood which can be used in an optimizer. The log-likelihood function will compute the volatility recursion and the log-likelihood. It will also, optionally, return the $T$ by 1 vector of individual log-likelihoods which are useful when approximating the scores. 21 | 22 | # In[ ]: 23 | 24 | 25 | import matplotlib.pyplot as plt 26 | import numpy as np 27 | import pandas as pd 28 | from numpy import size, log, pi, sum, array, zeros, diag, mat, asarray, sqrt, copy 29 | from numpy.linalg import inv 30 | from scipy.optimize import minimize 31 | 32 | 33 | # The conditional log-likelihood of a normal random variable is 34 | # 35 | # $$\ln f\left(r_{t}|\mu,\sigma_{t}^{2}\right)=-\frac{1}{2}\left(\ln2\pi+\ln\sigma_{t}^{2}+\frac{\left(r_{t}-\mu\right)^{2}}{\sigma_{t}^{2}}\right),$$ 36 | # 37 | # which is negated in the code since the optimizers all minimize. 38 | 39 | # In[ ]: 40 | 41 | 42 | def gjr_garch_likelihood(parameters, data, sigma2, out=None): 43 | """Negative log-likelihood for GJR-GARCH(1,1,1) model""" 44 | mu = parameters[0] 45 | omega = parameters[1] 46 | alpha = parameters[2] 47 | gamma = parameters[3] 48 | beta = parameters[4] 49 | 50 | T = size(data,0) 51 | eps = data - mu 52 | # Data and sigma2 are T by 1 vectors 53 | for t in range(1,T): 54 | sigma2[t] = (omega + alpha * eps[t-1]**2 55 | + gamma * eps[t-1]**2 * (eps[t-1]<0) + beta * sigma2[t-1]) 56 | 57 | logliks = 0.5*(log(2*pi) + log(sigma2) + eps**2/sigma2) 58 | loglik = sum(logliks) 59 | 60 | if out is None: 61 | return loglik 62 | else: 63 | return loglik, logliks, copy(sigma2) 64 | 65 | 66 | # The keyword argument `out` has a default value of `None`, and is used to determine whether to return 1 output or 3. This is common practice since the optimizer requires a single output -- the log-likelihood function value, but it is also useful to be able to output other useful quantities, such as $\left\{ \sigma_{t}^{2}\right\}$. 67 | # 68 | # The optimization is constrained so that $\alpha+\gamma/2+\beta\leq 1$, and the constraint is provided in a separate function. 69 | 70 | # In[ ]: 71 | 72 | 73 | def gjr_constraint(parameters): 74 | """ Constraint that alpha+gamma/2+beta<=1""" 75 | 76 | alpha = parameters[2] 77 | gamma = parameters[3] 78 | beta = parameters[4] 79 | 80 | return array([1-alpha-gamma/2-beta]) 81 | 82 | constraint = {"type": "ineq", "fun": gjr_constraint} 83 | 84 | 85 | # Note that the constraint function takes the same inputs as the negative of the log-likelihood function, even though only parameters is required to compute the constraint. 86 | # 87 | # It is necessary to discuss one other function before proceeding with the main block of code. The asymptotic variance is estimated using the “sandwich” form which is commonly expressed as 88 | # 89 | # $$\mathcal{J}^{-1}\mathcal{I}\mathcal{J}^{-1}$$ 90 | # 91 | # where $\mathcal{J}$ is the expected Hessian and $\mathcal{I}$ is the covariance of the scores. Both are numerically approximated, and the strategy for computing the Hessian is to use the definition that 92 | # 93 | # $$\mathcal{J}_{ij}\approx\frac{f\left(\theta+e_{i}h_{i}+e_{j}h_{j}\right)-f\left(\theta+e_{i}h_{i}\right)-f\left(\theta+e_{j}h_{j}\right)+f\left(\theta\right)}{h_{i}h_{j}}$$ 94 | # 95 | # where $h_{i}$ is a scalar “step size” and $e_{i}$ is a vector of 0s except for element $i$, which is 1. A 2-sided version of this approximation, which takes both forward and backward steps and then averages, is below. For more on numerical derivatives, see [FlanneryPressTeukolskyTeukolsky::1992]. 96 | 97 | # In[ ]: 98 | 99 | 100 | def hessian_2sided(fun, theta, args): 101 | f = fun(theta, *args) 102 | h = 1e-5*np.abs(theta) 103 | thetah = theta + h 104 | h = thetah - theta 105 | K = size(theta,0) 106 | h = np.diag(h) 107 | 108 | fp = zeros(K) 109 | fm = zeros(K) 110 | for i in range(K): 111 | fp[i] = fun(theta+h[i], *args) 112 | fm[i] = fun(theta-h[i], *args) 113 | 114 | fpp = zeros((K,K)) 115 | fmm = zeros((K,K)) 116 | for i in range(K): 117 | for j in range(i,K): 118 | fpp[i,j] = fun(theta + h[i] + h[j], *args) 119 | fpp[j,i] = fpp[i,j] 120 | fmm[i,j] = fun(theta - h[i] - h[j], *args) 121 | fmm[j,i] = fmm[i,j] 122 | 123 | hh = (diag(h)) 124 | hh = hh.reshape((K,1)) 125 | hh = hh @ hh.T 126 | 127 | H = zeros((K,K)) 128 | for i in range(K): 129 | for j in range(i,K): 130 | H[i,j] = (fpp[i,j] - fp[i] - fp[j] + f 131 | + f - fm[i] - fm[j] + fmm[i,j])/hh[i,j]/2 132 | H[j,i] = H[i,j] 133 | 134 | return H 135 | 136 | 137 | # Finally, the code that does the actual work can be written. The first block imports the data, flips it using a slicing operator, and computes 100 times returns. Scaling data can be useful to improve optimizer performance, and ideally estimated parameters should have similar magnitudes (i.e. $\omega\approx.01$ and $\alpha\approx.05$). 138 | 139 | # In[ ]: 140 | 141 | 142 | # Import data 143 | ftse = pd.read_csv('FTSE_1984_2012.csv', parse_dates=[0]) 144 | # Set index 145 | ftse.index = ftse.pop('Date') 146 | # Flip upside down 147 | ftse = ftse.iloc[::-1] 148 | # Compute returns 149 | ftse_price = ftse['Adj Close'] 150 | ftse_return = 100 * ftse_price.pct_change().dropna() 151 | 152 | 153 | # Good starting values are important. These are my guesses based on experience fitting these types of models models. An alternative is to attempt a crude grid search and use the best (smallest) log-likelihood value from the grid search. 154 | 155 | # In[ ]: 156 | 157 | 158 | # Starting values 159 | starting_vals = array([ftse_return.mean(), 160 | ftse_return.var() * .01, 161 | .03, .09, .90]) 162 | 163 | 164 | # Bounds are used in estimation to ensure that all parameters in the conditional variance are $\geq 0$ and to set sensible upper bounds on the mean and $\omega$. The vector `sigma2` is then initialized, and the arguments are placed in a tuple. 165 | 166 | # In[ ]: 167 | 168 | 169 | # Estimate parameters 170 | finfo = np.finfo(np.float64) 171 | bounds = [(-10*ftse_return.mean(), 10*ftse_return.mean()), 172 | (finfo.eps, 2*ftse_return.var() ), 173 | (0.0,1.0), (0.0,1.0), (0.0,1.0)] 174 | 175 | T = ftse_return.shape[0] 176 | sigma2 = np.ones(T) * ftse_return.var() 177 | # Pass a NumPy array, not a pandas Series 178 | args = (np.asarray(ftse_return), sigma2) 179 | opt = minimize(gjr_garch_likelihood, 180 | starting_vals, 181 | constraints=constraint, 182 | bounds = bounds, 183 | args = args) 184 | estimates = opt.x 185 | 186 | 187 | # The optimized log-likelihood and the time series of variances are computed by calling the objective using the keyword argument `out=True`. 188 | 189 | # In[ ]: 190 | 191 | 192 | loglik, logliks, sigma2final = gjr_garch_likelihood(estimates, ftse_return, 193 | sigma2, out=True) 194 | 195 | 196 | # Next, the numerical scores and the covariance of the scores are computed. These exploit the definition of a derivative, so that for a scalar function, 197 | # 198 | # $$\frac{\partial f\left(\theta\right)}{\partial\theta_{i}}\approx\frac{f\left(\theta+e_{i}h_{i}\right)-f\left(\theta\right)}{h_{i}}.$$ 199 | # 200 | # The covariance is computed as the outer product of the scores since the scores should have mean 0 when evaluated at the solution to the optimization problem. 201 | 202 | # In[ ]: 203 | 204 | 205 | step = 1e-5 * estimates 206 | scores = zeros((T,5)) 207 | for i in range(5): 208 | h = step[i] 209 | delta = np.zeros(5) 210 | delta[i] = h 211 | 212 | loglik, logliksplus, sigma2 = gjr_garch_likelihood(estimates + delta, np.asarray(ftse_return), sigma2, out=True) 213 | loglik, logliksminus, sigma2 = gjr_garch_likelihood(estimates - delta, np.asarray(ftse_return), sigma2, out=True) 214 | 215 | scores[:,i] = (logliksplus - logliksminus)/(2*h) 216 | 217 | I = (scores.T @ scores)/T 218 | 219 | 220 | # The next block calls `hessian_2sided` to estimate the Hessian, and then computes the asymptotic covariance. 221 | 222 | # In[ ]: 223 | 224 | 225 | J = hessian_2sided(gjr_garch_likelihood, estimates, args) 226 | J = J/T 227 | Jinv = mat(inv(J)) 228 | vcv = Jinv*mat(I)*Jinv/T 229 | vcv = asarray(vcv) 230 | 231 | 232 | # The penultimate step is to pretty print the results and to produce a plot of the conditional variances. 233 | 234 | # In[ ]: 235 | 236 | 237 | output = np.vstack((estimates,sqrt(diag(vcv)),estimates/sqrt(diag(vcv)))).T 238 | print('Parameter Estimate Std. Err. T-stat') 239 | param = ['mu','omega','alpha','gamma','beta'] 240 | for i in range(len(param)): 241 | print(f'{param[i]:<11} {output[i,0]:>0.6f} {output[i,1]:0.6f} {output[i,2]: 0.5f}') 242 | 243 | 244 | # This final block produces a plot of the annualized conditional standard deviations. 245 | 246 | # In[ ]: 247 | 248 | 249 | # Register date converters 250 | from pandas.plotting import register_matplotlib_converters 251 | register_matplotlib_converters() 252 | 253 | # Produce a plot 254 | dates = ftse.index[1:] 255 | fig = plt.figure() 256 | ax = fig.add_subplot(111) 257 | volatility = pd.DataFrame(np.sqrt(252 * sigma2), index=dates) 258 | ax.plot(volatility) 259 | ax.autoscale(tight='x') 260 | fig.autofmt_xdate() 261 | fig.tight_layout(pad=1.5) 262 | ax.set_ylabel('Volatility') 263 | ax.set_title('FTSE Annualized Volatility (GJR GARCH(1,1,1))') 264 | plt.show() 265 | 266 | -------------------------------------------------------------------------------- /examples/example-gmm.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # # Risk Premia Estimation using GMM 5 | 6 | # Start by importing the modules and functions needed 7 | 8 | # In[ ]: 9 | 10 | 11 | from numpy import hstack, ones, array, mat, tile, reshape, squeeze, eye, asmatrix 12 | from numpy.linalg import inv 13 | from pandas import read_csv, Series 14 | from scipy.linalg import kron 15 | from scipy.optimize import minimize 16 | import numpy as np 17 | import statsmodels.api as sm 18 | 19 | 20 | # Next a callable function is used to produce iteration-by-iteration output when using the non-linear optimizer. 21 | 22 | # In[ ]: 23 | 24 | 25 | iteration = 0 26 | last_value = 0 27 | function_count = 0 28 | 29 | def iter_print(params): 30 | global iteration, last_value, function_count 31 | iteration += 1 32 | print(f'Func value: {last_value:6.6g}, Iteration: {iteration}, Function Count: {function_count}') 33 | 34 | 35 | # The GMM objective, which is minimized, is defined next. 36 | 37 | # In[ ]: 38 | 39 | 40 | def gmm_objective(params, p_rets, f_rets, Winv, out=False): 41 | global last_value, function_count 42 | t,n = p_rets.shape 43 | t,k = f_rets.shape 44 | beta = squeeze(array(params[:(n*k)])) 45 | lam = squeeze(array(params[(n*k):])) 46 | beta = reshape(beta,(n,k)) 47 | lam = reshape(lam,(k,1)) 48 | betalam = beta @ lam 49 | expected_ret = f_rets @ beta.T 50 | e = p_rets - expected_ret 51 | instr = tile(f_rets,n) 52 | moments1 = kron(e,ones((1,k))) 53 | moments1 = moments1 * instr 54 | moments2 = p_rets - betalam.T 55 | moments = hstack((moments1,moments2)) 56 | 57 | avg_moment = moments.mean(axis=0) 58 | 59 | J = t * mat(avg_moment) * mat(Winv) * mat(avg_moment).T 60 | J = J[0,0] 61 | last_value = J 62 | function_count += 1 63 | if not out: 64 | return J 65 | else: 66 | return J, moments 67 | 68 | 69 | # The `G` matrix, which is the derivative of the GMM moments with respect to the parameters, is defined. 70 | 71 | # In[ ]: 72 | 73 | 74 | def gmm_G(params, p_rets, f_rets): 75 | t,n = p_rets.shape 76 | t,k = f_rets.shape 77 | beta = squeeze(array(params[:(n*k)])) 78 | lam = squeeze(array(params[(n*k):])) 79 | beta = reshape(beta,(n,k)) 80 | lam = reshape(lam,(k,1)) 81 | G = np.zeros((n*k+k,n*k+n)) 82 | ffp = (f_rets.T @ f_rets) / t 83 | G[:(n*k),:(n*k)]=kron(eye(n),ffp) 84 | G[:(n*k),(n*k):] = kron(eye(n),-lam) 85 | G[(n*k):,(n*k):] = -beta.T 86 | 87 | return G 88 | 89 | 90 | # Next, the data is imported and a subset of the test portfolios is selected to make the estimation faster. 91 | 92 | # In[ ]: 93 | 94 | 95 | data = read_csv('FamaFrench.csv') 96 | 97 | # Split using both named colums and ix for larger blocks 98 | dates = data['date'].values 99 | factors = data[['VWMe','SMB','HML']].values 100 | riskfree = data['RF'].values 101 | portfolios = data.iloc[:,5:].values 102 | 103 | t,n = portfolios.shape 104 | portfolios = portfolios[:,np.arange(0,n,2)] 105 | t,n = portfolios.shape 106 | excess_ret = portfolios - np.reshape(riskfree,(t,1)) 107 | k = np.size(factors,1) 108 | 109 | 110 | # Starting values for the factor loadings and rick premia are estimated using OLS and simple means. 111 | 112 | # In[ ]: 113 | 114 | 115 | betas = [] 116 | for i in range(n): 117 | res = sm.OLS(excess_ret[:,i],sm.add_constant(factors)).fit() 118 | betas.append(res.params[1:]) 119 | 120 | avg_return = excess_ret.mean(axis=0) 121 | avg_return.shape = n,1 122 | betas = array(betas) 123 | res = sm.OLS(avg_return, betas).fit() 124 | risk_premia = res.params 125 | 126 | 127 | # The starting values are computed the first step estimates are found using the non-linear optimizer. The initial weighting matrix is just the identify matrix. 128 | 129 | # In[ ]: 130 | 131 | 132 | risk_premia.shape = 3 133 | starting_vals = np.concatenate((betas.flatten(),risk_premia)) 134 | 135 | Winv = np.eye(n*(k+1)) 136 | args = (excess_ret, factors, Winv) 137 | iteration = 0 138 | function_count = 0 139 | opt = minimize(gmm_objective, starting_vals, args=args, callback=iter_print) 140 | step1opt = opt.x 141 | 142 | 143 | # Here we look at the risk premia estimates from the first step (inefficient) estimates. 144 | 145 | # In[ ]: 146 | 147 | 148 | premia = step1opt[-3:] 149 | premia = Series(premia,index=['VWMe', 'SMB', 'HML']) 150 | print('Annualized Risk Premia (First step)') 151 | print(12 * premia) 152 | 153 | 154 | # Next the first step estimates are used to estimate the moment conditions which are in-turn used to estimate the optimal weighting matrix for the moment conditions. This is then used as an input for the 2nd-step estimates. 155 | 156 | # In[ ]: 157 | 158 | 159 | out = gmm_objective(step1opt, excess_ret, factors, Winv, out=True) 160 | S = np.cov(out[1].T) 161 | Winv2 = inv(S) 162 | args = (excess_ret, factors, Winv2) 163 | 164 | iteration = 0 165 | function_count = 0 166 | opt = minimize(gmm_objective, step1opt, args=args, callback=iter_print) 167 | step2opt = opt.x 168 | 169 | 170 | # Finally the VCV of the parameter estimates is computed. 171 | 172 | # In[ ]: 173 | 174 | 175 | out = gmm_objective(step2opt, excess_ret, factors, Winv2, out=True) 176 | G = gmm_G(step2opt, excess_ret, factors) 177 | S = np.cov(out[1].T) 178 | vcv = inv(G @ inv(S) @ G.T)/t 179 | 180 | 181 | # The annualized risk premia and their associated t-stats. 182 | 183 | # In[ ]: 184 | 185 | 186 | premia = step2opt[-3:] 187 | premia = Series(premia,index=['VWMe', 'SMB', 'HML']) 188 | premia_vcv = vcv[-3:,-3:] 189 | print('Annualized Risk Premia') 190 | print(12 * premia) 191 | 192 | premia_stderr = np.diag(premia_vcv) 193 | premia_stderr = Series(premia_stderr,index=['VWMe', 'SMB', 'HML']) 194 | print('T-stats') 195 | print(premia / premia_stderr) 196 | 197 | -------------------------------------------------------------------------------- /examples/example-latex-output.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # # Example: Exporting to $\LaTeX$ 5 | # 6 | # The first code block contains the imports needed and defines a flag which determines whether the 7 | # output $\LaTeX$ should be compiled. 8 | 9 | # In[ ]: 10 | 11 | 12 | # imports 13 | import numpy as np 14 | import subprocess 15 | 16 | # Flag to compile output tables 17 | compile_latex = False 18 | 19 | 20 | # The next code block loads the npz file created using the output from the Fama-MacBeth example. 21 | # The second part shows a generic method to restore all variables. The loaded data is in a dictionary, 22 | # and so iterating over the keys and using `globals()` (a dictionary) in the main program. 23 | 24 | # In[ ]: 25 | 26 | 27 | # Load variables 28 | f = np.load('fama-macBeth-results.npz') 29 | data = f.items() 30 | # Manually load parameters and std errors 31 | arp = f['arp'] 32 | arp_se = f['arp_se'] 33 | beta = f['beta'] 34 | beta_se = f['beta_se'] 35 | J = f['J'] 36 | Jpval = f['Jpval'] 37 | 38 | # Generic restore of all data in a npz file 39 | for key in f.keys(): 40 | globals()[key] = f[key] 41 | f.close() 42 | 43 | 44 | # The document is be stored in a list. The first few lines contain the required header for a 45 | # $\LaTeX$ document, including some packages used to improve table display and to select a custom font. 46 | # 47 | 48 | # In[ ]: 49 | 50 | 51 | # List to hold table 52 | latex = [] 53 | # Initializd LaTeX document 54 | latex.append(r'\documentclass[a4paper]{article}') 55 | latex.append(r'\usepackage{amsmath}') 56 | latex.append(r'\usepackage{booktabs}') 57 | latex.append(r'\usepackage[adobe-utopia]{mathdesign}') 58 | latex.append(r'\usepackage[T1]{fontenc}') 59 | latex.append(r'\begin{document}') 60 | 61 | 62 | # Table 1 is stored in its own list, and then extend will be used to add it to the main list. 63 | 64 | # In[ ]: 65 | 66 | 67 | # Table 1 68 | table1 = [] 69 | table1.append(r'\begin{center}') 70 | table1.append(r'\begin{tabular}{lrrr} \toprule') 71 | # Header 72 | colNames = [r'VWM$^e$','SMB','HML'] 73 | header = '' 74 | for cName in colNames: 75 | header += ' & ' + cName 76 | 77 | header += r'\\ \cmidrule{2-4}' 78 | table1.append(header) 79 | # Main row 80 | row = '' 81 | for a,se in zip(arp,arp_se): 82 | row += r' & $\underset{{({0:0.3f})}}{{{1:0.3f}}}$'.format(se,a) 83 | table1.append(row) 84 | # Blank row 85 | row = r'\\' 86 | table1.append(row) 87 | # J-stat row 88 | row = r'J-stat: $\underset{{({0:0.3f})}}{{{1:0.1f}}}$ \\'.format(float(Jpval),float(J)) 89 | table1.append(row) 90 | table1.append(r'\bottomrule \end{tabular}') 91 | table1.append(r'\end{center}') 92 | # Extend latex with table 1 93 | latex.extend(table1) 94 | latex.append(r'\newpage') 95 | 96 | 97 | # Table 2 is a bit more complex, and uses loops to iterate over the rows of the arrays containing 98 | # the $\beta$s and their standard errors. 99 | 100 | # In[ ]: 101 | 102 | 103 | # Format information for table 2 104 | sizes = ['S','2','3','4','B'] 105 | values = ['L','2','3','4','H'] 106 | # Table 2 has the same header as table 1, copy with a slice 107 | table2 = table1[:3] 108 | m = 0 109 | for i in range(len(sizes)): 110 | for j in range(len(values)): 111 | row = 'Size: {:}, Value: {:} '.format(sizes[i],values[j]) 112 | b = beta[:,m] 113 | s = beta_se[m,1:] 114 | for k in range(len(b)): 115 | row += r' & $\underset{{({0:0.3f})}}{{{1: .3f}}}$'.format(s[k],b[k]) 116 | row += r'\\ ' 117 | table2.append(row) 118 | m += 1 119 | if i<(len(sizes)-1): 120 | table2.append(r'\cmidrule{2-4}') 121 | 122 | table2.append(r'\bottomrule \end{tabular}') 123 | table2.append(r'\end{center}') 124 | # Extend with table 2 125 | latex.extend(table2) 126 | 127 | 128 | # The penultimate block finished the document, and uses write to write the lines to the $\LaTeX$ file. 129 | 130 | # In[ ]: 131 | 132 | 133 | # Finish document 134 | latex.append(r'\end{document}') 135 | # Write to table 136 | fid = open('latex.tex','w') 137 | for line in latex: 138 | fid.write(line + '\n') 139 | fid.close() 140 | 141 | 142 | # Finally, if the flag is set, subprocess is used to compile the LaTeX. 143 | # 144 | 145 | # In[ ]: 146 | 147 | 148 | # Compile if needed 149 | if compile_latex: 150 | exit_status = subprocess.call(r'pdflatex latex.tex', shell=True) 151 | else: 152 | print("\n".join(latex)) 153 | 154 | -------------------------------------------------------------------------------- /examples/examples_gjrgarch_lineprofiler.py: -------------------------------------------------------------------------------- 1 | """ 2 | To get line profiling data, first install line-profiler 3 | 4 | pip install line-profiler --upgrade 5 | 6 | Then run the following two commands 7 | 8 | kernprof -l examples_gjrgarch_lineprofiler.py 9 | python -m line_profiler examples_gjrgarch_lineprofiler.py.lprof 10 | """ 11 | 12 | import numpy as np 13 | import matplotlib.pyplot as plt 14 | from numpy import size, log, pi, sum, array, zeros, diag, asarray, sqrt, copy 15 | from numpy.linalg import inv 16 | import pandas as pd 17 | from scipy.optimize import minimize 18 | import builtins 19 | 20 | try: 21 | builtins.profile 22 | print("Running with kernprof") 23 | except AttributeError: 24 | # No line profiler, provide a pass-through version 25 | def profile(func): 26 | return func 27 | 28 | 29 | builtins.profile = profile 30 | print("Running without kernprof") 31 | 32 | 33 | @profile 34 | def gjr_garch_likelihood(parameters, data, sigma2, out=None): 35 | """Negative log-likelihood for GJR-GARCH(1,1,1) model""" 36 | mu = parameters[0] 37 | omega = parameters[1] 38 | alpha = parameters[2] 39 | gamma = parameters[3] 40 | beta = parameters[4] 41 | 42 | T = size(data, 0) 43 | eps = data - mu 44 | # Data and sigma2 are T by 1 vectors 45 | for t in range(1, T): 46 | sigma2[t] = (omega + alpha * eps[t - 1] ** 2 47 | + gamma * eps[t - 1] ** 2 * (eps[t - 1] < 0) + beta * sigma2[t - 1]) 48 | 49 | logliks = 0.5 * (log(2 * pi) + log(sigma2) + eps ** 2 / sigma2) 50 | loglik = sum(logliks) 51 | 52 | if out is None: 53 | return loglik 54 | else: 55 | return loglik, logliks, copy(sigma2) 56 | 57 | 58 | def gjr_constraint(parameters): 59 | """ Constraint that alpha+gamma/2+beta<=1""" 60 | 61 | alpha = parameters[2] 62 | gamma = parameters[3] 63 | beta = parameters[4] 64 | 65 | return array([1 - alpha - gamma / 2 - beta]) 66 | 67 | 68 | constraint = {"type": "ineq", "fun": gjr_constraint} 69 | 70 | 71 | def hessian_2sided(fun, theta, args): 72 | f = fun(theta, *args) 73 | h = 1e-5 * np.abs(theta) 74 | thetah = theta + h 75 | h = thetah - theta 76 | K = size(theta, 0) 77 | h = np.diag(h) 78 | 79 | fp = zeros(K) 80 | fm = zeros(K) 81 | for i in range(K): 82 | fp[i] = fun(theta + h[i], *args) 83 | fm[i] = fun(theta - h[i], *args) 84 | 85 | fpp = zeros((K, K)) 86 | fmm = zeros((K, K)) 87 | for i in range(K): 88 | for j in range(i, K): 89 | fpp[i, j] = fun(theta + h[i] + h[j], *args) 90 | fpp[j, i] = fpp[i, j] 91 | fmm[i, j] = fun(theta - h[i] - h[j], *args) 92 | fmm[j, i] = fmm[i, j] 93 | 94 | hh = (diag(h)) 95 | hh = hh.reshape((K, 1)) 96 | hh = hh @ hh.T 97 | 98 | H = zeros((K, K)) 99 | for i in range(K): 100 | for j in range(i, K): 101 | H[i, j] = (fpp[i, j] - fp[i] - fp[j] + f 102 | + f - fm[i] - fm[j] + fmm[i, j]) / hh[i, j] / 2 103 | H[j, i] = H[i, j] 104 | 105 | return H 106 | 107 | 108 | # Import data 109 | ftse = pd.read_csv('FTSE_1984_2012.csv', parse_dates=[0]) 110 | # Set index 111 | ftse.index = ftse.pop('Date') 112 | # Flip upside down 113 | ftse = ftse.iloc[::-1] 114 | # Compute returns 115 | ftse_price = ftse['Adj Close'] 116 | ftse_return = 100 * ftse_price.pct_change().dropna() 117 | 118 | # Starting values 119 | starting_vals = array([ftse_return.mean(), 120 | ftse_return.var() * .01, 121 | .03, .09, .90]) 122 | 123 | # Estimate parameters 124 | finfo = np.finfo(np.float64) 125 | bounds = [(-10 * ftse_return.mean(), 10 * ftse_return.mean()), 126 | (finfo.eps, 2 * ftse_return.var()), 127 | (0.0, 1.0), (0.0, 1.0), (0.0, 1.0)] 128 | 129 | T = ftse_return.shape[0] 130 | sigma2 = np.ones(T) * ftse_return.var() 131 | # Pass a NumPy array, not a pandas Series 132 | args = (np.asarray(ftse_return), sigma2) 133 | opt = minimize(gjr_garch_likelihood, 134 | starting_vals, 135 | constraints=constraint, 136 | bounds=bounds, 137 | args=args) 138 | estimates = opt.x 139 | 140 | loglik, logliks, sigma2final = gjr_garch_likelihood(estimates, ftse_return, 141 | sigma2, out=True) 142 | 143 | step = 1e-5 * estimates 144 | scores = zeros((T, 5)) 145 | for i in range(5): 146 | h = step[i] 147 | delta = np.zeros(5) 148 | delta[i] = h 149 | 150 | loglik, logliksplus, sigma2 = gjr_garch_likelihood(estimates + delta, np.asarray(ftse_return), 151 | sigma2, out=True) 152 | loglik, logliksminus, sigma2 = gjr_garch_likelihood(estimates - delta, np.asarray(ftse_return), 153 | sigma2, out=True) 154 | 155 | scores[:, i] = (logliksplus - logliksminus) / (2 * h) 156 | 157 | I = (scores.T @ scores) / T 158 | 159 | J = hessian_2sided(gjr_garch_likelihood, estimates, args) 160 | J = J / T 161 | Jinv = inv(J) 162 | vcv = Jinv @ I @ Jinv / T 163 | vcv = asarray(vcv) 164 | 165 | 166 | output = np.vstack((estimates, sqrt(diag(vcv)), estimates / sqrt(diag(vcv)))).T 167 | print('Parameter Estimate Std. Err. T-stat') 168 | param = ['mu', 'omega', 'alpha', 'gamma', 'beta'] 169 | for i in range(len(param)): 170 | print( 171 | f'{param[i]:<11} {output[i, 0]:>0.6f} {output[i, 1]:0.6f} {output[i, 2]: 0.5f}') 172 | 173 | # Register date converters 174 | from pandas.plotting import register_matplotlib_converters 175 | 176 | register_matplotlib_converters() 177 | 178 | # Produce a plot 179 | dates = ftse.index[1:] 180 | fig = plt.figure() 181 | ax = fig.add_subplot(111) 182 | volatility = pd.DataFrame(np.sqrt(252 * sigma2), index=dates) 183 | ax.plot(volatility) 184 | ax.autoscale(tight='x') 185 | fig.autofmt_xdate() 186 | fig.tight_layout(pad=1.5) 187 | ax.set_ylabel('Volatility') 188 | ax.set_title('FTSE Annualized Volatility (GJR GARCH(1,1,1))') 189 | plt.show() 190 | -------------------------------------------------------------------------------- /examples/examples_gjrgarch_lineprofiler.py.prof: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/examples/examples_gjrgarch_lineprofiler.py.prof -------------------------------------------------------------------------------- /examples/latex.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/examples/latex.pdf -------------------------------------------------------------------------------- /examples/latex.tex: -------------------------------------------------------------------------------- 1 | \documentclass[a4paper]{article} 2 | \usepackage{amsmath} 3 | \usepackage{booktabs} 4 | \usepackage[adobe-utopia]{mathdesign} 5 | \usepackage[T1]{fontenc} 6 | \begin{document} 7 | \begin{center} 8 | \begin{tabular}{lrrr} \toprule 9 | & VWM$^e$ & SMB & HML\\ \cmidrule{2-4} 10 | & $\underset{(0.599)}{6.664}$ & $\underset{(0.401)}{2.873}$ & $\underset{(0.430)}{2.808}$ 11 | \\ 12 | J-stat: $\underset{(0.000)}{95.3}$ \\ 13 | \bottomrule \end{tabular} 14 | \end{center} 15 | \newpage 16 | \begin{center} 17 | \begin{tabular}{lrrr} \toprule 18 | & VWM$^e$ & SMB & HML\\ \cmidrule{2-4} 19 | Size: S, Value: L & $\underset{(0.127)}{ 1.310}$ & $\underset{(0.167)}{ 1.289}$ & $\underset{(0.275)}{ 0.394}$\\ 20 | Size: S, Value: 2 & $\underset{(0.064)}{ 1.085}$ & $\underset{(0.189)}{ 1.610}$ & $\underset{(0.144)}{ 0.332}$\\ 21 | Size: S, Value: 3 & $\underset{(0.042)}{ 1.075}$ & $\underset{(0.094)}{ 1.181}$ & $\underset{(0.072)}{ 0.465}$\\ 22 | Size: S, Value: 4 & $\underset{(0.023)}{ 0.963}$ & $\underset{(0.100)}{ 1.225}$ & $\underset{(0.035)}{ 0.585}$\\ 23 | Size: S, Value: H & $\underset{(0.026)}{ 0.985}$ & $\underset{(0.082)}{ 1.345}$ & $\underset{(0.061)}{ 0.905}$\\ 24 | \cmidrule{2-4} 25 | Size: 2, Value: L & $\underset{(0.032)}{ 1.069}$ & $\underset{(0.061)}{ 1.052}$ & $\underset{(0.059)}{-0.265}$\\ 26 | Size: 2, Value: 2 & $\underset{(0.017)}{ 1.042}$ & $\underset{(0.078)}{ 0.988}$ & $\underset{(0.035)}{ 0.188}$\\ 27 | Size: 2, Value: 3 & $\underset{(0.017)}{ 0.959}$ & $\underset{(0.073)}{ 0.862}$ & $\underset{(0.032)}{ 0.355}$\\ 28 | Size: 2, Value: 4 & $\underset{(0.014)}{ 0.979}$ & $\underset{(0.045)}{ 0.818}$ & $\underset{(0.028)}{ 0.556}$\\ 29 | Size: 2, Value: H & $\underset{(0.018)}{ 1.050}$ & $\underset{(0.028)}{ 0.937}$ & $\underset{(0.026)}{ 0.849}$\\ 30 | \cmidrule{2-4} 31 | Size: 3, Value: L & $\underset{(0.019)}{ 1.142}$ & $\underset{(0.045)}{ 0.788}$ & $\underset{(0.041)}{-0.198}$\\ 32 | Size: 3, Value: 2 & $\underset{(0.018)}{ 1.013}$ & $\underset{(0.034)}{ 0.515}$ & $\underset{(0.033)}{ 0.072}$\\ 33 | Size: 3, Value: 3 & $\underset{(0.027)}{ 1.013}$ & $\underset{(0.032)}{ 0.413}$ & $\underset{(0.032)}{ 0.338}$\\ 34 | Size: 3, Value: 4 & $\underset{(0.014)}{ 0.961}$ & $\underset{(0.048)}{ 0.465}$ & $\underset{(0.030)}{ 0.507}$\\ 35 | Size: 3, Value: H & $\underset{(0.020)}{ 1.145}$ & $\underset{(0.051)}{ 0.497}$ & $\underset{(0.039)}{ 0.914}$\\ 36 | \cmidrule{2-4} 37 | Size: 4, Value: L & $\underset{(0.015)}{ 1.066}$ & $\underset{(0.044)}{ 0.286}$ & $\underset{(0.032)}{-0.369}$\\ 38 | Size: 4, Value: 2 & $\underset{(0.022)}{ 1.031}$ & $\underset{(0.030)}{ 0.243}$ & $\underset{(0.029)}{ 0.133}$\\ 39 | Size: 4, Value: 3 & $\underset{(0.021)}{ 1.010}$ & $\underset{(0.044)}{ 0.221}$ & $\underset{(0.049)}{ 0.298}$\\ 40 | Size: 4, Value: 4 & $\underset{(0.022)}{ 1.044}$ & $\underset{(0.034)}{ 0.202}$ & $\underset{(0.048)}{ 0.586}$\\ 41 | Size: 4, Value: H & $\underset{(0.022)}{ 1.228}$ & $\underset{(0.049)}{ 0.297}$ & $\underset{(0.038)}{ 0.983}$\\ 42 | \cmidrule{2-4} 43 | Size: B, Value: L & $\underset{(0.010)}{ 1.031}$ & $\underset{(0.025)}{-0.151}$ & $\underset{(0.017)}{-0.251}$\\ 44 | Size: B, Value: 2 & $\underset{(0.017)}{ 0.958}$ & $\underset{(0.024)}{-0.189}$ & $\underset{(0.024)}{-0.011}$\\ 45 | Size: B, Value: 3 & $\underset{(0.018)}{ 0.975}$ & $\underset{(0.031)}{-0.217}$ & $\underset{(0.026)}{ 0.313}$\\ 46 | Size: B, Value: 4 & $\underset{(0.023)}{ 1.055}$ & $\underset{(0.030)}{-0.173}$ & $\underset{(0.032)}{ 0.712}$\\ 47 | Size: B, Value: H & $\underset{(0.114)}{ 1.105}$ & $\underset{(0.159)}{ 0.008}$ & $\underset{(0.149)}{ 0.853}$\\ 48 | \bottomrule \end{tabular} 49 | \end{center} 50 | \end{document} 51 | -------------------------------------------------------------------------------- /solutions/chapter02/builtin_datatypes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Built-in Datatypes" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "This first block contains imports used in later exercises" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import copy" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "## Exercise 1\n" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "a = 4\n", 40 | "b = 3.1415\n", 41 | "c = 1.0\n", 42 | "d = 2 + 4j\n", 43 | "e = \"Hello\"\n", 44 | "f = \"World\"" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "## Exercise 2\n", 52 | "\n", 53 | "Note that many solutions make use of f-strings to improve the output.\n", 54 | "See the chapter on strings for more on f-strings. In a nutshell, and f-string\n", 55 | "lets you mix a string with code, where the code is always surrounded by curly\n", 56 | "braces `{`*code*`}`. An f-string is equivalent to adding a string with the\n", 57 | "`str` of an object. For example, the first solution `print(f\"a: {a}\")` is\n", 58 | "the same as `print(\"a: \" + str(a))`." 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": { 65 | "collapsed": false, 66 | "jupyter": { 67 | "outputs_hidden": false 68 | } 69 | }, 70 | "outputs": [], 71 | "source": [ 72 | "print(f\"a: {a}\")\n", 73 | "print(f\"type(a): {type(a)}\")\n", 74 | "\n", 75 | "print(f\"a: {b}\")\n", 76 | "print(f\"type(a): {type(b)}\")\n", 77 | "\n", 78 | "print(f\"a: {c}\")\n", 79 | "print(f\"type(a): {type(c)}\")\n", 80 | "\n", 81 | "print(f\"a: {d}\")\n", 82 | "print(f\"type(a): {type(d)}\")\n", 83 | "\n", 84 | "print(f\"a: {e}\")\n", 85 | "print(f\"type(a): {type(e)}\")\n", 86 | "\n", 87 | "print(f\"a: {f}\")\n", 88 | "print(f\"type(a): {type(f)}\")" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "## Exercise 3" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": { 102 | "collapsed": false, 103 | "jupyter": { 104 | "outputs_hidden": false 105 | } 106 | }, 107 | "outputs": [], 108 | "source": [ 109 | "# Added\n", 110 | "print(f\"a + b: {a + b}\")\n", 111 | "print(f\"a + c: {a + c}\")\n", 112 | "print(f\"a + d: {a + d}\")\n", 113 | "print(f\"b + c: {b + c}\")\n", 114 | "print(f\"b + d: {b + d}\")\n", 115 | "print(f\"c + d: {c + d}\")\n", 116 | "print(f\"e + f: {e + f}\")\n", 117 | "# Subtracted - No e - f\n", 118 | "print(f\"a - b: {a - b}\")\n", 119 | "print(f\"a - c: {a - c}\")\n", 120 | "print(f\"a - d: {a - d}\")\n", 121 | "print(f\"b - c: {b - c}\")\n", 122 | "print(f\"b - d: {b - d}\")\n", 123 | "print(f\"c - d: {c - d}\")\n", 124 | "# Multiplied - Note e *a, f * a\n", 125 | "print(f\"a * b: {a * b}\")\n", 126 | "print(f\"a * c: {a * c}\")\n", 127 | "print(f\"a * d: {a * d}\")\n", 128 | "print(f\"b * c: {b * c}\")\n", 129 | "print(f\"b * d: {b * d}\")\n", 130 | "print(f\"c * d: {c * d}\")\n", 131 | "print(f\"e * a: {e * a}\")\n", 132 | "print(f\"f * a: {f * a}\")\n", 133 | "# Divided\n", 134 | "print(f\"a / b: {a / b}\")\n", 135 | "print(f\"a / c: {a / c}\")\n", 136 | "print(f\"a / d: {a / d}\")\n", 137 | "print(f\"b / c: {b / c}\")\n", 138 | "print(f\"b / d: {b / d}\")\n", 139 | "print(f\"c / d: {c / d}\")" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "## Exercise 4" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": { 153 | "collapsed": false, 154 | "jupyter": { 155 | "outputs_hidden": false 156 | } 157 | }, 158 | "outputs": [], 159 | "source": [ 160 | "print(f\"type(a + b): {type(a + b)}\")\n", 161 | "print(f\"type(a + c): {type(a + c)}\")\n", 162 | "print(f\"type(a + d): {type(a + d)}\")\n", 163 | "print(f\"type(b + c): {type(b + c)}\")\n", 164 | "print(f\"type(b + d): {type(b + d)}\")\n", 165 | "print(f\"type(c + d): {type(c + d)}\")\n", 166 | "print(f\"type(e + f): {type(e + f)}\")\n", 167 | "# Subtracted - No e - f\n", 168 | "print(f\"type(a - b): {type(a - b)}\")\n", 169 | "print(f\"type(a - c): {type(a - c)}\")\n", 170 | "print(f\"type(a - d): {type(a - d)}\")\n", 171 | "print(f\"type(b - c): {type(b - c)}\")\n", 172 | "print(f\"type(b - d): {type(b - d)}\")\n", 173 | "print(f\"type(c - d): {type(c - d)}\")\n", 174 | "# Multiplied - Note e *a, f * a\n", 175 | "print(f\"type(a * b): {type(a * b)}\")\n", 176 | "print(f\"type(a * c): {type(a * c)}\")\n", 177 | "print(f\"type(a * d): {type(a * d)}\")\n", 178 | "print(f\"type(b * c): {type(b * c)}\")\n", 179 | "print(f\"type(b * d): {type(b * d)}\")\n", 180 | "print(f\"type(c * d): {type(c * d)}\")\n", 181 | "print(f\"type(e * a): {type(e * a)}\")\n", 182 | "print(f\"type(f * a): {type(f * a)}\")\n", 183 | "# Divided\n", 184 | "print(f\"type(a / b): {type(a / b)}\")\n", 185 | "print(f\"type(a / c): {type(a / c)}\")\n", 186 | "print(f\"type(a / d): {type(a / d)}\")\n", 187 | "print(f\"type(b / c): {type(b / c)}\")\n", 188 | "print(f\"type(b / d): {type(b / d)}\")\n", 189 | "print(f\"type(c / d): {type(c / d)}\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "## Exercise 5" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": { 203 | "collapsed": false, 204 | "jupyter": { 205 | "outputs_hidden": false 206 | } 207 | }, 208 | "outputs": [], 209 | "source": [ 210 | "ex = \"Python is an interesting and useful language for numerical computing!\"\n", 211 | "print(f\"ex[:6]: {ex[:6]}\")\n", 212 | "print(f\"ex[0:6]: {ex[0:6]}\")\n", 213 | "print(f\"ex[:-63]: {ex[:-63]}\")\n", 214 | "print(f\"ex[-69:-63]: {ex[-69:-63]}\")\n", 215 | "print(f\"ex[-1:]: {ex[-1:]}\")\n", 216 | "print(f\"ex[68]: {ex[68]}\")\n", 217 | "print(f\"ex[68:]: {ex[68:]}\")\n", 218 | "print(f\"ex[-10:-1]: {ex[-10:-1]}\")\n", 219 | "print(f\"ex[59:68]: {ex[59:68]}\")\n", 220 | "print(f\"ex[21:23]: {ex[21:23]}\")\n", 221 | "print(f\"ex[-48:-46]: {ex[-48:-46]}\")\n", 222 | "print(f\"ex[::-1]: {ex[::-1]}\")\n", 223 | "print(f\"ex[68::-1]: {ex[68::-1]}\")\n", 224 | "print(f\"ex[5::-1]: {ex[5::-1]}\")" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "## Exercise 6\n" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": { 238 | "collapsed": false, 239 | "jupyter": { 240 | "outputs_hidden": false 241 | } 242 | }, 243 | "outputs": [], 244 | "source": [ 245 | "print(f\"(a,): {(a,)}\")\n", 246 | "print(f\"tuple([a]): {tuple([a])}\")\n", 247 | "print(f\"[a]: {[a]}\")\n", 248 | "print(f\"list((a,)): {list((a,))}\")" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "## Exercise 7\n" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": null, 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [ 264 | "x = [[1, 0.5], [0.5, 1]]" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": {}, 270 | "source": [ 271 | "## Exercise 8\n" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": { 278 | "collapsed": false, 279 | "jupyter": { 280 | "outputs_hidden": false 281 | } 282 | }, 283 | "outputs": [], 284 | "source": [ 285 | "y = x\n", 286 | "print(f\"y: {y}\")\n", 287 | "print(f\"x: {x}\")\n", 288 | "y[0][0] = 1.61\n", 289 | "print(f\"y: {y}\")\n", 290 | "print(f\"x: {x}\")" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "## Exercise 9\n" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": null, 303 | "metadata": { 304 | "collapsed": false, 305 | "jupyter": { 306 | "outputs_hidden": false 307 | } 308 | }, 309 | "outputs": [], 310 | "source": [ 311 | "z = x[:]\n", 312 | "y[0][0] = 1j\n", 313 | "print(f\"x: {x}\")\n", 314 | "print(f\"y: {y}\")\n", 315 | "print(f\"z: {z}\")" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "## Exercise 10\n" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": { 329 | "collapsed": false, 330 | "jupyter": { 331 | "outputs_hidden": false 332 | } 333 | }, 334 | "outputs": [], 335 | "source": [ 336 | "w = copy.deepcopy(x)\n", 337 | "w[0][0] = -1\n", 338 | "print(f\"w: {w}\")\n", 339 | "print(f\"x: {x}\")" 340 | ] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "metadata": {}, 345 | "source": [ 346 | "## Exercise 11\n" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": null, 352 | "metadata": { 353 | "collapsed": false, 354 | "jupyter": { 355 | "outputs_hidden": false 356 | } 357 | }, 358 | "outputs": [], 359 | "source": [ 360 | "x = [4, 3.1415, 1.0, 2 + 4j, \"Hello\", \"World\"]\n", 361 | "# Copy since single level and immutable data\n", 362 | "y = x[:]\n", 363 | "print(f\"y: {y}\")\n", 364 | "print(f\"y.pop(2): {y.pop(2)} \")\n", 365 | "print(f\"y: {y}\")\n", 366 | "y = x[:]\n", 367 | "del y[2] # NOPRINT\n", 368 | "print(f\"y: {y}\")\n", 369 | "y = x[:]\n", 370 | "print(f\"y.remove(1.0): {y.remove(1.0)}\")\n", 371 | "print(f\"y: {y}\")\n", 372 | "print(f\"x.extend([1.0, 2+4j, 'Hello']): {x.extend([1.0, 2+4j, 'Hello'])}\")\n", 373 | "print(f\"x: {x}\")\n", 374 | "# Makes a copy\n", 375 | "print(f\"x[::-1]: {x[::-1]}\")\n", 376 | "# Inplace\n", 377 | "print(f\"x.reverse(): {x.reverse()}\")\n", 378 | "print(f\"x.count('Hello'): {x.count('Hello')}\")" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": {}, 384 | "source": [ 385 | "## Exercise 12\n" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": null, 391 | "metadata": { 392 | "collapsed": false, 393 | "jupyter": { 394 | "outputs_hidden": false 395 | } 396 | }, 397 | "outputs": [], 398 | "source": [ 399 | "x = {\"alpha\": 1.0, \"beta\": 3.1415, \"gamma\": -99}\n", 400 | "print(f\"x: {x}\")\n", 401 | "print(f\"x['alpha']: {x['alpha']}\")\n", 402 | "\n", 403 | "x = [4, 3.1415, 1.0, 2 + 4j, \"Hello\", \"World\"]\n", 404 | "print(f\"x: {x}\")\n", 405 | "x.extend([1.0, 2 + 4j, \"Hello\"])\n", 406 | "print(f\"After running x.extend([1.0, 2+4j, 'Hello']): {x}\")\n", 407 | "# Set removes duplicates\n", 408 | "x = set(x)\n", 409 | "print(f\"Using set(x) to remove deplicates: {set(x)}\")" 410 | ] 411 | } 412 | ], 413 | "metadata": { 414 | "kernelspec": { 415 | "display_name": "Python 3 (ipykernel)", 416 | "language": "python", 417 | "name": "python3" 418 | }, 419 | "language_info": { 420 | "codemirror_mode": { 421 | "name": "ipython", 422 | "version": 3 423 | }, 424 | "file_extension": ".py", 425 | "mimetype": "text/x-python", 426 | "name": "python", 427 | "nbconvert_exporter": "python", 428 | "pygments_lexer": "ipython3", 429 | "version": "3.9.6" 430 | } 431 | }, 432 | "nbformat": 4, 433 | "nbformat_minor": 4 434 | } 435 | -------------------------------------------------------------------------------- /solutions/chapter03/arrays_and_matrices.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "source": [ 6 | "# Arrays and Matrices" 7 | ], 8 | "metadata": {} 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "source": [ 13 | "## imports" 14 | ], 15 | "metadata": {} 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "source": [ 21 | "import numpy as np\r\n", 22 | "from numpy import array, matrix, shape" 23 | ], 24 | "outputs": [], 25 | "metadata": {} 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "source": [ 30 | "## Exercise 1" 31 | ], 32 | "metadata": {} 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "source": [ 38 | "u = array([1.0, 1, 2, 3, 5, 8])\r\n", 39 | "u" 40 | ], 41 | "outputs": [], 42 | "metadata": {} 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "source": [ 48 | "v = array([[1.0], [1], [2], [3], [5], [8]])\r\n", 49 | "v" 50 | ], 51 | "outputs": [], 52 | "metadata": { 53 | "collapsed": false, 54 | "jupyter": { 55 | "outputs_hidden": false 56 | }, 57 | "pycharm": { 58 | "name": "#%%\n" 59 | } 60 | } 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "source": [ 66 | "x = array([[1.0, 0], [0, 1]])\r\n", 67 | "x" 68 | ], 69 | "outputs": [], 70 | "metadata": { 71 | "collapsed": false, 72 | "jupyter": { 73 | "outputs_hidden": false 74 | }, 75 | "pycharm": { 76 | "name": "#%%\n" 77 | } 78 | } 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "source": [ 84 | "y = array([[1.0, 2], [3, 4]])\r\n", 85 | "y" 86 | ], 87 | "outputs": [], 88 | "metadata": { 89 | "collapsed": false, 90 | "jupyter": { 91 | "outputs_hidden": false 92 | }, 93 | "pycharm": { 94 | "name": "#%%\n" 95 | } 96 | } 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "source": [ 102 | "z = array([[1.0, 2, 1, 2], [3, 4, 3, 4], [1, 2, 1, 3]])\r\n", 103 | "z" 104 | ], 105 | "outputs": [], 106 | "metadata": { 107 | "collapsed": false, 108 | "jupyter": { 109 | "outputs_hidden": false 110 | }, 111 | "pycharm": { 112 | "name": "#%%\n" 113 | } 114 | } 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "source": [ 120 | "w = array([[1.0, 0, 1, 0], [0, 1, 0, 1], [1, 2, 1, 2], [3, 4, 3, 4]])\n", 121 | "w" 122 | ], 123 | "outputs": [], 124 | "metadata": { 125 | "collapsed": false, 126 | "jupyter": { 127 | "outputs_hidden": false 128 | }, 129 | "pycharm": { 130 | "name": "#%%\n" 131 | } 132 | } 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "source": [ 137 | "## Exercise 2" 138 | ], 139 | "metadata": {} 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "source": [ 145 | "print(\"w[:2,:2]:\")\n", 146 | "print(w[:2, :2])" 147 | ], 148 | "outputs": [], 149 | "metadata": { 150 | "collapsed": false, 151 | "jupyter": { 152 | "outputs_hidden": false 153 | } 154 | } 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "source": [ 159 | "## Exercise 3" 160 | ], 161 | "metadata": {} 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "source": [ 167 | "print(\"w[:,:2]:\")\n", 168 | "print(w[:, :2])\n", 169 | "print(\"w[:,-2:]:\")\n", 170 | "print(w[:, -2:])" 171 | ], 172 | "outputs": [], 173 | "metadata": { 174 | "collapsed": false, 175 | "jupyter": { 176 | "outputs_hidden": false 177 | } 178 | } 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "source": [ 183 | "## Exercise 4" 184 | ], 185 | "metadata": {} 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "source": [ 191 | "print(\"z[:2,:2]:\")\n", 192 | "print(z[:2, :2])\n", 193 | "print(\"z[:2,2:]:\")\n", 194 | "print(z[:2, 2:])" 195 | ], 196 | "outputs": [], 197 | "metadata": { 198 | "collapsed": false, 199 | "jupyter": { 200 | "outputs_hidden": false 201 | } 202 | } 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "source": [ 207 | "# Exercise 5" 208 | ], 209 | "metadata": {} 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "source": [ 215 | "y = array([[1.0, -2], [-3, 4]], dtype=float)\n", 216 | "y" 217 | ], 218 | "outputs": [], 219 | "metadata": { 220 | "collapsed": false, 221 | "jupyter": { 222 | "outputs_hidden": false 223 | } 224 | } 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "source": [ 230 | "y = array([[1.0, -2], [-3, 4]], dtype=np.float64)\n", 231 | "y" 232 | ], 233 | "outputs": [], 234 | "metadata": { 235 | "collapsed": false, 236 | "jupyter": { 237 | "outputs_hidden": false 238 | }, 239 | "pycharm": { 240 | "name": "#%%\n" 241 | } 242 | } 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": null, 247 | "source": [ 248 | "y = array([[1.0, -2], [-3, 4]], dtype=np.int32)\n", 249 | "y" 250 | ], 251 | "outputs": [], 252 | "metadata": { 253 | "collapsed": false, 254 | "jupyter": { 255 | "outputs_hidden": false 256 | }, 257 | "pycharm": { 258 | "name": "#%%\n" 259 | } 260 | } 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "source": [ 266 | "y = array([[1.0, -2], [-3, 4]], dtype=np.uint32)\n", 267 | "y" 268 | ], 269 | "outputs": [], 270 | "metadata": { 271 | "collapsed": false, 272 | "jupyter": { 273 | "outputs_hidden": false 274 | }, 275 | "pycharm": { 276 | "name": "#%%\n" 277 | } 278 | } 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "source": [ 284 | "y = array([[1.0, -2], [-3, 4]], dtype=np.complex128)\n", 285 | "y" 286 | ], 287 | "outputs": [], 288 | "metadata": { 289 | "collapsed": false, 290 | "jupyter": { 291 | "outputs_hidden": false 292 | }, 293 | "pycharm": { 294 | "name": "#%%\n" 295 | } 296 | } 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "source": [ 302 | "y = array([[1.0, -2], [-3, 4]], copy=False)\n", 303 | "y" 304 | ], 305 | "outputs": [], 306 | "metadata": { 307 | "collapsed": false, 308 | "jupyter": { 309 | "outputs_hidden": false 310 | }, 311 | "pycharm": { 312 | "name": "#%%\n" 313 | } 314 | } 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "source": [ 320 | "y = array([[1.0, -2], [-3, 4]], ndmin=3)\n", 321 | "y" 322 | ], 323 | "outputs": [], 324 | "metadata": { 325 | "collapsed": false, 326 | "jupyter": { 327 | "outputs_hidden": false 328 | }, 329 | "pycharm": { 330 | "name": "#%%\n" 331 | } 332 | } 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "source": [ 338 | "print(\"shape(y):\")\n", 339 | "print(shape(y))" 340 | ], 341 | "outputs": [], 342 | "metadata": { 343 | "collapsed": false, 344 | "jupyter": { 345 | "outputs_hidden": false 346 | }, 347 | "pycharm": { 348 | "name": "#%%\n" 349 | } 350 | } 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "source": [ 356 | "y = array([[1.0, -2], [-3, 4]], ndmin=4)\n", 357 | "y" 358 | ], 359 | "outputs": [], 360 | "metadata": { 361 | "collapsed": false, 362 | "jupyter": { 363 | "outputs_hidden": false 364 | }, 365 | "pycharm": { 366 | "name": "#%%\n" 367 | } 368 | } 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": null, 373 | "source": [ 374 | "print(\"shape(y):\")\n", 375 | "print(shape(y))" 376 | ], 377 | "outputs": [], 378 | "metadata": { 379 | "collapsed": false, 380 | "jupyter": { 381 | "outputs_hidden": false 382 | }, 383 | "pycharm": { 384 | "name": "#%%\n" 385 | } 386 | } 387 | } 388 | ], 389 | "metadata": { 390 | "kernelspec": { 391 | "display_name": "Python 3 (ipykernel)", 392 | "language": "python", 393 | "name": "python3" 394 | }, 395 | "language_info": { 396 | "codemirror_mode": { 397 | "name": "ipython", 398 | "version": 3 399 | }, 400 | "file_extension": ".py", 401 | "mimetype": "text/x-python", 402 | "name": "python", 403 | "nbconvert_exporter": "python", 404 | "pygments_lexer": "ipython3", 405 | "version": "3.9.6" 406 | } 407 | }, 408 | "nbformat": 4, 409 | "nbformat_minor": 4 410 | } -------------------------------------------------------------------------------- /solutions/chapter06/special_matrices.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Special Arrays" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## imports" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "from numpy import zeros, ones, empty, dot, eye, exp, tile, array, diag" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "## Exercise 1\n" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": { 37 | "collapsed": false, 38 | "jupyter": { 39 | "outputs_hidden": false 40 | } 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "x = zeros((10,5))\n", 45 | "x" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": { 52 | "collapsed": false, 53 | "jupyter": { 54 | "outputs_hidden": false 55 | }, 56 | "pycharm": { 57 | "name": "#%%\n" 58 | } 59 | }, 60 | "outputs": [], 61 | "source": [ 62 | "y = ones((10,5))\n", 63 | "y" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "## Exercise 2\n" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": { 77 | "collapsed": false, 78 | "jupyter": { 79 | "outputs_hidden": false 80 | } 81 | }, 82 | "outputs": [], 83 | "source": [ 84 | "print(\"dot(x,y.T):\")\n", 85 | "dot(x,y.T)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": { 92 | "collapsed": false, 93 | "jupyter": { 94 | "outputs_hidden": false 95 | }, 96 | "pycharm": { 97 | "name": "#%%\n" 98 | } 99 | }, 100 | "outputs": [], 101 | "source": [ 102 | "print(\"dot(x.T,y):\")\n", 103 | "dot(x.T,y)\n" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "## Exercise 3\n" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": { 117 | "collapsed": false, 118 | "jupyter": { 119 | "outputs_hidden": false 120 | } 121 | }, 122 | "outputs": [], 123 | "source": [ 124 | "z = eye(5)\n", 125 | "print(\"exp(z):\")\n", 126 | "exp(z)\n" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "## Exercise 4\n" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": { 140 | "collapsed": false, 141 | "jupyter": { 142 | "outputs_hidden": false 143 | } 144 | }, 145 | "outputs": [], 146 | "source": [ 147 | "x = tile(array([0.0]),(10,5))\n", 148 | "x" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": { 155 | "collapsed": false, 156 | "jupyter": { 157 | "outputs_hidden": false 158 | }, 159 | "pycharm": { 160 | "name": "#%%\n" 161 | } 162 | }, 163 | "outputs": [], 164 | "source": [ 165 | "y = tile(array([1.0]),(10,5))\n", 166 | "y" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "## Exercise 5\n" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": { 180 | "collapsed": false, 181 | "jupyter": { 182 | "outputs_hidden": false 183 | } 184 | }, 185 | "outputs": [], 186 | "source": [ 187 | "z = diag(ones((5)))\n", 188 | "z" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "## Exercise 6\n" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": null, 201 | "metadata": { 202 | "collapsed": false, 203 | "jupyter": { 204 | "outputs_hidden": false 205 | } 206 | }, 207 | "outputs": [], 208 | "source": [ 209 | "y = empty((1,))\n", 210 | "y" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": { 217 | "collapsed": false, 218 | "jupyter": { 219 | "outputs_hidden": false 220 | }, 221 | "pycharm": { 222 | "name": "#%%\n" 223 | } 224 | }, 225 | "outputs": [], 226 | "source": [ 227 | "y = empty((10,))\n", 228 | "y" 229 | ] 230 | } 231 | ], 232 | "metadata": { 233 | "kernelspec": { 234 | "display_name": "Python 3", 235 | "language": "python", 236 | "name": "python3" 237 | }, 238 | "language_info": { 239 | "codemirror_mode": { 240 | "name": "ipython", 241 | "version": 3 242 | }, 243 | "file_extension": ".py", 244 | "mimetype": "text/x-python", 245 | "name": "python", 246 | "nbconvert_exporter": "python", 247 | "pygments_lexer": "ipython3", 248 | "version": "3.8.5" 249 | } 250 | }, 251 | "nbformat": 4, 252 | "nbformat_minor": 4 253 | } 254 | -------------------------------------------------------------------------------- /solutions/chapter07/Special_Arrays.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Special Arrays" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "from numpy import (\n", 17 | " arange,\n", 18 | " array,\n", 19 | " diag,\n", 20 | " dot,\n", 21 | " eye,\n", 22 | " hstack,\n", 23 | " kron,\n", 24 | " prod,\n", 25 | " reshape,\n", 26 | " tile,\n", 27 | " trace,\n", 28 | " vstack,\n", 29 | ")\n", 30 | "from numpy.linalg import cholesky, det, eig, eigvals, inv, lstsq, matrix_rank\n", 31 | "from numpy.random import randn" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "## Exercise 1" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "x = arange(12.0)\n", 48 | "x.shape = 2, 6\n", 49 | "x.shape = 3, 4\n", 50 | "x.shape = 4, 3\n", 51 | "x.shape = 6, 2\n", 52 | "x.shape = 2, 2, 3\n", 53 | "x.shape = 12" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "## Exercise 2" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": { 67 | "collapsed": false, 68 | "jupyter": { 69 | "outputs_hidden": false 70 | } 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "x = reshape(arange(12.0), (4, 3))\n", 75 | "print(\"x.flatten()[1::2]:\")\n", 76 | "print(x.flatten()[1::2])\n", 77 | "print(\"x.ravel()[1::2]:\")\n", 78 | "print(x.ravel()[1::2])\n", 79 | "print(\"x.flat[1::2]:\")\n", 80 | "print(x.flat[1::2])" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "## Exercise 3" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "x = randn(2, 2)\n", 97 | "y = randn(1, 1)\n", 98 | "z = randn(3, 2)\n", 99 | "\n", 100 | "pt1 = hstack((x, tile(y, (2, 3))))\n", 101 | "pt2 = vstack((z.T, tile(y, (1, 3))))\n", 102 | "pt3 = hstack((z, pt2))\n", 103 | "final = vstack((pt1, pt3))" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "## Exercise 4" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": { 117 | "collapsed": false, 118 | "jupyter": { 119 | "outputs_hidden": false 120 | } 121 | }, 122 | "outputs": [], 123 | "source": [ 124 | "x = reshape(arange(12.0), (2, 2, 3))\n", 125 | "print(\"x.squeeze():\")\n", 126 | "print(x.squeeze())" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "## Exercise 5" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "y = array([[2, 0.5], [0.5, 4]])\n", 143 | "z = diag(diag(y))" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "## Exercise 6" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": { 157 | "collapsed": false, 158 | "jupyter": { 159 | "outputs_hidden": false 160 | } 161 | }, 162 | "outputs": [], 163 | "source": [ 164 | "z = cholesky(y)\n", 165 | "print(\"dot(z,z.T):\")\n", 166 | "print(dot(z, z.T))" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "## Exercise 7" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": { 180 | "collapsed": false, 181 | "jupyter": { 182 | "outputs_hidden": false 183 | } 184 | }, 185 | "outputs": [], 186 | "source": [ 187 | "print(\"trace(y):\")\n", 188 | "print(trace(y))\n", 189 | "D = eigvals(y)\n", 190 | "print(\"sum(D):\")\n", 191 | "print(sum(D))" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "## Exercise 7b" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": { 205 | "collapsed": false, 206 | "jupyter": { 207 | "outputs_hidden": false 208 | } 209 | }, 210 | "outputs": [], 211 | "source": [ 212 | "print(\"det(y):\")\n", 213 | "print(det(y))\n", 214 | "print(\"prod(D):\")\n", 215 | "print(prod(D))" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "## Exercise 8" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": null, 228 | "metadata": { 229 | "collapsed": false, 230 | "jupyter": { 231 | "outputs_hidden": false 232 | } 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | "print(\"inv(y):\")\n", 237 | "print(inv(y))\n", 238 | "(D, V) = eig(y)\n", 239 | "D = 1 / D\n", 240 | "print(\"dot(dot(V,diag(D)),V.T):\")\n", 241 | "print(dot(dot(V, diag(D)), V.T))" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "## Exercise 9" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [ 257 | "x = randn(100, 2)\n", 258 | "e = randn(100, 1)\n", 259 | "B = array([[1], [0.5]])\n", 260 | "y = dot(x, B) + e\n", 261 | "\n", 262 | "out = lstsq(x, y, rcond=None)\n", 263 | "estimate = out[0]" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "## Exercise 10" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": { 277 | "collapsed": false, 278 | "jupyter": { 279 | "outputs_hidden": false 280 | } 281 | }, 282 | "outputs": [], 283 | "source": [ 284 | "y = array([[5, -1.5, -3.5], [-1.5, 2, -0.5], [-3.5, -0.5, 4]])\n", 285 | "D = eigvals(y)\n", 286 | "print(\"matrix_rank(y):\")\n", 287 | "print(matrix_rank(y))\n", 288 | "print(\"det(y):\")\n", 289 | "print(det(y))" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "## Exercise 11" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": null, 302 | "metadata": { 303 | "collapsed": false, 304 | "jupyter": { 305 | "outputs_hidden": false 306 | } 307 | }, 308 | "outputs": [], 309 | "source": [ 310 | "x = randn(100, 2)\n", 311 | "SigmaX = dot(x.T, x) / 100\n", 312 | "print(\"kron(eye(2),SigmaX):\")\n", 313 | "print(kron(eye(2), SigmaX))" 314 | ] 315 | } 316 | ], 317 | "metadata": { 318 | "kernelspec": { 319 | "display_name": "Python 3 (ipykernel)", 320 | "language": "python", 321 | "name": "python3" 322 | }, 323 | "language_info": { 324 | "codemirror_mode": { 325 | "name": "ipython", 326 | "version": 3 327 | }, 328 | "file_extension": ".py", 329 | "mimetype": "text/x-python", 330 | "name": "python", 331 | "nbconvert_exporter": "python", 332 | "pygments_lexer": "ipython3", 333 | "version": "3.9.6" 334 | } 335 | }, 336 | "nbformat": 4, 337 | "nbformat_minor": 4 338 | } 339 | -------------------------------------------------------------------------------- /solutions/chapter07/array_and_matrix_functions_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "source": [ 6 | "# Array Functions\n" 7 | ], 8 | "metadata": {} 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "source": [ 14 | "from numpy import (\r\n", 15 | " arange,\r\n", 16 | " array,\r\n", 17 | " diag,\r\n", 18 | " dot,\r\n", 19 | " eye,\r\n", 20 | " hstack,\r\n", 21 | " kron,\r\n", 22 | " prod,\r\n", 23 | " reshape,\r\n", 24 | " tile,\r\n", 25 | " trace,\r\n", 26 | " vstack,\r\n", 27 | ")\r\n", 28 | "from numpy.linalg import cholesky, det, eig, eigvals, inv, lstsq, matrix_rank\r\n", 29 | "from numpy.random import randn" 30 | ], 31 | "outputs": [], 32 | "metadata": { 33 | "collapsed": false, 34 | "jupyter": { 35 | "outputs_hidden": false 36 | }, 37 | "pycharm": { 38 | "name": "#%%\n" 39 | } 40 | } 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "source": [ 45 | "## Exercise 1\n" 46 | ], 47 | "metadata": {} 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "source": [ 53 | "x = arange(12.0)\r\n", 54 | "x.shape = 2, 6\r\n", 55 | "x.shape = 3, 4\r\n", 56 | "x.shape = 4, 3\r\n", 57 | "x.shape = 6, 2\r\n", 58 | "x.shape = 2, 2, 3\r\n", 59 | "x.shape = 12" 60 | ], 61 | "outputs": [], 62 | "metadata": {} 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "source": [ 67 | "## Exercise 2\n" 68 | ], 69 | "metadata": {} 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "source": [ 75 | "x = reshape(arange(12.0), (4, 3))\r\n", 76 | "x" 77 | ], 78 | "outputs": [], 79 | "metadata": {} 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "source": [ 85 | "print(\"x.flatten()[1::2]:\")\n", 86 | "x.flatten()[1::2]" 87 | ], 88 | "outputs": [], 89 | "metadata": { 90 | "collapsed": false, 91 | "jupyter": { 92 | "outputs_hidden": false 93 | }, 94 | "pycharm": { 95 | "name": "#%%\n" 96 | } 97 | } 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "source": [ 103 | "print(\"x.ravel()[1::2]:\")\n", 104 | "x.ravel()[1::2]" 105 | ], 106 | "outputs": [], 107 | "metadata": { 108 | "collapsed": false, 109 | "jupyter": { 110 | "outputs_hidden": false 111 | }, 112 | "pycharm": { 113 | "name": "#%%\n" 114 | } 115 | } 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "source": [ 121 | "print(\"x.flat[1::2]:\")\n", 122 | "x.flat[1::2]" 123 | ], 124 | "outputs": [], 125 | "metadata": { 126 | "collapsed": false, 127 | "jupyter": { 128 | "outputs_hidden": false 129 | }, 130 | "pycharm": { 131 | "name": "#%%\n" 132 | } 133 | } 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "source": [ 138 | "## Exercise 3\n" 139 | ], 140 | "metadata": {} 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "source": [ 146 | "x = randn(2, 2)\n", 147 | "y = randn(1, 1)\n", 148 | "z = randn(3, 2)\n", 149 | "pt1 = hstack((x, tile(y, (2, 3))))\n", 150 | "pt2 = vstack((z.T, tile(y, (1, 3))))\n", 151 | "pt3 = hstack((z, pt2))\n", 152 | "final = vstack((pt1, pt3))\n", 153 | "final" 154 | ], 155 | "outputs": [], 156 | "metadata": {} 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "source": [ 161 | "## Exercise 4\n" 162 | ], 163 | "metadata": {} 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "source": [ 169 | "x = reshape(arange(12.0), (2, 2, 3))\n", 170 | "print(\"x.squeeze():\")\n", 171 | "x.squeeze()" 172 | ], 173 | "outputs": [], 174 | "metadata": {} 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "source": [ 179 | "## Exercise 5\n" 180 | ], 181 | "metadata": {} 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "source": [ 187 | "y = array([[2, 0.5], [0.5, 4]])\n", 188 | "z = diag(diag(y))\n", 189 | "z" 190 | ], 191 | "outputs": [], 192 | "metadata": {} 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "source": [ 197 | "## Exercise 6\n" 198 | ], 199 | "metadata": {} 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "source": [], 205 | "outputs": [], 206 | "metadata": {} 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "source": [ 212 | "z = cholesky(y)\n", 213 | "z" 214 | ], 215 | "outputs": [], 216 | "metadata": { 217 | "collapsed": false, 218 | "jupyter": { 219 | "outputs_hidden": false 220 | }, 221 | "pycharm": { 222 | "name": "#%%\n" 223 | } 224 | } 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "source": [ 230 | "print(\"dot(z,z.T):\")\n", 231 | "dot(z, z.T)" 232 | ], 233 | "outputs": [], 234 | "metadata": { 235 | "collapsed": false, 236 | "jupyter": { 237 | "outputs_hidden": false 238 | }, 239 | "pycharm": { 240 | "name": "#%%\n" 241 | } 242 | } 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "source": [ 247 | "## Exercise 7\n" 248 | ], 249 | "metadata": {} 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "source": [ 255 | "print(\"trace(y):\")\n", 256 | "trace(y)" 257 | ], 258 | "outputs": [], 259 | "metadata": {} 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "source": [ 265 | "D = eigvals(y)\n", 266 | "print(\"sum(D):\")\n", 267 | "sum(D)" 268 | ], 269 | "outputs": [], 270 | "metadata": { 271 | "collapsed": false, 272 | "jupyter": { 273 | "outputs_hidden": false 274 | }, 275 | "pycharm": { 276 | "name": "#%%\n" 277 | } 278 | } 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "source": [ 283 | "## Exercise 7b\n" 284 | ], 285 | "metadata": {} 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "source": [ 291 | "print(\"det(y):\")\n", 292 | "det(y)" 293 | ], 294 | "outputs": [], 295 | "metadata": {} 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": null, 300 | "source": [ 301 | "print(\"prod(D):\")\n", 302 | "prod(D)" 303 | ], 304 | "outputs": [], 305 | "metadata": { 306 | "collapsed": false, 307 | "jupyter": { 308 | "outputs_hidden": false 309 | }, 310 | "pycharm": { 311 | "name": "#%%\n" 312 | } 313 | } 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "source": [ 318 | "## Exercise 8\n" 319 | ], 320 | "metadata": {} 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": null, 325 | "source": [ 326 | "print(\"inv(y):\")\n", 327 | "inv(y)" 328 | ], 329 | "outputs": [], 330 | "metadata": {} 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": null, 335 | "source": [ 336 | "(D, V) = eig(y)\n", 337 | "D = 1 / D\n", 338 | "print(\"dot(dot(V,diag(D)),V.T):\")\n", 339 | "dot(dot(V, diag(D)), V.T)" 340 | ], 341 | "outputs": [], 342 | "metadata": { 343 | "collapsed": false, 344 | "jupyter": { 345 | "outputs_hidden": false 346 | }, 347 | "pycharm": { 348 | "name": "#%%\n" 349 | } 350 | } 351 | }, 352 | { 353 | "cell_type": "markdown", 354 | "source": [ 355 | "## Exercise 9\n" 356 | ], 357 | "metadata": {} 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "source": [ 363 | "x = randn(100, 2)\n", 364 | "e = randn(100, 1)\n", 365 | "B = array([[1], [0.5]])\n", 366 | "y = dot(x, B) + e\n", 367 | "out = lstsq(x, y, rcond=None)\n", 368 | "estimate = out[0]\n", 369 | "estimate" 370 | ], 371 | "outputs": [], 372 | "metadata": {} 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "source": [ 377 | "## Exercise 10\n" 378 | ], 379 | "metadata": {} 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": null, 384 | "source": [ 385 | "y = array([[5, -1.5, -3.5], [-1.5, 2, -0.5], [-3.5, -0.5, 4]])\n", 386 | "D = eigvals(y)\n", 387 | "print(\"matrix_rank(y):\")\n", 388 | "matrix_rank(y)" 389 | ], 390 | "outputs": [], 391 | "metadata": {} 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": null, 396 | "source": [ 397 | "print(\"det(y):\")\n", 398 | "det(y)" 399 | ], 400 | "outputs": [], 401 | "metadata": { 402 | "collapsed": false, 403 | "jupyter": { 404 | "outputs_hidden": false 405 | }, 406 | "pycharm": { 407 | "name": "#%%\n" 408 | } 409 | } 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "source": [ 414 | "## Exercise 11\n" 415 | ], 416 | "metadata": {} 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": null, 421 | "source": [ 422 | "x = randn(100, 2)\n", 423 | "SigmaX = dot(x.T, x) / 100\n", 424 | "print(\"kron(eye(2),SigmaX):\")\n", 425 | "kron(eye(2), SigmaX)" 426 | ], 427 | "outputs": [], 428 | "metadata": {} 429 | } 430 | ], 431 | "metadata": { 432 | "kernelspec": { 433 | "display_name": "Python 3 (ipykernel)", 434 | "language": "python", 435 | "name": "python3" 436 | }, 437 | "language_info": { 438 | "codemirror_mode": { 439 | "name": "ipython", 440 | "version": 3 441 | }, 442 | "file_extension": ".py", 443 | "mimetype": "text/x-python", 444 | "name": "python", 445 | "nbconvert_exporter": "python", 446 | "pygments_lexer": "ipython3", 447 | "version": "3.9.6" 448 | } 449 | }, 450 | "nbformat": 4, 451 | "nbformat_minor": 4 452 | } -------------------------------------------------------------------------------- /solutions/chapter08/exercise3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/solutions/chapter08/exercise3 -------------------------------------------------------------------------------- /solutions/chapter08/exercise3.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/solutions/chapter08/exercise3.mat -------------------------------------------------------------------------------- /solutions/chapter08/exercise3.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/solutions/chapter08/exercise3.xls -------------------------------------------------------------------------------- /solutions/chapter08/exercise3.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/solutions/chapter08/exercise3.xlsx -------------------------------------------------------------------------------- /solutions/chapter08/exercise3_numeric.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/solutions/chapter08/exercise3_numeric.xls -------------------------------------------------------------------------------- /solutions/chapter08/importing_and_exporting_data_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Import and Exporting Data" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false, 15 | "jupyter": { 16 | "outputs_hidden": false 17 | }, 18 | "pycharm": { 19 | "name": "#%%\n" 20 | } 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "import matplotlib.mlab as ml\n", 25 | "import numpy as np\n", 26 | "import openpyxl\n", 27 | "import scipy.io as io\n", 28 | "import xlrd" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## Exercise 1\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "# This exercise is done in Excel or OpenOffice" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "## Exercise 2\n" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "import1 = np.genfromtxt(\"exercise3_numeric.csv\", delimiter=\",\")\n", 61 | "import2 = np.loadtxt(\"exercise3_numeric.csv\", delimiter=\",\", skiprows=1)\n", 62 | "import4 = np.genfromtxt(\"exercise3_numeric.txt\", delimiter=\"\\t\")\n", 63 | "import5 = np.loadtxt(\"exercise3_numeric.txt\", delimiter=\"\\t\", skiprows=1)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "## Exercise 3\n" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "dates = import2[:, 0]\n", 80 | "SP500 = import2[:, 1]\n", 81 | "XOM = import2[:, 2]" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## Exercise 4\n" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "np.savez(\"exercise3\", dates=dates, SP500=SP500, XOM=XOM)\n", 98 | "np.savez_compressed(\"exercise3_compressed\", dates=dates, SP500=SP500, XOM=XOM)\n", 99 | "mdict = {\"dates\": dates, \"SP500\": SP500, \"XOM\": XOM}\n", 100 | "io.savemat(\"exercise3\", mdict)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "## Exercise 5\n" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "sumreturns = SP500 + XOM\n", 117 | "outputdata = np.hstack((dates, sumreturns))" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "## Exercise 6\n" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": null, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "np.savetxt(\"exercise3_new.csv\", outputdata)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "## Exercise 7\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "wb = xlrd.open_workbook(\"exercise3.xls\")\n", 150 | "sheetNames = wb.sheet_names()\n", 151 | "# Assumes 1 sheet name\n", 152 | "sheet = wb.sheet_by_name(sheetNames[0])\n", 153 | "excel_data = []\n", 154 | "for i in range(sheet.nrows):\n", 155 | " excel_data.append(sheet.row_values(i))\n", 156 | "header = excel_data[0]\n", 157 | "import_excel = np.array(excel_data[1:])" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "## Exercise 8\n" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "wb = openpyxl.load_workbook(\"exercise3.xlsx\")\n", 174 | "sheet = wb[wb.sheetnames[0]]\n", 175 | "excel_data = []\n", 176 | "rows = list(sheet.rows)\n", 177 | "header = []\n", 178 | "for c in rows[0]:\n", 179 | " header.append(c.value)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": { 186 | "collapsed": false, 187 | "jupyter": { 188 | "outputs_hidden": false 189 | }, 190 | "pycharm": { 191 | "name": "#%%\n" 192 | } 193 | }, 194 | "outputs": [], 195 | "source": [ 196 | "for r in rows[1:]:\n", 197 | " data = []\n", 198 | " for c in r:\n", 199 | " data.append(c.value)\n", 200 | " excel_data.append(data)\n", 201 | "import_xlsx = np.array(excel_data)" 202 | ] 203 | } 204 | ], 205 | "metadata": { 206 | "kernelspec": { 207 | "display_name": "Python 3 (ipykernel)", 208 | "language": "python", 209 | "name": "python3" 210 | }, 211 | "language_info": { 212 | "codemirror_mode": { 213 | "name": "ipython", 214 | "version": 3 215 | }, 216 | "file_extension": ".py", 217 | "mimetype": "text/x-python", 218 | "name": "python", 219 | "nbconvert_exporter": "python", 220 | "pygments_lexer": "ipython3", 221 | "version": "3.9.6" 222 | } 223 | }, 224 | "nbformat": 4, 225 | "nbformat_minor": 4 226 | } 227 | -------------------------------------------------------------------------------- /solutions/chapter09/inf_nan_and_numerics_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Inf, NaN and Numerics" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false, 15 | "jupyter": { 16 | "outputs_hidden": false 17 | }, 18 | "pycharm": { 19 | "name": "#%%\n" 20 | } 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "from numpy import exp, finfo, log" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## Exercise 1\n", 32 | "\n", 33 | "Obviously 1000 is the correct answer" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "log(exp(1000))" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "## Exercise 2\n" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "eps = finfo(float).eps\n", 59 | "0 == eps / 10" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": { 65 | "pycharm": { 66 | "name": "#%% md\n" 67 | } 68 | }, 69 | "source": [ 70 | "## Exercise 3" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": { 77 | "collapsed": false, 78 | "jupyter": { 79 | "outputs_hidden": false 80 | }, 81 | "pycharm": { 82 | "name": "#%%\n" 83 | } 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "x = (1 + eps / 10) - 1\n", 88 | "x == 0" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "## Exercise 4\n" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "0.1 == (0.1 + eps / 10)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "## Exercise 5\n" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "x = 1.0 * 10 ** 120\n", 121 | "y = 1.0 * 10 ** 120 + 1 * 10 ** 102\n", 122 | "print(\"type(x):\")\n", 123 | "print(type(x))\n", 124 | "print(\"type(y):\")\n", 125 | "print(type(y))\n", 126 | "x == y" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "## Exercise 6\n", 134 | "\n", 135 | "This problem and the previous are different since x and y are integers here\n", 136 | "which have arbitrary precision in Python" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "x = 10 ** 120\n", 146 | "y = 10 ** 120 + 10 ** 102\n", 147 | "print(\"type(x):\")\n", 148 | "print(type(x))\n", 149 | "print(\"type(y):\")\n", 150 | "print(type(y))\n", 151 | "x == y" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "## Exercise 6\n", 159 | "\n", 160 | "_Note_: This solution uses a loop which is introduced in a later chapter." 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "x = 2.0\n", 170 | "count = 0\n", 171 | "while x != 1.0:\n", 172 | " count += 1\n", 173 | " x = 1.0 + (x - 1.0) / 2.0\n", 174 | " print(count, x)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": { 181 | "collapsed": false, 182 | "jupyter": { 183 | "outputs_hidden": false 184 | }, 185 | "pycharm": { 186 | "name": "#%%\n" 187 | } 188 | }, 189 | "outputs": [], 190 | "source": [ 191 | "print(\"count:\")\n", 192 | "print(count)\n", 193 | "print(\"x:\")\n", 194 | "print(x)\n", 195 | "print(\"2**(-count+1):\")\n", 196 | "print(2 ** (-count + 1))\n", 197 | "print(\"eps:\")\n", 198 | "print(eps)" 199 | ] 200 | } 201 | ], 202 | "metadata": { 203 | "kernelspec": { 204 | "display_name": "Python 3 (ipykernel)", 205 | "language": "python", 206 | "name": "python3" 207 | }, 208 | "language_info": { 209 | "codemirror_mode": { 210 | "name": "ipython", 211 | "version": 3 212 | }, 213 | "file_extension": ".py", 214 | "mimetype": "text/x-python", 215 | "name": "python", 216 | "nbconvert_exporter": "python", 217 | "pygments_lexer": "ipython3", 218 | "version": "3.9.6" 219 | } 220 | }, 221 | "nbformat": 4, 222 | "nbformat_minor": 4 223 | } 224 | -------------------------------------------------------------------------------- /solutions/chapter10/logical_operators_and_find_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Logical Operators and Find" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false, 15 | "jupyter": { 16 | "outputs_hidden": false 17 | }, 18 | "pycharm": { 19 | "name": "#%%\n" 20 | } 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "import numpy as np\n", 25 | "from numpy import mean, std" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "## Exercise 1\n" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "data = np.load(\"exercise3_compressed.npz\")\n", 42 | "dates = data[\"dates\"]\n", 43 | "SP500 = data[\"SP500\"]\n", 44 | "XOM = data[\"XOM\"]" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": { 51 | "collapsed": false, 52 | "jupyter": { 53 | "outputs_hidden": false 54 | }, 55 | "pycharm": { 56 | "name": "#%%\n" 57 | } 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "print(\"sum(SP500<0):\")\n", 62 | "sum(SP500 < 0)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": { 69 | "collapsed": false, 70 | "jupyter": { 71 | "outputs_hidden": false 72 | }, 73 | "pycharm": { 74 | "name": "#%%\n" 75 | } 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "print(\"sum(XOM<0):\")\n", 80 | "sum(XOM < 0)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "## Exercise 2\n" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "SP500big = SP500 > (2 * SP500.std())\n", 97 | "SP500small = SP500 < (-2 * SP500.std())" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": { 104 | "collapsed": false, 105 | "jupyter": { 106 | "outputs_hidden": false 107 | }, 108 | "pycharm": { 109 | "name": "#%%\n" 110 | } 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "print(\"mean(SP500[SP500big]):\")\n", 115 | "mean(SP500[SP500big])" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "collapsed": false, 123 | "jupyter": { 124 | "outputs_hidden": false 125 | }, 126 | "pycharm": { 127 | "name": "#%%\n" 128 | } 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "print(\"mean(SP500[SP500small]):\")\n", 133 | "mean(SP500[SP500small])" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": { 140 | "collapsed": false, 141 | "jupyter": { 142 | "outputs_hidden": false 143 | }, 144 | "pycharm": { 145 | "name": "#%%\n" 146 | } 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "XOMbig = XOM > (2 * std(XOM))\n", 151 | "XOMsmall = XOM < (-2 * std(XOM))\n", 152 | "print(\"mean(XOM[XOMbig]):\")\n", 153 | "mean(XOM[XOMbig])" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": { 160 | "collapsed": false, 161 | "jupyter": { 162 | "outputs_hidden": false 163 | }, 164 | "pycharm": { 165 | "name": "#%%\n" 166 | } 167 | }, 168 | "outputs": [], 169 | "source": [ 170 | "print(\"mean(XOM[XOMsmall]):\")\n", 171 | "mean(XOM[XOMsmall])" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "## Exercise 3\n" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [ 187 | "bothNeg = np.logical_and(SP500 < 0, XOM < 0)\n", 188 | "data = np.vstack((SP500, XOM)).T\n", 189 | "corr = np.corrcoef(data.T)\n", 190 | "negCorr = np.corrcoef(data[bothNeg, :].T)" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": { 197 | "collapsed": false, 198 | "jupyter": { 199 | "outputs_hidden": false 200 | }, 201 | "pycharm": { 202 | "name": "#%%\n" 203 | } 204 | }, 205 | "outputs": [], 206 | "source": [ 207 | "print(\"corr:\")\n", 208 | "corr" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "collapsed": false, 216 | "jupyter": { 217 | "outputs_hidden": false 218 | }, 219 | "pycharm": { 220 | "name": "#%%\n" 221 | } 222 | }, 223 | "outputs": [], 224 | "source": [ 225 | "print(\"negCorr:\")\n", 226 | "negCorr" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "## Exercise 4\n" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": null, 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [ 242 | "oneNeg = np.logical_or(SP500 < 0, XOM < 0)\n", 243 | "oneNegCorr = np.corrcoef(data[oneNeg, :].T)\n", 244 | "print(\"oneNegCorr:\")\n", 245 | "oneNegCorr" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "## Exercise 5\n" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "def myany(x):\n", 262 | " \"\"\"Returns True if any value in the input is True\"\"\"\n", 263 | " return not np.all(np.logical_not(x))\n", 264 | "\n", 265 | "\n", 266 | "def myall(x):\n", 267 | " \"\"\"Returns True if all values in the input is True\"\"\"\n", 268 | " return not np.any(np.logical_not(x))" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": { 275 | "collapsed": false, 276 | "jupyter": { 277 | "outputs_hidden": false 278 | }, 279 | "pycharm": { 280 | "name": "#%%\n" 281 | } 282 | }, 283 | "outputs": [], 284 | "source": [ 285 | "np.any(SP500 < 0)" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": null, 291 | "metadata": { 292 | "collapsed": false, 293 | "jupyter": { 294 | "outputs_hidden": false 295 | }, 296 | "pycharm": { 297 | "name": "#%%\n" 298 | } 299 | }, 300 | "outputs": [], 301 | "source": [ 302 | "myany(SP500 < 0)" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": { 309 | "collapsed": false, 310 | "jupyter": { 311 | "outputs_hidden": false 312 | }, 313 | "pycharm": { 314 | "name": "#%%\n" 315 | } 316 | }, 317 | "outputs": [], 318 | "source": [ 319 | "np.any(SP500 < -0.5)" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": null, 325 | "metadata": { 326 | "collapsed": false, 327 | "jupyter": { 328 | "outputs_hidden": false 329 | }, 330 | "pycharm": { 331 | "name": "#%%\n" 332 | } 333 | }, 334 | "outputs": [], 335 | "source": [ 336 | "myany(SP500 < -0.5)" 337 | ] 338 | } 339 | ], 340 | "metadata": { 341 | "kernelspec": { 342 | "display_name": "Python 3 (ipykernel)", 343 | "language": "python", 344 | "name": "python3" 345 | }, 346 | "language_info": { 347 | "codemirror_mode": { 348 | "name": "ipython", 349 | "version": 3 350 | }, 351 | "file_extension": ".py", 352 | "mimetype": "text/x-python", 353 | "name": "python", 354 | "nbconvert_exporter": "python", 355 | "pygments_lexer": "ipython3", 356 | "version": "3.9.6" 357 | } 358 | }, 359 | "nbformat": 4, 360 | "nbformat_minor": 4 361 | } 362 | -------------------------------------------------------------------------------- /solutions/chapter11/advanced_selection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Advanced Selection" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Let x=arange(10.0), y=reshape(arange(25.0),(5,5)) and z=reshape(arange(64.0),(4,4,4)) in all exercises." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "from numpy import arange, array, ix_, reshape, squeeze\n", 24 | "\n", 25 | "x = arange(10.0)\n", 26 | "y = reshape(arange(25.0), (5, 5))\n", 27 | "z = reshape(arange(64.0), (4, 4, 4))" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "# Exercise 1" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "x" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "x[4]" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "x[4]" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "# Exercise 2" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "x[:5]" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "x[-10:-5]" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "x[x < 5]" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "# Exercise 3" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "x[::2]" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "x[0:10:2]" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "x[8::-2][::-1]" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "# Exercise 4" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "y[1]" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "y[1, :]" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "# Has dimension 2\n", 164 | "y[1:2]" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "squeeze(y[1:2])" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "# Exercise 5" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "y[[2, 4]]" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "y[2::2]" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "# Exercise 6" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": {}, 212 | "outputs": [], 213 | "source": [ 214 | "y[2:5:2, 2:5]" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "y[[2, 4], 2:5]" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "y[2:5:2, [2, 3, 4]]" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "ix_([2, 4], [2, 3, 4])" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": null, 247 | "metadata": {}, 248 | "outputs": [], 249 | "source": [ 250 | "y[ix_([2, 4], [2, 3, 4])]" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "# Exercise 7" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "import numpy as np\n", 267 | "\n", 268 | "sel = np.sum(((y % 5) == 0) | ((y % 7) == 0), 1) == 2" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "y[sel]" 278 | ] 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "metadata": {}, 283 | "source": [ 284 | "# Exercise 8" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "metadata": {}, 291 | "outputs": [], 292 | "source": [ 293 | "z[0]" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [ 302 | "z[:1]" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "# Exercise 9" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": null, 315 | "metadata": {}, 316 | "outputs": [], 317 | "source": [ 318 | "z[:, 2:4]" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": null, 324 | "metadata": {}, 325 | "outputs": [], 326 | "source": [ 327 | "z[:, [2, 3]]" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": null, 333 | "metadata": {}, 334 | "outputs": [], 335 | "source": [ 336 | "sel = ix_([0, 1, 2, 3], [2, 3])\n", 337 | "sel" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": null, 343 | "metadata": {}, 344 | "outputs": [], 345 | "source": [ 346 | "z[sel]" 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": {}, 352 | "source": [ 353 | "# Exercise 10" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": null, 359 | "metadata": {}, 360 | "outputs": [], 361 | "source": [ 362 | "z = reshape(arange(64.0), (4, 4, 4))\n", 363 | "z.flat[::2] = 0\n", 364 | "z" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": null, 370 | "metadata": {}, 371 | "outputs": [], 372 | "source": [ 373 | "z = reshape(arange(64.0), (4, 4, 4))\n", 374 | "z[:, :, 0::2] = 0\n", 375 | "z" 376 | ] 377 | }, 378 | { 379 | "cell_type": "markdown", 380 | "metadata": {}, 381 | "source": [ 382 | "# Exercise 11\n", 383 | "\n", 384 | "_Note_: This is quite challenging. The first cell selects the rows that have values divisible by 4 and 6." 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": null, 390 | "metadata": {}, 391 | "outputs": [], 392 | "source": [ 393 | "import numpy as np\n", 394 | "\n", 395 | "z = reshape(arange(64.0), (4, 4, 4))\n", 396 | "div_4 = (z % 4) == 0\n", 397 | "div_6 = (z % 6) == 0\n", 398 | "sel = np.any(div_4, 2) & np.any(div_6, 2)\n", 399 | "sel" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "Next `z` is temporarily reshaped and then the `flat` version of sel is used to select the rows of the reshaped `z`" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": null, 412 | "metadata": {}, 413 | "outputs": [], 414 | "source": [ 415 | "z.reshape((16, -1))" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": null, 421 | "metadata": {}, 422 | "outputs": [], 423 | "source": [ 424 | "z.reshape((16, -1))[sel.flat] = [-1, -1, -1, -1]\n", 425 | "z" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "metadata": {}, 431 | "source": [ 432 | "# Exercise 12\n", 433 | "\n", 434 | "This exercise show that it selects rows 0 and 1 in the first dimension, and then rows 1 and 0. The resulting array has 3 dimensions, since each row has 1 and `sel` has 2." 435 | ] 436 | }, 437 | { 438 | "cell_type": "code", 439 | "execution_count": null, 440 | "metadata": {}, 441 | "outputs": [], 442 | "source": [ 443 | "sel = array([[0, 1], [1, 0]])\n", 444 | "y[sel]" 445 | ] 446 | } 447 | ], 448 | "metadata": { 449 | "kernelspec": { 450 | "display_name": "Python 3 (ipykernel)", 451 | "language": "python", 452 | "name": "python3" 453 | }, 454 | "language_info": { 455 | "codemirror_mode": { 456 | "name": "ipython", 457 | "version": 3 458 | }, 459 | "file_extension": ".py", 460 | "mimetype": "text/x-python", 461 | "name": "python", 462 | "nbconvert_exporter": "python", 463 | "pygments_lexer": "ipython3", 464 | "version": "3.9.6" 465 | } 466 | }, 467 | "nbformat": 4, 468 | "nbformat_minor": 4 469 | } 470 | -------------------------------------------------------------------------------- /solutions/chapter12/flow_control_and_loops_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Flow Control and Loops" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false, 15 | "jupyter": { 16 | "outputs_hidden": false 17 | }, 18 | "pycharm": { 19 | "name": "#%%\n" 20 | } 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "import scipy.stats as stats\n", 25 | "from numpy import absolute, ones, zeros\n", 26 | "from numpy.random import randn" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "## Exercise 1\n" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "r1 = randn(1)\n", 43 | "r2 = randn(1)\n", 44 | "if r1 < 0 and r2 < 0:\n", 45 | " pass\n", 46 | "elif (r1 >= 0 and r2 < 0) or (r1 < 0 and r2 >= 0):\n", 47 | " pass\n", 48 | "else:\n", 49 | " pass" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "## Exercise 2\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "T = 1000\n", 66 | "e = randn(2 * T)\n", 67 | "phi1 = 1.4\n", 68 | "phi2 = -0.8\n", 69 | "theta1 = 0.4\n", 70 | "theta2 = 0.8\n", 71 | "y = zeros(e.shape)\n", 72 | "for t in range(2, 2 * T):\n", 73 | " y[t] = (\n", 74 | " phi1 * y[t - 1] + phi2 * y[t - 2] + theta1 * e[t - 1] + theta2 * e[t - 2] + e[t]\n", 75 | " )\n", 76 | "y = y[1000:]" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "## Exercise 3\n" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "omega = 0.05\n", 93 | "alpha = 0.05\n", 94 | "beta = 0.9\n", 95 | "y = zeros(2 * T)\n", 96 | "sigma2 = ones(2 * T)\n", 97 | "for i in range(1, 2 * T):\n", 98 | " sigma2[t] = omega + alpha * (y[t - 1] ** 2) + beta * sigma2[t - 1]\n", 99 | " y[t] = sigma2[t] * e[t]\n", 100 | "sigma2 = sigma2[1000:]\n", 101 | "y = y[1000:]" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "## Exercise 4\n" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "omega = 0.05\n", 118 | "alpha = 0.02\n", 119 | "gamma = 0.07\n", 120 | "beta = 0.9\n", 121 | "y = zeros(2 * T)\n", 122 | "sigma2 = ones(2 * T)\n", 123 | "for i in range(1, 2 * T):\n", 124 | " sigma2[t] = (\n", 125 | " omega\n", 126 | " + alpha * (y[t - 1] ** 2)\n", 127 | " + gamma * ((y[t - 1] < 0) * y[t - 1] ** 2)\n", 128 | " + beta * sigma2[t - 1]\n", 129 | " )\n", 130 | " y[t] = sigma2[t] * e[t]\n", 131 | "sigma2 = sigma2[1000:]\n", 132 | "y = y[1000:]" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "## Exercise 6\n" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "M = 5\n", 149 | "x = zeros((M, M))\n", 150 | "for i in range(M):\n", 151 | " for j in range(M):\n", 152 | " x[i, j] = i * j\n", 153 | "y = zeros((M, M))\n", 154 | "for i, row in enumerate(y):\n", 155 | " for j, col in enumerate(row):\n", 156 | " y[i, j] = i * j" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "## Exercise 7\n" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "def invert_normal_cdf(prob):\n", 173 | " UB = 10.0\n", 174 | " LB = -10.0\n", 175 | " cdf = stats.norm(loc=0, scale=1).cdf\n", 176 | " distance = 1\n", 177 | " while distance > 1e-8:\n", 178 | " MP = (UB + LB) / 2\n", 179 | " MPc = cdf(MP)\n", 180 | " if prob > MPc:\n", 181 | " LB = MP\n", 182 | " else:\n", 183 | " UB = MP\n", 184 | " distance = absolute(MPc - prob)\n", 185 | " return MP" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "## Exercise 8 \n" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "icdf = stats.norm(loc=0, scale=1).ppf\n", 202 | "\n", 203 | "print(\"invert_normal_cdf(.01):\")\n", 204 | "invert_normal_cdf(0.01)" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": null, 210 | "metadata": { 211 | "collapsed": false, 212 | "jupyter": { 213 | "outputs_hidden": false 214 | }, 215 | "pycharm": { 216 | "name": "#%%\n" 217 | } 218 | }, 219 | "outputs": [], 220 | "source": [ 221 | "print(\"icdf(.01):\")\n", 222 | "icdf(0.01)" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": null, 228 | "metadata": { 229 | "collapsed": false, 230 | "jupyter": { 231 | "outputs_hidden": false 232 | }, 233 | "pycharm": { 234 | "name": "#%%\n" 235 | } 236 | }, 237 | "outputs": [], 238 | "source": [ 239 | "print(\"invert_normal_cdf(.5):\")\n", 240 | "invert_normal_cdf(0.5)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": null, 246 | "metadata": { 247 | "collapsed": false, 248 | "jupyter": { 249 | "outputs_hidden": false 250 | }, 251 | "pycharm": { 252 | "name": "#%%\n" 253 | } 254 | }, 255 | "outputs": [], 256 | "source": [ 257 | "print(\"icdf(.5):\")\n", 258 | "icdf(0.5)" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "metadata": { 265 | "collapsed": false, 266 | "jupyter": { 267 | "outputs_hidden": false 268 | }, 269 | "pycharm": { 270 | "name": "#%%\n" 271 | } 272 | }, 273 | "outputs": [], 274 | "source": [ 275 | "print(\"invert_normal_cdf(.975):\")\n", 276 | "invert_normal_cdf(0.975)" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": null, 282 | "metadata": { 283 | "collapsed": false, 284 | "jupyter": { 285 | "outputs_hidden": false 286 | }, 287 | "pycharm": { 288 | "name": "#%%\n" 289 | } 290 | }, 291 | "outputs": [], 292 | "source": [ 293 | "print(\"icdf(.975):\")\n", 294 | "icdf(0.975)" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "## Exercise 9\n" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [ 310 | "r = randn(20)\n", 311 | "y = [val for val in r if val < 0]\n", 312 | "y" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "metadata": {}, 319 | "outputs": [], 320 | "source": [ 321 | "print(\"r[r<0]:\")\n", 322 | "print(r[r < 0])" 323 | ] 324 | } 325 | ], 326 | "metadata": { 327 | "kernelspec": { 328 | "display_name": "Python 3 (ipykernel)", 329 | "language": "python", 330 | "name": "python3" 331 | }, 332 | "language_info": { 333 | "codemirror_mode": { 334 | "name": "ipython", 335 | "version": 3 336 | }, 337 | "file_extension": ".py", 338 | "mimetype": "text/x-python", 339 | "name": "python", 340 | "nbconvert_exporter": "python", 341 | "pygments_lexer": "ipython3", 342 | "version": "3.9.6" 343 | } 344 | }, 345 | "nbformat": 4, 346 | "nbformat_minor": 4 347 | } 348 | -------------------------------------------------------------------------------- /solutions/chapter13/no-exercises-in-chapter.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/solutions/chapter13/no-exercises-in-chapter.txt -------------------------------------------------------------------------------- /solutions/chapter14/dates_and_times.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "source": [ 6 | "# Dates and Times" 7 | ], 8 | "metadata": { 9 | "pycharm": { 10 | "name": "#%% md\n" 11 | } 12 | } 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "source": [ 17 | "## Exercise 1" 18 | ], 19 | "metadata": { 20 | "pycharm": { 21 | "name": "#%% md\n" 22 | } 23 | } 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "source": [ 29 | "import datetime as dt\r\n", 30 | "\r\n", 31 | "now = dt.datetime.now()\r\n", 32 | "now" 33 | ], 34 | "outputs": [], 35 | "metadata": { 36 | "collapsed": false, 37 | "jupyter": { 38 | "outputs_hidden": false 39 | }, 40 | "pycharm": { 41 | "name": "#%%\n" 42 | } 43 | } 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "source": [ 48 | "## Exercise 2" 49 | ], 50 | "metadata": { 51 | "pycharm": { 52 | "name": "#%% md\n" 53 | } 54 | } 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "source": [ 60 | "now + dt.timedelta(1)" 61 | ], 62 | "outputs": [], 63 | "metadata": { 64 | "collapsed": false, 65 | "jupyter": { 66 | "outputs_hidden": false 67 | }, 68 | "pycharm": { 69 | "name": "#%%\n" 70 | } 71 | } 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "source": [ 77 | "now + dt.timedelta(7)" 78 | ], 79 | "outputs": [], 80 | "metadata": { 81 | "collapsed": false, 82 | "jupyter": { 83 | "outputs_hidden": false 84 | }, 85 | "pycharm": { 86 | "name": "#%%\n" 87 | } 88 | } 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "source": [ 94 | "now + dt.timedelta(365)" 95 | ], 96 | "outputs": [], 97 | "metadata": { 98 | "collapsed": false, 99 | "jupyter": { 100 | "outputs_hidden": false 101 | }, 102 | "pycharm": { 103 | "name": "#%%\n" 104 | } 105 | } 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "source": [ 110 | "## Exercise 3" 111 | ], 112 | "metadata": {} 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "source": [ 118 | "date_1 = dt.datetime(1999, 2, 28)\r\n", 119 | "\r\n", 120 | "date_2 = dt.datetime(1999, 3, 1)\r\n", 121 | "\r\n", 122 | "day = dt.timedelta(1)\r\n", 123 | "week = dt.timedelta(1)\r\n", 124 | "year = dt.timedelta(365)" 125 | ], 126 | "outputs": [], 127 | "metadata": { 128 | "collapsed": false, 129 | "jupyter": { 130 | "outputs_hidden": false 131 | }, 132 | "pycharm": { 133 | "name": "#%%\n" 134 | } 135 | } 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "source": [ 141 | "date_1 + day" 142 | ], 143 | "outputs": [], 144 | "metadata": { 145 | "collapsed": false, 146 | "jupyter": { 147 | "outputs_hidden": false 148 | }, 149 | "pycharm": { 150 | "name": "#%%\n" 151 | } 152 | } 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "source": [ 158 | "date_1 + week" 159 | ], 160 | "outputs": [], 161 | "metadata": { 162 | "collapsed": false, 163 | "jupyter": { 164 | "outputs_hidden": false 165 | }, 166 | "pycharm": { 167 | "name": "#%%\n" 168 | } 169 | } 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "source": [ 175 | "date_1 + year" 176 | ], 177 | "outputs": [], 178 | "metadata": { 179 | "collapsed": false, 180 | "jupyter": { 181 | "outputs_hidden": false 182 | }, 183 | "pycharm": { 184 | "name": "#%%\n" 185 | } 186 | } 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": null, 191 | "source": [ 192 | "date_2 + day" 193 | ], 194 | "outputs": [], 195 | "metadata": { 196 | "collapsed": false, 197 | "jupyter": { 198 | "outputs_hidden": false 199 | }, 200 | "pycharm": { 201 | "name": "#%%\n" 202 | } 203 | } 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": null, 208 | "source": [ 209 | "date_2 + week" 210 | ], 211 | "outputs": [], 212 | "metadata": { 213 | "collapsed": false, 214 | "jupyter": { 215 | "outputs_hidden": false 216 | }, 217 | "pycharm": { 218 | "name": "#%%\n" 219 | } 220 | } 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "source": [ 225 | "This date is not a year away due the leap year." 226 | ], 227 | "metadata": { 228 | "pycharm": { 229 | "name": "#%% md\n" 230 | } 231 | } 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "source": [ 236 | "## Exercise 4" 237 | ], 238 | "metadata": { 239 | "pycharm": { 240 | "name": "#%% md\n" 241 | } 242 | } 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": null, 247 | "source": [ 248 | "now.strftime(\"%d-%m-%Y\")" 249 | ], 250 | "outputs": [], 251 | "metadata": { 252 | "collapsed": false, 253 | "jupyter": { 254 | "outputs_hidden": false 255 | }, 256 | "pycharm": { 257 | "name": "#%%\n" 258 | } 259 | } 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "source": [ 265 | "now.strftime(\"%Y/%m/%d\")" 266 | ], 267 | "outputs": [], 268 | "metadata": { 269 | "collapsed": false, 270 | "jupyter": { 271 | "outputs_hidden": false 272 | }, 273 | "pycharm": { 274 | "name": "#%%\n" 275 | } 276 | } 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "source": [ 282 | "# (c) HH:MM:SS LongMonthName dd, yyyy\n", 283 | "now.strftime(\"%H:%M:%S %B %d, %Y\")" 284 | ], 285 | "outputs": [], 286 | "metadata": { 287 | "collapsed": false, 288 | "jupyter": { 289 | "outputs_hidden": false 290 | }, 291 | "pycharm": { 292 | "name": "#%%\n" 293 | } 294 | } 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "source": [ 299 | "## Exercise 5" 300 | ], 301 | "metadata": { 302 | "pycharm": { 303 | "name": "#%% md\n" 304 | } 305 | } 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "source": [ 311 | "from numpy import datetime64, timedelta64\n", 312 | "\n", 313 | "now64 = datetime64(dt.datetime.now())\n", 314 | "now64" 315 | ], 316 | "outputs": [], 317 | "metadata": { 318 | "collapsed": false, 319 | "jupyter": { 320 | "outputs_hidden": false 321 | }, 322 | "pycharm": { 323 | "name": "#%%\n" 324 | } 325 | } 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": null, 330 | "source": [ 331 | "day = timedelta64(1, \"D\")\n", 332 | "week = timedelta64(1, \"W\")\n", 333 | "year = timedelta64(1, \"Y\")" 334 | ], 335 | "outputs": [], 336 | "metadata": { 337 | "collapsed": false, 338 | "jupyter": { 339 | "outputs_hidden": false 340 | }, 341 | "pycharm": { 342 | "name": "#%%\n" 343 | } 344 | } 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": null, 349 | "source": [ 350 | "now64 + day" 351 | ], 352 | "outputs": [], 353 | "metadata": { 354 | "collapsed": false, 355 | "jupyter": { 356 | "outputs_hidden": false 357 | }, 358 | "pycharm": { 359 | "name": "#%%\n" 360 | } 361 | } 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": null, 366 | "source": [ 367 | "now64 + week" 368 | ], 369 | "outputs": [], 370 | "metadata": { 371 | "collapsed": false, 372 | "jupyter": { 373 | "outputs_hidden": false 374 | }, 375 | "pycharm": { 376 | "name": "#%%\n" 377 | } 378 | } 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": null, 383 | "source": [ 384 | "now64 + timedelta64(365, \"D\")" 385 | ], 386 | "outputs": [], 387 | "metadata": { 388 | "collapsed": false, 389 | "jupyter": { 390 | "outputs_hidden": false 391 | }, 392 | "pycharm": { 393 | "name": "#%%\n" 394 | } 395 | } 396 | }, 397 | { 398 | "cell_type": "markdown", 399 | "source": [ 400 | "This final cell is an intentional error. NumPy understands that a year is not always well defined." 401 | ], 402 | "metadata": { 403 | "pycharm": { 404 | "name": "#%% md\n" 405 | } 406 | } 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": null, 411 | "source": [ 412 | "now64 + year" 413 | ], 414 | "outputs": [], 415 | "metadata": { 416 | "collapsed": false, 417 | "jupyter": { 418 | "outputs_hidden": false 419 | }, 420 | "pycharm": { 421 | "name": "#%%\n" 422 | } 423 | } 424 | }, 425 | { 426 | "cell_type": "markdown", 427 | "source": [ 428 | "String conversion is more limited" 429 | ], 430 | "metadata": { 431 | "pycharm": { 432 | "name": "#%% md\n" 433 | } 434 | } 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": null, 439 | "source": [ 440 | "import numpy as np\n", 441 | "\n", 442 | "np.datetime_as_string(now64)" 443 | ], 444 | "outputs": [], 445 | "metadata": { 446 | "collapsed": false, 447 | "jupyter": { 448 | "outputs_hidden": false 449 | }, 450 | "pycharm": { 451 | "name": "#%%\n" 452 | } 453 | } 454 | } 455 | ], 456 | "metadata": { 457 | "kernelspec": { 458 | "display_name": "Python 3 (ipykernel)", 459 | "language": "python", 460 | "name": "python3" 461 | }, 462 | "language_info": { 463 | "codemirror_mode": { 464 | "name": "ipython", 465 | "version": 3 466 | }, 467 | "file_extension": ".py", 468 | "mimetype": "text/x-python", 469 | "name": "python", 470 | "nbconvert_exporter": "python", 471 | "pygments_lexer": "ipython3", 472 | "version": "3.9.6" 473 | } 474 | }, 475 | "nbformat": 4, 476 | "nbformat_minor": 4 477 | } -------------------------------------------------------------------------------- /solutions/chapter15/graphics_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "source": [ 6 | "# Graphics." 7 | ], 8 | "metadata": {} 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "source": [ 14 | "import matplotlib.dates as mdates\r\n", 15 | "import matplotlib.pyplot as plt\r\n", 16 | "import pandas as pd\r\n", 17 | "from numpy import (\r\n", 18 | " array,\r\n", 19 | " column_stack,\r\n", 20 | " dot,\r\n", 21 | " hstack,\r\n", 22 | " logical_and,\r\n", 23 | " mean,\r\n", 24 | " ones,\r\n", 25 | " sqrt,\r\n", 26 | " squeeze,\r\n", 27 | " var,\r\n", 28 | " zeros,\r\n", 29 | ")\r\n", 30 | "from numpy.linalg import lstsq\r\n", 31 | "\r\n", 32 | "# Loading and transforming the data\r\n", 33 | "sp500_data = pd.read_csv(\"sp500.csv\", index_col=\"Date\", parse_dates=True)\r\n", 34 | "ftse_data = pd.read_csv(\"ftse.csv\", index_col=\"Date\", parse_dates=True)\r\n", 35 | "sp500 = sp500_data[\"Adj Close\"]\r\n", 36 | "sp500_dates = sp500_data.index\r\n", 37 | "ftse = ftse_data[\"Adj Close\"]\r\n", 38 | "ftse_dates = ftse_data.index" 39 | ], 40 | "outputs": [], 41 | "metadata": { 42 | "collapsed": false, 43 | "jupyter": { 44 | "outputs_hidden": false 45 | }, 46 | "pycharm": { 47 | "name": "#%%\n" 48 | } 49 | } 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "source": [ 54 | "## Exercise 1\n" 55 | ], 56 | "metadata": {} 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "source": [ 62 | "fig = plt.figure()\r\n", 63 | "ax = fig.add_subplot(111)\r\n", 64 | "ax.plot(sp500_dates, sp500)\r\n", 65 | "fmt = mdates.DateFormatter(\"%Y\")\r\n", 66 | "ax.xaxis.set_major_formatter(fmt)\r\n", 67 | "fig.autofmt_xdate()\r\n", 68 | "plt.draw()" 69 | ], 70 | "outputs": [], 71 | "metadata": {} 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "source": [ 76 | "## Exercise 2\n" 77 | ], 78 | "metadata": {} 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "source": [ 84 | "sp500_rets = sp500.resample(\"W-FRI\").last().pct_change().dropna()\n", 85 | "ftse_rets = ftse.resample(\"W-FRI\").last().pct_change().dropna()\n", 86 | "fig = plt.figure()\n", 87 | "ax = fig.add_subplot(111)\n", 88 | "ax.hist(sp500_rets)\n", 89 | "plt.draw()\n", 90 | "fig = plt.figure()\n", 91 | "ax = fig.add_subplot(111)\n", 92 | "ax.hist(sp500_rets, bins=20)\n", 93 | "plt.draw()" 94 | ], 95 | "outputs": [], 96 | "metadata": {} 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "source": [ 101 | "## Exercise 3\n" 102 | ], 103 | "metadata": {} 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "source": [ 109 | "bands = [(-1.0, -0.02), (-0.02, 0.0), (0.0, 0.02), (0.02, 1.0)]\n", 110 | "percs = zeros(len(bands))\n", 111 | "i = 0\n", 112 | "for b in bands:\n", 113 | " percs[i] = mean(logical_and(sp500_rets > b[0], sp500_rets <= b[1]))\n", 114 | " i += 1\n", 115 | "fig = plt.figure()\n", 116 | "ax = fig.add_subplot(111)\n", 117 | "labels = [\"<-2%\", \">-2% and <0\", \">0 and <2%\", \">2%\"]\n", 118 | "ax.pie(percs, labels=labels)\n", 119 | "plt.draw()" 120 | ], 121 | "outputs": [], 122 | "metadata": {} 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "source": [ 127 | "## Exercise 4\n" 128 | ], 129 | "metadata": {} 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "source": [ 135 | "fig = plt.figure()\n", 136 | "ax = fig.add_subplot(111)\n", 137 | "ax.scatter(sp500_rets, ftse_rets)\n", 138 | "ax.set_xlabel(\"S&P 500 returns\")\n", 139 | "ax.set_ylabel(\"ftse 100 returns\")\n", 140 | "plt.draw()" 141 | ], 142 | "outputs": [], 143 | "metadata": {} 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "source": [ 148 | "## Exercise 5\n" 149 | ], 150 | "metadata": {} 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "source": [ 156 | "x = column_stack((ones(sp500_rets.shape), sp500_rets))\n", 157 | "y = column_stack((ftse_rets,))\n", 158 | "x, y\n", 159 | "out = lstsq(x, y, rcond=None)\n", 160 | "b = out[0]\n", 161 | "fig = plt.figure()\n", 162 | "ax = fig.add_subplot(111)\n", 163 | "ax.scatter(sp500_rets, ftse_rets)\n", 164 | "ax.set_xlabel(\"S&P 500 returns\")\n", 165 | "ax.set_ylabel(\"FTSE 100 returns\")\n", 166 | "x = ax.get_xlim()\n", 167 | "x = array(x)\n", 168 | "x.shape = -1, 1\n", 169 | "x = hstack((ones(x.shape), x))\n", 170 | "fit = dot(x, b)\n", 171 | "b = squeeze(b)\n", 172 | "ax.plot(x[:, 1], fit, color=\"#800000\")\n", 173 | "ax.text(0, -0.15, f\"y = {b[0]:0.2f} + {b[1]:0.2f}x\")\n", 174 | "plt.draw()" 175 | ], 176 | "outputs": [], 177 | "metadata": {} 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "source": [ 182 | "## Exercise 6\n" 183 | ], 184 | "metadata": {} 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "source": [ 190 | "T = sp500_rets.size\n", 191 | "ewma = zeros((T, 2))\n", 192 | "r = column_stack((sp500_rets, ftse_rets))\n", 193 | "ewma[0] = var(r, axis=0)\n", 194 | "for i in range(1, T):\n", 195 | " ewma[i] = 0.97 * ewma[i - 1] + 0.03 * r[i - 1] ** 2\n", 196 | "ewma = 100 * sqrt(252 * ewma)\n", 197 | "fig = plt.figure()\n", 198 | "ax = fig.add_subplot(111)\n", 199 | "ax.plot(sp500_rets.index, ewma[:, 0], label=\"S&P 500 ewma Vol (Annualized)\")\n", 200 | "ax.plot(ftse_rets.index, ewma[:, 1], label=\"ftse 100 ewma Vol (Annualized)\")\n", 201 | "ax.legend(loc=0)\n", 202 | "ax.set_title(\"Annualized Volatility (%)\")\n", 203 | "fmt = mdates.DateFormatter(\"%Y\")\n", 204 | "ax.xaxis.set_major_formatter(fmt)\n", 205 | "fig.autofmt_xdate()\n", 206 | "plt.show()" 207 | ], 208 | "outputs": [], 209 | "metadata": {} 210 | } 211 | ], 212 | "metadata": { 213 | "kernelspec": { 214 | "display_name": "Python 3 (ipykernel)", 215 | "language": "python", 216 | "name": "python3" 217 | }, 218 | "language_info": { 219 | "codemirror_mode": { 220 | "name": "ipython", 221 | "version": 3 222 | }, 223 | "file_extension": ".py", 224 | "mimetype": "text/x-python", 225 | "name": "python", 226 | "nbconvert_exporter": "python", 227 | "pygments_lexer": "ipython3", 228 | "version": "3.9.6" 229 | } 230 | }, 231 | "nbformat": 4, 232 | "nbformat_minor": 4 233 | } -------------------------------------------------------------------------------- /solutions/chapter17/no-exercises-in-chapter.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/python-for-econometrics-statistics-data-analysis/0e86efe86357ea344ec765b9b201fb8867e06566/solutions/chapter17/no-exercises-in-chapter.txt -------------------------------------------------------------------------------- /solutions/chapter18/custom_functions_and_modules_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Custom Functions and Modules" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false, 15 | "jupyter": { 16 | "outputs_hidden": false 17 | }, 18 | "pycharm": { 19 | "name": "#%%\n" 20 | } 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "import numpy as np\n", 25 | "from numpy import (\n", 26 | " asarray,\n", 27 | " corrcoef,\n", 28 | " diag,\n", 29 | " eye,\n", 30 | " hstack,\n", 31 | " logical_and,\n", 32 | " logical_not,\n", 33 | " r_,\n", 34 | " sort,\n", 35 | " unique,\n", 36 | " zeros,\n", 37 | ")\n", 38 | "from numpy.linalg import inv\n", 39 | "from numpy.random import chisquare, standard_normal" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "## Exercise 1\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "def ascategory(x):\n", 56 | " \"\"\" \"\"\"\n", 57 | " x = asarray(x)\n", 58 | " t = x.shape[0]\n", 59 | " ux = unique(x)\n", 60 | " k = ux.shape[0]\n", 61 | " categories = np.zeros((t, k))\n", 62 | " for i in range(k):\n", 63 | " loc = np.squeeze(x == ux[i])\n", 64 | " categories[loc, i] = 1.0\n", 65 | " return categories" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": { 72 | "collapsed": false, 73 | "jupyter": { 74 | "outputs_hidden": false 75 | }, 76 | "pycharm": { 77 | "name": "#%%\n" 78 | } 79 | }, 80 | "outputs": [], 81 | "source": [ 82 | "ascategory([\"a\", \"b\", \"a\", \"c\", \"a\", \"b\"])" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "## Exercise 2\n" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "def gls(x, y, omega=None):\n", 99 | " \"\"\" \"\"\"\n", 100 | " t, k = x.shape\n", 101 | " if omega is None:\n", 102 | " omega = eye(t)\n", 103 | " omega_inv = inv(omega)\n", 104 | " xpx = x.T @ omega_inv @ x\n", 105 | " xpy = x.T @ omega_inv @ y\n", 106 | " beta_gls = asarray(inv(xpx) @ xpy)\n", 107 | " return beta_gls" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": { 114 | "collapsed": false, 115 | "jupyter": { 116 | "outputs_hidden": false 117 | }, 118 | "pycharm": { 119 | "name": "#%%\n" 120 | } 121 | }, 122 | "outputs": [], 123 | "source": [ 124 | "x = standard_normal((100, 3))\n", 125 | "y = standard_normal((100, 1))\n", 126 | "gls(x, y)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": { 133 | "collapsed": false, 134 | "jupyter": { 135 | "outputs_hidden": false 136 | }, 137 | "pycharm": { 138 | "name": "#%%\n" 139 | } 140 | }, 141 | "outputs": [], 142 | "source": [ 143 | "omega = np.diag(chisquare(5, size=100))\n", 144 | "omega" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": { 151 | "collapsed": false, 152 | "jupyter": { 153 | "outputs_hidden": false 154 | }, 155 | "pycharm": { 156 | "name": "#%%\n" 157 | } 158 | }, 159 | "outputs": [], 160 | "source": [ 161 | "gls(x, y, omega)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "## Exercise 3\n" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "def partial_corr(x, y=None, quantile=0.5, tail=\"Lower\"):\n", 178 | " \"\"\" \"\"\"\n", 179 | " if y is not None:\n", 180 | " X = x.view()\n", 181 | " Y = y.view()\n", 182 | " T = X.shape[0]\n", 183 | " X.shape = T, 1\n", 184 | " Y.shape = T, 1\n", 185 | " z = hstack((X, Y))\n", 186 | " else:\n", 187 | " z = x\n", 188 | " T, K = z.shape\n", 189 | " corr = eye(K)\n", 190 | " count = zeros((K, K))\n", 191 | " ind = zeros((T, K), dtype=np.bool)\n", 192 | " for i in range(K):\n", 193 | " temp = sort(z[:, i].ravel())\n", 194 | " cutoff = int(round(quantile * T))\n", 195 | " threshold = temp[cutoff]\n", 196 | " ind[:, i] = z[:, i] < threshold\n", 197 | " if tail == \"Upper\":\n", 198 | " ind[:, i] = logical_not(ind[:, i])\n", 199 | " for i in range(K):\n", 200 | " for j in range(i + 1, K):\n", 201 | " pl = logical_and(ind[:, i], ind[:, j])\n", 202 | " count[i, j] = sum(pl)\n", 203 | " count[j, i] = count[i, j]\n", 204 | " if sum(pl) > 1:\n", 205 | " w = z[pl, :]\n", 206 | " w = w[:, r_[i, j]]\n", 207 | " corr[i, j] = corrcoef(w.T)[0, 1]\n", 208 | " corr[j, i] = corr[i, j]\n", 209 | " else:\n", 210 | " corr[i, j] = np.nan\n", 211 | " corr[j, i] = np.nan\n", 212 | " return corr, count" 213 | ] 214 | } 215 | ], 216 | "metadata": { 217 | "kernelspec": { 218 | "display_name": "Python 3 (ipykernel)", 219 | "language": "python", 220 | "name": "python3" 221 | }, 222 | "language_info": { 223 | "codemirror_mode": { 224 | "name": "ipython", 225 | "version": 3 226 | }, 227 | "file_extension": ".py", 228 | "mimetype": "text/x-python", 229 | "name": "python", 230 | "nbconvert_exporter": "python", 231 | "pygments_lexer": "ipython3", 232 | "version": "3.9.6" 233 | } 234 | }, 235 | "nbformat": 4, 236 | "nbformat_minor": 4 237 | } 238 | -------------------------------------------------------------------------------- /solutions/chapter19/probability_and_statistics_functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Probability and Statistics" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## imports" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": { 21 | "jupyter": { 22 | "outputs_hidden": false 23 | } 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "import matplotlib.pyplot as plt\n", 28 | "import numpy as np\n", 29 | "import scipy.stats as stats\n", 30 | "import seaborn\n", 31 | "from numpy.random import gamma, lognormal, normal, randn, uniform" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "## Exercise 1\n" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "a = normal(0, 1, size=(1000,))\n", 48 | "b = normal(3, 3, size=(1000,))\n", 49 | "c = uniform(0, 1, size=(1000,))\n", 50 | "d = uniform(-1, 1, size=(1000,))\n", 51 | "e = gamma(1, 2, size=(1000,))\n", 52 | "f = lognormal(0.08, 0.2, size=(1000,))" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "## Exercise 2\n" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": { 66 | "jupyter": { 67 | "outputs_hidden": false 68 | } 69 | }, 70 | "outputs": [], 71 | "source": [ 72 | "stats.kstest(a, stats.norm(loc=0, scale=1).cdf)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": { 79 | "jupyter": { 80 | "outputs_hidden": false 81 | }, 82 | "pycharm": { 83 | "name": "#%%\n" 84 | } 85 | }, 86 | "outputs": [], 87 | "source": [ 88 | "stats.kstest(b, stats.norm(loc=3, scale=3).cdf)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "jupyter": { 96 | "outputs_hidden": false 97 | }, 98 | "pycharm": { 99 | "name": "#%%\n" 100 | } 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "stats.kstest(c, stats.uniform(loc=0, scale=1).cdf)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": { 111 | "jupyter": { 112 | "outputs_hidden": false 113 | }, 114 | "pycharm": { 115 | "name": "#%%\n" 116 | } 117 | }, 118 | "outputs": [], 119 | "source": [ 120 | "stats.kstest(d, stats.uniform(loc=-1, scale=2).cdf)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": { 127 | "jupyter": { 128 | "outputs_hidden": false 129 | }, 130 | "pycharm": { 131 | "name": "#%%\n" 132 | } 133 | }, 134 | "outputs": [], 135 | "source": [ 136 | "stats.kstest(e, stats.gamma(1, scale=2).cdf)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "## Exercise 3\n" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": { 150 | "jupyter": { 151 | "outputs_hidden": false 152 | }, 153 | "pycharm": { 154 | "name": "#%%\n" 155 | } 156 | }, 157 | "outputs": [], 158 | "source": [ 159 | "x = randn(1)\n", 160 | "x" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": { 167 | "jupyter": { 168 | "outputs_hidden": false 169 | }, 170 | "pycharm": { 171 | "name": "#%%\n" 172 | } 173 | }, 174 | "outputs": [], 175 | "source": [ 176 | "print(\"np.random.seed():\")\n", 177 | "np.random.seed()" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": { 184 | "jupyter": { 185 | "outputs_hidden": false 186 | }, 187 | "pycharm": { 188 | "name": "#%%\n" 189 | } 190 | }, 191 | "outputs": [], 192 | "source": [ 193 | "x = randn(1)\n", 194 | "x" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "## Exercise 4\n" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "metadata": { 208 | "jupyter": { 209 | "outputs_hidden": false 210 | } 211 | }, 212 | "outputs": [], 213 | "source": [ 214 | "state = np.random.get_state()\n", 215 | "x = randn(2, 1)\n", 216 | "print(\"x:\")\n", 217 | "x" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": { 224 | "jupyter": { 225 | "outputs_hidden": false 226 | }, 227 | "pycharm": { 228 | "name": "#%%\n" 229 | } 230 | }, 231 | "outputs": [], 232 | "source": [ 233 | "print(\"np.random.set_state(state):\")\n", 234 | "np.random.set_state(state)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": { 241 | "jupyter": { 242 | "outputs_hidden": false 243 | }, 244 | "pycharm": { 245 | "name": "#%%\n" 246 | } 247 | }, 248 | "outputs": [], 249 | "source": [ 250 | "x = randn(2, 1)\n", 251 | "print(\"x:\")\n", 252 | "x" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "## Exercise 5\n" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "metadata": { 266 | "jupyter": { 267 | "outputs_hidden": false 268 | } 269 | }, 270 | "outputs": [], 271 | "source": [ 272 | "def summary_stats(x):\n", 273 | " return x.mean(), x.std(), stats.skew(x), stats.kurtosis(x) + 3\n", 274 | "\n", 275 | "\n", 276 | "x = randn(100)\n", 277 | "print(\"summary_stats(x):\")\n", 278 | "summary_stats(x)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "## Exercise 6\n" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": null, 291 | "metadata": { 292 | "jupyter": { 293 | "outputs_hidden": false 294 | } 295 | }, 296 | "outputs": [], 297 | "source": [ 298 | "x = stats.multivariate_normal(cov=[[1, -0.5], [-0.5, 1]]).rvs(100)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": null, 304 | "metadata": { 305 | "jupyter": { 306 | "outputs_hidden": false 307 | }, 308 | "pycharm": { 309 | "name": "#%%\n" 310 | } 311 | }, 312 | "outputs": [], 313 | "source": [ 314 | "print(\"stats.pearsonr(x[:,0],x[:,1]):\")\n", 315 | "stats.pearsonr(x[:, 0], x[:, 1])" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": null, 321 | "metadata": { 322 | "jupyter": { 323 | "outputs_hidden": false 324 | }, 325 | "pycharm": { 326 | "name": "#%%\n" 327 | } 328 | }, 329 | "outputs": [], 330 | "source": [ 331 | "print(\"stats.spearmanr(x[:,0],x[:,1]):\")\n", 332 | "stats.spearmanr(x[:, 0], x[:, 1])" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "metadata": { 339 | "jupyter": { 340 | "outputs_hidden": false 341 | }, 342 | "pycharm": { 343 | "name": "#%%\n" 344 | } 345 | }, 346 | "outputs": [], 347 | "source": [ 348 | "print(\"stats.kendalltau(x[:,0],x[:,1]):\")\n", 349 | "stats.kendalltau(x[:, 0], x[:, 1])" 350 | ] 351 | }, 352 | { 353 | "cell_type": "markdown", 354 | "metadata": {}, 355 | "source": [ 356 | "## Exercise 7\n" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "metadata": { 363 | "jupyter": { 364 | "outputs_hidden": false 365 | } 366 | }, 367 | "outputs": [], 368 | "source": [ 369 | "gam = stats.gamma(1, scale=2)\n", 370 | "print(\"gam.median():\")\n", 371 | "gam.median()" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": null, 377 | "metadata": { 378 | "jupyter": { 379 | "outputs_hidden": false 380 | }, 381 | "pycharm": { 382 | "name": "#%%\n" 383 | } 384 | }, 385 | "outputs": [], 386 | "source": [ 387 | "print(\"np.median(gam.rvs(10000)):\")\n", 388 | "np.median(gam.rvs(10000))" 389 | ] 390 | }, 391 | { 392 | "cell_type": "markdown", 393 | "metadata": {}, 394 | "source": [ 395 | "## Exercise 8\n" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": null, 401 | "metadata": { 402 | "jupyter": { 403 | "outputs_hidden": false 404 | } 405 | }, 406 | "outputs": [], 407 | "source": [ 408 | "x = np.linspace(0, 1 - (1.0 / 1000), 1000) + 1.0 / 2000\n", 409 | "plt.figure(1)\n", 410 | "u = np.sort(stats.norm(loc=0, scale=1).cdf(a))\n", 411 | "plt.plot(x, u, x, x)\n", 412 | "plt.figure(2)\n", 413 | "u = np.sort(stats.norm(loc=3, scale=3).cdf(b))\n", 414 | "plt.plot(x, u, x, x)\n", 415 | "plt.figure(3)\n", 416 | "u = np.sort(stats.uniform(loc=0, scale=1).cdf(c))\n", 417 | "plt.plot(x, u, x, x)\n", 418 | "plt.figure(4)\n", 419 | "u = np.sort(stats.uniform(loc=-1, scale=2).cdf(d))\n", 420 | "plt.plot(x, u, x, x)\n", 421 | "plt.figure(5)\n", 422 | "u = np.sort(stats.gamma(1, scale=2).cdf(e))\n", 423 | "plt.plot(x, u, x, x)\n", 424 | "plt.show()" 425 | ] 426 | } 427 | ], 428 | "metadata": { 429 | "kernelspec": { 430 | "display_name": "Python 3 (ipykernel)", 431 | "language": "python", 432 | "name": "python3" 433 | }, 434 | "language_info": { 435 | "codemirror_mode": { 436 | "name": "ipython", 437 | "version": 3 438 | }, 439 | "file_extension": ".py", 440 | "mimetype": "text/x-python", 441 | "name": "python", 442 | "nbconvert_exporter": "python", 443 | "pygments_lexer": "ipython3", 444 | "version": "3.8.11" 445 | } 446 | }, 447 | "nbformat": 4, 448 | "nbformat_minor": 4 449 | } 450 | -------------------------------------------------------------------------------- /solutions/chapter21/optimization_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Chapter 20 Non-linear Function Optimization" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false, 15 | "jupyter": { 16 | "outputs_hidden": false 17 | }, 18 | "pycharm": { 19 | "name": "#%%\n" 20 | } 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "from numpy import array, dot, finfo, log, mean, pi, sqrt, squeeze, var, zeros\n", 25 | "from numpy.linalg import lstsq\n", 26 | "from numpy.random import randn\n", 27 | "from scipy.optimize import fmin, fmin_bfgs, fmin_slsqp\n", 28 | "\n", 29 | "\n", 30 | "# Negative Log-likellihood\n", 31 | "def normal_loglik(mu, x):\n", 32 | " n = x.size\n", 33 | " epe = dot(x - mu, x - mu)\n", 34 | " sigma2 = epe / n\n", 35 | " return 0.5 * (n * log(2 * pi) + n * log(sigma2) + epe / sigma2)\n", 36 | "\n", 37 | "\n", 38 | "# Negative Log-likellihood 2\n", 39 | "def normal_loglik2(p, x):\n", 40 | " mu = p[0]\n", 41 | " sigma2 = p[1]\n", 42 | " if sigma2 < 0:\n", 43 | " sigma2 = finfo(float).tiny\n", 44 | " epe = dot(x - mu, x - mu)\n", 45 | " n = x.size\n", 46 | " return 0.5 * (n * log(2 * pi) + n * log(sigma2) + epe / sigma2)\n", 47 | "\n", 48 | "\n", 49 | "# Negative Log-likellihood 3\n", 50 | "def normal_loglik3(p, x):\n", 51 | " mu = p[0]\n", 52 | " sigma2 = p[1] ** 2\n", 53 | " epe = dot(x - mu, x - mu)\n", 54 | " n = x.size\n", 55 | " return 0.5 * (n * log(2 * pi) + n * log(sigma2) + epe / sigma2)\n", 56 | "\n", 57 | "\n", 58 | "# Negative Log-likellihood 4\n", 59 | "def ols_loglik(b, y, X):\n", 60 | " bv = b.view()\n", 61 | " K = X.shape[1]\n", 62 | " bv.shape = K, 1\n", 63 | " e = y - dot(X, bv)\n", 64 | " n = e.size\n", 65 | " epe = dot(e.T, e)\n", 66 | " sigma2 = epe / n\n", 67 | " return squeeze(0.5 * (n * log(2 * pi) + n * log(sigma2) + epe / sigma2))\n", 68 | "\n", 69 | "\n", 70 | "# Constraint function\n", 71 | "def constraint(p, x):\n", 72 | " return array([1, p[1] - 0.0001])" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "## Exercise 1 \n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "x = randn(1000) + 2\n", 89 | "mu0 = mean(x) + 1\n", 90 | "args = (x,)\n", 91 | "mu = fmin_bfgs(normal_loglik, mu0, args=args)\n", 92 | "print(\"mu:\")\n", 93 | "print(mu)\n", 94 | "print(\"mean(x):\")\n", 95 | "print(mean(x))\n", 96 | "mu = fmin(normal_loglik, mu0, args=args)\n", 97 | "print(\"mu:\")\n", 98 | "print(mu)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "## Exercise 2\n" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "p0 = zeros(2)\n", 115 | "p0[0] = mean(x) + 1\n", 116 | "p0[1] = var(x) + 2\n", 117 | "print(\"normal_loglik2(p0,x):\")\n", 118 | "print(normal_loglik2(p0, x))\n", 119 | "p = fmin_slsqp(normal_loglik2, p0, args=args, bounds=[(-100, 100), (0.000001, 100)])\n", 120 | "print(\"array([mean(x),var(x)]):\")\n", 121 | "print(array([mean(x), var(x)]))\n", 122 | "print(\"p:\")\n", 123 | "print(p)\n", 124 | "p = fmin_slsqp(normal_loglik2, p0, args=args, f_ieqcons=constraint)\n", 125 | "print(\"p:\")\n", 126 | "print(p)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": { 132 | "pycharm": { 133 | "name": "#%% md\n" 134 | } 135 | }, 136 | "source": [ 137 | "## Exercise 3\n" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": { 144 | "collapsed": false, 145 | "jupyter": { 146 | "outputs_hidden": false 147 | }, 148 | "pycharm": { 149 | "name": "#%%\n" 150 | } 151 | }, 152 | "outputs": [], 153 | "source": [ 154 | "from scipy.optimize import minimize\n", 155 | "\n", 156 | "args = (x,)\n", 157 | "p = minimize(normal_loglik2, p0, args=args, bounds=[(-100, 100), (0.000001, 100)])\n", 158 | "print(\"array([mean(x),var(x)]):\")\n", 159 | "print(array([mean(x), var(x)]))\n", 160 | "print(\"p:\")\n", 161 | "print(p)\n", 162 | "minimize_cons = {\"type\": \"ineq\", \"fun\": lambda p: p[1] - 0.0001}\n", 163 | "p = minimize(normal_loglik2, p0, args=args, constraints=minimize_cons)\n", 164 | "print(\"p:\")\n", 165 | "print(p)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "## Exercise 4\n" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "p0[1] = sqrt(p0[1])\n", 182 | "p = fmin_bfgs(normal_loglik3, p0, args=args)\n", 183 | "p[1] = p[1] ** 2\n", 184 | "print(\"p:\")\n", 185 | "print(p)" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "## Exercise 5\n" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "b = array([[1.0], [2]])\n", 202 | "X = randn(1000, 2)\n", 203 | "y = dot(X, b) + randn(1000, 1)\n", 204 | "b0 = lstsq(X, y, rcond=None)\n", 205 | "b0 = b0[0]\n", 206 | "print(\"b0:\")\n", 207 | "print(b0)\n", 208 | "args = (y, X)\n", 209 | "print(\"ols_loglik(b0.ravel(),y,X):\")\n", 210 | "print(ols_loglik(b0.ravel(), y, X))\n", 211 | "b = fmin_slsqp(ols_loglik, b0.ravel(), args=args)\n", 212 | "print(\"b:\")\n", 213 | "print(b)" 214 | ] 215 | } 216 | ], 217 | "metadata": { 218 | "kernelspec": { 219 | "display_name": "Python 3 (ipykernel)", 220 | "language": "python", 221 | "name": "python3" 222 | }, 223 | "language_info": { 224 | "codemirror_mode": { 225 | "name": "ipython", 226 | "version": 3 227 | }, 228 | "file_extension": ".py", 229 | "mimetype": "text/x-python", 230 | "name": "python", 231 | "nbconvert_exporter": "python", 232 | "pygments_lexer": "ipython3", 233 | "version": "3.9.7" 234 | } 235 | }, 236 | "nbformat": 4, 237 | "nbformat_minor": 4 238 | } 239 | -------------------------------------------------------------------------------- /solutions/chapter22/string_manipulation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "pycharm": { 7 | "name": "#%% md\n" 8 | } 9 | }, 10 | "source": [ 11 | "# String Manipulation" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": { 17 | "pycharm": { 18 | "name": "#%% md\n" 19 | } 20 | }, 21 | "source": [ 22 | "## Exercise 1" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": { 29 | "collapsed": false, 30 | "jupyter": { 31 | "outputs_hidden": false 32 | }, 33 | "pycharm": { 34 | "name": "#%%\n" 35 | } 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "a = \"To\"\n", 40 | "b = \"Be\"\n", 41 | "c = \"Or\"\n", 42 | "d = \"Not\"" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": { 49 | "collapsed": false, 50 | "jupyter": { 51 | "outputs_hidden": false 52 | }, 53 | "pycharm": { 54 | "name": "#%%\n" 55 | } 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "a + \" \" + b + \" \" + c + \" \" + d + \" \" + b" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": { 66 | "collapsed": false, 67 | "jupyter": { 68 | "outputs_hidden": false 69 | }, 70 | "pycharm": { 71 | "name": "#%%\n" 72 | } 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "\" \".join((a, b, c, d, b))" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "## Exercise 2" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": { 90 | "collapsed": false, 91 | "jupyter": { 92 | "outputs_hidden": false 93 | }, 94 | "pycharm": { 95 | "name": "#%%\n" 96 | } 97 | }, 98 | "outputs": [], 99 | "source": [ 100 | "quote = \" \".join((a, b, c, d, b))\n", 101 | "quote.upper()" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": { 108 | "collapsed": false, 109 | "jupyter": { 110 | "outputs_hidden": false 111 | }, 112 | "pycharm": { 113 | "name": "#%%\n" 114 | } 115 | }, 116 | "outputs": [], 117 | "source": [ 118 | "quote.lower()" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": { 125 | "collapsed": false, 126 | "jupyter": { 127 | "outputs_hidden": false 128 | }, 129 | "pycharm": { 130 | "name": "#%%\n" 131 | } 132 | }, 133 | "outputs": [], 134 | "source": [ 135 | "quote" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": { 141 | "pycharm": { 142 | "name": "#%% md\n" 143 | } 144 | }, 145 | "source": [ 146 | "## Exercise 3" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": { 153 | "collapsed": false, 154 | "jupyter": { 155 | "outputs_hidden": false 156 | }, 157 | "pycharm": { 158 | "name": "#%%\n" 159 | } 160 | }, 161 | "outputs": [], 162 | "source": [ 163 | "csv = '''\\\n", 164 | "Bob,Silent,1 Pay Your Taxes St.,Holtsville, NY, 00501\n", 165 | "George,Curious,0 0th Ave.,New York, NY,10005\n", 166 | "\"Jay \"\"Party Time\"\"\",Grizwald,99 Main St.,Paris, TX,75324\n", 167 | "'''" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "metadata": { 174 | "collapsed": false, 175 | "jupyter": { 176 | "outputs_hidden": false 177 | }, 178 | "pycharm": { 179 | "name": "#%%\n" 180 | } 181 | }, 182 | "outputs": [], 183 | "source": [ 184 | "csv.split(\"\\n\")" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": { 191 | "collapsed": false, 192 | "jupyter": { 193 | "outputs_hidden": false 194 | }, 195 | "pycharm": { 196 | "name": "#%%\n" 197 | } 198 | }, 199 | "outputs": [], 200 | "source": [ 201 | "parts = csv.split(\"\\n\")\n", 202 | "for i in range(3):\n", 203 | " print(parts[i].split(\",\"))" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": { 209 | "pycharm": { 210 | "name": "#%% md\n" 211 | } 212 | }, 213 | "source": [ 214 | "Better to directly iterate on the tuple produced by `split`" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": { 221 | "collapsed": false, 222 | "jupyter": { 223 | "outputs_hidden": false 224 | }, 225 | "pycharm": { 226 | "name": "#%%\n" 227 | } 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "for row in csv.split(\"\\n\"):\n", 232 | " print(row.split(\",\"))" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": { 239 | "collapsed": false, 240 | "jupyter": { 241 | "outputs_hidden": false 242 | }, 243 | "pycharm": { 244 | "name": "#%%\n" 245 | } 246 | }, 247 | "outputs": [], 248 | "source": [ 249 | "for row in csv.split(\"\\n\"):\n", 250 | " zipcode = row.split(\",\")[-1]\n", 251 | " print(zipcode.strip())" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "## Exercise 4" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "metadata": { 265 | "collapsed": false, 266 | "jupyter": { 267 | "outputs_hidden": false 268 | }, 269 | "pycharm": { 270 | "name": "#%%\n" 271 | } 272 | }, 273 | "outputs": [], 274 | "source": [ 275 | "from numpy import exp, pi, sqrt\n", 276 | "\n", 277 | "print(f\"{pi:0.2f}\")\n", 278 | "print(f\"{pi:0.4f}\")\n", 279 | "print(f\"{pi:0.5e}\")\n", 280 | "print(f\"{pi:> 15.6f}\")\n", 281 | "\n", 282 | "e = exp(1)\n", 283 | "phi = (1 + sqrt(5)) / 2\n", 284 | "print(f\"{pi:>20.6f}{e:>20.6f}{phi:>20.6f}\")" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": { 290 | "pycharm": { 291 | "name": "#%% md\n" 292 | } 293 | }, 294 | "source": [ 295 | "## Exercise 5" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "metadata": { 302 | "collapsed": false, 303 | "jupyter": { 304 | "outputs_hidden": false 305 | }, 306 | "pycharm": { 307 | "name": "#%%\n" 308 | } 309 | }, 310 | "outputs": [], 311 | "source": [ 312 | "import re\n", 313 | "\n", 314 | "s = \"1,1.0,cherry,-4.321,horse,1.3+2.96j\"\n", 315 | "numbers = re.compile(\"[+-]?\\d+(?:\\.\\d+)?[+]?(\\d+)?(?:\\.\\d+)?[j]?\")\n", 316 | "values = []\n", 317 | "for v in numbers.finditer(s):\n", 318 | " val = v.group()\n", 319 | " print(val)\n", 320 | " values.append(val)" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "## Exercise 6" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": null, 333 | "metadata": { 334 | "collapsed": false, 335 | "jupyter": { 336 | "outputs_hidden": false 337 | }, 338 | "pycharm": { 339 | "name": "#%%\n" 340 | } 341 | }, 342 | "outputs": [], 343 | "source": [ 344 | "def conv(val):\n", 345 | " try:\n", 346 | " out = int(val)\n", 347 | " except:\n", 348 | " try:\n", 349 | " out = float(val)\n", 350 | " except:\n", 351 | " try:\n", 352 | " out = complex(val)\n", 353 | " except:\n", 354 | " out = val\n", 355 | " print(\"Not a number\")\n", 356 | " print(type(out))\n", 357 | " return out\n", 358 | "\n", 359 | "\n", 360 | "for val in s.split(\",\"):\n", 361 | " print(conv(val))" 362 | ] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": { 367 | "pycharm": { 368 | "name": "#%% md\n" 369 | } 370 | }, 371 | "source": [ 372 | "## Exercise 7" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": { 379 | "collapsed": false, 380 | "jupyter": { 381 | "outputs_hidden": false 382 | }, 383 | "pycharm": { 384 | "name": "#%%\n" 385 | } 386 | }, 387 | "outputs": [], 388 | "source": [ 389 | "example = \"\"\"\\\n", 390 | "John's favorite subject is econometrics. He always liked math\n", 391 | "and especially took to stats. His favorite teacher is Prof. Gauss,\n", 392 | "and his least favorite is Prof. Bayes. Sarah was stronger at\n", 393 | "physics than at statistics. She preferred to the purity of objects\n", 394 | "in a vacuum to causal inference. Of course, she was happy to\n", 395 | "apply statistics in her dissertation where she could measure\n", 396 | "the position of the falling feature to the nanometer, and so it\n", 397 | "was as if she had no measurement error.\n", 398 | "\"\"\"\n", 399 | "\n", 400 | "import re\n", 401 | "\n", 402 | "mod = re.sub(\"\\sHe|\\sShe\", \" Ze\", example)\n", 403 | "mod = re.sub(\"\\she|\\sshe\", \" ze\", mod)\n", 404 | "mod = re.sub(\"\\sHis|\\sHer\", \" Zir\", mod)\n", 405 | "mod = re.sub(\"\\shis|\\sher\", \" zir\", mod)\n", 406 | "print(mod)" 407 | ] 408 | } 409 | ], 410 | "metadata": { 411 | "kernelspec": { 412 | "display_name": "Python 3 (ipykernel)", 413 | "language": "python", 414 | "name": "python3" 415 | }, 416 | "language_info": { 417 | "codemirror_mode": { 418 | "name": "ipython", 419 | "version": 3 420 | }, 421 | "file_extension": ".py", 422 | "mimetype": "text/x-python", 423 | "name": "python", 424 | "nbconvert_exporter": "python", 425 | "pygments_lexer": "ipython3", 426 | "version": "3.9.6" 427 | } 428 | }, 429 | "nbformat": 4, 430 | "nbformat_minor": 4 431 | } 432 | -------------------------------------------------------------------------------- /solutions/chapter23/file_system_and_navigation_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# File System and Navigation" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false, 15 | "jupyter": { 16 | "outputs_hidden": false 17 | }, 18 | "pycharm": { 19 | "name": "#%%\n" 20 | } 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "import glob\n", 25 | "import os\n", 26 | "import shutil\n", 27 | "import zipfile as zf\n", 28 | "\n", 29 | "chapter_name = \"chapter_22\"\n", 30 | "# Initial cleanup\n", 31 | "try:\n", 32 | " shutil.rmtree(os.path.join(os.getcwd(), chapter_name))\n", 33 | "except:\n", 34 | " pass" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "## Exercise 1\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "print(\"Creating directory\")\n", 51 | "os.mkdir(chapter_name)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "## Exercise 2\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [ 67 | "print(\"Changing directory\")\n", 68 | "os.chdir(chapter_name)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "## Exercise 3\n" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "print(\"Creating a file\")\n", 85 | "f = open(\"tobedeleted.py\", \"w\")\n", 86 | "# Some content\n", 87 | "f.writelines([(str(i) + \"/n\") for i in range(10)])\n", 88 | "f.close()" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "## Exercise 4\n" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "print(\"Zipping file\")\n", 105 | "# Method 1\n", 106 | "cwd = os.getcwd()\n", 107 | "shutil.make_archive(\"tobedeleted\", \"zip\", cwd)\n", 108 | "# Method 2\n", 109 | "f = open(\"tobedeleted.py\")\n", 110 | "file_contents = f.read()\n", 111 | "z = zf.ZipFile(\"tobedeleted-2.zip\", mode=\"w\")\n", 112 | "z.writestr(\"tobedeleted.py\", file_contents)\n", 113 | "z.close()" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "## Exercise 5\n" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "print(\"Listing directory contents\")\n", 130 | "print(\"Directory: \" + os.getcwd())\n", 131 | "files = glob.glob(\"tobedeleted*\")\n", 132 | "for f in files:\n", 133 | " print(f)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "## Exercise 6\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "input(\"Press enter to delete the files and directory...\")\n", 150 | "for f in files:\n", 151 | " # Safety first!\n", 152 | " if f.find(\"tobedeleted\") >= 0:\n", 153 | " print(f\"Removing {f}\")\n", 154 | " os.unlink(f)\n", 155 | "os.chdir(\"..\")\n", 156 | "os.rmdir(chapter_name)" 157 | ] 158 | } 159 | ], 160 | "metadata": { 161 | "kernelspec": { 162 | "display_name": "Python 3 (ipykernel)", 163 | "language": "python", 164 | "name": "python3" 165 | }, 166 | "language_info": { 167 | "codemirror_mode": { 168 | "name": "ipython", 169 | "version": 3 170 | }, 171 | "file_extension": ".py", 172 | "mimetype": "text/x-python", 173 | "name": "python", 174 | "nbconvert_exporter": "python", 175 | "pygments_lexer": "ipython3", 176 | "version": "3.9.6" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 4 181 | } 182 | -------------------------------------------------------------------------------- /solutions/chapter25/improving_performance_using_numba.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "pycharm": { 7 | "name": "#%% md\n" 8 | } 9 | }, 10 | "source": [ 11 | "# Improving Performance using Numba" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "## Exercise 1\n", 19 | "\n", 20 | "The code uses loops and scalar access to compute the errors recursively." 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": { 27 | "collapsed": false, 28 | "jupyter": { 29 | "outputs_hidden": false 30 | }, 31 | "pycharm": { 32 | "name": "#%%\n" 33 | } 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "import numpy as np\n", 38 | "\n", 39 | "\n", 40 | "def arma(parameters, data, p=0, q=0):\n", 41 | " tau = data.shape[0]\n", 42 | " errors = np.zeros(tau)\n", 43 | "\n", 44 | " for t in range(p, tau):\n", 45 | " errors[t] = data[t] - parameters[0]\n", 46 | " for i in range(p):\n", 47 | " errors[t] -= parameters[i + 1] * data[t - i - 1]\n", 48 | " for i in range(q):\n", 49 | " if (t - i) >= 0:\n", 50 | " # If not, lagged error is assumed to be 0\n", 51 | " errors[t] -= parameters[i + p + 1] * errors[t - i - 1]\n", 52 | "\n", 53 | " return np.asarray(errors)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": false, 61 | "jupyter": { 62 | "outputs_hidden": false 63 | }, 64 | "pycharm": { 65 | "name": "#%%\n" 66 | } 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "data = np.random.standard_normal(1000)\n", 71 | "parameters = np.array([1, 0.1, 0.1, 0.4, -0.8])\n", 72 | "p = 2\n", 73 | "q = 2\n", 74 | "errors = arma(parameters, data, p, q)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": { 81 | "collapsed": false, 82 | "jupyter": { 83 | "outputs_hidden": false 84 | }, 85 | "pycharm": { 86 | "name": "#%%\n" 87 | } 88 | }, 89 | "outputs": [], 90 | "source": [ 91 | "%timeit arma(parameters, data, p, q)" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": { 97 | "pycharm": { 98 | "name": "#%% md\n" 99 | } 100 | }, 101 | "source": [ 102 | "## Exercise 2\n", 103 | "\n", 104 | "Using Numba here is very simple -- just wrap the function and\n", 105 | "then call the wrapped function. The code calls it once to \"warm-up\" the\n", 106 | "Just-in-time compiler before using `%timeit`.\n", 107 | "\n", 108 | "The JIT version should run around 500x faster than the Python version." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "collapsed": false, 116 | "jupyter": { 117 | "outputs_hidden": false 118 | }, 119 | "pycharm": { 120 | "name": "#%%\n" 121 | } 122 | }, 123 | "outputs": [], 124 | "source": [ 125 | "from numba import jit\n", 126 | "\n", 127 | "jit_arma = jit(arma)\n", 128 | "errors = jit_arma(parameters, data, p, q)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": { 135 | "collapsed": false, 136 | "jupyter": { 137 | "outputs_hidden": false 138 | }, 139 | "pycharm": { 140 | "name": "#%%\n" 141 | } 142 | }, 143 | "outputs": [], 144 | "source": [ 145 | "%timeit jit_arma(parameters, data, p, q)" 146 | ] 147 | } 148 | ], 149 | "metadata": { 150 | "kernelspec": { 151 | "display_name": "Python 3 (ipykernel)", 152 | "language": "python", 153 | "name": "python3" 154 | }, 155 | "language_info": { 156 | "codemirror_mode": { 157 | "name": "ipython", 158 | "version": 3 159 | }, 160 | "file_extension": ".py", 161 | "mimetype": "text/x-python", 162 | "name": "python", 163 | "nbconvert_exporter": "python", 164 | "pygments_lexer": "ipython3", 165 | "version": "3.9.6" 166 | } 167 | }, 168 | "nbformat": 4, 169 | "nbformat_minor": 4 170 | } 171 | -------------------------------------------------------------------------------- /solutions/chapter26/cython_arma.pyx: -------------------------------------------------------------------------------- 1 | #cython: language_level=3 2 | 3 | import numpy as np 4 | cimport numpy as np 5 | cimport cython 6 | 7 | 8 | @cython.boundscheck(False) 9 | @cython.wraparound(False) 10 | def arma(double[::1] parameters, 11 | double[::1] data, 12 | int p=0, 13 | int q=0): 14 | 15 | cdef size_t tau, t, i 16 | tau = data.shape[0] 17 | 18 | cdef double[::1] errors = np.zeros(tau) 19 | 20 | for t in range(p, tau): 21 | errors[t] = data[t] - parameters[0] 22 | for i in range(p): 23 | errors[t] -= parameters[i+1] * data[t-i-1] 24 | for i in range(q): 25 | if (t-i) >= 0: 26 | errors[t] -= parameters[i+p+1] * errors[t-i-1] 27 | 28 | return np.asarray(errors) 29 | -------------------------------------------------------------------------------- /solutions/chapter26/improving_performance_using_cython.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "pycharm": { 7 | "name": "#%% md\n" 8 | } 9 | }, 10 | "source": [ 11 | "# Improving Performance using Cython" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": null, 17 | "metadata": { 18 | "collapsed": false, 19 | "jupyter": { 20 | "outputs_hidden": false 21 | }, 22 | "pycharm": { 23 | "name": "#%%\n" 24 | } 25 | }, 26 | "outputs": [], 27 | "source": [ 28 | "import numpy as np\n", 29 | "\n", 30 | "\n", 31 | "def arma(parameters, data, p=0, q=0):\n", 32 | " tau = data.shape[0]\n", 33 | " errors = np.zeros(tau)\n", 34 | "\n", 35 | " for t in range(p, tau):\n", 36 | " errors[t] = data[t] - parameters[0]\n", 37 | " for i in range(p):\n", 38 | " errors[t] -= parameters[i + 1] * data[t - i - 1]\n", 39 | " for i in range(q):\n", 40 | " if (t - i) >= 0:\n", 41 | " # If not, lagged error is assumed to be 0\n", 42 | " errors[t] -= parameters[i + p + 1] * errors[t - i - 1]\n", 43 | "\n", 44 | " return np.asarray(errors)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": { 51 | "collapsed": false, 52 | "jupyter": { 53 | "outputs_hidden": false 54 | }, 55 | "pycharm": { 56 | "name": "#%%\n" 57 | } 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "data = np.random.standard_normal(1000)\n", 62 | "parameters = np.array([1, 0.1, 0.1, 0.4, -0.8])\n", 63 | "p = 2\n", 64 | "q = 2\n", 65 | "errors = arma(parameters, data, p, q)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": { 72 | "collapsed": false, 73 | "jupyter": { 74 | "outputs_hidden": false 75 | }, 76 | "pycharm": { 77 | "name": "#%%\n" 78 | } 79 | }, 80 | "outputs": [], 81 | "source": [ 82 | "%timeit arma(parameters, data, p, q)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": { 88 | "pycharm": { 89 | "name": "#%% md\n" 90 | } 91 | }, 92 | "source": [ 93 | "## Using Cython\n", 94 | "\n", 95 | "To run the code in the next cell, you need to have run\n", 96 | "\n", 97 | "```\n", 98 | "python setup_cython_arma.py build_ext -i\n", 99 | "```\n", 100 | "\n", 101 | "in the directory of this file.\n", 102 | "\n", 103 | "The Cythonized version should run around 500 times faster." 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": { 110 | "collapsed": false, 111 | "jupyter": { 112 | "outputs_hidden": false 113 | }, 114 | "pycharm": { 115 | "name": "#%%\n" 116 | } 117 | }, 118 | "outputs": [], 119 | "source": [ 120 | "import cython_arma\n", 121 | "\n", 122 | "%timeit cython_arma.arma(parameters, data, p, q)" 123 | ] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 3 (ipykernel)", 129 | "language": "python", 130 | "name": "python3" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 3 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython3", 142 | "version": "3.9.6" 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 4 147 | } 148 | -------------------------------------------------------------------------------- /solutions/chapter26/setup_cython_arma.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | from setuptools.extension import Extension 3 | from Cython.Distutils import build_ext 4 | import numpy 5 | 6 | setup( 7 | cmdclass = {'build_ext': build_ext}, 8 | ext_modules = [Extension("cython_arma", ["cython_arma.pyx"])], 9 | include_dirs = [numpy.get_include()] 10 | ) 11 | --------------------------------------------------------------------------------