├── .gitignore ├── LICENSE ├── README.md ├── homework_assignments ├── hw_set1 │ └── hw_set1.pdf ├── hw_set10 │ └── hw_set10.pdf ├── hw_set2 │ ├── demand.m │ ├── hw_set2.pdf │ ├── main.m │ └── supply.m ├── hw_set3 │ ├── company_list.csv │ ├── company_list_corrected.csv │ └── hw_set3.pdf ├── hw_set4 │ └── hw_set4.pdf ├── hw_set5 │ └── hw_set5.pdf ├── hw_set6 │ ├── hw_set6.pdf │ └── ols_via_projection │ │ ├── OLS_and_projection.ipynb │ │ └── trade_data.csv ├── hw_set7 │ └── fa_homework.pdf ├── hw_set8 │ └── hw_set_8.pdf └── hw_set9 │ └── hw_set_9.pdf ├── lecture1 ├── install-vm-environment.pdf └── lecture1.pdf ├── lecture10 ├── Interpolations_jl_alberto_polo.ipynb ├── Morelli_Presentation_final.ipynb └── lecture10.pdf ├── lecture11 ├── Scikit-Learn presentation.ipynb ├── lecture11.pdf ├── overfitting_noises_dcs.ipynb └── overfitting_presentation_dcs.pdf ├── lecture12 ├── lecture12.pdf ├── mabille_julia_parallel.ipynb ├── mabille_julia_parallel.pdf ├── sood_python_parallel_dask.pdf └── sood_python_parallel_preliminaries.pdf ├── lecture13 ├── carlos_lizama_Gadfly.ipynb ├── felipe_alves.pdf ├── felipe_alves_codes │ ├── .ipynb_checkpoints │ │ ├── HANK_felipe_alves-checkpoint.ipynb │ │ └── JuliaPackages-checkpoint.ipynb │ ├── HANK_felipe_alves.ipynb │ ├── aggregate.jl │ ├── main_fig.jl │ ├── solveHJB.jl │ ├── solveKFE.jl │ ├── testing.jl │ └── twoassets.jl └── lecture13.pdf ├── lecture14 ├── james_graham_DOLO.ipynb ├── pre_RuixueGong.ipynb ├── scikit-learn-flow-chart.jpg └── svm.jpg ├── lecture2 ├── .pass ├── c_examples │ ├── ar1_sample_mean.c │ ├── function.c │ ├── function_ref.c │ ├── hello.c │ ├── hello_again.c │ ├── make_grid.c │ ├── makefile │ └── sin_func.c ├── git_intro │ ├── Makefile │ ├── github.html │ ├── github.md │ ├── gitnotes.html │ ├── gitnotes.md │ └── images │ │ ├── gitlifecycle.png │ │ └── phd101212s.gif └── lecture2.pdf ├── lecture3 ├── .ipynb_checkpoints │ └── command_line-checkpoint.ipynb ├── command_line.ipynb └── lecture3.pdf ├── lecture4 └── lecture4.pdf ├── lecture5 ├── .ipynb_checkpoints │ └── numpy_timing-checkpoint.ipynb ├── fast_loop_examples │ ├── ar1_sample_mean.c │ ├── ar1_sample_mean.jl │ ├── ar1_sample_mean.py │ ├── foo │ └── makefile ├── lecture5.pdf └── numpy_timing.ipynb ├── lecture6 ├── .ipynb_checkpoints │ └── IntroToJulia-checkpoint.ipynb ├── IntroToJulia.ipynb ├── JuliaPackages.ipynb ├── ParallelJulia.ipynb └── install_julia ├── lecture7 ├── Intro_to_pymc.ipynb ├── Intro_to_pymc_pres.pdf ├── background_linalg.pdf └── lecture7.pdf ├── lecture8 ├── Pandas.ipynb ├── analysis_intermediate.pdf ├── analysis_introductory.pdf ├── lecture8.pdf └── presentation.pdf ├── lecture9 ├── Plotly_Presentation.html ├── Plotly_Presentation.ipynb └── lecture9.pdf └── other_PDFS ├── benhabib_wealth_distribution.pdf └── lp_spaces.pdf /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/.gitignore -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, John Stachurski 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of quantecon_nyu_2016 nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Topics in Computational Economics 3 | 4 | [John Stachurski](http://johnstachurski.net/) 5 | 6 | This is the home page of ECON-GA 3002, a PhD level course on computational economics to be held at [NYU](http://econ.as.nyu.edu/page/home) in the spring semester of 2016. 7 | 8 | (Note: This document is preliminary and still under development) 9 | 10 | Semi-Random quote 11 | 12 | > All this technology carries risk. There is no faster way for a trading 13 | > firm to destroy itself than to deploy a piece of trading software that 14 | > makes a bad decision over and over in a tight loop. Part of Jane Street's 15 | > reaction to these technological risks was to put a very strong focus on 16 | > building software that was easily understood--software that was readable. 17 | > 18 | > -- Yaron Minsky, Jane Street 19 | 20 | 21 | Table of Contents: 22 | 23 | * [News](#news) 24 | * [References](#references) 25 | * [Prerequisites](#prerequisites) 26 | * [Syllabus](#syllabus) 27 | * [Part I: Programming](#part-i-programming) 28 | * [Part II: Comp Econ Foundations](#part-ii-comp-econ-foundations) 29 | * [Part III: Applications](#part-iii-applications) 30 | * [Assessment](#assessment) 31 | * [Additional Resources](#additional-resources) 32 | 33 | ## News 34 | 35 | Please note that the lecture room has changed to **room 5-75 in the Stern Building**. 36 | 37 | The time is unchanged: Friday 9am--11am 38 | 39 | Please be sure to bring your laptop 40 | 41 | 42 | 43 | ## References 44 | 45 | * http://quant-econ.net/ 46 | * Secondary / Useful / Related / Recommended texts 47 | * Kendall Atkinson and Weimin Han (2009). *Theoretical Numerical Analysis* (3rd ed) 48 | * Ward Cheney (2001). *Analysis for Applied Mathematics* 49 | * Nancy Stokey and Robert Lucas Jr. (1989) *Recursive Methods in Economic Dynamics* 50 | * John Stachurski (2009). *Economic Dynamics: Theory and Computation* 51 | 52 | 53 | ## Prerequisites 54 | 55 | I assume that you have 56 | 57 | * At least a bit of programming experience 58 | * E.g., some experience writing Matlab code or similar 59 | * Econ PhD level quantitative skills, including some familiarity with 60 | * Linear algebra 61 | * Basic analysis (sequences, limits, continuity, etc.) 62 | * Dynamics (diff equations, finite Markov chains, AR(1) processes, etc.) 63 | 64 | If you would like to prepare for the course before hand please consider 65 | 66 | * Installing [Linux](http://www.ubuntu.com/desktop) on a [VM](https://www.virtualbox.org/wiki/Linux_Downloads) or in a bootable partition on your laptop 67 | * Backup your data first! 68 | * Help available in the first class 69 | * Build up your [Linux skills](http://manuals.bioinformatics.ucr.edu/home/linux-basics) (and 70 | [profit](http://www.eweek.com/it-management/demand-for-linux-skills-growing-faster-than-talent-pool-report.html)) 71 | * Do some exercises in real analysis if you are rusty 72 | * [These notes](http://math.louisville.edu/~lee/ira/IntroRealAnal.pdf) look like about the right level 73 | * Read the first 3 chapters of [RMT](https://mitpress.mit.edu/books/recursive-macroeconomic-theory-1) if you don't know any Markov chain theory or dynamic programming 74 | 75 | 76 | ## Syllabus 77 | 78 | Below is a sketch of the syllabus for the course. The details are still 79 | subject to some change. 80 | 81 | ### Part I: Programming 82 | 83 | 84 | #### Introduction 85 | 86 | * Scientific programming environments --- what do we want? 87 | * Speed? 88 | * Productivity? 89 | * [Fun?](https://xkcd.com/353/) 90 | * Why [Python](https://www.python.org/)? And what is it anyway? 91 | * Background 92 | * http://quant-econ.net/py/about_py.html 93 | * http://www.galvanize.com/blog/2015/10/01/bill-and-melinda-gates-foundation-saves-lives-with-python/ 94 | * Philosophy 95 | * http://legacy.python.org/dev/peps/pep-0020/ 96 | * https://gist.github.com/sloria/7001839 97 | * The [second best language for everything](http://blog.mikiobraun.de/2013/11/how-python-became-the-language-of-choice-for-data-science.html) 98 | * https://github.com/jstac/backup_scripts 99 | * What's Julia? 100 | * http://julialang.org/blog/2012/02/why-we-created-julia/ 101 | * http://libertystreeteconomics.newyorkfed.org/2015/12/the-frbny-dsge-model-meets-julia.html 102 | * Open Source 103 | * Examples of how contributions improve on the standard library 104 | * http://docs.python-requests.org/en/latest/ 105 | * https://python-programming.courses/general/better-date-and-time-handling-with-arrow/ 106 | * Open science 107 | * http://www.openscience.org/blog/?p=269 108 | * https://opensource.com/resources/open-science 109 | * http://www.nature.com/news/interactive-notebooks-sharing-the-code-1.16261 110 | * http://devblogs.nvidia.com/parallelforall/open-reproducible-computational-chemistry-python-cuda/ 111 | * How can open source produce **better** software than firms acting alone? 112 | * https://github.com/ 113 | * https://www.moore.org/newsroom/press-releases/2015/07/07/$6m-for-uc-berkeley-and-cal-poly-to-expand-and-enhance-open-source-software-for-scientific-computing-and-data-science 114 | * https://www.continuum.io/ 115 | 116 | 117 | #### Coding Foundations 118 | 119 | * UNIX and the UNIX shell 120 | * http://swcarpentry.github.io/shell-novice/ 121 | * Editing = Vim 122 | * https://danielmiessler.com/study/vim/ 123 | * https://realpython.com/blog/python/vim-and-python-a-match-made-in-heaven/ 124 | * http://vim-adventures.com/ 125 | * http://www.openvim.com/ 126 | * Tmux 127 | * http://tangosource.com/blog/a-tmux-crash-course-tips-and-tweaks/ 128 | * Version control 129 | * https://github.com/swcarpentry/git-novice 130 | * http://gitimmersion.com/ 131 | * http://luisbg.blogalia.com//historias/76017 --- Git cheatsheet 132 | * General software engineering skills 133 | * http://software-carpentry.org/ 134 | * Speed and Efficiency 135 | * Hardware 136 | * Interpreted / JIT compiled / AOT compiled 137 | * Vectorized code 138 | * C and Fortran 139 | * [GSL](http://www.gnu.org/software/gsl/) 140 | * http://computationalmodelling.bitbucket.org/tools/FORTRAN.html 141 | * Test driven development: 142 | * http://code.tutsplus.com/tutorials/beginning-test-driven-development-in-python--net-30137 143 | 144 | 145 | 146 | #### Core Python 147 | 148 | * [Getting started](http://quant-econ.net/py/getting_started.html) 149 | * The REPLs: Python and IPython shells 150 | * Jupyter 151 | * The beauty of introspection on the fly 152 | * Basic syntax 153 | * http://quant-econ.net/py/python_by_example.html 154 | * http://quant-econ.net/py/python_essentials.html 155 | * OOP. It's like structs with lazy evaluation 156 | * http://quant-econ.net/py/python_oop.html 157 | * http://quant-econ.net/py/python_foundations.html 158 | * http://quant-econ.net/py/python_advanced_features.html 159 | * Python style 160 | * https://blog.hartleybrody.com/python-style-guide/ 161 | * https://google.github.io/styleguide/pyguide.html 162 | * https://www.python.org/dev/peps/pep-0008/ 163 | * Other general Python resources 164 | * https://leanpub.com/intermediatepython/read 165 | * http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/01.ipynb 166 | * http://book.pythontips.com/en/latest/ 167 | * Debugging 168 | * http://www.scipy-lectures.org/advanced/debugging/ 169 | 170 | 171 | #### Scientific Python I: SciPy and Friends 172 | 173 | * General Resources 174 | * https://github.com/jrjohansson/scientific-python-lectures 175 | * http://bender.astro.sunysb.edu/classes/python-science/ 176 | * http://computationalmodelling.bitbucket.org/tools/ 177 | * [NumPy and SciPy](http://www.scipy.org/) 178 | * http://quant-econ.net/py/numpy.html 179 | * http://quant-econ.net/py/scipy.html 180 | * [Jupyter](http://jupyter.org/) 181 | * http://nbviewer.ipython.org/ 182 | * http://jupyter.cs.brynmawr.edu/hub/dblank/public/Jupyter%20Notebook%20Users%20Manual.ipynb 183 | * http://mindtrove.info/#nb-extensions 184 | * https://plot.ly/ipython-notebooks/ipython-notebook-tutorial/ 185 | * https://github.com/nicolaskruchten/pyconca/blob/master/jupyter_magic.ipynb 186 | * [Matplotlib](http://matplotlib.org/) 187 | * http://quant-econ.net/py/matplotlib.html 188 | * http://nbviewer.ipython.org/github/clbarnes/plotstyles/blob/master/plotstyles.ipynb 189 | 190 | 191 | #### Scientific Python II: The Ecosystem 192 | 193 | * [Pandas](http://pandas.pydata.org/) 194 | * http://geoffboeing.com/2015/11/landscape-us-rents/ 195 | * [Numba](http://numba.pydata.org/) and other JIT compilers 196 | * http://blog.pyston.org/2015/11/03/102/ 197 | * http://nbviewer.ipython.org/github/postelrich/fin_examples/blob/master/cva/cva1.ipynb 198 | * https://www.ibm.com/developerworks/community/blogs/jfp/entry/A_Comparison_Of_C_Julia_Python_Numba_Cython_Scipy_and_BLAS_on_LU_Factorization?lang=en 199 | * AOT compilers 200 | * [Cython](http://cython.org/) 201 | * Others (Nuitka?) 202 | * Visualization 203 | * [Plotly](https://plot.ly/), Bokeh 204 | * Statistics and machine learning 205 | * https://www.youtube.com/watch?v=5W715nfJNJw 206 | * PyMC, [Statsmodels](http://statsmodels.sourceforge.net/) 207 | * http://scikit-learn.org/stable/related_projects.html 208 | * https://www.youtube.com/watch?v=L7R4HUQ-eQ0&feature=youtu.be 209 | * [Seaborn](http://stanford.edu/~mwaskom/software/seaborn/) 210 | * Parallel processing 211 | * http://www.admin-magazine.com/HPC/Articles/Parallel-Python-with-Joblib 212 | * http://www.davekuhlman.org/python_multiprocessing_01.html 213 | * http://ufora.github.io/ufora/ 214 | * Blaze 215 | * Wrappers 216 | * https://github.com/wjakob/pybind11 217 | * f2py and related solutions (https://www.euroscipy.org/2015/schedule/presentation/58/) 218 | * [NetworkX](https://networkx.github.io/) 219 | * [Sympy](http://www.sympy.org/en/index.html) 220 | * http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/SymPy.ipynb 221 | * Webscraping 222 | * http://shop.oreilly.com/product/0636920034391.do 223 | * http://robertwdempsey.com/simple-python-web-scraper-get-pricing-data/ 224 | 225 | 226 | 227 | #### Julia 228 | 229 | 230 | * General, tutorials 231 | * http://julialang.org/ 232 | * http://www.slideshare.net/acidflask/an-introduction-to-julia 233 | * http://doodlingindata.com/2015/08/11/writing-good-julia-functions/ 234 | * http://computationalmodelling.bitbucket.org/tools/Julia.html 235 | * http://samuelcolvin.github.io/JuliaByExample/ 236 | * https://github.com/dpsanders/hands_on_julia 237 | * http://bogumilkaminski.pl/files/julia_express.pdf 238 | * https://en.wikibooks.org/wiki/Introducing_Julia 239 | * Libraries 240 | * [QuantEcon.jl](https://github.com/QuantEcon/QuantEcon.jl) 241 | * [Distributions.jl](https://github.com/JuliaStats/Distributions.jl) 242 | * [Gadfly](http://dcjones.github.io/Gadfly.jl/) 243 | 244 | 245 | 246 | ### Part II: Comp Econ Foundations 247 | 248 | 249 | #### Markov Dynamics I: Finite State 250 | 251 | * Asymptotics 252 | * The Dobrushin coefficient 253 | * A simple coupling argument 254 | * Code from QuantEcon 255 | * Applications 256 | 257 | 258 | #### Functional Analysis 259 | 260 | * A dash of measure and integration 261 | * Metric / Banach / Hilbert space 262 | * Space of bounded functions (cbS is a closed subset) 263 | * The Lp spaces 264 | * Banach contraction mapping theorem 265 | * Blackwell's sufficient condition 266 | * Orthogonal projections 267 | * Neumann series lemma 268 | * Applications 269 | * The Lucas 78 asset pricing paper 270 | 271 | 272 | #### Markov Dynamics II: General State 273 | 274 | * General state spaces 275 | * Feller chains, Boundedness in prob 276 | * Monotone methods 277 | * LLN and CLT 278 | * Look ahead method 279 | * examples in lae_extension? 280 | * examples in poverty traps survey? 281 | * Applications 282 | * ARCH, AZ, STAR, MCMC, etc. 283 | 284 | 285 | #### Solving Forward Looking Models 286 | 287 | * L2 methods 288 | * Asset Pricing 289 | 290 | 291 | #### Dynamic Programming 292 | 293 | * Fundamental theory 294 | * The principle of optimality 295 | * VFI 296 | * Howard's policy iteration algorithm 297 | * Approximation 298 | * Preserving the contraction property 299 | * MC for integrals 300 | * Weighted sup norm approach 301 | 302 | 303 | ### Part III: Applications 304 | 305 | 306 | #### DP II: Applications and Extensions 307 | 308 | * The Coleman operator 309 | * [The income fluctuation problem](http://quant-econ.net/py/ifp.html) 310 | * Benhabib wealth distribution paper, heavy tails 311 | * Recursive and risk sensitive preferences 312 | * [Stochastic Optimal Growth Model with Risk Sensitive Preferences](http://arxiv.org/abs/1509.05638) 313 | * Other (see TE paper, monotone LLN) 314 | 315 | 316 | #### Optimal Stopping 317 | 318 | * Reservation rule operator 319 | * Theory 320 | * Applications 321 | 322 | 323 | #### Coase's Theory of the Firm 324 | 325 | * Theory 326 | * Implementation 327 | 328 | 329 | 330 | ## Assessment 331 | 332 | See lecture 1 slides. 333 | 334 | ### Notes on Class Presentations 335 | 336 | All students enrolled in the course must give a 20 minute presentation. 337 | The presentation can be on your class project or on a code library or 338 | algorithm in Julia or Python that you find interesting. Here are some 339 | suggestions: 340 | 341 | * Profiling (see, e.g., [this link](http://pynash.org/2013/03/06/timing-and-profiling.html) or [this one](https://zapier.com/engineering/profiling-python-boss/)) 342 | * [scikit-learn](http://scikit-learn.org/stable/) (a machine learning library) 343 | * Unit tests (see, e.g., [here](http://docs.python-guide.org/en/latest/writing/tests/) or [here](https://www.jeffknupp.com/blog/2013/12/09/improve-your-python-understanding-unit-testing/)) 344 | * Alternative plotting libraries and their strengths / weaknesses 345 | * [Distributions.jl](https://github.com/JuliaStats/Distributions.jl) (a well-written Julia library) 346 | * Some features of vim or vim plug-in(s) that you find particularly useful 347 | * Techniques for parallel processing 348 | * Interfacing with C and Fortran code in either Python or Julia 349 | 350 | 351 | ### Notes on the Class Project 352 | 353 | You should discuss your class project at least briefly with me before you 354 | start. I am flexible about topics and mainly concerned with quality. 355 | 356 | All projects are due by midnight on June 3rd. 357 | 358 | #### Structure of the Project 359 | 360 | A completed class project is a GitHub repository containing 361 | 362 | * Code 363 | * A Jupyter notebook that pulls all the code together and runs it 364 | * A PDF document that provides analysis and reports results 365 | * like a short research paper 366 | 367 | Good projects demonstrate proficiency with 368 | 369 | * Python or Julia 370 | * Good programming style 371 | * Ideally, the techical material discussed during the course 372 | 373 | #### Random Ideas 374 | 375 | Here are some very random ideas that I'll add to over the semester. The links 376 | are to papers, code or discussions of algorithms, quantitative work, etc. that could 377 | be implemented / replicated / improved using Python or Julia. Feel free to use or ignore. (Ideally you 378 | will find your own topic according to your own interests. Please discuss your 379 | topic with me either way). 380 | 381 | * [Computing equilibria in dynamic games](https://www.andrew.cmu.edu/user/sevin/sevin/Research_files/Supergame_March_2015_KJ.pdf) 382 | * [Heterogeneous agents in continuous time](http://www.princeton.edu/~moll/HACTproject.htm) 383 | * [Computing Nash equilibria](https://en.wikipedia.org/wiki/Lemke%E2%80%93Howson_algorithm) 384 | * [The stable marriage problem](https://en.wikipedia.org/wiki/Stable_marriage_problem) 385 | * [Behavioral Macroeconomics via Sparse Dynamic Programming](http://pages.stern.nyu.edu/~xgabaix/papers/brdp.pdf) 386 | * [Krusell-Smith](https://ideas.repec.org/c/dge/qmrbcd/180.html) 387 | * [Krusell-Smith II](http://www.econ.yale.edu/smith/code.htm) 388 | * Numbafy everything in random.py (ask me) 389 | * Numbafy some of the optimization / root finding routines from SciPy (ask me) 390 | * [Assorted code / ideas from Dean Corbae](https://sites.google.com/site/deancorbae/teaching) 391 | * [Assorted code / ideas from Karen Kopecky](http://www.karenkopecky.net/) 392 | * [Assorted code / ideas from Chris Carroll](http://www.econ2.jhu.edu/people/ccarroll/) 393 | * [A paper on dynamics by Kiminori Matsuyama](http://faculty.wcas.northwestern.edu/~kmatsu/Revisiting%20the%20model%20of%20credit%20cycles%20with%20Good%20and%20Bad%20Projects-2016-2.pdf) 394 | * [An econ geography paper by Paul Krugman](https://ideas.repec.org/a/eee/eecrev/v37y1993i2-3p293-298.html) 395 | * Routines from Miranda and Fackler's [CompEcon](http://www4.ncsu.edu/~pfackler/compecon/toolbox.html) toolkit and [textbook](http://www4.ncsu.edu/~pfackler/compecon/) 396 | * [Angeletos 2007 paper](http://www.sciencedirect.com/science/article/pii/S1094202506000627) 397 | 398 | 399 | 400 | ## Additional Resources 401 | 402 | * Jupyter 403 | * https://github.com/bloomberg/bqplot 404 | * https://cloud.google.com/datalab/ 405 | * http://blog.dominodatalab.com/lesser-known-ways-of-using-notebooks/ 406 | * https://github.com/jupyter/jupyterhub 407 | * http://mybinder.org/ 408 | 409 | * Data, machine learning and prediction 410 | * www.galvanize.com/blog/how-random-forest-modeling-solves-seattles-bikesharing-problem/ 411 | * https://anaconda.org/ikkebr/brazilian-federal-payroll/notebook 412 | 413 | * Language comparisons 414 | * http://sebastianraschka.com/Articles/2014_matlab_vs_numpy.html 415 | * http://scottsievert.github.io/blog/2015/09/01/matlab-to-python/ 416 | * https://www.ibm.com/developerworks/community/blogs/jfp/entry/Python_Meets_Julia_Micro_Performance?lang=en 417 | 418 | * Python, general 419 | * https://www.reddit.com/r/Python/comments/3s4j6n/zen_of_python_verse_2/ 420 | * https://github.com/s16h/py-must-watch 421 | * https://www.reddit.com/r/Python/comments/3m3ll9/where_python_is_used_in_industry_other_than_webdev/ 422 | * http://bruceeckel.github.io/2015/08/29/what-i-do/ 423 | * http://blog.apcelent.com/python-decorator-tutorial-with-example.html 424 | * http://noeticforce.com/best-free-tutorials-to-learn-python-pdfs-ebooks-online-interactive 425 | 426 | Vectorization: 427 | * http://blog.datascience.com/straightening-loops-how-to-vectorize-data-aggregation-with-pandas-and-numpy/ 428 | 429 | Good reads 430 | * http://undsci.berkeley.edu/article/cold_fusion_01 431 | * https://msdn.microsoft.com/en-us/library/dn568100.aspx 432 | 433 | 434 | -------------------------------------------------------------------------------- /homework_assignments/hw_set1/hw_set1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set1/hw_set1.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set10/hw_set10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set10/hw_set10.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set2/demand.m: -------------------------------------------------------------------------------- 1 | function yd = demand(price); 2 | 3 | global a epsilon; 4 | 5 | yd = a*(price^(-epsilon)); 6 | 7 | end 8 | 9 | 10 | -------------------------------------------------------------------------------- /homework_assignments/hw_set2/hw_set2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set2/hw_set2.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set2/main.m: -------------------------------------------------------------------------------- 1 | global a b epsilon; 2 | a = 1; 3 | b = 0.1; 4 | epsilon = 1; 5 | 6 | mxiter = 30; 7 | toler = 1.0e-6; 8 | 9 | plow = 0.1; 10 | phigh = 10.0; 11 | 12 | niter = mxiter; 13 | 14 | for i = 1:mxiter; 15 | 16 | pcur = (plow + phigh)/2; 17 | 18 | yd = demand(pcur); 19 | ys = supply(pcur); 20 | 21 | excesssupply = ys - yd; 22 | 23 | if (excesssupply > 0); 24 | phigh = pcur; 25 | else; 26 | plow = pcur; 27 | end; 28 | 29 | diff = abs(phigh - plow); 30 | 31 | if (diff <= toler); 32 | niter = i; 33 | break; 34 | end; 35 | 36 | end; 37 | 38 | pclear = (plow + phigh)/2; 39 | yd = demand(pcur); 40 | ys = supply(pcur); 41 | excesssupply = ys - yd; 42 | 43 | [niter pclear yd ys excesssupply] 44 | -------------------------------------------------------------------------------- /homework_assignments/hw_set2/supply.m: -------------------------------------------------------------------------------- 1 | function ys = supply(price); 2 | 3 | global b; 4 | 5 | ys = exp(b*price) - 1; 6 | 7 | end 8 | 9 | -------------------------------------------------------------------------------- /homework_assignments/hw_set3/hw_set3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set3/hw_set3.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set4/hw_set4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set4/hw_set4.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set5/hw_set5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set5/hw_set5.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set6/hw_set6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set6/hw_set6.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set6/ols_via_projection/OLS_and_projection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## OLS Through StatsModels vs Projection" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "In this exercise we're going to run a regression using some trade data. (The regression model is a gravity model, although the details don't really matter for this exercise.) The idea is to compute the OLS coefficients and other related quantities using \n", 15 | "\n", 16 | "1. A regression package, and\n", 17 | "2. The expressions given in the lecture on orthogonal projection.\n", 18 | "\n", 19 | "Note that you need to download the data set \"trade_data.csv\" as well as this notebook.\n", 20 | "\n", 21 | "Your task is to complete the notebook, as discussed below.\n", 22 | "\n", 23 | "First let's try a standard approach, using StatsModels." 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 3, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "matplotlib inline" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 4, 40 | "metadata": { 41 | "collapsed": true 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "import pandas as pd\n", 46 | "import numpy as np\n", 47 | "from numpy import log\n", 48 | "import statsmodels.formula.api as smf" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "First we read in the data." 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 5, 61 | "metadata": { 62 | "collapsed": true 63 | }, 64 | "outputs": [], 65 | "source": [ 66 | "data = pd.read_csv(\"trade_data.csv\")" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "Let's see what it looks like." 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 6, 79 | "metadata": { 80 | "collapsed": false 81 | }, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/html": [ 86 | "
\n", 87 | "\n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | " \n", 164 | " \n", 165 | " \n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | " \n", 183 | " \n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | "
Unnamed: 0yeariiso3ceiso3cvaluecontigcomlang_offcolonydistdistcapdistwdistwcesellillegdpegdppcepopigdpigdppcipop
002013ABWBEL7743530107847.0707847.0707843.2557843.006004.204710e+1137599.73549811182817NaNNaN102921
112013ABWBHS47125370001588.5151588.5151634.5151628.143007.835118e+0920736.547344377841NaNNaN102921
222013ABWCHE178126260008056.3328056.3328074.218073.511104.772463e+1158996.8961428089346NaNNaN102921
332013ABWCHN2531916800014155.35014155.35014590.9214560.28004.912954e+123619.4391081357380000NaNNaN102921
442013ABWCOL221600860101036.6341036.634929.5887861.2452002.129079e+114497.19693647342363NaNNaN102921
\n", 231 | "
" 232 | ], 233 | "text/plain": [ 234 | " Unnamed: 0 year iiso3c eiso3c value contig comlang_off colony \\\n", 235 | "0 0 2013 ABW BEL 774353 0 1 0 \n", 236 | "1 1 2013 ABW BHS 4712537 0 0 0 \n", 237 | "2 2 2013 ABW CHE 17812626 0 0 0 \n", 238 | "3 3 2013 ABW CHN 25319168 0 0 0 \n", 239 | "4 4 2013 ABW COL 22160086 0 1 0 \n", 240 | "\n", 241 | " dist distcap distw distwces ell ill egdp \\\n", 242 | "0 7847.070 7847.070 7843.255 7843.006 0 0 4.204710e+11 \n", 243 | "1 1588.515 1588.515 1634.515 1628.143 0 0 7.835118e+09 \n", 244 | "2 8056.332 8056.332 8074.21 8073.511 1 0 4.772463e+11 \n", 245 | "3 14155.350 14155.350 14590.92 14560.28 0 0 4.912954e+12 \n", 246 | "4 1036.634 1036.634 929.5887 861.2452 0 0 2.129079e+11 \n", 247 | "\n", 248 | " egdppc epop igdp igdppc ipop \n", 249 | "0 37599.735498 11182817 NaN NaN 102921 \n", 250 | "1 20736.547344 377841 NaN NaN 102921 \n", 251 | "2 58996.896142 8089346 NaN NaN 102921 \n", 252 | "3 3619.439108 1357380000 NaN NaN 102921 \n", 253 | "4 4497.196936 47342363 NaN NaN 102921 " 254 | ] 255 | }, 256 | "execution_count": 6, 257 | "metadata": {}, 258 | "output_type": "execute_result" 259 | } 260 | ], 261 | "source": [ 262 | "data.head()" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "Let's get a full list of columns." 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 7, 275 | "metadata": { 276 | "collapsed": false 277 | }, 278 | "outputs": [ 279 | { 280 | "data": { 281 | "text/plain": [ 282 | "Index(['Unnamed: 0', 'year', 'iiso3c', 'eiso3c', 'value', 'contig',\n", 283 | " 'comlang_off', 'colony', 'dist', 'distcap', 'distw', 'distwces', 'ell',\n", 284 | " 'ill', 'egdp', 'egdppc', 'epop', 'igdp', 'igdppc', 'ipop'],\n", 285 | " dtype='object')" 286 | ] 287 | }, 288 | "execution_count": 7, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "data.columns" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Let's regress 'value' on 'egdp', 'igdp' and 'dist', all in logs. To do this we make a `formula` object." 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 8, 307 | "metadata": { 308 | "collapsed": true 309 | }, 310 | "outputs": [], 311 | "source": [ 312 | "formula = \"log(value) ~ log(egdp) + log(igdp) + log(dist)\"\n", 313 | "model = smf.ols(formula, data)" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 9, 319 | "metadata": { 320 | "collapsed": false 321 | }, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | " OLS Regression Results \n", 328 | "==============================================================================\n", 329 | "Dep. Variable: log(value) R-squared: 0.615\n", 330 | "Model: OLS Adj. R-squared: 0.614\n", 331 | "Method: Least Squares F-statistic: 936.4\n", 332 | "Date: Thu, 10 Mar 2016 Prob (F-statistic): 0.00\n", 333 | "Time: 17:24:14 Log-Likelihood: -4228.1\n", 334 | "No. Observations: 1777 AIC: 8464.\n", 335 | "Df Residuals: 1773 BIC: 8486.\n", 336 | "Df Model: 3 \n", 337 | "Covariance Type: HC1 \n", 338 | "==============================================================================\n", 339 | " coef std err z P>|z| [95.0% Conf. Int.]\n", 340 | "------------------------------------------------------------------------------\n", 341 | "Intercept -27.0265 1.198 -22.563 0.000 -29.374 -24.679\n", 342 | "log(egdp) 1.2224 0.028 44.202 0.000 1.168 1.277\n", 343 | "log(igdp) 0.9679 0.031 30.963 0.000 0.907 1.029\n", 344 | "log(dist) -1.4130 0.069 -20.426 0.000 -1.549 -1.277\n", 345 | "==============================================================================\n", 346 | "Omnibus: 179.128 Durbin-Watson: 1.763\n", 347 | "Prob(Omnibus): 0.000 Jarque-Bera (JB): 316.166\n", 348 | "Skew: -0.683 Prob(JB): 2.22e-69\n", 349 | "Kurtosis: 4.551 Cond. No. 652.\n", 350 | "==============================================================================\n", 351 | "\n", 352 | "Warnings:\n", 353 | "[1] Standard Errors are heteroscedasticity robust (HC1)\n" 354 | ] 355 | } 356 | ], 357 | "source": [ 358 | "result = model.fit(cov_type='HC1')\n", 359 | "print(result.summary())" 360 | ] 361 | }, 362 | { 363 | "cell_type": "markdown", 364 | "metadata": {}, 365 | "source": [ 366 | "### Replication using Projection" 367 | ] 368 | }, 369 | { 370 | "cell_type": "markdown", 371 | "metadata": {}, 372 | "source": [ 373 | "Now let's reproduce the same values using the formulas from the lecture on projection. I'm going to be nice and build $\\mathbf X$ and $\\mathbf y$ for you." 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": 11, 379 | "metadata": { 380 | "collapsed": true 381 | }, 382 | "outputs": [], 383 | "source": [ 384 | "data2 = data[['value', 'egdp', 'igdp', 'dist']]\n", 385 | "data2 = data2.dropna()\n", 386 | "\n", 387 | "y = np.asarray(np.log(data2.value))\n", 388 | "X = np.ones((len(y), 4))\n", 389 | "X[:, 1] = log(data2.egdp)\n", 390 | "X[:, 2] = log(data2.igdp)\n", 391 | "X[:, 3] = log(data2.dist)" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": {}, 397 | "source": [ 398 | "Now reproduce the coefficients by computing $\\hat \\beta$, using the matrix expression given in the lectures." 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": null, 404 | "metadata": { 405 | "collapsed": true 406 | }, 407 | "outputs": [], 408 | "source": [ 409 | "# Derive betahat using the expression from the lectures\n", 410 | "print(betahat)" 411 | ] 412 | }, 413 | { 414 | "cell_type": "markdown", 415 | "metadata": {}, 416 | "source": [ 417 | "Next replicate the value for $R^2$ produced in the table above using the formula given in the lecture slides." 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": 12, 423 | "metadata": { 424 | "collapsed": false 425 | }, 426 | "outputs": [ 427 | { 428 | "ename": "NameError", 429 | "evalue": "name 'Rsq' is not defined", 430 | "output_type": "error", 431 | "traceback": [ 432 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 433 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 434 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Derive R^2 using y, Py, etc. as defined in the lecture\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mRsq\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 435 | "\u001b[1;31mNameError\u001b[0m: name 'Rsq' is not defined" 436 | ] 437 | } 438 | ], 439 | "source": [ 440 | "# Derive R^2 using y, Py, etc. as defined in the lecture\n", 441 | "print(Rsq)" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "metadata": { 448 | "collapsed": true 449 | }, 450 | "outputs": [], 451 | "source": [] 452 | } 453 | ], 454 | "metadata": { 455 | "kernelspec": { 456 | "display_name": "Python 3", 457 | "language": "python", 458 | "name": "python3" 459 | }, 460 | "language_info": { 461 | "codemirror_mode": { 462 | "name": "ipython", 463 | "version": 3 464 | }, 465 | "file_extension": ".py", 466 | "mimetype": "text/x-python", 467 | "name": "python", 468 | "nbconvert_exporter": "python", 469 | "pygments_lexer": "ipython3", 470 | "version": "3.5.1" 471 | } 472 | }, 473 | "nbformat": 4, 474 | "nbformat_minor": 0 475 | } 476 | -------------------------------------------------------------------------------- /homework_assignments/hw_set7/fa_homework.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set7/fa_homework.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set8/hw_set_8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set8/hw_set_8.pdf -------------------------------------------------------------------------------- /homework_assignments/hw_set9/hw_set_9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/homework_assignments/hw_set9/hw_set_9.pdf -------------------------------------------------------------------------------- /lecture1/install-vm-environment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture1/install-vm-environment.pdf -------------------------------------------------------------------------------- /lecture1/lecture1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture1/lecture1.pdf -------------------------------------------------------------------------------- /lecture10/lecture10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture10/lecture10.pdf -------------------------------------------------------------------------------- /lecture11/lecture11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture11/lecture11.pdf -------------------------------------------------------------------------------- /lecture11/overfitting_presentation_dcs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture11/overfitting_presentation_dcs.pdf -------------------------------------------------------------------------------- /lecture12/lecture12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture12/lecture12.pdf -------------------------------------------------------------------------------- /lecture12/mabille_julia_parallel.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture12/mabille_julia_parallel.pdf -------------------------------------------------------------------------------- /lecture12/sood_python_parallel_dask.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture12/sood_python_parallel_dask.pdf -------------------------------------------------------------------------------- /lecture12/sood_python_parallel_preliminaries.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture12/sood_python_parallel_preliminaries.pdf -------------------------------------------------------------------------------- /lecture13/felipe_alves.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture13/felipe_alves.pdf -------------------------------------------------------------------------------- /lecture13/felipe_alves_codes/aggregate.jl: -------------------------------------------------------------------------------- 1 | ################################## 2 | ## 3 | ## Prices 4 | ## 5 | ################################## 6 | """ 7 | Type that contains information of prices 8 | 9 | ## Prices 10 | 11 | - `rᴷ` : rental rate of capital 12 | - `rᴬ` : return on the illiquid asset 13 | - `rᴮ` : return on the liquid asset 14 | - `wedge` : 15 | - `w` : wage rate 16 | 17 | """ 18 | type Prices 19 | rᴷ ::Float64 20 | rᴬ ::Float64 21 | rᴮ ::Float64 22 | wedge::Float64 23 | w ::Float64 24 | end 25 | 26 | function update_prices!(pc::Prices, P::Vector{Float64}) 27 | pc.rᴷ, pc.rᴬ, pc.rᴮ, pc.w = P 28 | end 29 | 30 | price_partial() = Prices(0.0, 0.04, 0.03, 0.09,4.0) 31 | 32 | function Base.show(io::IO, pr::Prices) 33 | @printf io "\n" 34 | @printf io " Prices \n" 35 | @printf io " rᴷ : %.3f \n" pr.rᴷ 36 | @printf io " rᴬ : %.3f \n" pr.rᴬ 37 | @printf io " rᴮ : %.3f \n" pr.rᴮ 38 | @printf io " wedge : %.3f \n" pr.wedge 39 | @printf io " wage : %.3f \n" pr.w 40 | end 41 | 42 | ################################## 43 | ## 44 | ## SteadyState and Transition 45 | ## 46 | ################################## 47 | """ 48 | Description of the SteadyState 49 | # Endogenous 50 | 51 | ### Prices 52 | - `rcapital` 53 | - `ra` 54 | - `rb` 55 | - `rborr` 56 | - `wage ` 57 | 58 | ### Interest rate 59 | - `rnom` 60 | 61 | ### Aggregates to clear 62 | - `Eb` : aggregate liq saving from households 63 | - `bond` : total amount of bonds in the market 64 | - `Ea` : aggregate illiq saving from households 65 | - `capital` : capital demanded by firms 66 | - `labor` : 67 | 68 | ### Firm Stats 69 | - `KYratio` 70 | - `KNratio` 71 | 72 | - `π` 73 | - `mc` 74 | - `priceadjust` 75 | 76 | - `output` 77 | - `profit` 78 | - `dividend` 79 | 80 | ### Fund 81 | - `divrate` 82 | - `investment` 83 | - `deprec` 84 | - `caputil` 85 | 86 | ### Government 87 | - `G` 88 | - `lumptransfer` 89 | - `τ` 90 | - `taxrev` 91 | - `govbond` 92 | 93 | """ 94 | 95 | function initializeSS(KYratio = 8., rb = 0.015) 96 | 97 | ssvar = [:rcapital, :ra, :rb, :wedge, :wage, # prices 98 | :rnom, # 99 | :Eb, :bond, :Ea, :capital, :labor, # aggregates 100 | :KYratio, :KNratio, :π, :mc, :priceadjust, # 101 | :output, :profit, :dividend, # 102 | :divrate, :investment, :deprec, :caputil, # 103 | :G, :lumptransfer, :τ, :taxrev, :govbond, # 104 | :tfp] # 105 | 106 | equmSS = (Symbol => Float64)[var => 0.0 for var in ssvar] 107 | 108 | equmSS[:tfp] = 1. 109 | equmSS[:KYratio] = KYratio 110 | equmSS[:τ] = 0.25 111 | equmSS[:rb] = rb #having problem with 0.02/4 112 | 113 | equmSS[:wedge] = 0.10 #0.0161134 114 | 115 | 116 | equmSS[:KNratio] = ( equmSS[:tfp] * equmSS[:KYratio] )^(1./(1.-α)) 117 | equmSS[:mc] = (ϵ-1) / ϵ 118 | 119 | #== Prices ==# 120 | equmSS[:rcapital] = equmSS[:mc] * α / equmSS[:KYratio] 121 | equmSS[:wage] = equmSS[:mc] * equmSS[:tfp] * (1. - α) * equmSS[:KNratio]^α 122 | 123 | #== Firm ==# 124 | equmSS[:priceadjust] = 0 125 | equmSS[:labor] = ( (1 - equmSS[:τ])*equmSS[:wage] /ψ )^σ 126 | 127 | equmSS[:capital] = equmSS[:KNratio] * equmSS[:labor] 128 | equmSS[:investment] = δbar * equmSS[:capital] 129 | 130 | equmSS[:profit] = (1. - equmSS[:mc])* equmSS[:capital] / equmSS[:KYratio] - equmSS[:priceadjust] 131 | equmSS[:divrate] = equmSS[:profit] / equmSS[:capital] 132 | 133 | #== Illiquid return ==# 134 | equmSS[:ra] = equmSS[:rcapital] - δbar + equmSS[:divrate] 135 | 136 | #== Government ==# 137 | equmSS[:lumptransfer] = 0.20 * equmSS[:wage] * equmSS[:labor] 138 | 139 | ### Solution ### 140 | 141 | return equmSS 142 | 143 | end 144 | 145 | _unpackprices(equmSS::Dict{Symbol, Float64}) = 146 | Prices(equmSS[:rcapital], equmSS[:ra], equmSS[:rb], equmSS[:wedge] , equmSS[:wage]) 147 | 148 | ################################## 149 | ## OLD AGGREGATE vars 150 | ################################## 151 | 152 | """ 153 | Holds the aggregate variables of the econmy 154 | 155 | ### Prices 156 | 157 | - `prices` 158 | 159 | ### Government 160 | - `Rev::Float64` 161 | - `τ::Float64` 162 | - `T::Float64` 163 | 164 | ### Firm 165 | - `tfp::Float64` 166 | - `KY::Float64` 167 | - `KN::Float64` 168 | 169 | - `output::Float64` 170 | - `profit::Float64` 171 | - `capital::Float64` 172 | - `investment::Float64` 173 | 174 | """ 175 | type AggVar 176 | 177 | prices::Prices 178 | 179 | ##Gov 180 | Rev::Float64 181 | τ ::Float64 182 | T ::Float64 183 | G ::Float64 184 | 185 | ## Firm 186 | tfp::Float64 187 | KY ::Float64 188 | KN ::Float64 189 | 190 | output::Float64 191 | profit::Float64 192 | capital::Float64 193 | investment::Float64 194 | 195 | end 196 | 197 | function AggVar(;tfp::Float64 = 1., KY::Float64 = 5., 198 | τ::Float64 = 0.25, rᴮ::Float64 = 0.03, wedge::Float64 = 0.09) 199 | 200 | KN = (tfp * KY)^(1./(1.-α)) 201 | 202 | #== Prices ==# 203 | rᴷ = mc * α / KY 204 | w = mc * tfp * (1. - α) * KN^α 205 | priceadjust = 0 206 | ℓ = 1.0 207 | capital = KN * ℓ 208 | investment = δbar * capital 209 | profit = (1. - mc)* capital / KY - priceadjust 210 | divr = profit / capital 211 | 212 | rᴬ = rᴷ - δbar + divr 213 | 214 | # Define this so that a fixed fraction receives more transfer than pays 215 | T = 0.8 * w 216 | 217 | output = 0. 218 | Rev = 0.0 219 | G = 0.0 220 | 221 | AggVar(Prices(rᴷ,rᴬ,rᴮ,wedge,w), Rev, τ, T, G, tfp, KY, KN, 222 | output, profit, capital, investment) 223 | end 224 | -------------------------------------------------------------------------------- /lecture13/felipe_alves_codes/main_fig.jl: -------------------------------------------------------------------------------- 1 | using PyPlot 2 | using QuantEcon: meshgrid 3 | 4 | 5 | 6 | #== Value Function ==# 7 | function fig_vf(fd::FDSpec) 8 | b = fd.b; a = fd.a; z = fd.z; 9 | 10 | an = length(a) 11 | bn = length(b) 12 | zn = length(z) 13 | V = vcat(fd.V...) 14 | V = reshape(V,bn,an,zn) 15 | p_args = Dict{Symbol,Float64}(:lw => 2, :alpha =>0.7) 16 | 17 | fig, ax = subplots() 18 | for ai in 1:7:an 19 | ax[:plot](fd.b[end-10:end], V[end-10:end,ai,1] ; p_args...) 20 | end 21 | # ax[:legend](loc= "upper right", fontsize = 11) 22 | ax[:set_xlabel]("Liquid asset") 23 | fig[:show]() 24 | 25 | fig, ax = subplots() 26 | for bi in 1:7:an 27 | ax[:plot](fd.a, squeeze(V[bi,:,1],1) ; p_args...) 28 | end 29 | # ax[:legend](loc= "upper right", fontsize = 11) 30 | ax[:set_xlabel]("Iliquid asset") 31 | fig[:show]() 32 | end 33 | 34 | """ 35 | Compute the figures... 36 | """ 37 | function fig_pol(fd::FDSpec) 38 | 39 | sol = fd.sol 40 | c = sol.c; d = sol.d; sc = sol.sc 41 | b = fd.b; a = fd.a; z = fd.z; 42 | 43 | an = length(a) 44 | bn = length(b) 45 | zn = length(z) 46 | 47 | netainc = fd.netainc 48 | 49 | p_args = Dict{Symbol,Any}(:rstride=>2, :cstride=>2, :cmap=>ColorMap("Oranges"), :alpha=>0.7, :linewidth=>0.5) 50 | pp_args = Dict{Symbol,Float64}(:alpha=>0.7, :linewidth=>2) 51 | agrid, bgrid = meshgrid(a,b) 52 | 53 | #== Consumption 3D ==# 54 | cons_low = c[:,:,1] 55 | fig = figure(figsize = (8,6)) 56 | ax = fig[:gca](projection="3d") 57 | ax[:plot_surface](agrid, bgrid, cons_low; p_args...) 58 | ax[:set_xlabel]("illiquid") 59 | ax[:set_ylabel]("liquid") 60 | ax[:legend]() 61 | ax[:set_title]("Consumption") 62 | fig[:show]() 63 | 64 | #== Consumption 2D ==# 65 | fig, ax = subplots() 66 | for ai in 1:7:an 67 | ax[:plot](fd.b, c[:,ai,1] ; pp_args...) 68 | end 69 | # ax[:legend](loc= "upper right", fontsize = 11) 70 | ax[:set_xlabel]("Liquid asset") 71 | ax[:set_title]("Consumption Low Shock ") 72 | fig[:show]() 73 | 74 | fig, ax = subplots() 75 | for ai in 1:7:an 76 | ax[:plot](fd.b, c[:,ai,2] ; pp_args...) 77 | end 78 | # ax[:legend](loc= "upper right", fontsize = 11) 79 | ax[:set_xlabel]("Liquid asset") 80 | ax[:set_title]("Consumption High Shock") 81 | fig[:show]() 82 | 83 | #== Deposit ==# 84 | fig = figure(figsize = (8,6)) 85 | ax = fig[:gca](projection="3d") 86 | dep_high = d[:,:,end] 87 | ax[:plot_surface](agrid, bgrid, dep_high; p_args...) 88 | ax[:set_xlabel]("illiquid") 89 | ax[:set_ylabel]("liquid") 90 | ax[:legend]() 91 | ax[:set_title]("Deposit High") 92 | fig[:show]() 93 | 94 | #== Illiquid Savings ==# 95 | # adrift = zeros(d) 96 | # for zi in 1:zn, ai in 1:an, bi in 1:bn 97 | # adrift[bi,ai,zi] = d[bi, ai, zi] + netainc[ai,zi] 98 | # end 99 | # fig = figure(figsize = (8,6)) 100 | # ax = fig[:gca](projection="3d") 101 | # adrift_low = adrift[:,:,1] 102 | # ax[:plot_surface](agrid, bgrid, adrift_low; p_args...) 103 | # ax[:set_xlabel]("illiquid") 104 | # ax[:set_ylabel]("liquid") 105 | # ax[:legend]() 106 | # ax[:set_title]("Illiquid Saving") 107 | # fig[:show]() 108 | end 109 | 110 | function fig_dens(fd::FDSpec) 111 | 112 | a,b = fd.a, fd.b 113 | an = length(a) 114 | bn = length(b) 115 | zn = length(fd.z) 116 | gmat = reshape(vcat(fd.sol.g...),bn,an,zn) 117 | 118 | ΔTbgrid = fd.ΔTbgrid 119 | ΔTagrid = fd.ΔTagrid 120 | 121 | 122 | p_args = Dict{Symbol,Float64}(:lw => 2, :alpha =>0.7) 123 | #== Marginal wrt to a ==# 124 | adens = zeros(an,zn) 125 | for zi in 1:zn, ai in 1:an 126 | adens[ai,zi] = sum(gmat[:,ai,zi].*ΔTbgrid) 127 | end 128 | #= Check summation =# 129 | @printf("Check SUM \n") 130 | @printf("Sum to %.2f \n",sum(adens.*repmat(fd.ΔTagrid,1,zn))) 131 | 132 | Ea = sum( a.* sum(adens,2).* ΔTagrid ) 133 | fig, ax = subplots() 134 | for zi in 1:zn 135 | ax[:plot](fd.a, adens[:,zi], label="Labor shock $zi";p_args...) 136 | end 137 | ax[:legend](loc= "upper right", fontsize = 11) 138 | ax[:set_xlim]([0.0,2*Ea]) 139 | ax[:set_title]("Density Illiquid") 140 | ax[:set_xlabel]("Illiquid asset") 141 | fig[:show]() 142 | 143 | #== Marginal wrt to b ==# 144 | bdens = zeros(bn,zn) 145 | for zi in 1:zn, bi in 1:bn 146 | bdens[bi,zi] = sum(squeeze(gmat[bi,:,zi],1).*ΔTagrid) 147 | end 148 | #= Check summation =# 149 | @printf("Check SUM \n") 150 | @printf("Sum to %.2f \n",sum(bdens.*repmat(fd.ΔTbgrid,1,zn))) 151 | 152 | fig, ax = subplots() 153 | for zi in 1:zn 154 | ax[:plot](b, bdens[:,zi], label="Labor shock $zi";p_args...) 155 | end 156 | ax[:set_xlabel]("Liquid asset") 157 | ax[:set_ylim]([0,0.8*maximum(bdens[:,1])]) 158 | ax[:set_xlim]([b[1],5.0]) 159 | ax[:set_title]("Density Illiquid", fontsize = 20) 160 | ax[:legend](loc= "upper right", fontsize = 11) 161 | fig[:show]() 162 | 163 | bran_max = searchsortedfirst(b,5.0) 164 | # aran_max = searchsortedfirst(a,Ea) 165 | aran_max = searchsortedfirst(a,25.0) 166 | #== Joint density ==# 167 | arange, brange = 1:min(round(Int,aran_max),an), 1:bran_max 168 | # dens_low = gmat[brange,arange,1] 169 | # dens_high = gmat[brange,arange,2] 170 | 171 | #= Construct probabilities =# 172 | gvec = vcat(gmat) 173 | 174 | ΔTab = zeros(gvec) 175 | bvec = zeros(gvec) 176 | avec = zeros(gvec) 177 | ijk = zero(Int) 178 | 179 | for zi = 1:zn, abi = 1:bn*an 180 | ijk += 1 181 | ΔTab[ijk] = ΔTagrid[afromab(abi)] * ΔTbgrid[bfromab(abi)] 182 | bvec[ijk] = b[bfromab(abi)] 183 | avec[ijk] = a[afromab(abi)] 184 | end 185 | 186 | prob = gvec .* ΔTab 187 | ggmat = reshape(prob,bn,an,zn) 188 | 189 | dens = sum(ggmat[brange,arange,:],3) 190 | dens = squeeze(dens,3) 191 | dens2 = ggmat[brange,arange,2] 192 | 193 | #== Construct 3D figure ==# 194 | fig = figure(figsize = (8,6)) 195 | ax = fig[:gca](projection="3d") 196 | agrid, bgrid = meshgrid(a[arange],b[brange]) 197 | ax[:plot_surface](agrid, bgrid, dens, 198 | rstride=2,cstride=2, cmap=ColorMap("jet"), alpha=0.5, linewidth=0.5) 199 | ax[:set_xlabel]("Illiquid") 200 | ax[:set_ylabel]("Liquid") 201 | # ax[:set_xlim]([0,) 202 | # ax[:set_ylim]([-2,10]) 203 | ax[:legend]() 204 | fig[:show]() 205 | 206 | fig, ax = subplots(figsize = (8,6)) 207 | ax[:xaxis][:grid](true, zorder=0) 208 | ax[:yaxis][:grid](true, zorder=0) 209 | ax[:contourf](agrid, bgrid, dens, 5, alpha=0.6, cmap=ColorMap("jet")) 210 | cs1 = ax[:contour](agrid, bgrid, dens, 5, colors="black",lw=2) 211 | ax[:clabel](cs1, inline=1, fontsize=10) 212 | ax[:set_xlabel]("Illiquid") 213 | ax[:set_ylabel]("Liquid") 214 | # ax[:set_xlim]([0,) 215 | # ax[:set_ylim]([-2,10]) 216 | ax[:legend]() 217 | fig[:show]() 218 | 219 | # [0,0.005,0.01,0.02,0.04] 220 | 221 | end 222 | 223 | # function fig_pol(fd::TwoAssetsFD) 224 | # 225 | # c = fd.c; d = fd.d; sc = fd.sc 226 | # b = fd.b; a = fd.a; z = fd.z; 227 | # 228 | # an = length(a) 229 | # bn = length(b) 230 | # zn = length(z) 231 | # 232 | # p_args = Dict{Symbol,Any}(:rstride=>2, :cstride=>2, :cmap=>ColorMap("Oranges"), :alpha=>0.7, :linewidth=>0.5) 233 | # agrid, bgrid = meshgrid(a,b) 234 | # 235 | # #== Consumption ==# 236 | # cons_low = c[:,:,1] 237 | # fig = figure(figsize = (8,6)) 238 | # ax = fig[:gca](projection="3d") 239 | # ax[:plot_surface](agrid, bgrid, cons_low; p_args...) 240 | # ax[:set_xlabel]("illiquid") 241 | # ax[:set_ylabel]("liquid") 242 | # ax[:legend]() 243 | # fig[:show]() 244 | # 245 | # #== Deposit ==# 246 | # fig = figure(figsize = (8,6)) 247 | # ax = fig[:gca](projection="3d") 248 | # dep_low = d[:,:,1] 249 | # ax[:plot_surface](agrid, bgrid, dep_low; p_args...) 250 | # ax[:set_xlabel]("illiquid") 251 | # ax[:set_ylabel]("liquid") 252 | # ax[:legend]() 253 | # ax[:set_title]("Deposit") 254 | # fig[:show]() 255 | # end 256 | -------------------------------------------------------------------------------- /lecture13/felipe_alves_codes/solveHJB.jl: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | ## 3 | ## Solve hamilton-jacobi-bellman 4 | ## 5 | ############################################################################## 6 | 7 | 8 | ############################## 9 | ## 10 | ## FDSpec 11 | ## 12 | ############################## 13 | """ 14 | Use finite difference method to solve for HJB equation. 15 | 16 | ### INPUTS 17 | - `fd` : Finite Difference scheme 18 | - `sol` : holds solution information 19 | - `twoap` : household problem description 20 | """ 21 | function solve_hjb!(twoap::TwoAssetsProb, fd::FDSpec, sol::SolutionExp; 22 | maxit::Int = 400, crit::Float64 = 1e-8, monotonicity = true, verbose::Bool = true) 23 | 24 | bn, an, zn = length(fd.b), length(fd.a), length(fd.z) 25 | 26 | #== Initialize Vold ==# 27 | Vold = zeros(bn,an,zn) 28 | 29 | #== Construct functions ==# 30 | compute_pol!(solut, V_old) = comp_pol!(twoap, fd, solut, V_old) 31 | update_V!(solut, V_old) = updateV!(twoap, fd, solut, V_old, test_mon = monotonicity) 32 | 33 | for it in 1:maxit 34 | copy!(Vold, vcat(sol.V...)) 35 | # vcat is important in the FDExp case where fd.V is vector of vectors 36 | 37 | #== Compute the Optimal Policy ==# 38 | compute_pol!(sol, Vold) 39 | 40 | #== Update vⁿ ==# 41 | update_V!(sol, Vold) 42 | 43 | # check convergence 44 | distance = chebyshev(vcat(sol.V...), vec(Vold)) 45 | # distance = norm(vec(fd.V)-vec(Vold), Inf) 46 | if distance < crit 47 | println("hjb solved : $(it) iterations") 48 | # println(out_text,"hjb solved : $(it) iterations") 49 | break 50 | else 51 | # update V using newV 52 | verbose && (it % 10 == 0) && @printf(" value function iteration %d, distance %.4f \n", it,distance) 53 | # verbose && (it % 10 == 0) && @printf(out_text," value function iteration %d, distance %.4f \n", it,distance) 54 | end 55 | end 56 | end 57 | 58 | function optconsump(twoap::TwoAssetsProb, ∂Vb::Float64, liqinc::Float64, ℓutil::Float64) 59 | 60 | invγ = 1.0/twoap.γ 61 | 62 | # ∂Vb>=0 ? c = ∂Vb^(-invγ) : error("negative value not allowed") 63 | # REVIEW:110 trick to not get stuck in case of non-monotonicity 64 | ∂Vb>=0 ? c = ∂Vb^(-invγ) + ℓutil : c = liqinc 65 | sc = liqinc - c 66 | dV = utilfn(twoap, c, ℓutil) + sc*∂Vb 67 | 68 | return c, sc, dV 69 | end 70 | 71 | function comp_pol!(twoap::TwoAssetsProb, fd::FDSpec, sol::SolutionExp, V::Array{Float64,3}) 72 | 73 | #== Construct fns ==# 74 | dχinv1(∂ratio, a) = dχinv(twoap, ∂ratio, a) 75 | χ1(d,a) = χ(twoap, d, a) 76 | utilfn1(c, ℓutil) = utilfn(twoap, c, ℓutil) 77 | 78 | #== Check if function is changing ==# 79 | # println(dχinv1(1.1, 1.0)) 80 | # println(χ1(1e-5, 100.0)) 81 | 82 | #== Unpack from FD ==# 83 | z = fd.z; a = fd.a ; b = fd.b; 84 | Δbgrid = fd.Δbgrid 85 | Δagrid = fd.Δagrid 86 | netbinc = fd.netbinc 87 | 88 | ℓutilgrid = fd.ℓutilgrid 89 | 90 | # precompute the lengths 91 | bn = length(b); an = length(a); zn = length(z) 92 | 93 | invdb = 1.0./Δbgrid 94 | invda = 1.0./Δagrid 95 | 96 | for zi in 1:zn, ai in 1:an, bi in 1:bn 97 | 98 | # == Derivative w.r.t B == # 99 | bi1 && ( ∂Vbᴮ = ( V[bi, ai, zi] - V[bi-1, ai, zi] ) * invdb[bi-1] ) 101 | 102 | # == Derivative w.r.t A == # 103 | ai1 && ( ∂Vaᴮ = ( V[bi, ai, zi] - V[bi, ai-1, zi] ) * invda[ai-1] ) 105 | 106 | ### CONSUMPTION decision ### 107 | liqinc = netbinc[bi,zi] 108 | ℓutil = ℓutilgrid[zi] 109 | 110 | # == Foward == # 111 | if bi0.0 ? (validᶠ = 1 ) : (validᶠ = 0 ) 117 | 118 | # == Backward == # 119 | if bi>1 120 | cᴮ, scᴮ, Hcᴮ = optconsump(twoap, ∂Vbᴮ, liqinc, ℓutil) 121 | else 122 | scᴮ, Hcᴮ = 0.0, -1.0e12 123 | end 124 | scᴮ<0.0 ? (validᴮ = 1) : (validᴮ = 0) 125 | 126 | #== Catch Other cases ==# 127 | c⁰ = liqinc 128 | sc⁰ = 0.0 129 | Hc⁰ = utilfn1(c⁰, ℓutil) 130 | 131 | if validᶠ==1 && ( validᴮ==0 || Hcᶠ>=Hcᴮ ) && ( Hcᶠ>=Hc⁰ ) 132 | sol.c[bi, ai, zi] = cᶠ 133 | sol.sc[bi, ai, zi] = scᶠ 134 | elseif validᴮ==1 && ( validᶠ==0 || Hcᴮ>=Hcᶠ ) && ( Hcᴮ>=Hc⁰ ) 135 | sol.c[bi, ai, zi] = cᴮ 136 | sol.sc[bi, ai, zi] = scᴮ 137 | else 138 | sol.c[bi, ai, zi] = c⁰ 139 | sol.sc[bi, ai, zi] = sc⁰ 140 | end 141 | 142 | #== DEPOSIT decision ==# 143 | #== b backward, a forward ==# 144 | if bi>1 && ai0.0 && Hdᴮᶠ>=0.0) ? ( validᴮᶠ=1 ) : ( validᴮᶠ=0 ) 150 | else 151 | validᴮᶠ = 0; Hdᴮᶠ = -1.0e-12 152 | end 153 | 154 | #== b forward, a backward ==# 155 | if bi < bn && ai > 1 156 | dᶠᴮ = dχinv1(∂Vaᴮ/∂Vbᶠ, a[ai]) 157 | # change in utility 158 | Hdᶠᴮ = ∂Vaᴮ * dᶠᴮ - ∂Vbᶠ*( dᶠᴮ + χ1(dᶠᴮ,a[ai]) ) 159 | 160 | (dᶠᴮ<-χ1(dᶠᴮ,a[ai]) && Hdᶠᴮ>=0.0) ? ( validᶠᴮ=1 ) : ( validᶠᴮ=0 ) 161 | else 162 | validᶠᴮ = 0; Hdᶠᴮ = -1.0e-12 163 | end 164 | 165 | #== b backward, a backward ==# 166 | if bi>1 && ai>1 167 | dᴮᴮ = dχinv1(∂Vaᴮ/∂Vbᴮ, a[ai]) 168 | # change in utility 169 | Hdᴮᴮ = ∂Vaᴮ * dᴮᴮ - ∂Vbᴮ*( dᴮᴮ + χ1(dᴮᴮ,a[ai]) ) 170 | 171 | (dᴮᴮ>-χ1(dᴮᴮ, a[ai]) && dᴮᴮ<=0 && Hdᴮᴮ>=0.0) ? ( validᴮᴮ=1 ) : ( validᴮᴮ=0 ) 172 | 173 | # REVIEW:80 BOUNDARY Adjustment 01 174 | # force the use of ∂Vbᴮ in case bi==bn 175 | # bi == bn && (dᴮᴮ<=0 && Hdᴮᴮ>=0.0) && (validᴮᴮ = 1) 176 | else 177 | validᴮᴮ = 0; Hdᴮᴮ = -1.0e-12 178 | end 179 | 180 | #== Check and assign deposit ==# 181 | if validᴮᶠ==1 && (validᶠᴮ==0 || Hdᴮᶠ>=Hdᶠᴮ) && (validᴮᴮ==0 || Hdᴮᶠ>=Hdᴮᴮ) 182 | 183 | sol.d[bi, ai, zi] = dᴮᶠ 184 | # ------------------------------------------------------------------------------------------ # 185 | elseif (validᴮᶠ==0 || Hdᶠᴮ>=Hdᴮᶠ) && validᶠᴮ==1 && (validᴮᴮ==0 || Hdᶠᴮ>=Hdᴮᴮ) 186 | 187 | sol.d[bi, ai, zi] = dᶠᴮ 188 | # ------------------------------------------------------------------------------------------ # 189 | elseif (validᴮᶠ==0 || Hdᴮᴮ>=Hdᴮᶠ) && (validᶠᴮ==0 || Hdᴮᴮ>=Hdᶠᴮ) && validᴮᴮ==1 190 | 191 | sol.d[bi, ai, zi] = dᴮᴮ 192 | # ------------------------------------------------------------------------------------------ # 193 | elseif validᴮᶠ==0 && validᶠᴮ==0 && validᴮᴮ==0 194 | 195 | sol.d[bi, ai, zi] = 0.0 196 | end 197 | 198 | # u[bi, ai, zi] = utilfn1( c[bi, ai, zi] ) 199 | # bdot[bi, ai, zi] = sc[bi, ai, zi] - d[bi, ai, zi] - χ1(d, a[ai]) 200 | end 201 | 202 | Void 203 | end 204 | 205 | 206 | function updateV!(twoap::TwoAssetsProb, fd::FDExp, sol::SolutionExp, V::Array{Float64,3}; test_mon = true) 207 | 208 | #== Construct function ==# 209 | χ1(d,a) = χ(twoap, d, a) 210 | utilfn1(c, ℓutil) = utilfn(twoap, c, ℓutil) 211 | #== Idiosyncratic component ==# 212 | λdiag = fd.λdiag 213 | λtoff = fd.λoff' 214 | 215 | #== HOUSEHOLD Parameters ==# 216 | γ, ρ, _, σ, ψ = _unpackparams(twoap) 217 | 218 | #== Optimal policies ==# 219 | c = sol.c; sc = sol.sc; d = sol.d 220 | 221 | #== Extract GRIDS ==# 222 | b = fd.b; a = fd.a; z = fd.z 223 | Δbgrid, Δagrid = fd.Δbgrid, fd.Δagrid 224 | netainc = fd.netainc; 225 | ℓutilgrid = fd.ℓutilgrid 226 | 227 | invΔ = fd.invΔ 228 | 229 | #== SOLUTION Matrices to fill ==# 230 | Vnew = sol.V 231 | A = sol.A 232 | Au = sol.Au 233 | 234 | #== Storage matrices ==# 235 | B = fd.B 236 | b̃ = fd.b̃ 237 | 238 | # precompute the lengths 239 | bn = length(b); an = length(a); zn = length(z); abn = bn*an 240 | 241 | invdb = 1.0./Δbgrid 242 | invda = 1.0./Δagrid 243 | inv1γ = 1.0/(1.0-γ) 244 | 245 | for zi in 1:zn 246 | #== set values to zero ==# 247 | fill!(nonzeros(A[zi]), zero(Float64)) 248 | fill!(nonzeros(Au[zi]), zero(Float64)) 249 | 250 | for abi in 1:abn 251 | # NOTE:90 Positions of A that I need to fill up 252 | # A[zi][abi-bn, abi] - v(i ,j-1,k) --> - invda*[ d⁻ ] (SHOULD BE POSITIVE) 253 | # A[zi][abi-1 , abi] - v(i+1,j ,k) --> - invdb*[ (scᴮ)⁻ + (sdᴮ)⁻ ] (SHOULD BE POSITIVE) 254 | # A[zi][abi , abi] - v(i ,j ,k) --> + invdb*[ (scᴮ)⁻ + (sdᴮ)⁻ - ( (scᶠ)⁺ - (sdᶠ)⁺ ) ] 255 | # + invda*[ d⁻ - (d⁺ + ξwz + rᵃa) ] 256 | # A[zi][abi+1 , abi] - v(i-1,j ,k) --> + invdb*[ (scᶠ)⁺ + (sdᶠ)⁺ ] (SHOULD BE POSITIVE) 257 | # A[zi][abi+bn, abi] - v(i .j+1,k) --> + invda*[ d⁺ + ξwx + rᵃa ] (SHOULD BE POSITIVE) 258 | 259 | ai = afromab(abi) 260 | bi = bfromab(abi) 261 | ℓutil = ℓutilgrid[zi] 262 | 263 | #== RHS of the Bellman equation==# 264 | b̃[zi][abi] = utilfn1(c[bi, ai, zi], ℓutil) + V[bi, ai, zi] * invΔ + dot(λtoff[:,zi] ,V[bi,ai,:][:] ) 265 | 266 | ## COMPUTE THE ENTRIES ## 267 | # ================================================================== 268 | #== bdrift ==# 269 | bdriftᶠ = max( sc[bi, ai, zi], 0.0 ) + max( -d[bi, ai, zi] -χ1(d[bi, ai, zi], a[ai]) , 0.0 ) 270 | bdriftᴮ = min( sc[bi, ai, zi], 0.0 ) + min( -d[bi, ai, zi] -χ1(d[bi, ai, zi], a[ai]) , 0.0 ) 271 | 272 | bi < bn && ( budriftᶠ = max( sc[bi, ai, zi] - d[bi, ai, zi] - χ1(d[bi, ai, zi], a[ai]) , 0.0 ) ) 273 | bi < bn && ( budriftᴮ = min( sc[bi, ai, zi] - d[bi, ai, zi] - χ1(d[bi, ai, zi], a[ai]) , 0.0 ) ) 274 | bi == bn && ( budriftᶠ = max( sc[bi, ai, zi] - d[bi-1, ai, zi] - χ1(d[bi-1, ai, zi], a[ai]) , 0.0 ) ) 275 | bi == bn && ( budriftᴮ = min( sc[bi, ai, zi] - d[bi-1, ai, zi] - χ1(d[bi-1, ai, zi], a[ai]) , 0.0 ) ) 276 | 277 | #== adrift ==# 278 | adriftᶠ = max( d[bi, ai, zi], 0.0 ) + netainc[ai,zi] 279 | adriftᴮ = min( d[bi, ai, zi], 0.0 ) 280 | 281 | bi < bn && ( audriftᶠ = max( d[bi, ai, zi] + netainc[ai,zi], 0.0 ) ) 282 | bi < bn && ( audriftᴮ = min( d[bi, ai, zi] + netainc[ai,zi], 0.0 ) ) 283 | bi == bn && ( audriftᶠ = max( d[bi-1, ai, zi] + netainc[ai,zi], 0.0 ) ) 284 | bi == bn && ( audriftᴮ = min( d[bi-1, ai, zi] + netainc[ai,zi], 0.0 ) ) 285 | 286 | #== BOUNDARY Adjustment 02 ==# 287 | # bi < bn && ( bdriftᴮ = min( sc[bi, ai, zi], 0.0 ) + min( -d[bi, ai, zi] -χ1(d[bi, ai, zi], a[ai]) , 0.0 ) ) 288 | # bi == bn && ( bdriftᴮ = min( sc[bi, ai, zi], 0.0 ) -d[bi, ai, zi] -χ1(d[bi, ai, zi], a[ai]) ) 289 | #== BOUNDARY Adjustment 03 ==# 290 | # ai < an && ( adriftᴮ = min( d[bi, ai, zi], 0.0 ) ) 291 | # ai == an && ( adriftᴮ = d[bi, ai, zi] + netainc[ai,zi] ) 292 | 293 | # v[i, j- 1, k] 294 | if adriftᴮ != 0.0 && ai>1 295 | row = abfromab(ai-1,bi) 296 | A[zi][row, abi] = -adriftᴮ * invda[ai-1] 297 | end 298 | 299 | # v[i, j- 1, k] KF 300 | if audriftᴮ != 0.0 && ai>1 301 | row = abfromab(ai-1,bi) 302 | Au[zi][row, abi] = -audriftᴮ * invda[ai-1] 303 | end 304 | 305 | # v[i-1, j, k] 306 | if bdriftᴮ != 0.0 && bi>1 307 | row = abfromab(ai,bi-1) 308 | A[zi][row, abi] = -bdriftᴮ * invdb[bi-1] 309 | end 310 | 311 | # v[i-1, j, k] KF 312 | if budriftᴮ != 0.0 && bi>1 313 | row = abfromab(ai,bi-1) 314 | Au[zi][row, abi] = -budriftᴮ * invdb[bi-1] 315 | end 316 | 317 | # v[i, j, k] 318 | val = 0.0 319 | ( bdriftᴮ != 0.0 && bi > 1 ) && ( val += bdriftᴮ * invdb[bi-1] ) 320 | ( bdriftᶠ != 0.0 && bi < bn) && ( val -= bdriftᶠ * invdb[bi] ) 321 | ( adriftᴮ != 0.0 && ai > 1) && ( val += adriftᴮ * invda[ai-1] ) 322 | ( adriftᶠ != 0.0 && ai < an) && ( val -= adriftᶠ * invda[ai] ) 323 | val != 0.0 && ( A[zi][abi, abi] = val ) 324 | 325 | # v[i, j, k] KF 326 | val = 0.0 327 | ( budriftᴮ != 0.0 && bi > 1 ) && ( val += budriftᴮ * invdb[bi-1] ) 328 | ( budriftᶠ != 0.0 && bi < bn) && ( val -= budriftᶠ * invdb[bi] ) 329 | ( audriftᴮ != 0.0 && ai > 1) && ( val += audriftᴮ * invda[ai-1] ) 330 | ( audriftᶠ != 0.0 && ai < an) && ( val -= audriftᶠ * invda[ai] ) 331 | val != 0.0 && ( Au[zi][abi, abi] = val ) 332 | 333 | # v[i-1, j, k] 334 | if bdriftᶠ != 0.0 && bi1 387 | Vnew[zi][abi]1 393 | Vnew[zi][abi] - invda*[ d⁻ ] 445 | # A[ijk- 1, ijk] - v(i+1,j ,k) --> - invdb*[ (scᴮ)⁻ + (sdᴮ)⁻ ] 446 | # A[ijk , ijk] - v(i ,j ,k) --> invdb*[ (scᴮ)⁻ + (sdᴮ)⁻ -(scᶠ)⁺ -(sdᶠ)⁺] + invda*[ d⁻ -(d⁺ + ξwz + rᵃa) ] 447 | # A[ijk+ 1, ijk] - v(i-1,j ,k) --> invdb*[ (scᶠ)⁺ + (sdᶠ)⁺ ] 448 | # A[ijk+bn, ijk] - v(i .j+1,k) --> invda*[ d⁺ + ξwx + rᵃa ] 449 | 450 | ijk +=1 451 | 452 | ai = afromab(abi) 453 | bi = bfromab(abi) 454 | ℓutil = ℓutilgrid[zi] 455 | 456 | #== RHS of the Bellman equation==# 457 | b̃[ijk] = utilfn1(c[bi, ai, zi], ℓutil) + V[bi, ai, zi] * invΔ 458 | # ================================================================== 459 | 460 | ### COMPUTE THE ENTRIES ### 461 | 462 | #== bdrift ==# 463 | bdriftᶠ = max( sc[bi, ai, zi], 0.0 ) + max( -d[bi, ai, zi] -χ1(d[bi, ai, zi], a[ai]) , 0.0 ) 464 | bdriftᴮ = min( sc[bi, ai, zi], 0.0 ) + min( -d[bi, ai, zi] -χ1(d[bi, ai, zi], a[ai]) , 0.0 ) 465 | #== bdrift for KF ==# 466 | bi < bn && ( budriftᶠ = max( sc[bi, ai, zi] - d[bi, ai, zi] - χ1(d[bi, ai, zi], a[ai]) , 0.0 ) ) 467 | bi < bn && ( budriftᴮ = min( sc[bi, ai, zi] - d[bi, ai, zi] - χ1(d[bi, ai, zi], a[ai]) , 0.0 ) ) 468 | bi == bn && ( budriftᶠ = max( sc[bi, ai, zi] - d[bi-1, ai, zi] - χ1(d[bi-1, ai, zi], a[ai]) , 0.0 ) ) 469 | bi == bn && ( budriftᴮ = min( sc[bi, ai, zi] - d[bi-1, ai, zi] - χ1(d[bi-1, ai, zi], a[ai]) , 0.0 ) ) 470 | 471 | #== adrift ==# 472 | adriftᶠ = max( d[bi, ai, zi], 0.0 ) + netainc[ai,zi] 473 | adriftᴮ = min( d[bi, ai, zi], 0.0 ) 474 | # for KF 475 | bi < bn && ( audriftᶠ = max( d[bi, ai, zi] + netainc[ai,zi], 0.0 ) ) 476 | bi < bn && ( audriftᴮ = min( d[bi, ai, zi] + netainc[ai,zi], 0.0 ) ) 477 | bi == bn && ( audriftᶠ = max( d[bi-1, ai, zi] + netainc[ai,zi], 0.0 ) ) 478 | bi == bn && ( audriftᴮ = min( d[bi-1, ai, zi] + netainc[ai,zi], 0.0 ) ) 479 | 480 | #== REVIEW:100 BOUNDARY ADJUSTMENT 02 ==# 481 | # bi < bn && ( bdriftᴮ = min( sc[bi, ai, zi], 0.0 ) + min( -d[bi, ai, zi] -χ1(d[bi, ai, zi], a[ai]) , 0.0 ) ) 482 | # bi == bn && ( bdriftᴮ = min( sc[bi, ai, zi], 0.0 ) -d[bi, ai, zi] -χ1(d[bi, ai, zi], a[ai]) ) 483 | #== REVIEW:100 BOUNDARY ADJUSTMENT 03 ==# 484 | # ai < an && ( adriftᴮ = min( d[bi, ai, zi], 0.0 ) ) 485 | # ai == an && ( adriftᴮ = d[bi, ai, zi] + netainc[ai,zi] ) 486 | 487 | # v[i, j- 1, k] 488 | if adriftᴮ != 0.0 && ai>1 489 | row = abzfromabz(ai-1,bi,zi) 490 | A[row, ijk] = -adriftᴮ * invda[ai-1] 491 | end 492 | 493 | # v[i, j- 1, k] KF 494 | if audriftᴮ != 0.0 && ai>1 495 | row = abzfromabz(ai-1,bi,zi) 496 | Au[row, ijk] = -audriftᴮ * invda[ai-1] 497 | end 498 | 499 | # v[i-1, j, k] 500 | if bdriftᴮ != 0.0 && bi>1 501 | row = abzfromabz(ai,bi-1,zi) 502 | A[row,ijk] = -bdriftᴮ*invdb[bi-1] 503 | end 504 | 505 | # v[i-1, j, k] KF 506 | if budriftᴮ != 0.0 && bi>1 507 | row = abzfromabz(ai,bi-1,zi) 508 | Au[row,ijk] = -budriftᴮ*invdb[bi-1] 509 | end 510 | 511 | # v[i, j, k] 512 | val = 0.0 513 | ( bdriftᴮ != 0.0 && bi > 1 ) && ( val += bdriftᴮ * invdb[bi-1] ) 514 | ( bdriftᶠ != 0.0 && bi < bn) && ( val -= bdriftᶠ * invdb[bi] ) 515 | ( adriftᴮ != 0.0 && ai > 1) && ( val += adriftᴮ * invda[ai-1] ) 516 | ( adriftᶠ != 0.0 && ai < an) && ( val -= adriftᶠ * invda[ai] ) 517 | val != 0.0 && ( A[ijk, ijk] = val ) 518 | 519 | # v[i, j, k] KF 520 | val = 0.0 521 | ( budriftᴮ != 0.0 && bi > 1 ) && ( val += budriftᴮ * invdb[bi-1] ) 522 | ( budriftᶠ != 0.0 && bi < bn) && ( val -= budriftᶠ * invdb[bi] ) 523 | ( audriftᴮ != 0.0 && ai > 1) && ( val += audriftᴮ * invda[ai-1] ) 524 | ( audriftᶠ != 0.0 && ai < an) && ( val -= audriftᶠ * invda[ai] ) 525 | val != 0.0 && ( Au[ijk, ijk] = val ) 526 | 527 | # v[i-1, j, k] 528 | if bdriftᶠ != 0.0 && bi0)*rᴮ⁺ + ( 1-(b[bi]>0) )*rᴮ⁻ # set the interest rate 664 | # 665 | # ## == Derivative w.r.t B == ## 666 | # bi1 ? ( ∂Vbᴮ = ( V[bi, ai, zi] - V[bi-1, ai, zi] ) * invdb ) : ( ∂Vbᴮ = ( ( 1-ξ ) * w * z[zi] + rᴮ⁻ * b[1] )^(-γ) ) 668 | # 669 | # ## == Derivative w.r.t A == ## 670 | # ai1 ? ( ∂Vaᴮ = ( V[bi, ai, zi] - V[bi, ai-1, zi] ) * invda ) : ∂Vaᴮ = zero(Float64) 672 | # 673 | # ## CONSUMPTION decision ## 674 | # cᶠ = ∂Vbᶠ^(-invγ); cᴮ = ∂Vbᴮ^(-invγ) 675 | # scᴮ = (1-ξ) * z[zi] * w + b[bi] * rᴮ - cᴮ 676 | # scᶠ = (1-ξ) * z[zi] * w + b[bi] * rᴮ - cᶠ 677 | # 678 | # ## DEPOSIT decision ## 679 | # dᴮᴮ = foc_dep( ∂Vbᴮ, ∂Vaᴮ, a[ai] ) 680 | # dᴮᶠ = foc_dep( ∂Vbᴮ, ∂Vaᶠ, a[ai] ) 681 | # dᶠᴮ = foc_dep( ∂Vbᶠ, ∂Vaᴮ, a[ai] ) 682 | # dᶠᶠ = foc_dep( ∂Vbᶠ, ∂Vaᶠ, a[ai] ) 683 | # 684 | # # DOUBT: having a hard time to understand these constraints... 685 | # # Although the deposits are computed for bi=1,bn, later on 686 | # # these are set to zero when doing upwind for b 687 | # 688 | # #== eq (13),(14) + set the boundaries in a ==# 689 | # dᴮ = min( dᴮᴮ, 0.0 ) + max(dᴮᶠ, 0.0) # eq (13)/ note dᴮᴮ > dᴮᶠ 690 | # ai==1 && ( dᴮ = (dᴮᶠ>0)*dᴮᶠ ) # Moll: make sure d>=0 at amin, don't use VaB(:,1,:) 691 | # ai==an && ( dᴮ = (dᴮᴮ<0)*dᴮᴮ ) # Moll: make sure d<=0 at amax, don't use VaF(:,J,:) 692 | # 693 | # dᶠ = min( dᶠᴮ,0.0 ) + max(dᶠᶠ, 0.0) # eq (14) 694 | # ai==1 && ( dᶠ = (dᶠᶠ>0)*dᶠᶠ ) 695 | # ai==an && ( dᶠ = (dᶠᴮ<0)*dᶠᴮ ) 696 | # 697 | # sdᴮ = - dᴮ - χ(dᴮ,a[ai]) 698 | # sdᶠ = - dᶠ - χ(dᶠ,a[ai]) 699 | # bi==bn && ( sdᶠ = min(sdᶠ,0.0) ) 700 | # 701 | # # == consumption UPWIND SCHEME == # 702 | # c₀ = (1-ξ) * z[zi] * w + b[bi] * rᴮ 703 | # Icᴮ = (scᴮ<0) ; Icᶠ = (scᶠ>0 && scᴮ>=0); Ic⁰ = 1 - Icᴮ - Icᶠ 704 | # 705 | # c = Icᴮ*cᴮ + Icᶠ*cᶠ + Ic⁰*c₀ 706 | # u[ij] = inv1γ * c^(1-γ) + invΔ * V[ij] # notice that V[ij] still valid 707 | # 708 | # # == dep UPWIND SCHEME for Vb == # 709 | # Idᶠ = (sdᶠ>0); Idᴮ = (sdᴮ<0 && sdᶠ<=0) 710 | # 711 | # # NOTE:30 Boundary constraints of b 712 | # bi==1 && ( Idᴮ =0 ) # make sure don't use ∂Vbᴮ if ib=1 for deposit decision 713 | # bi==bn && ((Idᴮ,Idᶠ)=(1,0)) # make sure don't use ∂Vbᶠ if ib=bn for deposit decision 714 | # # and FORCE ∂Vbᴮ is used at bn for deposit decision 715 | # Id⁰ = 1 - Idᶠ - Idᴮ # INACTION Region 716 | # 717 | # #== dep UPWINDE SCHEMEq for Va- eq (17) ==# 718 | # d⁻ = min(Idᴮ * dᴮᴮ + Idᶠ * dᶠᴮ,0.0) # use backward for ∂Va 719 | # d⁺ = max(Idᴮ * dᴮᶠ + Idᶠ * dᶠᶠ,0.0) # use forward for ∂Va 720 | # mf = d⁺ + ξ*w*z[zi] + rᴬ*a[ai] 721 | # 722 | # # NOTE:40 Boundary constraints of a 723 | # # Aggregate all terms in ∂Vaᴮ at an 724 | # ai==an && (mf = 0; d⁻ = ξ*w*z[zi] + rᴬ*a[an] + d⁻) 725 | # 726 | # # NOTE:50 Positions of A that I need to fill 727 | # # A[ij-bn, ij] - v(i ,j-1,k) --> - invda*[ d⁻ ] 728 | # # A[ij- 1, ij] - v(i+1,j ,k) --> - invdb*[(scᴮ)⁻ + (sdᴮ)⁻ ] 729 | # # A[ij , ij] - v(i ,j ,k) --> invdb*[ (scᴮ)⁻ - (scᶠ)⁺ + (sdᴮ)⁻ - (sdᶠ)⁺ ] + invda*[ d⁻ - (d⁺ + ξwz + rᵃa) ] 730 | # # A[ij+ 1, ij] - v(i-1,j ,k) --> invdb*[ (scᶠ)⁺ + (sdᶠ)⁺ ] 731 | # # A[ij+bn, ij] - v(i .j+1,k) --> invda*[ d⁺ + ξwx + rᵃa ] 732 | # # 733 | # # the matrix a is changing its entrances... Note that code from aigary only changed when 734 | # # it was nonzero... 735 | # 736 | # current = Array(Float64,0) 737 | # zi == 2 && push!(current, 0.0) 738 | # 739 | # ij>bn && ( ai>1 ? push!(current, - invda * d⁻ ) : push!(current, 0.0) ) 740 | # ij>1 && ( bi>1 ? push!(current, - invdb *( Icᴮ*scᴮ + Idᴮ*sdᴮ ) ) : push!(current, 0.0) ) 741 | # push!(current, invdb *( Icᴮ*scᴮ + Idᴮ*sdᴮ - Icᶠ*scᶠ - Idᶠ*sdᶠ ) + invda *( d⁻ - mf ) ) 742 | # ijtolFK && it<=maxit 50 | g̃ = gold * C 51 | updateg!(gout, g̃, B) 52 | 53 | ## Aggregating household decisions ## 54 | # copy!(gvec_old, gold) 55 | # copy!(gvec_out, gout) 56 | 57 | # Eaold = sum(avec .* gvec_out .* ΔTab) 58 | # Eaout = sum(avec .* gvec_out .* ΔTab) 59 | # Eb = sum(bvec .* gvec .* ΔTab) 60 | 61 | distance = chebyshev(vec(gout), vec(gold)) 62 | # distance = abs(Eaold - Eaout) 63 | 64 | if distance < tolFK 65 | println("kfe solved : $(it) iterations") 66 | # println(out_text,"kfe solved : $(it) iterations") 67 | break 68 | else 69 | (it % 50 == 0) && @printf(" density iteration %d, distance %.4f \n", it, distance) 70 | (it % 50 == 0) && @printf(" sum %.4f --> %.4f\n", sum(gout'*ΔTab), sum(gold'*ΔTab) ) 71 | # (it % 50 == 0) && @printf(out_text," density iteration %d, distance %.4f \n", it, distance) 72 | # (it % 50 == 0) && @printf(out_text," sum %.4f --> %.4f\n", sum(gout'*ΔTab), sum(gold'*ΔTab) ) 73 | end 74 | 75 | copy!(gold, gout) 76 | it += 1 77 | end 78 | 79 | #== Update the converged density on solution ==# 80 | for zi=1:zn 81 | sol.g[zi] = gout[:,zi] 82 | end 83 | 84 | return Void 85 | end 86 | 87 | function updateg!(gout, g̃, B) 88 | 89 | zn = size(gout,2) 90 | for zi=1:zn 91 | #== Solve the system B(k) gⁿ⁺¹ (k) = C[] ==# 92 | gout[:,zi] = B[zi] \ g̃[:,zi] 93 | end 94 | return Void 95 | end 96 | 97 | function fillB!(fd::FDExp, sol::SolutionExp) 98 | 99 | abn = length(fd.a)*length(fd.b) 100 | zn = length(fd.z) 101 | #= Construct a vector nb*na with the terms Δb̃[bi] * Δã[ai] =# 102 | ΔTab = fd.ΔTab 103 | 104 | #= D matrix =# 105 | D = ΔTab 106 | Dinv = 1./ΔTab 107 | # ========================================================================= 108 | 109 | ### Allocate LHS/RHS matrices ### 110 | ## ........................................................................ 111 | 112 | #== Back out information from fd ==# 113 | λdiag = fd.λdiag 114 | invΔᴷ = fd.invΔᴷ 115 | 116 | #== matrices to be filled ==# 117 | Au = sol.Au # intensity matrix Au 118 | A = sol.A # use A from HJB as storage 119 | B = fd.B # storage matrix 120 | 121 | for zi = 1:zn 122 | Avals = nonzeros(A[zi]) 123 | fill!(Avals, zero(Float64)) 124 | 125 | Auvals = nonzeros(Au[zi]) 126 | Arows = rowvals(Au[zi]) 127 | for abi in 1:abn 128 | for k in nzrange(Au[zi], abi) 129 | row = Arows[k] 130 | current = Dinv[row]*Auvals[k]*D[abi] 131 | Avals[k] = current 132 | end 133 | end 134 | end 135 | 136 | @inbounds for zi in 1:zn 137 | 138 | Avals = nonzeros(A[zi]) 139 | Bvals = nonzeros(B[zi]) 140 | 141 | #== erase Bvals ==# 142 | fill!(Bvals, zero(Float64)) 143 | 144 | Brows = rowvals(B[zi]) 145 | for abi in 1:abn 146 | for k in nzrange(B[zi], abi) 147 | # loop over elements in the column ij 148 | row = Brows[k] 149 | Bvals[k] = -Avals[k] 150 | row == abi && (Bvals[k] += invΔᴷ - λdiag[zi]) 151 | #NOTE:110 `λdiag` is entering only in this part of the code 152 | end 153 | end 154 | end 155 | # ========================================================================= 156 | 157 | return Void 158 | end 159 | 160 | 161 | 162 | 163 | function solve_fp!(fd::FDImp; maxit::Int64 = 400, tolFK::Float64 = 1e-5) 164 | 165 | #== Back out information ==# 166 | invΔᴷ = fd.invΔᴷ 167 | Au = fd.Au # use Au 168 | B = fd.B # storage matrix 169 | 170 | ΔTbgrid = fd.ΔTbgrid 171 | ΔTagrid = fd.ΔTagrid 172 | 173 | abn = length(ΔTbgrid) * length(ΔTagrid) 174 | zn = length(fd.z) 175 | 176 | #== Construct a vector nb*na with the terms Δb̃[bi] * Δã[ai] ==# 177 | ΔTab = zeros(fd.g⁰) 178 | ijk = zero(Int) 179 | for zi = 1:zn 180 | 181 | for abi = 1:abn 182 | ijk += 1 183 | ΔTab[ijk] = ΔTagrid[afromab(abi)] * ΔTbgrid[bfromab(abi)] 184 | end 185 | end 186 | D = ΔTab 187 | Dinv = 1./ΔTab 188 | 189 | Avals = nonzeros(Au) 190 | Arows = rowvals(Au) 191 | ijk = zero(Int) 192 | for zi in 1:zn, abi in 1:abn 193 | ijk += 1 194 | 195 | for k in nzrange(Au, ijk) 196 | row = Arows[k] 197 | current = Dinv[row]*Avals[k]*D[ijk] 198 | Avals[k] = current 199 | end 200 | end 201 | 202 | Bvals = nonzeros(B); fill!(Bvals, zero(Float64)) 203 | Brows = rowvals(B) 204 | ijk = zero(Int) 205 | @inbounds for zi in 1:zn, abi in 1:abn 206 | ijk += 1 207 | for k in nzrange(B, ijk) 208 | # loop over elements in the column ij 209 | row = Brows[k] 210 | Bvals[k] = - Avals[k] 211 | row == ijk && (Bvals[k] += invΔᴷ) 212 | end 213 | end 214 | 215 | gold = copy(fd.g⁰) 216 | gout = zeros(gold) 217 | it = 1 218 | distance = 1 219 | while distance>tolFK && it<=maxit 220 | g̃ = gold * invΔᴷ 221 | gout = B \ g̃ 222 | 223 | distance = chebyshev(vec(gout), vec(gold)) 224 | 225 | if it % 5 == 0 226 | @printf("Density iteration %d, distance %.4f \n", it, distance) 227 | 228 | #== Check if the summation is held constant across iterations ==# 229 | @printf("sum gold %.4f \n", sum(gold'*ΔTab)) 230 | @printf("sum gout %.4f \n", sum(gout'*ΔTab)) 231 | end 232 | copy!(gold, gout) 233 | it += 1 234 | end 235 | 236 | fd.g = gout 237 | 238 | return Void 239 | end 240 | 241 | # function solve_fp!(fd::TwoAssetsFD) 242 | # 243 | # g⁰ = fd.g⁰ 244 | # Δa = fd.Δa; Δb = fd.Δb 245 | # 246 | # # == A' in the notes == # 247 | # A = fd.A 248 | # 249 | # i_fix = 1 250 | # Aurows = rowvals(A) 251 | # Auvals = nonzeros(A) 252 | # @inbounds for ij in 1:size(A, 2) 253 | # for k in nzrange(A, ij) 254 | # Arows[k] == i_fix && ( Avals[k] = zero(Float64) ) 255 | # end 256 | # end 257 | # A[i_fix,i_fix] = one(Float64) 258 | # 259 | # # == solve system Ag = b == # 260 | # gg = A \ g⁰ 261 | # 262 | # total = 1/( Δb*Δa*sum(gg) ) 263 | # fd.gg = total * gg 264 | # 265 | # return Void 266 | # end 267 | -------------------------------------------------------------------------------- /lecture13/felipe_alves_codes/testing.jl: -------------------------------------------------------------------------------- 1 | #== INCLUDE files ==# 2 | include("aggregate.jl") 3 | include("twoassets.jl") 4 | 5 | include("solveHJB.jl") 6 | include("solveKFE.jl") 7 | 8 | #== Instance of prices ==# 9 | pr = Prices(0.0, 0.04, 0.03, 0.09, 8.0) 10 | 11 | #== Instance of TwoAssetProblem ==# 12 | twoap = TwoAssetsProb2(pr; γ = 2.0, ρ = 0.06, ξ = 0.10, χ₀ = 0.08, χ₁= 3.0, τ = 0.0, T = 0.0) 13 | 14 | #== Create and FiniteDifference Structure ==# 15 | fde = FDExp(twoap; z = [.8, 1.3], λ = [-1/3 1/3; 1/3 -1/3], 16 | fixedΔa = true, fixedΔb = false, bn = 100, bmin = -2.0, bmax = 40.0, an =70, amax = 70.0, invΔᴷ = 0.05) 17 | 18 | #== Solve the HJB equation ==# 19 | solve_hjb!(twoap, fde, fde.sol) 20 | 21 | #== Solve the KF equation ==# 22 | solve_fp!(fde, fde.sol, maxit = 4000, tolFK = 1e-9) 23 | 24 | include("main_fig.jl"); 25 | 26 | #== Policies ==# 27 | # fig_pol(fde) 28 | #== Distributions ==# 29 | fig_dens(fde) 30 | -------------------------------------------------------------------------------- /lecture13/felipe_alves_codes/twoassets.jl: -------------------------------------------------------------------------------- 1 | import Distances: chebyshev 2 | 3 | ############################################################################## 4 | ## 5 | ## Types and constructors 6 | ## 7 | ############################################################################## 8 | 9 | 10 | ################################## 11 | ## 12 | ## Household PROBLEM 13 | ## 14 | ################################## 15 | """ 16 | Consumption-saving problem with two assets and non-convex adjustment costs 17 | 18 | ## Fields 19 | 20 | #### Parameters 21 | - `γ::Float64` : parameter on CRRA utility 22 | - `ρ::Float64` : discount factor 23 | - `ξ::Float64` : automatic deposit 24 | - `σ::Float64` : frisch elasticity of labor supply 25 | - `ψ::Float64` : disutility of labor 26 | 27 | #### Prices 28 | - `rᴬ::Float64` : interest rate on illiquid asset 29 | - `rᴮ::Float64` : interest rate for savings in liquid asset 30 | - `wedge::Float64` : interest rate for borrowing in liquid asset 31 | - `w::Float64` : wage rate 32 | 33 | #### Tax 34 | - `τ::Float64` : tax on income 35 | - `T::Float64` : Lump-sum transfer 36 | 37 | #### Cost Function 38 | - `χ₀::Float64` : 39 | - `χ₁::Float64` : 40 | """ 41 | type TwoAssetsProb 42 | γ::Float64 # CRRA utility with parameter γ 43 | ρ::Float64 # discount rate 44 | ξ::Float64 # automatic deposit on illiquid asset 45 | σ::Float64 46 | ψ::Float64 47 | 48 | rᴬ ::Float64 # ret on illiquid asset 49 | rᴮ::Float64 # ret on liq asset 50 | wedge::Float64 51 | w ::Float64 # wage rate 52 | 53 | τ::Float64 54 | T::Float64 55 | 56 | χ₀::Float64 # parameters on adjustment cost 57 | χ₁::Float64 58 | end 59 | 60 | function Base.show(io::IO, twoap::TwoAssetsProb) 61 | @printf io " HOUSEHOLD PROBLEM\n" 62 | @printf io "\n" 63 | @printf io " Parameters \n" 64 | @printf io " ------------ \n" 65 | @printf io " γ: %.3f\n" twoap.γ 66 | @printf io " ρ: %.3f\n" twoap.ρ 67 | @printf io " ξ: %.3f\n" twoap.ξ 68 | @printf io "\n" 69 | @printf io " Prices \n" 70 | @printf io " -------- \n" 71 | @printf io " rᴬ : %.3f \n" twoap.rᴬ 72 | @printf io " rᴮ : %.3f \n" twoap.rᴮ 73 | @printf io " wage : %.3f \n" twoap.w 74 | @printf io " τ : %.3f \n" twoap.τ 75 | @printf io " T : %.3f \n" twoap.T 76 | @printf io "\n" 77 | @printf io " Cost Function \n" 78 | @printf io " --------------- \n" 79 | 80 | m =""" 81 | χ₀ + χ₁ * (d/a)² * a 82 | """ 83 | @printf io " %s\n" m 84 | end 85 | 86 | function TwoAssetsProb2(pc::Prices; γ::Float64 = 2.0, ρ::Float64 = 0.06, ξ::Float64 = 0.1, 87 | σ = 0.5, ψ = 27.0, 88 | τ::Float64 = 0.0, T::Float64 = 0., 89 | χ₀::Float64 = 0.08, χ₁::Float64 = 3.0) 90 | 91 | # REVIEW: define some global functions that will now change on the go 92 | #== Substitute the functions to be used afterward ==# 93 | # global χ(d,a) = χ(d,a, [χ₀,χ₁]) 94 | # global dχinv(pVb::Float64, pVa::Float64, a::Float64) = dχinv(pVb, pVa, a, χ₁) 95 | # global 96 | 97 | TwoAssetsProb(γ, ρ, ξ, σ, ψ, pc.rᴬ, pc.rᴮ, pc.wedge, pc.w, τ, T, χ₀, χ₁) 98 | end 99 | 100 | _unpackparams(twoap::TwoAssetsProb) = 101 | twoap.γ, twoap.ρ, twoap.ξ, twoap.σ, twoap.ψ 102 | _unpackprices(twoap::TwoAssetsProb) = 103 | twoap.rᴬ, twoap.rᴮ, twoap.rᴮ+twoap.wedge, twoap.w 104 | _unpacktax(twoap::TwoAssetsProb) = 105 | twoap.τ, twoap.T 106 | 107 | function utilfn(twoap::TwoAssetsProb, c::Float64, ℓutil::Float64) 108 | return (c-ℓutil)^(1.- twoap.γ)/(1.-twoap.γ) 109 | end 110 | 111 | function χ(twoap::TwoAssetsProb, d::Float64, a::Float64, abar::Float64 = 2.0) 112 | χ₀, χ₁ = twoap.χ₀, twoap.χ₁ 113 | return χ₀*(abs(d)>0) + 0.5*χ₁*d^2.0/max(a,abar) 114 | end 115 | # χ(d::Float64,a::Float64, χval::Array{Float64,1}) = χval[1]*(abs(d)>0) + 0.5*χval[2]*d^2.0/max(a,1e-5) 116 | 117 | function dχinv(twoap::TwoAssetsProb, ∂ratio::Float64, a::Float64, abar::Float64 = 2.0) 118 | χ₁ = twoap.χ₁ 119 | return 1.0/χ₁ * ( ∂ratio-1.0 ) * max(a,abar) 120 | end 121 | 122 | function change_prices!(twoap::TwoAssetsProb, pc::Prices) 123 | 124 | twoap.rᴬ, twoap.rᴮ, twoap.w = pc.rᴬ, pc.rᴮ, pc.w 125 | return Void 126 | end 127 | 128 | function change_transfer!(twoap::TwoAssetsProb, τ::Float64, T::Float64) 129 | 130 | twoap.τ, twoap.T = τ, T 131 | return Void 132 | end 133 | 134 | # OLD function 135 | # function foc_dep(∂Vb::Float64, ∂Va::Float64, a::Float64, χ₁::Float64) 136 | # 137 | # d = ( ∂Va/∂Vb-1 ) * a/χ₁ # optimal d in case of adjustment 138 | # 139 | # # NOTE:340 compute the change in utility and use that to decide whether to act or 140 | # # not... 141 | # 142 | # ∂V = ∂Va*d - ∂Vb*( d + χ(d,a) ) # change in utility 143 | # d = d * (∂V > 0) 144 | # end 145 | 146 | ############################################################################## 147 | ## 148 | ## Helping functions 149 | ## 150 | ############################################################################## 151 | 152 | function powerspacegrid(init::Real, eend::Real, n::Int64, k::Real = 1) 153 | 154 | if n<=2 155 | println("n has to be larger than 2") 156 | return 157 | end 158 | 159 | x = collect(linspace(0,1,n)) 160 | 161 | z = x.^(1.0/float(k)) 162 | 163 | return init +(eend - init)*z 164 | end 165 | 166 | 167 | 168 | 169 | ################################## 170 | ## 171 | ## FDSpec 172 | ## 173 | ################################## 174 | 175 | abstract FDSol 176 | abstract FDSpec 177 | 178 | type SolutionExp <: FDSol 179 | 180 | ## Solution 181 | V::Vector{Vector{Float64}} # Value function 182 | g::Vector{Vector{Float64}} # Density over state space 183 | c::Array{Float64,3} # Optimal consumption 184 | sc::Array{Float64,3} # Savings without deposit 185 | d::Array{Float64,3} # Optimal deposit flow 186 | 187 | ## Matrices to be filled 188 | A ::Vector{Base.SparseMatrix.SparseMatrixCSC{Float64, Int}} # used on HJB to compute vⁿ⁺¹ 189 | Au::Vector{Base.SparseMatrix.SparseMatrixCSC{Float64, Int}} # used for KFE to compute transition dynamics 190 | 191 | end 192 | 193 | unpackcopy_fdsol(fdsol::SolutionExp) = deepcopy(fdsol.V), deepcopy(fdsol.g), deepcopy(fdsol.c), deepcopy(fdsol.sc), deepcopy(fdsol.d), deepcopy(fdsol.A), deepcopy(fdsol.Au) 194 | 195 | """ 196 | Specification for the Implicit-Explicit Finite Difference method 197 | """ 198 | type FDExp <: FDSpec 199 | 200 | ## GRID INFO 201 | b ::Vector{Float64} 202 | Δbgrid ::Vector{Float64} 203 | ΔTbgrid::Vector{Float64} 204 | rbdrift::Vector{Float64} 205 | netbinc::Matrix{Float64} 206 | 207 | a ::Vector{Float64} 208 | Δagrid ::Vector{Float64} 209 | ΔTagrid::Vector{Float64} 210 | radrift::Vector{Float64} 211 | netainc::Matrix{Float64} 212 | 213 | ΔTab::Vector{Float64} 214 | 215 | ## LaborSupply decisions 216 | ℓsupply::Vector{Float64} 217 | ℓutilgrid::Vector{Float64} 218 | 219 | ## Δ in finite difference scheme 220 | invΔ::Float64 221 | invΔᴷ::Float64 222 | 223 | ## Stochastic Information 224 | z::Vector{Float64} 225 | λ::Matrix{Float64} 226 | λdiag::Vector{Float64} 227 | λoff::Matrix{Float64} 228 | 229 | ## Solution 230 | sol::SolutionExp 231 | 232 | ## Storage 233 | b̃ ::Vector{Vector{Float64}} # RHS of hjb 234 | B ::Vector{Base.SparseMatrix.SparseMatrixCSC{Float64, Int}} # storage matrix 235 | end 236 | 237 | function Base.show(io::IO, fd::FDExp) 238 | fd.Δbgrid[2]-fd.Δbgrid[1] == 0 ? (binfo = "uniform ") : (binfo = "non-uniform ") 239 | fd.Δagrid[2]-fd.Δagrid[1] == 0 ? (ainfo = "uniform ") : (ainfo = "non-uniform ") 240 | @printf io "\n" 241 | @printf io " Explicit-Implicit Finite Difference Method\n" 242 | @printf io "\n" 243 | @printf io " Grids \n" 244 | @printf io " ------- \n" 245 | @printf io " %12sb: %3d points in [% .0f, %.0f]\n" binfo length(fd.b) fd.b[1] fd.b[end] 246 | @printf io " %12sa: %3d poitns in [% .0f, %.0f]\n" ainfo length(fd.a) fd.a[1] fd.a[end] 247 | end 248 | 249 | function FDExp(twoap::TwoAssetsProb; 250 | z::Vector{Float64} = [.5,1.5], λ::Matrix{Float64} = [-1/3 1/3; 1/3 -1/3], 251 | fixedΔb = true, bn::Int =100, bmin::Float64 =-1.0, bmax::Float64 = 40.0, bparam::Vector{Float64} = [0.35, 0.7], 252 | fixedΔa = true, an::Int = 50, amin::Float64 = 1e-8, amax::Float64 = 70.0, aparam::Float64 = 0.7, 253 | invΔ::Float64 = .025, invΔᴷ::Float64 = 0.025) 254 | # z = [.8,1.3]; 255 | # λ = [-1/3 1/3;1/3 -1/3]; 256 | # 257 | # fixedΔb = true; 258 | # bn =100; 259 | # bmin =-2.0; 260 | # bmax = 40.0; 261 | # bparam = [0.35;0.7]; 262 | # 263 | # fixedΔa = true; 264 | # an = 50; 265 | # amin = 0.0; 266 | # amax = 70.0; 267 | # aparam = 0.7; 268 | # 269 | # invΔ = .025; 270 | # invΔᴷ = 0.075 271 | 272 | #== Household parameters and prices ==# 273 | γ, ρ, ξ, σ, ψ = _unpackparams(twoap) 274 | rᴬ, rᴮ, rᴮ⁻, w = _unpackprices(twoap) 275 | τ, T = _unpacktax(twoap) 276 | 277 | #== Construct the grids ==# 278 | if fixedΔb 279 | b = powerspacegrid(bmin,bmax,bn) 280 | else 281 | ngpbPOS = Int(0.9*bn) 282 | ngpbNEG = bn - ngpbPOS 283 | 284 | bpos = powerspacegrid(1e-8, bmax, ngpbPOS, bparam[2]) 285 | bneg = zeros(ngpbNEG) 286 | n = Int(ngpbNEG/2+1) 287 | bneg[1:n] = powerspacegrid(bmin, (bpos[1]+bmin)/2, n , bparam[1]) 288 | b = [bneg;bpos] 289 | for i in n+1:ngpbNEG 290 | b[i] = bpos[1] - (b[ngpbNEG+2-i]-b[1]) 291 | end 292 | end 293 | 294 | fixedΔa ? (a = powerspacegrid(amin,amax,an)) : (a = powerspacegrid(amin,amax,an,aparam[1])) 295 | 296 | zn = length(z); abn = an*bn 297 | 298 | #== Grid counting functions ==# 299 | global afromab(abi) = div(abi-1,bn)+1 300 | global bfromab(abi) = rem(abi-1,bn)+1 301 | global abfromab(ai,bi) = (ai-1)*bn + bi 302 | global abzfromabz(ai,bi,zi) = (zi-1)*an*bn + ( ai-1 )*bn +bi 303 | 304 | #== Grid info ==# 305 | Δbgrid = b[2:end]-b[1:end-1] 306 | Δagrid = a[2:end]-a[1:end-1] 307 | 308 | ΔTbgrid = zeros(bn); ΔTagrid = zeros(an) 309 | ΔTbgrid[1] = 0.5*Δbgrid[1] 310 | ΔTbgrid[2:end-1] = 0.5*( Δbgrid[2:end] + Δbgrid[1:end-1] ) 311 | ΔTbgrid[end] = 0.5*Δbgrid[end] 312 | 313 | ΔTagrid[1] = 0.5*Δagrid[1] 314 | ΔTagrid[2:end-1] = 0.5*( Δagrid[2:end] + Δagrid[1:end-1] ) 315 | ΔTagrid[end] = 0.5*Δagrid[end] 316 | 317 | #== Construct a vector nb*na with the terms Δb̃[bi] * Δã[ai] =# 318 | ΔTab = zeros(abn) 319 | for abi = 1:abn 320 | 321 | ΔTab[abi] = ΔTagrid[afromab(abi)] * ΔTbgrid[bfromab(abi)] 322 | end 323 | 324 | #== Labor grid info ==# 325 | ℓsupply = zeros(zn) 326 | ℓsupply[:] = (1/ψ * (1-τ) * w)^σ 327 | ℓutilgrid = ψ * (z .* ℓsupply.^( 1 + 1.0/σ ) ) / ( 1+1./σ ) 328 | 329 | #== return drifts ==# 330 | rbdrift = (b.>= 0.0)*rᴮ + (b.<0.0)*rᴮ⁻ 331 | rbdrift = b .* rbdrift 332 | 333 | # NOTE: trick on the upper part of grid 334 | #== impose tax on the very top part of grid ==# 335 | τa = 15 336 | τc = rᴬ * ( a[end] * 0.999 )^(1 - τa) 337 | radrift = rᴬ * a - τc * a .^ τa 338 | 339 | #== net?drifts ==# 340 | netbinc = zeros(bn,zn) 341 | netainc = zeros(an,zn) 342 | 343 | for zi in 1:zn 344 | netℓinc = (1-τ)* w * z[zi] * ℓsupply[zi] + T 345 | netbinc[:,zi] = (1-ξ) * netℓinc + rbdrift 346 | netainc[:,zi] = radrift + ξ * netℓinc 347 | end 348 | 349 | #== Idiosyncratic shock ==# 350 | λdiag = diag(λ) 351 | λoff = λ - diagm(λdiag) 352 | 353 | ### STORAGE ### 354 | A = SparseMatrixCSC{Float64, Int}[spdiagm( 355 | (ones(abn), ones(abn-1), ones(abn-1), ones( abn -bn ) , ones( abn -bn ) ), 356 | (0, 1, -1, bn, -bn) ) for zi =1:zn] 357 | 358 | # B = SparseMatrixCSC{Float64, Int}[spdiagm( 359 | # (ones(abn), ones(abn-1), ones(abn-1), ones( abn -bn ) , ones( abn -bn ) ), 360 | # (0, 1, -1, bn, -bn) ) for zi =1:zn] 361 | 362 | B = Array(SparseMatrixCSC{Float64, Int},zn) 363 | Au = Array(SparseMatrixCSC{Float64, Int},zn) 364 | for zi=1:zn 365 | fill!(nonzeros(A[zi]), zero(Float64)) 366 | B[zi] = deepcopy(A[zi]) 367 | Au[zi] = deepcopy(A[zi]) 368 | end 369 | 370 | #== Initial Distribution ==# 371 | g = zeros(abn,zn) 372 | bpos_ind = findfirst(b.>0) 373 | 374 | #REVIEW:70 change ydist assumption 375 | ydist = ones(zn)./zn 376 | for zi=1:zn 377 | for abi =1:abn 378 | ai = afromab(abi) 379 | bi = bfromab(abi) 380 | ai==1 && bi==bpos_ind && ( g[abi,zi] = ydist[zi]/(ΔTagrid[ai]*ΔTbgrid[bi]) ) 381 | end 382 | end 383 | 384 | g = Vector{Float64}[g[:,zi] for zi=1:zn] 385 | #---------------------------------------------------------------- 386 | 387 | ### SOLUTION ### 388 | V = Vector{Float64}[Array(Float64, abn) for zi=1:zn] 389 | ij = zero(Int) 390 | for zi in 1:zn 391 | ij = Int(0) 392 | for ai in 1:an, bi in 1:bn 393 | ij += 1 394 | V[zi][ij] = ( (1- ξ)*w*z[zi] + rᴬ*a[ai] + rᴮ⁻*b[bi] ).^( 1- γ )/( 1- γ )/ρ 395 | end 396 | end 397 | 398 | 399 | # == storage arrays == # 400 | b̃ = Vector{Float64}[Array(Float64, abn) for zi=1:zn] # RHS of the Bellman equation 401 | 402 | #== policies ==# 403 | c = Array(Float64, bn, an, zn) # consumption policy 404 | sc = copy(c); d = copy(c) 405 | 406 | #== Create solution type ==# 407 | sol = SolutionExp(V, g, c, sc, d, A, Au) 408 | 409 | FDExp( 410 | b, Δbgrid, ΔTbgrid, rbdrift, netbinc, 411 | a, Δagrid, ΔTagrid, radrift, netainc, ΔTab, 412 | ℓsupply, ℓutilgrid, 413 | invΔ, invΔᴷ, 414 | z, λ, λdiag, λoff, 415 | sol, 416 | b̃, B) 417 | end 418 | 419 | function initialize_solexp(twoap::TwoAssetsProb, fd::FDSpec) 420 | 421 | #= precompute the lengths =# 422 | b = fd.b 423 | a = fd.a 424 | z = fd.z 425 | bn = length(b); an = length(a); zn = length(z); abn = bn*an 426 | 427 | #= extract some other from fd =# 428 | ΔTagrid = fd.ΔTagrid 429 | ΔTbgrid = fd.ΔTbgrid 430 | netbinc = fd.netbinc 431 | 432 | 433 | 434 | #== Create the A matrix ==# 435 | A = SparseMatrixCSC{Float64, Int}[spdiagm( 436 | (ones(abn), ones(abn-1), ones(abn-1), ones( abn -bn ) , ones( abn -bn ) ), 437 | (0, 1, -1, bn, -bn) ) for zi =1:zn] 438 | 439 | Au = Array(SparseMatrixCSC{Float64, Int},zn) 440 | for zi=1:zn 441 | fill!(nonzeros(A[zi]), zero(Float64)) 442 | Au[zi] = deepcopy(A[zi]) 443 | end 444 | 445 | #== Initial Distribution ==# 446 | g = zeros(abn,zn) 447 | bpos_ind = findfirst(b.>0) 448 | 449 | #REVIEW:70 change ydist assumption 450 | ydist = ones(zn)./zn 451 | for zi=1:zn 452 | for abi =1:abn 453 | ai = afromab(abi) 454 | bi = bfromab(abi) 455 | ai==1 && bi==bpos_ind && ( g[abi,zi] = ydist[zi]/(ΔTagrid[ai]*ΔTbgrid[bi]) ) 456 | end 457 | end 458 | 459 | g = Vector{Float64}[g[:,zi] for zi=1:zn] 460 | #---------------------------------------------------------------- 461 | 462 | ### SOLUTION ### 463 | V = Vector{Float64}[Array(Float64, abn) for zi=1:zn] 464 | ij = zero(Int) 465 | for zi in 1:zn 466 | ij = Int(0) 467 | for ai in 1:an, bi in 1:bn 468 | ij += 1 469 | V[zi][ij] = ( netbinc[bi,zi] ).^( 1- γ )/( 1- γ )/ρ 470 | end 471 | end 472 | 473 | #== policies ==# 474 | c = Array(Float64, bn, an, zn) # consumption policy 475 | sc = copy(c); d = copy(c) 476 | 477 | #== Create solution type ==# 478 | SolutionExp(V, g, c, sc, d, A, Au) 479 | end 480 | 481 | """ 482 | Update the grid for a different specification of HOUSEHOLD PROBLEM 483 | """ 484 | function change_grid!(fd::FDSpec, twoap::TwoAssetsProb) 485 | 486 | #== Parameters ==# 487 | _, _, ξ, σ, ψ = _unpackparams(twoap) 488 | rᴬ, rᴮ, rᴮ⁻, w = _unpackprices(twoap) 489 | τ, T = _unpacktax(twoap) 490 | 491 | b, a, z = fd.b, fd.a, fd.z 492 | bn = length(b) 493 | an = length(a) 494 | abn = bn * an 495 | zn = length(z) 496 | 497 | #== Labor grid info ==# 498 | ℓsupply = zeros(zn) 499 | ℓsupply[:] = (1/ψ * (1-τ) * w)^σ 500 | fd.ℓsupply = ℓsupply 501 | fd.ℓutilgrid = ψ * (z .* ℓsupply.^( 1 + 1.0/σ ) ) / ( 1+1./σ ) 502 | 503 | #== Update the drifts ==# 504 | rbdrift = ( b.>= 0.0)*rᴮ + (b.<0.0)*rᴮ⁻ 505 | fd.rbdrift = b .* rbdrift 506 | 507 | τa = 15 508 | τc = rᴬ * ( a[end] * 0.999 )^(1 - τa) 509 | fd.radrift = rᴬ * a - τc * a .^ τa 510 | 511 | for zi in 1:zn 512 | netℓinc = (1-τ)* w *z[zi] * ℓsupply[zi] + T 513 | fd.netbinc[:,zi] = (1-ξ) * netℓinc + fd.rbdrift 514 | fd.netainc[:,zi] = fd.radrift + ξ * netℓinc 515 | end 516 | 517 | end 518 | 519 | 520 | 521 | ############################################################################## 522 | ## 523 | ## Implicit scheme 524 | ## 525 | ############################################################################## 526 | 527 | type SolutionImp <: FDSol 528 | 529 | ## Household Solution 530 | V::Vector{Float64} # Value function 531 | g::Vector{Float64} # Density over state space 532 | c::Array{Float64,3} # Optimal consumption 533 | sc::Array{Float64,3} # Savings without deposit 534 | d::Array{Float64,3} # Optimal deposit flow 535 | 536 | ## matrices to be filled 537 | A ::Base.SparseMatrix.SparseMatrixCSC{Float64, Int} # intensity matrix A on vⁿ⁺¹ 538 | Au ::Base.SparseMatrix.SparseMatrixCSC{Float64, Int} # intensity matrix A for kfe 539 | 540 | end 541 | """ 542 | Specification for the Implicit Finite Difference method 543 | """ 544 | type FDImp <: FDSpec 545 | 546 | ## GRID INFO 547 | b ::Vector{Float64} 548 | Δbgrid ::Vector{Float64} 549 | ΔTbgrid::Vector{Float64} 550 | rbdrift::Vector{Float64} 551 | netbinc::Matrix{Float64} 552 | 553 | a ::Vector{Float64} 554 | Δagrid ::Vector{Float64} 555 | ΔTagrid::Vector{Float64} 556 | radrift::Vector{Float64} 557 | netainc::Matrix{Float64} 558 | 559 | ## LaborSupply decisions 560 | ℓsupply ::Vector{Float64} 561 | ℓutilgrid::Vector{Float64} 562 | 563 | ## Δ in finite difference scheme 564 | invΔ ::Float64 565 | invΔᴷ::Float64 566 | 567 | ## Stochastic Information 568 | z::Vector{Float64} 569 | λ::Matrix{Float64} 570 | 571 | ## OUTPUT 572 | sol::SolutionImp 573 | 574 | ## Storage 575 | b̃ ::Vector{Float64} # RHS of hjb 576 | B ::Base.SparseMatrix.SparseMatrixCSC{Float64, Int} # B storage matrix 577 | Λ ::Base.SparseMatrix.SparseMatrixCSC{Float64, Int} # stochastic process terms (useful bc can fill before) 578 | end 579 | 580 | function Base.show(io::IO, fd::FDImp) 581 | fd.Δbgrid[2]-fd.Δbgrid[1] == 0 ? (binfo = "uniform ") : (binfo = "non-uniform ") 582 | fd.Δagrid[2]-fd.Δagrid[1] == 0 ? (ainfo = "uniform ") : (ainfo = "non-uniform ") 583 | @printf io "\n" 584 | @printf io " Implicit Finite Difference Method\n" 585 | @printf io "\n" 586 | @printf io " Grids \n" 587 | @printf io " ------- \n" 588 | @printf io " %12sb: %3d points in [% .0f, %.0f]\n" binfo length(fd.b) fd.b[1] fd.b[end] 589 | @printf io " %12sa: %3d poitns in [% .0f, %.0f]\n" ainfo length(fd.a) fd.a[1] fd.a[end] 590 | # @printf io " z: %.2f\n" twoap.ξ 591 | end 592 | 593 | function FDImp(twoap::TwoAssetsProb; 594 | z::Vector{Float64} = [.8,1.3], λ::Matrix{Float64} = [-1/3 1/3; 1/3 -1/3], 595 | fixedΔb = true, bn::Int =100, bmin::Float64 =-2.0, bmax::Float64 = 40.0, bparam::Vector{Float64} = [0.35, 0.7], 596 | fixedΔa = true, an::Int = 50, amin::Float64 = 0.0, amax::Float64 = 70.0, aparam::Float64 = 0.7, 597 | invΔ::Float64 = .01, invΔᴷ::Float64 = .05) 598 | 599 | #== Parameters ==# 600 | γ, ρ, ξ, σ, ψ = _unpackparams(twoap) 601 | rᴬ, rᴮ, rᴮ⁻, w = _unpackprices(twoap) 602 | τ, T = _unpacktax(twoap) 603 | 604 | 605 | #== construct the grids ==# 606 | if fixedΔb 607 | b = powerspacegrid(bmin,bmax,bn) 608 | else 609 | ngpbPOS = Int(0.8*bn) 610 | ngpbNEG = bn - ngpbPOS 611 | 612 | bpos = powerspacegrid(1e-8, bmax, ngpbPOS, bparam[2]) 613 | bneg = zeros(ngpbNEG) 614 | n = Int(ngpbNEG/2+1) 615 | bneg[1:n] = powerspacegrid(bmin, (bpos[1]+bmin)/2, n , bparam[1]) 616 | b = [bneg;bpos] 617 | for i in n+1:ngpbNEG 618 | b[i] = bpos[1] - (b[ngpbNEG+2-i]-b[1]) 619 | end 620 | end 621 | fixedΔa ? (a = powerspacegrid(amin,amax,an)) : (a = powerspacegrid(amin,amax,an,aparam[1])) 622 | 623 | zn = length(z) 624 | abn = an*bn 625 | 626 | #== Grid counting functions ==# 627 | global afromab(abi) = div(abi-1,bn)+1 628 | global bfromab(abi) = rem(abi-1,bn)+1 629 | global abfromab(ai,bi) = (ai-1)*bn + bi 630 | global abzfromabz(ai,bi,zi) = (zi-1)*an*bn + ( ai-1 )*bn +bi 631 | 632 | #== Grid info ==# 633 | Δbgrid = b[2:end]-b[1:end-1] 634 | Δagrid = a[2:end]-a[1:end-1] 635 | 636 | ΔTbgrid = zeros(bn); ΔTagrid = zeros(an) 637 | ΔTbgrid[1] = 0.5*Δbgrid[1] 638 | ΔTbgrid[2:end-1] = 0.5*( Δbgrid[2:end] + Δbgrid[1:end-1] ) 639 | ΔTbgrid[end] = 0.5*Δbgrid[end] 640 | 641 | ΔTagrid[1] = 0.5*Δagrid[1] 642 | ΔTagrid[2:end-1] = 0.5*( Δagrid[2:end] + Δagrid[1:end-1] ) 643 | ΔTagrid[end] = 0.5*Δagrid[end] 644 | 645 | #== Labor grid info ==# 646 | ℓsupply = zeros(zn) 647 | ℓsupply[:] = (1/ψ * (1-τ) * w)^σ 648 | ℓutilgrid = ψ * (z .* ℓsupply.^( 1 + 1.0/σ ) ) / ( 1+1./σ ) 649 | 650 | rbdrift = (b.>= 0.0)*rᴮ + (b.<0.0)*rᴮ⁻ 651 | rbdrift = b .* rbdrift 652 | 653 | #== impose tax on the very top part of grid ==# 654 | τa = 15 655 | τc = rᴬ * ( a[end] * 0.999 )^(1 - τa) 656 | radrift = rᴬ * a - τc * a .^ τa 657 | 658 | #== net_?drifts ==# 659 | netbinc = zeros(bn,zn) 660 | netainc = zeros(an,zn) 661 | 662 | for zi in 1:zn 663 | netℓinc = (1-τ)* w * z[zi] * ℓsupply[zi] + T 664 | netbinc[:,zi] = (1-ξ) * netℓinc + rbdrift 665 | netainc[:,zi] = radrift + ξ * netℓinc 666 | end 667 | 668 | ## STORAGE 669 | 670 | # NOTE:250 create a sparse matrix of the RIGHT format that later will be filled with zeros 671 | # here we have to be careful bc prob has 3 state variables (bi, aj, zk) 672 | onesλ = Array{Float64,1}[ones( (zn-i)*abn ) for i=1:zn-1] 673 | colλ = Int64[(i)*abn for i=1:zn-1] 674 | 675 | A = spdiagm( 676 | (ones(abn*zn), ones(abn*zn-1), ones(abn*zn-1), ones( abn*zn -bn ) , ones( abn*zn -bn ), 677 | onesλ..., onesλ...), 678 | (0, 1, -1, bn, -bn, colλ..., -colλ...) 679 | ) 680 | 681 | fill!(nonzeros(A), zero(Float64)) 682 | Au = deepcopy(A) 683 | B = deepcopy(A) 684 | Λ = deepcopy(A) 685 | 686 | # == fill up matrix Λ == # 687 | ij = zero(Int) 688 | λt = λ' 689 | @inbounds for zi in 1:zn, abi in 1:abn 690 | ij += 1 691 | Λ[ abi:abn:(zn-1)*abn+abi , ij] = λt[:,zi] 692 | end 693 | 694 | # == storage arrays == # 695 | b̃ = Array(Float64, abn*zn) # RHS of the Bellman equation 696 | c = Array(Float64, bn, an, zn) # consumption policy 697 | sc = copy(c); d = copy(c) 698 | 699 | #== Initial value function guess ==# 700 | V = Array(Float64, abn*zn) 701 | ij = zero(Int) 702 | for zi in 1:zn, ai in 1:an, bi in 1:bn 703 | ij += 1 704 | V[ij] = ( (1- ξ)*w*z[zi] + rᴬ*a[ai] + rᴮ⁻*b[bi] ).^( 1- γ )/( 1- γ )/ρ 705 | end 706 | 707 | #== Initial Distribution ==# 708 | g = zeros(abn,zn) 709 | bpos_ind = findfirst(b.>0) 710 | 711 | #REVIEW:60 change ydist assumption 712 | ydist = ones(zn)./zn 713 | for zi=1:zn 714 | for abi =1:abn 715 | ai = afromab(abi) 716 | bi = bfromab(abi) 717 | ai==1 && bi==bpos_ind && ( g[abi,zi] = ydist[zi]/(ΔTagrid[ai]*ΔTbgrid[bi]) ) 718 | end 719 | end 720 | g = g[:] 721 | 722 | sol = SolutionImp(V, g, c, sc, d, A, Au) 723 | 724 | FDImp( 725 | b, Δbgrid, ΔTbgrid, rbdrift, netbinc, 726 | a, Δagrid, ΔTagrid, radrift, netainc, 727 | ℓsupply, ℓutilgrid, 728 | invΔ, invΔᴷ, 729 | z, λ, 730 | sol, 731 | b̃, B, Λ) 732 | end 733 | 734 | 735 | ################################## 736 | ## 737 | ## TwoAssetsFD 738 | ## 739 | ################################## 740 | # 741 | # type TwoAssetsFD 742 | # ## Parameters of the problem 743 | # twoap::TwoAssetsProb 744 | # 745 | # ## Discretization 746 | # b::Vector{Float64} 747 | # Δb::Float64 748 | # rbdrift::Vector{Float64} 749 | # a::Vector{Float64} 750 | # Δa::Float64 751 | # radrift::Vector{Float64} 752 | # invΔ::Float64 753 | # 754 | # ## Stochastic Information 755 | # λ::Matrix{Float64} 756 | # z::Vector{Float64} 757 | # 758 | # ## Solution 759 | # V::Vector{Float64} # Value function 760 | # gg::Vector{Float64} # distribution 761 | # 762 | # ## Storage 763 | # newV::Vector{Float64} # New Value function 764 | # u::Vector{Float64} # utility term 765 | # g⁰::Vector{Float64} # for KolmogorovForward 766 | # A::Base.SparseMatrix.SparseMatrixCSC{Float64, Int} # rhs matrix on vⁿ⁺¹ 767 | # B::Base.SparseMatrix.SparseMatrixCSC{Float64, Int} # B = (indΔ + ρ)I - A 768 | # Λ::Base.SparseMatrix.SparseMatrixCSC{Float64, Int} # stochastic process terms (useful bc can fill before) 769 | # end 770 | # 771 | # 772 | # function TwoAssetsFD(twoap::TwoAssetsProb, 773 | # z::Vector{Float64} = [.8,1.3], λ::Matrix{Float64} = [-1/3 1/3; 1/3 -1/3], 774 | # bn::Int =100, bmin::Float64 =-2.0, bmax::Float64 = 40.0, 775 | # an::Int = 50, amin::Float64 = 0.0, amax::Float64 = 70.0, 776 | # invΔ::Float64 = .01) 777 | # 778 | # #== Parameters ==# 779 | # γ = twoap.γ; ρ = twoap.ρ; ξ = twoap.ξ; rᴬ = twoap.rᴬ; 780 | # rᴮ = twoap.rᴮ; ; w = twoap.w 781 | # rᴮ⁻ = twoap.rᴮ + twoap.wedge 782 | # b = collect(linspace(bmin,bmax,bn)) 783 | # a = collect(linspace(amin,amax,an)) 784 | # Δb = b[2]-b[1]; Δa = a[2]-a[1]; 785 | # 786 | # rbdrift = ( b.>= 0.0)*rᴮ + (b.<0.0)*rᴮ⁻ 787 | # rbdrift = b .* rbdrift 788 | # radrift = rᴬ * a 789 | # 790 | # zn = length(z) 791 | # abn = an*bn 792 | # 793 | # # NOTE:380 create a sparse matrix of the RIGHT format that later will be filled with zeros 794 | # # here we have to be careful bc prob has 3 state variables (bi, aj, zk) 795 | # onesλ = Array{Float64,1}[ones( (zn-i)*abn ) for i=1:zn-1] 796 | # colλ = Int64[(i)*abn for i=1:zn-1] 797 | # 798 | # A = spdiagm( 799 | # (ones(abn*zn), ones(abn*zn-1), ones(abn*zn-1), ones( abn*zn -bn ) , ones( abn*zn -bn ), 800 | # onesλ..., onesλ...), 801 | # (0, 1, -1, bn, -bn, colλ..., -colλ...) 802 | # ) 803 | # 804 | # fill!(nonzeros(A), zero(Float64)) 805 | # B = deepcopy(A) 806 | # Λ = deepcopy(A) 807 | # 808 | # # == fill up matrix Λ == # 809 | # ij = zero(Int) 810 | # λt = λ' 811 | # @inbounds for zi in 1:zn, abi in 1:abn 812 | # ij += 1 813 | # Λ[ abi:abn:(zn-1)*abn+abi , ij] = λt[:,zi] 814 | # end 815 | # 816 | # V = Array(Float64, abn*zn) 817 | # ij = zero(Int) 818 | # for zi in 1:zn, ai in 1:an, bi in 1:bn 819 | # ij += 1 820 | # V[ij] = ( (1- ξ)*w*z[zi] + rᴬ*a[ai] + rᴮ⁻*b[bi] ).^( 1- γ )/( 1- γ )/ρ 821 | # end 822 | # 823 | # # == b such that Ag = b in plank 824 | # g⁰ = fill(zero(Float64), abn*zn) 825 | # i_fix = 1 826 | # g⁰[i_fix] = .1 827 | # 828 | # # == storage arrays == # 829 | # gg = Array(Float64, abn*zn) 830 | # u = Array(Float64, abn*zn) 831 | # newV = deepcopy(V) 832 | # 833 | # TwoAssetsFD(twoap, 834 | # b, Δb, rbdrift, a, Δa, radrift, invΔ, 835 | # λ, z, 836 | # V, gg, 837 | # newV, u, g⁰, A, B, Λ ) 838 | # end 839 | -------------------------------------------------------------------------------- /lecture13/lecture13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture13/lecture13.pdf -------------------------------------------------------------------------------- /lecture14/scikit-learn-flow-chart.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture14/scikit-learn-flow-chart.jpg -------------------------------------------------------------------------------- /lecture14/svm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture14/svm.jpg -------------------------------------------------------------------------------- /lecture2/.pass: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture2/.pass -------------------------------------------------------------------------------- /lecture2/c_examples/ar1_sample_mean.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | double ar1_ts (double * params, int n, unsigned long int seed) 9 | { 10 | double beta = params[0]; 11 | double alpha = params[1]; 12 | double s = params[2]; 13 | /* create a generator chosen by the environment variable GSL_RNG_TYPE */ 14 | const gsl_rng_type * T; 15 | gsl_rng * r; 16 | gsl_rng_env_setup(); 17 | T = gsl_rng_default; 18 | r = gsl_rng_alloc(T); 19 | gsl_rng_set(r, seed); 20 | 21 | int i; 22 | double x = beta / (1 - alpha); // Start at mean of stationary dist 23 | double sum = 0; 24 | for (i = 1; i <= n; i++) { 25 | sum += x; 26 | x = beta + alpha * x + gsl_ran_gaussian(r, s); 27 | } 28 | 29 | gsl_rng_free (r); 30 | return sum / n; 31 | } 32 | 33 | int main(void) 34 | { 35 | clock_t start, end; 36 | double cpu_time_used; 37 | 38 | int N = 10000000; 39 | double beta = 1.0; 40 | double alpha = 0.9; 41 | double s = 1; 42 | double params[3] = {beta, alpha, s}; 43 | 44 | start = clock(); 45 | double sample_mean = ar1_ts(params, N, 1); 46 | end = clock(); 47 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; 48 | 49 | printf("mean = %g\n", sample_mean); 50 | printf("time elapsed = %g\n", cpu_time_used); 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /lecture2/c_examples/function.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double f(double x) 4 | { 5 | return 1.0 / x; 6 | } 7 | 8 | int main(void) 9 | { 10 | double x = 4.0; 11 | double y = f(x); 12 | printf("y = %f\n", y); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /lecture2/c_examples/function_ref.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void f(double x, double *y_pointer) 4 | { 5 | *y_pointer = 1.0 / x; 6 | } 7 | 8 | int main(void) 9 | { 10 | double x = 4.0; 11 | double y; 12 | f(x, &y); 13 | printf("y = %f\n", y); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /lecture2/c_examples/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("hello world\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /lecture2/c_examples/hello_again.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("hello world\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /lecture2/c_examples/make_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Creates a linear array. */ 4 | void linspace(double *ls, double a, double b, int n) 5 | { 6 | double step = (b - a) / (n - 1); 7 | int i; 8 | for (i = 0; i < n; i++) 9 | { 10 | ls[i] = a; 11 | a += step; 12 | } 13 | } 14 | 15 | int main(void) 16 | { 17 | int n = 10; 18 | double grid[n]; 19 | linspace(grid, 1, 2, n); 20 | int i; 21 | for (i = 0; i < n; i++) 22 | { 23 | printf("%g\n", grid[i]); 24 | } 25 | return 0; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /lecture2/c_examples/makefile: -------------------------------------------------------------------------------- 1 | maketest: 2 | gcc -Wall ar1_sample_mean.c -lgsl -lgslcblas -lm -o ar1_sample_mean 3 | 4 | -------------------------------------------------------------------------------- /lecture2/c_examples/sin_func.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int main() { 6 | double x; 7 | int i = 3; 8 | x = 1.0 / (double) i; /* cast i to double */ 9 | double y = sin(x); 10 | printf("i = %d, x = %f, y = %f\n", i, x, y); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /lecture2/git_intro/Makefile: -------------------------------------------------------------------------------- 1 | all: reveal beamer pdf 2 | 3 | reveal: 4 | pandoc --self-contained --variable theme="simple" -t revealjs -s github.md -o github.html 5 | 6 | beamer: 7 | pandoc -t beamer -s github.md -o github_slides.pdf 8 | 9 | pdf: 10 | pandoc -t beamer -s github.md -o github.pdf 11 | -------------------------------------------------------------------------------- /lecture2/git_intro/github.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 'Intro to GitHub' 3 | author: "Spencer Lyon" 4 | date : "2016-02-05" 5 | --- 6 | 7 | # Git `remote`s 8 | 9 | ## What's a `remote`? 10 | 11 | - A git repo can be on your hard drive: called local 12 | - A `remote` is a copy of the repository on someone else's hard drive or server 13 | - You `git push` commits from local to remote 14 | - `git pull` commits from remote to local 15 | 16 | ## Collaboration 17 | 18 | - Working with remotes enables many [workflows](https://git-scm.com/book/en/v2/Distributed-Git-Distributed-Workflows) 19 | - One common workflow (image taken from [here](https://git-scm.com/book/en/v2/Distributed-Git-Distributed-Workflows)) 20 | 21 | ![Centralized workflow](images/git_centralized_workflow.png) 22 | 23 | # [GitHub](https://github.com) 24 | 25 | ## Facts 26 | 27 | - GitHub is a common place to have a `remote` 28 | - [Over 2.2 million](http://githut.info) active repositories 29 | - Unlimited free public repositories 30 | - 5 free private repositories for academics 31 | 32 | ## GitHub Collaboration 33 | 34 | - Permission management: only certain users can `push` 35 | - Forking: anyone can create a copy of any (visible) repository under their account 36 | - Pull requests: system for project maintainers to review proposed changes before accepting them 37 | - Automated testing (CI) and coverage 38 | 39 | # Example 40 | 41 | ## quantecon_nyu_2016 42 | 43 | - Add this lecture to course repo 44 | - Steps: 45 | 1. Login to github (online) 46 | 2. Clone repository 47 | ```sh 48 | git clone https://github.com/jstac/quantecon_nyu_2016.git 49 | ``` 50 | 3. Fork repository (online) 51 | 4. Add `remote` that points to fork 52 | ```sh 53 | git remote add fork https://github.com/spencerlyon2/quantecon_nyu_2016.git 54 | ``` 55 | 56 | ## Steps (continued) 57 | 58 | - More steps 59 | 5. `add` + `commit` files 60 | ``` 61 | git add files 62 | git commit -m "Adding github intro slides" 63 | ``` 64 | 6. `push` to fork 65 | ``` 66 | git push fork master 67 | ``` 68 | 7. Open pull request (online) 69 | 70 | # Exercise 71 | 72 | ## Pushing homework 73 | 74 | - We will let you practice hw submission process 75 | - Steps 76 | - Create account on github 77 | - Clone [homework repository](https://github.com/jstac/quantecon_nyu_2016_homework) 78 | - Fork homework repo 79 | - Add your fork as remote 80 | - Create file `firstname_lastname` in folder `hw_github_intro` 81 | - Leave short message in file 82 | - Add and commit the file 83 | - Push to fork 84 | - Submit pull request 85 | 86 | 87 | # Extras 88 | 89 | ## SSH keys 90 | 91 | - Two modes of authenticating to github: https, SSH 92 | - https will require you to enter password on every push 93 | - Adding ssh-key removes this requirement 94 | - Follow these steps: 95 | - `ssh-keygen`: follow prompts 96 | - Copy the output of `cat ~/.ssh/id_rsa.pub` (use shift-c) 97 | - NOTE: path `~/.ssh/id_rsa.pub` might be different depending on your answers to previous step 98 | - Important part is to get the `.pub` version 99 | - Go to github account [settings](https://github.com/settings/profile) 100 | - Click "ssh keys" link on left, then "New ssh key" 101 | - Give it a title (name) and paste clipboard contents in key 102 | - Click big green "add SSH key button" 103 | -------------------------------------------------------------------------------- /lecture2/git_intro/gitnotes.html: -------------------------------------------------------------------------------- 1 |

Version Control with Git

2 |
3 |

4 |
5 |

I suspect that the most of us have participated in some sort of "home-rolled" version of version control. For example, below are three forms of "collaboration" that we might have engaged in

6 |
    7 |
  • Tediously "careful" approach: This is when a person (or people) maintain a type of version control by placing the date edited at the end of the file name and then maintaining a full folder of these types of files.
  • 8 |
  • Stake your claim approach: This appraoch takes the form of calling "dibs" on the use of a specific file for the time being by shouting across the office or sending emails to your collaborators and informing others that the file is "currently under your control" and that others should stay away!
  • 9 |
  • Where's that email approach: Perhaps worst of all is the approach in which collaborators each maintain their own file and email it back and forth with changes as they make them.
  • 10 |
11 |

All of these are dangerous workflows and will result in mistakes and unwanted material showing up in your files (or wanted material being erased). Luckily for us, a variety of version control products have evolved in order to help us eliminate the need for these types of behaviors. Dropbox, Google Drives, SVN, Mercurial, and git are all examples of version control systems.

12 |

Version control allows us to keep track of what changes have been made over time. Careful maintenance of code and data is vital to reproducibility which "is the hallmark of good science." While Dropbox and Google Drives may be useful for sharing certain types of files (I am not an advocate of their complete abandonment), they are not suitable for the fine tuned version control that we need to maintain good science (or software).

13 |

What is git?

14 |

Git is a distributed version control system. Distributed version control means that the entire history of every file is kept on your computer. It was originally written by Linus Torvalds (creator of Linux) to help maintain the Linux project (Fun Fact: Git was originally written because Torvalds found all other alternatives of version control to be too slow to manage a project as large as Linux, so he decided to write his own version which he began on 3 April 2005 and started being used on 7 April 2005. Read more about the history).

15 |

Using git

16 |

Many people associate git with the cloud based repository service Github, but git can be run independently either just on your own computer or on a self-hosted server. In this short tutorial, we are going to create a git repository hosted on our computer. We will then talk about some of the day to day commads that will be used in git.

17 |

WARNING: Until you know what you're doing and exactly how they work, NEVER use the -f or --force flags no matter what the internet tells you.

18 |

We are now going to walk through some of the basic settings you should set and an example of some commands.

19 |

Configuration

20 |

Here we deal with configuration details such as our default editor, user name, email, colors, etc...

21 |
    22 |
  • Set name: git config --global user.name FirstName LastName
  • 23 |
  • Set email: git config --global user.email email@email.com
  • 24 |
  • Sets git colors: git config --global color.ui "auto"
  • 25 |
  • Sets editor to vim: git config --global core.editor "vim"
  • 26 |
27 |

Creating a folder

28 |

We will create a folder called <MyFirstGitRepo> using mkdir <MyFirstGitRepo>

29 |

Initializing a git repository

30 |

Now enter that directory using cd <MyFirstGitRepo>. To initialize this directory as a git repository (which in the background will create a series of directories and files) we will use the command git init.

31 |

We can see the things that were created by typing ls .git, but don't worry too much about what is being kept inside yet.

32 |

Four Stages of Files

33 |

Files in a git repository can be in four different stages: untracked, unstaged, staged, and committed.

34 |
    35 |
  • Untracked: This is a new file that your repository hasn't seen before.
  • 36 |
  • Unstaged: A file that has previously been tracked and saved, but has changed since its last version.
  • 37 |
  • Staged: A file that has been changed and is prepared to be committed. Nothing is set in stone yet though and new changes can be made.
  • 38 |
  • Committed: When a file is committed it becomes a piece of the history of the repository. This moment of time in the file will be able to be referenced or referred to in the future.
  • 39 |
40 |

The picture below illustrates this "life cycle"

41 |
42 |

43 |
44 |

Let's illustrate this through an example and to expose ourselves to the commands that will be helpful: git add, git commit, git diff, and git status.

45 |

First let's check what is new in our repository. Type git status. What does it say? It should say that there is nothing to commit because we haven't done anything yet.

46 |

Now let's create a file called README.md. Open this file and type your name in it (could also use the command echo "FirstName LastName" >> README.md). If you type git status now, what do you see? It should list README.md as an untracked file.

47 |

We can move this file from untracked to staged by using git add README.md. Type this command and then once again type git status. Our README.md file now shows up in green as a change to be committed. This means it is staged.

48 |

We can take our "snapshot" of the file by using the command git commit -m "Type a meaningful msg here". Commit the file and then once again type git status. What do you see now? The repository should be clean again.

49 |

Add something new to the README.md file. Once again, we can check the status of our git repository by typing git status. It will tell us that README.md is unstaged because we have made changes to it.

50 |

Imagine we wanted to double check the things that we changed. We could type git diff README.md which will show us the changes that have been made to that file since its previous commit.

51 |

These commands will be the core of your git workflow so I suggest familiarizing yourself with what they do.

52 |

Commit History

53 |

Remember how we can leave ourselves commit messages. It is useful to leave meaningful commit messages because they are left as a guide for yourself. We can see our history of commits by typing git log. We can do this in several formats: Try git log --pretty=oneline, git log --stat, git log --since=2.weeks, etc... See the Git Pro book for more options.

54 |

If we want to return to a previous commit in our history we can use the information from git log to get back. There is a sequence of numbers and letters, which we will call commit hash, at the top of each entry in your history. If you copy this and type git reset <commit hash> then it will return us to that moment of our history. All of our more recent changes will be there, but they will be as if they had been just staged. Can use git reset <commit hash> --hard to reset to a previous point in time and delete all changes, but be very careful with that command as it can erase your history (in fact, I suggest not using it until you really know enough that you know it is what you want).

55 |

Branching

56 |

A branch is essentially an specific version of your folder. You start with a main branch which is called master. When you create another branch, it is an exact replica of the branch it is being created from (typically master) and includes the full history of the repository. After creating a new branch you can make changes and this new branch will develop its own history (without changing the history of the original branch -- such as master). If you decide you like the changes that you made then you can bring them into the original branch.

57 |

The command git checkout is used for a few different things, but we will mostly use it for switching between branches. When used with the -b flag, it creates a new branch and switches to it.

58 |

Let's create a new branch in our repository. git checkout -b test. What has changed? Let's look at the output of git branch. Now let's make some changes in our branch.

59 |
> echo "New line" >> README.md
60 | > git add README.md
61 | > git commit -m "Branch update"
62 | > git log
63 |

We can see the history has changed and we have a new commit. Let's return to the master branch by typing git checkout master. Now check git log, notice that we no longer have the changes from our test branch.

64 |

Imagine that we decided to bring those changes into our master branch. We could use the git merge command by typing git merge test which will merge the branch test into the current branch.

65 |

Ignore Files

66 |

Sometimes there are files that get created via an intermediate process (such as .aux, .synctex, etc... in latex). We often don't care about keeping track of these files. Another wonderful thing about git is that it allows us to ignore files we don't care about!

67 |

We do this by creating a file called .gitignore at the initial directory of our git repository.

68 |

For example, echo *.garbage >> .gitignore.

69 |

Then create a file called foo.garbage with touch foo.garbage.

70 |

Now type git status. Notice that this file doesn't show up! It is because it recognizes that anything with an ending of .garbage should be ignored (this is because * is treated as a wildcard. Read more about regular expressions or wild cards for more information on how to use them).

71 |

Resources and References

72 |

Below is a sequence of references to things that I have found useful. I suggest reading a few of them and at least skimming the majority of them. In particular, the software carpentry lectures on git are very useful for learning the basics -- The Pro Git book is the biblical reference for git and can likely answer any question you will have for at least a few years to come. I have organized both sections by how relevant/convincing I found the documents.

73 |

Git Technical References

74 |

Software Carpentry: Git Lectures Pro Git Git Tower

75 |

Why Git References

76 |

Difference between git and Dropbox Version control for scientific research Why Physicists Should Use Git Or Why Everyone Should Try Git Git can facilitate greater reproducibility and increased transparency in science

77 | -------------------------------------------------------------------------------- /lecture2/git_intro/gitnotes.md: -------------------------------------------------------------------------------- 1 | # Version Control with Git 2 | 3 |
4 | 5 |
6 | 7 | I suspect that the most of us have participated in some sort of "home-rolled" version of version control. For example, below are three forms of "collaboration" that we might have engaged in 8 | 9 | * Tediously "careful" approach: This is when a person (or people) maintain a type of version control by placing the date edited at the end of the file name and then maintaining a full folder of these types of files. 10 | * Stake your claim approach: This appraoch takes the form of calling "dibs" on the use of a specific file for the time being by shouting across the office or sending emails to your collaborators and informing others that the file is "currently under your control" and that others should stay away! 11 | * Where's that email approach: Perhaps worst of all is the approach in which collaborators each maintain their own file and email it back and forth with changes as they make them. 12 | 13 | All of these are dangerous workflows and will result in mistakes and unwanted material showing up in your files (or wanted material being erased). Luckily for us, a variety of version control products have evolved in order to help us eliminate the need for these types of behaviors. Dropbox, Google Drives, SVN, Mercurial, and git are all examples of version control systems. 14 | 15 | Version control allows us to keep track of what changes have been made over time. Careful maintenance of code and data is vital to reproducibility which "is the hallmark of good science." While Dropbox and Google Drives may be useful for sharing certain types of files (I am not an advocate of their complete abandonment), they are not suitable for the fine tuned version control that we need to maintain good science (or software). 16 | 17 | ## What is `git`? 18 | 19 | Git is a _distributed_ version control system. _Distributed version control_ means that the entire history of every file is kept on your computer. It was originally written by Linus Torvalds (creator of Linux) to help maintain the Linux project (Fun Fact: Git was originally written because Torvalds found all other alternatives of version control to be too slow to manage a project as large as Linux, so he decided to write his own version which he began on 3 April 2005 and started being used on 7 April 2005. Read more about the [history](https://en.wikipedia.org/wiki/Git_(software))). 20 | 21 | ## Using `git` 22 | 23 | Many people associate `git` with the cloud based repository service Github, but `git` can be run independently either just on your own computer or on a self-hosted server. In this short tutorial, we are going to create a `git` repository hosted on our computer. We will then talk about some of the day to day commads that will be used in `git`. 24 | 25 | WARNING: Until you know what you're doing and exactly how they work, NEVER use the `-f` or `--force` flags no matter what the internet tells you. 26 | 27 | We are now going to walk through some of the basic settings you should set and an example of some commands. 28 | 29 | ### Configuration 30 | 31 | Here we deal with configuration details such as our default editor, user name, email, colors, etc... 32 | 33 | * Set name: `git config --global user.name FirstName LastName` 34 | * Set email: `git config --global user.email email@email.com` 35 | * Sets git colors: `git config --global color.ui "auto"` 36 | * Sets editor to vim: `git config --global core.editor "vim"` 37 | 38 | ### Creating a folder 39 | 40 | We will create a folder called `` using `mkdir ` 41 | 42 | ### Initializing a `git` repository 43 | 44 | Now enter that directory using `cd `. To initialize this directory as a `git` repository (which in the background will create a series of directories and files) we will use the command `git init`. 45 | 46 | We can see the things that were created by typing `ls .git`, but don't worry too much about what is being kept inside yet. 47 | 48 | ### Four Stages of Files 49 | 50 | Files in a git repository can be in four different stages: untracked, unstaged, staged, and committed. 51 | 52 | * Untracked: This is a new file that your repository hasn't seen before. 53 | * Unstaged: A file that has previously been tracked and saved, but has changed since its last version. 54 | * Staged: A file that has been changed and is prepared to be committed. Nothing is set in stone yet though and new changes can be made. 55 | * Committed: When a file is committed it becomes a piece of the history of the repository. This moment of time in the file will be able to be referenced or referred to in the future. 56 | 57 | The picture below illustrates this "life cycle" 58 | 59 |
60 | 61 |
62 | 63 | Let's illustrate this through an example and to expose ourselves to the commands that will be helpful: `git add`, `git commit`, `git diff`, and `git status`. 64 | 65 | First let's check what is new in our repository. Type `git status`. What does it say? It should say that there is nothing to commit because we haven't done anything yet. 66 | 67 | Now let's create a file called `README.md`. Open this file and type your name in it (could also use the command `echo "FirstName LastName" >> README.md`). If you type `git status` now, what do you see? It should list `README.md` as an untracked file. 68 | 69 | We can move this file from untracked to staged by using `git add README.md`. Type this command and then once again type `git status`. Our `README.md` file now shows up in green as a change to be committed. This means it is staged. 70 | 71 | We can take our "snapshot" of the file by using the command `git commit -m "Type a meaningful msg here"`. Commit the file and then once again type `git status`. What do you see now? The repository should be clean again. 72 | 73 | Add something new to the `README.md` file. Once again, we can check the status of our git repository by typing `git status`. It will tell us that `README.md` is unstaged because we have made changes to it. 74 | 75 | Imagine we wanted to double check the things that we changed. We could type `git diff README.md` which will show us the changes that have been made to that file since its previous commit. 76 | 77 | These commands will be the core of your `git` workflow so I suggest familiarizing yourself with what they do. 78 | 79 | ### Commit History 80 | 81 | Remember how we can leave ourselves commit messages. It is useful to leave meaningful commit messages because they are left as a guide for yourself. We can see our history of commits by typing `git log`. We can do this in several formats: Try `git log --pretty=oneline`, `git log --stat`, `git log --since=2.weeks`, etc... See the Git Pro book for more options. 82 | 83 | If we want to return to a previous commit in our history we can use the information from `git log` to get back. There is a sequence of numbers and letters, which we will call commit hash, at the top of each entry in your history. If you copy this and type `git reset ` then it will return us to that moment of our history. All of our more recent changes will be there, but they will be as if they had been just staged. Can use `git reset --hard` to reset to a previous point in time and delete all changes, but be very careful with that command as it can erase your history (in fact, I suggest not using it until you really know enough that you know it is what you want). 84 | 85 | ### Branching 86 | 87 | A branch is essentially an specific version of your folder. You start with a main branch which is called master. When you create another branch, it is an exact replica of the branch it is being created from (typically master) and includes the full history of the repository. After creating a new branch you can make changes and this new branch will develop its own history (without changing the history of the original branch -- such as master). If you decide you like the changes that you made then you can bring them into the original branch. 88 | 89 | The command `git checkout` is used for a few different things, but we will mostly use it for switching between branches. When used with the `-b` flag, it creates a new branch and switches to it. 90 | 91 | Let's create a new branch in our repository. `git checkout -b test`. What has changed? Let's look at the output of `git branch`. Now let's make some changes in our branch. 92 | 93 | ``` 94 | > echo "New line" >> README.md 95 | > git add README.md 96 | > git commit -m "Branch update" 97 | > git log 98 | ``` 99 | 100 | We can see the history has changed and we have a new commit. Let's return to the master branch by typing `git checkout master`. Now check `git log`, notice that we no longer have the changes from our test branch. 101 | 102 | Imagine that we decided to bring those changes into our master branch. We could use the `git merge` command by typing `git merge test` which will merge the branch `test` into the current branch. 103 | 104 | ### Ignore Files 105 | 106 | Sometimes there are files that get created via an intermediate process (such as `.aux`, `.synctex`, etc... in latex). We often don't care about keeping track of these files. Another wonderful thing about `git` is that it allows us to ignore files we don't care about! 107 | 108 | We do this by creating a file called `.gitignore` at the initial directory of our git repository. 109 | 110 | For example, `echo *.garbage >> .gitignore`. 111 | 112 | Then create a file called `foo.garbage` with `touch foo.garbage`. 113 | 114 | Now type `git status`. Notice that this file doesn't show up! It is because it recognizes that anything with an ending of `.garbage` should be ignored (this is because `*` is treated as a wildcard. Read more about regular expressions or [wild cards](http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm) for more information on how to use them). 115 | 116 | ## Resources and References 117 | 118 | Below is a sequence of references to things that I have found useful. I suggest reading a few of them and at least skimming the majority of them. In particular, the software carpentry lectures on git are very useful for learning the basics -- The Pro Git book is the biblical reference for git and can likely answer any question you will have for at least a few years to come. I have organized both sections by how relevant/convincing I found the documents. 119 | 120 | ### Git Technical References 121 | [Software Carpentry: Git Lectures](http://swcarpentry.github.io/git-novice/02-setup.html) 122 | [Pro Git](http://git-scm.com/) 123 | [Git Tower](https://www.git-tower.com/learn) 124 | 125 | ### Why Git References 126 | [Difference between git and Dropbox](https://gist.github.com/magicseth/1951984) 127 | [Version control for scientific research](http://blogs.biomedcentral.com/bmcblog/2013/02/28/version-control-for-scientific-research/) 128 | [Why Physicists Should Use Git Or Why Everyone Should Try Git](http://openmetric.org/assets/slides/whygit/#/) 129 | [Git can facilitate greater reproducibility and increased transparency in science](http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3639880/) 130 | 131 | -------------------------------------------------------------------------------- /lecture2/git_intro/images/gitlifecycle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture2/git_intro/images/gitlifecycle.png -------------------------------------------------------------------------------- /lecture2/git_intro/images/phd101212s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture2/git_intro/images/phd101212s.gif -------------------------------------------------------------------------------- /lecture2/lecture2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture2/lecture2.pdf -------------------------------------------------------------------------------- /lecture3/lecture3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture3/lecture3.pdf -------------------------------------------------------------------------------- /lecture4/lecture4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture4/lecture4.pdf -------------------------------------------------------------------------------- /lecture5/.ipynb_checkpoints/numpy_timing-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Speed Comparisons: NumPy vs Pure Python" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Speed comparisons for two different routines. \n", 15 | "\n", 16 | "First, some imports:" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": { 23 | "collapsed": true 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "import numpy as np\n", 28 | "import random" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Fix data size" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "n = int(10**6)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Make an evenly spaced grid of n points between 0 and 1." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 7, 59 | "metadata": { 60 | "collapsed": false 61 | }, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "10 loops, best of 3: 117 ms per loop\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "%%timeit\n", 73 | "x = []\n", 74 | "a = 0\n", 75 | "step = 1 / (n - 1)\n", 76 | "for i in range(n):\n", 77 | " x.append(a)\n", 78 | " a += step" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "Do the same using `np.linspace`." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 8, 91 | "metadata": { 92 | "collapsed": false 93 | }, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "100 loops, best of 3: 3.92 ms per loop\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "%%timeit\n", 105 | "x = np.linspace(0, 1, n)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "Take the maximum of n standard normals." 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 5, 118 | "metadata": { 119 | "collapsed": false 120 | }, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "1 loop, best of 3: 1 s per loop\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "%%timeit\n", 132 | "running_max = 0\n", 133 | "for i in range(n):\n", 134 | " x = random.normalvariate(0, 1)\n", 135 | " if x > running_max:\n", 136 | " running_max = x" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Do the same in NumPy." 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 6, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "10 loops, best of 3: 38.4 ms per loop\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "%%timeit\n", 163 | "all_max = np.max(np.random.randn(n))" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": { 170 | "collapsed": true 171 | }, 172 | "outputs": [], 173 | "source": [] 174 | } 175 | ], 176 | "metadata": { 177 | "kernelspec": { 178 | "display_name": "Python 3", 179 | "language": "python", 180 | "name": "python3" 181 | }, 182 | "language_info": { 183 | "codemirror_mode": { 184 | "name": "ipython", 185 | "version": 3 186 | }, 187 | "file_extension": ".py", 188 | "mimetype": "text/x-python", 189 | "name": "python", 190 | "nbconvert_exporter": "python", 191 | "pygments_lexer": "ipython3", 192 | "version": "3.5.1" 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 0 197 | } 198 | -------------------------------------------------------------------------------- /lecture5/fast_loop_examples/ar1_sample_mean.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | double ar1_ts (double * params, int n, unsigned long int seed) 9 | { 10 | double beta = params[0]; 11 | double alpha = params[1]; 12 | double s = params[2]; 13 | /* create a generator chosen by the environment variable GSL_RNG_TYPE */ 14 | const gsl_rng_type * T; 15 | gsl_rng * r; 16 | gsl_rng_env_setup(); 17 | T = gsl_rng_default; 18 | r = gsl_rng_alloc(T); 19 | gsl_rng_set(r, seed); 20 | 21 | int i; 22 | double x = beta / (1 - alpha); // Start at mean of stationary dist 23 | double sum = 0; 24 | for (i = 1; i <= n; i++) { 25 | sum += x; 26 | x = beta + alpha * x + gsl_ran_gaussian(r, s); 27 | } 28 | 29 | gsl_rng_free (r); 30 | return sum / n; 31 | } 32 | 33 | int main(void) 34 | { 35 | clock_t start, end; 36 | double cpu_time_used; 37 | 38 | int N = 10000000; 39 | double beta = 1.0; 40 | double alpha = 0.9; 41 | double s = 1; 42 | double params[3] = {beta, alpha, s}; 43 | 44 | start = clock(); 45 | double sample_mean = ar1_ts(params, N, 1); 46 | end = clock(); 47 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; 48 | 49 | printf("mean = %g\n", sample_mean); 50 | printf("time elapsed = %g\n", cpu_time_used); 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /lecture5/fast_loop_examples/ar1_sample_mean.jl: -------------------------------------------------------------------------------- 1 | function ar1_sample_mean(N, beta, alpha, s) 2 | sm = 0.0 3 | x = beta / (1 - alpha) 4 | for i in 1:N 5 | sm += x 6 | x = beta + alpha * x + s * randn() 7 | end 8 | return sm / N 9 | end 10 | 11 | N = 10000000 12 | beta = 1.0 13 | alpha = 0.9 14 | s = 1.0 15 | tic() 16 | result = ar1_sample_mean(N, beta, alpha, s) 17 | println("mean = $result") 18 | toc() 19 | 20 | -------------------------------------------------------------------------------- /lecture5/fast_loop_examples/ar1_sample_mean.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import time 3 | from numba import jit 4 | 5 | @jit 6 | def ar1_sample_mean(N, alpha, beta, s): 7 | x = beta / (1 - alpha) 8 | sm = 0.0 9 | for i in range(N): 10 | x = beta + alpha * x + s * np.random.randn() 11 | sm += x 12 | return sm / N 13 | 14 | N = 10000000 15 | alpha = 0.9 16 | beta = 1.0 17 | s = 1.0 18 | 19 | t = time.time() 20 | result = ar1_sample_mean(N, alpha, beta, s) 21 | elapsed = time.time() - t 22 | 23 | print("mean = {}".format(result)) 24 | print("elapsed time = {}".format(elapsed)) 25 | -------------------------------------------------------------------------------- /lecture5/fast_loop_examples/foo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture5/fast_loop_examples/foo -------------------------------------------------------------------------------- /lecture5/fast_loop_examples/makefile: -------------------------------------------------------------------------------- 1 | maketest: 2 | gcc -Wall ar1_sample_mean.c -lgsl -lgslcblas -lm -o foo 3 | 4 | -------------------------------------------------------------------------------- /lecture5/lecture5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture5/lecture5.pdf -------------------------------------------------------------------------------- /lecture5/numpy_timing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Speed Comparisons: NumPy vs Pure Python" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Speed comparisons for two different routines. \n", 15 | "\n", 16 | "First, some imports:" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": { 23 | "collapsed": true 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "import numpy as np\n", 28 | "import random" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Fix data size" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "n = int(10**6)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Make an evenly spaced grid of n points between 0 and 1." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 7, 59 | "metadata": { 60 | "collapsed": false 61 | }, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "10 loops, best of 3: 117 ms per loop\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "%%timeit\n", 73 | "x = []\n", 74 | "a = 0\n", 75 | "step = 1 / (n - 1)\n", 76 | "for i in range(n):\n", 77 | " x.append(a)\n", 78 | " a += step" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "Do the same using `np.linspace`." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 8, 91 | "metadata": { 92 | "collapsed": false 93 | }, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "100 loops, best of 3: 3.92 ms per loop\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "%%timeit\n", 105 | "x = np.linspace(0, 1, n)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "Take the maximum of n standard normals." 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 5, 118 | "metadata": { 119 | "collapsed": false 120 | }, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "1 loop, best of 3: 1 s per loop\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "%%timeit\n", 132 | "running_max = 0\n", 133 | "for i in range(n):\n", 134 | " x = random.normalvariate(0, 1)\n", 135 | " if x > running_max:\n", 136 | " running_max = x" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Do the same in NumPy." 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 6, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "10 loops, best of 3: 38.4 ms per loop\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "%%timeit\n", 163 | "all_max = np.max(np.random.randn(n))" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": { 170 | "collapsed": true 171 | }, 172 | "outputs": [], 173 | "source": [] 174 | } 175 | ], 176 | "metadata": { 177 | "kernelspec": { 178 | "display_name": "Python 3", 179 | "language": "python", 180 | "name": "python3" 181 | }, 182 | "language_info": { 183 | "codemirror_mode": { 184 | "name": "ipython", 185 | "version": 3 186 | }, 187 | "file_extension": ".py", 188 | "mimetype": "text/x-python", 189 | "name": "python", 190 | "nbconvert_exporter": "python", 191 | "pygments_lexer": "ipython3", 192 | "version": "3.5.1" 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 0 197 | } 198 | -------------------------------------------------------------------------------- /lecture6/.ipynb_checkpoints/IntroToJulia-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "## Introduction to Julia\n", 12 | "\n", 13 | "**Chase Coleman & Spencer Lyon**\n", 14 | "\n", 15 | "3-4-16" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "slideshow": { 22 | "slide_type": "slide" 23 | } 24 | }, 25 | "source": [ 26 | "## Opening Example" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 4, 32 | "metadata": { 33 | "collapsed": false, 34 | "slideshow": { 35 | "slide_type": "fragment" 36 | } 37 | }, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "bisect (generic function with 3 methods)" 43 | ] 44 | }, 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "# *1 *2 *3\n", 52 | "function bisect(f, a, b, maxit=100, tol::Float64=1e-9)\n", 53 | " fa, fb = f(a), f(b)\n", 54 | " # *4 *5\n", 55 | " for it in 1:maxit \n", 56 | " mid = (a + b)/2\n", 57 | " fmid = f(mid)\n", 58 | " \n", 59 | " # *6\n", 60 | " if abs(fmid) < tol \n", 61 | " # *7\n", 62 | " return mid\n", 63 | " end\n", 64 | "\n", 65 | " if fa*fmid > 0\n", 66 | " fa, a = fmid, mid \n", 67 | " else\n", 68 | " fb, b = fmid, mid\n", 69 | " end\n", 70 | " end\n", 71 | " \n", 72 | " # *8\n", 73 | " error(\"maximum iterations exceeded\")\n", 74 | "end" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": { 80 | "slideshow": { 81 | "slide_type": "subslide" 82 | } 83 | }, 84 | "source": [ 85 | "1. Define new functions with `function ... end`\n", 86 | "2. Default arugments `(..., arg=default_value)`\n", 87 | "3. Typed arguments `(..., arg::Type)`\n", 88 | "4. For loop `for X (in|=) SOMETHING ... end`\n", 89 | "5. Create ranges `A:B` (not dense like Matlab)\n", 90 | "6. If statement `if CONDITION BLOCK end`\n", 91 | "7. Return statement `return STUFF` (optional, see next example)\n", 92 | "8. Throwing error `error(MESSAGE)`" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "metadata": { 99 | "collapsed": false, 100 | "slideshow": { 101 | "slide_type": "subslide" 102 | } 103 | }, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "f3 (generic function with 1 method)" 109 | ] 110 | }, 111 | "execution_count": 5, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "# shorthand function syntax\n", 118 | "f(x) = x^2-2\n", 119 | "\n", 120 | "# longer syntax -- equivalent to above\n", 121 | "function f2(x)\n", 122 | " x^2-2\n", 123 | "end\n", 124 | "\n", 125 | "# much longer syntax -- still equivalent to above\n", 126 | "function f3(x)\n", 127 | " return x^2-2\n", 128 | "end" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 6, 134 | "metadata": { 135 | "collapsed": false, 136 | "slideshow": { 137 | "slide_type": "fragment" 138 | } 139 | }, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "1.4142135623842478\n", 146 | "1.4142135623842478\n", 147 | "1.4142135623842478\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "println(bisect(f, 0, 4))\n", 153 | "println(bisect(f2, 0, 4))\n", 154 | "println(bisect(f3, 0, 4))" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": { 160 | "slideshow": { 161 | "slide_type": "slide" 162 | } 163 | }, 164 | "source": [ 165 | "## Types\n", 166 | "\n", 167 | "Everything in Julia has a `type`.\n", 168 | "\n", 169 | "You can inspect the `type` of an object using the `typeof` function:" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 18, 175 | "metadata": { 176 | "collapsed": false, 177 | "slideshow": { 178 | "slide_type": "fragment" 179 | } 180 | }, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "1.0 is a Float64\n", 187 | "1 is a Int64\n", 188 | "foo is a ASCIIString\n", 189 | "φ is a UTF8String\n", 190 | "bisect is a Function\n", 191 | "4 is a Int8\n", 192 | "Float64 is a DataType\n", 193 | "true is a Bool\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "for obj in [1.0, 1, \"foo\", \"φ\", bisect, Int8(4), Float64, true]\n", 199 | " # Notice string interpolation syntax `$`\n", 200 | " println(\"$obj is a $(typeof(obj))\")\n", 201 | "end" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": { 207 | "slideshow": { 208 | "slide_type": "subslide" 209 | } 210 | }, 211 | "source": [ 212 | "### Type Parameters\n", 213 | "\n", 214 | "Types can have parameters. \n", 215 | "\n", 216 | "This concept is best understood by example" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 20, 222 | "metadata": { 223 | "collapsed": false, 224 | "slideshow": { 225 | "slide_type": "fragment" 226 | } 227 | }, 228 | "outputs": [ 229 | { 230 | "data": { 231 | "text/plain": [ 232 | "Array{Int64,1}" 233 | ] 234 | }, 235 | "execution_count": 20, 236 | "metadata": {}, 237 | "output_type": "execute_result" 238 | } 239 | ], 240 | "source": [ 241 | "x = [1, 2, 3]\n", 242 | "typeof(x) # whats the `{` and `}` stuff all about?" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": { 248 | "slideshow": { 249 | "slide_type": "fragment" 250 | } 251 | }, 252 | "source": [ 253 | "`Int` and `1` are type parameters. In this case it tells us that the array\n", 254 | "is filled with `Int`s and has `1` dimension (it is a vector, yes Julia has\n", 255 | "vectors).\n", 256 | "\n", 257 | "Allow you to do all sorts of magic that will become clear later. For now just\n", 258 | "recognize the `{` `}` syntax." 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": { 264 | "collapsed": true, 265 | "slideshow": { 266 | "slide_type": "subslide" 267 | } 268 | }, 269 | "source": [ 270 | "### User defined types\n", 271 | "\n", 272 | "You can define your own types (you _should_ do this a lot).\n", 273 | "\n", 274 | "Two forms of types:\n", 275 | "\n", 276 | "- abstract: you can't create these, but they help you group related types together\n", 277 | "- composite: you do create these, they are the actual data of your program" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 21, 283 | "metadata": { 284 | "collapsed": true, 285 | "slideshow": { 286 | "slide_type": "subslide" 287 | } 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "abstract Exog\n", 292 | "\n", 293 | "type AR1 <: Exog\n", 294 | " rho::Float64 # you should put types on the fields of your types\n", 295 | " sigma::Float64\n", 296 | "end\n", 297 | "\n", 298 | "\"\"\"\n", 299 | "I'm docstring. I describe the `MarkovChain` type.\n", 300 | "\n", 301 | "`x0` is the initial distribution\n", 302 | "\"\"\"\n", 303 | "type MarkovChain{T} <: Exog\n", 304 | " Π::Matrix{Float64}\n", 305 | " vals::AbstractVector{T}\n", 306 | " x0::Vector{Float64}\n", 307 | "end\n", 308 | "\n", 309 | "# functions to give `vals` and `x0` default arguments\n", 310 | "MarkovChain(Π, v) = MarkovChain(Π, v, fill(1/length(v), length(v)))\n", 311 | "MarkovChain(Π) = MarkovChain(Π, 1:size(Π, 1))" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 24, 317 | "metadata": { 318 | "collapsed": false, 319 | "slideshow": { 320 | "slide_type": "fragment" 321 | } 322 | }, 323 | "outputs": [ 324 | { 325 | "data": { 326 | "text/plain": [ 327 | "AR1(0.9,0.1)" 328 | ] 329 | }, 330 | "execution_count": 24, 331 | "metadata": {}, 332 | "output_type": "execute_result" 333 | } 334 | ], 335 | "source": [ 336 | "AR1(0.9, 0.1)" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 23, 342 | "metadata": { 343 | "collapsed": false, 344 | "slideshow": { 345 | "slide_type": "fragment" 346 | } 347 | }, 348 | "outputs": [ 349 | { 350 | "name": "stdout", 351 | "output_type": "stream", 352 | "text": [ 353 | "search: " 354 | ] 355 | }, 356 | { 357 | "data": { 358 | "text/latex": [ 359 | "I'm docstring. I describe the \\texttt{MarkovChain} type.\n", 360 | "\\texttt{x0} is the initial distribution\n" 361 | ], 362 | "text/markdown": [ 363 | "I'm docstring. I describe the `MarkovChain` type.\n", 364 | "\n", 365 | "`x0` is the initial distribution\n" 366 | ], 367 | "text/plain": [ 368 | "I'm docstring. I describe the `MarkovChain` type.\n", 369 | "\n", 370 | "`x0` is the initial distribution\n" 371 | ] 372 | }, 373 | "execution_count": 23, 374 | "metadata": {}, 375 | "output_type": "execute_result" 376 | }, 377 | { 378 | "name": "stdout", 379 | "output_type": "stream", 380 | "text": [ 381 | "MarkovChain\n", 382 | "\n" 383 | ] 384 | } 385 | ], 386 | "source": [ 387 | "?MarkovChain" 388 | ] 389 | }, 390 | { 391 | "cell_type": "markdown", 392 | "metadata": { 393 | "slideshow": { 394 | "slide_type": "slide" 395 | } 396 | }, 397 | "source": [ 398 | "## Multiple Dispatch\n", 399 | "\n", 400 | "Function behavior can be specialized based on the type (and number) of all function arguments\n", 401 | "\n", 402 | "Let's see some examples" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 41, 408 | "metadata": { 409 | "collapsed": false, 410 | "slideshow": { 411 | "slide_type": "fragment" 412 | } 413 | }, 414 | "outputs": [ 415 | { 416 | "name": "stdout", 417 | "output_type": "stream", 418 | "text": [ 419 | "g(hello) I have something\n", 420 | "g(1) I have an integer\n", 421 | "g(1.0) I have a float\n", 422 | "g(1//2) I have some kind of number\n", 423 | "g([1,2,3]) I have an array\n" 424 | ] 425 | } 426 | ], 427 | "source": [ 428 | "g(x) = \"I have something\"\n", 429 | "g(x::Int) = \"I have an integer\"\n", 430 | "g(x::Float64) = \"I have a float\"\n", 431 | "g(x::Number) = \"I have some kind of number\"\n", 432 | "g(x::Array) = \"I have an array\"\n", 433 | "\n", 434 | "for x in (\"hello\", 1, 1.0, 1//2, [1, 2, 3])\n", 435 | " @printf \"%-12s%s\\n\" \"g($x)\" g(x)\n", 436 | "end" 437 | ] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": { 442 | "slideshow": { 443 | "slide_type": "subslide" 444 | } 445 | }, 446 | "source": [ 447 | "We can add methods to the `g` function that take multiple arguments\n", 448 | "\n", 449 | "Notice how the return value depends on the types of both arguments" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 51, 455 | "metadata": { 456 | "collapsed": false 457 | }, 458 | "outputs": [ 459 | { 460 | "data": { 461 | "text/plain": [ 462 | "g (generic function with 13 methods)" 463 | ] 464 | }, 465 | "execution_count": 51, 466 | "metadata": {}, 467 | "output_type": "execute_result" 468 | } 469 | ], 470 | "source": [ 471 | "g(x, y) = \"I have two things\"\n", 472 | "g(x::Int, y) = \"I have an integer and something else\"\n", 473 | "g(x::Int, y::Number) = \"I have an integer and a number\"\n", 474 | "g(x::Int, y::Int) = \"I have two integers\"\n", 475 | "g(x::Array, y::Array) = \"I have two arrays\"\n", 476 | "g(x::Array{Float64}, y::Array{Float64}) = \"I have two arrays that have floats\"" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 52, 482 | "metadata": { 483 | "collapsed": false, 484 | "slideshow": { 485 | "slide_type": "fragment" 486 | } 487 | }, 488 | "outputs": [ 489 | { 490 | "name": "stdout", 491 | "output_type": "stream", 492 | "text": [ 493 | "g(x, y) I have two things\n", 494 | "g(1, x) I have an integer and something else\n", 495 | "g(1, 1//2) I have an integer and a number\n", 496 | "g(1, 2) I have two integers\n", 497 | "g(1, 2.0) I have an integer and a number\n", 498 | "g([1], [2]) I have two arrays\n", 499 | "g([1.0], [2.0]) I have two arrays that have floats\n" 500 | ] 501 | } 502 | ], 503 | "source": [ 504 | "stuff = ((\"x\", \"y\"), (1, \"x\"), (1, 1//2), \n", 505 | " (1, 2), (1, 2.0), ([1], [2]), \n", 506 | " ([1.0], [2.0]))\n", 507 | "\n", 508 | "for (x1, x2) in stuff\n", 509 | " @printf \"%-18s%s\\n\" \"g($x1, $x2)\" g(x1, x2)\n", 510 | "end" 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": { 516 | "slideshow": { 517 | "slide_type": "slide" 518 | } 519 | }, 520 | "source": [ 521 | "## Example\n", 522 | "\n", 523 | "Let's construct routines that will allow us to simulate any subtype of `Exog`\n", 524 | "\n", 525 | "To do this we will need each subtype of `Exog` to implement a `iter` method\n", 526 | "\n", 527 | "This method should take two arguments:\n", 528 | "\n", 529 | "- The `Exog` subtype\n", 530 | "- The current state\n", 531 | "\n", 532 | "It should return the state on the next `iter`ation" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 58, 538 | "metadata": { 539 | "collapsed": false, 540 | "slideshow": { 541 | "slide_type": "subslide" 542 | } 543 | }, 544 | "outputs": [ 545 | { 546 | "data": { 547 | "text/plain": [ 548 | "iter (generic function with 4 methods)" 549 | ] 550 | }, 551 | "execution_count": 58, 552 | "metadata": {}, 553 | "output_type": "execute_result" 554 | } 555 | ], 556 | "source": [ 557 | "iter(ar1::AR1, x) = ar1.rho*x + ar1.sigma*randn()\n", 558 | "\n", 559 | "function iter(mc::MarkovChain, s::Int, v)\n", 560 | " ind = searchsortedfirst(cumsum(vec(mc.Π[s, :])), rand())\n", 561 | " return mc.vals[ind]\n", 562 | "end\n", 563 | "\n", 564 | "iter{T}(mc::MarkovChain{T}, v::T) = iter(mc, findfirst(mc.vals, v), v)\n", 565 | "iter(::Exog, x) = error(\"iter should be implemented by each Exog subtype\")" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": { 571 | "slideshow": { 572 | "slide_type": "subslide" 573 | } 574 | }, 575 | "source": [ 576 | "Now we will define single `simulate` function for all `Exog` subtypes" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": 64, 582 | "metadata": { 583 | "collapsed": false 584 | }, 585 | "outputs": [ 586 | { 587 | "data": { 588 | "text/plain": [ 589 | "simulate (generic function with 2 methods)" 590 | ] 591 | }, 592 | "execution_count": 64, 593 | "metadata": {}, 594 | "output_type": "execute_result" 595 | } 596 | ], 597 | "source": [ 598 | "# NOTE `;` for keyword argument\n", 599 | "function simulate(ex::Exog, x0; capT::Int=10)\n", 600 | " out = Array(typeof(x0), capT)\n", 601 | " out[1] = x0\n", 602 | "\n", 603 | " for t = 2:capT\n", 604 | " out[t] = iter(ex, out[t-1])\n", 605 | " end\n", 606 | " out\n", 607 | "end\n", 608 | "\n", 609 | "# for MarkovChain we have more info, so we don't need to give it an x0\n", 610 | "# define another method that hands off to the method above\n", 611 | "function simulate(mc::MarkovChain; capT::Int=100)\n", 612 | " v = mc.vals[searchsortedfirst(cumsum(mc.x0), rand())]\n", 613 | " simulate(mc, v; capT=capT)\n", 614 | "end" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": 70, 620 | "metadata": { 621 | "collapsed": false, 622 | "slideshow": { 623 | "slide_type": "subslide" 624 | } 625 | }, 626 | "outputs": [ 627 | { 628 | "data": { 629 | "text/plain": [ 630 | "10-element Array{Float64,1}:\n", 631 | " 0.5 \n", 632 | " 0.415777\n", 633 | " 0.364054\n", 634 | " 0.320674\n", 635 | " 0.300484\n", 636 | " 0.456054\n", 637 | " 0.316475\n", 638 | " 0.303914\n", 639 | " 0.35384 \n", 640 | " 0.282838" 641 | ] 642 | }, 643 | "execution_count": 70, 644 | "metadata": {}, 645 | "output_type": "execute_result" 646 | } 647 | ], 648 | "source": [ 649 | "ar1 = AR1(0.9, 0.1)\n", 650 | "simulate(ar1, 0.5)" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": 69, 656 | "metadata": { 657 | "collapsed": false, 658 | "slideshow": { 659 | "slide_type": "fragment" 660 | } 661 | }, 662 | "outputs": [ 663 | { 664 | "data": { 665 | "text/plain": [ 666 | "5-element Array{Float64,1}:\n", 667 | " 0.5\n", 668 | " 0.5\n", 669 | " 0.5\n", 670 | " 0.9\n", 671 | " 0.5" 672 | ] 673 | }, 674 | "execution_count": 69, 675 | "metadata": {}, 676 | "output_type": "execute_result" 677 | } 678 | ], 679 | "source": [ 680 | "mc = MarkovChain([0.7 0.3; 0.4 0.6], [0.5, 0.9], [0.2, 0.8])\n", 681 | "simulate(mc; capT=5)" 682 | ] 683 | } 684 | ], 685 | "metadata": { 686 | "kernelspec": { 687 | "display_name": "Julia 0.4.2", 688 | "language": "julia", 689 | "name": "julia-0.4" 690 | }, 691 | "language_info": { 692 | "file_extension": ".jl", 693 | "mimetype": "application/julia", 694 | "name": "julia", 695 | "version": "0.4.2" 696 | }, 697 | "livereveal": { 698 | "scroll": true, 699 | "start_slideshow_at": "selected", 700 | "theme": "white", 701 | "transition": "fade" 702 | } 703 | }, 704 | "nbformat": 4, 705 | "nbformat_minor": 0 706 | } 707 | -------------------------------------------------------------------------------- /lecture6/IntroToJulia.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "## Introduction to Julia\n", 12 | "\n", 13 | "**Chase Coleman & Spencer Lyon**\n", 14 | "\n", 15 | "3-4-16" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "slideshow": { 22 | "slide_type": "slide" 23 | } 24 | }, 25 | "source": [ 26 | "## Opening Example" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 4, 32 | "metadata": { 33 | "collapsed": false, 34 | "slideshow": { 35 | "slide_type": "fragment" 36 | } 37 | }, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "bisect (generic function with 3 methods)" 43 | ] 44 | }, 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "# *1 *2 *3\n", 52 | "function bisect(f, a, b, maxit=100, tol::Float64=1e-9)\n", 53 | " fa, fb = f(a), f(b)\n", 54 | " # *4 *5\n", 55 | " for it in 1:maxit \n", 56 | " mid = (a + b)/2\n", 57 | " fmid = f(mid)\n", 58 | " \n", 59 | " # *6\n", 60 | " if abs(fmid) < tol \n", 61 | " # *7\n", 62 | " return mid\n", 63 | " end\n", 64 | "\n", 65 | " if fa*fmid > 0\n", 66 | " fa, a = fmid, mid \n", 67 | " else\n", 68 | " fb, b = fmid, mid\n", 69 | " end\n", 70 | " end\n", 71 | " \n", 72 | " # *8\n", 73 | " error(\"maximum iterations exceeded\")\n", 74 | "end" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": { 80 | "slideshow": { 81 | "slide_type": "subslide" 82 | } 83 | }, 84 | "source": [ 85 | "1. Define new functions with `function ... end`\n", 86 | "2. Default arugments `(..., arg=default_value)`\n", 87 | "3. Typed arguments `(..., arg::Type)`\n", 88 | "4. For loop `for X (in|=) SOMETHING ... end`\n", 89 | "5. Create ranges `A:B` (not dense like Matlab)\n", 90 | "6. If statement `if CONDITION BLOCK end`\n", 91 | "7. Return statement `return STUFF` (optional, see next example)\n", 92 | "8. Throwing error `error(MESSAGE)`" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "metadata": { 99 | "collapsed": false, 100 | "slideshow": { 101 | "slide_type": "subslide" 102 | } 103 | }, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "f3 (generic function with 1 method)" 109 | ] 110 | }, 111 | "execution_count": 5, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "# shorthand function syntax\n", 118 | "f(x) = x^2-2\n", 119 | "\n", 120 | "# longer syntax -- equivalent to above\n", 121 | "function f2(x)\n", 122 | " x^2-2\n", 123 | "end\n", 124 | "\n", 125 | "# much longer syntax -- still equivalent to above\n", 126 | "function f3(x)\n", 127 | " return x^2-2\n", 128 | "end" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 6, 134 | "metadata": { 135 | "collapsed": false, 136 | "slideshow": { 137 | "slide_type": "fragment" 138 | } 139 | }, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "1.4142135623842478\n", 146 | "1.4142135623842478\n", 147 | "1.4142135623842478\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "println(bisect(f, 0, 4))\n", 153 | "println(bisect(f2, 0, 4))\n", 154 | "println(bisect(f3, 0, 4))" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": { 160 | "slideshow": { 161 | "slide_type": "slide" 162 | } 163 | }, 164 | "source": [ 165 | "## Types\n", 166 | "\n", 167 | "Everything in Julia has a `type`.\n", 168 | "\n", 169 | "You can inspect the `type` of an object using the `typeof` function:" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 18, 175 | "metadata": { 176 | "collapsed": false, 177 | "slideshow": { 178 | "slide_type": "fragment" 179 | } 180 | }, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "1.0 is a Float64\n", 187 | "1 is a Int64\n", 188 | "foo is a ASCIIString\n", 189 | "φ is a UTF8String\n", 190 | "bisect is a Function\n", 191 | "4 is a Int8\n", 192 | "Float64 is a DataType\n", 193 | "true is a Bool\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "for obj in [1.0, 1, \"foo\", \"φ\", bisect, Int8(4), Float64, true]\n", 199 | " # Notice string interpolation syntax `$`\n", 200 | " println(\"$obj is a $(typeof(obj))\")\n", 201 | "end" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": { 207 | "slideshow": { 208 | "slide_type": "subslide" 209 | } 210 | }, 211 | "source": [ 212 | "### Type Parameters\n", 213 | "\n", 214 | "Types can have parameters. \n", 215 | "\n", 216 | "This concept is best understood by example" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 20, 222 | "metadata": { 223 | "collapsed": false, 224 | "slideshow": { 225 | "slide_type": "fragment" 226 | } 227 | }, 228 | "outputs": [ 229 | { 230 | "data": { 231 | "text/plain": [ 232 | "Array{Int64,1}" 233 | ] 234 | }, 235 | "execution_count": 20, 236 | "metadata": {}, 237 | "output_type": "execute_result" 238 | } 239 | ], 240 | "source": [ 241 | "x = [1, 2, 3]\n", 242 | "typeof(x) # whats the `{` and `}` stuff all about?" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": { 248 | "slideshow": { 249 | "slide_type": "fragment" 250 | } 251 | }, 252 | "source": [ 253 | "`Int` and `1` are type parameters. In this case it tells us that the array\n", 254 | "is filled with `Int`s and has `1` dimension (it is a vector, yes Julia has\n", 255 | "vectors).\n", 256 | "\n", 257 | "Allow you to do all sorts of magic that will become clear later. For now just\n", 258 | "recognize the `{` `}` syntax." 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": { 264 | "collapsed": true, 265 | "slideshow": { 266 | "slide_type": "subslide" 267 | } 268 | }, 269 | "source": [ 270 | "### User defined types\n", 271 | "\n", 272 | "You can define your own types (you _should_ do this a lot).\n", 273 | "\n", 274 | "Two forms of types:\n", 275 | "\n", 276 | "- abstract: you can't create these, but they help you group related types together\n", 277 | "- composite: you do create these, they are the actual data of your program" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 21, 283 | "metadata": { 284 | "collapsed": true, 285 | "slideshow": { 286 | "slide_type": "subslide" 287 | } 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "abstract Exog\n", 292 | "\n", 293 | "type AR1 <: Exog\n", 294 | " rho::Float64 # you should put types on the fields of your types\n", 295 | " sigma::Float64\n", 296 | "end\n", 297 | "\n", 298 | "\"\"\"\n", 299 | "I'm docstring. I describe the `MarkovChain` type.\n", 300 | "\n", 301 | "`x0` is the initial distribution\n", 302 | "\"\"\"\n", 303 | "type MarkovChain{T} <: Exog\n", 304 | " Π::Matrix{Float64}\n", 305 | " vals::AbstractVector{T}\n", 306 | " x0::Vector{Float64}\n", 307 | "end\n", 308 | "\n", 309 | "# functions to give `vals` and `x0` default arguments\n", 310 | "MarkovChain(Π, v) = MarkovChain(Π, v, fill(1/length(v), length(v)))\n", 311 | "MarkovChain(Π) = MarkovChain(Π, 1:size(Π, 1))" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 24, 317 | "metadata": { 318 | "collapsed": false, 319 | "slideshow": { 320 | "slide_type": "fragment" 321 | } 322 | }, 323 | "outputs": [ 324 | { 325 | "data": { 326 | "text/plain": [ 327 | "AR1(0.9,0.1)" 328 | ] 329 | }, 330 | "execution_count": 24, 331 | "metadata": {}, 332 | "output_type": "execute_result" 333 | } 334 | ], 335 | "source": [ 336 | "AR1(0.9, 0.1)" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 23, 342 | "metadata": { 343 | "collapsed": false, 344 | "slideshow": { 345 | "slide_type": "fragment" 346 | } 347 | }, 348 | "outputs": [ 349 | { 350 | "name": "stdout", 351 | "output_type": "stream", 352 | "text": [ 353 | "search: " 354 | ] 355 | }, 356 | { 357 | "data": { 358 | "text/latex": [ 359 | "I'm docstring. I describe the \\texttt{MarkovChain} type.\n", 360 | "\\texttt{x0} is the initial distribution\n" 361 | ], 362 | "text/markdown": [ 363 | "I'm docstring. I describe the `MarkovChain` type.\n", 364 | "\n", 365 | "`x0` is the initial distribution\n" 366 | ], 367 | "text/plain": [ 368 | "I'm docstring. I describe the `MarkovChain` type.\n", 369 | "\n", 370 | "`x0` is the initial distribution\n" 371 | ] 372 | }, 373 | "execution_count": 23, 374 | "metadata": {}, 375 | "output_type": "execute_result" 376 | }, 377 | { 378 | "name": "stdout", 379 | "output_type": "stream", 380 | "text": [ 381 | "MarkovChain\n", 382 | "\n" 383 | ] 384 | } 385 | ], 386 | "source": [ 387 | "?MarkovChain" 388 | ] 389 | }, 390 | { 391 | "cell_type": "markdown", 392 | "metadata": { 393 | "slideshow": { 394 | "slide_type": "slide" 395 | } 396 | }, 397 | "source": [ 398 | "## Multiple Dispatch\n", 399 | "\n", 400 | "Function behavior can be specialized based on the type (and number) of all function arguments\n", 401 | "\n", 402 | "Let's see some examples" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 41, 408 | "metadata": { 409 | "collapsed": false, 410 | "slideshow": { 411 | "slide_type": "fragment" 412 | } 413 | }, 414 | "outputs": [ 415 | { 416 | "name": "stdout", 417 | "output_type": "stream", 418 | "text": [ 419 | "g(hello) I have something\n", 420 | "g(1) I have an integer\n", 421 | "g(1.0) I have a float\n", 422 | "g(1//2) I have some kind of number\n", 423 | "g([1,2,3]) I have an array\n" 424 | ] 425 | } 426 | ], 427 | "source": [ 428 | "g(x) = \"I have something\"\n", 429 | "g(x::Int) = \"I have an integer\"\n", 430 | "g(x::Float64) = \"I have a float\"\n", 431 | "g(x::Number) = \"I have some kind of number\"\n", 432 | "g(x::Array) = \"I have an array\"\n", 433 | "\n", 434 | "for x in (\"hello\", 1, 1.0, 1//2, [1, 2, 3])\n", 435 | " @printf \"%-12s%s\\n\" \"g($x)\" g(x)\n", 436 | "end" 437 | ] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": { 442 | "slideshow": { 443 | "slide_type": "subslide" 444 | } 445 | }, 446 | "source": [ 447 | "We can add methods to the `g` function that take multiple arguments\n", 448 | "\n", 449 | "Notice how the return value depends on the types of both arguments" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 51, 455 | "metadata": { 456 | "collapsed": false 457 | }, 458 | "outputs": [ 459 | { 460 | "data": { 461 | "text/plain": [ 462 | "g (generic function with 13 methods)" 463 | ] 464 | }, 465 | "execution_count": 51, 466 | "metadata": {}, 467 | "output_type": "execute_result" 468 | } 469 | ], 470 | "source": [ 471 | "g(x, y) = \"I have two things\"\n", 472 | "g(x::Int, y) = \"I have an integer and something else\"\n", 473 | "g(x::Int, y::Number) = \"I have an integer and a number\"\n", 474 | "g(x::Int, y::Int) = \"I have two integers\"\n", 475 | "g(x::Array, y::Array) = \"I have two arrays\"\n", 476 | "g(x::Array{Float64}, y::Array{Float64}) = \"I have two arrays that have floats\"" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 52, 482 | "metadata": { 483 | "collapsed": false, 484 | "slideshow": { 485 | "slide_type": "fragment" 486 | } 487 | }, 488 | "outputs": [ 489 | { 490 | "name": "stdout", 491 | "output_type": "stream", 492 | "text": [ 493 | "g(x, y) I have two things\n", 494 | "g(1, x) I have an integer and something else\n", 495 | "g(1, 1//2) I have an integer and a number\n", 496 | "g(1, 2) I have two integers\n", 497 | "g(1, 2.0) I have an integer and a number\n", 498 | "g([1], [2]) I have two arrays\n", 499 | "g([1.0], [2.0]) I have two arrays that have floats\n" 500 | ] 501 | } 502 | ], 503 | "source": [ 504 | "stuff = ((\"x\", \"y\"), (1, \"x\"), (1, 1//2), \n", 505 | " (1, 2), (1, 2.0), ([1], [2]), \n", 506 | " ([1.0], [2.0]))\n", 507 | "\n", 508 | "for (x1, x2) in stuff\n", 509 | " @printf \"%-18s%s\\n\" \"g($x1, $x2)\" g(x1, x2)\n", 510 | "end" 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": { 516 | "slideshow": { 517 | "slide_type": "slide" 518 | } 519 | }, 520 | "source": [ 521 | "## Example\n", 522 | "\n", 523 | "Let's construct routines that will allow us to simulate any subtype of `Exog`\n", 524 | "\n", 525 | "To do this we will need each subtype of `Exog` to implement a `iter` method\n", 526 | "\n", 527 | "This method should take two arguments:\n", 528 | "\n", 529 | "- The `Exog` subtype\n", 530 | "- The current state\n", 531 | "\n", 532 | "It should return the state on the next `iter`ation" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 58, 538 | "metadata": { 539 | "collapsed": false, 540 | "slideshow": { 541 | "slide_type": "subslide" 542 | } 543 | }, 544 | "outputs": [ 545 | { 546 | "data": { 547 | "text/plain": [ 548 | "iter (generic function with 4 methods)" 549 | ] 550 | }, 551 | "execution_count": 58, 552 | "metadata": {}, 553 | "output_type": "execute_result" 554 | } 555 | ], 556 | "source": [ 557 | "iter(ar1::AR1, x) = ar1.rho*x + ar1.sigma*randn()\n", 558 | "\n", 559 | "function iter(mc::MarkovChain, s::Int, v)\n", 560 | " ind = searchsortedfirst(cumsum(vec(mc.Π[s, :])), rand())\n", 561 | " return mc.vals[ind]\n", 562 | "end\n", 563 | "\n", 564 | "iter{T}(mc::MarkovChain{T}, v::T) = iter(mc, findfirst(mc.vals, v), v)\n", 565 | "iter(::Exog, x) = error(\"iter should be implemented by each Exog subtype\")" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": { 571 | "slideshow": { 572 | "slide_type": "subslide" 573 | } 574 | }, 575 | "source": [ 576 | "Now we will define single `simulate` function for all `Exog` subtypes" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": 64, 582 | "metadata": { 583 | "collapsed": false 584 | }, 585 | "outputs": [ 586 | { 587 | "data": { 588 | "text/plain": [ 589 | "simulate (generic function with 2 methods)" 590 | ] 591 | }, 592 | "execution_count": 64, 593 | "metadata": {}, 594 | "output_type": "execute_result" 595 | } 596 | ], 597 | "source": [ 598 | "# NOTE `;` for keyword argument\n", 599 | "function simulate(ex::Exog, x0; capT::Int=10)\n", 600 | " out = Array(typeof(x0), capT)\n", 601 | " out[1] = x0\n", 602 | "\n", 603 | " for t = 2:capT\n", 604 | " out[t] = iter(ex, out[t-1])\n", 605 | " end\n", 606 | " out\n", 607 | "end\n", 608 | "\n", 609 | "# for MarkovChain we have more info, so we don't need to give it an x0\n", 610 | "# define another method that hands off to the method above\n", 611 | "function simulate(mc::MarkovChain; capT::Int=100)\n", 612 | " v = mc.vals[searchsortedfirst(cumsum(mc.x0), rand())]\n", 613 | " simulate(mc, v; capT=capT)\n", 614 | "end" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": 70, 620 | "metadata": { 621 | "collapsed": false, 622 | "slideshow": { 623 | "slide_type": "subslide" 624 | } 625 | }, 626 | "outputs": [ 627 | { 628 | "data": { 629 | "text/plain": [ 630 | "10-element Array{Float64,1}:\n", 631 | " 0.5 \n", 632 | " 0.415777\n", 633 | " 0.364054\n", 634 | " 0.320674\n", 635 | " 0.300484\n", 636 | " 0.456054\n", 637 | " 0.316475\n", 638 | " 0.303914\n", 639 | " 0.35384 \n", 640 | " 0.282838" 641 | ] 642 | }, 643 | "execution_count": 70, 644 | "metadata": {}, 645 | "output_type": "execute_result" 646 | } 647 | ], 648 | "source": [ 649 | "ar1 = AR1(0.9, 0.1)\n", 650 | "simulate(ar1, 0.5)" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": 69, 656 | "metadata": { 657 | "collapsed": false, 658 | "slideshow": { 659 | "slide_type": "fragment" 660 | } 661 | }, 662 | "outputs": [ 663 | { 664 | "data": { 665 | "text/plain": [ 666 | "5-element Array{Float64,1}:\n", 667 | " 0.5\n", 668 | " 0.5\n", 669 | " 0.5\n", 670 | " 0.9\n", 671 | " 0.5" 672 | ] 673 | }, 674 | "execution_count": 69, 675 | "metadata": {}, 676 | "output_type": "execute_result" 677 | } 678 | ], 679 | "source": [ 680 | "mc = MarkovChain([0.7 0.3; 0.4 0.6], [0.5, 0.9], [0.2, 0.8])\n", 681 | "simulate(mc; capT=5)" 682 | ] 683 | } 684 | ], 685 | "metadata": { 686 | "kernelspec": { 687 | "display_name": "Julia 0.4.2", 688 | "language": "julia", 689 | "name": "julia-0.4" 690 | }, 691 | "language_info": { 692 | "file_extension": ".jl", 693 | "mimetype": "application/julia", 694 | "name": "julia", 695 | "version": "0.4.2" 696 | }, 697 | "livereveal": { 698 | "scroll": true, 699 | "start_slideshow_at": "selected", 700 | "theme": "white", 701 | "transition": "fade" 702 | } 703 | }, 704 | "nbformat": 4, 705 | "nbformat_minor": 0 706 | } 707 | -------------------------------------------------------------------------------- /lecture6/ParallelJulia.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "## Parallel programming in Julia\n", 12 | "\n", 13 | "**Chase Coleman & Spencer Lyon**\n", 14 | "\n", 15 | "3-4-16" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "slideshow": { 22 | "slide_type": "slide" 23 | } 24 | }, 25 | "source": [ 26 | "## Basics\n", 27 | "\n", 28 | "Julia has built in support for parallel programming\n", 29 | "\n", 30 | "To add more computing processes use the `addprocs` function" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": { 37 | "collapsed": false, 38 | "scrolled": true, 39 | "slideshow": { 40 | "slide_type": "fragment" 41 | } 42 | }, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "4-element Array{Int64,1}:\n", 48 | " 2\n", 49 | " 3\n", 50 | " 4\n", 51 | " 5" 52 | ] 53 | }, 54 | "execution_count": 2, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "addprocs(4)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": { 66 | "slideshow": { 67 | "slide_type": "fragment" 68 | } 69 | }, 70 | "source": [ 71 | "Each process has a unique `id` (integer)\n", 72 | "\n", 73 | "You can see that we added processes with id from 2 to 5" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": { 79 | "slideshow": { 80 | "slide_type": "fragment" 81 | } 82 | }, 83 | "source": [ 84 | "You can also add processes on remote machines.\n", 85 | "\n", 86 | "See the docstring for `addprocs` for more info" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": { 92 | "slideshow": { 93 | "slide_type": "subslide" 94 | } 95 | }, 96 | "source": [ 97 | "To get the number of active processes use the `nprocs` function " 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 3, 103 | "metadata": { 104 | "collapsed": false, 105 | "slideshow": { 106 | "slide_type": "fragment" 107 | } 108 | }, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "5" 114 | ] 115 | }, 116 | "execution_count": 3, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "nprocs()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": { 128 | "slideshow": { 129 | "slide_type": "subslide" 130 | } 131 | }, 132 | "source": [ 133 | "When you have `n` active processes, typically `n-1` will be used for computation\n", 134 | "\n", 135 | "The first process (with id 1) is used to direct the computation\n", 136 | "\n", 137 | "Other processes are called workers:\n" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 4, 143 | "metadata": { 144 | "collapsed": false, 145 | "slideshow": { 146 | "slide_type": "fragment" 147 | } 148 | }, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "4-element Array{Int64,1}:\n", 154 | " 2\n", 155 | " 3\n", 156 | " 4\n", 157 | " 5" 158 | ] 159 | }, 160 | "execution_count": 4, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "workers()" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": { 172 | "slideshow": { 173 | "slide_type": "slide" 174 | } 175 | }, 176 | "source": [ 177 | "## `pmap`\n", 178 | "\n", 179 | "\n", 180 | "One of the easiest ways to get started parallel programming in Julia is the `pmap` function\n", 181 | "\n", 182 | "In its simplest form `pmap` takes two arguments: a function and a collection (array or tuple)\n", 183 | "\n", 184 | "The function is applied in parallel to each item in the collection" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 7, 190 | "metadata": { 191 | "collapsed": false, 192 | "slideshow": { 193 | "slide_type": "fragment" 194 | } 195 | }, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | " 0.201885 seconds (98 allocations: 21.513 MB, 1.02% gc time)\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "# TODO find a more compelling econ example\n", 207 | "\n", 208 | "args = (rand(200, 200), rand(400, 400), rand(200, 200), rand(400, 400))\n", 209 | "\n", 210 | "# first a serial version\n", 211 | "@time for X in args\n", 212 | " svd(X)\n", 213 | "end" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 8, 219 | "metadata": { 220 | "collapsed": false, 221 | "slideshow": { 222 | "slide_type": "fragment" 223 | } 224 | }, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | " 0.105647 seconds (1.28 k allocations: 6.189 MB)\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "# now in parallel\n", 236 | "@time pmap(svd, args);" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": { 242 | "slideshow": { 243 | "slide_type": "subslide" 244 | } 245 | }, 246 | "source": [ 247 | "We have 4 workers and had 4 arrays, why didn't get get a 4x speedup?" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": { 253 | "slideshow": { 254 | "slide_type": "fragment" 255 | } 256 | }, 257 | "source": [ 258 | "Notice that 2 arrays were `200x200` and two were `400x400` (computational load is _unbalanced_)\n", 259 | "\n", 260 | "Julia gave each worker one array, but some had less work than others so they finished first" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": { 266 | "slideshow": { 267 | "slide_type": "fragment" 268 | } 269 | }, 270 | "source": [ 271 | "This means some processes were inactive during the total computation time\n", 272 | "\n", 273 | "Also, there is (small) overhead in passing data to the worker and passing the result back to process 1" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": { 279 | "slideshow": { 280 | "slide_type": "slide" 281 | } 282 | }, 283 | "source": [ 284 | "## `@parallel` loops\n", 285 | "\n", 286 | "Julia also has the ability to make a for loop run in parallel\n", 287 | "\n", 288 | "To do this use the `@parallel` macro" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": { 294 | "slideshow": { 295 | "slide_type": "fragment" 296 | } 297 | }, 298 | "source": [ 299 | "> There are subtleties to using `@parallel` \n", 300 | ">\n", 301 | "> We will cover only basic examples here\n", 302 | ">\n", 303 | "> Consult the [documentation](http://docs.julialang.org/en/latest/manual/parallel-computing/#parallel-map-and-loops) for more information" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": { 309 | "slideshow": { 310 | "slide_type": "subslide" 311 | } 312 | }, 313 | "source": [ 314 | "There are two possible syntaxes for `@parallel`:" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": { 320 | "slideshow": { 321 | "slide_type": "fragment" 322 | } 323 | }, 324 | "source": [ 325 | "The first is\n", 326 | "\n", 327 | "```julia\n", 328 | "@parallel for ...\n", 329 | "end\n", 330 | "```\n", 331 | "\n", 332 | "and simply executes the body of the for loop in parallel" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": { 338 | "slideshow": { 339 | "slide_type": "fragment" 340 | } 341 | }, 342 | "source": [ 343 | "The second is\n", 344 | "\n", 345 | "```julia\n", 346 | "@parallel (f) for ...\n", 347 | "end\n", 348 | "```\n", 349 | "\n", 350 | "It the same, but also applies a reduction function `f` to the result of each iteration\n", 351 | "\n", 352 | "The result of each iteration is the result of the last statement in the loop's body\n", 353 | "\n", 354 | "The function `f` should take two elements and output one" 355 | ] 356 | }, 357 | { 358 | "cell_type": "markdown", 359 | "metadata": { 360 | "slideshow": { 361 | "slide_type": "subslide" 362 | } 363 | }, 364 | "source": [ 365 | "Let's see this in practice" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 10, 371 | "metadata": { 372 | "collapsed": false, 373 | "slideshow": { 374 | "slide_type": "fragment" 375 | } 376 | }, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "\tFrom worker 3:\trandn() = -1.4823226833572487\n", 383 | "\tFrom worker 3:\trandn() = 0.008485042894568029\n", 384 | "\tFrom worker 2:\trandn() = -1.4372949820146432\n", 385 | "\tFrom worker 2:\trandn() = 0.14678523736405727\n", 386 | "\tFrom worker 2:\trandn() = -1.293961883026427\n", 387 | "\tFrom worker 5:\trandn() = -0.39493431470157886\n", 388 | "\tFrom worker 5:\trandn() = 0.2494670221840125\n", 389 | "\tFrom worker 4:\trandn() = -0.6202425665423612\n", 390 | "\tFrom worker 4:\trandn() = -1.853569956218026\n", 391 | "\tFrom worker 3:\trandn() = 1.5581361796835485\n" 392 | ] 393 | } 394 | ], 395 | "source": [ 396 | "@parallel for i = 1:10\n", 397 | " @show randn()\n", 398 | "end;" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 21, 404 | "metadata": { 405 | "collapsed": false, 406 | "slideshow": { 407 | "slide_type": "fragment" 408 | } 409 | }, 410 | "outputs": [ 411 | { 412 | "name": "stdout", 413 | "output_type": "stream", 414 | "text": [ 415 | "\tFrom worker 2:\trandn() = -1.1271781195591901\n", 416 | "\tFrom worker 2:\trandn() = 0.9025559215992444\n", 417 | "\tFrom worker 3:\trandn() = -0.912358216201542\n", 418 | "\tFrom worker 2:\trandn() = 0.009225014074008112\n", 419 | "\tFrom worker 3:\trandn() = 0.8497725800879736\n", 420 | "\tFrom worker 3:\trandn() = -1.005123841699878\n", 421 | "\tFrom worker 5:\trandn() = -0.3685509736298961\n", 422 | "\tFrom worker 5:\trandn() = -1.637375058113114\n", 423 | "\tFrom worker 4:\trandn() = -1.8505401231390373\n", 424 | "\tFrom worker 4:\trandn() = -0.7754764372191437\n", 425 | "-5.915049253800575\n" 426 | ] 427 | } 428 | ], 429 | "source": [ 430 | "# now with a (+)\n", 431 | "total = @parallel (+) for i=1:10\n", 432 | " @show randn()\n", 433 | "end\n", 434 | "println(total)" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": { 440 | "slideshow": { 441 | "slide_type": "subslide" 442 | } 443 | }, 444 | "source": [ 445 | "One issue with `@parallel` is that all variables used in the loop are copied to the working process, but not copied back to process 1\n", 446 | "\n", 447 | "That means code like this will not work as you might expect:" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 22, 453 | "metadata": { 454 | "collapsed": false, 455 | "slideshow": { 456 | "slide_type": "fragment" 457 | } 458 | }, 459 | "outputs": [ 460 | { 461 | "data": { 462 | "text/plain": [ 463 | "10-element Array{Float64,1}:\n", 464 | " 0.0\n", 465 | " 0.0\n", 466 | " 0.0\n", 467 | " 0.0\n", 468 | " 0.0\n", 469 | " 0.0\n", 470 | " 0.0\n", 471 | " 0.0\n", 472 | " 0.0\n", 473 | " 0.0" 474 | ] 475 | }, 476 | "execution_count": 22, 477 | "metadata": {}, 478 | "output_type": "execute_result" 479 | } 480 | ], 481 | "source": [ 482 | "a = zeros(10)\n", 483 | "@parallel for i=1:10\n", 484 | " a[i] = i\n", 485 | "end\n", 486 | "a" 487 | ] 488 | }, 489 | { 490 | "cell_type": "markdown", 491 | "metadata": { 492 | "slideshow": { 493 | "slide_type": "fragment" 494 | } 495 | }, 496 | "source": [ 497 | "For that to work we need an array that provides shared memory access across processes..." 498 | ] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "metadata": { 503 | "slideshow": { 504 | "slide_type": "slide" 505 | } 506 | }, 507 | "source": [ 508 | "## `SharedArray`\n", 509 | "\n", 510 | "A `SharedArray` is an array whose memory can be shared across all processes on the same machine" 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": { 516 | "slideshow": { 517 | "slide_type": "fragment" 518 | } 519 | }, 520 | "source": [ 521 | "This comes with a number of benefits over `Array` for parallel computing.\n", 522 | "\n", 523 | "Some of them are:\n", 524 | "\n", 525 | "- Save on the overhead of passing arrays to worker processes\n", 526 | "- Update arrays in a predictable way from `@parallel` loops" 527 | ] 528 | }, 529 | { 530 | "cell_type": "markdown", 531 | "metadata": { 532 | "slideshow": { 533 | "slide_type": "subslide" 534 | } 535 | }, 536 | "source": [ 537 | "Let's see an example" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 30, 543 | "metadata": { 544 | "collapsed": false 545 | }, 546 | "outputs": [ 547 | { 548 | "name": "stdout", 549 | "output_type": "stream", 550 | "text": [ 551 | "[0,0,0,0,0,0,0,0,0,0,0]\n" 552 | ] 553 | } 554 | ], 555 | "source": [ 556 | "a = SharedArray(Int, 1000)\n", 557 | "@parallel for i in eachindex(a)\n", 558 | " a[i] = i\n", 559 | "end\n", 560 | "println(a[end-10:end])" 561 | ] 562 | }, 563 | { 564 | "cell_type": "markdown", 565 | "metadata": { 566 | "slideshow": { 567 | "slide_type": "fragment" 568 | } 569 | }, 570 | "source": [ 571 | "I know what you're thinking \"wait, you told me that this example should work\"" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": { 577 | "slideshow": { 578 | "slide_type": "fragment" 579 | } 580 | }, 581 | "source": [ 582 | "Well it did..." 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "execution_count": 31, 588 | "metadata": { 589 | "collapsed": false 590 | }, 591 | "outputs": [ 592 | { 593 | "name": "stdout", 594 | "output_type": "stream", 595 | "text": [ 596 | "[990,991,992,993,994,995,996,997,998,999,1000]\n" 597 | ] 598 | } 599 | ], 600 | "source": [ 601 | "println(a[end-10:end])" 602 | ] 603 | }, 604 | { 605 | "cell_type": "markdown", 606 | "metadata": { 607 | "slideshow": { 608 | "slide_type": "fragment" 609 | } 610 | }, 611 | "source": [ 612 | "... but there's a caveat" 613 | ] 614 | }, 615 | { 616 | "cell_type": "markdown", 617 | "metadata": { 618 | "slideshow": { 619 | "slide_type": "subslide" 620 | } 621 | }, 622 | "source": [ 623 | "An `@parallel` loop with a `SharedArray` will run _asynchronously_\n", 624 | "\n", 625 | "This means the instructions will be sent to the workers, and then the main process will just continue without waiting for workers to finish" 626 | ] 627 | }, 628 | { 629 | "cell_type": "markdown", 630 | "metadata": { 631 | "slideshow": { 632 | "slide_type": "fragment" 633 | } 634 | }, 635 | "source": [ 636 | "To fix this problem we need to tell Julia to `@sync` all computations that happen in the loop:" 637 | ] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": 32, 642 | "metadata": { 643 | "collapsed": false 644 | }, 645 | "outputs": [ 646 | { 647 | "name": "stdout", 648 | "output_type": "stream", 649 | "text": [ 650 | "[990,991,992,993,994,995,996,997,998,999,1000]\n" 651 | ] 652 | } 653 | ], 654 | "source": [ 655 | "b = SharedArray(Int, 1000)\n", 656 | "@sync @parallel for i in eachindex(b)\n", 657 | " b[i] = i\n", 658 | "end\n", 659 | "println(b[end-10:end])" 660 | ] 661 | }, 662 | { 663 | "cell_type": "markdown", 664 | "metadata": { 665 | "slideshow": { 666 | "slide_type": "fragment" 667 | } 668 | }, 669 | "source": [ 670 | "Because `SharedArray` data is available to multiple processes, you need to be careful about how and when it is accessed\n", 671 | "\n", 672 | "For more details see the [`SharedArray` docs](http://docs.julialang.org/en/latest/manual/parallel-computing/#id2)" 673 | ] 674 | }, 675 | { 676 | "cell_type": "markdown", 677 | "metadata": { 678 | "slideshow": { 679 | "slide_type": "slide" 680 | } 681 | }, 682 | "source": [ 683 | "## More info\n", 684 | "\n", 685 | "We've only scratched the surface of Julia's parallel computing capabilities. \n", 686 | "\n", 687 | "For more information see these references:\n", 688 | "\n", 689 | "- The offical [documentation](http://docs.julialang.org/en/latest/manual/parallel-computing/) on parallel computing\n", 690 | "- [Parallel Julia](https://github.com/JuliaParallel) github organization\n", 691 | "- Other packages:\n", 692 | " - [MPI.jl](https://github.com/JuliaParallel/MPI.jl)\n", 693 | " - [ParallelAccelerator.jl](https://github.com/IntelLabs/ParallelAccelerator.jl)\n", 694 | " - [DistributedArrays.jl](https://github.com/JuliaParallel/DistributedArrays.jl)" 695 | ] 696 | } 697 | ], 698 | "metadata": { 699 | "celltoolbar": "Slideshow", 700 | "kernelspec": { 701 | "display_name": "Julia 0.4.3-pre", 702 | "language": "julia", 703 | "name": "julia-0.4" 704 | }, 705 | "language_info": { 706 | "file_extension": ".jl", 707 | "mimetype": "application/julia", 708 | "name": "julia", 709 | "version": "0.4.3" 710 | }, 711 | "livereveal": { 712 | "scroll": true, 713 | "start_slideshow_at": "selected", 714 | "theme": "white", 715 | "transition": "fade" 716 | } 717 | }, 718 | "nbformat": 4, 719 | "nbformat_minor": 0 720 | } 721 | -------------------------------------------------------------------------------- /lecture6/install_julia: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | wget -O julia_binary.tar.gz https://julialang.s3.amazonaws.com/bin/linux/x64/0.4/julia-0.4-latest-linux-x86_64.tar.gz 4 | 5 | mkdir -p $HOME/src 6 | 7 | rm -rf $HOME/src/julia* 8 | mkdir -p $HOME/src/julia 9 | tar -C $HOME/src/julia -zxf julia_binary.tar.gz --strip-components=1 10 | rm julia_binary.tar.gz 11 | 12 | # prepend julia to path 13 | echo "Adding julia to path" 14 | echo "export PATH=$HOME/src/julia/bin:$PATH" >> $HOME/.bashrc 15 | -------------------------------------------------------------------------------- /lecture7/Intro_to_pymc_pres.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture7/Intro_to_pymc_pres.pdf -------------------------------------------------------------------------------- /lecture7/background_linalg.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture7/background_linalg.pdf -------------------------------------------------------------------------------- /lecture7/lecture7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture7/lecture7.pdf -------------------------------------------------------------------------------- /lecture8/analysis_intermediate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture8/analysis_intermediate.pdf -------------------------------------------------------------------------------- /lecture8/analysis_introductory.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture8/analysis_introductory.pdf -------------------------------------------------------------------------------- /lecture8/lecture8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture8/lecture8.pdf -------------------------------------------------------------------------------- /lecture8/presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture8/presentation.pdf -------------------------------------------------------------------------------- /lecture9/lecture9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture9/lecture9.pdf -------------------------------------------------------------------------------- /other_PDFS/benhabib_wealth_distribution.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/other_PDFS/benhabib_wealth_distribution.pdf -------------------------------------------------------------------------------- /other_PDFS/lp_spaces.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/other_PDFS/lp_spaces.pdf --------------------------------------------------------------------------------