├── .gitignore ├── intro ├── slides.pdf ├── fastloop_examples │ ├── makefile │ ├── ar1_sample_mean.jl │ └── ar1_sample_mean.c ├── languages.jpeg ├── .gitignore ├── languages_shifted.jpeg ├── hello.ipynb └── slides.tex ├── dsgejl ├── .gitignore ├── output_data │ └── an_schorfheide │ │ └── ss0 │ │ └── estimate │ │ └── raw │ │ └── paramsmode_vint=170528.h5 ├── util.jl ├── input_data │ ├── raw │ │ ├── population_data_levels_170528.csv │ │ └── fred_170528.csv │ └── data │ │ └── data_dsid=00_vint=170528.csv └── ma1.jl ├── README.md ├── packages ├── index.ipynb ├── optimization_solvers.ipynb ├── numerics_stats.ipynb └── economics.ipynb ├── state_space_models ├── us.txt └── state_space_routines.ipynb └── syntax └── julia_language.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_STORE 2 | .ipynb_checkpoints -------------------------------------------------------------------------------- /intro/slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FRBNY-DSGE/IADB_2017_Workshop/HEAD/intro/slides.pdf -------------------------------------------------------------------------------- /dsgejl/.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | output_data/an_schorfheide/ss0/forecast 3 | output_data/ma1 -------------------------------------------------------------------------------- /intro/fastloop_examples/makefile: -------------------------------------------------------------------------------- 1 | maketest: 2 | gcc -Wall ar1_sample_mean.c -lgsl -lgslcblas -lm -o foo 3 | -------------------------------------------------------------------------------- /intro/languages.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FRBNY-DSGE/IADB_2017_Workshop/HEAD/intro/languages.jpeg -------------------------------------------------------------------------------- /intro/.gitignore: -------------------------------------------------------------------------------- 1 | *.aux 2 | *.log 3 | *.nav 4 | *.out 5 | *.snm 6 | *.toc 7 | *.vrb 8 | 9 | fastloop_examples/foo -------------------------------------------------------------------------------- /intro/languages_shifted.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FRBNY-DSGE/IADB_2017_Workshop/HEAD/intro/languages_shifted.jpeg -------------------------------------------------------------------------------- /dsgejl/output_data/an_schorfheide/ss0/estimate/raw/paramsmode_vint=170528.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FRBNY-DSGE/IADB_2017_Workshop/HEAD/dsgejl/output_data/an_schorfheide/ss0/estimate/raw/paramsmode_vint=170528.h5 -------------------------------------------------------------------------------- /intro/fastloop_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 = 1e7 12 | beta = 1.0 13 | alpha = 0.9 14 | s = 1.0 15 | @time result = ar1_sample_mean(N, beta, alpha, s) 16 | println("mean = $result") 17 | -------------------------------------------------------------------------------- /dsgejl/util.jl: -------------------------------------------------------------------------------- 1 | function plot_actual!(p::Plots.Subplot{Plots.GRBackend}, v::Float64) 2 | # Get y-axis limits 3 | ylim = p.attr[:yaxis].d[:lims] 4 | 5 | # Plot actual value 6 | plot!(p, [v, v], [ylim[1], ylim[2]], linewidth = 3, label = "", color = :black) 7 | end 8 | 9 | function plot_actual!(p::Plots.Subplot{Plots.PlotlyBackend}, v::Float64) 10 | # Get y-axis limits 11 | ylim = p.attr[:yaxis].d[:extrema] 12 | 13 | # Plot actual value 14 | plot!(p, [v, v], [ylim.emin, ylim.emax], linewidth = 3, label = "", color = :black) 15 | end 16 | -------------------------------------------------------------------------------- /intro/hello.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# A First Julia Program" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "First program: always Hello World!" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "Hello world\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "println(\"Hello world\")" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "Okay, done! On to learning syntax." 39 | ] 40 | } 41 | ], 42 | "metadata": { 43 | "anaconda-cloud": {}, 44 | "kernelspec": { 45 | "display_name": "Julia 0.6.1", 46 | "language": "julia", 47 | "name": "julia-0.6" 48 | }, 49 | "language_info": { 50 | "file_extension": ".jl", 51 | "mimetype": "application/julia", 52 | "name": "julia", 53 | "version": "0.6.1" 54 | } 55 | }, 56 | "nbformat": 4, 57 | "nbformat_minor": 1 58 | } 59 | -------------------------------------------------------------------------------- /intro/fastloop_examples/ar1_sample_mean.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | double ar1_ts_unpacked (int n, double beta, double alpha, double s, gsl_rng * r) 8 | { 9 | 10 | int i; 11 | double x = beta / (1 - alpha); // Start at mean of stationary dist 12 | double sum = 0; 13 | for (i = 1; i <= n; i++) { 14 | sum += x; 15 | x = beta + alpha * x + gsl_ran_gaussian(r, s); 16 | } 17 | 18 | return sum / n; 19 | } 20 | 21 | int main(void) 22 | { 23 | clock_t start, end; 24 | double cpu_time_used; 25 | 26 | int N = 1e7; 27 | double beta = 1.0; 28 | double alpha = 0.9; 29 | double s = 1; 30 | double sample_mean; 31 | 32 | /* create a generator via GSL_RNG_TYPE */ 33 | const gsl_rng_type * T; 34 | gsl_rng * r; 35 | gsl_rng_env_setup(); 36 | T = gsl_rng_default; 37 | r = gsl_rng_alloc(T); 38 | gsl_rng_set(r, 1); 39 | 40 | start = clock(); 41 | sample_mean = ar1_ts_unpacked(N, beta, alpha, s, r); 42 | end = clock(); 43 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; 44 | 45 | gsl_rng_free (r); 46 | 47 | printf("time elapsed = %g seconds\n", cpu_time_used); 48 | printf("mean = %g\n", sample_mean); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Macroeconomic Modeling with Julia 2 | 3 | Code and teaching material for "Macroeconomic Modeling with Julia", a workshop given for the IADB at the Central Bank of Argentina in December 2017. 4 | 5 | ## Tentative Outline 6 | 7 | - December 18, 2:00 - 2:30 pm: Introduction to Julia 8 | - December 18, 2:45 - 3:45 pm: Syntax 9 | - December 18, 4:00 - 5:30 pm: Types and multiple dispatch 10 | - December 19, 9:00 - 9:30 am: Package ecosystem 11 | - December 19, 9:45 - 11:00 am: State-space models and DSGE.jl 12 | 13 | ## Software 14 | 15 | We assume that participants will have access to 16 | 17 | * [Julia v0.6.1](https://julialang.org/downloads/) 18 | * [IJulia](https://github.com/JuliaLang/IJulia.jl) and [Jupyter notebooks](https://jupyter.org/): type `Pkg.add("IJulia")` in the Julia REPL 19 | - Optional: [RISE](https://github.com/damianavila/RISE), a Jupyter extension which lets you convert notebooks (which are already in slide format) to slides within the browser and run code blocks in the slides. After installing IJulia, run in the Julia REPL: 20 | ```julia 21 | using Conda 22 | Conda.add_channel("damianavila82") 23 | Conda.add("rise") 24 | ``` 25 | * [DSGE](https://github.com/FRBNY-DSGE/DSGE.jl): type `Pkg.add("DSGE")` in the Julia REPL 26 | 27 | ## Resources 28 | 29 | * [Cheat sheets](http://cheatsheets.quantecon.org/) for MATLAB and Python users 30 | * [Lectures](http://lectures.quantecon.org/) for learning Julia, or for economics concepts 31 | * [Discourse forum](http://discourse.julialang.org/) for Julia questions 32 | 33 | ## Contact 34 | 35 | * Abhi Gupta: abhi.gupta@ny.frb.org 36 | * Pearl Li: pearl.li@ny.frb.org 37 | 38 | Disclaimer 39 | ------ 40 | Copyright Federal Reserve Bank of New York. You may reproduce, use, modify, make derivative works of, and distribute and this code in whole or in part so long as you keep this notice in the documentation associated with any distributed works. Neither the name of the Federal Reserve Bank of New York (FRBNY) nor the names of any of the authors may be used to endorse or promote works derived from this code without prior written permission. Portions of the code attributed to third parties are subject to applicable third party licenses and rights. By your use of this code you accept this license and any applicable third party license. 41 | 42 | THIS CODE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT ANY WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, EXCEPT TO THE EXTENT THAT THESE DISCLAIMERS ARE HELD TO BE LEGALLY INVALID. FRBNY IS NOT, UNDER ANY CIRCUMSTANCES, LIABLE TO YOU FOR DAMAGES OF ANY KIND ARISING OUT OF OR IN CONNECTION WITH USE OF OR INABILITY TO USE THE CODE, INCLUDING, BUT NOT LIMITED TO DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, PUNITIVE, SPECIAL OR EXEMPLARY DAMAGES, WHETHER BASED ON BREACH OF CONTRACT, BREACH OF WARRANTY, TORT OR OTHER LEGAL OR EQUITABLE THEORY, EVEN IF FRBNY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR LOSS AND REGARDLESS OF WHETHER SUCH DAMAGES OR LOSS IS FORESEEABLE. 43 | -------------------------------------------------------------------------------- /packages/index.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Packages in Julia\n", 12 | "\n", 13 | "Based on a notebook by Chase Coleman and Spencer Lyon and on material from QuantEcon\n", 14 | "\n", 15 | "18 December 2017" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "slideshow": { 22 | "slide_type": "fragment" 23 | } 24 | }, 25 | "source": [ 26 | "- Key strength of Julia is its [package ecosystem](https://pkg.julialang.org)\n", 27 | "- 1,446 packages as of 6-26-17\n", 28 | "- Great diversity:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 5, 34 | "metadata": { 35 | "scrolled": true, 36 | "slideshow": { 37 | "slide_type": "fragment" 38 | } 39 | }, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "10-element Array{String,1}:\n", 45 | " \"OdsIO\" \n", 46 | " \"LSODA\" \n", 47 | " \"GUITestRunner\" \n", 48 | " \"SimJulia\" \n", 49 | " \"CrossDecomposition\"\n", 50 | " \"Reel\" \n", 51 | " \"Boltzmann\" \n", 52 | " \"uCSV\" \n", 53 | " \"Shapefile\" \n", 54 | " \"ShowSet\" " 55 | ] 56 | }, 57 | "execution_count": 5, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "pkg_list = readdir(joinpath(expanduser(\"~\"), \".julia\", \"v0.6\", \"METADATA\"))\n", 64 | "pkg_list[rand(1:length(pkg_list), 10)]" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": { 70 | "slideshow": { 71 | "slide_type": "slide" 72 | } 73 | }, 74 | "source": [ 75 | "- Some are very high quality (JuMP, NLopt, Distributions, QuantEcon, DSGE, TensorFlow, Cxx)\n", 76 | "- The goal of these notebooks is to introduce the most useful packages for economic modeling\n", 77 | "- Will break down by topic:\n", 78 | " - [Function approximation](function_approximation.ipynb)\n", 79 | " - [Numerical/statistics tools](numerics_stats.ipynb)\n", 80 | " - [Optimization/solvers](optimization_solvers.ipynb)\n", 81 | " - [Plotting](plotting.ipynb)\n", 82 | " - [Economics](economics.ipynb)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": { 89 | "collapsed": true 90 | }, 91 | "outputs": [], 92 | "source": [] 93 | } 94 | ], 95 | "metadata": { 96 | "anaconda-cloud": {}, 97 | "kernelspec": { 98 | "display_name": "Julia 0.6.1", 99 | "language": "julia", 100 | "name": "julia-0.6" 101 | }, 102 | "language_info": { 103 | "file_extension": ".jl", 104 | "mimetype": "application/julia", 105 | "name": "julia", 106 | "version": "0.6.1" 107 | }, 108 | "livereveal": { 109 | "scroll": true, 110 | "start_slideshow_at": "selected", 111 | "theme": "white", 112 | "transition": "fade" 113 | } 114 | }, 115 | "nbformat": 4, 116 | "nbformat_minor": 2 117 | } 118 | -------------------------------------------------------------------------------- /state_space_models/us.txt: -------------------------------------------------------------------------------- 1 | 0.99621900 0.27220144 8.6533333 2 | 1.9871645 4.5993067 8.8033333 3 | 1.7135544 3.8815764 9.4600000 4 | 1.7873392 3.9761759 9.4300000 5 | 1.7060490 5.6311292 9.6866667 6 | 1.4702848 3.7534655 10.556667 7 | 0.73189190 3.4632251 11.390000 8 | 0.58340721 3.4334975 9.2666667 9 | 0.69288335 3.6552956 8.4766667 10 | 0.61313503 3.6221949 7.9233333 11 | 1.3124225 2.4790907 7.9000000 12 | 0.52293452 4.0572040 8.1033333 13 | 0.70073514 2.0741239 7.8266667 14 | 0.15289176 -1.9518185 6.9200000 15 | 0.71051201 2.4382886 6.2066667 16 | 0.26062387 2.7857796 6.2666667 17 | 0.39461500 4.7990978 6.2200000 18 | 0.86077046 4.5064218 6.6500000 19 | 0.67919479 4.2229131 6.8433333 20 | 1.5151159 3.7166353 6.9166667 21 | 0.27564144 3.1092714 6.6633333 22 | 1.0661438 4.5623534 7.1566667 23 | 0.34407705 4.8471778 7.9833333 24 | 1.1340883 4.3460519 8.4700000 25 | 0.88065851 4.5185768 9.4433333 26 | 0.48041228 6.4140524 9.7266667 27 | 0.53232665 3.1153642 9.0833333 28 | 0.069165999 4.0458157 8.6133333 29 | 0.99418756 6.8270004 8.2500000 30 | 0.046254271 3.9378556 8.2433333 31 | -0.22121931 6.8500077 8.1600000 32 | -1.0059852 6.7346735 7.7433333 33 | -0.82618933 2.9791597 6.4266667 34 | 0.38084382 2.3674545 5.8633333 35 | 0.21770641 3.0373692 5.6433333 36 | 0.20385677 3.3050006 4.8166667 37 | 0.78189158 2.7014092 4.0233333 38 | 0.69177854 3.0651491 3.7700000 39 | 0.70313023 3.0418398 3.2566667 40 | 0.81814394 3.4885312 3.0366667 41 | -0.17043963 2.8995801 3.0400000 42 | 0.22221029 2.8787124 3.0000000 43 | 0.22781192 1.8462991 3.0600000 44 | 1.0516122 3.3020137 2.9900000 45 | 0.72996673 2.0045600 3.2133333 46 | 1.0070986 2.2657815 3.9400000 47 | 0.26724988 3.6883203 4.4866667 48 | 0.87204111 2.3214351 5.1666667 49 | -0.0020987643 2.9271668 5.8100000 50 | -0.12771629 3.2567019 6.0200000 51 | 0.49609578 2.0111530 5.7966667 52 | 0.40412811 2.1746275 5.7200000 53 | 0.36264185 3.5409926 5.3633333 54 | 1.2856896 3.4246785 5.2433333 55 | 0.48831544 2.2951928 5.3066667 56 | 0.80947489 3.4603022 5.2800000 57 | 0.39919707 2.4295812 5.2766667 58 | 1.1487125 0.91771862 5.5233333 59 | 0.87575002 1.9950166 5.5333333 60 | 0.37178218 2.1500982 5.5066667 61 | 0.73069627 0.82389319 5.5200000 62 | 0.29584929 1.3147095 5.5000000 63 | 0.78232589 2.0456217 5.5333333 64 | 1.1460726 1.8727770 4.8600000 65 | 0.46577486 1.4595597 4.7333333 66 | 0.47152203 2.9835840 4.7466667 67 | 0.81669806 2.9614943 5.0933333 68 | 1.4255869 2.9397292 5.3066667 69 | -0.050205261 3.8601974 5.6766667 70 | 1.2290210 3.2015788 6.2733333 71 | -0.44534490 3.6388522 6.5200000 72 | 0.18651993 2.8415210 6.4733333 73 | -0.46415573 3.6564719 5.5933333 74 | -0.025224449 3.1722221 4.3266667 75 | -0.68281419 0.97680146 3.4966667 76 | 0.065220213 -0.52567359 2.1333333 77 | 0.50718605 1.2754395 1.7333333 78 | 0.26544415 3.4309366 1.7500000 79 | 0.32039959 2.2218165 1.7400000 80 | -0.13384788 1.9156419 1.4433333 81 | -------------------------------------------------------------------------------- /dsgejl/input_data/raw/population_data_levels_170528.csv: -------------------------------------------------------------------------------- 1 | "date","CNP16OV" 2 | "1959-03-31",114714.0 3 | "1959-06-30",115139.0 4 | "1959-09-30",115551.0 5 | "1959-12-31",115918.0 6 | "1960-03-31",116708.0 7 | "1960-06-30",117037.0 8 | "1960-09-30",117411.0 9 | "1960-12-31",117824.0 10 | "1961-03-31",118254.0 11 | "1961-06-30",118636.0 12 | "1961-09-30",119001.0 13 | "1961-12-31",119190.0 14 | "1962-03-31",119379.0 15 | "1962-06-30",119819.0 16 | "1962-09-30",120368.0 17 | "1962-12-31",121046.0 18 | "1963-03-31",121640.0 19 | "1963-06-30",122167.0 20 | "1963-09-30",122670.0 21 | "1963-12-31",123189.0 22 | "1964-03-31",123708.0 23 | "1964-06-30",124203.0 24 | "1964-09-30",124739.0 25 | "1964-12-31",125289.0 26 | "1965-03-31",125814.0 27 | "1965-06-30",126325.0 28 | "1965-09-30",126745.0 29 | "1965-12-31",127169.0 30 | "1966-03-31",127511.0 31 | "1966-06-30",127869.0 32 | "1966-09-30",128234.0 33 | "1966-12-31",128617.0 34 | "1967-03-31",129044.0 35 | "1967-06-30",129527.0 36 | "1967-09-30",130166.0 37 | "1967-12-31",130757.0 38 | "1968-03-31",131267.0 39 | "1968-06-30",131712.0 40 | "1968-09-30",132250.0 41 | "1968-12-31",132880.0 42 | "1969-03-31",133476.0 43 | "1969-06-30",134020.0 44 | "1969-09-30",134595.0 45 | "1969-12-31",135247.0 46 | "1970-03-31",135950.0 47 | "1970-06-30",136677.0 48 | "1970-09-30",137456.0 49 | "1970-12-31",138260.0 50 | "1971-03-31",139034.0 51 | "1971-06-30",139827.0 52 | "1971-09-30",140603.0 53 | "1971-12-31",141402.0 54 | "1972-03-31",143005.0 55 | "1972-06-30",143759.0 56 | "1972-09-30",144523.0 57 | "1972-12-31",145215.0 58 | "1973-03-31",145964.0 59 | "1973-06-30",146720.0 60 | "1973-09-30",147478.0 61 | "1973-12-31",148226.0 62 | "1974-03-31",148987.0 63 | "1974-06-30",149747.0 64 | "1974-09-30",150498.0 65 | "1974-12-31",151253.0 66 | "1975-03-31",151987.0 67 | "1975-06-30",152708.0 68 | "1975-09-30",153579.0 69 | "1975-12-31",154336.0 70 | "1976-03-31",155075.0 71 | "1976-06-30",155774.0 72 | "1976-09-30",156527.0 73 | "1976-12-31",157222.0 74 | "1977-03-31",157911.0 75 | "1977-06-30",158652.0 76 | "1977-09-30",159430.0 77 | "1977-12-31",160140.0 78 | "1978-03-31",160829.0 79 | "1978-06-30",161525.0 80 | "1978-09-30",162265.0 81 | "1978-12-31",163024.0 82 | "1979-03-31",163756.0 83 | "1979-06-30",164447.0 84 | "1979-09-30",165200.0 85 | "1979-12-31",166055.0 86 | "1980-03-31",166762.0 87 | "1980-06-30",167416.0 88 | "1980-09-30",168111.0 89 | "1980-12-31",168694.0 90 | "1981-03-31",169279.0 91 | "1981-06-30",169837.0 92 | "1981-09-30",170413.0 93 | "1981-12-31",170990.0 94 | "1982-03-31",171497.0 95 | "1982-06-30",172020.0 96 | "1982-09-30",172522.0 97 | "1982-12-31",173046.0 98 | "1983-03-31",173505.0 99 | "1983-06-30",173957.0 100 | "1983-09-30",174449.0 101 | "1983-12-31",174950.0 102 | "1984-03-31",175679.0 103 | "1984-06-30",176125.0 104 | "1984-09-30",176595.0 105 | "1984-12-31",177132.0 106 | "1985-03-31",177522.0 107 | "1985-06-30",177946.0 108 | "1985-09-30",178413.0 109 | "1985-12-31",178941.0 110 | "1986-03-31",179825.0 111 | "1986-06-30",180321.0 112 | "1986-09-30",180836.0 113 | "1986-12-31",181365.0 114 | "1987-03-31",182001.0 115 | "1987-06-30",182527.0 116 | "1987-09-30",183016.0 117 | "1987-12-31",183467.0 118 | "1988-03-31",183967.0 119 | "1988-06-30",184389.0 120 | "1988-09-30",184840.0 121 | "1988-12-31",185253.0 122 | "1989-03-31",185773.0 123 | "1989-06-30",186178.0 124 | "1989-09-30",186602.0 125 | "1989-12-31",187018.0 126 | "1990-03-31",188520.0 127 | "1990-06-30",188916.0 128 | "1990-09-30",189353.0 129 | "1990-12-31",189866.0 130 | "1991-03-31",190272.0 131 | "1991-06-30",190656.0 132 | "1991-09-30",191121.0 133 | "1991-12-31",191651.0 134 | "1992-03-31",192075.0 135 | "1992-06-30",192507.0 136 | "1992-09-30",193024.0 137 | "1992-12-31",193616.0 138 | "1993-03-31",194106.0 139 | "1993-06-30",194555.0 140 | "1993-09-30",195068.0 141 | "1993-12-31",195621.0 142 | "1994-03-31",196085.0 143 | "1994-06-30",196522.0 144 | "1994-09-30",197050.0 145 | "1994-12-31",197601.0 146 | "1995-03-31",197882.0 147 | "1995-06-30",198296.0 148 | "1995-09-30",198807.0 149 | "1995-12-31",199352.0 150 | "1996-03-31",199776.0 151 | "1996-06-30",200279.0 152 | "1996-09-30",200850.0 153 | "1996-12-31",201457.0 154 | "1997-03-31",202396.0 155 | "1997-06-30",202835.0 156 | "1997-09-30",203367.0 157 | "1997-12-31",203935.0 158 | "1998-03-31",204395.0 159 | "1998-06-30",204905.0 160 | "1998-09-30",205483.0 161 | "1998-12-31",206098.0 162 | "1999-03-31",206876.0 163 | "1999-06-30",207432.0 164 | "1999-09-30",208044.0 165 | "1999-12-31",208660.0 166 | "2000-03-31",211586.0 167 | "2000-06-30",212242.0 168 | "2000-09-30",212919.0 169 | "2000-12-31",213560.0 170 | "2001-03-31",214101.0 171 | "2001-06-30",214736.0 172 | "2001-09-30",215422.0 173 | "2001-12-31",216112.0 174 | "2002-03-31",216664.0 175 | "2002-06-30",217204.0 176 | "2002-09-30",217868.0 177 | "2002-12-31",218543.0 178 | "2003-03-31",220109.0 179 | "2003-06-30",220774.0 180 | "2003-09-30",221513.0 181 | "2003-12-31",222276.0 182 | "2004-03-31",222356.0 183 | "2004-06-30",222973.0 184 | "2004-09-30",223680.0 185 | "2004-12-31",224418.0 186 | "2005-03-31",225038.0 187 | "2005-06-30",225674.0 188 | "2005-09-30",226422.0 189 | "2005-12-31",227196.0 190 | "2006-03-31",227764.0 191 | "2006-06-30",228433.0 192 | "2006-09-30",229166.0 193 | "2006-12-31",229896.0 194 | "2007-03-31",230839.0 195 | "2007-06-30",231482.0 196 | "2007-09-30",232210.0 197 | "2007-12-31",232937.0 198 | "2008-03-31",232807.0 199 | "2008-06-30",233410.0 200 | "2008-09-30",234110.0 201 | "2008-12-31",234825.0 202 | "2009-03-31",234913.0 203 | "2009-06-30",235459.0 204 | "2009-09-30",236093.0 205 | "2009-12-31",236739.0 206 | "2010-03-31",236996.0 207 | "2010-06-30",237506.0 208 | "2010-09-30",238104.0 209 | "2010-12-31",238711.0 210 | "2011-03-31",238852.0 211 | "2011-06-30",239316.0 212 | "2011-09-30",239871.0 213 | "2011-12-31",240431.0 214 | "2012-03-31",242436.0 215 | "2012-06-30",242968.0 216 | "2012-09-30",243564.0 217 | "2012-12-31",244169.0 218 | "2013-03-31",244829.0 219 | "2013-06-30",245363.0 220 | "2013-09-30",245961.0 221 | "2013-12-31",246564.0 222 | "2014-03-31",247086.0 223 | "2014-06-30",247625.0 224 | "2014-09-30",248233.0 225 | "2014-12-31",248843.0 226 | "2015-03-31",249901.0 227 | "2015-06-30",250461.0 228 | "2015-09-30",251099.0 229 | "2015-12-31",251741.0 230 | "2016-03-31",252581.0 231 | "2016-06-30",253180.0 232 | "2016-09-30",253855.0 233 | "2016-12-31",254534.0 234 | "2017-03-31",254247.0 235 | -------------------------------------------------------------------------------- /dsgejl/ma1.jl: -------------------------------------------------------------------------------- 1 | """ 2 | ``` 3 | MA1{T} <: AbstractModel{T} 4 | ``` 5 | 6 | Implements the following MA(1) model: 7 | 8 | ``` 9 | x_t = μ + u_t + β*u_{t-1} 10 | u_t ∼ N(0, σ^2) 11 | ``` 12 | 13 | This is represented as the following state-space model: 14 | 15 | ``` 16 | |u_t | = |0 0| |u_{t-1}| + |1| |u_t| (transition equation) 17 | |u_{t-1}| |1 0| |u_{t-2}| |0| 18 | 19 | |x_t| = |1 β| |u_t | + |μ| (measurement equation) 20 | |u_{t-1}| 21 | ``` 22 | """ 23 | type MA1{T} <: AbstractModel{T} 24 | parameters::ParameterVector{T} # vector of all time-invariant model parameters 25 | steady_state::ParameterVector{T} # model steady-state values 26 | keys::OrderedDict{Symbol,Int} # human-readable names for all the model 27 | # parameters and steady-states 28 | 29 | endogenous_states::OrderedDict{Symbol,Int} # these fields used to create matrices in the 30 | exogenous_shocks::OrderedDict{Symbol,Int} # measurement and equilibrium condition equations. 31 | expected_shocks::OrderedDict{Symbol,Int} # 32 | equilibrium_conditions::OrderedDict{Symbol,Int} # 33 | endogenous_states_augmented::OrderedDict{Symbol,Int} # 34 | observables::OrderedDict{Symbol,Int} # 35 | 36 | spec::String # Model specification number (eg "m990") 37 | subspec::String # Model subspecification (eg "ss0") 38 | settings::Dict{Symbol,Setting} # Settings/flags for computation 39 | test_settings::Dict{Symbol,Setting} # Settings/flags for testing mode 40 | rng::MersenneTwister # Random number generator 41 | testing::Bool # Whether we are in testing mode or not 42 | 43 | observable_mappings::OrderedDict{Symbol, Observable} 44 | end 45 | 46 | DSGE.description(m::MA1) = "Moving average model of order 1: MA1, $(m.subspec)" 47 | 48 | function MA1(subspec::String="ss0"; testing = false) 49 | # Model-specific specifications 50 | spec = "ma1" 51 | subspec = subspec 52 | settings = Dict{Symbol,Setting}() 53 | test_settings = Dict{Symbol,Setting}() 54 | rng = MersenneTwister() 55 | 56 | # initialize empty model 57 | m = MA1{Float64}( 58 | # model parameters and steady state values 59 | Vector{AbstractParameter{Float64}}(), Vector{Float64}(), OrderedDict{Symbol,Int}(), 60 | 61 | # model indices 62 | OrderedDict{Symbol,Int}(), OrderedDict{Symbol,Int}(), OrderedDict{Symbol,Int}(), 63 | OrderedDict{Symbol,Int}(), OrderedDict{Symbol,Int}(), OrderedDict{Symbol,Int}(), 64 | 65 | spec, 66 | subspec, 67 | settings, 68 | test_settings, 69 | rng, 70 | testing, 71 | OrderedDict{Symbol,Observable}()) 72 | 73 | # Set settings 74 | settings_ma1!(m) 75 | default_test_settings!(m) 76 | 77 | # Initialize parameters 78 | init_parameters!(m) 79 | 80 | init_model_indices!(m) 81 | steadystate!(m) 82 | 83 | return m 84 | end 85 | 86 | """ 87 | ``` 88 | init_parameters!(m::MA1) 89 | ``` 90 | 91 | Initializes the model's parameters, as well as empty values for the steady-state 92 | parameters (in preparation for `steadystate!(m)` being called to initialize 93 | those). 94 | """ 95 | function init_parameters!(m::MA1) 96 | m <= parameter(:μ, 0.5, (-1e5, 1e5), (1e-5, 5.0), DSGE.Exponential(), 97 | DSGE.Normal(0.0, 1.0), fixed = false, 98 | description = "μ: constant coefficient", 99 | tex_label = "\\mu") 100 | m <= parameter(:β, 0.5, (-1e5, 1e5), (1e-5, 5.0), DSGE.Exponential(), 101 | DSGE.Normal(0.0, 1.0), fixed = false, 102 | description = "β: coefficient on u_{t-1}", 103 | tex_label = "\\beta") 104 | m <= parameter(:σ, 0.1, (1e-5, 5.0), (1e-5, 5.0), DSGE.Exponential(), 105 | DSGE.RootInverseGamma(5.0, 0.5), fixed = false, 106 | description = "σ: standard deviation of u_t", 107 | tex_label = "\\sigma") 108 | end 109 | 110 | """ 111 | ``` 112 | init_model_indices!(m::MA1) 113 | ``` 114 | 115 | Initializes indices for all of `m`'s states, shocks, and equilibrium conditions. 116 | """ 117 | function init_model_indices!(m::MA1) 118 | # Endogenous states 119 | endogenous_states = [:u_t, :u_t1] 120 | 121 | # Exogenous shocks 122 | exogenous_shocks = [:u_t] 123 | 124 | # Expectations shocks 125 | expected_shocks = Symbol[] 126 | 127 | # Equilibrium conditions 128 | equilibrium_conditions = [:eq_u_t, :eq_u_t1] 129 | 130 | # Additional states added after solving model 131 | # Lagged states and observables measurement error 132 | endogenous_states_augmented = [] 133 | 134 | # Measurement equation observables 135 | observables = [:x_t] 136 | 137 | for (i,k) in enumerate(endogenous_states); m.endogenous_states[k] = i end 138 | for (i,k) in enumerate(exogenous_shocks); m.exogenous_shocks[k] = i end 139 | for (i,k) in enumerate(expected_shocks); m.expected_shocks[k] = i end 140 | for (i,k) in enumerate(equilibrium_conditions); m.equilibrium_conditions[k] = i end 141 | for (i,k) in enumerate(endogenous_states); m.endogenous_states[k] = i end 142 | for (i,k) in enumerate(endogenous_states_augmented); m.endogenous_states_augmented[k] = i+length(endogenous_states) end 143 | for (i,k) in enumerate(observables); m.observables[k] = i end 144 | end 145 | 146 | """ 147 | ``` 148 | steadystate!(m::MA1) 149 | ``` 150 | 151 | Calculates the model's steady-state values. `steadystate!(m)` must be called whenever 152 | the parameters of `m` are updated. 153 | """ 154 | function DSGE.steadystate!(m::MA1) 155 | return m 156 | end 157 | 158 | function settings_ma1!(m::MA1) 159 | default_settings!(m) 160 | 161 | m <= Setting(:use_population_forecast, false, "Whether to use population forecasts as data") 162 | end 163 | 164 | # This is an MA(1)-specific version of `solve` which we can use because MA(1) 165 | # models are trivially rational expectations models 166 | function DSGE.solve(m::MA1) 167 | Γ0, Γ1, C, Ψ, Π = eqcond(m) 168 | 169 | # Check that Γ0 is the identity 170 | @assert Γ0 == eye(n_states(m)) 171 | 172 | # Check that Π is empty 173 | @assert isempty(Π) 174 | 175 | T = Γ1 176 | R = Ψ 177 | 178 | return T, R, C 179 | end 180 | 181 | function DSGE.augment_states{T<:AbstractFloat}(m::MA1{T}, TTT::Matrix{T}, RRR::Matrix{T}, CCC::Vector{T}) 182 | return TTT, RRR, CCC 183 | end -------------------------------------------------------------------------------- /dsgejl/input_data/raw/fred_170528.csv: -------------------------------------------------------------------------------- 1 | "date","GDP","CNP16OV","GDPCTPI","CPIAUCSL","DFF" 2 | "1959-03-31",511.1,114714.0,17.189,28.993,2.58 3 | "1959-06-30",524.2,115139.0,17.237,29.043,3.08 4 | "1959-09-30",525.2,115551.0,17.308,29.193,3.57 5 | "1959-12-31",529.3,115918.0,17.375,29.37,3.99 6 | "1960-03-31",543.3,116708.0,17.41,29.397,3.93 7 | "1960-06-30",542.7,117037.0,17.473,29.573,3.7 8 | "1960-09-30",546.0,117411.0,17.552,29.59,2.94 9 | "1960-12-31",541.1,117824.0,17.631,29.78,2.3 10 | "1961-03-31",545.9,118254.0,17.651,29.84,1.99 11 | "1961-06-30",557.4,118636.0,17.688,29.83,1.73 12 | "1961-09-30",568.2,119001.0,17.727,29.947,1.68 13 | "1961-12-31",581.6,119190.0,17.769,29.99,2.4 14 | "1962-03-31",595.2,119379.0,17.859,30.107,2.46 15 | "1962-06-30",602.6,119819.0,17.908,30.22,2.61 16 | "1962-09-30",609.6,120368.0,17.95,30.307,2.85 17 | "1962-12-31",613.1,121046.0,17.992,30.38,2.92 18 | "1963-03-31",622.7,121640.0,18.08,30.477,2.97 19 | "1963-06-30",631.8,122167.0,18.094,30.533,2.96 20 | "1963-09-30",645.0,122670.0,18.112,30.72,3.33 21 | "1963-12-31",654.8,123189.0,18.231,30.803,3.45 22 | "1964-03-31",671.1,123708.0,18.3,30.93,3.46 23 | "1964-06-30",680.8,124203.0,18.355,30.98,3.49 24 | "1964-09-30",692.8,124739.0,18.447,31.05,3.46 25 | "1964-12-31",698.4,125289.0,18.526,31.193,3.58 26 | "1965-03-31",719.2,125814.0,18.606,31.29,3.98 27 | "1965-06-30",732.4,126325.0,18.692,31.49,4.08 28 | "1965-09-30",750.2,126745.0,18.778,31.583,4.08 29 | "1965-12-31",773.1,127169.0,18.9,31.75,4.17 30 | "1966-03-31",797.3,127511.0,19.016,32.047,4.56 31 | "1966-06-30",807.2,127869.0,19.189,32.337,4.91 32 | "1966-09-30",820.8,128234.0,19.359,32.617,5.41 33 | "1966-12-31",834.9,128617.0,19.518,32.883,5.56 34 | "1967-03-31",846.0,129044.0,19.6,32.967,4.82 35 | "1967-06-30",851.1,129527.0,19.717,33.167,3.99 36 | "1967-09-30",866.6,130166.0,19.894,33.5,3.89 37 | "1967-12-31",883.2,130757.0,20.112,33.867,4.17 38 | "1968-03-31",911.1,131267.0,20.332,34.2,4.79 39 | "1968-06-30",936.3,131712.0,20.559,34.533,5.98 40 | "1968-09-30",952.3,132250.0,20.757,35.0,5.95 41 | "1968-12-31",970.1,132880.0,21.047,35.433,5.92 42 | "1969-03-31",995.4,133476.0,21.259,35.867,6.57 43 | "1969-06-30",1011.4,134020.0,21.54,36.433,8.33 44 | "1969-09-30",1032.0,134595.0,21.847,36.933,8.98 45 | "1969-12-31",1040.7,135247.0,22.12,37.5,8.94 46 | "1970-03-31",1053.5,135950.0,22.425,38.1,8.56 47 | "1970-06-30",1070.1,136677.0,22.749,38.633,7.88 48 | "1970-09-30",1088.5,137456.0,22.935,39.033,6.71 49 | "1970-12-31",1091.5,138260.0,23.234,39.6,5.57 50 | "1971-03-31",1137.8,139034.0,23.589,39.933,3.86 51 | "1971-06-30",1159.4,139827.0,23.905,40.3,4.57 52 | "1971-09-30",1180.3,140603.0,24.146,40.7,5.48 53 | "1971-12-31",1193.6,141402.0,24.345,41.0,4.75 54 | "1972-03-31",1233.8,143005.0,24.742,41.333,3.55 55 | "1972-06-30",1270.1,143759.0,24.891,41.6,4.3 56 | "1972-09-30",1293.8,144523.0,25.111,41.933,4.74 57 | "1972-12-31",1332.0,145215.0,25.394,42.367,5.15 58 | "1973-03-31",1380.7,145964.0,25.72,43.033,6.54 59 | "1973-06-30",1417.6,146720.0,26.142,43.933,7.82 60 | "1973-09-30",1436.8,147478.0,26.631,44.8,10.56 61 | "1973-12-31",1479.1,148226.0,27.092,45.933,10.0 62 | "1974-03-31",1494.7,148987.0,27.644,47.3,9.33 63 | "1974-06-30",1534.2,149747.0,28.262,48.567,11.25 64 | "1974-09-30",1563.4,150498.0,29.124,49.933,12.1 65 | "1974-12-31",1603.0,151253.0,30.008,51.467,9.34 66 | "1975-03-31",1619.6,151987.0,30.686,52.567,6.31 67 | "1975-06-30",1656.4,152708.0,31.134,53.2,5.42 68 | "1975-09-30",1713.8,153579.0,31.688,54.267,6.16 69 | "1975-12-31",1765.9,154336.0,32.216,55.267,5.41 70 | "1976-03-31",1824.5,155075.0,32.563,55.9,4.83 71 | "1976-06-30",1856.9,155774.0,32.889,56.4,5.2 72 | "1976-09-30",1890.5,156527.0,33.309,57.3,5.28 73 | "1976-12-31",1938.4,157222.0,33.868,58.133,4.87 74 | "1977-03-31",1992.5,157911.0,34.411,59.2,4.66 75 | "1977-06-30",2060.2,158652.0,34.952,60.233,5.16 76 | "1977-09-30",2122.4,159430.0,35.437,61.067,5.82 77 | "1977-12-31",2168.7,160140.0,36.034,61.967,6.51 78 | "1978-03-31",2208.7,160829.0,36.637,63.033,6.76 79 | "1978-06-30",2336.6,161525.0,37.338,64.467,7.28 80 | "1978-09-30",2398.9,162265.0,37.987,65.967,8.09 81 | "1978-12-31",2482.2,163024.0,38.759,67.5,9.58 82 | "1979-03-31",2531.6,163756.0,39.47,69.2,10.07 83 | "1979-06-30",2595.9,164447.0,40.411,71.4,10.18 84 | "1979-09-30",2670.4,165200.0,41.235,73.7,10.94 85 | "1979-12-31",2730.7,166055.0,42.044,76.033,13.58 86 | "1980-03-31",2796.5,166762.0,42.955,79.033,15.07 87 | "1980-06-30",2799.9,167416.0,43.895,81.7,12.67 88 | "1980-09-30",2860.0,168111.0,44.903,83.233,9.82 89 | "1980-12-31",2993.5,168694.0,46.165,85.567,15.85 90 | "1981-03-31",3131.8,169279.0,47.358,87.933,16.6 91 | "1981-06-30",3167.3,169837.0,48.197,89.767,17.79 92 | "1981-09-30",3261.2,170413.0,49.096,92.267,17.59 93 | "1981-12-31",3283.5,170990.0,49.98,93.767,13.59 94 | "1982-03-31",3273.8,171497.0,50.652,94.6,14.21 95 | "1982-06-30",3331.3,172020.0,51.278,95.967,14.51 96 | "1982-09-30",3367.1,172522.0,52.001,97.633,11.01 97 | "1982-12-31",3407.8,173046.0,52.566,97.933,9.28 98 | "1983-03-31",3480.3,173505.0,53.013,98.0,8.66 99 | "1983-06-30",3583.8,173957.0,53.371,99.133,8.8 100 | "1983-09-30",3692.3,174449.0,53.929,100.1,9.46 101 | "1983-12-31",3796.1,174950.0,54.32,101.1,9.43 102 | "1984-03-31",3912.8,175679.0,54.885,102.533,9.69 103 | "1984-06-30",4015.0,176125.0,55.37,103.5,10.55 104 | "1984-09-30",4087.4,176595.0,55.827,104.4,11.39 105 | "1984-12-31",4147.6,177132.0,56.174,105.3,9.26 106 | "1985-03-31",4237.0,177522.0,56.839,106.267,8.48 107 | "1985-06-30",4302.3,177946.0,57.161,107.233,7.92 108 | "1985-09-30",4394.6,178413.0,57.528,107.9,7.9 109 | "1985-12-31",4453.1,178941.0,57.837,109.0,8.1 110 | "1986-03-31",4516.3,179825.0,58.118,109.567,7.83 111 | "1986-06-30",4555.2,180321.0,58.334,109.033,6.92 112 | "1986-09-30",4619.6,180836.0,58.606,109.7,6.21 113 | "1986-12-31",4669.4,181365.0,58.96,110.467,6.27 114 | "1987-03-31",4736.2,182001.0,59.306,111.8,6.22 115 | "1987-06-30",4821.5,182527.0,59.694,113.067,6.65 116 | "1987-09-30",4900.5,183016.0,60.135,114.267,6.84 117 | "1987-12-31",5022.7,183467.0,60.605,115.333,6.92 118 | "1988-03-31",5090.6,183967.0,61.075,116.233,6.67 119 | "1988-06-30",5207.7,184389.0,61.681,117.567,7.15 120 | "1988-09-30",5299.5,184840.0,62.426,119.0,7.98 121 | "1988-12-31",5412.7,185253.0,62.961,120.3,8.47 122 | "1989-03-31",5527.4,185773.0,63.602,121.667,9.45 123 | "1989-06-30",5628.4,186178.0,64.271,123.633,9.73 124 | "1989-09-30",5711.6,186602.0,64.744,124.6,9.08 125 | "1989-12-31",5763.4,187018.0,65.174,125.867,8.61 126 | "1990-03-31",5890.8,188520.0,65.901,128.033,8.25 127 | "1990-06-30",5974.7,188916.0,66.58,129.3,8.24 128 | "1990-09-30",6029.5,189353.0,67.18,131.533,8.16 129 | "1990-12-31",6023.3,189866.0,67.702,133.767,7.74 130 | "1991-03-31",6054.9,190272.0,68.373,134.767,6.43 131 | "1991-06-30",6143.6,190656.0,68.832,135.567,5.86 132 | "1991-09-30",6218.4,191121.0,69.328,136.6,5.65 133 | "1991-12-31",6279.3,191651.0,69.694,137.733,4.82 134 | "1992-03-31",6380.8,192075.0,70.014,138.667,4.02 135 | "1992-06-30",6492.3,192507.0,70.457,139.733,3.77 136 | "1992-09-30",6586.5,193024.0,70.785,140.8,3.26 137 | "1992-12-31",6697.6,193616.0,71.275,142.033,3.03 138 | "1993-03-31",6748.2,194106.0,71.704,143.067,3.04 139 | "1993-06-30",6829.6,194555.0,72.136,144.1,3.0 140 | "1993-09-30",6904.2,195068.0,72.504,144.767,3.06 141 | "1993-12-31",7032.8,195621.0,72.913,145.967,2.99 142 | "1994-03-31",7136.3,196085.0,73.291,146.7,3.21 143 | "1994-06-30",7269.8,196522.0,73.652,147.533,3.94 144 | "1994-09-30",7352.3,197050.0,74.021,148.9,4.49 145 | "1994-12-31",7476.7,197601.0,74.44,149.767,5.17 146 | "1995-03-31",7545.3,197882.0,74.89,150.867,5.8 147 | "1995-06-30",7604.9,198296.0,75.226,152.1,6.02 148 | "1995-09-30",7706.5,198807.0,75.548,152.867,5.8 149 | "1995-12-31",7799.5,199352.0,75.908,153.7,5.72 150 | "1996-03-31",7893.1,199776.0,76.296,155.067,5.37 151 | "1996-06-30",8061.5,200279.0,76.584,156.4,5.24 152 | "1996-09-30",8159.0,200850.0,76.932,157.3,5.31 153 | "1996-12-31",8287.1,201457.0,77.257,158.667,5.28 154 | "1997-03-31",8402.1,202396.0,77.637,159.633,5.28 155 | "1997-06-30",8551.9,202835.0,77.998,160.0,5.52 156 | "1997-09-30",8691.8,203367.0,78.225,160.8,5.53 157 | "1997-12-31",8788.3,203935.0,78.493,161.667,5.51 158 | "1998-03-31",8889.7,204395.0,78.606,162.0,5.52 159 | "1998-06-30",8994.7,204905.0,78.786,162.533,5.5 160 | "1998-09-30",9146.5,205483.0,79.071,163.367,5.53 161 | "1998-12-31",9325.7,206098.0,79.278,164.133,4.86 162 | "1999-03-31",9447.1,206876.0,79.575,164.733,4.73 163 | "1999-06-30",9557.0,207432.0,79.908,165.967,4.75 164 | "1999-09-30",9712.3,208044.0,80.191,167.2,5.1 165 | "1999-12-31",9926.1,208660.0,80.585,168.433,5.3 166 | "2000-03-31",10031.0,211586.0,81.184,170.1,5.68 167 | "2000-06-30",10278.3,212242.0,81.631,171.433,6.27 168 | "2000-09-30",10357.4,212919.0,82.154,173.0,6.52 169 | "2000-12-31",10472.3,213560.0,82.591,174.233,6.47 170 | "2001-03-31",10508.1,214101.0,83.117,175.9,5.6 171 | "2001-06-30",10638.4,214736.0,83.699,177.133,4.33 172 | "2001-09-30",10639.5,215422.0,83.97,177.633,3.5 173 | "2001-12-31",10701.3,216112.0,84.233,177.5,2.13 174 | "2002-03-31",10834.4,216664.0,84.48,178.067,1.73 175 | "2002-06-30",10934.8,217204.0,84.829,179.467,1.75 176 | "2002-09-30",11037.1,217868.0,85.204,180.433,1.74 177 | "2002-12-31",11103.8,218543.0,85.649,181.5,1.44 178 | "2003-03-31",11230.1,220109.0,86.184,183.367,1.25 179 | "2003-06-30",11370.7,220774.0,86.463,183.067,1.25 180 | "2003-09-30",11625.1,221513.0,86.932,184.433,1.02 181 | "2003-12-31",11816.8,222276.0,87.363,185.133,1.0 182 | "2004-03-31",11988.4,222356.0,88.115,186.7,1.0 183 | "2004-06-30",12181.4,222973.0,88.856,188.167,1.01 184 | "2004-09-30",12367.7,223680.0,89.438,189.367,1.43 185 | "2004-12-31",12562.2,224418.0,90.063,191.4,1.95 186 | "2005-03-31",12813.7,225038.0,90.894,192.367,2.47 187 | "2005-06-30",12974.1,225674.0,91.549,193.667,2.94 188 | "2005-09-30",13205.4,226422.0,92.399,196.6,3.46 189 | "2005-12-31",13381.6,227196.0,93.098,198.433,3.98 190 | "2006-03-31",13648.9,227764.0,93.816,199.467,4.45 191 | "2006-06-30",13799.8,228433.0,94.589,201.267,4.91 192 | "2006-09-30",13908.5,229166.0,95.25,203.167,5.25 193 | "2006-12-31",14066.4,229896.0,95.591,202.333,5.24 194 | "2007-03-31",14233.2,230839.0,96.659,204.317,5.25 195 | "2007-06-30",14422.3,231482.0,97.216,206.631,5.25 196 | "2007-09-30",14569.7,232210.0,97.538,207.939,5.07 197 | "2007-12-31",14685.3,232937.0,97.945,210.49,4.5 198 | "2008-03-31",14668.4,232807.0,98.506,212.77,3.18 199 | "2008-06-30",14813.0,233410.0,98.941,215.538,2.08 200 | "2008-09-30",14843.0,234110.0,99.619,218.861,1.94 201 | "2008-12-31",14549.9,234825.0,99.805,213.849,0.51 202 | "2009-03-31",14383.9,234913.0,100.045,212.378,0.18 203 | "2009-06-30",14340.4,235459.0,99.889,213.507,0.18 204 | "2009-09-30",14384.1,236093.0,99.882,215.344,0.15 205 | "2009-12-31",14566.5,236739.0,100.183,217.03,0.12 206 | "2010-03-31",14681.1,236996.0,100.517,217.374,0.13 207 | "2010-06-30",14888.6,237506.0,100.981,217.297,0.19 208 | "2010-09-30",15057.7,238104.0,101.444,217.934,0.19 209 | "2010-12-31",15230.2,238711.0,101.963,219.699,0.19 210 | "2011-03-31",15238.4,238852.0,102.409,222.044,0.15 211 | "2011-06-30",15460.9,239316.0,103.17,224.568,0.09 212 | "2011-09-30",15587.1,239871.0,103.77,226.033,0.08 213 | "2011-12-31",15785.3,240431.0,103.913,227.047,0.07 214 | "2012-03-31",15973.9,242436.0,104.466,228.326,0.1 215 | "2012-06-30",16121.9,242968.0,104.93,228.808,0.15 216 | "2012-09-30",16227.9,243564.0,105.547,229.841,0.14 217 | "2012-12-31",16297.3,244169.0,105.937,231.369,0.16 218 | "2013-03-31",16475.4,244829.0,106.318,232.299,0.14 219 | "2013-06-30",16541.4,245363.0,106.565,232.028,0.12 220 | "2013-09-30",16749.3,245961.0,107.112,233.281,0.09 221 | "2013-12-31",16999.9,246564.0,107.674,234.187,0.09 222 | "2014-03-31",17025.2,247086.0,108.14,235.678,0.07 223 | "2014-06-30",17285.6,247625.0,108.714,236.777,0.09 224 | "2014-09-30",17569.4,248233.0,109.178,237.389,0.09 225 | "2014-12-31",17692.2,248843.0,109.321,236.971,0.1 226 | "2015-03-31",17783.6,249901.0,109.307,235.464,0.11 227 | "2015-06-30",17998.3,250461.0,109.922,236.837,0.13 228 | "2015-09-30",18141.9,251099.0,110.268,237.718,0.13 229 | "2015-12-31",18222.8,251741.0,110.498,237.93,0.16 230 | "2016-03-31",18281.6,252581.0,110.635,237.997,0.36 231 | "2016-06-30",18450.1,253180.0,111.268,239.371,0.37 232 | "2016-09-30",18675.3,253855.0,111.662,240.431,0.39 233 | "2016-12-31",18869.4,254534.0,112.238,242.238,0.45 234 | "2017-03-31",19027.6,254247.0,112.859,244.122,0.7 235 | -------------------------------------------------------------------------------- /dsgejl/input_data/data/data_dsid=00_vint=170528.csv: -------------------------------------------------------------------------------- 1 | "date","obs_gdp","obs_cpi","obs_nominalrate" 2 | "1959-09-30",-0.5928941049430092,2.0605856407669876,3.57 3 | "1959-12-31",0.02018381486104115,2.4179162810266774,3.99 4 | "1960-03-31",2.0396041783242946,0.3675532445051033,3.93 5 | "1960-06-30",-0.840207740247112,2.387661829199139,3.7 6 | "1960-09-30",-0.21226949567756392,0.2298734069317021,2.94 7 | "1960-12-31",-1.7171288157988185,2.5602243374978073,2.3 8 | "1961-03-31",0.4037156018447785,0.8050992318789696,1.99 9 | "1961-06-30",1.5093084964866055,-0.1340707235613081,1.73 10 | "1961-09-30",1.3323504359356253,1.5658216291399185,1.68 11 | "1961-12-31",1.7269304351347436,0.5739360645643288,2.4 12 | "1962-03-31",1.4374277391216994,1.557484038398016,2.46 13 | "1962-06-30",0.590999897470379,1.4985015950722769,2.61 14 | "1962-09-30",0.5481359529227126,1.1499008365250418,2.85 15 | "1962-12-31",-0.03552896771055192,0.9623152926883094,2.92 16 | "1963-03-31",0.6900121771595824,1.275121443956273,2.97 17 | "1963-06-30",0.9966473616243032,0.7343060577193583,2.96 18 | "1963-09-30",1.5910086945867485,2.4423369430621733,3.33 19 | "1963-12-31",0.47564751010202855,1.079271821654082,3.45 20 | "1964-03-31",1.7038899377346928,1.6457995453487584,3.46 21 | "1964-06-30",0.7583288953932898,0.6460993166976792,3.49 22 | "1964-09-30",0.8714628389234536,0.902789356306144,3.46 23 | "1964-12-31",0.002765457124653814,1.8379609157399557,3.58 24 | "1965-03-31",2.129738132972747,1.2419388047812063,3.98 25 | "1965-06-30",0.9840983280661408,2.5485909728569567,4.08 26 | "1965-09-30",1.5689962572967397,1.179586414435363,4.08 27 | "1965-12-31",1.9855617147928495,2.1094896758716786,4.17 28 | "1966-03-31",2.095419773957552,3.72433996108974,4.56 29 | "1966-06-30",-0.04881936915910323,3.603404090470441,4.91 30 | "1966-09-30",0.4082118886425734,3.4486157512882087,5.41 31 | "1966-12-31",0.5002407324533609,3.2488728223496466,5.56 32 | "1967-03-31",0.5108859937845933,1.02050169248038,4.82 33 | "1967-06-30",-0.39133370107793386,2.4193378257173492,3.99 34 | "1967-09-30",0.5064072829144894,3.9960132535322046,3.89 35 | "1967-12-31",0.3946300441121764,4.358260046599405,4.17 36 | "1968-03-31",1.600274957355441,3.9138220484161934,4.79 37 | "1968-06-30",1.1865652657286896,3.8758978129900257,5.98 38 | "1968-09-30",0.29429361703634527,5.373069155351473,5.95 39 | "1968-12-31",0.01219069427627062,4.918211123362504,5.92 40 | "1969-03-31",1.109139819933258,4.869625359546426,6.57 41 | "1969-06-30",-0.19278902299713074,6.262921858004411,8.33 42 | "1969-09-30",0.11573346299225129,5.452201196756334,8.98 43 | "1969-12-31",-0.8987326319791045,6.094189057110988,8.94 44 | "1970-03-31",-0.6539761019745738,6.349339662516051,8.56 45 | "1970-06-30",-0.3881178656691242,5.557020552008929,7.88 46 | "1970-09-30",0.3642026598111414,4.120243470708296,6.71 47 | "1970-12-31",-1.5547329070608296,5.768670428394529,5.57 48 | "1971-03-31",2.0960410314301736,3.34957258902282,3.86 49 | "1971-06-30",0.0019213307169696492,3.6593676878581505,4.57 50 | "1971-09-30",0.23083135677348654,3.9506493983649804,5.48 51 | "1971-12-31",-0.2561739733350832,2.937589702303356,4.75 52 | "1972-03-31",1.1372290490639703,3.2356582735886263,3.55 53 | "1972-06-30",1.7414118383245913,2.5755819515751455,4.3 54 | "1972-09-30",0.41210939688531845,3.189175669528943,4.74 55 | "1972-12-31",1.2345847671984433,4.118660350368586,5.15 56 | "1973-03-31",1.7638244424760252,6.239002361656354,6.54 57 | "1973-06-30",0.46224553484527364,8.279393158798953,7.82 58 | "1973-09-30",-1.051553118071713,7.816957321136009,10.56 59 | "1973-12-31",0.6462748336072521,9.990269428051235,10.0 60 | "1974-03-31",-1.5020019854286804,11.73059300272783,9.33 61 | "1974-06-30",-0.1316892311338913,10.57359697183653,11.25 62 | "1974-09-30",-1.6429805435466127,11.095127559130447,12.1 63 | "1974-12-31",-1.007424893472253,12.103487504090182,9.34 64 | "1975-03-31",-1.7174094679909047,8.45908833973521,6.31 65 | "1975-06-30",0.2892432395826594,4.787939965092214,5.42 66 | "1975-09-30",1.1400918554235195,7.94316441600369,6.16 67 | "1975-12-31",0.8447252570727093,7.303871196150702,5.41 68 | "1976-03-31",1.7010136313587054,4.5553579132274535,4.83 69 | "1976-06-30",0.2771660308813262,3.5619113371840427,5.2 70 | "1976-09-30",0.04265833807051145,6.332586086672265,5.28 71 | "1976-12-31",0.3613524694657366,5.773146049872935,4.87 72 | "1977-03-31",0.6908219014977623,7.275221217833661,4.66 73 | "1977-06-30",1.3151910616753915,6.91953319619536,5.16 74 | "1977-09-30",1.135438684297796,5.50049887674291,5.82 75 | "1977-12-31",0.031801564528555204,5.852145283465049,6.51 76 | "1978-03-31",-0.2820672377995437,6.822565397621361,6.76 77 | "1978-06-30",3.289520757900144,8.998026477404508,7.28 78 | "1978-09-30",0.46950784238567467,9.200460806484045,8.09 79 | "1978-12-31",0.9691586724966617,9.189192357496268,9.58 80 | "1979-03-31",-0.2731268558581301,9.949305898055982,10.07 81 | "1979-06-30",-0.26707532545325874,12.518802688765973,10.18 82 | "1979-09-30",0.3990014252848506,12.681971939900905,10.94 83 | "1979-12-31",-0.11437558752169785,12.465862950097772,13.58 84 | "1980-03-31",-0.1591080291300062,15.479212077651283,15.07 85 | "1980-06-30",-2.4316299342444725,13.275406040517268,12.67 86 | "1980-09-30",-0.5267433780068664,7.435960775149653,9.82 87 | "1980-12-31",1.418641619508798,11.062316398216154,15.85 88 | "1981-03-31",1.6015364165630541,10.910186423191703,16.6 89 | "1981-06-30",-0.9844353358881897,8.25690544642228,17.79 90 | "1981-09-30",0.7257757860716585,10.987649324894022,17.59 91 | "1981-12-31",-1.4433747827353516,6.450573583724051,13.59 92 | "1982-03-31",-1.9647631798625165,3.5377977251524584,14.21 93 | "1982-06-30",0.18596114879960712,5.738762525771435,14.51 94 | "1982-09-30",-0.6521112405710072,6.884467461188493,11.01 95 | "1982-12-31",-0.1947116971282581,1.2272081458686301,9.28 96 | "1983-03-31",0.9475996303664913,0.2735629122671668,8.66 97 | "1983-06-30",1.9508660903020725,4.597961682775065,8.8 98 | "1983-09-30",1.639494474234282,3.88292137746582,9.46 99 | "1983-12-31",1.750134868093034,3.9761758821004634,9.43 100 | "1984-03-31",1.6958594810431826,5.629828782550561,9.69 101 | "1984-06-30",1.4036033965974082,3.754765889048528,10.55 102 | "1984-09-30",0.6720515646896019,3.463225097246081,11.39 103 | "1984-12-31",0.5507979760332571,3.4334974765563686,9.26 104 | "1985-03-31",0.6652390857280532,3.6565503463847904,8.48 105 | "1985-06-30",0.6749576062442864,3.619696831992414,7.92 106 | "1985-09-30",1.193801389712723,2.4803340712864497,7.9 107 | "1985-12-31",0.4983899098484512,4.057203986021918,8.1 108 | "1986-03-31",0.636846156027504,2.075340822437255,7.83 109 | "1986-06-30",0.19962224306584453,-1.9542582330970504,6.92 110 | "1986-09-30",0.6524310388910437,2.4395114314764044,6.21 111 | "1986-12-31",0.18469564904058933,2.7869866160497025,6.27 112 | "1987-03-31",0.5510001760388072,4.797890759875756,6.22 113 | "1987-06-30",0.8496511904668824,4.507601055000521,6.65 114 | "1987-09-30",0.607047291195828,4.222900738247759,6.84 115 | "1987-12-31",1.403494217248713,3.7143124031633334,6.92 116 | "1988-03-31",0.29032072688126576,3.10928040107612,6.67 117 | "1988-06-30",1.007925589530656,4.564634650370891,7.15 118 | "1988-09-30",0.268686759132708,4.8460437083537755,7.98 119 | "1988-12-31",0.9828308282738285,4.346051947641527,8.47 120 | "1989-03-31",0.807371156030301,4.5196726624091355,9.45 121 | "1989-06-30",0.48851680464631064,6.41187801705243,9.73 122 | "1989-09-30",0.459087993528684,3.116442669626096,9.08 123 | "1989-12-31",-0.03316280668188565,4.04687502058465,8.61 124 | "1990-03-31",0.8044122016453881,6.824899714748511,8.25 125 | "1990-06-30",0.118241449697154,3.938897034450406,8.24 126 | "1990-09-30",-0.2529071261710869,6.848993971009065,8.16 127 | "1990-12-31",-1.1433978544932888,6.736683919057285,7.74 128 | "1991-03-31",-0.7271141592003705,2.9791523533649666,6.43 129 | "1991-06-30",0.5234108511533941,2.367448668202954,5.86 130 | "1991-09-30",0.2325638962625165,3.0363856319169713,5.65 131 | "1991-12-31",0.19052524190749098,3.304032520891198,4.82 132 | "1992-03-31",0.8897794519316715,2.703338799000221,4.02 133 | "1992-06-30",0.8476836465500459,3.06323336131058,3.77 134 | "1992-09-30",0.7236372573950489,3.042793953698819,3.26 135 | "1992-12-31",0.7316941504657781,3.4875924976514483,3.03 136 | "1993-03-31",-0.09756104956587519,2.901450813508788,3.04 137 | "1993-06-30",0.349014849995144,2.877780401453478,3.0 138 | "1993-09-30",0.32865905473155044,1.8472201681507983,3.06 139 | "1993-12-31",1.0342454662824352,3.3020061348725704,2.99 140 | "1994-03-31",0.6948972150996324,2.003646554360472,3.21 141 | "1994-06-30",1.112450530065201,2.264877768375939,3.94 142 | "1994-09-30",0.37789674962465103,3.6892240480352,4.49 143 | "1994-12-31",0.8609093008917545,2.322325359984845,5.17 144 | "1995-03-31",0.05593731993898121,2.9271603581737082,5.8 145 | "1995-06-30",0.08157206915908599,3.255818111955122,6.02 146 | "1995-09-30",0.6389609638434934,2.01202521502708,5.8 147 | "1995-12-31",0.4590660043608752,2.173755296693969,5.72 148 | "1996-03-31",0.413406821561102,3.5418524170435006,5.37 149 | "1996-06-30",1.4595377427925937,3.42381860962071,5.24 150 | "1996-09-30",0.46855515335771925,2.2951927812087547,5.31 151 | "1996-12-31",0.8501776359840951,3.4611425348828107,5.28 152 | "1997-03-31",0.5952957570788509,2.427905663291696,5.28 153 | "1997-06-30",1.0047934357796162,0.9185538696630857,5.52 154 | "1997-09-30",1.0270721139827188,1.995016604415767,5.53 155 | "1997-12-31",0.45043707515137754,2.150922951220835,5.51 156 | "1998-03-31",0.6848160803236802,0.8230684437862124,5.52 157 | "1998-06-30",0.6200128628539581,1.313889137263402,5.5 158 | "1998-09-30",0.9800799135847638,2.047258227874593,5.53 159 | "1998-12-31",1.3396705174397283,1.871148450274518,4.86 160 | "1999-03-31",0.5739294893184366,1.4595626564638309,4.73 161 | "1999-06-30",0.3877219928840714,2.985196800545964,4.75 162 | "1999-09-30",0.9021359408222906,2.960690894864726,5.1 163 | "1999-12-31",1.3271295613530576,2.9389375819324215,5.3 164 | "2000-03-31",-0.05213342770377105,3.9393819185534085,5.68 165 | "2000-06-30",1.522521084978179,3.122408127448395,6.27 166 | "2000-09-30",-0.23558015695605006,3.6396299109370034,6.52 167 | "2000-12-31",0.21042025338270376,2.8407557631449976,6.47 168 | "2001-03-31",-0.6538892931609475,3.8088671351481196,5.6 169 | "2001-06-30",0.17679712175246998,2.7940844362870365,4.33 170 | "2001-09-30",-0.6678396013807708,1.127504509583943,3.5 171 | "2001-12-31",-0.08533933015861805,-0.2996060770875886,2.13 172 | "2002-03-31",0.59479800952148,1.2757100194384918,1.73 173 | "2002-06-30",0.1650474739882335,3.132585220697237,1.75 174 | "2002-09-30",0.14850699309159054,2.1472682851637614,1.74 175 | "2002-12-31",-0.2563516305340463,2.358454390474307,1.44 176 | "2003-03-31",0.17427474345051985,4.093582143217844,1.25 177 | "2003-06-30",0.5911758402268985,-0.6549612034408625,1.25 178 | "2003-09-30",1.3462591473773156,2.9736191368730402,1.02 179 | "2003-12-31",0.8200101113492009,1.5152927209189926,1.0 180 | "2004-03-31",0.26802090777713894,3.371425939539563,1.0 181 | "2004-06-30",0.4471951520353512,3.13072634061875,1.01 182 | "2004-09-30",0.5564990223554167,2.542825899070067,1.43 183 | "2004-12-31",0.5594574072247505,4.27141914878213,1.95 184 | "2005-03-31",0.7630412748996118,2.015810732158485,2.47 185 | "2005-06-30",0.2289941103769899,2.694073401386632,2.94 186 | "2005-09-30",0.549706607588113,6.012407344139703,3.46 187 | "2005-12-31",0.2825071408916746,3.7121215805839824,3.98 188 | "2006-03-31",0.9242256307479213,2.0789189796921903,4.45 189 | "2006-06-30",-0.002295476059399615,3.593430266817421,4.91 190 | "2006-09-30",-0.18877425475537501,3.7583664640639114,5.25 191 | "2006-12-31",0.4989081230407699,-1.645378398954378,5.24 192 | "2007-03-31",-0.20030723343730794,3.9031417529724166,5.25 193 | "2007-06-30",0.4818069648075185,4.5047538024434175,5.25 194 | "2007-09-30",0.42735813620486396,2.524069450641875,5.07 195 | "2007-12-31",0.11954905961186757,4.877351438813093,4.5 196 | "2008-03-31",-0.9364803901167951,4.3094496119504555,3.18 197 | "2008-06-30",0.2937658099728291,5.170183207949108,2.08 198 | "2008-09-30",-0.7240837604668371,6.1198388021239225,1.94 199 | "2008-12-31",-2.4219057059525206,-9.2666666494857,0.51 200 | "2009-03-31",-1.6266427730777466,-2.7609810553297365,0.18 201 | "2009-06-30",-0.38458299950402397,2.120765267070368,0.18 202 | "2009-09-30",0.07411451894157595,3.426852189965146,0.15 203 | "2009-12-31",0.7220006492627773,3.1195373366138313,0.12 204 | "2010-03-31",0.2130034456530261,0.6335117943780944,0.13 205 | "2010-06-30",0.7038829789973722,-0.14171638007667298,0.19 206 | "2010-09-30",0.4310979826630812,1.170873315254184,0.19 207 | "2010-12-31",0.38575736051061904,3.226465224826569,0.19 208 | "2011-03-31",-0.628206052503899,4.246853085183844,0.15 209 | "2011-06-30",0.46080904473251305,4.521198483964994,0.09 210 | "2011-09-30",-0.018277482055345518,2.6009798680529883,0.08 211 | "2011-12-31",0.871705177637444,1.7904152784495864,0.07 212 | "2012-03-31",0.40037151236618773,2.2469551128246223,0.1 213 | "2012-06-30",0.22067454386021645,0.8435166557223539,0.15 214 | "2012-09-30",-0.19067758443584992,1.801816633996367,0.14 215 | "2012-12-31",-0.2027410633734239,2.6504292457598666,0.16 216 | "2013-03-31",0.4666030336684057,1.6045985440232613,0.14 217 | "2013-06-30",-0.0939159315827498,-0.46691237988909506,0.12 218 | "2013-09-30",0.4751574288446414,2.154272586454198,0.09 219 | "2013-12-31",0.6998315208746997,1.5504824020212027,0.09 220 | "2014-03-31",-0.5451011619941548,2.5386100340913487,0.07 221 | "2014-06-30",0.7266184555212996,1.8609213274867642,0.09 222 | "2014-09-30",0.9407988964030778,1.0325503555105087,0.09 223 | "2014-12-31",0.30402701844173263,-0.7049500095980932,0.1 224 | "2015-03-31",0.2668426854225303,-2.5518940999148754,0.11 225 | "2015-06-30",0.37829221165484594,2.3256421099567604,0.13 226 | "2015-09-30",0.2203767159070058,1.4851825598722002,0.13 227 | "2015-12-31",-0.02267358640681838,0.3565662238266043,0.16 228 | "2016-03-31",-0.06011706840896025,0.11262231467021877,0.36 229 | "2016-06-30",0.08954197108000939,2.302632425005058,0.37 230 | "2016-09-30",0.6032820115684512,1.767398592820868,0.39 231 | "2016-12-31",0.26393424245871877,2.9950271382613636,0.45 232 | "2017-03-31",0.028424805059812375,3.0989545578485433,0.7 233 | -------------------------------------------------------------------------------- /intro/slides.tex: -------------------------------------------------------------------------------- 1 | \documentclass[ 2 | xcolor={svgnames}, 3 | hyperref={colorlinks,citecolor=DeepPink4,linkcolor=DarkRed,urlcolor=DarkBlue} 4 | ]{beamer} % for hardcopy add 'trans' 5 | 6 | \usetheme{Boadilla} 7 | % \usepackage{beamerthemesplit} // Activate for custom appearance 8 | 9 | %\usepackage{paralist} 10 | %\usepackage{enumerate} 11 | \setbeamertemplate{caption}[numbered] 12 | 13 | \setbeamercolor{item projected}{bg=blue} 14 | \setbeamertemplate{enumerate items}[default] 15 | \setbeamertemplate{navigation symbols}{} 16 | \setbeamercovered{transparent} 17 | \setbeamercolor{block title}{fg=blue} 18 | \setbeamercolor{local structure}{fg=blue} 19 | \setbeamercolor{caption name}{fg=blue} 20 | 21 | \usepackage{transparent} 22 | % \usepackage{tikz} 23 | % \usepackage{CJKutf8} 24 | % \usetikzlibrary{shapes} 25 | 26 | % \newcommand{\semitransp}[2][35]{\color{fg!#1}#2} 27 | 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 29 | %% Load packages 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 31 | 32 | \usepackage{fancybox} 33 | % \usepackage{enumitem} % Has to do with enumeration 34 | \usepackage{amsfonts} 35 | \usepackage{amsmath} 36 | \usepackage{amsthm} % Allows for labeling of theorems 37 | \usepackage[T1]{fontenc} 38 | \usepackage[utf8]{inputenc} 39 | \usepackage{graphicx} 40 | \graphicspath{ {Figures/} } 41 | \usepackage{hyperref} 42 | \hypersetup{ 43 | colorlinks, 44 | citecolor=black, 45 | filecolor=black, 46 | linkcolor=blue, 47 | urlcolor=blue 48 | } 49 | \newtheorem{thm}{Theorem}[section] 50 | \newtheorem{lem}[thm]{Lemma} 51 | \newtheorem{prop}[thm]{Proposition} 52 | \newtheorem{cor}[thm]{Corollary} 53 | % \usepackage{appendix} 54 | % \usepackage{subfigure} % For plotting multiple figures at once 55 | % \usepackage{verbatim} % for including verbatim code from a file 56 | % \usepackage{pdfpages} 57 | 58 | \usepackage{textcomp} % to get upquotes in listings environments. 59 | \usepackage{listings} % for texing code 60 | %\usepackage{alltt} 61 | %\usepackage{courier} 62 | \usepackage{xcolor} 63 | 64 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 65 | %% Colors 66 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 67 | 68 | \definecolor{codegreen}{RGB}{28,172,0} 69 | \definecolor{codelilas}{RGB}{170,55,241} 70 | \definecolor{gray}{RGB}{120,120,120} 71 | \definecolor{LightGray}{gray}{.85} 72 | 73 | \newcommand\boldblue[1]{\textcolor{blue}{\textbf{#1}}} 74 | 75 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 76 | %% Configure TeXed code 77 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 78 | 79 | %% Bash 80 | \lstdefinestyle{bash}{% 81 | language=bash,% 82 | basicstyle=\footnotesize\ttfamily,% 83 | showstringspaces=false,% 84 | commentstyle=\color{gray},% 85 | keywordstyle=\color{blue},% 86 | xleftmargin=0.25in,% 87 | xrightmargin=0.25in,% 88 | upquote=true 89 | } 90 | \newcommand\bashstyle{\lstset{style=bash}} 91 | 92 | %% MATLAB 93 | \lstdefinestyle{matlab}{% 94 | language=Matlab,% 95 | basicstyle=\footnotesize\ttfamily,% 96 | breaklines=true,% 97 | morekeywords={matlab2tikz},% 98 | keywordstyle=\color{blue},% 99 | morekeywords=[2]{1}, keywordstyle=[2]{\color{black}},% 100 | identifierstyle=\color{black},% 101 | stringstyle=\color{codelilas},% 102 | commentstyle=\color{codegreen},% 103 | showstringspaces=false,% 104 | % Without this there will be a symbol in 105 | % the places where there is a space 106 | emph=[1]{for,end,break,switch,case},emphstyle=[1]\color{blue},% 107 | % Some words to emphasise 108 | upquote=true 109 | } 110 | \newcommand\matlabstyle{\lstset{style=matlab}} 111 | 112 | \newcommand{\matlabcode}[1]{% 113 | \matlabstyle% 114 | \lstinputlisting{#1} 115 | } 116 | 117 | %% Julia 118 | 119 | % Highlight the 'julia>' prompt. 120 | % See http://tex.stackexchange.com/a/172076/50294 for the additional highlighting 121 | % See http://tex.stackexchange.com/a/180619/50294 for the '>' issue 122 | \makeatletter 123 | \lst@InstallKeywords k{attributes}{attributestyle}\slshape{attributestyle}{}ld 124 | \makeatother 125 | 126 | % (c) 2014 Jubobs 127 | % See http://tex.stackexchange.com/a/212794 128 | \lstdefinelanguage{Julia}% 129 | {morekeywords={abstract,break,case,catch,const,continue,do,else,elseif,% 130 | end,export,false,for,function,immutable,import,importall,if,in,% 131 | macro,module,otherwise,quote,return,switch,true,try,type,typealias,% 132 | using,while},% 133 | sensitive=true,% 134 | alsoother={\$},% 135 | morecomment=[l]\#,% 136 | morecomment=[n]{\#=}{=\#},% 137 | morestring=[s]{"}{"},% 138 | morestring=[m]{'}{'},% 139 | }[keywords,comments,strings]% 140 | 141 | 142 | \lstdefinestyle{julia}{% 143 | language=Julia,% 144 | basicstyle=\footnotesize\ttfamily,% 145 | breaklines=true,% 146 | keywordstyle=\bfseries\color{blue},% 147 | identifierstyle=\color{black},% 148 | stringstyle=\color{codegreen},% 149 | commentstyle=\color{codelilas},% 150 | showstringspaces=false,% 151 | upquote=true,% 152 | backgroundcolor=\color{LightGray},% 153 | alsoletter={>},% 154 | moreattributes={julia>}, % 155 | attributestyle = \bfseries\color{green}, % 156 | escapeinside={\%*}{*)}, % if you want to add LaTeX within your code 157 | } 158 | \newcommand\juliastyle{\lstset{style=julia}} 159 | 160 | \newcommand{\juliacode}[1]{% 161 | \juliastyle% 162 | \lstinputlisting{#1} 163 | } 164 | 165 | 166 | % Set up in-line code snippets. 167 | % See http://tex.stackexchange.com/a/42964 and 168 | % http://tex.stackexchange.com/a/83883/50294 169 | \newcommand\bashinline[1]{\colorbox{LightGray}{{\bashstyle\lstinline!#1!}}} 170 | \newcommand\matlabinline[1]{\colorbox{LightGray}{{\matlabstyle\lstinline!#1!}}} 171 | \newcommand\juliainline[1]{\colorbox{LightGray}{{\juliastyle\lstinline!#1!}}} 172 | \newcommand\genericinline[1]{\colorbox{LightGray}{#1}} 173 | 174 | 175 | \title{Macroeconomic Modeling with Julia} 176 | \author{Pearl Li} 177 | \institute[New York Fed]{Federal Reserve Bank of New York} 178 | \date{December 18, 2017} 179 | 180 | \begin{document} 181 | \maketitle 182 | 183 | \section{Intro} 184 | \begin{frame} 185 | \frametitle{Thank yous} 186 | 187 | \begin{itemize} 188 | \setlength\itemsep{1em} 189 | \item Cristina Griffa and workshop organizers 190 | \item DSGE team at the New York Fed 191 | \item John Stachurski, for much of the content on these slides 192 | \item You all for being here 193 | \end{itemize} 194 | \end{frame} 195 | 196 | \begin{frame} 197 | \frametitle{Team} 198 | 199 | \begin{itemize} 200 | \setlength\itemsep{1em} 201 | \item \boldblue{Abhi Gupta}: Senior Research Analyst at New York Fed 202 | \item \boldblue{Pearl Li }: Senior Research Analyst at New York Fed 203 | \end{itemize} 204 | \end{frame} 205 | 206 | \begin{frame} 207 | \frametitle{Goals and Assumptions} 208 | 209 | \vspace{1em} 210 | 211 | \boldblue{Assumptions} 212 | \begin{itemize} 213 | \item Attendees are programmers but new to Julia 214 | \item Interested in macroeconomics 215 | \end{itemize} 216 | 217 | \vspace{1em} 218 | 219 | \boldblue{Goals} 220 | \begin{itemize} 221 | \item Overview of Julia and comparisons to other languages 222 | \item Lower fixed costs to learning Julia 223 | \item Resources for further study 224 | \end{itemize} 225 | 226 | \end{frame} 227 | 228 | \begin{frame} 229 | \frametitle{Schedule} 230 | 231 | \boldblue{Day 1} 232 | % 233 | \begin{itemize} 234 | \item Introduction to Julia (Pearl) 235 | \item Julia syntax (Abhi) 236 | \item Types and multiple dispatch (Pearl) 237 | \end{itemize} 238 | 239 | \vspace{1em} 240 | \boldblue{Day 2} 241 | % 242 | \begin{itemize} 243 | \item Julia for economists -- packages and libraries (Abhi) 244 | \item State-space routines (Abhi) and DSGE.jl (Pearl) 245 | \end{itemize} 246 | \end{frame} 247 | 248 | 249 | \begin{frame} 250 | \frametitle{Resources} 251 | 252 | \begin{center} 253 | \href{http://github.com/FRBNY-DSGE/IADB\_2017\_Workshop}{http://github.com/FRBNY-DSGE/IADB\_2017\_Workshop} 254 | \end{center} 255 | \vspace{2em} 256 | 257 | More resources listed there. 258 | \end{frame} 259 | 260 | 261 | \begin{frame} 262 | \frametitle{Software Options} 263 | 264 | Install 265 | \begin{itemize} 266 | \item Julia 267 | \item Packages (IJulia, QuantEcon.jl, DSGE.jl) 268 | \item IDE if you like (such as Juno) 269 | \end{itemize} 270 | 271 | \vspace{1em} 272 | Or 273 | \begin{itemize} 274 | \item JuliaPro 275 | \end{itemize} 276 | 277 | \vspace{1em} 278 | Or don't install 279 | \begin{itemize} 280 | \item JuliaBox (\href{juliabox.org}{juliabox.org}) TODO 281 | \end{itemize} 282 | 283 | \end{frame} 284 | 285 | \section{Overview} 286 | 287 | \begin{frame} 288 | \frametitle{Scientific Computing} 289 | 290 | Using computation to solve scientific/engineering problems. 291 | 292 | \vspace{1em} 293 | \boldblue{Tasks:} 294 | \begin{itemize} 295 | \item Solve numerical problems 296 | \item Manipulate data 297 | \item Simulate 298 | \item Visualize results with tables and graphs 299 | \item Explore 300 | \end{itemize} 301 | 302 | \pause 303 | \vspace{1em} 304 | And we often want to do these things \boldblue{fast}. 305 | \end{frame} 306 | 307 | \begin{frame} 308 | \frametitle{Speed is nontrivial} 309 | 310 | To get maximum speed, we need: 311 | \begin{itemize} 312 | \item Optimal use of hardware 313 | \item High level of control over operations/logic 314 | \end{itemize} 315 | 316 | \vspace{1em} 317 | First best: \boldblue{assembly language}/machine code. This gives us 318 | instructions at the CPU level, specific to your chip architecture. 319 | 320 | \vspace{1em} 321 | For example: $1 + 2$ 322 | 323 | \end{frame} 324 | 325 | \begin{frame}[fragile] 326 | \frametitle{$1 + 2$} 327 | 328 | \lstset{style=julia} 329 | \begin{lstlisting} 330 | .cfi_startproc 331 | pushq %rbp 332 | .cfi_def_cfa_offset 16 333 | .cfi_offset 6, -16 334 | movq %rsp, %rbp 335 | .cfi_def_cfa_register 6 336 | movl $1, -12(%rbp) 337 | movl $2, -8(%rbp) 338 | movl -12(%rbp), %edx 339 | movl -8(%rbp), %eax 340 | addl %edx, %eax 341 | movl %eax, -4(%rbp) 342 | movl -4(%rbp), %eax 343 | popq %rbp 344 | .cfi_def_cfa 7, 8 345 | ret 346 | .cfi_endproc 347 | \end{lstlisting} 348 | 349 | \end{frame} 350 | 351 | \begin{frame} 352 | Now imagine programming a 3-equation DSGE model like this... \\ 353 | 354 | \vspace{1em} 355 | You would have to optimize for specific hardware: 356 | \begin{itemize} 357 | \item Pipelining 358 | \item Cache hierarchies 359 | \item Branch prediction 360 | \item Multiprocessing 361 | \item etc, etc, etc 362 | \end{itemize} 363 | 364 | \vspace{1em} 365 | \pause 366 | And change it every time there's a new processor. 367 | 368 | \end{frame} 369 | 370 | \begin{frame} 371 | \frametitle{Tradeoffs} 372 | 373 | \boldblue{Low level languages} give us: 374 | \begin{itemize} 375 | \item Speed 376 | \item Fine-grained control 377 | \item Less machine time, more programmer time 378 | \end{itemize} 379 | 380 | \pause 381 | 382 | \vspace{1em} 383 | \boldblue{High level languages} give us: 384 | \begin{itemize} 385 | \item Abstraction (from hardware, pointers, etc) 386 | \item Automation of some tasks (garbage collection) 387 | \item Natural language representations 388 | \item Less programmer time, more machine time 389 | \end{itemize} 390 | 391 | \end{frame} 392 | 393 | \begin{frame} 394 | 395 | \begin{figure} 396 | \includegraphics[width=\textwidth]{languages.jpeg} 397 | \end{figure} 398 | 399 | But the curve is starting to shift... 400 | \end{frame} 401 | 402 | \begin{frame} 403 | 404 | \begin{figure} 405 | \includegraphics[width=\textwidth]{languages_shifted.jpeg} 406 | \end{figure} 407 | \end{frame} 408 | 409 | \begin{frame} 410 | \frametitle{A Horse Race} 411 | 412 | \boldblue{Task:} 413 | \vspace{1em} 414 | \begin{enumerate} 415 | \item Compute $X_1, X_2, \dots, X_n$ according to $X_{t+1} = \beta + \alpha X_{t} + W_{t+1}$, where $W_t \sim N(0,1)$ 416 | \item Calculate and return $ \frac{1}{n} \sum_{t=1}^n X_t $ 417 | \end{enumerate} 418 | 419 | \vspace{1em} 420 | Where $n = 10^7$ 421 | 422 | \end{frame} 423 | 424 | \section{Getting Started With Julia} 425 | \begin{frame} 426 | \frametitle{Julia Overview} 427 | 428 | High-level, open-source, scientific computing language \\ 429 | 430 | \vspace{1em} 431 | \boldblue{Strengths} 432 | \begin{itemize} 433 | \item High productivity 434 | \item And high performance! 435 | \end{itemize} 436 | 437 | \pause 438 | \vspace{1em} 439 | \boldblue{Potential Negatives} 440 | \begin{itemize} 441 | \item Still under rapid development (will slow w/ v1.0) 442 | \item Lots of advanced features either out there or in development: rabbit holes abound 443 | \end{itemize} 444 | 445 | \end{frame} 446 | 447 | \begin{frame} 448 | \frametitle{Why (free and) Open Source?} 449 | 450 | \begin{itemize} 451 | \setlength\itemsep{1em} 452 | 453 | \item Reproducibility with minimal headache: 454 | 455 | \begin{quote} 456 | Let's be clear: the work of science has nothing whatever 457 | to do with consensus. Consensus is the business of 458 | politics. Science, on the contrary, requires only one 459 | investigator who happens to be right, which means that 460 | he or she has results that are veriable by reference to 461 | the real world. In science consensus is irrelevant. What is 462 | relevant is reproducible results. 463 | 464 | \hspace{1em} - Michael Crichton 465 | \end{quote} 466 | \pause 467 | 468 | \item Free! Don't rely on expensive licenses 469 | \end{itemize} 470 | \end{frame} 471 | 472 | \begin{frame} 473 | \frametitle{Interacting with Julia} 474 | 475 | \boldblue{Options:} 476 | 477 | \begin{itemize} 478 | \setlength\itemsep{1em} 479 | \item REPL + text editor (Atom, Sublime, Emacs, etc) 480 | \begin{itemize} 481 | \item Minimal difference from language to language 482 | \item Customize your text editor as much as you want 483 | \end{itemize} 484 | \pause 485 | \item IDEs like Juno 486 | \begin{itemize} 487 | \item Mouse support: buttons, etc 488 | \item Possibly most familiar for MATLAB IDE users 489 | \end{itemize} 490 | \pause 491 | \item Jupyter notebooks 492 | \end{itemize} 493 | \end{frame} 494 | 495 | \begin{frame} 496 | \frametitle{Jupyter Notebooks} 497 | 498 | Browser-based front-end for Python, Julia, R, and increasingly more 499 | 500 | \vspace{1em} 501 | \begin{itemize} 502 | \item Easy to run in the cloud or on servers 503 | \item Allows for rich text and graphics - nice for presenting notes alongside computation 504 | \end{itemize} 505 | \vspace{1em} 506 | Examples: \href{http://notebooks.quantecon.org/}{http://notebooks.quantecon.org/} 507 | 508 | \pause 509 | \vspace{1em} 510 | Let's try one! 511 | \end{frame} 512 | 513 | \end{document} 514 | -------------------------------------------------------------------------------- /syntax/julia_language.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Introduction to Julia Syntax\n", 8 | "\n", 9 | "Abhi Gupta\n", 10 | "\n", 11 | "12/13/2017\n", 12 | "\n", 13 | "Based on Pearl Li's notebook from [QuantEcon's RBA/RBNZ Julia workshops](https://github.com/QuantEcon/RBA_RBNZ_Workshops).\n", 14 | "\n", 15 | "Exercises taken from QuantEcon's [Julia Essentials](https://lectures.quantecon.org/jl/julia_essentials.html) lecture. Check there for further coverage of Julia syntax and features." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "## Syntax Review" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "Most of the syntax covered here will be fairly familiar to users of MATLAB, but is worth covering in one place nonetheless." 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "### Variable Assignment" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "# Assign the value 10 to the variable x\n", 46 | "x = 10" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "# Variable names can have Unicode characters\n", 56 | "# To get ϵ in the REPL, type \\epsilon\n", 57 | "ϵ = 1e-4" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "In Julia, a variable name is just a reference to some data, not the piece of data itself. Multiple names can be associated with the same piece of data, unlike in MATLAB, where the name of a piece of data is bound to the data itself.\n", 65 | "\n", 66 | "Variable names are case-sensitive. By convention, they are in snake_case." 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "### Booleans" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "Equality comparisons:" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "0 == 1" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "2 != 3" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "3 <= 4" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "Boolean operators:" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "true && false" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "true || false" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "!true" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "### Strings" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "# Strings are written using double quotes\n", 158 | "str = \"This is a string\"" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "# Strings can also contain Unicode characters\n", 168 | "fancy_str = \"α is a string\"" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "# String interpolation using $\n", 178 | "# The expression in parentheses is evaluated and the result is \n", 179 | "# inserted into the string\n", 180 | "\"2 + 2 = $(2+2)\"" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "### Functions" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "# Regular function definition\n", 197 | "function double(x)\n", 198 | " y = 2x # scalar multiplication does not need a *\n", 199 | " return y\n", 200 | "end" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "# Inline function definition\n", 210 | "inline_double(x) = 2x" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [ 219 | "# Functions can refer to variables that are in scope when the\n", 220 | "# function is defined\n", 221 | "a = 5\n", 222 | "add_a(x) = x + a\n", 223 | "add_a(1)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "# Functions can return multiple arguments\n", 233 | "duple_of(x) = x, x + 1\n", 234 | "a, b = duple_of(3)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": {}, 241 | "outputs": [], 242 | "source": [ 243 | "# Optional arguments - no more varargin!\n", 244 | "function foo(x, y = 0, override = 0)\n", 245 | " if override == 0\n", 246 | " return x + y\n", 247 | " else\n", 248 | " return override\n", 249 | " end\n", 250 | "end\n", 251 | "\n", 252 | "# Call with one argument\n", 253 | "foo(5)" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [ 262 | "# Call with two arguments\n", 263 | "foo(5, 3)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "# If we want to specify override, we must also specify y\n", 273 | "foo(5, 3, 100)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "metadata": {}, 280 | "outputs": [], 281 | "source": [ 282 | "# Keyword arguments allow arguments to be identified by name\n", 283 | "# instead of only by position\n", 284 | "function join_strings(string1, string2; separator = \",\")\n", 285 | " return string1 * separator * string2\n", 286 | "end\n", 287 | "\n", 288 | "# Call without keyword argument\n", 289 | "join_strings(\"ciao\", \"mondo\")" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": null, 295 | "metadata": {}, 296 | "outputs": [], 297 | "source": [ 298 | "# Call with keyword argument\n", 299 | "join_strings(\"ciao\", \"mondo\"; separator = \" \")" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "### Arrays" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "Explicit array construction:" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "A = [1, 2]" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [ 331 | "B = [1 2 3; 4 5 6]" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": {}, 337 | "source": [ 338 | "One-dimensional arrays `Array{Int64,1}` are also called (type-aliased) `Vector{Int64}`s. Two-dimensional arrays are called `Matrix{Int64}`s.\n", 339 | "\n", 340 | "Note that `A` is a `Vector{Int64}` of length 2, which is distinct from a `Matrix{Int64}` of size $2 \\times 1$ (like a MATLAB \"column vector\") or a `Matrix{Int64}` or size $1 \\times 2$ (\"row vector\")." 341 | ] 342 | }, 343 | { 344 | "cell_type": "markdown", 345 | "metadata": {}, 346 | "source": [ 347 | "Built-in array constructors:" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": null, 353 | "metadata": {}, 354 | "outputs": [], 355 | "source": [ 356 | "zeros(2)" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "metadata": {}, 363 | "outputs": [], 364 | "source": [ 365 | "ones(2)" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": null, 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [ 374 | "eye(2)" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": null, 380 | "metadata": {}, 381 | "outputs": [], 382 | "source": [ 383 | "fill(true, 2)" 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "metadata": {}, 389 | "source": [ 390 | "Matrix operations:" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": null, 396 | "metadata": {}, 397 | "outputs": [], 398 | "source": [ 399 | "# Matrix transpose\n", 400 | "B'" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [ 409 | "# Matrix addition\n", 410 | "B + B" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": null, 416 | "metadata": {}, 417 | "outputs": [], 418 | "source": [ 419 | "# Add a matrix to a vector using broadcasting\n", 420 | "B .+ A" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": null, 426 | "metadata": {}, 427 | "outputs": [], 428 | "source": [ 429 | "# Matrix inverse\n", 430 | "C = 4*eye(2)\n", 431 | "inv(C)" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": null, 437 | "metadata": {}, 438 | "outputs": [], 439 | "source": [ 440 | "# Elementwise operations\n", 441 | "B .> 3" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "metadata": {}, 448 | "outputs": [], 449 | "source": [ 450 | "# Reduce arrays of Booleans to a single element with all() and any()" 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": null, 456 | "metadata": {}, 457 | "outputs": [], 458 | "source": [ 459 | "any(B <. 0)" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": null, 465 | "metadata": {}, 466 | "outputs": [], 467 | "source": [ 468 | "all(B .== 0)" 469 | ] 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "metadata": {}, 474 | "source": [ 475 | "Access array elements using square brackets:" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": null, 481 | "metadata": {}, 482 | "outputs": [], 483 | "source": [ 484 | "# First row of B\n", 485 | "B[1, :]" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": null, 491 | "metadata": {}, 492 | "outputs": [], 493 | "source": [ 494 | "# Element in row 2, column 3 of B\n", 495 | "B[2, 3]" 496 | ] 497 | }, 498 | { 499 | "cell_type": "markdown", 500 | "metadata": {}, 501 | "source": [ 502 | "The last element of an `Array` or similar type can be accessed using the `end` keyword:" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": {}, 509 | "outputs": [], 510 | "source": [ 511 | "A[end]" 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": null, 517 | "metadata": {}, 518 | "outputs": [], 519 | "source": [ 520 | "A[end] == A[2]" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "B[end,:]" 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "execution_count": null, 535 | "metadata": {}, 536 | "outputs": [], 537 | "source": [ 538 | "B[2:end, 2:end]" 539 | ] 540 | }, 541 | { 542 | "cell_type": "markdown", 543 | "metadata": {}, 544 | "source": [ 545 | "### Exercise\n", 546 | "Write a function `check` that takes two sequences `seq_a` and `seq_b` as arguments and returns true if every element in seq_a is also an element of seq_b, else false. By “sequence” we mean an array, tuple or string (many Julia functions work on all three!).\n", 547 | "\n", 548 | "Here is some starter code:" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": null, 554 | "metadata": {}, 555 | "outputs": [], 556 | "source": [ 557 | "function check(seq_a, seq_b)\n", 558 | " # your code here\n", 559 | " return \n", 560 | "end" 561 | ] 562 | }, 563 | { 564 | "cell_type": "markdown", 565 | "metadata": {}, 566 | "source": [ 567 | "### Control Flow" 568 | ] 569 | }, 570 | { 571 | "cell_type": "markdown", 572 | "metadata": {}, 573 | "source": [ 574 | "If statements:" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": null, 580 | "metadata": {}, 581 | "outputs": [], 582 | "source": [ 583 | "x = -3\n", 584 | "if x < 0\n", 585 | " println(\"x is negative\")\n", 586 | "elseif x > 0 # optional and unlimited\n", 587 | " println(\"x is positive\")\n", 588 | "else # optional\n", 589 | " println(\"x is zero\")\n", 590 | "end" 591 | ] 592 | }, 593 | { 594 | "cell_type": "markdown", 595 | "metadata": {}, 596 | "source": [ 597 | "While loops:" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": null, 603 | "metadata": {}, 604 | "outputs": [], 605 | "source": [ 606 | "i = 3\n", 607 | "while i > 0\n", 608 | " println(i)\n", 609 | " i = i - 1\n", 610 | "end" 611 | ] 612 | }, 613 | { 614 | "cell_type": "markdown", 615 | "metadata": {}, 616 | "source": [ 617 | "For loops:" 618 | ] 619 | }, 620 | { 621 | "cell_type": "code", 622 | "execution_count": null, 623 | "metadata": {}, 624 | "outputs": [], 625 | "source": [ 626 | "# Iterate through ranges of numbers\n", 627 | "for i = 1:3\n", 628 | " println(i)\n", 629 | "end" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": null, 635 | "metadata": {}, 636 | "outputs": [], 637 | "source": [ 638 | "# Iterate through arrays\n", 639 | "cities = [\"Buenos Aires\", \"New York\", \"Toronto\"]\n", 640 | "for city in cities\n", 641 | " println(city)\n", 642 | "end" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": null, 648 | "metadata": {}, 649 | "outputs": [], 650 | "source": [ 651 | "# Iterate through arrays of tuples using zip\n", 652 | "countries = [\"Argentina\", \"USA\", \"Canada\"]\n", 653 | "for (city, country) in zip(cities, countries)\n", 654 | " println(\"$city, $countries\")\n", 655 | "end" 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "execution_count": null, 661 | "metadata": {}, 662 | "outputs": [], 663 | "source": [ 664 | "# Iterate through arrays and their indices using enumerate\n", 665 | "for (i, city) in enumerate(cities)\n", 666 | " println(\"City $i is $city\")\n", 667 | "end" 668 | ] 669 | }, 670 | { 671 | "cell_type": "markdown", 672 | "metadata": {}, 673 | "source": [ 674 | "### Exercises\n", 675 | "1. Consider the polynomial $$p(x) = \\sum_{i=0}^n a_i x^i$$ Using `enumerate`, write a function `p` such that `p(x, coeff)` computes the value of the polynomial with coefficients `coeff` evaluated at `x`.\n", 676 | "\n", 677 | "2. Write a function that takes two 1-d arrays `x` and `y` and computes their inner product using `zip`.\n", 678 | "\n", 679 | "3. Write a function linapprox that takes as arguments:\n", 680 | "\n", 681 | " - A function f mapping some interval [a,b] into ℝ\n", 682 | " - Two scalars a and b providing the limits of this interval\n", 683 | " - An integer n determining the number of grid points\n", 684 | " - A number x satisfying a≤x≤b\n", 685 | "\n", 686 | " and returns the piecewise linear interpolation of f at x, based on n evenly spaced grid points a = point[1] < point[2] < ... < point[n] = b. Aim for clarity, not efficiency.\n" 687 | ] 688 | }, 689 | { 690 | "cell_type": "code", 691 | "execution_count": null, 692 | "metadata": {}, 693 | "outputs": [], 694 | "source": [] 695 | } 696 | ], 697 | "metadata": { 698 | "celltoolbar": "Raw Cell Format", 699 | "kernelspec": { 700 | "display_name": "Julia 0.6.1", 701 | "language": "julia", 702 | "name": "julia-0.6" 703 | }, 704 | "language_info": { 705 | "file_extension": ".jl", 706 | "mimetype": "application/julia", 707 | "name": "julia", 708 | "version": "0.6.1" 709 | } 710 | }, 711 | "nbformat": 4, 712 | "nbformat_minor": 1 713 | } 714 | -------------------------------------------------------------------------------- /packages/optimization_solvers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true, 7 | "slideshow": { 8 | "slide_type": "slide" 9 | } 10 | }, 11 | "source": [ 12 | "# Julia Workshop: Optimization and Solvers\n", 13 | "\n", 14 | "Based on a notebook by Chase Coleman and Spencer Lyon and on material from QuantEcon\n", 15 | "\n", 16 | "18 December 2017" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Goal\n", 24 | "\n", 25 | "A few different packages used for optimization or non-linear equation solving.\n" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": { 31 | "slideshow": { 32 | "slide_type": "slide" 33 | } 34 | }, 35 | "source": [ 36 | "## QuantEcon\n", 37 | "\n", 38 | "The QuantEcon library has a few simple optimizers and solvers. See the economics [notebook](economics.ipynb) for more information." 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 1, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# Pkg.add(\"QuantEcon\")" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "slideshow": { 54 | "slide_type": "slide" 55 | } 56 | }, 57 | "source": [ 58 | "## Optim.jl\n", 59 | "\n", 60 | "Package for optimizing written in pure Julia\n", 61 | "\n", 62 | "[Documentation](http://julianlsolvers.github.io/Optim.jl/stable/)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 2, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "# Pkg.add(\"Optim\")" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 3, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "using Optim" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 4, 86 | "metadata": { 87 | "slideshow": { 88 | "slide_type": "-" 89 | } 90 | }, 91 | "outputs": [ 92 | { 93 | "data": { 94 | "text/plain": [ 95 | "rosenbrock (generic function with 1 method)" 96 | ] 97 | }, 98 | "execution_count": 4, 99 | "metadata": {}, 100 | "output_type": "execute_result" 101 | } 102 | ], 103 | "source": [ 104 | "rosenbrock(x::Vector) = (1.0 - x[1])^2 + 100.0*(x[2] - x[1]^2)^2" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": { 110 | "slideshow": { 111 | "slide_type": "subslide" 112 | } 113 | }, 114 | "source": [ 115 | "### Optimizing without gradient" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 5, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "Results of Optimization Algorithm\n", 127 | " * Algorithm: Nelder-Mead\n", 128 | " * Starting Point: [0.0,0.0]\n", 129 | " * Minimizer: [0.9999634355313174,0.9999315506115275]\n", 130 | " * Minimum: 3.525527e-09\n", 131 | " * Iterations: 60\n", 132 | " * Convergence: true\n", 133 | " * √(Σ(yᵢ-ȳ)²)/n < 1.0e-08: true\n", 134 | " * Reached Maximum Number of Iterations: false\n", 135 | " * Objective Calls: 117" 136 | ] 137 | }, 138 | "execution_count": 5, 139 | "metadata": {}, 140 | "output_type": "execute_result" 141 | } 142 | ], 143 | "source": [ 144 | "optimize(rosenbrock, zeros(2), NelderMead())" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 6, 150 | "metadata": { 151 | "slideshow": { 152 | "slide_type": "fragment" 153 | } 154 | }, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "Results of Optimization Algorithm\n", 160 | " * Algorithm: BFGS\n", 161 | " * Starting Point: [0.0,0.0]\n", 162 | " * Minimizer: [0.9999999926033423,0.9999999852005353]\n", 163 | " * Minimum: 5.471433e-17\n", 164 | " * Iterations: 16\n", 165 | " * Convergence: true\n", 166 | " * |x - x'| < 1.0e-32: false \n", 167 | " |x - x'| = 3.47e-07 \n", 168 | " * |f(x) - f(x')| / |f(x)| < 1.0e-32: false\n", 169 | " |f(x) - f(x')| / |f(x)| = 1.20e+03 \n", 170 | " * |g(x)| < 1.0e-08: true \n", 171 | " |g(x)| = 2.33e-09 \n", 172 | " * Stopped by an increasing objective: false\n", 173 | " * Reached Maximum Number of Iterations: false\n", 174 | " * Objective Calls: 53\n", 175 | " * Gradient Calls: 53" 176 | ] 177 | }, 178 | "execution_count": 6, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "optimize(rosenbrock, zeros(2), BFGS())" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": { 190 | "slideshow": { 191 | "slide_type": "subslide" 192 | } 193 | }, 194 | "source": [ 195 | "### Optimizing with gradient" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 7, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "rosenbrock_grad! (generic function with 1 method)" 207 | ] 208 | }, 209 | "execution_count": 7, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "function rosenbrock_grad!(grad::Vector, x::Vector)\n", 216 | " grad[1] = -2.0*(1.0 - x[1]) - 400.0*(x[2] - x[1]^2)*x[1]\n", 217 | " grad[2] = 200.0*(x[2] - x[1]^2)\n", 218 | "end" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 8, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "Results of Optimization Algorithm\n", 230 | " * Algorithm: L-BFGS\n", 231 | " * Starting Point: [0.0,0.0]\n", 232 | " * Minimizer: [0.999999999999928,0.9999999999998559]\n", 233 | " * Minimum: 5.191703e-27\n", 234 | " * Iterations: 24\n", 235 | " * Convergence: true\n", 236 | " * |x - x'| < 1.0e-10: true \n", 237 | " |x - x'| = 4.58e-11 \n", 238 | " * |f(x) - f(x')| / |f(x)| < 1.0e-09: true\n", 239 | " |f(x) - f(x')| / |f(x)| = 4.41e-10 \n", 240 | " * |g(x)| < 1.0e-08: true \n", 241 | " |g(x)| = 1.44e-13 \n", 242 | " * Stopped by an increasing objective: false\n", 243 | " * Reached Maximum Number of Iterations: false\n", 244 | " * Objective Calls: 67\n", 245 | " * Gradient Calls: 67" 246 | ] 247 | }, 248 | "execution_count": 8, 249 | "metadata": {}, 250 | "output_type": "execute_result" 251 | } 252 | ], 253 | "source": [ 254 | "optimize(rosenbrock, rosenbrock_grad!, zeros(2), LBFGS(),\n", 255 | " Optim.Options(x_tol=1e-10, f_tol=1e-9, iterations=25000,\n", 256 | " allow_f_increases=true))" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 9, 262 | "metadata": { 263 | "slideshow": { 264 | "slide_type": "fragment" 265 | } 266 | }, 267 | "outputs": [ 268 | { 269 | "data": { 270 | "text/plain": [ 271 | "Results of Optimization Algorithm\n", 272 | " * Algorithm: Gradient Descent\n", 273 | " * Starting Point: [0.0,0.0]\n", 274 | " * Minimizer: [0.9999999572646519,0.9999999142802773]\n", 275 | " * Minimum: 1.832511e-15\n", 276 | " * Iterations: 18460\n", 277 | " * Convergence: true\n", 278 | " * |x - x'| < 1.0e-10: true \n", 279 | " |x - x'| = 9.99e-11 \n", 280 | " * |f(x) - f(x')| / |f(x)| < 1.0e-09: false\n", 281 | " |f(x) - f(x')| / |f(x)| = 2.95e-09 \n", 282 | " * |g(x)| < 1.0e-08: false \n", 283 | " |g(x)| = 4.98e-08 \n", 284 | " * Stopped by an increasing objective: false\n", 285 | " * Reached Maximum Number of Iterations: false\n", 286 | " * Objective Calls: 46182\n", 287 | " * Gradient Calls: 46182" 288 | ] 289 | }, 290 | "execution_count": 9, 291 | "metadata": {}, 292 | "output_type": "execute_result" 293 | } 294 | ], 295 | "source": [ 296 | "optimize(rosenbrock, rosenbrock_grad!, zeros(2), GradientDescent(),\n", 297 | " Optim.Options(x_tol=1e-10, f_tol=1e-9, iterations=25000,\n", 298 | " allow_f_increases=true))" 299 | ] 300 | }, 301 | { 302 | "cell_type": "markdown", 303 | "metadata": { 304 | "slideshow": { 305 | "slide_type": "subslide" 306 | } 307 | }, 308 | "source": [ 309 | "### Optimizing with Hessian" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 10, 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "data": { 319 | "text/plain": [ 320 | "rosenbrock_hess! (generic function with 1 method)" 321 | ] 322 | }, 323 | "execution_count": 10, 324 | "metadata": {}, 325 | "output_type": "execute_result" 326 | } 327 | ], 328 | "source": [ 329 | "function rosenbrock_hess!(hess::Matrix, x::Vector)\n", 330 | " hess[1, 1] = 2.0 - 400.0 * x[2] + 1200.0*x[1]^2\n", 331 | " hess[1, 2] = -400.0 * x[1]\n", 332 | " hess[2, 1] = -400.0 * x[1]\n", 333 | " hess[2, 2] = 200.0\n", 334 | "end" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 11, 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "data": { 344 | "text/plain": [ 345 | "Results of Optimization Algorithm\n", 346 | " * Algorithm: Newton's Method\n", 347 | " * Starting Point: [0.0,0.0]\n", 348 | " * Minimizer: [0.9999999999999994,0.9999999999999989]\n", 349 | " * Minimum: 3.081488e-31\n", 350 | " * Iterations: 14\n", 351 | " * Convergence: true\n", 352 | " * |x - x'| < 1.0e-32: false \n", 353 | " |x - x'| = 3.06e-09 \n", 354 | " * |f(x) - f(x')| / |f(x)| < 1.0e-32: false\n", 355 | " |f(x) - f(x')| / |f(x)| = 2.94e+13 \n", 356 | " * |g(x)| < 1.0e-08: true \n", 357 | " |g(x)| = 1.11e-15 \n", 358 | " * Stopped by an increasing objective: false\n", 359 | " * Reached Maximum Number of Iterations: false\n", 360 | " * Objective Calls: 44\n", 361 | " * Gradient Calls: 44\n", 362 | " * Hessian Calls: 14" 363 | ] 364 | }, 365 | "execution_count": 11, 366 | "metadata": {}, 367 | "output_type": "execute_result" 368 | } 369 | ], 370 | "source": [ 371 | "optimize(rosenbrock, rosenbrock_grad!, rosenbrock_hess!, zeros(2), Newton())" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": { 377 | "slideshow": { 378 | "slide_type": "slide" 379 | } 380 | }, 381 | "source": [ 382 | "## NLopt\n", 383 | "\n", 384 | "Julia wrapper to high quality C library.\n", 385 | "\n", 386 | "This library has _lots_ of options for algorithms. See [list](http://ab-initio.mit.edu/wiki/index.php/NLopt_Algorithms) of algorithms\n", 387 | "\n", 388 | "[Julia package](https://github.com/JuliaOpt/NLopt.jl) and [C Documentation](http://ab-initio.mit.edu/wiki/index.php/NLopt)\n", 389 | "\n", 390 | "Note that functions optimized with NLopt have a different ordering of arguments compared to our previous examples using Optim.jl" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 12, 396 | "metadata": {}, 397 | "outputs": [], 398 | "source": [ 399 | "# Pkg.add(\"NLopt\")" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 13, 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "name": "stderr", 409 | "output_type": "stream", 410 | "text": [ 411 | "WARNING: using NLopt.optimize in module Main conflicts with an existing identifier.\n" 412 | ] 413 | } 414 | ], 415 | "source": [ 416 | "using NLopt" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 14, 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "data": { 426 | "text/plain": [ 427 | "rosenbrock_nlopt (generic function with 1 method)" 428 | ] 429 | }, 430 | "execution_count": 14, 431 | "metadata": {}, 432 | "output_type": "execute_result" 433 | } 434 | ], 435 | "source": [ 436 | "function rosenbrock_nlopt(x::Vector, grad::Vector)\n", 437 | " if length(grad) > 0\n", 438 | " rosenbrock_grad!(grad, x)\n", 439 | " end\n", 440 | "\n", 441 | " return rosenbrock(x)\n", 442 | "end" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": { 448 | "slideshow": { 449 | "slide_type": "subslide" 450 | } 451 | }, 452 | "source": [ 453 | "### A non-gradient and gradient based method" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 15, 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "data": { 463 | "text/plain": [ 464 | "(3.251384063527492e-21, [1.0, 1.0], :SUCCESS)" 465 | ] 466 | }, 467 | "execution_count": 15, 468 | "metadata": {}, 469 | "output_type": "execute_result" 470 | } 471 | ], 472 | "source": [ 473 | "opt_LBFGS = Opt(:LD_LBFGS, 2)\n", 474 | "\n", 475 | "min_objective!(opt_LBFGS, rosenbrock_nlopt)\n", 476 | "xtol_rel!(opt_LBFGS, 1e-10)\n", 477 | "ftol_rel!(opt_LBFGS, 1e-9)\n", 478 | "\n", 479 | "NLopt.optimize(opt_LBFGS, zeros(2))" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 16, 485 | "metadata": { 486 | "slideshow": { 487 | "slide_type": "fragment" 488 | } 489 | }, 490 | "outputs": [ 491 | { 492 | "data": { 493 | "text/plain": [ 494 | "(5.231133877746835e-29, [1.0, 1.0], :SUCCESS)" 495 | ] 496 | }, 497 | "execution_count": 16, 498 | "metadata": {}, 499 | "output_type": "execute_result" 500 | } 501 | ], 502 | "source": [ 503 | "opt_PRAXIS = Opt(:LN_PRAXIS, 2)\n", 504 | "\n", 505 | "min_objective!(opt_PRAXIS, rosenbrock_nlopt)\n", 506 | "\n", 507 | "NLopt.optimize(opt_PRAXIS, zeros(2))" 508 | ] 509 | }, 510 | { 511 | "cell_type": "markdown", 512 | "metadata": { 513 | "slideshow": { 514 | "slide_type": "subslide" 515 | } 516 | }, 517 | "source": [ 518 | "### A constrained optimization problem\n", 519 | "\n", 520 | "\\begin{align*}\n", 521 | " \\min_{x_1, x_2} &\\sqrt{x_2} \\\\\n", 522 | " &\\text{subject to } \\\\\n", 523 | " &x_2 \\geq 0 \\\\\n", 524 | " &x_2 \\geq (2 x_1)^3 \\\\\n", 525 | " &x_2 \\geq (-x_1 + 1)^3\n", 526 | "\\end{align*}" 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 17, 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "data": { 536 | "text/plain": [ 537 | "myconstraint (generic function with 1 method)" 538 | ] 539 | }, 540 | "execution_count": 17, 541 | "metadata": {}, 542 | "output_type": "execute_result" 543 | } 544 | ], 545 | "source": [ 546 | "function myfunc(x::Vector, grad::Vector)\n", 547 | " if length(grad) > 0\n", 548 | " grad[1] = 0\n", 549 | " grad[2] = 0.5/sqrt(x[2])\n", 550 | " end\n", 551 | "\n", 552 | " return sqrt(x[1] + x[2])\n", 553 | "end\n", 554 | "\n", 555 | "function myconstraint(x::Vector, grad::Vector, a, b)\n", 556 | " if length(grad) > 0\n", 557 | " grad[1] = 3a * (a*x[1] + b)^2\n", 558 | " grad[2] = -1\n", 559 | " end\n", 560 | "\n", 561 | " return (a*x[1] + b)^3 - x[2]\n", 562 | "end" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": 18, 568 | "metadata": { 569 | "slideshow": { 570 | "slide_type": "fragment" 571 | } 572 | }, 573 | "outputs": [ 574 | { 575 | "name": "stdout", 576 | "output_type": "stream", 577 | "text": [ 578 | "got 0.7934920514599207 at [0.333333, 0.296296] (returned XTOL_REACHED)\n" 579 | ] 580 | } 581 | ], 582 | "source": [ 583 | "opt = Opt(:LD_MMA, 2)\n", 584 | "\n", 585 | "lower_bounds!(opt, [-Inf, 0.])\n", 586 | "xtol_rel!(opt, 1e-6)\n", 587 | "\n", 588 | "min_objective!(opt, myfunc)\n", 589 | "\n", 590 | "inequality_constraint!(opt, (x,g) -> myconstraint(x, g, 2.0, 0.0), 1e-8)\n", 591 | "inequality_constraint!(opt, (x,g) -> myconstraint(x, g, -1.0 ,1.0), 1e-8)\n", 592 | "\n", 593 | "(minf, minx, ret) = NLopt.optimize(opt, [1.234, 5.678])\n", 594 | "\n", 595 | "println(\"got $minf at $minx (returned $ret)\")" 596 | ] 597 | }, 598 | { 599 | "cell_type": "markdown", 600 | "metadata": { 601 | "collapsed": true, 602 | "slideshow": { 603 | "slide_type": "subslide" 604 | } 605 | }, 606 | "source": [ 607 | "## NLsolve\n", 608 | "\n", 609 | "Julia package written to solve systems of non-linear equations\n", 610 | "\n", 611 | "[Documentation](https://github.com/JuliaNLSolvers/NLsolve.jl)" 612 | ] 613 | }, 614 | { 615 | "cell_type": "code", 616 | "execution_count": 19, 617 | "metadata": {}, 618 | "outputs": [], 619 | "source": [ 620 | "# Pkg.add(\"NLsolve\")" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 20, 626 | "metadata": {}, 627 | "outputs": [], 628 | "source": [ 629 | "using NLsolve" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": 21, 635 | "metadata": {}, 636 | "outputs": [ 637 | { 638 | "data": { 639 | "text/plain": [ 640 | "g! (generic function with 1 method)" 641 | ] 642 | }, 643 | "execution_count": 21, 644 | "metadata": {}, 645 | "output_type": "execute_result" 646 | } 647 | ], 648 | "source": [ 649 | "function f!(xy::Vector, fxy::Vector)\n", 650 | " # Pull out arguments\n", 651 | " x, y = xy\n", 652 | "\n", 653 | " # Fill fxy\n", 654 | " fxy[1] = x^2 - sin(y)\n", 655 | " fxy[2] = y^2 - cos(x)\n", 656 | "end\n", 657 | "\n", 658 | "function g!(xy::Vector, jacxy::Matrix)\n", 659 | " x, y = xy\n", 660 | " # Fill with derivatives of first function\n", 661 | " jacxy[1, 1] = 2*x\n", 662 | " jacxy[1, 2] = -cos(y)\n", 663 | "\n", 664 | " # Fill off-diagonal\n", 665 | " jacxy[2, 1] = sin(x)\n", 666 | " jacxy[2, 2] = 2*y\n", 667 | "end" 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "execution_count": 22, 673 | "metadata": { 674 | "slideshow": { 675 | "slide_type": "fragment" 676 | } 677 | }, 678 | "outputs": [ 679 | { 680 | "data": { 681 | "text/plain": [ 682 | "Results of Nonlinear Solver Algorithm\n", 683 | " * Algorithm: Trust-region with dogleg and autoscaling\n", 684 | " * Starting Point: [0.5, 0.5]\n", 685 | " * Zero: [0.8517, 0.811606]\n", 686 | " * Inf-norm of residuals: 0.000000\n", 687 | " * Iterations: 5\n", 688 | " * Convergence: true\n", 689 | " * |x - x'| < 0.0e+00: false\n", 690 | " * |f(x)| < 1.0e-08: true\n", 691 | " * Function Calls (f): 6\n", 692 | " * Jacobian Calls (df/dx): 6" 693 | ] 694 | }, 695 | "execution_count": 22, 696 | "metadata": {}, 697 | "output_type": "execute_result" 698 | } 699 | ], 700 | "source": [ 701 | "res = nlsolve(f!, g!, [0.5, 0.5], ftol=1e-8)" 702 | ] 703 | }, 704 | { 705 | "cell_type": "code", 706 | "execution_count": null, 707 | "metadata": {}, 708 | "outputs": [], 709 | "source": [] 710 | } 711 | ], 712 | "metadata": { 713 | "anaconda-cloud": {}, 714 | "kernelspec": { 715 | "display_name": "Julia 0.6.1", 716 | "language": "julia", 717 | "name": "julia-0.6" 718 | }, 719 | "language_info": { 720 | "file_extension": ".jl", 721 | "mimetype": "application/julia", 722 | "name": "julia", 723 | "version": "0.6.1" 724 | }, 725 | "livereveal": { 726 | "scroll": true, 727 | "start_slideshow_at": "selected", 728 | "theme": "white", 729 | "transition": "fade" 730 | } 731 | }, 732 | "nbformat": 4, 733 | "nbformat_minor": 2 734 | } 735 | -------------------------------------------------------------------------------- /packages/numerics_stats.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Numerical and statistical tools\n", 12 | "\n", 13 | "Based on a notebook by Chase Coleman and Spencer Lyon and on material from QuantEcon\n", 14 | "\n", 15 | "18 December 2017" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "slideshow": { 22 | "slide_type": "fragment" 23 | } 24 | }, 25 | "source": [ 26 | "This notebook covers a selection of additional packages, covering topics in statistics, data handling, and other areas of interest.\n", 27 | "\n", 28 | "Anyone interested in autodiff can look at [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) and [ReverseDiff.jl](https://github.com/JuliaDiff/ReverseDiff.jl)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "slideshow": { 35 | "slide_type": "slide" 36 | } 37 | }, 38 | "source": [ 39 | "## Distributions.jl\n", 40 | "\n", 41 | "Provides routines for working with probability distributions and...\n", 42 | " - Computing moments/statistics: mean, median, mode, entropy, mgf, quantile\n", 43 | " - Probability evaluation: pdf, cdf, ccdf, quantile, invlogcdf\n", 44 | " - Sampling: rand and rand!\n", 45 | "\n", 46 | "[Documentation](http://distributionsjl.readthedocs.io/en/latest/)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 27, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "# Pkg.add(\"Distributions\")" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": { 63 | "slideshow": { 64 | "slide_type": "subslide" 65 | } 66 | }, 67 | "source": [ 68 | "### Distributions.jl Basics" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 1, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "using Distributions" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 29, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "67" 89 | ] 90 | }, 91 | "execution_count": 29, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "# all subtypes of `Distributions.Distribution`\n", 98 | "length(subtypes(Distribution))" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 2, 104 | "metadata": { 105 | "slideshow": { 106 | "slide_type": "fragment" 107 | } 108 | }, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "search: \u001b[1mN\u001b[22m\u001b[1mo\u001b[22m\u001b[1mr\u001b[22m\u001b[1mm\u001b[22m\u001b[1ma\u001b[22m\u001b[1ml\u001b[22m \u001b[1mn\u001b[22m\u001b[1mo\u001b[22m\u001b[1mr\u001b[22m\u001b[1mm\u001b[22m\u001b[1ma\u001b[22m\u001b[1ml\u001b[22mize \u001b[1mn\u001b[22m\u001b[1mo\u001b[22m\u001b[1mr\u001b[22m\u001b[1mm\u001b[22m\u001b[1ma\u001b[22m\u001b[1ml\u001b[22mize! \u001b[1mN\u001b[22m\u001b[1mo\u001b[22m\u001b[1mr\u001b[22m\u001b[1mm\u001b[22m\u001b[1ma\u001b[22m\u001b[1ml\u001b[22mCanon \u001b[1mn\u001b[22m\u001b[1mo\u001b[22m\u001b[1mr\u001b[22m\u001b[1mm\u001b[22m\u001b[1ma\u001b[22m\u001b[1ml\u001b[22mize_string\n", 115 | "\n" 116 | ] 117 | }, 118 | { 119 | "data": { 120 | "text/markdown": [ 121 | "```\n", 122 | "Normal(μ,σ)\n", 123 | "```\n", 124 | "\n", 125 | "The *Normal distribution* with mean `μ` and standard deviation `σ` has probability density function\n", 126 | "\n", 127 | "$$\n", 128 | "f(x; \\mu, \\sigma) = \\frac{1}{\\sqrt{2 \\pi \\sigma^2}}\n", 129 | "\\exp \\left( - \\frac{(x - \\mu)^2}{2 \\sigma^2} \\right)\n", 130 | "$$\n", 131 | "\n", 132 | "```julia\n", 133 | "Normal() # standard Normal distribution with zero mean and unit variance\n", 134 | "Normal(mu) # Normal distribution with mean mu and unit variance\n", 135 | "Normal(mu, sig) # Normal distribution with mean mu and variance sig^2\n", 136 | "\n", 137 | "params(d) # Get the parameters, i.e. (mu, sig)\n", 138 | "mean(d) # Get the mean, i.e. mu\n", 139 | "std(d) # Get the standard deviation, i.e. sig\n", 140 | "```\n", 141 | "\n", 142 | "External links\n", 143 | "\n", 144 | " * [Normal distribution on Wikipedia](http://en.wikipedia.org/wiki/Normal_distribution)\n" 145 | ], 146 | "text/plain": [ 147 | "```\n", 148 | "Normal(μ,σ)\n", 149 | "```\n", 150 | "\n", 151 | "The *Normal distribution* with mean `μ` and standard deviation `σ` has probability density function\n", 152 | "\n", 153 | "$$\n", 154 | "f(x; \\mu, \\sigma) = \\frac{1}{\\sqrt{2 \\pi \\sigma^2}}\n", 155 | "\\exp \\left( - \\frac{(x - \\mu)^2}{2 \\sigma^2} \\right)\n", 156 | "$$\n", 157 | "\n", 158 | "```julia\n", 159 | "Normal() # standard Normal distribution with zero mean and unit variance\n", 160 | "Normal(mu) # Normal distribution with mean mu and unit variance\n", 161 | "Normal(mu, sig) # Normal distribution with mean mu and variance sig^2\n", 162 | "\n", 163 | "params(d) # Get the parameters, i.e. (mu, sig)\n", 164 | "mean(d) # Get the mean, i.e. mu\n", 165 | "std(d) # Get the standard deviation, i.e. sig\n", 166 | "```\n", 167 | "\n", 168 | "External links\n", 169 | "\n", 170 | " * [Normal distribution on Wikipedia](http://en.wikipedia.org/wiki/Normal_distribution)\n" 171 | ] 172 | }, 173 | "execution_count": 2, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "?Normal # good documentation" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 3, 185 | "metadata": { 186 | "scrolled": false, 187 | "slideshow": { 188 | "slide_type": "fragment" 189 | } 190 | }, 191 | "outputs": [ 192 | { 193 | "name": "stdout", 194 | "output_type": "stream", 195 | "text": [ 196 | "Working with distribution: Distributions.Normal{Float64}(μ=0.0, σ=1.0)\n", 197 | "mean(d) = 0.0\n", 198 | "rand(d, 2, 2) = [1.13714 -0.183199; -0.552657 -0.821711]\n", 199 | "pdf(d, rand(d)) = 0.3181295845549784\n", 200 | "\n", 201 | "\n", 202 | "\n", 203 | "\n", 204 | "Working with distribution: Distributions.Beta{Float64}(α=1.0, β=2.0)\n", 205 | "mean(d) = 0.3333333333333333\n", 206 | "rand(d, 2, 2) = [0.170901 0.0361851; 0.204202 0.739539]\n", 207 | "pdf(d, rand(d)) = 1.3037006671046503\n", 208 | "\n", 209 | "\n", 210 | "\n", 211 | "\n", 212 | "Working with distribution: Distributions.Chisq{Float64}(ν=5.0)\n", 213 | "mean(d) = 5.0\n", 214 | "rand(d, 2, 2) = [5.50546 4.6375; 12.0028 3.93241]\n", 215 | "pdf(d, rand(d)) = 0.13638158972826442\n", 216 | "\n", 217 | "\n", 218 | "\n", 219 | "\n", 220 | "Working with distribution: Distributions.Frechet{Float64}(α=5.0, θ=2.0)\n", 221 | "mean(d) = 2.3284594274506065\n", 222 | "rand(d, 2, 2) = [1.75435 1.72247; 1.66146 1.67428]\n", 223 | "pdf(d, rand(d)) = 0.8873754496107239\n", 224 | "\n", 225 | "\n", 226 | "\n", 227 | "\n", 228 | "Working with distribution: Distributions.Gamma{Float64}(α=1.0, θ=2.0)\n", 229 | "mean(d) = 2.0\n", 230 | "rand(d, 2, 2) = [0.0447799 0.431371; 1.7511 2.16835]\n", 231 | "pdf(d, rand(d)) = 0.002599582677349569\n", 232 | "\n", 233 | "\n", 234 | "\n", 235 | "\n", 236 | "Working with distribution: Distributions.Pareto{Float64}(α=3.0, θ=2.0)\n", 237 | "mean(d) = 3.0\n", 238 | "rand(d, 2, 2) = [3.70509 2.30255; 3.91045 2.75899]\n", 239 | "pdf(d, rand(d)) = 0.3774305941945478\n", 240 | "\n", 241 | "\n", 242 | "\n", 243 | "\n", 244 | "Working with distribution: Distributions.Binomial{Float64}(n=10, p=0.6)\n", 245 | "mean(d) = 6.0\n", 246 | "rand(d, 2, 2) = [8 5; 6 6]\n", 247 | "pdf(d, rand(d)) = 0.250822656\n", 248 | "\n", 249 | "\n", 250 | "\n", 251 | "\n", 252 | "Working with distribution: Distributions.Poisson{Float64}(λ=0.7)\n", 253 | "mean(d) = 0.7\n", 254 | "rand(d, 2, 2) = [1 1; 0 1]\n", 255 | "pdf(d, rand(d)) = 0.4965853037914095\n", 256 | "\n", 257 | "\n", 258 | "\n", 259 | "\n", 260 | "Working with distribution: Distributions.MvLogNormal{Float64,PDMats.PDMat{Float64,Array{Float64,2}},Array{Float64,1}}(\n", 261 | "dim: 2\n", 262 | "μ: [1.0, 1.0]\n", 263 | "Σ: [3.0 0.0; 0.0 3.0]\n", 264 | ")\n", 265 | "\n", 266 | "mean(d) = [12.1825, 12.1825]\n", 267 | "rand(d, 2) = [52.1449 2.48705; 2.99419 1.44291]\n", 268 | "pdf(d, rand(d)) = 0.0038941056157832696\n", 269 | "\n", 270 | "\n", 271 | "\n", 272 | "\n", 273 | "Working with distribution: Distributions.Dirichlet{Float64}(alpha=[0.1, 0.2, 0.3, 0.4])\n", 274 | "mean(d) = [0.1, 0.2, 0.3, 0.4]\n", 275 | "rand(d, 2) = [0.420241 4.2805e-6; 0.0152342 5.05126e-8; 0.0506917 0.708622; 0.513833 0.291374]\n", 276 | "pdf(d, rand(d)) = 15.280018101140051\n", 277 | "\n", 278 | "\n", 279 | "\n", 280 | "\n", 281 | "Working with distribution: Distributions.InverseWishart{Float64,PDMats.PDMat{Float64,Array{Float64,2}}}(\n", 282 | "df: 5.0\n", 283 | "Ψ: [1.0 0.0; 0.0 1.0]\n", 284 | ")\n", 285 | "\n", 286 | "mean(d) = [0.5 0.0; 0.0 0.5]\n", 287 | "rand(d, 2) = Array{Float64,2}[[5.39938 -0.590012; -0.590012 0.172586], [0.216843 -0.0374544; -0.0374544 0.360345]]\n", 288 | "pdf(d, rand(d)) = 53.771062472118956\n", 289 | "\n", 290 | "\n", 291 | "\n", 292 | "\n", 293 | "Working with distribution: MixtureModel{Distributions.Normal}(K = 3)\n", 294 | "components[1] (prior = 0.2000): Distributions.Normal{Float64}(μ=-2.0, σ=1.2)\n", 295 | "components[2] (prior = 0.5000): Distributions.Normal{Float64}(μ=0.0, σ=1.0)\n", 296 | "components[3] (prior = 0.3000): Distributions.Normal{Float64}(μ=3.0, σ=2.5)\n", 297 | "\n", 298 | "mean(d) = 0.4999999999999999\n", 299 | "rand(d, 2, 2) = [-1.01935 3.66368; 1.33084 -3.6402]\n", 300 | "pdf(d, rand(d)) = 0.04670318763847431\n", 301 | "\n", 302 | "\n", 303 | "\n", 304 | "\n" 305 | ] 306 | } 307 | ], 308 | "source": [ 309 | "dists = [\n", 310 | " Normal(0, 1),\n", 311 | " Beta(1.0, 2.0),\n", 312 | " Chisq(5),\n", 313 | " Frechet(5.0, 2.0),\n", 314 | " Gamma(1.0, 2.0),\n", 315 | " Pareto(3.0, 2.0),\n", 316 | " Binomial(10, 0.6),\n", 317 | " Poisson(0.7),\n", 318 | " MvLogNormal(ones(2), 3*eye(2)),\n", 319 | " Dirichlet([0.1, 0.2, 0.3, 0.4]),\n", 320 | " InverseWishart(5, eye(2)),\n", 321 | " MixtureModel(Normal[\n", 322 | " Normal(-2.0, 1.2),\n", 323 | " Normal(0.0, 1.0),\n", 324 | " Normal(3.0, 2.5)], \n", 325 | " [0.2, 0.5, 0.3] # prior\n", 326 | " )\n", 327 | "]\n", 328 | "\n", 329 | "for d in dists\n", 330 | " println(\"Working with distribution: $(repr(d))\")\n", 331 | " @show mean(d)\n", 332 | " if isa(d, Distributions.UnivariateDistribution)\n", 333 | " @show rand(d, 2, 2)\n", 334 | " else\n", 335 | " @show rand(d, 2)\n", 336 | " end\n", 337 | " \n", 338 | " @show pdf(d, rand(d))\n", 339 | " println(\"\\n\\n\\n\")\n", 340 | "end" 341 | ] 342 | }, 343 | { 344 | "cell_type": "markdown", 345 | "metadata": { 346 | "slideshow": { 347 | "slide_type": "subslide" 348 | } 349 | }, 350 | "source": [ 351 | "### More than you need\n" 352 | ] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "metadata": {}, 357 | "source": [ 358 | "Let's list all the available distributions, by type of distribution" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 32, 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "name": "stdout", 368 | "output_type": "stream", 369 | "text": [ 370 | "Distributions.Distribution{Distributions.Matrixvariate,Distributions.Discrete}: \n", 371 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Matrixvariate,Distributions.Discrete,C} where C<:Distributions.Distribution]\n", 372 | "\n", 373 | "\n", 374 | "\n", 375 | "Distributions.Distribution{Distributions.Multivariate,Distributions.Discrete}: \n", 376 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Multivariate,Distributions.Discrete,C} where C<:Distributions.Distribution, Distributions.DirichletMultinomial, Distributions.Multinomial]\n", 377 | "\n", 378 | "\n", 379 | "\n", 380 | "Distributions.Distribution{Distributions.Univariate,Distributions.Discrete}: \n", 381 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Univariate,Distributions.Discrete,C} where C<:Distributions.Distribution, Distributions.Bernoulli, Distributions.BetaBinomial, Distributions.Binomial, Distributions.Categorical, Distributions.DiscreteUniform, Distributions.Geometric, Distributions.Hypergeometric, Distributions.NegativeBinomial, Distributions.NoncentralHypergeometric, Distributions.Poisson, Distributions.PoissonBinomial, Distributions.Skellam, Distributions.Truncated{D,Distributions.Discrete} where D<:(Distributions.Distribution{Distributions.Univariate,S} where S<:Distributions.ValueSupport)]\n", 382 | "\n", 383 | "\n", 384 | "\n", 385 | "Distributions.Distribution{Distributions.Matrixvariate,Distributions.Continuous}: \n", 386 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Matrixvariate,Distributions.Continuous,C} where C<:Distributions.Distribution, Distributions.InverseWishart, Distributions.Wishart]\n", 387 | "\n", 388 | "\n", 389 | "\n", 390 | "Distributions.Distribution{Distributions.Multivariate,Distributions.Continuous}: \n", 391 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Multivariate,Distributions.Continuous,C} where C<:Distributions.Distribution, Distributions.AbstractMvLogNormal, Distributions.AbstractMvNormal, Distributions.AbstractMvTDist, Distributions.Dirichlet, Distributions.VonMisesFisher]\n", 392 | "\n", 393 | "\n", 394 | "\n", 395 | "Distributions.Distribution{Distributions.Univariate,Distributions.Continuous}: \n", 396 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Univariate,Distributions.Continuous,C} where C<:Distributions.Distribution, Distributions.Arcsine, Distributions.Beta, Distributions.BetaPrime, Distributions.Biweight, Distributions.Cauchy, Distributions.Chi, Distributions.Chisq, Distributions.Cosine, Distributions.EdgeworthAbstract, Distributions.EmpiricalUnivariateDistribution, Distributions.Epanechnikov, Distributions.Erlang, Distributions.Exponential, Distributions.FDist, Distributions.Frechet, Distributions.Gamma, Distributions.GeneralizedExtremeValue, Distributions.GeneralizedPareto, Distributions.Gumbel, Distributions.InverseGamma, Distributions.InverseGaussian, Distributions.KSDist, Distributions.KSOneSided, Distributions.Kolmogorov, Distributions.Laplace, Distributions.Levy, Distributions.LogNormal, Distributions.Logistic, Distributions.NoncentralBeta, Distributions.NoncentralChisq, Distributions.NoncentralF, Distributions.NoncentralT, Distributions.Normal, Distributions.NormalCanon, Distributions.NormalInverseGaussian, Distributions.Pareto, Distributions.Rayleigh, Distributions.SymTriangularDist, Distributions.TDist, Distributions.TriangularDist, Distributions.Triweight, Distributions.Truncated{D,Distributions.Continuous} where D<:(Distributions.Distribution{Distributions.Univariate,S} where S<:Distributions.ValueSupport), Distributions.Uniform, Distributions.VonMises, Distributions.Weibull]\n", 397 | "\n", 398 | "\n", 399 | "\n" 400 | ] 401 | } 402 | ], 403 | "source": [ 404 | "dist_types = [\n", 405 | " Distributions.DiscreteMatrixDistribution,\n", 406 | " Distributions.DiscreteMultivariateDistribution,\n", 407 | " Distributions.DiscreteUnivariateDistribution,\n", 408 | " Distributions.ContinuousMatrixDistribution,\n", 409 | " Distributions.ContinuousMultivariateDistribution,\n", 410 | " Distributions.ContinuousUnivariateDistribution, \n", 411 | "]\n", 412 | "\n", 413 | "for T in dist_types\n", 414 | " println(\"$T: \")\n", 415 | " @show subtypes(T)\n", 416 | " println(\"\\n\\n\")\n", 417 | "end " 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": 33, 423 | "metadata": { 424 | "slideshow": { 425 | "slide_type": "fragment" 426 | } 427 | }, 428 | "outputs": [ 429 | { 430 | "data": { 431 | "text/plain": [ 432 | "Distributions.Normal{Float64}(μ=-0.0015332949351311543, σ=1.0000359500569347)" 433 | ] 434 | }, 435 | "execution_count": 33, 436 | "metadata": {}, 437 | "output_type": "execute_result" 438 | } 439 | ], 440 | "source": [ 441 | "# fitting a distribution, given some samples\n", 442 | "fit_mle(Normal, randn(100_000)) # should get close to N(0, 1)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 34, 448 | "metadata": { 449 | "slideshow": { 450 | "slide_type": "fragment" 451 | } 452 | }, 453 | "outputs": [ 454 | { 455 | "data": { 456 | "text/plain": [ 457 | "Distributions.Uniform{Float64}(a=1.0000075805099509, b=2.999950042341385)" 458 | ] 459 | }, 460 | "execution_count": 34, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "# do fitting with mle\n", 467 | "fit_mle(Uniform, rand(100_000) .* 2 .+ 1) # should get close to U(1, 3)" 468 | ] 469 | }, 470 | { 471 | "cell_type": "markdown", 472 | "metadata": { 473 | "slideshow": { 474 | "slide_type": "slide" 475 | } 476 | }, 477 | "source": [ 478 | "## Calculus.jl\n", 479 | "\n", 480 | "- Computes analytical derivatives of Julia `Expr`essions and accurate numerical derivatives of functions\n", 481 | "\n", 482 | "[Package](https://github.com/johnmyleswhite/Calculus.jl)" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 35, 488 | "metadata": { 489 | "collapsed": true 490 | }, 491 | "outputs": [], 492 | "source": [ 493 | "# Pkg.add(\"Calculus\")" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": { 499 | "slideshow": { 500 | "slide_type": "subslide" 501 | } 502 | }, 503 | "source": [ 504 | "### Calculus.jl Basics" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 4, 510 | "metadata": {}, 511 | "outputs": [], 512 | "source": [ 513 | "using Calculus" 514 | ] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": { 519 | "slideshow": { 520 | "slide_type": "fragment" 521 | } 522 | }, 523 | "source": [ 524 | "#### Symbolic derivatives" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 5, 530 | "metadata": {}, 531 | "outputs": [ 532 | { 533 | "data": { 534 | "text/plain": [ 535 | ":(cos(x))" 536 | ] 537 | }, 538 | "execution_count": 5, 539 | "metadata": {}, 540 | "output_type": "execute_result" 541 | } 542 | ], 543 | "source": [ 544 | "differentiate(:(sin(x)), :x)" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": 38, 550 | "metadata": {}, 551 | "outputs": [ 552 | { 553 | "data": { 554 | "text/plain": [ 555 | ":(cos(y) * -(sin(sin(y))))" 556 | ] 557 | }, 558 | "execution_count": 38, 559 | "metadata": {}, 560 | "output_type": "execute_result" 561 | } 562 | ], 563 | "source": [ 564 | "differentiate(:(cos(sin(y))), :y)" 565 | ] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "execution_count": 6, 570 | "metadata": {}, 571 | "outputs": [ 572 | { 573 | "data": { 574 | "text/plain": [ 575 | ":(((1 - γ) * c ^ ((1 - γ) - 1)) / (1 - γ))" 576 | ] 577 | }, 578 | "execution_count": 6, 579 | "metadata": {}, 580 | "output_type": "execute_result" 581 | } 582 | ], 583 | "source": [ 584 | "differentiate(:(c^(1-γ)/(1-γ)), :c)" 585 | ] 586 | }, 587 | { 588 | "cell_type": "markdown", 589 | "metadata": { 590 | "slideshow": { 591 | "slide_type": "fragment" 592 | } 593 | }, 594 | "source": [ 595 | "#### Finite difference" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": 40, 601 | "metadata": {}, 602 | "outputs": [ 603 | { 604 | "data": { 605 | "text/plain": [ 606 | "-5.036193684304635e-12" 607 | ] 608 | }, 609 | "execution_count": 40, 610 | "metadata": {}, 611 | "output_type": "execute_result" 612 | } 613 | ], 614 | "source": [ 615 | "derivative(sin, 1.0) - cos(1.0)" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": 41, 621 | "metadata": {}, 622 | "outputs": [ 623 | { 624 | "data": { 625 | "text/plain": [ 626 | "-6.647716624952338e-7" 627 | ] 628 | }, 629 | "execution_count": 41, 630 | "metadata": {}, 631 | "output_type": "execute_result" 632 | } 633 | ], 634 | "source": [ 635 | "second_derivative(sin, 1.0) + sin(1.0)" 636 | ] 637 | }, 638 | { 639 | "cell_type": "code", 640 | "execution_count": 7, 641 | "metadata": {}, 642 | "outputs": [ 643 | { 644 | "data": { 645 | "text/plain": [ 646 | "2-element Array{Float64,1}:\n", 647 | " 2.71828\n", 648 | " -1.0 " 649 | ] 650 | }, 651 | "execution_count": 7, 652 | "metadata": {}, 653 | "output_type": "execute_result" 654 | } 655 | ], 656 | "source": [ 657 | "Calculus.gradient(x -> exp(x[1]) + sin(x[2]) / x[1], [1.0, π])" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 43, 663 | "metadata": {}, 664 | "outputs": [ 665 | { 666 | "data": { 667 | "text/plain": [ 668 | "2×2 Array{Float64,2}:\n", 669 | " 2.71828 1.0 \n", 670 | " 1.0 1.71123e-7" 671 | ] 672 | }, 673 | "execution_count": 43, 674 | "metadata": {}, 675 | "output_type": "execute_result" 676 | } 677 | ], 678 | "source": [ 679 | "Calculus.hessian(x -> exp(x[1]) + sin(x[2]) / x[1], [1.0, π])" 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "execution_count": 44, 685 | "metadata": {}, 686 | "outputs": [ 687 | { 688 | "data": { 689 | "text/plain": [ 690 | "2×2 Array{Float64,2}:\n", 691 | " 2.71828 0.0\n", 692 | " -1.22465e-16 -1.0" 693 | ] 694 | }, 695 | "execution_count": 44, 696 | "metadata": {}, 697 | "output_type": "execute_result" 698 | } 699 | ], 700 | "source": [ 701 | "Calculus.jacobian(x -> [exp(x[1]), sin(x[2]) / x[1]], [1.0, π], :central)" 702 | ] 703 | }, 704 | { 705 | "cell_type": "markdown", 706 | "metadata": { 707 | "slideshow": { 708 | "slide_type": "slide" 709 | } 710 | }, 711 | "source": [ 712 | "## SymEngine.jl\n", 713 | "\n", 714 | "- Next generation C++ backend for sympy computer algebra system\n", 715 | "- A very fast alternative to Calculus.jl for symbolic differentiation\n", 716 | "\n", 717 | "[Jula package](https://github.com/symengine/SymEngine.jl) and [\"Documentation\"](https://github.com/symengine/symengine/blob/master/symengine/cwrapper.h)" 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": 45, 723 | "metadata": { 724 | "collapsed": true 725 | }, 726 | "outputs": [], 727 | "source": [ 728 | "# Pkg.add(\"SymEngine\")" 729 | ] 730 | }, 731 | { 732 | "cell_type": "markdown", 733 | "metadata": { 734 | "slideshow": { 735 | "slide_type": "subslide" 736 | } 737 | }, 738 | "source": [ 739 | "### SymEngine.jl Basics" 740 | ] 741 | }, 742 | { 743 | "cell_type": "code", 744 | "execution_count": 46, 745 | "metadata": {}, 746 | "outputs": [], 747 | "source": [ 748 | "using SymEngine" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": 47, 754 | "metadata": {}, 755 | "outputs": [ 756 | { 757 | "data": { 758 | "text/plain": [ 759 | "cos(x)" 760 | ] 761 | }, 762 | "execution_count": 47, 763 | "metadata": {}, 764 | "output_type": "execute_result" 765 | } 766 | ], 767 | "source": [ 768 | "# needs first argument to be of type SymEngine.Basic\n", 769 | "diff(Basic(:(sin(x))), :x)" 770 | ] 771 | }, 772 | { 773 | "cell_type": "code", 774 | "execution_count": 48, 775 | "metadata": {}, 776 | "outputs": [ 777 | { 778 | "data": { 779 | "text/plain": [ 780 | "-cos(y)*sin(sin(y))" 781 | ] 782 | }, 783 | "execution_count": 48, 784 | "metadata": {}, 785 | "output_type": "execute_result" 786 | } 787 | ], 788 | "source": [ 789 | "diff(Basic(\"cos(sin(y))\"), :y)" 790 | ] 791 | }, 792 | { 793 | "cell_type": "code", 794 | "execution_count": 49, 795 | "metadata": {}, 796 | "outputs": [ 797 | { 798 | "data": { 799 | "text/plain": [ 800 | "c^(-γ)" 801 | ] 802 | }, 803 | "execution_count": 49, 804 | "metadata": {}, 805 | "output_type": "execute_result" 806 | } 807 | ], 808 | "source": [ 809 | "diff(Basic(\"c^(1-γ)/(1-γ)\"), :c)" 810 | ] 811 | }, 812 | { 813 | "cell_type": "markdown", 814 | "metadata": { 815 | "slideshow": { 816 | "slide_type": "subslide" 817 | } 818 | }, 819 | "source": [ 820 | "Let's see how fast SymEngine is compared to Calculus.jl\n", 821 | "\n", 822 | "To do this we will load the BenchmarkTools.jl package that goes to great lengths to produce statistically accurate and robust timing estimates at the sub-microsecond level" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": 50, 828 | "metadata": {}, 829 | "outputs": [], 830 | "source": [ 831 | "# Pkg.add(\"BenchmarkTools\")\n", 832 | "using BenchmarkTools" 833 | ] 834 | }, 835 | { 836 | "cell_type": "code", 837 | "execution_count": 51, 838 | "metadata": {}, 839 | "outputs": [ 840 | { 841 | "data": { 842 | "text/plain": [ 843 | "BenchmarkTools.Trial: \n", 844 | " memory estimate: 66.66 KiB\n", 845 | " allocs estimate: 1109\n", 846 | " --------------\n", 847 | " minimum time: 827.381 μs (0.00% GC)\n", 848 | " median time: 877.233 μs (0.00% GC)\n", 849 | " mean time: 1.218 ms (1.21% GC)\n", 850 | " maximum time: 11.096 ms (88.43% GC)\n", 851 | " --------------\n", 852 | " samples: 4075\n", 853 | " evals/sample: 1" 854 | ] 855 | }, 856 | "execution_count": 51, 857 | "metadata": {}, 858 | "output_type": "execute_result" 859 | } 860 | ], 861 | "source": [ 862 | "@benchmark Calculus.differentiate(:((y + r*a - ap)^(1-γ)/(1-γ)), :ap)" 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": 52, 868 | "metadata": {}, 869 | "outputs": [ 870 | { 871 | "data": { 872 | "text/plain": [ 873 | "BenchmarkTools.Trial: \n", 874 | " memory estimate: 840 bytes\n", 875 | " allocs estimate: 42\n", 876 | " --------------\n", 877 | " minimum time: 56.437 μs (0.00% GC)\n", 878 | " median time: 58.088 μs (0.00% GC)\n", 879 | " mean time: 59.610 μs (0.00% GC)\n", 880 | " maximum time: 374.913 μs (0.00% GC)\n", 881 | " --------------\n", 882 | " samples: 10000\n", 883 | " evals/sample: 1" 884 | ] 885 | }, 886 | "execution_count": 52, 887 | "metadata": {}, 888 | "output_type": "execute_result" 889 | } 890 | ], 891 | "source": [ 892 | "@benchmark diff(Basic(\"(y + r*a - ap)^(1-γ)/(1-γ)\"), :ap)" 893 | ] 894 | }, 895 | { 896 | "cell_type": "markdown", 897 | "metadata": { 898 | "slideshow": { 899 | "slide_type": "slide" 900 | } 901 | }, 902 | "source": [ 903 | "## Data handling\n", 904 | "\n", 905 | "We won't cover these packages in detail, but the following are some of the most commonly used data-related packages available today. \n", 906 | "\n", 907 | "- [DataFrames.jl](https://github.com/JuliaStats/DataFrames.jl): Provides a DataFrame type for handling columnar data. Similar to data frames in Python Pandas or R.\n", 908 | "- [CSV.jl](https://github.com/JuliaData/CSV.jl): reading and writing of delimited data files\n", 909 | "- [DataStreams.jl](https://github.com/JuliaData/DataStreams.jl): provide an interface for streaming data from a source to a sink\n", 910 | "- [Query.jl](https://github.com/davidanthoff/Query.jl): filter, project, join, group any iterable data source\n", 911 | "- [HDF5.jl](https://github.com/JuliaIO/HDF5.jl): Read and write HDF5 files, commonly used by MATLAB and others to store numeric data\n", 912 | "- [JLD.jl](https://github.com/JuliaIO/JLD.jl): Read and write datasets using Julia-native JLD files, which are enhanced HDF5 files \n" 913 | ] 914 | }, 915 | { 916 | "cell_type": "code", 917 | "execution_count": null, 918 | "metadata": { 919 | "collapsed": true 920 | }, 921 | "outputs": [], 922 | "source": [] 923 | } 924 | ], 925 | "metadata": { 926 | "anaconda-cloud": {}, 927 | "kernelspec": { 928 | "display_name": "Julia 0.6.1", 929 | "language": "julia", 930 | "name": "julia-0.6" 931 | }, 932 | "language_info": { 933 | "file_extension": ".jl", 934 | "mimetype": "application/julia", 935 | "name": "julia", 936 | "version": "0.6.1" 937 | }, 938 | "livereveal": { 939 | "scroll": true, 940 | "start_slideshow_at": "selected", 941 | "theme": "white", 942 | "transition": "fade" 943 | } 944 | }, 945 | "nbformat": 4, 946 | "nbformat_minor": 2 947 | } 948 | -------------------------------------------------------------------------------- /packages/economics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true, 7 | "slideshow": { 8 | "slide_type": "slide" 9 | } 10 | }, 11 | "source": [ 12 | "# Economics-specific packages\n", 13 | "\n", 14 | "Based on a notebook by Chase Coleman and Spencer Lyon and on material from QuantEcon\n", 15 | "\n", 16 | "18 December 2017" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "This lecture will focus mostly on QuantEcon. Other economics packages of interest include DSGE.jl and StateSpaceRoutines.jl, which will be covered separately." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "slideshow": { 30 | "slide_type": "subslide" 31 | } 32 | }, 33 | "source": [ 34 | "## QuantEcon.jl\n", 35 | "\n", 36 | "What is QuantEcon?\n", 37 | "\n", 38 | "- Set of [lectures](https://lectures.quantecon.org/) by Tom Sargent and John Stachurski to teach computational economics and programming principles\n", 39 | "- A [community](http://discourse.quantecon.org) aimed at teaching best practices and encouraging collaboration\n", 40 | "- A set of software libraries in [Python](https://quantecon.org/quantecon-py) and [Julia](https://quantecon.org/quantecon-jl) that implement common numerical routines used in economic research\n", 41 | "- [QuantEcon.jl](https://github.com/QuantEcon/QuantEcon.jl) is the Julia version of the software library" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 1, 47 | "metadata": { 48 | "collapsed": true 49 | }, 50 | "outputs": [], 51 | "source": [ 52 | "# Pkg.add(\"QuantEcon\")" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": { 58 | "slideshow": { 59 | "slide_type": "subslide" 60 | } 61 | }, 62 | "source": [ 63 | "### QuantEcon.jl Basics" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 1, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "using QuantEcon" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": { 78 | "slideshow": { 79 | "slide_type": "subslide" 80 | } 81 | }, 82 | "source": [ 83 | "#### Markov Chains\n", 84 | "\n", 85 | "QuantEcon.jl has rich support for working with discrete state MarkovChains" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 2, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "Discrete Markov Chain\n", 97 | "stochastic matrix of type Array{Float64,2}:\n", 98 | "[0.9 0.1; 0.2 0.8]" 99 | ] 100 | }, 101 | "execution_count": 2, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "mc = MarkovChain([0.9 0.1; 0.2 0.8])" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 4, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "1-element Array{Array{Float64,1},1}:\n", 119 | " [0.666667, 0.333333]" 120 | ] 121 | }, 122 | "execution_count": 4, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "stationary_distributions(mc)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 3, 134 | "metadata": { 135 | "slideshow": { 136 | "slide_type": "fragment" 137 | } 138 | }, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "search: \u001b[1ms\u001b[22m\u001b[1mi\u001b[22m\u001b[1mm\u001b[22m\u001b[1mu\u001b[22m\u001b[1ml\u001b[22m\u001b[1ma\u001b[22m\u001b[1mt\u001b[22m\u001b[1me\u001b[22m \u001b[1ms\u001b[22m\u001b[1mi\u001b[22m\u001b[1mm\u001b[22m\u001b[1mu\u001b[22m\u001b[1ml\u001b[22m\u001b[1ma\u001b[22m\u001b[1mt\u001b[22m\u001b[1me\u001b[22m! \u001b[1ms\u001b[22m\u001b[1mi\u001b[22m\u001b[1mm\u001b[22m\u001b[1mu\u001b[22m\u001b[1ml\u001b[22m\u001b[1ma\u001b[22m\u001b[1mt\u001b[22m\u001b[1me\u001b[22m_indices \u001b[1ms\u001b[22m\u001b[1mi\u001b[22m\u001b[1mm\u001b[22m\u001b[1mu\u001b[22m\u001b[1ml\u001b[22m\u001b[1ma\u001b[22m\u001b[1mt\u001b[22m\u001b[1me\u001b[22m_indices! \u001b[1ms\u001b[22m\u001b[1mi\u001b[22m\u001b[1mm\u001b[22m\u001b[1mu\u001b[22m\u001b[1ml\u001b[22m\u001b[1ma\u001b[22m\u001b[1mt\u001b[22mion\n", 145 | "\n" 146 | ] 147 | }, 148 | { 149 | "data": { 150 | "text/markdown": [ 151 | "Simulate one sample path of the Markov chain `mc`. The resulting vector has the state values of `mc` as elements.\n", 152 | "\n", 153 | "### Arguments\n", 154 | "\n", 155 | " * `mc::MarkovChain` : MarkovChain instance.\n", 156 | " * `ts_length::Int` : Length of simulation\n", 157 | " * `;init::Int=rand(1:n_states(mc))` : Initial state\n", 158 | "\n", 159 | "### Returns\n", 160 | "\n", 161 | " * `X::Vector` : Vector containing the sample path, with length ts_length\n" 162 | ], 163 | "text/plain": [ 164 | "Simulate one sample path of the Markov chain `mc`. The resulting vector has the state values of `mc` as elements.\n", 165 | "\n", 166 | "### Arguments\n", 167 | "\n", 168 | " * `mc::MarkovChain` : MarkovChain instance.\n", 169 | " * `ts_length::Int` : Length of simulation\n", 170 | " * `;init::Int=rand(1:n_states(mc))` : Initial state\n", 171 | "\n", 172 | "### Returns\n", 173 | "\n", 174 | " * `X::Vector` : Vector containing the sample path, with length ts_length\n" 175 | ] 176 | }, 177 | "execution_count": 3, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "?simulate" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 4, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "data": { 193 | "text/html": [ 194 | "1.3358" 195 | ], 196 | "text/plain": [ 197 | "1.3358" 198 | ] 199 | }, 200 | "execution_count": 4, 201 | "metadata": {}, 202 | "output_type": "execute_result" 203 | } 204 | ], 205 | "source": [ 206 | "mean(simulate(mc, 10000)) # should be roughly 2/3*1 + 1/3*2 = 4/3" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 6, 212 | "metadata": { 213 | "slideshow": { 214 | "slide_type": "subslide" 215 | } 216 | }, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "Discrete Markov Chain\n", 222 | "stochastic matrix of type Array{Float64,2}:\n", 223 | "[0.9 0.1; 0.2 0.8]" 224 | ] 225 | }, 226 | "execution_count": 6, 227 | "metadata": {}, 228 | "output_type": "execute_result" 229 | } 230 | ], 231 | "source": [ 232 | "# can also pass state_values as second argument\n", 233 | "mc2 = MarkovChain(\n", 234 | " mc.p, # re-use transition matrix\n", 235 | " [0.0, 42.0]\n", 236 | ")" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 8, 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/html": [ 247 | "13.91544" 248 | ], 249 | "text/plain": [ 250 | "13.91544" 251 | ] 252 | }, 253 | "execution_count": 8, 254 | "metadata": {}, 255 | "output_type": "execute_result" 256 | } 257 | ], 258 | "source": [ 259 | "mean(simulate(mc2, 100000)) # should be roughly 2/3*0 + 1/3*42 = 14" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 7, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "data": { 269 | "text/plain": [ 270 | "10-element Array{Int64,1}:\n", 271 | " 2\n", 272 | " 2\n", 273 | " 2\n", 274 | " 2\n", 275 | " 2\n", 276 | " 2\n", 277 | " 1\n", 278 | " 1\n", 279 | " 1\n", 280 | " 1" 281 | ] 282 | }, 283 | "execution_count": 7, 284 | "metadata": {}, 285 | "output_type": "execute_result" 286 | } 287 | ], 288 | "source": [ 289 | "# can also simulate the indices\n", 290 | "simulate_indices(mc2, 10, init=2)" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 8, 296 | "metadata": { 297 | "slideshow": { 298 | "slide_type": "subslide" 299 | } 300 | }, 301 | "outputs": [ 302 | { 303 | "data": { 304 | "text/plain": [ 305 | "500×10 Array{Float64,2}:\n", 306 | " 42.0 42.0 42.0 42.0 42.0 42.0 42.0 42.0 42.0 42.0\n", 307 | " 42.0 42.0 42.0 42.0 42.0 42.0 42.0 42.0 42.0 0.0\n", 308 | " 42.0 42.0 42.0 42.0 0.0 42.0 42.0 0.0 42.0 0.0\n", 309 | " 42.0 42.0 42.0 0.0 0.0 0.0 42.0 0.0 42.0 0.0\n", 310 | " 42.0 0.0 42.0 0.0 0.0 0.0 42.0 0.0 0.0 0.0\n", 311 | " 42.0 0.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 312 | " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 42.0 0.0 0.0\n", 313 | " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 314 | " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 315 | " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 316 | " 0.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 317 | " 0.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0 0.0 0.0\n", 318 | " 0.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0 0.0 0.0\n", 319 | " ⋮ ⋮ \n", 320 | " 0.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 321 | " 0.0 0.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0 0.0\n", 322 | " 0.0 0.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0 0.0\n", 323 | " 42.0 0.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0 0.0\n", 324 | " 0.0 0.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 325 | " 0.0 0.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 326 | " 0.0 0.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 327 | " 0.0 0.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0 0.0\n", 328 | " 0.0 0.0 42.0 42.0 0.0 0.0 0.0 42.0 0.0 0.0\n", 329 | " 42.0 0.0 42.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0\n", 330 | " 42.0 0.0 42.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0\n", 331 | " 0.0 0.0 0.0 42.0 0.0 0.0 0.0 0.0 42.0 0.0" 332 | ] 333 | }, 334 | "execution_count": 8, 335 | "metadata": {}, 336 | "output_type": "execute_result" 337 | } 338 | ], 339 | "source": [ 340 | "# fill pre-allocated matrix with samples\n", 341 | "# each column is a time-series\n", 342 | "out = zeros(500, 10)\n", 343 | "simulate!(out, mc2, init=2)\n", 344 | "out" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 11, 350 | "metadata": { 351 | "slideshow": { 352 | "slide_type": "fragment" 353 | } 354 | }, 355 | "outputs": [ 356 | { 357 | "data": { 358 | "text/plain": [ 359 | "500×10 Array{Int64,2}:\n", 360 | " 2 2 2 2 2 2 2 2 2 2\n", 361 | " 2 1 1 2 2 1 2 1 2 2\n", 362 | " 2 2 2 2 2 2 1 1 2 2\n", 363 | " 1 2 2 2 2 2 1 1 2 2\n", 364 | " 1 2 2 2 2 2 1 1 2 2\n", 365 | " 2 2 2 2 2 2 1 1 2 2\n", 366 | " 2 1 2 2 2 2 1 1 2 2\n", 367 | " 1 1 2 2 2 2 1 1 2 2\n", 368 | " 1 1 2 1 2 1 1 1 1 2\n", 369 | " 1 1 2 1 1 1 1 2 1 1\n", 370 | " 1 1 2 2 1 1 1 2 2 1\n", 371 | " 1 1 2 1 1 1 1 2 2 1\n", 372 | " 1 1 1 1 1 1 1 1 2 1\n", 373 | " ⋮ ⋮ \n", 374 | " 1 1 2 1 1 2 1 2 1 2\n", 375 | " 1 1 2 1 2 2 1 2 2 2\n", 376 | " 1 1 2 1 2 1 2 2 2 2\n", 377 | " 2 1 2 1 1 1 2 2 2 1\n", 378 | " 2 2 1 2 1 1 2 1 2 1\n", 379 | " 2 2 1 2 1 1 1 1 2 1\n", 380 | " 1 2 1 2 1 1 1 1 2 1\n", 381 | " 2 2 1 2 1 1 1 1 2 1\n", 382 | " 2 1 1 2 1 2 2 1 2 1\n", 383 | " 2 1 1 2 1 2 2 1 2 2\n", 384 | " 2 1 1 1 1 2 2 1 2 2\n", 385 | " 2 1 1 1 1 2 1 1 2 2" 386 | ] 387 | }, 388 | "execution_count": 11, 389 | "metadata": {}, 390 | "output_type": "execute_result" 391 | } 392 | ], 393 | "source": [ 394 | "# same with indices\n", 395 | "out_inds = zeros(Int, 500, 10)\n", 396 | "simulate_indices!(out_inds, mc2, init=2)\n", 397 | "out_inds" 398 | ] 399 | }, 400 | { 401 | "cell_type": "code", 402 | "execution_count": 9, 403 | "metadata": { 404 | "slideshow": { 405 | "slide_type": "fragment" 406 | } 407 | }, 408 | "outputs": [ 409 | { 410 | "data": { 411 | "text/html": [ 412 | "10-element Array{Method,1}:" 413 | ], 414 | "text/plain": [ 415 | "10-element Array{Method,1}:\n", 416 | " period(mc::QuantEcon.MarkovChain) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:220 \n", 417 | " communication_classes(mc::QuantEcon.MarkovChain) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:177 \n", 418 | " is_aperiodic(mc::QuantEcon.MarkovChain) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:205 \n", 419 | " is_irreducible(mc::QuantEcon.MarkovChain) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:191 \n", 420 | " n_states(mc::QuantEcon.MarkovChain) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:61 \n", 421 | " recurrent_classes(mc::QuantEcon.MarkovChain) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:162 \n", 422 | " simulate(mc::QuantEcon.MarkovChain, ts_length::Int64) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:360 \n", 423 | " simulate!(X::Union{AbstractArray{T,1} where T, AbstractArray{T,2} where T}, mc::QuantEcon.MarkovChain) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:385 \n", 424 | " simulate_indices(mc::QuantEcon.MarkovChain, ts_length::Int64) in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:415 \n", 425 | " simulate_indices!(X::Union{AbstractArray{T,1}, AbstractArray{T,2}}, mc::QuantEcon.MarkovChain) where T<:Integer in QuantEcon at /Users/rceaxg24/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:438" 426 | ] 427 | }, 428 | "execution_count": 9, 429 | "metadata": {}, 430 | "output_type": "execute_result" 431 | } 432 | ], 433 | "source": [ 434 | "# other things we can do with MarkovChains\n", 435 | "methodswith(MarkovChain)" 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": { 441 | "slideshow": { 442 | "slide_type": "subslide" 443 | } 444 | }, 445 | "source": [ 446 | "#### Discretizing AR(1) process\n", 447 | "\n", 448 | "QuantEcon.jl provides two methods for discretizing an AR(1) process into an instance of MarkovChain:\n" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 10, 454 | "metadata": { 455 | "slideshow": { 456 | "slide_type": "fragment" 457 | } 458 | }, 459 | "outputs": [ 460 | { 461 | "name": "stdout", 462 | "output_type": "stream", 463 | "text": [ 464 | "search: \u001b[1mt\u001b[22m\u001b[1ma\u001b[22m\u001b[1mu\u001b[22m\u001b[1mc\u001b[22m\u001b[1mh\u001b[22m\u001b[1me\u001b[22m\u001b[1mn\u001b[22m\n", 465 | "\n" 466 | ] 467 | }, 468 | { 469 | "data": { 470 | "text/markdown": [ 471 | "Tauchen's (1996) method for approximating AR(1) process with finite markov chain\n", 472 | "\n", 473 | "The process follows\n", 474 | "\n", 475 | "$$\n", 476 | " y_t = \\mu + \\rho y_{t-1} + \\epsilon_t\n", 477 | "$$\n", 478 | "\n", 479 | "where $\\epsilon_t \\sim N (0, \\sigma^2)$\n", 480 | "\n", 481 | "##### Arguments\n", 482 | "\n", 483 | " * `N::Integer`: Number of points in markov process\n", 484 | " * `ρ::Real` : Persistence parameter in AR(1) process\n", 485 | " * `σ::Real` : Standard deviation of random component of AR(1) process\n", 486 | " * `μ::Real(0.0)` : Mean of AR(1) process\n", 487 | " * `n_std::Integer(3)` : The number of standard deviations to each side the process should span\n", 488 | "\n", 489 | "##### Returns\n", 490 | "\n", 491 | " * `mc::MarkovChain{Float64}` : Markov chain holding the state values and transition matrix\n" 492 | ], 493 | "text/plain": [ 494 | "Tauchen's (1996) method for approximating AR(1) process with finite markov chain\n", 495 | "\n", 496 | "The process follows\n", 497 | "\n", 498 | "$$\n", 499 | " y_t = \\mu + \\rho y_{t-1} + \\epsilon_t\n", 500 | "$$\n", 501 | "\n", 502 | "where $\\epsilon_t \\sim N (0, \\sigma^2)$\n", 503 | "\n", 504 | "##### Arguments\n", 505 | "\n", 506 | " * `N::Integer`: Number of points in markov process\n", 507 | " * `ρ::Real` : Persistence parameter in AR(1) process\n", 508 | " * `σ::Real` : Standard deviation of random component of AR(1) process\n", 509 | " * `μ::Real(0.0)` : Mean of AR(1) process\n", 510 | " * `n_std::Integer(3)` : The number of standard deviations to each side the process should span\n", 511 | "\n", 512 | "##### Returns\n", 513 | "\n", 514 | " * `mc::MarkovChain{Float64}` : Markov chain holding the state values and transition matrix\n" 515 | ] 516 | }, 517 | "execution_count": 10, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | } 521 | ], 522 | "source": [ 523 | "?tauchen" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 11, 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "name": "stdout", 533 | "output_type": "stream", 534 | "text": [ 535 | "search: \u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1mu\u001b[22m\u001b[1mw\u001b[22m\u001b[1me\u001b[22m\u001b[1mn\u001b[22m\u001b[1mh\u001b[22m\u001b[1mo\u001b[22m\u001b[1mr\u001b[22m\u001b[1ms\u001b[22m\u001b[1mt\u001b[22m\n", 536 | "\n" 537 | ] 538 | }, 539 | { 540 | "data": { 541 | "text/markdown": [ 542 | "Rouwenhorst's method to approximate AR(1) processes.\n", 543 | "\n", 544 | "The process follows\n", 545 | "\n", 546 | "$$\n", 547 | " y_t = \\mu + \\rho y_{t-1} + \\epsilon_t\n", 548 | "$$\n", 549 | "\n", 550 | "where $\\epsilon_t \\sim N (0, \\sigma^2)$\n", 551 | "\n", 552 | "##### Arguments\n", 553 | "\n", 554 | " * `N::Integer` : Number of points in markov process\n", 555 | " * `ρ::Real` : Persistence parameter in AR(1) process\n", 556 | " * `σ::Real` : Standard deviation of random component of AR(1) process\n", 557 | " * `μ::Real(0.0)` : Mean of AR(1) process\n", 558 | "\n", 559 | "##### Returns\n", 560 | "\n", 561 | " * `mc::MarkovChain{Float64}` : Markov chain holding the state values and transition matrix\n" 562 | ], 563 | "text/plain": [ 564 | "Rouwenhorst's method to approximate AR(1) processes.\n", 565 | "\n", 566 | "The process follows\n", 567 | "\n", 568 | "$$\n", 569 | " y_t = \\mu + \\rho y_{t-1} + \\epsilon_t\n", 570 | "$$\n", 571 | "\n", 572 | "where $\\epsilon_t \\sim N (0, \\sigma^2)$\n", 573 | "\n", 574 | "##### Arguments\n", 575 | "\n", 576 | " * `N::Integer` : Number of points in markov process\n", 577 | " * `ρ::Real` : Persistence parameter in AR(1) process\n", 578 | " * `σ::Real` : Standard deviation of random component of AR(1) process\n", 579 | " * `μ::Real(0.0)` : Mean of AR(1) process\n", 580 | "\n", 581 | "##### Returns\n", 582 | "\n", 583 | " * `mc::MarkovChain{Float64}` : Markov chain holding the state values and transition matrix\n" 584 | ] 585 | }, 586 | "execution_count": 11, 587 | "metadata": {}, 588 | "output_type": "execute_result" 589 | } 590 | ], 591 | "source": [ 592 | "?rouwenhorst" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": 12, 598 | "metadata": { 599 | "slideshow": { 600 | "slide_type": "fragment" 601 | } 602 | }, 603 | "outputs": [ 604 | { 605 | "name": "stdout", 606 | "output_type": "stream", 607 | "text": [ 608 | "stationary_distributions(mc3) = Array{Float64,1}[[0.00195312, 0.0175781, 0.0703125, 0.164063, 0.246094, 0.246094, 0.164062, 0.0703125, 0.0175781, 0.00195312]]\n", 609 | "mc3.state_values = -3.6076892283052313:2.1350420507344947:15.60768922830522\n" 610 | ] 611 | }, 612 | { 613 | "data": { 614 | "text/plain": [ 615 | "20-element Array{Float64,1}:\n", 616 | " 11.3376 \n", 617 | " 11.3376 \n", 618 | " 11.3376 \n", 619 | " 11.3376 \n", 620 | " 11.3376 \n", 621 | " 11.3376 \n", 622 | " 11.3376 \n", 623 | " 9.20256\n", 624 | " 9.20256\n", 625 | " 9.20256\n", 626 | " 9.20256\n", 627 | " 9.20256\n", 628 | " 9.20256\n", 629 | " 9.20256\n", 630 | " 9.20256\n", 631 | " 9.20256\n", 632 | " 9.20256\n", 633 | " 7.06752\n", 634 | " 4.93248\n", 635 | " 4.93248" 636 | ] 637 | }, 638 | "execution_count": 12, 639 | "metadata": {}, 640 | "output_type": "execute_result" 641 | } 642 | ], 643 | "source": [ 644 | "mc3 = rouwenhorst(10, 0.95, 1.0, 0.3)\n", 645 | "@show stationary_distributions(mc3)\n", 646 | "@show mc3.state_values\n", 647 | "simulate(mc3, 20)" 648 | ] 649 | }, 650 | { 651 | "cell_type": "markdown", 652 | "metadata": { 653 | "slideshow": { 654 | "slide_type": "subslide" 655 | } 656 | }, 657 | "source": [ 658 | "#### Quadrature routines\n", 659 | "\n", 660 | "QuantEcon.jl has the family of Quadrature routines written by Miranda and Fackler in CompEcon as well as others used by Maliar, Maliar, and Judd" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": 16, 666 | "metadata": { 667 | "slideshow": { 668 | "slide_type": "fragment" 669 | } 670 | }, 671 | "outputs": [ 672 | { 673 | "data": { 674 | "text/plain": [ 675 | "12-element Array{Symbol,1}:\n", 676 | " :qnwbeta \n", 677 | " :qnwcheb \n", 678 | " :qnwequi \n", 679 | " :qnwgamma \n", 680 | " :qnwlege \n", 681 | " :qnwlogn \n", 682 | " :qnwmonomial1\n", 683 | " :qnwmonomial2\n", 684 | " :qnwnorm \n", 685 | " :qnwsimp \n", 686 | " :qnwtrap \n", 687 | " :qnwunif " 688 | ] 689 | }, 690 | "execution_count": 16, 691 | "metadata": {}, 692 | "output_type": "execute_result" 693 | } 694 | ], 695 | "source": [ 696 | "filter(x -> startswith(string(x), \"qnw\"), names(QuantEcon))" 697 | ] 698 | }, 699 | { 700 | "cell_type": "code", 701 | "execution_count": 13, 702 | "metadata": {}, 703 | "outputs": [ 704 | { 705 | "name": "stdout", 706 | "output_type": "stream", 707 | "text": [ 708 | "search: \u001b[1mq\u001b[22m\u001b[1mn\u001b[22m\u001b[1mw\u001b[22m\u001b[1mn\u001b[22m\u001b[1mo\u001b[22m\u001b[1mr\u001b[22m\u001b[1mm\u001b[22m \u001b[1mq\u001b[22m\u001b[1mn\u001b[22m\u001b[1mw\u001b[22mmo\u001b[1mn\u001b[22m\u001b[1mo\u001b[22mmial2 \u001b[1mq\u001b[22m\u001b[1mn\u001b[22m\u001b[1mw\u001b[22mmo\u001b[1mn\u001b[22m\u001b[1mo\u001b[22mmial1\n", 709 | "\n" 710 | ] 711 | }, 712 | { 713 | "data": { 714 | "text/markdown": [ 715 | "Computes nodes and weights for multivariate normal distribution.\n", 716 | "\n", 717 | "##### Arguments\n", 718 | "\n", 719 | " * `n::Union{Int, Vector{Int}}` : Number of desired nodes along each dimension\n", 720 | " * `mu::Union{Real, Vector{Real}}` : Mean along each dimension\n", 721 | " * `sig2::Union{Real, Vector{Real}, Matrix{Real}}(eye(length(n)))` : Covariance structure\n", 722 | "\n", 723 | "##### Returns\n", 724 | "\n", 725 | " * `nodes::Array{Float64}` : An array of quadrature nodes\n", 726 | " * `weights::Array{Float64}` : An array of corresponding quadrature weights\n", 727 | "\n", 728 | "##### Notes\n", 729 | "\n", 730 | "This function has many methods. I try to describe them here.\n", 731 | "\n", 732 | "`n` or `mu` can be a vector or a scalar. If just one is a scalar the other is repeated to match the length of the other. If both are scalars, then the number of repeats is inferred from `sig2`.\n", 733 | "\n", 734 | "`sig2` can be a matrix, vector or scalar. If it is a matrix, it is treated as the covariance matrix. If it is a vector, it is considered the diagonal of a diagonal covariance matrix. If it is a scalar it is repeated along the diagonal as many times as necessary, where the number of repeats is determined by the length of either `n` and/or `mu` (which ever is a vector).\n", 735 | "\n", 736 | "If all 3 are scalars, then 1d nodes are computed. `mu` and `sig2` are treated as the mean and variance of a 1d normal distribution\n", 737 | "\n", 738 | "##### References\n", 739 | "\n", 740 | "Miranda, Mario J, and Paul L Fackler. Applied Computational Economics and Finance, MIT Press, 2002.\n" 741 | ], 742 | "text/plain": [ 743 | "Computes nodes and weights for multivariate normal distribution.\n", 744 | "\n", 745 | "##### Arguments\n", 746 | "\n", 747 | " * `n::Union{Int, Vector{Int}}` : Number of desired nodes along each dimension\n", 748 | " * `mu::Union{Real, Vector{Real}}` : Mean along each dimension\n", 749 | " * `sig2::Union{Real, Vector{Real}, Matrix{Real}}(eye(length(n)))` : Covariance structure\n", 750 | "\n", 751 | "##### Returns\n", 752 | "\n", 753 | " * `nodes::Array{Float64}` : An array of quadrature nodes\n", 754 | " * `weights::Array{Float64}` : An array of corresponding quadrature weights\n", 755 | "\n", 756 | "##### Notes\n", 757 | "\n", 758 | "This function has many methods. I try to describe them here.\n", 759 | "\n", 760 | "`n` or `mu` can be a vector or a scalar. If just one is a scalar the other is repeated to match the length of the other. If both are scalars, then the number of repeats is inferred from `sig2`.\n", 761 | "\n", 762 | "`sig2` can be a matrix, vector or scalar. If it is a matrix, it is treated as the covariance matrix. If it is a vector, it is considered the diagonal of a diagonal covariance matrix. If it is a scalar it is repeated along the diagonal as many times as necessary, where the number of repeats is determined by the length of either `n` and/or `mu` (which ever is a vector).\n", 763 | "\n", 764 | "If all 3 are scalars, then 1d nodes are computed. `mu` and `sig2` are treated as the mean and variance of a 1d normal distribution\n", 765 | "\n", 766 | "##### References\n", 767 | "\n", 768 | "Miranda, Mario J, and Paul L Fackler. Applied Computational Economics and Finance, MIT Press, 2002.\n" 769 | ] 770 | }, 771 | "execution_count": 13, 772 | "metadata": {}, 773 | "output_type": "execute_result" 774 | } 775 | ], 776 | "source": [ 777 | "?qnwnorm" 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "execution_count": 14, 783 | "metadata": {}, 784 | "outputs": [ 785 | { 786 | "name": "stdout", 787 | "output_type": "stream", 788 | "text": [ 789 | "search: \u001b[1mq\u001b[22m\u001b[1mn\u001b[22m\u001b[1mw\u001b[22m\u001b[1mg\u001b[22m\u001b[1ma\u001b[22m\u001b[1mm\u001b[22m\u001b[1mm\u001b[22m\u001b[1ma\u001b[22m\n", 790 | "\n" 791 | ] 792 | }, 793 | { 794 | "data": { 795 | "text/markdown": [ 796 | "Computes nodes and weights for beta distribution\n", 797 | "\n", 798 | "##### Arguments\n", 799 | "\n", 800 | " * `n::Union{Int, Vector{Int}}` : Number of desired nodes along each dimension\n", 801 | " * `a::Union{Real, Vector{Real}}` : Shape parameter of the gamma distribution, along each dimension. Must be positive. Default is 1\n", 802 | " * `b::Union{Real, Vector{Real}}` : Scale parameter of the gamma distribution, along each dimension. Must be positive. Default is 1\n", 803 | "\n", 804 | "##### Returns\n", 805 | "\n", 806 | " * `nodes::Array{Float64}` : An array of quadrature nodes\n", 807 | " * `weights::Array{Float64}` : An array of corresponding quadrature weights\n", 808 | "\n", 809 | "##### Notes\n", 810 | "\n", 811 | "If any of the parameters to this function are scalars while others are vectors of length `n`, the the scalar parameter is repeated `n` times.\n", 812 | "\n", 813 | "##### References\n", 814 | "\n", 815 | "Miranda, Mario J, and Paul L Fackler. Applied Computational Economics and Finance, MIT Press, 2002.\n" 816 | ], 817 | "text/plain": [ 818 | "Computes nodes and weights for beta distribution\n", 819 | "\n", 820 | "##### Arguments\n", 821 | "\n", 822 | " * `n::Union{Int, Vector{Int}}` : Number of desired nodes along each dimension\n", 823 | " * `a::Union{Real, Vector{Real}}` : Shape parameter of the gamma distribution, along each dimension. Must be positive. Default is 1\n", 824 | " * `b::Union{Real, Vector{Real}}` : Scale parameter of the gamma distribution, along each dimension. Must be positive. Default is 1\n", 825 | "\n", 826 | "##### Returns\n", 827 | "\n", 828 | " * `nodes::Array{Float64}` : An array of quadrature nodes\n", 829 | " * `weights::Array{Float64}` : An array of corresponding quadrature weights\n", 830 | "\n", 831 | "##### Notes\n", 832 | "\n", 833 | "If any of the parameters to this function are scalars while others are vectors of length `n`, the the scalar parameter is repeated `n` times.\n", 834 | "\n", 835 | "##### References\n", 836 | "\n", 837 | "Miranda, Mario J, and Paul L Fackler. Applied Computational Economics and Finance, MIT Press, 2002.\n" 838 | ] 839 | }, 840 | "execution_count": 14, 841 | "metadata": {}, 842 | "output_type": "execute_result" 843 | } 844 | ], 845 | "source": [ 846 | "?qnwgamma" 847 | ] 848 | }, 849 | { 850 | "cell_type": "markdown", 851 | "metadata": { 852 | "slideshow": { 853 | "slide_type": "subslide" 854 | } 855 | }, 856 | "source": [ 857 | "#### Root finding and optimization\n", 858 | "\n", 859 | "QuantEcon.jl provides a handful of routines for solving or optimizing univariate functions.\n", 860 | "\n", 861 | "The solvers are: `brent` `brenth`, `bisect` and `ridder` " 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": 15, 867 | "metadata": { 868 | "slideshow": { 869 | "slide_type": "fragment" 870 | } 871 | }, 872 | "outputs": [ 873 | { 874 | "name": "stdout", 875 | "output_type": "stream", 876 | "text": [ 877 | "(solver, solver(f2, 0.5, 2.0)) = (QuantEcon.brent, 1.0000000000001794)\n", 878 | "(solver, solver(f2, 0.5, 2.0)) = (QuantEcon.brenth, 0.9999999999999903)\n", 879 | "(solver, solver(f2, 0.5, 2.0)) = (QuantEcon.bisect, 1.0000000000002274)\n", 880 | "(solver, solver(f2, 0.5, 2.0)) = (QuantEcon.ridder, 1.0000000000005003)\n" 881 | ] 882 | } 883 | ], 884 | "source": [ 885 | "f2(x) = x^2 - 1\n", 886 | "for solver in [brent, brenth, bisect, ridder]\n", 887 | " @show solver, solver(f2, 0.5, 2.0)\n", 888 | "end" 889 | ] 890 | }, 891 | { 892 | "cell_type": "markdown", 893 | "metadata": {}, 894 | "source": [ 895 | "The optimization routine is `golden_method`" 896 | ] 897 | }, 898 | { 899 | "cell_type": "code", 900 | "execution_count": 16, 901 | "metadata": {}, 902 | "outputs": [ 903 | { 904 | "data": { 905 | "text/plain": [ 906 | "(7.071019315919172e-9, 1.0)" 907 | ] 908 | }, 909 | "execution_count": 16, 910 | "metadata": {}, 911 | "output_type": "execute_result" 912 | } 913 | ], 914 | "source": [ 915 | "# golden_method finds a max, let's find the min of x^2 - 1 between -1 and 1 (which is 0.0)\n", 916 | "xstar, f2star = golden_method(x -> -f2(x), -1, 1)" 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": null, 922 | "metadata": { 923 | "collapsed": true 924 | }, 925 | "outputs": [], 926 | "source": [] 927 | } 928 | ], 929 | "metadata": { 930 | "anaconda-cloud": {}, 931 | "kernelspec": { 932 | "display_name": "Julia 0.6.1", 933 | "language": "julia", 934 | "name": "julia-0.6" 935 | }, 936 | "language_info": { 937 | "file_extension": ".jl", 938 | "mimetype": "application/julia", 939 | "name": "julia", 940 | "version": "0.6.1" 941 | }, 942 | "livereveal": { 943 | "scroll": true, 944 | "start_slideshow_at": "selected", 945 | "theme": "white", 946 | "transition": "fade" 947 | } 948 | }, 949 | "nbformat": 4, 950 | "nbformat_minor": 2 951 | } 952 | -------------------------------------------------------------------------------- /state_space_models/state_space_routines.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# State-Space Routines\n", 8 | "\n", 9 | "Our package `StateSpaceRoutines.jl` implements some common routines for state-space models.\n", 10 | "`DSGE.jl` relies on `StateSpaceRoutines.jl`'s filtering and smoothing functions, but the \n", 11 | "functionality provided is general enough to be used in other settings.\n", 12 | "\n", 13 | "The provided algorithms are:\n", 14 | "\n", 15 | "- Kalman filter (`kalman_filter`)\n", 16 | "- Tempered particle filter (`tempered_particle_filter`): [\"Tempered Particle Filtering\"](https://federalreserve.gov/econresdata/feds/2016/files/2016072pap.pdf) (2016)\n", 17 | "- Kalman smoothers:\n", 18 | " + `hamilton_smoother`: James Hamilton, [_Time Series Analysis_](https://www.amazon.com/Time-Analysis-James-Douglas-Hamilton/dp/0691042896) (1994)\n", 19 | " + `koopman_smoother`: S.J. Koopman, [\"Disturbance Smoother for State Space Models\"](https://www.jstor.org/stable/2336762) (_Biometrika_, 1993)\n", 20 | "- Simulation smoothers:\n", 21 | " + `carter_kohn_smoother`: C.K. Carter and R. Kohn, [\"On Gibbs Sampling for State Space Models\"](https://www.jstor.org/stable/2337125) (_Biometrika_, 1994)\n", 22 | " + `durbin_koopman_smoother`: J. Durbin and S.J. Koopman, [\"A Simple and Efficient Simulation Smoother for State Space Time Series Analysis\"](https://www.jstor.org/stable/4140605) (_Biometrika_, 2002)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "## Linear Estimation\n", 30 | "\n", 31 | "### Linear State Space System\n", 32 | "\n", 33 | "We assume a standard linear state space system, in which measumement error and fundamental shocks are \n", 34 | "independent. \n", 35 | "\n", 36 | "```\n", 37 | "z_{t+1} = CCC + TTT*z_t + RRR*ϵ_t (transition equation)\n", 38 | "y_t = DD + ZZ*z_t + η_t (measurement equation)\n", 39 | "\n", 40 | "ϵ_t ∼ N(0, QQ)\n", 41 | "η_t ∼ N(0, EE)\n", 42 | "Cov(ϵ_t, η_t) = 0\n", 43 | "```\n", 44 | "\n", 45 | "\n", 46 | "### Time-Invariant Methods\n", 47 | "\n", 48 | "`StateSpaceRoutines.jl`'s linear filtering and smoothing functions handle both the single-regime and multiple-regimes cases. \n", 49 | "\n", 50 | "\n", 51 | "```\n", 52 | "kalman_filter(data, TTT, RRR, CCC, QQ, ZZ, DD, EE, z0 = Vector(), P0 = Matrix(); allout = true, n_presample_periods = 0)\n", 53 | "\n", 54 | "hamilton_smoother(data, TTT, RRR, CCC, QQ, ZZ, DD, EE, z0, P0; n_presample_periods = 0)\n", 55 | "koopman_smoother(data, TTT, RRR, CCC, QQ, ZZ, DD, z0, P0, pred, vpred; n_presample_periods = 0)\n", 56 | "carter_kohn_smoother(data, TTT, RRR, CCC, QQ, ZZ, DD, EE, z0, P0; n_presample_periods = 0, draw_states = true)\n", 57 | "durbin_koopman_smoother(data, TTT, RRR, CCC, QQ, ZZ, DD, EE, z0, P0; n_presample_periods = 0, draw_states = true)\n", 58 | "```\n", 59 | "\n", 60 | "For more information, see the documentation for each function (e.g. by entering\n", 61 | "`?kalman_filter` in the REPL).\n", 62 | "\n", 63 | "\n", 64 | "First, let's consider a single-regime example using the `AnSchorfheide` model from `DSGE.jl`. This small, 3 equation New Keynesian model is described in \"Bayesian Estimation of DSGE Models\" by Sungbae An and Frank Schorfheide. " 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 1, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "using DSGE, StateSpaceRoutines\n", 74 | "using QuantEcon: solve_discrete_lyapunov\n", 75 | "using DataFrames, Plots" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 2, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "Dynamic Stochastic General Equilibrium Model\n", 87 | "no. states: 8\n", 88 | "no. anticipated shocks: 0\n", 89 | "data vintage: 171219\n", 90 | "description:\n", 91 | " Julia implementation of model defined in 'Bayesian Estimation of DSGE Models' by Sungbae An and Frank Schorfheide: AnSchorfheide, ss0\n" 92 | ] 93 | }, 94 | "execution_count": 2, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "# Setup the model and data\n", 101 | "m = AnSchorfheide()\n", 102 | "df = readtable(\"us.txt\", header = false, separator = ' ')\n", 103 | "data = convert(Matrix{Float64}, df)'\n", 104 | "dates = 1983:.25:2002.75 # data is from 1983Q1 to 2002Q4\n", 105 | "\n", 106 | "params = [2.09, 0.98, 2.25, 0.65, 0.34, 3.16, 0.51, 0.81, 0.98, 0.93, 0.19, 0.65, 0.24,\n", 107 | " 0.115985, 0.294166, 0.447587]\n", 108 | "update!(m, params)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 3, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "3×3 Array{Float64,2}:\n", 120 | " 0.0576 0.0 0.0 \n", 121 | " 0.0 0.4225 0.0 \n", 122 | " 0.0 0.0 0.0361" 123 | ] 124 | }, 125 | "execution_count": 3, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "# Solution to a Linear DSGE Model w/ IID Gaussian Errors\n", 132 | "system = compute_system(m)\n", 133 | "TTT = system[:TTT]\n", 134 | "RRR = system[:RRR]\n", 135 | "CCC = system[:CCC]\n", 136 | "EE = system[:EE]\n", 137 | "DD = system[:DD]\n", 138 | "ZZ = system[:ZZ]\n", 139 | "QQ = system[:QQ]" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "Running the `kalman_filter` will return the following items:\n", 147 | "- `log_likelihood`: log likelihood of the state-space model\n", 148 | "- `zend`: `Nz` x 1 final filtered state `z_{T|T}`\n", 149 | "- `Pend`: `Nz` x `Nz` final filtered state covariance matrix `P_{T|T}`\n", 150 | "- `pred`: `Nz` x `T` matrix of one-step predicted state vectors `z_{t|t-1}`\n", 151 | "- `vpred`: `Nz` x `Nz` x `T` array of mean squared errors `P_{t|t-1}` of\n", 152 | " predicted state vectors\n", 153 | "- `filt`: `Nz` x `T` matrix of filtered state vectors `z_{t|t}`\n", 154 | "- `vfilt`: `Nz` x `Nz` x `T` matrix containing mean squared errors `P_{t|t}` of\n", 155 | " filtered state vectors\n", 156 | "- `yprederror`: `Ny` x `T` matrix of observable prediction errors\n", 157 | " `y_t - y_{t|t-1}`\n", 158 | "- `ystdprederror`: `Ny` x `T` matrix of standardized observable prediction errors\n", 159 | " `V_{t|t-1} \\ (y_t - y_{t|t-1})`, where `y_t - y_{t|t-1} ∼ N(0, V_{t|t-1}`\n", 160 | "- `rmse`: 1 x `T` row vector of root mean squared prediction errors\n", 161 | "- `rmsd`: 1 x `T` row vector of root mean squared standardized prediction errors\n", 162 | "- `z0`: `Nz` x 1 initial state vector. This may have reassigned to the last\n", 163 | " presample state vector if `n_presample_periods > 0`\n", 164 | "- `P0`: `Nz` x `Nz` initial state covariance matrix. This may have reassigned to\n", 165 | " the last presample state covariance if `n_presample_periods > 0`\n", 166 | "- `marginal_loglh`: a vector of the marginal log likelihoods from t = 1 to T" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 4, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/plain": [ 177 | "8×80 Array{Float64,2}:\n", 178 | " -0.184703 -10.7754 -19.6144 … 3.06589 3.10173 3.09017 \n", 179 | " -0.72195 0.347567 0.193455 0.0519992 -0.227797 -0.311898\n", 180 | " 0.778333 0.77998 0.913637 -0.90262 -0.901968 -0.974907\n", 181 | " -0.354339 -11.6059 -20.1197 2.70587 2.61902 2.97034 \n", 182 | " 0.340308 -10.8586 -19.5903 2.90376 3.09887 3.12445 \n", 183 | " 0.316583 0.646672 0.691767 … -0.608649 -0.694255 -0.770598\n", 184 | " 0.139365 -10.6002 -19.1972 2.89796 3.02754 3.03739 \n", 185 | " -0.207616 0.266262 0.217263 -0.106976 -0.230791 -0.278541" 186 | ] 187 | }, 188 | "execution_count": 4, 189 | "metadata": {}, 190 | "output_type": "execute_result" 191 | } 192 | ], 193 | "source": [ 194 | "n_states = size(TTT, 1)\n", 195 | "kal = kalman_filter(data, TTT, RRR, CCC, QQ, ZZ, DD, EE, zeros(n_states,), 1e6*eye(n_states, n_states))\n", 196 | "\n", 197 | "# entry 6 is the n_periods by n_states matrix of filtered states\n", 198 | "kal[6] " 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 5, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "DataStructures.OrderedDict{Symbol,Int64} with 8 entries:\n", 210 | " :y_t => 1\n", 211 | " :π_t => 2\n", 212 | " :R_t => 3\n", 213 | " :y_t1 => 4\n", 214 | " :g_t => 5\n", 215 | " :z_t => 6\n", 216 | " :Ey_t1 => 7\n", 217 | " :Eπ_t1 => 8" 218 | ] 219 | }, 220 | "execution_count": 5, 221 | "metadata": {}, 222 | "output_type": "execute_result" 223 | } 224 | ], 225 | "source": [ 226 | "m.endogenous_states # display the list of states" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "Now that we have run the filter, we can see the filtered estimates for the time series of `z_t`, the technology process:" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 6, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "data": { 243 | "text/html": [ 244 | "\n", 245 | "\n", 246 | "\n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | "\n", 251 | "\n", 254 | "\n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | "\n", 259 | "\n", 262 | "\n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | "\n", 267 | "\n", 270 | "\n", 273 | "\n", 276 | "\n", 279 | "\n", 282 | "\n", 285 | "\n", 288 | "\n", 291 | "\n", 294 | "\n", 297 | "\n", 300 | "\n", 303 | "\n", 306 | "\n", 309 | "\n", 312 | "\n", 315 | "\n", 318 | "\n", 321 | "\n", 324 | "\n", 327 | "\n", 330 | "\n", 333 | "\n", 336 | "\n", 339 | "\n", 340 | "1985\n", 341 | "\n", 342 | "\n", 343 | "1990\n", 344 | "\n", 345 | "\n", 346 | "1995\n", 347 | "\n", 348 | "\n", 349 | "2000\n", 350 | "\n", 351 | "\n", 352 | "-0.75\n", 353 | "\n", 354 | "\n", 355 | "-0.50\n", 356 | "\n", 357 | "\n", 358 | "-0.25\n", 359 | "\n", 360 | "\n", 361 | "0.00\n", 362 | "\n", 363 | "\n", 364 | "0.25\n", 365 | "\n", 366 | "\n", 367 | "0.50\n", 368 | "\n", 369 | "\n", 370 | "0.75\n", 371 | "\n", 372 | "\n", 383 | "\n", 386 | "\n", 389 | "\n", 392 | "\n", 393 | "filtered z\n", 394 | "\n", 395 | "\n" 396 | ] 397 | }, 398 | "execution_count": 6, 399 | "metadata": {}, 400 | "output_type": "execute_result" 401 | } 402 | ], 403 | "source": [ 404 | "z_filt = kal[6][6,:]\n", 405 | "plot(dates, z_filt, label = \"filtered z\")" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "We can compare these filtered states with the smoothed results of the `hamilton_smoother`:" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 7, 418 | "metadata": {}, 419 | "outputs": [ 420 | { 421 | "data": { 422 | "text/html": [ 423 | "\n", 424 | "\n", 425 | "\n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | "\n", 430 | "\n", 433 | "\n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | "\n", 438 | "\n", 441 | "\n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | "\n", 446 | "\n", 449 | "\n", 452 | "\n", 455 | "\n", 458 | "\n", 461 | "\n", 464 | "\n", 467 | "\n", 470 | "\n", 473 | "\n", 476 | "\n", 479 | "\n", 482 | "\n", 485 | "\n", 488 | "\n", 491 | "\n", 494 | "\n", 497 | "\n", 500 | "\n", 503 | "\n", 506 | "\n", 509 | "\n", 512 | "\n", 515 | "\n", 518 | "\n", 519 | "1985\n", 520 | "\n", 521 | "\n", 522 | "1990\n", 523 | "\n", 524 | "\n", 525 | "1995\n", 526 | "\n", 527 | "\n", 528 | "2000\n", 529 | "\n", 530 | "\n", 531 | "-0.75\n", 532 | "\n", 533 | "\n", 534 | "-0.50\n", 535 | "\n", 536 | "\n", 537 | "-0.25\n", 538 | "\n", 539 | "\n", 540 | "0.00\n", 541 | "\n", 542 | "\n", 543 | "0.25\n", 544 | "\n", 545 | "\n", 546 | "0.50\n", 547 | "\n", 548 | "\n", 549 | "0.75\n", 550 | "\n", 551 | "\n", 562 | "\n", 573 | "\n", 576 | "\n", 579 | "\n", 582 | "\n", 583 | "filtered z\n", 584 | "\n", 585 | "\n", 588 | "\n", 589 | "smoothed z\n", 590 | "\n", 591 | "\n" 592 | ] 593 | }, 594 | "execution_count": 7, 595 | "metadata": {}, 596 | "output_type": "execute_result" 597 | } 598 | ], 599 | "source": [ 600 | "smoothed = hamilton_smoother(data, TTT, RRR, CCC, QQ, ZZ, DD, EE, zeros(n_states,), 1e6*eye(n_states, n_states))\n", 601 | "smoothed_states = smoothed[1]\n", 602 | "smoothed_shocks = smoothed[2]\n", 603 | "z_smoothed = smoothed_states[6,:]\n", 604 | "\n", 605 | "plot(dates, z_filt, label = \"filtered z\");\n", 606 | "plot!(dates, z_smoothed, label = \"smoothed z\")" 607 | ] 608 | }, 609 | { 610 | "cell_type": "markdown", 611 | "metadata": {}, 612 | "source": [ 613 | "Here, the estimates of $z_{t|T}$ are approximately the same as $z_{t|t}$. " 614 | ] 615 | }, 616 | { 617 | "cell_type": "markdown", 618 | "metadata": {}, 619 | "source": [ 620 | "### Regime-Switching Methods\n", 621 | "\n", 622 | "All of the provided algorithms can handle time-varying state-space systems. To\n", 623 | "do this, define `regime_indices`, a `Vector{Range{Int64}}` of length\n", 624 | "`n_regimes`, where `regime_indices[i]` indicates the range of periods `t` in\n", 625 | "regime `i`. Let `TTT_i`, `RRR_i`, etc. denote the state-space matrices in regime\n", 626 | "`i`. Then the state space is given by:\n", 627 | "\n", 628 | "```\n", 629 | "z_{t+1} = CCC_i + TTT_i*z_t + RRR_i*ϵ_t (transition equation)\n", 630 | "y_t = DD_i + ZZ_i*z_t + η_t (measurement equation)\n", 631 | "\n", 632 | "ϵ_t ∼ N(0, QQ_i)\n", 633 | "η_t ∼ N(0, EE_i)\n", 634 | "```\n", 635 | "\n", 636 | "Letting `TTTs = [TTT_1, ..., TTT_{n_regimes}]`, etc., we can then call the time-\n", 637 | "varying methods of the algorithms:\n", 638 | "\n", 639 | "```\n", 640 | "kalman_filter(regime_indices, data, TTTs, RRRs, CCCs, QQs, ZZs, DDs, EEs, z0 = Vector(), P0 = Matrix(); allout = true, n_presample_periods = 0)\n", 641 | "\n", 642 | "hamilton_smoother(regime_indices, data, TTTs, RRRs, CCCs, QQs, ZZs, DDs, EEs, z0, P0; n_presample_periods = 0)\n", 643 | "koopman_smoother(regime_indices, data, TTTs, RRRs, CCCs, QQs, ZZs, DDs, z0, P0, pred, vpred; n_presample_periods = 0)\n", 644 | "carter_kohn_smoother(regime_indices, data, TTTs, RRRs, CCCs, QQs, ZZs, DDs, EEs, z0, P0; n_presample_periods = 0, draw_states = true)\n", 645 | "durbin_koopman_smoother(regime_indices, data, TTTs, RRRs, CCCs, QQs, ZZs, DDs, EEs, z0, P0; n_presample_periods = 0, draw_states = true)\n", 646 | "```\n", 647 | "\n", 648 | "In DSGE.jl, we use this regime-switching framework to differentiate between the pre-forward guidance regime and the Fed's current regime." 649 | ] 650 | }, 651 | { 652 | "cell_type": "markdown", 653 | "metadata": {}, 654 | "source": [ 655 | "As an example, we will filter our `AnSchorfheide` model on two regimes: 1983-1987 and 1988-2002. These correspond loosely to the pre-Great Moderation period and the Great Moderation. The regimes will be identical except that the variance of the monetary policy shock will be larger in the second:" 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "execution_count": 9, 661 | "metadata": {}, 662 | "outputs": [ 663 | { 664 | "data": { 665 | "text/plain": [ 666 | "Dynamic Stochastic General Equilibrium Model\n", 667 | "no. states: 8\n", 668 | "no. anticipated shocks: 0\n", 669 | "data vintage: 171219\n", 670 | "description:\n", 671 | " Julia implementation of model defined in 'Bayesian Estimation of DSGE Models' by Sungbae An and Frank Schorfheide: AnSchorfheide, ss0\n" 672 | ] 673 | }, 674 | "execution_count": 9, 675 | "metadata": {}, 676 | "output_type": "execute_result" 677 | } 678 | ], 679 | "source": [ 680 | "m_2 = AnSchorfheide()\n", 681 | "params_2 = [2.09, 0.98, 2.25, 0.65, 0.34, 3.16, 0.51, 0.81, 0.98, 0.93, 0.19*3, 0.65, 0.24,\n", 682 | " 0.115985, 0.294166, 0.447587]\n", 683 | "update!(m_2, params_2)" 684 | ] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "execution_count": 38, 689 | "metadata": {}, 690 | "outputs": [ 691 | { 692 | "data": { 693 | "text/plain": [ 694 | "2-element Array{Range{Int64},1}:\n", 695 | " 1:16 \n", 696 | " 17:80" 697 | ] 698 | }, 699 | "execution_count": 38, 700 | "metadata": {}, 701 | "output_type": "execute_result" 702 | } 703 | ], 704 | "source": [ 705 | "system_2 = compute_system(m)\n", 706 | "TTT_2 = system_2[:TTT]\n", 707 | "RRR_2 = system_2[:RRR]\n", 708 | "CCC_2 = system_2[:CCC]\n", 709 | "EE_2 = system_2[:EE]\n", 710 | "DD_2 = system_2[:DD]\n", 711 | "ZZ_2 = system_2[:ZZ]\n", 712 | "QQ_2 = system_2[:QQ]\n", 713 | "\n", 714 | "TTTs = [TTT_2, TTT]\n", 715 | "RRRs = [RRR_2, RRR]\n", 716 | "CCCs = [CCC_2, CCC]\n", 717 | "EEs = [EE_2, EE]\n", 718 | "DDs = [DD_2, DD]\n", 719 | "ZZs = [ZZ_2, ZZ]\n", 720 | "QQs = [QQ_2, QQ]\n", 721 | "\n", 722 | "regime_indices = Range{Int64}[1:16,17:80]" 723 | ] 724 | }, 725 | { 726 | "cell_type": "code", 727 | "execution_count": null, 728 | "metadata": {}, 729 | "outputs": [], 730 | "source": [ 731 | "kal_regime = kalman_filter(regime_indices, data, TTTs, RRRs, CCCs, QQs, ZZs, DDs, EEs, \n", 732 | " zeros(n_states,), 1e6*eye(n_states, n_states))\n", 733 | "smoothed_regime = hamilton_smoother(regime_indices, data, TTTs, RRRs, CCCs, QQs, ZZs, DDs, EEs, \n", 734 | " zeros(n_states,), 1e6*eye(n_states, n_states))" 735 | ] 736 | }, 737 | { 738 | "cell_type": "code", 739 | "execution_count": null, 740 | "metadata": {}, 741 | "outputs": [], 742 | "source": [ 743 | "z_filt_regime = kal_regime[6][6,:]\n", 744 | "z_smoothed_regime = smoothed_regime[1][6,:]\n", 745 | "\n", 746 | "plot(dates, z_filt_regime, label = \"filtered z\");\n", 747 | "plot!(dates, z_smoothed_regime, label = \"smoothed z\")" 748 | ] 749 | }, 750 | { 751 | "cell_type": "markdown", 752 | "metadata": {}, 753 | "source": [ 754 | "## Nonlinear Estimation\n", 755 | "\n", 756 | "The tempered particle filter is a particle filtering method which can approximate the log-likelihood value implied by a general (potentially non-linear) state space system with the following representation:\n", 757 | "\n", 758 | "### General State Space System\n", 759 | "```\n", 760 | "z_{t+1} = Φ(z_t, ϵ_t) (transition equation)\n", 761 | "y_t = Ψ(z_t) + u_t (measurement equation)\n", 762 | "\n", 763 | "ϵ_t ∼ F_ϵ(∙; θ)\n", 764 | "u_t ∼ N(0, HH), where HH is the variance matrix of the i.i.d measurement error\n", 765 | "Cov(ϵ_t, u_t) = 0\n", 766 | "```\n", 767 | "- The documentation and code are located in [src/filters/tempered_particle_filter](https://github.com/FRBNY-DSGE/StateSpaceRoutines.jl/tree/doc/src/filters/tempered_particle_filter).\n", 768 | "- The example is located in [docs/examples/tempered_particle_filter](https://github.com/FRBNY-DSGE/StateSpaceRoutines.jl/tree/doc/docs/examples/tempered_particle_filter)" 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 42, 774 | "metadata": {}, 775 | "outputs": [ 776 | { 777 | "data": { 778 | "text/plain": [ 779 | "FullNormal(\n", 780 | "dim: 3\n", 781 | "μ: [0.0, 0.0, 0.0]\n", 782 | "Σ: [0.0134524 0.0 0.0; 0.0 0.0865339 0.0; 0.0 0.0 0.200334]\n", 783 | ")\n" 784 | ] 785 | }, 786 | "execution_count": 42, 787 | "metadata": {}, 788 | "output_type": "execute_result" 789 | } 790 | ], 791 | "source": [ 792 | "# Define the (potentially non-linear) transition and measurement equations\n", 793 | "Φ(s_t::Vector{Float64}, ϵ_t::Vector{Float64}) = TTT*s_t + RRR*ϵ_t + CCC\n", 794 | "Ψ(s_t::Vector{Float64}, u_t::Vector{Float64}) = ZZ*s_t + DD + u_t\n", 795 | "\n", 796 | "F_ϵ = Distributions.MvNormal(zeros(size(QQ, 1)), QQ)\n", 797 | "F_u = Distributions.MvNormal(zeros(size(EE, 1)), EE)" 798 | ] 799 | }, 800 | { 801 | "cell_type": "markdown", 802 | "metadata": {}, 803 | "source": [] 804 | } 805 | ], 806 | "metadata": { 807 | "kernelspec": { 808 | "display_name": "Julia 0.6.1", 809 | "language": "julia", 810 | "name": "julia-0.6" 811 | }, 812 | "language_info": { 813 | "file_extension": ".jl", 814 | "mimetype": "application/julia", 815 | "name": "julia", 816 | "version": "0.6.1" 817 | } 818 | }, 819 | "nbformat": 4, 820 | "nbformat_minor": 2 821 | } 822 | --------------------------------------------------------------------------------