├── dsgejl ├── .gitignore ├── output_data │ └── an_schorfheide │ │ └── ss0 │ │ └── estimate │ │ └── raw │ │ └── paramsmode_vint=170528.h5 ├── util.jl ├── plot_history_and_forecast.jl ├── input_data │ └── data │ │ ├── population_data_levels_170528.csv │ │ ├── fred_170528.csv │ │ └── data_170528.csv ├── ma1.jl ├── exercise.ipynb └── slides.ipynb ├── intro ├── main.pdf ├── fast_loop_examples │ ├── makefile │ ├── ar1_sample_mean.jl │ └── ar1_sample_mean.c └── hello.ipynb ├── .gitignore ├── README.md ├── packages ├── index.ipynb ├── optimization_solvers.ipynb ├── numerics_stats.ipynb └── economics.ipynb └── julia_language └── julia_language.ipynb /dsgejl/.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | 3 | *.h5 4 | *.jld -------------------------------------------------------------------------------- /intro/main.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FRBNY-DSGE/CEF_2017_Workshop/HEAD/intro/main.pdf -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.aux 2 | .ipynb_checkpoints/ 3 | *.log 4 | *.nav 5 | *.out 6 | *.toc 7 | *.snm 8 | *.vrb 9 | -------------------------------------------------------------------------------- /intro/fast_loop_examples/makefile: -------------------------------------------------------------------------------- 1 | maketest: 2 | gcc -Wall ar1_sample_mean.c -lgsl -lgslcblas -lm -o foo 3 | -------------------------------------------------------------------------------- /dsgejl/output_data/an_schorfheide/ss0/estimate/raw/paramsmode_vint=170528.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FRBNY-DSGE/CEF_2017_Workshop/HEAD/dsgejl/output_data/an_schorfheide/ss0/estimate/raw/paramsmode_vint=170528.h5 -------------------------------------------------------------------------------- /intro/fast_loop_examples/ar1_sample_mean.jl: -------------------------------------------------------------------------------- 1 | 2 | function ar1_sample_mean(N, beta, alpha, s) 3 | sm = 0.0 4 | x = beta / (1 - alpha) 5 | for i in 1:N 6 | sm += x 7 | x = beta + alpha * x + s * randn() 8 | end 9 | return sm / N 10 | end 11 | 12 | N = 1e7 13 | beta = 1.0 14 | alpha = 0.9 15 | s = 1.0 16 | tic() 17 | result = ar1_sample_mean(N, beta, alpha, s) 18 | toc() 19 | println("mean = $result") 20 | -------------------------------------------------------------------------------- /intro/hello.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## A First Julia Program\n", 8 | "\n", 9 | "#### Erica Moszkowski\n", 10 | "#### Prepared for the CEF 2017 Julia Workshop" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "First program: always Hello World!" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 1, 23 | "metadata": { 24 | "collapsed": false 25 | }, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "Hello world\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "println(\"Hello world\")" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "Ok, done! On to learning syntax." 44 | ] 45 | } 46 | ], 47 | "metadata": { 48 | "anaconda-cloud": {}, 49 | "kernelspec": { 50 | "display_name": "Julia 0.4.5", 51 | "language": "julia", 52 | "name": "julia-0.4" 53 | }, 54 | "language_info": { 55 | "file_extension": ".jl", 56 | "mimetype": "application/julia", 57 | "name": "julia", 58 | "version": "0.4.5" 59 | } 60 | }, 61 | "nbformat": 4, 62 | "nbformat_minor": 1 63 | } 64 | -------------------------------------------------------------------------------- /intro/fast_loop_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("mean = %g\n", sample_mean); 48 | printf("time elapsed = %g seconds\n", cpu_time_used); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CEF 2017 Workshop 2 | 3 | Code and teaching material for "Modeling with Julia -- with an Application to the New York Fed DSGE", a workshop at [CEF 2017](http://comp-econ.org/CEF_2017/index.htm). 4 | 5 | ## Software 6 | 7 | We assume that participants will have access to 8 | 9 | * [Julia](http://julialang.org/) 10 | * [IJulia](https://github.com/JuliaLang/IJulia.jl) and [Jupyter notebooks](https://jupyter.org/) 11 | * [DSGE](https://github.com/FRBNY-DSGE/DSGE.jl) 12 | 13 | For those using their own machines, it's possible install all of this (except DSGE.jl) and more in one go by installing 14 | 15 | * [JuliaPro](http://juliacomputing.com/products/juliapro.html) 16 | 17 | Make sure that you have the latest version, which is currently JuliaPro 18 | 0.5.1. A free version is now available. 19 | 20 | ## Resources 21 | 22 | * [Cheat sheets](http://cheatsheets.quantecon.org/) for MATLAB and Python users 23 | * [Lectures](http://lectures.quantecon.org/) for learning Julia, or for economics concepts 24 | * [Discourse forum](http://discourse.quantecon.org/) for QuantEcon 25 | * [Discourse forum](http://discourse.julialang.org/) for Julia questions 26 | * [Julia Day](https://juliacomputing.com/blog/2016/11/01/Julia-Day-NY.html): videos with talks about Julia and finance 27 | 28 | ## Contact 29 | 30 | * Abhi Gupta (New York Fed): abhi.gupta@ny.frb.org 31 | * Pearl Li (New York Fed): pearl.li@ny.frb.org 32 | * Erica Moszkowski (New York Fed): erica.moszkowski@ny.frb.org 33 | * Chase Coleman (NYU Stern): cc7768@gmail.com 34 | * Spencer Lyon (NYU Stern): spencer.lyon@stern.nyu.edu 35 | -------------------------------------------------------------------------------- /dsgejl/util.jl: -------------------------------------------------------------------------------- 1 | function quarter_date_to_number(date::Date) 2 | y = Dates.year(date) 3 | m = Dates.month(date) 4 | if m == 3 5 | return y 6 | elseif m == 6 7 | return y + 0.25 8 | elseif m == 9 9 | return y + 0.5 10 | elseif m == 12 11 | return y + 0.75 12 | end 13 | end 14 | 15 | function get_date_ticks(start_date::Date, end_date::Date; 16 | tick_size::Int = 5) 17 | dates = DSGE.quarter_range(start_date, end_date) 18 | get_date_ticks(dates, tick_size = tick_size) 19 | end 20 | 21 | function get_date_ticks(dates::AbstractArray{Date, 1}; 22 | tick_size::Int = 5) 23 | datenums = map(quarter_date_to_number, dates) 24 | t0 = ceil(datenums[1] / tick_size) * tick_size 25 | t1 = datenums[end] 26 | ticks = t0:tick_size:t1 27 | return ticks 28 | end 29 | 30 | function get_date_limits(start_date::Nullable{Date}, end_date::Nullable{Date}, 31 | dates::AbstractArray{Date, 1}) 32 | if isnull(start_date) 33 | start_date = Nullable(dates[1]) 34 | end 35 | if isnull(end_date) 36 | end_date = Nullable(dates[end]) 37 | end 38 | 39 | return start_date, end_date 40 | end 41 | 42 | function get_date_limit_indices(start_date::Nullable{Date}, end_date::Nullable{Date}, 43 | dates::AbstractArray{Date, 1}) 44 | start_ind = if isnull(start_date) 45 | 1 46 | else 47 | if dates[1] <= get(start_date) <= dates[end] 48 | findfirst(dates, get(start_date)) 49 | elseif get(start_date) < dates[1] 50 | 1 51 | else 52 | error("start_date $(get(start_date)) cannot be after last forecast period $(dates[end])") 53 | end 54 | end 55 | end_ind = if isnull(end_date) 56 | length(dates) 57 | else 58 | if dates[1] <= get(end_date) <= dates[end] 59 | findfirst(dates, get(end_date)) 60 | elseif get(end_date) > dates[end] 61 | length(dates) 62 | else 63 | error("end_date $(get(end_date)) cannot be before first historical period $(dates[1])") 64 | end 65 | end 66 | return start_ind, end_ind 67 | end 68 | 69 | function plot_actual!(p::Plots.Subplot{Plots.GRBackend}, v::Float64) 70 | # Get y-axis limits 71 | ylim = p.attr[:yaxis].d[:lims] 72 | 73 | # Plot actual value 74 | plot!(p, [v, v], [ylim[1], ylim[2]], linewidth = 3, label = "", color = :black) 75 | end 76 | 77 | function plot_actual!(p::Plots.Subplot{Plots.PlotlyBackend}, v::Float64) 78 | # Get y-axis limits 79 | ylim = p.attr[:yaxis].d[:extrema] 80 | 81 | # Plot actual value 82 | plot!(p, [v, v], [ylim.emin, ylim.emax], linewidth = 3, label = "", color = :black) 83 | end 84 | -------------------------------------------------------------------------------- /dsgejl/plot_history_and_forecast.jl: -------------------------------------------------------------------------------- 1 | using DSGE, Plots 2 | 3 | function plot_history_and_forecast(var::Symbol, history::MeansBands, forecast::MeansBands; 4 | start_date::Nullable{Date} = Nullable{Date}(), 5 | end_date::Nullable{Date} = Nullable{Date}(), 6 | output_file::String = "", 7 | hist_label::String = "History", 8 | forecast_label::String = "Forecast", 9 | hist_color::Colorant = RGBA(0., 0., 0., 1.), 10 | forecast_mean_color::Colorant = RGBA(1., 0., 0., 1.), 11 | forecast_band_color::Colorant = RGBA(0., 0., 1., 0.1), 12 | tick_size::Int = 5, 13 | legend = :best) 14 | # Concatenate MeansBands 15 | combined = cat(history, forecast) 16 | 17 | # Dates 18 | start_date, end_date = get_date_limits(start_date, end_date, combined.means[:date]) 19 | start_ind, end_ind = get_date_limit_indices(start_date, end_date, combined.means[:date]) 20 | datenums = map(quarter_date_to_number, combined.means[:date]) 21 | 22 | # Indices 23 | n_hist_periods = size(history.means, 1) 24 | n_fcast_periods = size(forecast.means, 1) 25 | n_all_periods = n_hist_periods + n_fcast_periods 26 | 27 | hist_inds = start_ind:min(end_ind, n_hist_periods) 28 | fcast_inds = max(start_ind, n_hist_periods):end_ind 29 | all_inds = start_ind:end_ind 30 | 31 | p = Plots.plot(legend = legend) 32 | 33 | # Plot bands 34 | band_percents = DSGE.which_density_bands(combined; uniquify = true) 35 | for pct in band_percents 36 | plot!(p, datenums[all_inds], combined.bands[var][all_inds, Symbol("$pct UB")], 37 | fillto = combined.bands[var][all_inds, Symbol("$pct LB")], 38 | label = "", color = forecast_band_color, α = 0.10) 39 | end 40 | 41 | # Plot mean 42 | plot!(p, datenums[hist_inds], combined.means[hist_inds, var], label = hist_label, 43 | linewidth = 2, linecolor = hist_color) 44 | plot!(p, datenums[fcast_inds], combined.means[fcast_inds, var], label = forecast_label, 45 | linewidth = 2, linecolor = forecast_mean_color) 46 | 47 | # Set x-axis limits and ticks 48 | # xlims attribute only sets finite values, e.g. (-Inf, 2) sets only the right limit 49 | t0 = isnull(start_date) ? -Inf : quarter_date_to_number(get(start_date)) 50 | t1 = isnull(end_date) ? Inf : quarter_date_to_number(get(end_date)) 51 | date_ticks = get_date_ticks(get(start_date), get(end_date), tick_size = tick_size) 52 | xaxis!(p, xlims = (t0, t1), xtick = date_ticks) 53 | 54 | # Save if output_file provided 55 | if !isempty(output_file) 56 | output_dir = dirname(output_file) 57 | !isdir(output_dir) && mkdir(output_dir) 58 | Plots.savefig(output_file) 59 | println("Saved $output_file") 60 | end 61 | 62 | return p 63 | end 64 | -------------------------------------------------------------------------------- /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 | "** @ CEF 2017** \n", 14 | "\n", 15 | "**Authors**: Chase Coleman and Spencer Lyon\n", 16 | "\n", 17 | "**Date**: 27 June 2017" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "slideshow": { 24 | "slide_type": "fragment" 25 | } 26 | }, 27 | "source": [ 28 | "- Key strength of Julia is its [package ecosystem](https://pkg.julialang.org)\n", 29 | "- 1,446 packages as of 6-26-17\n", 30 | "- Great diversity:" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": { 37 | "scrolled": true, 38 | "slideshow": { 39 | "slide_type": "fragment" 40 | } 41 | }, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "10-element Array{String,1}:\n", 47 | " \"MLDataUtils\" \n", 48 | " \"Sugar\" \n", 49 | " \"CUDAnativelib\" \n", 50 | " \"HTTPClient\" \n", 51 | " \"CUTEst\" \n", 52 | " \"RandomMatrices\"\n", 53 | " \"JuLIP\" \n", 54 | " \"EMIRT\" \n", 55 | " \"ColorSchemes\" \n", 56 | " \"Pedigrees\" " 57 | ] 58 | }, 59 | "execution_count": 2, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "pkg_list = readdir(joinpath(expanduser(\"~\"), \".julia\", \"v0.5\", \"METADATA\"))\n", 66 | "pkg_list[rand(1:length(pkg_list), 10)]" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": { 72 | "slideshow": { 73 | "slide_type": "slide" 74 | } 75 | }, 76 | "source": [ 77 | "- Some are very high quality (JuMP, NLopt, Distributions, QuantEcon, DSGE, TensorFlow, Cxx)\n", 78 | "- The goal of these notebooks is to introduce the most useful packages for economic modeling\n", 79 | "- Will break down by topic:\n", 80 | " - [Function approximation](function_approximation.ipynb)\n", 81 | " - [Numerical/statistics tools](numerics_stats.ipynb)\n", 82 | " - [Optimization/solvers](optimization_solvers.ipynb)\n", 83 | " - [Plotting](plotting.ipynb)\n", 84 | " - [Economics](economics.ipynb)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": { 91 | "collapsed": true 92 | }, 93 | "outputs": [], 94 | "source": [] 95 | } 96 | ], 97 | "metadata": { 98 | "anaconda-cloud": {}, 99 | "kernelspec": { 100 | "display_name": "Julia 0.6.0", 101 | "language": "julia", 102 | "name": "julia-0.6" 103 | }, 104 | "language_info": { 105 | "file_extension": ".jl", 106 | "mimetype": "application/julia", 107 | "name": "julia", 108 | "version": "0.6.0" 109 | }, 110 | "livereveal": { 111 | "scroll": true, 112 | "start_slideshow_at": "selected", 113 | "theme": "white", 114 | "transition": "fade" 115 | } 116 | }, 117 | "nbformat": 4, 118 | "nbformat_minor": 2 119 | } 120 | -------------------------------------------------------------------------------- /dsgejl/input_data/data/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 -------------------------------------------------------------------------------- /dsgejl/input_data/data/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/exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise: MA(1) Model" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "This set of exercises will walk through implementing and using the following MA(1) model in DSGE.jl:\n", 15 | "\n", 16 | "$$\n", 17 | "\\begin{align*}\n", 18 | "x_t &= u_t + \\beta u_{t-1} + \\mu \\\\\n", 19 | "u_t &\\sim N(0, \\sigma^2)\n", 20 | "\\end{align*}\n", 21 | "$$\n", 22 | "\n", 23 | "which we can also express in state-space form:\n", 24 | "\n", 25 | "$$\n", 26 | "\\begin{align*}\n", 27 | "\\begin{bmatrix} u_t \\\\ u_{t-1} \\end{bmatrix} &= \\begin{bmatrix} 0 & 0 \\\\ 1 & 0 \\end{bmatrix} \\begin{bmatrix} u_{t-1} \\\\ u_{t-2} \\end{bmatrix} + \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} \\begin{bmatrix} u_t \\end{bmatrix} & \\text{(transition equation)} \\\\\n", 28 | "\\begin{bmatrix} x_t \\end{bmatrix} &= \\begin{bmatrix} 1 & \\beta \\end{bmatrix} \\begin{bmatrix} u_t \\\\ u_{t-1} \\end{bmatrix} + \\begin{bmatrix} \\mu \\end{bmatrix} & \\text{(measurement equation)}\n", 29 | "\\end{align*}\n", 30 | "$$" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": { 37 | "collapsed": false 38 | }, 39 | "outputs": [], 40 | "source": [ 41 | "using DSGE\n", 42 | "using DataFrames, DataStructures, Distributions, Plots\n", 43 | "include(\"util.jl\")\n", 44 | "\n", 45 | "# This file defines the MA1 type\n", 46 | "include(\"ma1.jl\")" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": { 53 | "collapsed": false 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "# See docstring for MA1\n", 58 | "?MA1" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": { 65 | "collapsed": false 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "# Initialize MA(1) model\n", 70 | "m = MA1()\n", 71 | "\n", 72 | "m <= Setting(:saveroot, dirname(@__FILE__))\n", 73 | "m <= Setting(:date_presample_start, DSGE.quartertodate(\"2001-Q1\"))\n", 74 | "m <= Setting(:date_mainsample_start, DSGE.quartertodate(\"2001-Q1\"))\n", 75 | "m <= Setting(:date_forecast_start, DSGE.quartertodate(\"2026-Q1\"))\n", 76 | "m <= Setting(:n_mh_simulations, 500)\n", 77 | "m <= Setting(:n_mh_blocks, 10)\n", 78 | "m <= Setting(:n_mh_burn, 2)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "collapsed": false 86 | }, 87 | "outputs": [], 88 | "source": [ 89 | "m.parameters" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": { 96 | "collapsed": false 97 | }, 98 | "outputs": [], 99 | "source": [ 100 | "m.endogenous_states" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": { 107 | "collapsed": false 108 | }, 109 | "outputs": [], 110 | "source": [ 111 | "m.exogenous_shocks" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [], 121 | "source": [ 122 | "m.observables" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "## Exercise 1\n", 130 | "\n", 131 | "Given the following definition of `eqcond` (which returns the equilibrium condition matrices for an `MA1` model), fill in the `ZZ` and `QQ` matrices in `measurement`." 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": { 138 | "collapsed": true 139 | }, 140 | "outputs": [], 141 | "source": [ 142 | "function DSGE.eqcond(m::MA1)\n", 143 | " endo = m.endogenous_states\n", 144 | " exo = m.exogenous_shocks\n", 145 | " eq = m.equilibrium_conditions\n", 146 | "\n", 147 | " Γ0 = zeros(n_states(m), n_states(m))\n", 148 | " Γ1 = zeros(n_states(m), n_states(m))\n", 149 | " C = zeros(n_states(m))\n", 150 | " Ψ = zeros(n_states(m), n_shocks_exogenous(m))\n", 151 | " Π = zeros(n_states(m), n_shocks_expectational(m))\n", 152 | "\n", 153 | " # Γ0*s_t = Γ1*s_{t-1} + Ψ*ϵ_t + Π*η_t + C\n", 154 | " # s_t = [u_t, u_{t-1}]'\n", 155 | " # ϵ_t = [u_t]'\n", 156 | "\n", 157 | " # Row 1: u_t = 0*u_{t-1} + 0*u_{t-2} + u_t\n", 158 | " Γ0[eq[:eq_u_t], endo[:u_t]] = 1\n", 159 | " Ψ[eq[:eq_u_t], exo[:u_t]] = 1\n", 160 | "\n", 161 | " # Row 2: u_{t-1} = u_{t-1} + 0 u_{t-2} + 0 u_t\n", 162 | " Γ0[eq[:eq_u_t1], endo[:u_t1]] = 1\n", 163 | " Γ1[eq[:eq_u_t1], endo[:u_t]] = 1\n", 164 | "\n", 165 | " return Γ0, Γ1, C, Ψ, Π\n", 166 | "end" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": { 173 | "collapsed": true 174 | }, 175 | "outputs": [], 176 | "source": [ 177 | "function DSGE.measurement{T<:AbstractFloat}(m::MA1{T}, TTT::Matrix{T}, RRR::Matrix{T}, CCC::Vector{T};\n", 178 | " shocks::Bool = false)\n", 179 | " endo = m.endogenous_states # OrderedDict{Symbol, Int} mapping state names (e.g. `:u_t`) to indices\n", 180 | " exo = m.exogenous_shocks # ... mapping shock names to indices\n", 181 | " obs = m.observables # ... mapping observable names to indices\n", 182 | "\n", 183 | " ZZ = zeros(n_observables(m), n_states(m))\n", 184 | " DD = zeros(n_observables(m))\n", 185 | " MM = zeros(n_observables(m), n_shocks_exogenous(m))\n", 186 | " EE = zeros(n_observables(m), n_observables(m))\n", 187 | " QQ = zeros(n_shocks_exogenous(m), n_shocks_exogenous(m))\n", 188 | "\n", 189 | " # y_t = Z*s_t + D\n", 190 | " # y_t = [x_t]'\n", 191 | " # s_t = [u_t, u_{t-1}]'\n", 192 | "\n", 193 | " # TODO: fill in entries of ZZ matrix\n", 194 | " # x_t = μ + u_t + β*u_{t-1}\n", 195 | " DD[obs[:x_t]] = m[:μ]\n", 196 | " ZZ[obs[:x_t], ...] = ...\n", 197 | "\n", 198 | " # TODO: fill in entries of QQ matrix\n", 199 | " QQ[exo[:u_t], exo[:u_t]] = ...\n", 200 | "\n", 201 | " QQ[exo[:u_t], exo[:u_t]] = m[:σ]^2\n", 202 | "\n", 203 | " HH = EE + MM*QQ*MM'\n", 204 | " VV = QQ*MM'\n", 205 | " VVall = [[RRR*QQ*RRR' RRR*VV];\n", 206 | " [VV'*RRR' HH]]\n", 207 | "\n", 208 | " return Measurement(ZZ, DD, QQ, EE, MM, VVall)\n", 209 | "end" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "## Exercise 2\n", 217 | "\n", 218 | "Generate 4000 draws from the prior distribution of each parameter." 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": null, 224 | "metadata": { 225 | "collapsed": false 226 | }, 227 | "outputs": [], 228 | "source": [ 229 | "# Hint: use the `rand` function, which can be called on any `Distribution`.\n", 230 | "dist_μ = get(m[:μ].prior)\n", 231 | "dist_β = get(m[:β].prior)\n", 232 | "dist_σ = get(m[:σ].prior)\n", 233 | "\n", 234 | "isa(dist_μ, Distribution)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": { 241 | "collapsed": true 242 | }, 243 | "outputs": [], 244 | "source": [ 245 | "prior_draws = zeros(4000, 3)\n", 246 | "for i = 1:4000\n", 247 | " # TODO: fill in `prior_draws`\n", 248 | "end" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": { 255 | "collapsed": false 256 | }, 257 | "outputs": [], 258 | "source": [ 259 | "# Plot histograms of prior draws\n", 260 | "prior_plot = plot(prior_draws, \n", 261 | " layout = @layout([a b c]),\n", 262 | " label = [m[:μ].tex_label m[:β].tex_label m[:σ].tex_label],\n", 263 | " t = [:histogram :histogram :histogram],\n", 264 | " color = [:red :blue :green],\n", 265 | " title = [\"\" \"Prior\" \"\"])" 266 | ] 267 | }, 268 | { 269 | "cell_type": "markdown", 270 | "metadata": {}, 271 | "source": [ 272 | "## Exercise 3\n", 273 | "\n", 274 | "Given data generated below, sample from the posterior distribution of the parameters. (No new code needs to be written in this section.)" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "metadata": { 281 | "collapsed": false 282 | }, 283 | "outputs": [], 284 | "source": [ 285 | "# True values of parameters\n", 286 | "μ = 0.75\n", 287 | "β = 0.9\n", 288 | "σ = 0.25\n", 289 | "\n", 290 | "dist_u = Normal(0, σ)\n", 291 | "\n", 292 | "# Initialize states\n", 293 | "u_t = 0.0\n", 294 | "u_t1 = 0.0\n", 295 | "\n", 296 | "# Initialize DataFrame with 100 periods (2001-Q1 to 2025-Q4)\n", 297 | "df = DataFrame(date = DSGE.quarter_range(DSGE.quartertodate(\"2001-Q1\"), DSGE.quartertodate(\"2025-Q4\")))\n", 298 | "df[:x_t] = NaN\n", 299 | "\n", 300 | "for t = 1:100\n", 301 | " # Set last period's u_t value to this period's u_{t-1}\n", 302 | " u_t1 = u_t\n", 303 | "\n", 304 | " # Draw new value of u_t\n", 305 | " u_t = rand(dist_u)\n", 306 | "\n", 307 | " # Apply measurement equation to get x_t\n", 308 | " x_t = μ + u_t + β*u_t1\n", 309 | "\n", 310 | " # Record in DataFrame\n", 311 | " df[t, :x_t] = x_t\n", 312 | "end" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "metadata": { 319 | "collapsed": false 320 | }, 321 | "outputs": [], 322 | "source": [ 323 | "# Find posterior mode and sample using Metropolis-Hastings\n", 324 | "# This function call will take a minute to run the first time\n", 325 | "estimate(m, df)" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": null, 331 | "metadata": { 332 | "collapsed": false 333 | }, 334 | "outputs": [], 335 | "source": [ 336 | "# Load posterior draws\n", 337 | "post_draws = load_draws(m, :full)\n", 338 | "\n", 339 | "# Plot posterior draws\n", 340 | "post_plot = plot(post_draws,\n", 341 | " layout = @layout([a b c]),\n", 342 | " label = [m[:μ].tex_label m[:β].tex_label m[:σ].tex_label],\n", 343 | " t = [:histogram :histogram :histogram],\n", 344 | " color = [:red :blue :green],\n", 345 | " title = [\"\" \"Posterior\" \"\"])\n", 346 | "\n", 347 | "# Plot actual values\n", 348 | "plot_actual!(post_plot.subplots[1], μ)\n", 349 | "plot_actual!(post_plot.subplots[2], β)\n", 350 | "plot_actual!(post_plot.subplots[3], σ)" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": null, 356 | "metadata": { 357 | "collapsed": false 358 | }, 359 | "outputs": [], 360 | "source": [ 361 | "# Plot actuals on top of prior\n", 362 | "plot_actual!(prior_plot.subplots[1], μ)\n", 363 | "plot_actual!(prior_plot.subplots[2], β)\n", 364 | "plot_actual!(prior_plot.subplots[3], σ)\n", 365 | "\n", 366 | "# Plot both prior and posterior together\n", 367 | "all_plots = plot(prior_plot, post_plot, layout = @layout([a; b]))" 368 | ] 369 | } 370 | ], 371 | "metadata": { 372 | "anaconda-cloud": {}, 373 | "kernelspec": { 374 | "display_name": "Julia 0.5.0", 375 | "language": "julia", 376 | "name": "julia-0.5" 377 | }, 378 | "language_info": { 379 | "file_extension": ".jl", 380 | "mimetype": "application/julia", 381 | "name": "julia", 382 | "version": "0.5.0" 383 | } 384 | }, 385 | "nbformat": 4, 386 | "nbformat_minor": 1 387 | } 388 | -------------------------------------------------------------------------------- /dsgejl/input_data/data/data_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 | -------------------------------------------------------------------------------- /dsgejl/slides.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "nbpresent": { 7 | "id": "9f5565ef-7cd0-4521-a7a4-d2c9eda58112" 8 | }, 9 | "slideshow": { 10 | "slide_type": "slide" 11 | } 12 | }, 13 | "source": [ 14 | "# Introduction to DSGE.jl\n", 15 | "\n", 16 | "Pearl Li
CEF 2017\n", 17 | "\n", 18 | "June 27, 2017" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "nbpresent": { 25 | "id": "3d5cc66a-9cbe-4c34-a654-32981d650ea3" 26 | }, 27 | "slideshow": { 28 | "slide_type": "slide" 29 | } 30 | }, 31 | "source": [ 32 | "## Outline\n", 33 | "\n", 34 | "1. Notation\n", 35 | "2. Solving, estimating, and forecasting an existing model\n", 36 | "3. Model implementation\n", 37 | "4. Exercise: MA(1) model\n", 38 | "5. State-space routines\n", 39 | "6. Conclusion" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": { 45 | "nbpresent": { 46 | "id": "70968b4e-4c80-4304-9efa-be289b744dee" 47 | }, 48 | "slideshow": { 49 | "slide_type": "slide" 50 | } 51 | }, 52 | "source": [ 53 | "## Notation" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": { 59 | "nbpresent": { 60 | "id": "ef3f58a3-4127-4d7c-8c95-99dbfb058737" 61 | }, 62 | "slideshow": { 63 | "slide_type": "slide" 64 | } 65 | }, 66 | "source": [ 67 | "- $y_t$ is a vector of observables at time $t$\n", 68 | "- $s_t$ is a vector of states, including expectations of future states and lags\n", 69 | "- $\\epsilon_t$ is a vector of exogenous shocks\n", 70 | "- $\\eta_t$ is a vector of rational expectations errors\n", 71 | "- $u_t$ is a vector of measurement error\n", 72 | "- $\\theta$ is a vector of parameters" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": { 78 | "nbpresent": { 79 | "id": "a507d41c-0c6a-4a50-886a-3a9ecf2cdc1d" 80 | }, 81 | "slideshow": { 82 | "slide_type": "slide" 83 | } 84 | }, 85 | "source": [ 86 | "Equilibrium conditions\n", 87 | "\n", 88 | "$$\\Gamma_0(\\theta) s_t = \\Gamma_1(\\theta) s_{t-1} + \\Psi(\\theta) \\epsilon_t + \\Pi(\\theta) \\eta_t + C(\\theta)$$\n", 89 | "\n", 90 | "which are solved to give the state-space representation\n", 91 | "\n", 92 | "$$\n", 93 | "\\begin{align*}\n", 94 | "s_t &= T(\\theta) s_{t-1} + R(\\theta) \\epsilon_t + C(\\theta) & \\epsilon_t \\sim N(0, Q(\\theta)) \\\\\n", 95 | "y_t &= Z(\\theta) s_t + D(\\theta) + u_t & u_t \\sim N(0, E(\\theta))\n", 96 | "\\end{align*}\n", 97 | "$$" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": { 103 | "collapsed": true, 104 | "nbpresent": { 105 | "id": "9a8dcd28-44cd-4be0-a42b-2e27cc688fd1" 106 | }, 107 | "slideshow": { 108 | "slide_type": "slide" 109 | } 110 | }, 111 | "source": [ 112 | "## Using Existing Models" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": { 118 | "nbpresent": { 119 | "id": "447fd38e-3cc5-4d9a-8b30-f0fd51ca47e5" 120 | }, 121 | "slideshow": { 122 | "slide_type": "slide" 123 | } 124 | }, 125 | "source": [ 126 | "Let's construct an instance of the following (log-linearized) three-equation New Keynesian model:\n", 127 | "\n", 128 | "$$\n", 129 | "\\begin{align*}\n", 130 | "y_t - g_t &= -\\frac{1}{\\tau} R_t + \\frac{1}{1 + \\tau} \\mathbb{E} \\pi_{t+1} + \\mathbb{E}(y_{t+1} - g_{t+1}) + \\frac{1}{\\tau} \\mathbb{E} z_{t+1} \\\\\n", 131 | "\\pi_t &= \\beta \\mathbb{E} \\pi_{t+1} + \\kappa (y_t - g_t) \\\\\n", 132 | "R_t &= \\rho_R R_{t-1} + (1 - \\rho_R) [\\psi_1 \\pi_t + \\psi_2 (y_t - g_t)] + \\epsilon_{R,t}\n", 133 | "\\end{align*}\n", 134 | "$$" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": { 140 | "nbpresent": { 141 | "id": "112b87a7-5855-4601-8b20-e7bac95a5e55" 142 | }, 143 | "slideshow": { 144 | "slide_type": "slide" 145 | } 146 | }, 147 | "source": [ 148 | "where\n", 149 | "\n", 150 | "$$\n", 151 | "\\begin{align*}\n", 152 | "s_t &= [y_t, \\pi_t, R_t, y_{t-1}, g_t, z_t, \\mathbb{E} y_{t+1}, \\mathbb{E} \\pi_{t+1}]' \\\\\n", 153 | "y_t &= [\\text{Real per-capita GDP growth}_t, \\text{CPI inflation}_t, \\text{Nominal FFR}_t]'\n", 154 | "\\end{align*}\n", 155 | "$$\n", 156 | "\n", 157 | "are the state and observable vectors respectively." 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "metadata": { 164 | "collapsed": false, 165 | "nbpresent": { 166 | "id": "f7ff2ae0-8510-4ff8-bad2-e56759a681d9" 167 | }, 168 | "slideshow": { 169 | "slide_type": "slide" 170 | } 171 | }, 172 | "outputs": [], 173 | "source": [ 174 | "using DSGE\n", 175 | "\n", 176 | "# Construct model object\n", 177 | "m = AnSchorfheide()\n", 178 | "\n", 179 | "# Set data vintage and initial forecast date\n", 180 | "m <= Setting(:data_vintage, \"170528\")\n", 181 | "m <= Setting(:date_forecast_start, DSGE.quartertodate(\"2017-Q2\"))\n", 182 | "\n", 183 | "# Set input and output directories\n", 184 | "m <= Setting(:dataroot, joinpath(pwd(), \"input_data\"))\n", 185 | "m <= Setting(:saveroot, pwd())\n", 186 | "\n", 187 | "# Don't use population forecast\n", 188 | "m <= Setting(:use_population_forecast, false)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "metadata": { 195 | "collapsed": false, 196 | "nbpresent": { 197 | "id": "84a715c1-f5a8-43f7-9fb8-639074ca1e3e" 198 | }, 199 | "slideshow": { 200 | "slide_type": "slide" 201 | } 202 | }, 203 | "outputs": [], 204 | "source": [ 205 | "m" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": { 212 | "collapsed": false, 213 | "nbpresent": { 214 | "id": "ede680d7-0978-4963-872c-0b28b9c8b28f" 215 | }, 216 | "slideshow": { 217 | "slide_type": "slide" 218 | } 219 | }, 220 | "outputs": [], 221 | "source": [ 222 | "# Read in a previously computed vector of modal parameters\n", 223 | "mode_file = rawpath(m, \"estimate\", \"paramsmode.h5\")\n", 224 | "specify_mode!(m, mode_file)" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": { 231 | "collapsed": false, 232 | "slideshow": { 233 | "slide_type": "slide" 234 | } 235 | }, 236 | "outputs": [], 237 | "source": [ 238 | "# Solve the rational expectations model to get transition equation \n", 239 | "# matrices\n", 240 | "TTT, RRR, CCC = solve(m)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": { 246 | "nbpresent": { 247 | "id": "64b700c2-2430-40fd-9d3d-79957f03ff00" 248 | }, 249 | "slideshow": { 250 | "slide_type": "slide" 251 | } 252 | }, 253 | "source": [ 254 | "Estimate and forecast the model:" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": null, 260 | "metadata": { 261 | "collapsed": true, 262 | "nbpresent": { 263 | "id": "88ca4b52-a672-460e-a46f-646fec0f2455" 264 | }, 265 | "slideshow": { 266 | "slide_type": "-" 267 | } 268 | }, 269 | "outputs": [], 270 | "source": [ 271 | "# Find a posterior mode, sample from the posterior distribution\n", 272 | "estimate(m)\n", 273 | "\n", 274 | "# Add parallel workers\n", 275 | "my_procs = addprocs(50)\n", 276 | "\n", 277 | "# Forecast using the full distribution of parameters\n", 278 | "output_vars = [:forecaststates, :forecastobs]\n", 279 | "forecast_one(m, :full, :none, output_vars)\n", 280 | "means_bands_all(m, :full, :none, output_vars)\n", 281 | "\n", 282 | "# Remove parallel workers\n", 283 | "rmprocs(my_procs)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": { 289 | "nbpresent": { 290 | "id": "ef0bee98-4417-4d65-b61d-2b9a9c4db3c6" 291 | }, 292 | "slideshow": { 293 | "slide_type": "slide" 294 | } 295 | }, 296 | "source": [ 297 | "Since in practice estimating and forecasting the full distribution is time-consuming, we'll forecast only at the mode using the mode we read in:" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": null, 303 | "metadata": { 304 | "collapsed": false, 305 | "nbpresent": { 306 | "id": "a245e3b8-5d31-4bcb-9a74-35cf7f133931" 307 | } 308 | }, 309 | "outputs": [], 310 | "source": [ 311 | "# Load data\n", 312 | "df = load_data(m)\n", 313 | "\n", 314 | "# Forecast using modal parameters\n", 315 | "output_vars = [:histobs, :forecaststates, :forecastobs]\n", 316 | "forecast_one(m, :mode, :none, output_vars, df = df, verbose = :none)\n", 317 | "means_bands_all(m, :mode, :none, output_vars, verbose = :none)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": { 324 | "collapsed": false, 325 | "nbpresent": { 326 | "id": "af377c21-fd1a-4162-8adc-0fa0b9977488" 327 | }, 328 | "slideshow": { 329 | "slide_type": "slide" 330 | } 331 | }, 332 | "outputs": [], 333 | "source": [ 334 | "# Read in forecasted observables\n", 335 | "files = get_meansbands_output_files(m, :mode, :none, output_vars)\n", 336 | "mb = read_mb(files[:forecastobs])" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": null, 342 | "metadata": { 343 | "collapsed": false, 344 | "nbpresent": { 345 | "id": "a288f08a-1409-45c4-897c-f9612e078445" 346 | } 347 | }, 348 | "outputs": [], 349 | "source": [ 350 | "# Show first four forecasted quarters\n", 351 | "mb.means[1:4, :]" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": { 358 | "collapsed": false, 359 | "nbpresent": { 360 | "id": "3591edd3-2e94-475d-8b23-7d16d95f076f" 361 | }, 362 | "slideshow": { 363 | "slide_type": "slide" 364 | } 365 | }, 366 | "outputs": [], 367 | "source": [ 368 | "# Read in forecasted states\n", 369 | "mb = read_mb(files[:forecaststates])\n", 370 | "\n", 371 | "# Show first forecasted quarters of output, inflation, and interest rate\n", 372 | "mb.means[1:4, [:y_t, :π_t, :R_t]]" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": { 379 | "collapsed": false, 380 | "slideshow": { 381 | "slide_type": "slide" 382 | } 383 | }, 384 | "outputs": [], 385 | "source": [ 386 | "using Plots\n", 387 | "include(\"util.jl\")\n", 388 | "include(\"plot_history_and_forecast.jl\")\n", 389 | "\n", 390 | "# Plot nominal rate history and modal forecast\n", 391 | "hist_mb = read_mb(files[:histobs])\n", 392 | "fcast_mb = read_mb(files[:forecastobs])\n", 393 | "plot_history_and_forecast(:obs_nominalrate, hist_mb, fcast_mb)" 394 | ] 395 | }, 396 | { 397 | "cell_type": "markdown", 398 | "metadata": { 399 | "nbpresent": { 400 | "id": "c36485e1-793b-4791-9a05-2414d53bd1a1" 401 | }, 402 | "slideshow": { 403 | "slide_type": "slide" 404 | } 405 | }, 406 | "source": [ 407 | "## The Model Object" 408 | ] 409 | }, 410 | { 411 | "cell_type": "markdown", 412 | "metadata": { 413 | "nbpresent": { 414 | "id": "e3b562e9-322c-4b33-8bd8-bcba39fbaa86" 415 | }, 416 | "slideshow": { 417 | "slide_type": "slide" 418 | } 419 | }, 420 | "source": [ 421 | "Subtypes of `AbstractModel` contain the following fields:" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": { 428 | "collapsed": false, 429 | "nbpresent": { 430 | "id": "62fc466c-c050-4b3a-8c6f-457950add74d" 431 | }, 432 | "slideshow": { 433 | "slide_type": "-" 434 | } 435 | }, 436 | "outputs": [], 437 | "source": [ 438 | "fieldnames(m)" 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "metadata": { 444 | "collapsed": true, 445 | "nbpresent": { 446 | "id": "7a293d5c-36a6-4bed-bd85-366cf611379b" 447 | }, 448 | "slideshow": { 449 | "slide_type": "slide" 450 | } 451 | }, 452 | "source": [ 453 | "These fields include:\n", 454 | "\n", 455 | "- Vectors of time-invariant (`parameters`) and steady-state parameters (`steady_state`)\n", 456 | "- `Dict{Symbol, Int}`s mapping states, shocks, expectational errors, equations, or observables to indices. For example:" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": null, 462 | "metadata": { 463 | "collapsed": false, 464 | "nbpresent": { 465 | "id": "28089ee9-82a4-4fdf-bbc8-d7c566419be6" 466 | }, 467 | "slideshow": { 468 | "slide_type": "slide" 469 | } 470 | }, 471 | "outputs": [], 472 | "source": [ 473 | "m.endogenous_states" 474 | ] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "metadata": { 479 | "nbpresent": { 480 | "id": "cda12a81-a64b-465c-b60d-5843b3186c73" 481 | }, 482 | "slideshow": { 483 | "slide_type": "-" 484 | } 485 | }, 486 | "source": [ 487 | "`m.endogenous_states[:y_t] = 1` indicates that the first element of the state vector\n", 488 | "\n", 489 | "$$s_t = [y_t, \\pi_t, R_t, y_{t-1}, g_t, z_t, \\mathbb{E} y_{t+1}, \\mathbb{E} \\pi_{t+1}]$$\n", 490 | "\n", 491 | "is $y_t$." 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": null, 497 | "metadata": { 498 | "collapsed": false, 499 | "nbpresent": { 500 | "id": "0c1b8853-8358-4e6d-a51f-a4e6b048f620" 501 | }, 502 | "slideshow": { 503 | "slide_type": "slide" 504 | } 505 | }, 506 | "outputs": [], 507 | "source": [ 508 | "m.equilibrium_conditions" 509 | ] 510 | }, 511 | { 512 | "cell_type": "markdown", 513 | "metadata": { 514 | "nbpresent": { 515 | "id": "edce2bfa-3e5b-46f2-86de-4b6051229f9c" 516 | } 517 | }, 518 | "source": [ 519 | "`m.equilibrium_conditions[:eq_euler] = 1` means the first row of the equilibrium conditions\n", 520 | "\n", 521 | "$$\\Gamma_0 s_t = \\Gamma_1 s_{t-1} + \\Psi \\epsilon_t + \\Pi \\eta_t + C$$\n", 522 | "\n", 523 | "is the consumption Euler equation." 524 | ] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": { 529 | "nbpresent": { 530 | "id": "21da31ad-5847-42a8-8c59-28bc0b857e5c" 531 | }, 532 | "slideshow": { 533 | "slide_type": "slide" 534 | } 535 | }, 536 | "source": [ 537 | "Additional fields in the model object:\n", 538 | "\n", 539 | "- Strings giving the model specification (`spec`) and subspecification (`subspec`)\n", 540 | "- `Dict{Symbol, Setting}`s of model settings, both for regular use (`settings`) and testing the package (`test_settings`)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": { 546 | "collapsed": true, 547 | "slideshow": { 548 | "slide_type": "slide" 549 | } 550 | }, 551 | "source": [ 552 | "## Exercise: MA(1) Model\n", 553 | "\n", 554 | "See `exercise.ipynb`" 555 | ] 556 | }, 557 | { 558 | "cell_type": "markdown", 559 | "metadata": { 560 | "slideshow": { 561 | "slide_type": "slide" 562 | } 563 | }, 564 | "source": [ 565 | "## State-Space Routines" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": { 571 | "slideshow": { 572 | "slide_type": "slide" 573 | } 574 | }, 575 | "source": [ 576 | "[StateSpaceRoutines.jl](https://github.com/FRBNY-DSGE/StateSpaceRoutines.jl)\n", 577 | "\n", 578 | "- Package implementing **DSGE.jl-agnostic** state-space routines\n", 579 | "- DSGE.jl functions `filter` and `smooth` wrap calls to these functions" 580 | ] 581 | }, 582 | { 583 | "cell_type": "markdown", 584 | "metadata": { 585 | "slideshow": { 586 | "slide_type": "slide" 587 | } 588 | }, 589 | "source": [ 590 | "Implemented routines:\n", 591 | "\n", 592 | "- Kalman filter (`kalman_filter`)\n", 593 | "- Kalman smoothers:\n", 594 | " + `hamilton_smoother`: James Hamilton, [_Time Series Analysis_](https://www.amazon.com/Time-Analysis-James-Douglas-Hamilton/dp/0691042896) (1994)\n", 595 | " + `koopman_smoother`: S.J. Koopman, [\"Disturbance Smoother for State Space Models\"](https://www.jstor.org/stable/2336762) (_Biometrika_, 1993)" 596 | ] 597 | }, 598 | { 599 | "cell_type": "markdown", 600 | "metadata": { 601 | "slideshow": { 602 | "slide_type": "slide" 603 | } 604 | }, 605 | "source": [ 606 | "- Simulation smoothers:\n", 607 | " + `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", 608 | " + `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)" 609 | ] 610 | }, 611 | { 612 | "cell_type": "code", 613 | "execution_count": null, 614 | "metadata": { 615 | "collapsed": false, 616 | "slideshow": { 617 | "slide_type": "slide" 618 | } 619 | }, 620 | "outputs": [], 621 | "source": [ 622 | "using StateSpaceRoutines" 623 | ] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": null, 628 | "metadata": { 629 | "collapsed": false 630 | }, 631 | "outputs": [], 632 | "source": [ 633 | "?kalman_filter" 634 | ] 635 | }, 636 | { 637 | "cell_type": "markdown", 638 | "metadata": { 639 | "slideshow": { 640 | "slide_type": "slide" 641 | } 642 | }, 643 | "source": [ 644 | "Example: Kalman filter" 645 | ] 646 | }, 647 | { 648 | "cell_type": "code", 649 | "execution_count": null, 650 | "metadata": { 651 | "collapsed": false, 652 | "slideshow": { 653 | "slide_type": "-" 654 | } 655 | }, 656 | "outputs": [], 657 | "source": [ 658 | "# Compute state-space matrices\n", 659 | "sys = compute_system(m)\n", 660 | "\n", 661 | "# Convert DataFrame to matrix\n", 662 | "data = df_to_matrix(m, df)\n", 663 | "\n", 664 | "# Call Kalman filter\n", 665 | "loglike, s_T, P_T, _, _, _, _, _, _, _, _, s_0, P_0 =\n", 666 | " kalman_filter(data, sys[:TTT], sys[:RRR], sys[:CCC], sys[:QQ],\n", 667 | " sys[:ZZ], sys[:DD], sys[:EE])\n", 668 | "loglike" 669 | ] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": { 675 | "collapsed": false 676 | }, 677 | "outputs": [], 678 | "source": [ 679 | "s_T" 680 | ] 681 | }, 682 | { 683 | "cell_type": "markdown", 684 | "metadata": { 685 | "slideshow": { 686 | "slide_type": "slide" 687 | } 688 | }, 689 | "source": [ 690 | "Example: simulation smoother" 691 | ] 692 | }, 693 | { 694 | "cell_type": "code", 695 | "execution_count": null, 696 | "metadata": { 697 | "collapsed": false, 698 | "slideshow": { 699 | "slide_type": "-" 700 | } 701 | }, 702 | "outputs": [], 703 | "source": [ 704 | "# Call simulation smoother\n", 705 | "smoothed_states, smoothed_shocks =\n", 706 | " durbin_koopman_smoother(data, sys[:TTT], sys[:RRR], sys[:CCC], sys[:QQ],\n", 707 | " sys[:ZZ], sys[:DD], sys[:EE], s_0, P_0,\n", 708 | " draw_states = true)\n", 709 | "smoothed_states" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": null, 715 | "metadata": { 716 | "collapsed": false 717 | }, 718 | "outputs": [], 719 | "source": [ 720 | "smoothed_shocks" 721 | ] 722 | }, 723 | { 724 | "cell_type": "markdown", 725 | "metadata": { 726 | "slideshow": { 727 | "slide_type": "slide" 728 | } 729 | }, 730 | "source": [ 731 | "## Conclusion" 732 | ] 733 | }, 734 | { 735 | "cell_type": "markdown", 736 | "metadata": { 737 | "slideshow": { 738 | "slide_type": "slide" 739 | } 740 | }, 741 | "source": [ 742 | "Things learned:\n", 743 | "\n", 744 | "- We love open source (and Julia)!\n", 745 | " + Open-source languages and packages reduce costs of writing code and make it easier to share\n", 746 | " + Julia is high-performance and high-productivity\n", 747 | "- Challenges to be aware of\n", 748 | " + New language: frequent updates. This will slow down when v1.0 comes out (hopefully this year)\n", 749 | " + Sparse StackOverflow activity" 750 | ] 751 | }, 752 | { 753 | "cell_type": "markdown", 754 | "metadata": { 755 | "slideshow": { 756 | "slide_type": "slide" 757 | } 758 | }, 759 | "source": [ 760 | "Ongoing work:\n", 761 | "\n", 762 | "- Forecasting under alternative monetary policy rules\n", 763 | "- Forecast evaluation and decomposing changes in forecasts\n", 764 | "- Estimating nonlinear models using the tempered particle filter (Herbst & Schorfheide 2017)" 765 | ] 766 | }, 767 | { 768 | "cell_type": "markdown", 769 | "metadata": { 770 | "slideshow": { 771 | "slide_type": "slide" 772 | } 773 | }, 774 | "source": [ 775 | "### Thank you!\n", 776 | "\n", 777 | "https://github.com/FRBNY-DSGE/DSGE.jl
\n", 778 | "https://github.com/FRBNY-DSGE/StateSpaceRoutines.jl" 779 | ] 780 | } 781 | ], 782 | "metadata": { 783 | "anaconda-cloud": {}, 784 | "celltoolbar": "Slideshow", 785 | "kernelspec": { 786 | "display_name": "Julia 0.5.0", 787 | "language": "julia", 788 | "name": "julia-0.5" 789 | }, 790 | "language_info": { 791 | "file_extension": ".jl", 792 | "mimetype": "application/julia", 793 | "name": "julia", 794 | "version": "0.5.0" 795 | } 796 | }, 797 | "nbformat": 4, 798 | "nbformat_minor": 2 799 | } 800 | -------------------------------------------------------------------------------- /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 | "## @ CEF 2017\n", 15 | "\n", 16 | "**Authors**: Chase Coleman and Spencer Lyon\n", 17 | "\n", 18 | "**Date**: 27 June 2017\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "## Goal\n", 26 | "\n", 27 | "A few different packages used for optimization or non-linear equation solving.\n" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "slideshow": { 34 | "slide_type": "slide" 35 | } 36 | }, 37 | "source": [ 38 | "## QuantEcon\n", 39 | "\n", 40 | "The QuantEcon library has a few simple optimizers and solvers. See the economics [notebook](economics.ipynb) for more information." 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 1, 46 | "metadata": { 47 | "collapsed": true 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "# Pkg.add(\"QuantEcon\")" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": { 57 | "slideshow": { 58 | "slide_type": "slide" 59 | } 60 | }, 61 | "source": [ 62 | "## Optim.jl\n", 63 | "\n", 64 | "Package for optimizing written in pure Julia\n", 65 | "\n", 66 | "[Documentation](http://julianlsolvers.github.io/Optim.jl/stable/)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 2, 72 | "metadata": { 73 | "collapsed": true 74 | }, 75 | "outputs": [], 76 | "source": [ 77 | "# Pkg.add(\"Optim\")" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": { 84 | "collapsed": true 85 | }, 86 | "outputs": [], 87 | "source": [ 88 | "using Optim" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "metadata": { 95 | "slideshow": { 96 | "slide_type": "-" 97 | } 98 | }, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "rosenbrock (generic function with 1 method)" 104 | ] 105 | }, 106 | "execution_count": 4, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "rosenbrock(x) = (1.0 - x[1])^2 + 100.0*(x[2] - x[1]^2)^2" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": { 118 | "slideshow": { 119 | "slide_type": "subslide" 120 | } 121 | }, 122 | "source": [ 123 | "### Optimizing without gradient" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 5, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "Results of Optimization Algorithm\n", 135 | " * Algorithm: Nelder-Mead\n", 136 | " * Starting Point: [0.0,0.0]\n", 137 | " * Minimizer: [0.9999710322210338,0.9999438685860869]\n", 138 | " * Minimum: 1.164323e-09\n", 139 | " * Iterations: 74\n", 140 | " * Convergence: true\n", 141 | " * √(Σ(yᵢ-ȳ)²)/n < 1.0e-08: true\n", 142 | " * Reached Maximum Number of Iterations: false\n", 143 | " * Objective Calls: 144" 144 | ] 145 | }, 146 | "execution_count": 5, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "optimize(rosenbrock, zeros(2), NelderMead())" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 6, 158 | "metadata": { 159 | "slideshow": { 160 | "slide_type": "fragment" 161 | } 162 | }, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "Results of Optimization Algorithm\n", 168 | " * Algorithm: BFGS\n", 169 | " * Starting Point: [0.0,0.0]\n", 170 | " * Minimizer: [0.9999999926033423,0.9999999852005353]\n", 171 | " * Minimum: 5.471433e-17\n", 172 | " * Iterations: 16\n", 173 | " * Convergence: true\n", 174 | " * |x - x'| < 1.0e-32: false \n", 175 | " |x - x'| = 3.47e-07 \n", 176 | " * |f(x) - f(x')| / |f(x)| < 1.0e-32: false\n", 177 | " |f(x) - f(x')| / |f(x)| = NaN \n", 178 | " * |g(x)| < 1.0e-08: true \n", 179 | " |g(x)| = 2.33e-09 \n", 180 | " * stopped by an increasing objective: false\n", 181 | " * Reached Maximum Number of Iterations: false\n", 182 | " * Objective Calls: 53\n", 183 | " * Gradient Calls: 53" 184 | ] 185 | }, 186 | "execution_count": 6, 187 | "metadata": {}, 188 | "output_type": "execute_result" 189 | } 190 | ], 191 | "source": [ 192 | "optimize(rosenbrock, zeros(2), BFGS())" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": { 198 | "slideshow": { 199 | "slide_type": "subslide" 200 | } 201 | }, 202 | "source": [ 203 | "### Optimizing with gradient" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 7, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "rosenbrock_grad! (generic function with 1 method)" 215 | ] 216 | }, 217 | "execution_count": 7, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "function rosenbrock_grad!(x::Vector, grad::Vector)\n", 224 | " grad[1] = -2.0*(1.0 - x[1]) - 400.0*(x[2] - x[1]^2)*x[1]\n", 225 | " grad[2] = 200.0*(x[2] - x[1]^2)\n", 226 | "end" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 8, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "name": "stderr", 236 | "output_type": "stream", 237 | "text": [ 238 | "\u001b[1m\u001b[33mWARNING: \u001b[39m\u001b[22m\u001b[33mStorage (g) and evaluation point (x) order has changed. The order is now g!(storage, x) as opposed to the old g!(x, storage). Changing the order and proceeding, but please change your code to use the new syntax.\u001b[39m\u001b[33mStorage (g) and evaluation point (x) order has changed. The order is now fg!(storage, x) as opposed to the old fg!(x, storage). Changing the order and proceeding, but please change your code to use the new syntax.\u001b[39m\n" 239 | ] 240 | }, 241 | { 242 | "data": { 243 | "text/plain": [ 244 | "Results of Optimization Algorithm\n", 245 | " * Algorithm: L-BFGS\n", 246 | " * Starting Point: [0.0,0.0]\n", 247 | " * Minimizer: [1.0000142961896872,1.0000147679027978]\n", 248 | " * Minimum: 9.896955e-01\n", 249 | " * Iterations: 25000\n", 250 | " * Convergence: false\n", 251 | " * |x - x'| < 1.0e-10: false \n", 252 | " |x - x'| = 1.55e-09 \n", 253 | " * |f(x) - f(x')| / |f(x)| < 1.0e-09: false\n", 254 | " |f(x) - f(x')| / |f(x)| = 1.00e-06 \n", 255 | " * |g(x)| < 1.0e-08: false \n", 256 | " |g(x)| = 5.56e-03 \n", 257 | " * stopped by an increasing objective: true\n", 258 | " * Reached Maximum Number of Iterations: true\n", 259 | " * Objective Calls: 1059533\n", 260 | " * Gradient Calls: 1059533" 261 | ] 262 | }, 263 | "execution_count": 8, 264 | "metadata": {}, 265 | "output_type": "execute_result" 266 | }, 267 | { 268 | "name": "stderr", 269 | "output_type": "stream", 270 | "text": [ 271 | "\n", 272 | "\u001b[1m\u001b[33mWARNING: \u001b[39m\u001b[22m" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "optimize(rosenbrock, rosenbrock_grad!, zeros(2), LBFGS(),\n", 278 | " Optim.Options(x_tol=1e-10, f_tol=1e-9, iterations=25000,\n", 279 | " allow_f_increases=true))" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 9, 285 | "metadata": { 286 | "slideshow": { 287 | "slide_type": "fragment" 288 | } 289 | }, 290 | "outputs": [ 291 | { 292 | "name": "stderr", 293 | "output_type": "stream", 294 | "text": [ 295 | "\u001b[1m\u001b[33mWARNING: \u001b[39m\u001b[22m\u001b[33mStorage (g) and evaluation point (x) order has changed. The order is now g!(storage, x) as opposed to the old g!(x, storage). Changing the order and proceeding, but please change your code to use the new syntax.\u001b[39m\n", 296 | "\u001b[1m\u001b[33mWARNING: \u001b[39m\u001b[22m\u001b[33mStorage (g) and evaluation point (x) order has changed. The order is now fg!(storage, x) as opposed to the old fg!(x, storage). Changing the order and proceeding, but please change your code to use the new syntax.\u001b[39m\n" 297 | ] 298 | }, 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "Results of Optimization Algorithm\n", 303 | " * Algorithm: Gradient Descent\n", 304 | " * Starting Point: [0.0,0.0]\n", 305 | " * Minimizer: [0.9998054306416495,0.9995907329189162]\n", 306 | " * Minimum: 9.863819e-01\n", 307 | " * Iterations: 25000\n", 308 | " * Convergence: false\n", 309 | " * |x - x'| < 1.0e-10: false \n", 310 | " |x - x'| = 6.23e-10 \n", 311 | " * |f(x) - f(x')| / |f(x)| < 1.0e-09: false\n", 312 | " |f(x) - f(x')| / |f(x)| = NaN \n", 313 | " * |g(x)| < 1.0e-08: false \n", 314 | " |g(x)| = 7.68e-03 \n", 315 | " * stopped by an increasing objective: false\n", 316 | " * Reached Maximum Number of Iterations: true\n", 317 | " * Objective Calls: 1029026\n", 318 | " * Gradient Calls: 1029026" 319 | ] 320 | }, 321 | "execution_count": 9, 322 | "metadata": {}, 323 | "output_type": "execute_result" 324 | } 325 | ], 326 | "source": [ 327 | "optimize(rosenbrock, rosenbrock_grad!, zeros(2), GradientDescent(),\n", 328 | " Optim.Options(x_tol=1e-10, f_tol=1e-9, iterations=25000,\n", 329 | " allow_f_increases=true))" 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": { 335 | "slideshow": { 336 | "slide_type": "subslide" 337 | } 338 | }, 339 | "source": [ 340 | "### Optimizing with Hessian" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 10, 346 | "metadata": {}, 347 | "outputs": [ 348 | { 349 | "data": { 350 | "text/plain": [ 351 | "rosenbrock_hess! (generic function with 1 method)" 352 | ] 353 | }, 354 | "execution_count": 10, 355 | "metadata": {}, 356 | "output_type": "execute_result" 357 | } 358 | ], 359 | "source": [ 360 | "function rosenbrock_hess!(x::Vector, hess::Matrix)\n", 361 | " hess[1, 1] = 2.0 - 400.0 * x[2] + 1200.0*x[1]^2\n", 362 | " hess[1, 2] = -400.0 * x[1]\n", 363 | " hess[2, 1] = -400.0 * x[1]\n", 364 | " hess[2, 2] = 200.0\n", 365 | "end" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 11, 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "name": "stderr", 375 | "output_type": "stream", 376 | "text": [ 377 | "\u001b[1m\u001b[33mWARNING: \u001b[39m\u001b[22m\u001b[33mStorage and evaluation point order has changed. The syntax is now h!(storage, x) as opposed to the old h!(x, storage). Your Hessian appears to have it the wrong way around. Changing the order and proceeding, but please change your code to use the new syntax.\u001b[39m\n", 378 | "\u001b[1m\u001b[33mWARNING: \u001b[39m\u001b[22m\u001b[33mLinesearch failed, using alpha = 0.0 and exiting optimization.\u001b[39m\n" 379 | ] 380 | }, 381 | { 382 | "data": { 383 | "text/plain": [ 384 | "Results of Optimization Algorithm\n", 385 | " * Algorithm: Newton's Method\n", 386 | " * Starting Point: [-0.0,0.0]\n", 387 | " * Minimizer: [-0.0,0.0]\n", 388 | " * Minimum: 1.000000e+00\n", 389 | " * Iterations: 1\n", 390 | " * Convergence: false\n", 391 | " * |x - x'| < 1.0e-32: false \n", 392 | " |x - x'| = 0.00e+00 \n", 393 | " * |f(x) - f(x')| / |f(x)| < 1.0e-32: false\n", 394 | " |f(x) - f(x')| / |f(x)| = NaN \n", 395 | " * |g(x)| < 1.0e-08: false \n", 396 | " |g(x)| = 1.00e+00 \n", 397 | " * stopped by an increasing objective: false\n", 398 | " * Reached Maximum Number of Iterations: false\n", 399 | " * Objective Calls: 51\n", 400 | " * Gradient Calls: 51\n", 401 | " * Hessian Calls: 1" 402 | ] 403 | }, 404 | "execution_count": 11, 405 | "metadata": {}, 406 | "output_type": "execute_result" 407 | } 408 | ], 409 | "source": [ 410 | "optimize(rosenbrock, rosenbrock_grad!, rosenbrock_hess!, zeros(2), Newton())" 411 | ] 412 | }, 413 | { 414 | "cell_type": "markdown", 415 | "metadata": { 416 | "slideshow": { 417 | "slide_type": "slide" 418 | } 419 | }, 420 | "source": [ 421 | "## NLopt\n", 422 | "\n", 423 | "Julia wrapper to high quality C library.\n", 424 | "\n", 425 | "This library has _lots_ of options for algorithms. See [list](http://ab-initio.mit.edu/wiki/index.php/NLopt_Algorithms) of algorithms\n", 426 | "\n", 427 | "[Julia package](https://github.com/JuliaOpt/NLopt.jl) and [C Documentation](http://ab-initio.mit.edu/wiki/index.php/NLopt)" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": 12, 433 | "metadata": { 434 | "collapsed": true 435 | }, 436 | "outputs": [], 437 | "source": [ 438 | "# Pkg.add(\"NLopt\")" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "execution_count": 13, 444 | "metadata": {}, 445 | "outputs": [ 446 | { 447 | "name": "stderr", 448 | "output_type": "stream", 449 | "text": [ 450 | "WARNING: using NLopt.optimize in module Main conflicts with an existing identifier.\n" 451 | ] 452 | } 453 | ], 454 | "source": [ 455 | "using NLopt" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 14, 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "data": { 465 | "text/plain": [ 466 | "rosenbrock_nlopt (generic function with 1 method)" 467 | ] 468 | }, 469 | "execution_count": 14, 470 | "metadata": {}, 471 | "output_type": "execute_result" 472 | } 473 | ], 474 | "source": [ 475 | "function rosenbrock_nlopt(x::Vector, grad::Vector)\n", 476 | " if length(grad) > 0\n", 477 | " rosenbrock_grad!(x, grad)\n", 478 | " end\n", 479 | "\n", 480 | " return rosenbrock(x)\n", 481 | "end" 482 | ] 483 | }, 484 | { 485 | "cell_type": "markdown", 486 | "metadata": { 487 | "slideshow": { 488 | "slide_type": "subslide" 489 | } 490 | }, 491 | "source": [ 492 | "### A non-gradient and gradient based method" 493 | ] 494 | }, 495 | { 496 | "cell_type": "code", 497 | "execution_count": 15, 498 | "metadata": {}, 499 | "outputs": [ 500 | { 501 | "data": { 502 | "text/plain": [ 503 | "(3.251384063527492e-21, [1.0, 1.0], :SUCCESS)" 504 | ] 505 | }, 506 | "execution_count": 15, 507 | "metadata": {}, 508 | "output_type": "execute_result" 509 | } 510 | ], 511 | "source": [ 512 | "opt_LBFGS = Opt(:LD_LBFGS, 2)\n", 513 | "\n", 514 | "min_objective!(opt_LBFGS, rosenbrock_nlopt)\n", 515 | "xtol_rel!(opt_LBFGS, 1e-10)\n", 516 | "ftol_rel!(opt_LBFGS, 1e-9)\n", 517 | "\n", 518 | "NLopt.optimize(opt_LBFGS, zeros(2))" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 16, 524 | "metadata": { 525 | "slideshow": { 526 | "slide_type": "fragment" 527 | } 528 | }, 529 | "outputs": [ 530 | { 531 | "data": { 532 | "text/plain": [ 533 | "(1.1093356479670479e-29, [1.0, 1.0], :SUCCESS)" 534 | ] 535 | }, 536 | "execution_count": 16, 537 | "metadata": {}, 538 | "output_type": "execute_result" 539 | } 540 | ], 541 | "source": [ 542 | "opt_PRAXIS = Opt(:LN_PRAXIS, 2)\n", 543 | "\n", 544 | "min_objective!(opt_PRAXIS, rosenbrock_nlopt)\n", 545 | "\n", 546 | "NLopt.optimize(opt_PRAXIS, zeros(2))" 547 | ] 548 | }, 549 | { 550 | "cell_type": "markdown", 551 | "metadata": { 552 | "slideshow": { 553 | "slide_type": "subslide" 554 | } 555 | }, 556 | "source": [ 557 | "### A constrained optimization problem\n", 558 | "\n", 559 | "\\begin{align*}\n", 560 | " \\min_{x_1, x_2} &\\sqrt{x_2} \\\\\n", 561 | " &\\text{subject to } \\\\\n", 562 | " &x_2 \\geq 0 \\\\\n", 563 | " &x_2 \\geq (2 x_1)^3 \\\\\n", 564 | " &x_2 \\geq (-x_1 + 1)^3\n", 565 | "\\end{align*}" 566 | ] 567 | }, 568 | { 569 | "cell_type": "code", 570 | "execution_count": 17, 571 | "metadata": {}, 572 | "outputs": [ 573 | { 574 | "data": { 575 | "text/plain": [ 576 | "myconstraint (generic function with 1 method)" 577 | ] 578 | }, 579 | "execution_count": 17, 580 | "metadata": {}, 581 | "output_type": "execute_result" 582 | } 583 | ], 584 | "source": [ 585 | "function myfunc(x::Vector, grad::Vector)\n", 586 | " if length(grad) > 0\n", 587 | " grad[1] = 0\n", 588 | " grad[2] = 0.5/sqrt(x[2])\n", 589 | " end\n", 590 | "\n", 591 | " return sqrt(x[1] + x[2])\n", 592 | "end\n", 593 | "\n", 594 | "function myconstraint(x::Vector, grad::Vector, a, b)\n", 595 | " if length(grad) > 0\n", 596 | " grad[1] = 3a * (a*x[1] + b)^2\n", 597 | " grad[2] = -1\n", 598 | " end\n", 599 | "\n", 600 | " return (a*x[1] + b)^3 - x[2]\n", 601 | "end" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 18, 607 | "metadata": { 608 | "slideshow": { 609 | "slide_type": "fragment" 610 | } 611 | }, 612 | "outputs": [ 613 | { 614 | "name": "stdout", 615 | "output_type": "stream", 616 | "text": [ 617 | "got 0.7934920514599207 at [0.333333, 0.296296] (returned XTOL_REACHED)\n" 618 | ] 619 | } 620 | ], 621 | "source": [ 622 | "opt = Opt(:LD_MMA, 2)\n", 623 | "\n", 624 | "lower_bounds!(opt, [-Inf, 0.])\n", 625 | "xtol_rel!(opt, 1e-6)\n", 626 | "\n", 627 | "min_objective!(opt, myfunc)\n", 628 | "\n", 629 | "inequality_constraint!(opt, (x,g) -> myconstraint(x, g, 2.0, 0.0), 1e-8)\n", 630 | "inequality_constraint!(opt, (x,g) -> myconstraint(x, g, -1.0 ,1.0), 1e-8)\n", 631 | "\n", 632 | "(minf, minx, ret) = NLopt.optimize(opt, [1.234, 5.678])\n", 633 | "\n", 634 | "println(\"got $minf at $minx (returned $ret)\")" 635 | ] 636 | }, 637 | { 638 | "cell_type": "markdown", 639 | "metadata": { 640 | "collapsed": true, 641 | "slideshow": { 642 | "slide_type": "subslide" 643 | } 644 | }, 645 | "source": [ 646 | "## NLsolve\n", 647 | "\n", 648 | "Julia package written to solve systems of non-linear equations\n", 649 | "\n", 650 | "[Documentation](https://github.com/JuliaNLSolvers/NLsolve.jl)" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": 19, 656 | "metadata": { 657 | "collapsed": true 658 | }, 659 | "outputs": [], 660 | "source": [ 661 | "# Pkg.add(\"NLsolve\")" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 20, 667 | "metadata": { 668 | "collapsed": true 669 | }, 670 | "outputs": [], 671 | "source": [ 672 | "using NLsolve" 673 | ] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "execution_count": 21, 678 | "metadata": {}, 679 | "outputs": [ 680 | { 681 | "data": { 682 | "text/plain": [ 683 | "g! (generic function with 1 method)" 684 | ] 685 | }, 686 | "execution_count": 21, 687 | "metadata": {}, 688 | "output_type": "execute_result" 689 | } 690 | ], 691 | "source": [ 692 | "function f!(xy::Vector, fxy::Vector)\n", 693 | " # Pull out arguments\n", 694 | " x, y = xy\n", 695 | "\n", 696 | " # Fill fxy\n", 697 | " fxy[1] = x^2 - sin(y)\n", 698 | " fxy[2] = y^2 - cos(x)\n", 699 | "end\n", 700 | "\n", 701 | "function g!(xy::Vector, jacxy::Matrix)\n", 702 | " x, y = xy\n", 703 | " # Fill with derivatives of first function\n", 704 | " jacxy[1, 1] = 2*x\n", 705 | " jacxy[1, 2] = -cos(y)\n", 706 | "\n", 707 | " # Fill off-diagonal\n", 708 | " jacxy[2, 1] = sin(x)\n", 709 | " jacxy[2, 2] = 2*y\n", 710 | "end" 711 | ] 712 | }, 713 | { 714 | "cell_type": "code", 715 | "execution_count": 22, 716 | "metadata": { 717 | "slideshow": { 718 | "slide_type": "fragment" 719 | } 720 | }, 721 | "outputs": [ 722 | { 723 | "data": { 724 | "text/plain": [ 725 | "Results of Nonlinear Solver Algorithm\n", 726 | " * Algorithm: Trust-region with dogleg and autoscaling\n", 727 | " * Starting Point: [0.5, 0.5]\n", 728 | " * Zero: [0.8517, 0.811606]\n", 729 | " * Inf-norm of residuals: 0.000000\n", 730 | " * Iterations: 5\n", 731 | " * Convergence: true\n", 732 | " * |x - x'| < 0.0e+00: false\n", 733 | " * |f(x)| < 1.0e-08: true\n", 734 | " * Function Calls (f): 6\n", 735 | " * Jacobian Calls (df/dx): 6" 736 | ] 737 | }, 738 | "execution_count": 22, 739 | "metadata": {}, 740 | "output_type": "execute_result" 741 | } 742 | ], 743 | "source": [ 744 | "res = nlsolve(f!, g!, [0.5, 0.5], ftol=1e-8)" 745 | ] 746 | }, 747 | { 748 | "cell_type": "code", 749 | "execution_count": null, 750 | "metadata": { 751 | "collapsed": true 752 | }, 753 | "outputs": [], 754 | "source": [] 755 | } 756 | ], 757 | "metadata": { 758 | "anaconda-cloud": {}, 759 | "kernelspec": { 760 | "display_name": "Julia 0.6.0", 761 | "language": "julia", 762 | "name": "julia-0.6" 763 | }, 764 | "language_info": { 765 | "file_extension": ".jl", 766 | "mimetype": "application/julia", 767 | "name": "julia", 768 | "version": "0.6.0" 769 | }, 770 | "livereveal": { 771 | "scroll": true, 772 | "start_slideshow_at": "selected", 773 | "theme": "white", 774 | "transition": "fade" 775 | } 776 | }, 777 | "nbformat": 4, 778 | "nbformat_minor": 2 779 | } 780 | -------------------------------------------------------------------------------- /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 | "** @ CEF 2017** \n", 14 | "\n", 15 | "**Authors**: Chase Coleman and Spencer Lyon\n", 16 | "\n", 17 | "**Date**: 27 June 2017" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "slideshow": { 24 | "slide_type": "fragment" 25 | } 26 | }, 27 | "source": [ 28 | "- In this notebook we cover packages that didn't have a home in one of the other sections\n", 29 | "- These include packages for computing derivatives, basic statistics, handling data and more\n", 30 | "- Anyone interested in autodiff can look at [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) and [ReverseDiff.jl](https://github.com/JuliaDiff/ReverseDiff.jl)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": { 36 | "slideshow": { 37 | "slide_type": "slide" 38 | } 39 | }, 40 | "source": [ 41 | "## Distributions.jl\n", 42 | "\n", 43 | "- In my opinion Distributions.jl is one of the best examples of flexible, performant, and idiomatic Julia code\n", 44 | "- Provides routines for working with probability distributions and...\n", 45 | " - Computing moments/statistics: mean, median, mode, entropy, mgf, quantile\n", 46 | " - Probability evaluation: pdf, cdf, ccdf, quantile, invlogcdf\n", 47 | " - Sampling: rand and rand!\n", 48 | "\n", 49 | "[Documentation](http://distributionsjl.readthedocs.io/en/latest/)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 27, 55 | "metadata": { 56 | "collapsed": true 57 | }, 58 | "outputs": [], 59 | "source": [ 60 | "# Pkg.add(\"Distributions\")" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": { 66 | "slideshow": { 67 | "slide_type": "subslide" 68 | } 69 | }, 70 | "source": [ 71 | "### Distributions.jl Basics" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 28, 77 | "metadata": { 78 | "collapsed": true 79 | }, 80 | "outputs": [], 81 | "source": [ 82 | "using Distributions" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 29, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "67" 94 | ] 95 | }, 96 | "execution_count": 29, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "# all subtypes of `Distributions.Distribution`\n", 103 | "length(subtypes(Distribution))" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 30, 109 | "metadata": { 110 | "slideshow": { 111 | "slide_type": "fragment" 112 | } 113 | }, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "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", 120 | "\n" 121 | ] 122 | }, 123 | { 124 | "data": { 125 | "text/markdown": [ 126 | "```\n", 127 | "Normal(μ,σ)\n", 128 | "```\n", 129 | "\n", 130 | "The *Normal distribution* with mean `μ` and standard deviation `σ` has probability density function\n", 131 | "\n", 132 | "$$\n", 133 | "f(x; \\mu, \\sigma) = \\frac{1}{\\sqrt{2 \\pi \\sigma^2}}\n", 134 | "\\exp \\left( - \\frac{(x - \\mu)^2}{2 \\sigma^2} \\right)\n", 135 | "$$\n", 136 | "\n", 137 | "```julia\n", 138 | "Normal() # standard Normal distribution with zero mean and unit variance\n", 139 | "Normal(mu) # Normal distribution with mean mu and unit variance\n", 140 | "Normal(mu, sig) # Normal distribution with mean mu and variance sig^2\n", 141 | "\n", 142 | "params(d) # Get the parameters, i.e. (mu, sig)\n", 143 | "mean(d) # Get the mean, i.e. mu\n", 144 | "std(d) # Get the standard deviation, i.e. sig\n", 145 | "```\n", 146 | "\n", 147 | "External links\n", 148 | "\n", 149 | " * [Normal distribution on Wikipedia](http://en.wikipedia.org/wiki/Normal_distribution)\n" 150 | ], 151 | "text/plain": [ 152 | "```\n", 153 | "Normal(μ,σ)\n", 154 | "```\n", 155 | "\n", 156 | "The *Normal distribution* with mean `μ` and standard deviation `σ` has probability density function\n", 157 | "\n", 158 | "$$\n", 159 | "f(x; \\mu, \\sigma) = \\frac{1}{\\sqrt{2 \\pi \\sigma^2}}\n", 160 | "\\exp \\left( - \\frac{(x - \\mu)^2}{2 \\sigma^2} \\right)\n", 161 | "$$\n", 162 | "\n", 163 | "```julia\n", 164 | "Normal() # standard Normal distribution with zero mean and unit variance\n", 165 | "Normal(mu) # Normal distribution with mean mu and unit variance\n", 166 | "Normal(mu, sig) # Normal distribution with mean mu and variance sig^2\n", 167 | "\n", 168 | "params(d) # Get the parameters, i.e. (mu, sig)\n", 169 | "mean(d) # Get the mean, i.e. mu\n", 170 | "std(d) # Get the standard deviation, i.e. sig\n", 171 | "```\n", 172 | "\n", 173 | "External links\n", 174 | "\n", 175 | " * [Normal distribution on Wikipedia](http://en.wikipedia.org/wiki/Normal_distribution)\n" 176 | ] 177 | }, 178 | "execution_count": 30, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "?Normal # good documentation" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 31, 190 | "metadata": { 191 | "scrolled": false, 192 | "slideshow": { 193 | "slide_type": "fragment" 194 | } 195 | }, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "Working with distribution: Distributions.Normal{Float64}(μ=0.0, σ=1.0)\n", 202 | "mean(d) = 0.0\n", 203 | "rand(d, 2, 2) = [-1.21323 0.799726; 1.14577 0.857076]\n", 204 | "pdf(d, rand(d)) = 0.3826680403382609\n", 205 | "\n", 206 | "\n", 207 | "\n", 208 | "\n", 209 | "Working with distribution: Distributions.Beta{Float64}(α=1.0, β=2.0)\n", 210 | "mean(d) = 0.3333333333333333\n", 211 | "rand(d, 2, 2) = [0.158296 0.110813; 0.37878 0.25033]\n", 212 | "pdf(d, rand(d)) = 1.4286232897393212\n", 213 | "\n", 214 | "\n", 215 | "\n", 216 | "\n", 217 | "Working with distribution: Distributions.Chisq{Float64}(ν=5.0)\n", 218 | "mean(d) = 5.0\n", 219 | "rand(d, 2, 2) = [5.43613 4.38398; 8.48096 4.2077]\n", 220 | "pdf(d, rand(d)) = 0.059644830374106346\n", 221 | "\n", 222 | "\n", 223 | "\n", 224 | "\n", 225 | "Working with distribution: Distributions.Frechet{Float64}(α=5.0, θ=2.0)\n", 226 | "mean(d) = 2.3284594274506065\n", 227 | "rand(d, 2, 2) = [2.46281 1.72811; 2.94149 2.07315]\n", 228 | "pdf(d, rand(d)) = 0.9277002771067797\n", 229 | "\n", 230 | "\n", 231 | "\n", 232 | "\n", 233 | "Working with distribution: Distributions.Gamma{Float64}(α=1.0, θ=2.0)\n", 234 | "mean(d) = 2.0\n", 235 | "rand(d, 2, 2) = [13.4958 1.9001; 3.26745 0.10026]\n", 236 | "pdf(d, rand(d)) = 0.2225281540346122\n", 237 | "\n", 238 | "\n", 239 | "\n", 240 | "\n", 241 | "Working with distribution: Distributions.Pareto{Float64}(α=3.0, θ=2.0)\n", 242 | "mean(d) = 3.0\n", 243 | "rand(d, 2, 2) = [2.70097 3.41851; 3.08175 2.17007]\n", 244 | "pdf(d, rand(d)) = 1.333689336658193\n", 245 | "\n", 246 | "\n", 247 | "\n", 248 | "\n", 249 | "Working with distribution: Distributions.Binomial{Float64}(n=10, p=0.6)\n", 250 | "mean(d) = 6.0\n", 251 | "rand(d, 2, 2) = [5 7; 6 6]\n", 252 | "pdf(d, rand(d)) = 0.25082265600000003\n", 253 | "\n", 254 | "\n", 255 | "\n", 256 | "\n", 257 | "Working with distribution: Distributions.Poisson{Float64}(λ=0.7)\n", 258 | "mean(d) = 0.7\n", 259 | "rand(d, 2, 2) = [0 2; 1 0]\n", 260 | "pdf(d, rand(d)) = 0.4965853037914095\n", 261 | "\n", 262 | "\n", 263 | "\n", 264 | "\n", 265 | "Working with distribution: Distributions.MvLogNormal{Float64,PDMats.PDMat{Float64,Array{Float64,2}},Array{Float64,1}}(\n", 266 | "dim: 2\n", 267 | "μ: [1.0, 1.0]\n", 268 | "Σ: [3.0 0.0; 0.0 3.0]\n", 269 | ")\n", 270 | "\n", 271 | "mean(d) = [12.1825, 12.1825]\n", 272 | "rand(d, 2) = [1.02693 5.15986; 4.10579 2.93371]\n", 273 | "pdf(d, rand(d)) = 0.00880971882871988\n", 274 | "\n", 275 | "\n", 276 | "\n", 277 | "\n", 278 | "Working with distribution: Distributions.Dirichlet{Float64}(alpha=[0.1, 0.2, 0.3, 0.4])\n", 279 | "mean(d) = [0.1, 0.2, 0.3, 0.4]\n", 280 | "rand(d, 2) = [0.00212211 4.28933e-6; 0.0599607 0.000222481; 0.0819614 0.00932527; 0.855956 0.990448]\n", 281 | "pdf(d, rand(d)) = 123.02872581625883\n", 282 | "\n", 283 | "\n", 284 | "\n", 285 | "\n", 286 | "Working with distribution: Distributions.InverseWishart{Float64,PDMats.PDMat{Float64,Array{Float64,2}}}(\n", 287 | "df: 5.0\n", 288 | "Ψ: [1.0 0.0; 0.0 1.0]\n", 289 | ")\n", 290 | "\n", 291 | "mean(d) = [0.5 0.0; 0.0 0.5]\n", 292 | "rand(d, 2) = Array{Float64,2}[[0.0974866 -0.00231638; -0.00231638 0.125187], [0.0731395 0.0473001; 0.0473001 0.236627]]\n", 293 | "pdf(d, rand(d)) = 0.4320066190551551\n", 294 | "\n", 295 | "\n", 296 | "\n", 297 | "\n", 298 | "Working with distribution: MixtureModel{Distributions.Normal}(K = 3)\n", 299 | "components[1] (prior = 0.2000): Distributions.Normal{Float64}(μ=-2.0, σ=1.2)\n", 300 | "components[2] (prior = 0.5000): Distributions.Normal{Float64}(μ=0.0, σ=1.0)\n", 301 | "components[3] (prior = 0.3000): Distributions.Normal{Float64}(μ=3.0, σ=2.5)\n", 302 | "\n", 303 | "mean(d) = 0.4999999999999999\n", 304 | "rand(d, 2, 2) = [-0.848354 0.754772; -0.482289 -0.299589]\n", 305 | "pdf(d, rand(d)) = 0.20149882175875378\n", 306 | "\n", 307 | "\n", 308 | "\n", 309 | "\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "dists = [\n", 315 | " Normal(0, 1),\n", 316 | " Beta(1.0, 2.0),\n", 317 | " Chisq(5),\n", 318 | " Frechet(5.0, 2.0),\n", 319 | " Gamma(1.0, 2.0),\n", 320 | " Pareto(3.0, 2.0),\n", 321 | " Binomial(10, 0.6),\n", 322 | " Poisson(0.7),\n", 323 | " MvLogNormal(ones(2), 3*eye(2)),\n", 324 | " Dirichlet([0.1, 0.2, 0.3, 0.4]),\n", 325 | " InverseWishart(5, eye(2)),\n", 326 | " MixtureModel(Normal[\n", 327 | " Normal(-2.0, 1.2),\n", 328 | " Normal(0.0, 1.0),\n", 329 | " Normal(3.0, 2.5)], \n", 330 | " [0.2, 0.5, 0.3] # prior\n", 331 | " )\n", 332 | "]\n", 333 | "\n", 334 | "for d in dists\n", 335 | " println(\"Working with distribution: $(repr(d))\")\n", 336 | " @show mean(d)\n", 337 | " if isa(d, Distributions.UnivariateDistribution)\n", 338 | " @show rand(d, 2, 2)\n", 339 | " else\n", 340 | " @show rand(d, 2)\n", 341 | " end\n", 342 | " \n", 343 | " @show pdf(d, rand(d))\n", 344 | " println(\"\\n\\n\\n\")\n", 345 | "end" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": { 351 | "slideshow": { 352 | "slide_type": "subslide" 353 | } 354 | }, 355 | "source": [ 356 | "### More than you need\n" 357 | ] 358 | }, 359 | { 360 | "cell_type": "markdown", 361 | "metadata": {}, 362 | "source": [ 363 | "Let's list all the available distributions, by type of distribution" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 32, 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "name": "stdout", 373 | "output_type": "stream", 374 | "text": [ 375 | "Distributions.Distribution{Distributions.Matrixvariate,Distributions.Discrete}: \n", 376 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Matrixvariate,Distributions.Discrete,C} where C<:Distributions.Distribution]\n", 377 | "\n", 378 | "\n", 379 | "\n", 380 | "Distributions.Distribution{Distributions.Multivariate,Distributions.Discrete}: \n", 381 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Multivariate,Distributions.Discrete,C} where C<:Distributions.Distribution, Distributions.DirichletMultinomial, Distributions.Multinomial]\n", 382 | "\n", 383 | "\n", 384 | "\n", 385 | "Distributions.Distribution{Distributions.Univariate,Distributions.Discrete}: \n", 386 | "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", 387 | "\n", 388 | "\n", 389 | "\n", 390 | "Distributions.Distribution{Distributions.Matrixvariate,Distributions.Continuous}: \n", 391 | "subtypes(T) = Union{DataType, UnionAll}[Distributions.AbstractMixtureModel{Distributions.Matrixvariate,Distributions.Continuous,C} where C<:Distributions.Distribution, Distributions.InverseWishart, Distributions.Wishart]\n", 392 | "\n", 393 | "\n", 394 | "\n", 395 | "Distributions.Distribution{Distributions.Multivariate,Distributions.Continuous}: \n", 396 | "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", 397 | "\n", 398 | "\n", 399 | "\n", 400 | "Distributions.Distribution{Distributions.Univariate,Distributions.Continuous}: \n", 401 | "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", 402 | "\n", 403 | "\n", 404 | "\n" 405 | ] 406 | } 407 | ], 408 | "source": [ 409 | "dist_types = [\n", 410 | " Distributions.DiscreteMatrixDistribution,\n", 411 | " Distributions.DiscreteMultivariateDistribution,\n", 412 | " Distributions.DiscreteUnivariateDistribution,\n", 413 | " Distributions.ContinuousMatrixDistribution,\n", 414 | " Distributions.ContinuousMultivariateDistribution,\n", 415 | " Distributions.ContinuousUnivariateDistribution, \n", 416 | "]\n", 417 | "\n", 418 | "for T in dist_types\n", 419 | " println(\"$T: \")\n", 420 | " @show subtypes(T)\n", 421 | " println(\"\\n\\n\")\n", 422 | "end " 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 33, 428 | "metadata": { 429 | "slideshow": { 430 | "slide_type": "fragment" 431 | } 432 | }, 433 | "outputs": [ 434 | { 435 | "data": { 436 | "text/plain": [ 437 | "Distributions.Normal{Float64}(μ=-0.0015332949351311543, σ=1.0000359500569347)" 438 | ] 439 | }, 440 | "execution_count": 33, 441 | "metadata": {}, 442 | "output_type": "execute_result" 443 | } 444 | ], 445 | "source": [ 446 | "# fitting a distribution, given some samples\n", 447 | "fit_mle(Normal, randn(100_000)) # should get close to N(0, 1)" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 34, 453 | "metadata": { 454 | "slideshow": { 455 | "slide_type": "fragment" 456 | } 457 | }, 458 | "outputs": [ 459 | { 460 | "data": { 461 | "text/plain": [ 462 | "Distributions.Uniform{Float64}(a=1.0000075805099509, b=2.999950042341385)" 463 | ] 464 | }, 465 | "execution_count": 34, 466 | "metadata": {}, 467 | "output_type": "execute_result" 468 | } 469 | ], 470 | "source": [ 471 | "# do fitting with mle\n", 472 | "fit_mle(Uniform, rand(100_000) .* 2 .+ 1) # should get close to U(1, 3)" 473 | ] 474 | }, 475 | { 476 | "cell_type": "markdown", 477 | "metadata": { 478 | "slideshow": { 479 | "slide_type": "slide" 480 | } 481 | }, 482 | "source": [ 483 | "## Calculus.jl\n", 484 | "\n", 485 | "- Computes analytical derivatives of Julia `Expr`essions and accurate numerical derivatives of functions\n", 486 | "\n", 487 | "[Package](https://github.com/johnmyleswhite/Calculus.jl)" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 35, 493 | "metadata": { 494 | "collapsed": true 495 | }, 496 | "outputs": [], 497 | "source": [ 498 | "# Pkg.add(\"Calculus\")" 499 | ] 500 | }, 501 | { 502 | "cell_type": "markdown", 503 | "metadata": { 504 | "slideshow": { 505 | "slide_type": "subslide" 506 | } 507 | }, 508 | "source": [ 509 | "### Calculus.jl Basics" 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "execution_count": 36, 515 | "metadata": { 516 | "collapsed": true 517 | }, 518 | "outputs": [], 519 | "source": [ 520 | "using Calculus" 521 | ] 522 | }, 523 | { 524 | "cell_type": "markdown", 525 | "metadata": { 526 | "slideshow": { 527 | "slide_type": "fragment" 528 | } 529 | }, 530 | "source": [ 531 | "#### Symbolic derivatives" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 37, 537 | "metadata": {}, 538 | "outputs": [ 539 | { 540 | "data": { 541 | "text/plain": [ 542 | ":(cos(x))" 543 | ] 544 | }, 545 | "execution_count": 37, 546 | "metadata": {}, 547 | "output_type": "execute_result" 548 | } 549 | ], 550 | "source": [ 551 | "differentiate(:(sin(x)), :x)" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 38, 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "data": { 561 | "text/plain": [ 562 | ":(cos(y) * -(sin(sin(y))))" 563 | ] 564 | }, 565 | "execution_count": 38, 566 | "metadata": {}, 567 | "output_type": "execute_result" 568 | } 569 | ], 570 | "source": [ 571 | "differentiate(:(cos(sin(y))), :y)" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 39, 577 | "metadata": {}, 578 | "outputs": [ 579 | { 580 | "data": { 581 | "text/plain": [ 582 | ":(((1 - γ) * c ^ ((1 - γ) - 1)) / (1 - γ))" 583 | ] 584 | }, 585 | "execution_count": 39, 586 | "metadata": {}, 587 | "output_type": "execute_result" 588 | } 589 | ], 590 | "source": [ 591 | "differentiate(:(c^(1-γ)/(1-γ)), :c)" 592 | ] 593 | }, 594 | { 595 | "cell_type": "markdown", 596 | "metadata": { 597 | "slideshow": { 598 | "slide_type": "fragment" 599 | } 600 | }, 601 | "source": [ 602 | "#### Finite difference" 603 | ] 604 | }, 605 | { 606 | "cell_type": "code", 607 | "execution_count": 40, 608 | "metadata": {}, 609 | "outputs": [ 610 | { 611 | "data": { 612 | "text/plain": [ 613 | "-5.036193684304635e-12" 614 | ] 615 | }, 616 | "execution_count": 40, 617 | "metadata": {}, 618 | "output_type": "execute_result" 619 | } 620 | ], 621 | "source": [ 622 | "derivative(sin, 1.0) - cos(1.0)" 623 | ] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": 41, 628 | "metadata": {}, 629 | "outputs": [ 630 | { 631 | "data": { 632 | "text/plain": [ 633 | "-6.647716624952338e-7" 634 | ] 635 | }, 636 | "execution_count": 41, 637 | "metadata": {}, 638 | "output_type": "execute_result" 639 | } 640 | ], 641 | "source": [ 642 | "second_derivative(sin, 1.0) + sin(1.0)" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 42, 648 | "metadata": {}, 649 | "outputs": [ 650 | { 651 | "data": { 652 | "text/plain": [ 653 | "2-element Array{Float64,1}:\n", 654 | " 2.71828\n", 655 | " -1.0 " 656 | ] 657 | }, 658 | "execution_count": 42, 659 | "metadata": {}, 660 | "output_type": "execute_result" 661 | } 662 | ], 663 | "source": [ 664 | "Calculus.gradient(x -> exp(x[1]) + sin(x[2]) / x[1], [1.0, π])" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": 43, 670 | "metadata": {}, 671 | "outputs": [ 672 | { 673 | "data": { 674 | "text/plain": [ 675 | "2×2 Array{Float64,2}:\n", 676 | " 2.71828 1.0 \n", 677 | " 1.0 1.71123e-7" 678 | ] 679 | }, 680 | "execution_count": 43, 681 | "metadata": {}, 682 | "output_type": "execute_result" 683 | } 684 | ], 685 | "source": [ 686 | "Calculus.hessian(x -> exp(x[1]) + sin(x[2]) / x[1], [1.0, π])" 687 | ] 688 | }, 689 | { 690 | "cell_type": "code", 691 | "execution_count": 44, 692 | "metadata": {}, 693 | "outputs": [ 694 | { 695 | "data": { 696 | "text/plain": [ 697 | "2×2 Array{Float64,2}:\n", 698 | " 2.71828 0.0\n", 699 | " -1.22465e-16 -1.0" 700 | ] 701 | }, 702 | "execution_count": 44, 703 | "metadata": {}, 704 | "output_type": "execute_result" 705 | } 706 | ], 707 | "source": [ 708 | "Calculus.jacobian(x -> [exp(x[1]), sin(x[2]) / x[1]], [1.0, π], :central)" 709 | ] 710 | }, 711 | { 712 | "cell_type": "markdown", 713 | "metadata": { 714 | "slideshow": { 715 | "slide_type": "slide" 716 | } 717 | }, 718 | "source": [ 719 | "## SymEngine.jl\n", 720 | "\n", 721 | "- Next generation C++ backend for sympy computer algebra system\n", 722 | "- A very fast alternative to Calculus.jl for symbolic differentiation\n", 723 | "\n", 724 | "[Jula package](https://github.com/symengine/SymEngine.jl) and [\"Documentation\"](https://github.com/symengine/symengine/blob/master/symengine/cwrapper.h)" 725 | ] 726 | }, 727 | { 728 | "cell_type": "code", 729 | "execution_count": 45, 730 | "metadata": { 731 | "collapsed": true 732 | }, 733 | "outputs": [], 734 | "source": [ 735 | "# Pkg.add(\"SymEngine\")" 736 | ] 737 | }, 738 | { 739 | "cell_type": "markdown", 740 | "metadata": { 741 | "slideshow": { 742 | "slide_type": "subslide" 743 | } 744 | }, 745 | "source": [ 746 | "### SymEngine.jl Basics" 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": 46, 752 | "metadata": {}, 753 | "outputs": [], 754 | "source": [ 755 | "using SymEngine" 756 | ] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "execution_count": 47, 761 | "metadata": {}, 762 | "outputs": [ 763 | { 764 | "data": { 765 | "text/plain": [ 766 | "cos(x)" 767 | ] 768 | }, 769 | "execution_count": 47, 770 | "metadata": {}, 771 | "output_type": "execute_result" 772 | } 773 | ], 774 | "source": [ 775 | "# needs first argument to be of type SymEngine.Basic\n", 776 | "diff(Basic(:(sin(x))), :x)" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 48, 782 | "metadata": {}, 783 | "outputs": [ 784 | { 785 | "data": { 786 | "text/plain": [ 787 | "-cos(y)*sin(sin(y))" 788 | ] 789 | }, 790 | "execution_count": 48, 791 | "metadata": {}, 792 | "output_type": "execute_result" 793 | } 794 | ], 795 | "source": [ 796 | "diff(Basic(\"cos(sin(y))\"), :y)" 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": 49, 802 | "metadata": {}, 803 | "outputs": [ 804 | { 805 | "data": { 806 | "text/plain": [ 807 | "c^(-γ)" 808 | ] 809 | }, 810 | "execution_count": 49, 811 | "metadata": {}, 812 | "output_type": "execute_result" 813 | } 814 | ], 815 | "source": [ 816 | "diff(Basic(\"c^(1-γ)/(1-γ)\"), :c)" 817 | ] 818 | }, 819 | { 820 | "cell_type": "markdown", 821 | "metadata": { 822 | "slideshow": { 823 | "slide_type": "subslide" 824 | } 825 | }, 826 | "source": [ 827 | "Let's see how fast SymEngine is compared to Calculus.jl\n", 828 | "\n", 829 | "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" 830 | ] 831 | }, 832 | { 833 | "cell_type": "code", 834 | "execution_count": 50, 835 | "metadata": {}, 836 | "outputs": [], 837 | "source": [ 838 | "# Pkg.add(\"BenchmarkTools\")\n", 839 | "using BenchmarkTools" 840 | ] 841 | }, 842 | { 843 | "cell_type": "code", 844 | "execution_count": 51, 845 | "metadata": {}, 846 | "outputs": [ 847 | { 848 | "data": { 849 | "text/plain": [ 850 | "BenchmarkTools.Trial: \n", 851 | " memory estimate: 66.66 KiB\n", 852 | " allocs estimate: 1109\n", 853 | " --------------\n", 854 | " minimum time: 827.381 μs (0.00% GC)\n", 855 | " median time: 877.233 μs (0.00% GC)\n", 856 | " mean time: 1.218 ms (1.21% GC)\n", 857 | " maximum time: 11.096 ms (88.43% GC)\n", 858 | " --------------\n", 859 | " samples: 4075\n", 860 | " evals/sample: 1" 861 | ] 862 | }, 863 | "execution_count": 51, 864 | "metadata": {}, 865 | "output_type": "execute_result" 866 | } 867 | ], 868 | "source": [ 869 | "@benchmark Calculus.differentiate(:((y + r*a - ap)^(1-γ)/(1-γ)), :ap)" 870 | ] 871 | }, 872 | { 873 | "cell_type": "code", 874 | "execution_count": 52, 875 | "metadata": {}, 876 | "outputs": [ 877 | { 878 | "data": { 879 | "text/plain": [ 880 | "BenchmarkTools.Trial: \n", 881 | " memory estimate: 840 bytes\n", 882 | " allocs estimate: 42\n", 883 | " --------------\n", 884 | " minimum time: 56.437 μs (0.00% GC)\n", 885 | " median time: 58.088 μs (0.00% GC)\n", 886 | " mean time: 59.610 μs (0.00% GC)\n", 887 | " maximum time: 374.913 μs (0.00% GC)\n", 888 | " --------------\n", 889 | " samples: 10000\n", 890 | " evals/sample: 1" 891 | ] 892 | }, 893 | "execution_count": 52, 894 | "metadata": {}, 895 | "output_type": "execute_result" 896 | } 897 | ], 898 | "source": [ 899 | "@benchmark diff(Basic(\"(y + r*a - ap)^(1-γ)/(1-γ)\"), :ap)" 900 | ] 901 | }, 902 | { 903 | "cell_type": "markdown", 904 | "metadata": { 905 | "slideshow": { 906 | "slide_type": "slide" 907 | } 908 | }, 909 | "source": [ 910 | "## Data handling\n", 911 | "\n", 912 | "- Julia's data picture is young, but still maturing\n", 913 | "- Python is still my go-to choice for data cleaning/analysis\n", 914 | "- That being said, working with data in Julia is still doable and effective\n", 915 | "\n", 916 | "I won't demo them now, but some the key packages are:\n", 917 | "\n", 918 | "- [DataFrames.jl](https://github.com/JuliaStats/DataFrames.jl): Provides a DataFrame type for handling columnar data\n", 919 | "- [CSV.jl](https://github.com/JuliaData/CSV.jl): very high performance reading and writing of delimited data files\n", 920 | "- [DataStreams.jl](https://github.com/JuliaData/DataStreams.jl): provide an interface for streaming data from a source to a sink\n", 921 | "- [Query.jl](https://github.com/davidanthoff/Query.jl): filter, project, join, group any iterable data source" 922 | ] 923 | }, 924 | { 925 | "cell_type": "code", 926 | "execution_count": null, 927 | "metadata": { 928 | "collapsed": true 929 | }, 930 | "outputs": [], 931 | "source": [] 932 | } 933 | ], 934 | "metadata": { 935 | "anaconda-cloud": {}, 936 | "kernelspec": { 937 | "display_name": "Julia 0.6.0", 938 | "language": "julia", 939 | "name": "julia-0.6" 940 | }, 941 | "language_info": { 942 | "file_extension": ".jl", 943 | "mimetype": "application/julia", 944 | "name": "julia", 945 | "version": "0.6.0" 946 | }, 947 | "livereveal": { 948 | "scroll": true, 949 | "start_slideshow_at": "selected", 950 | "theme": "white", 951 | "transition": "fade" 952 | } 953 | }, 954 | "nbformat": 4, 955 | "nbformat_minor": 2 956 | } 957 | -------------------------------------------------------------------------------- /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 | "** @ CEF 2017** \n", 15 | "\n", 16 | "**Authors**: Chase Coleman and Spencer Lyon\n", 17 | "\n", 18 | "**Date**: 27 June 2017" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "There are a handful of economics specific Julia packages.\n", 26 | "\n", 27 | "We'll talk about QuantEcon.jl here, let Pearl talk about DSGE.jl next, and save Dolo.jl for tomorrow's talk." 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "slideshow": { 34 | "slide_type": "subslide" 35 | } 36 | }, 37 | "source": [ 38 | "## QuantEcon.jl\n", 39 | "\n", 40 | "What is QuantEcon?\n", 41 | "\n", 42 | "- Set of [lectures](https://lectures.quantecon.org/) by Tom Sargent and John Stachurski to teach computational economics and programming principles\n", 43 | "- A [community](http://discourse.quantecon.org) aimed at teaching best practices and encouraging collaboration\n", 44 | "- 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", 45 | "- [QuantEcon.jl](https://github.com/QuantEcon/QuantEcon.jl) is the Julia version of the software library" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 1, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "# Pkg.add(\"QuantEcon\")" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": { 62 | "slideshow": { 63 | "slide_type": "subslide" 64 | } 65 | }, 66 | "source": [ 67 | "### QuantEcon.jl Basics" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 2, 73 | "metadata": { 74 | "collapsed": true 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "using QuantEcon" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": { 84 | "slideshow": { 85 | "slide_type": "subslide" 86 | } 87 | }, 88 | "source": [ 89 | "#### Markov Chains\n", 90 | "\n", 91 | "QuantEcon.jl has rich support for working with discrete state MarkovChains" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 3, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "Discrete Markov Chain\n", 103 | "stochastic matrix of type Array{Float64,2}:\n", 104 | "[0.9 0.1; 0.2 0.8]" 105 | ] 106 | }, 107 | "execution_count": 3, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "mc = MarkovChain([0.9 0.1; 0.2 0.8])" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 4, 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "1-element Array{Array{Float64,1},1}:\n", 125 | " [0.666667, 0.333333]" 126 | ] 127 | }, 128 | "execution_count": 4, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "stationary_distributions(mc)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 5, 140 | "metadata": { 141 | "slideshow": { 142 | "slide_type": "fragment" 143 | } 144 | }, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "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", 151 | "\n" 152 | ] 153 | }, 154 | { 155 | "data": { 156 | "text/markdown": [ 157 | "Simulate one sample path of the Markov chain `mc`. The resulting vector has the state values of `mc` as elements.\n", 158 | "\n", 159 | "### Arguments\n", 160 | "\n", 161 | " * `mc::MarkovChain` : MarkovChain instance.\n", 162 | " * `ts_length::Int` : Length of simulation\n", 163 | " * `;init::Int=rand(1:n_states(mc))` : Initial state\n", 164 | "\n", 165 | "### Returns\n", 166 | "\n", 167 | " * `X::Vector` : Vector containing the sample path, with length\n", 168 | "\n", 169 | "ts_length\n" 170 | ], 171 | "text/plain": [ 172 | "Simulate one sample path of the Markov chain `mc`. The resulting vector has the state values of `mc` as elements.\n", 173 | "\n", 174 | "### Arguments\n", 175 | "\n", 176 | " * `mc::MarkovChain` : MarkovChain instance.\n", 177 | " * `ts_length::Int` : Length of simulation\n", 178 | " * `;init::Int=rand(1:n_states(mc))` : Initial state\n", 179 | "\n", 180 | "### Returns\n", 181 | "\n", 182 | " * `X::Vector` : Vector containing the sample path, with length\n", 183 | "\n", 184 | "ts_length\n" 185 | ] 186 | }, 187 | "execution_count": 5, 188 | "metadata": {}, 189 | "output_type": "execute_result" 190 | } 191 | ], 192 | "source": [ 193 | "?simulate" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 6, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/html": [ 204 | "1.3312" 205 | ], 206 | "text/plain": [ 207 | "1.3312" 208 | ] 209 | }, 210 | "execution_count": 6, 211 | "metadata": {}, 212 | "output_type": "execute_result" 213 | } 214 | ], 215 | "source": [ 216 | "mean(simulate(mc, 10000)) # should be roughly 2/3*1 + 1/3*2 = 4/3" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 7, 222 | "metadata": { 223 | "slideshow": { 224 | "slide_type": "subslide" 225 | } 226 | }, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "Discrete Markov Chain\n", 232 | "stochastic matrix of type Array{Float64,2}:\n", 233 | "[0.9 0.1; 0.2 0.8]" 234 | ] 235 | }, 236 | "execution_count": 7, 237 | "metadata": {}, 238 | "output_type": "execute_result" 239 | } 240 | ], 241 | "source": [ 242 | "# can also pass state_values as second argument\n", 243 | "mc2 = MarkovChain(\n", 244 | " mc.p, # re-use transition matrix\n", 245 | " [0.0, 42.0]\n", 246 | ")" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 8, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/html": [ 257 | "13.91544" 258 | ], 259 | "text/plain": [ 260 | "13.91544" 261 | ] 262 | }, 263 | "execution_count": 8, 264 | "metadata": {}, 265 | "output_type": "execute_result" 266 | } 267 | ], 268 | "source": [ 269 | "mean(simulate(mc2, 100000)) # should be roughly 2/3*0 + 1/3*42 = 14" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 9, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "data": { 279 | "text/plain": [ 280 | "10-element Array{Int64,1}:\n", 281 | " 2\n", 282 | " 2\n", 283 | " 2\n", 284 | " 2\n", 285 | " 1\n", 286 | " 1\n", 287 | " 1\n", 288 | " 1\n", 289 | " 1\n", 290 | " 1" 291 | ] 292 | }, 293 | "execution_count": 9, 294 | "metadata": {}, 295 | "output_type": "execute_result" 296 | } 297 | ], 298 | "source": [ 299 | "# can also simulate the indices\n", 300 | "simulate_indices(mc2, 10, init=2)" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 10, 306 | "metadata": { 307 | "slideshow": { 308 | "slide_type": "subslide" 309 | } 310 | }, 311 | "outputs": [ 312 | { 313 | "data": { 314 | "text/plain": [ 315 | "500×10 Array{Float64,2}:\n", 316 | " 42.0 42.0 42.0 42.0 42.0 42.0 42.0 42.0 42.0 42.0\n", 317 | " 42.0 42.0 42.0 42.0 42.0 0.0 0.0 42.0 42.0 42.0\n", 318 | " 42.0 42.0 42.0 42.0 0.0 0.0 0.0 0.0 42.0 42.0\n", 319 | " 42.0 42.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 42.0\n", 320 | " 42.0 0.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 42.0\n", 321 | " 42.0 0.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 42.0\n", 322 | " 42.0 0.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 42.0\n", 323 | " 42.0 0.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 324 | " 42.0 42.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 325 | " 42.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 326 | " 42.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 327 | " 42.0 42.0 42.0 42.0 0.0 0.0 0.0 0.0 0.0 0.0\n", 328 | " 42.0 42.0 42.0 42.0 0.0 42.0 0.0 0.0 0.0 0.0\n", 329 | " ⋮ ⋮ \n", 330 | " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 42.0 42.0 42.0\n", 331 | " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 42.0 42.0 0.0\n", 332 | " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 42.0 42.0 0.0\n", 333 | " 0.0 0.0 0.0 0.0 42.0 0.0 0.0 42.0 42.0 0.0\n", 334 | " 42.0 0.0 0.0 0.0 0.0 0.0 0.0 42.0 42.0 0.0\n", 335 | " 42.0 0.0 42.0 0.0 0.0 42.0 0.0 42.0 42.0 0.0\n", 336 | " 42.0 0.0 42.0 0.0 0.0 42.0 0.0 42.0 42.0 0.0\n", 337 | " 42.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 42.0 0.0\n", 338 | " 42.0 0.0 0.0 0.0 42.0 0.0 0.0 0.0 42.0 0.0\n", 339 | " 42.0 0.0 0.0 0.0 42.0 42.0 0.0 0.0 42.0 0.0\n", 340 | " 42.0 0.0 0.0 0.0 42.0 42.0 0.0 0.0 42.0 0.0\n", 341 | " 42.0 0.0 0.0 0.0 42.0 42.0 0.0 42.0 42.0 0.0" 342 | ] 343 | }, 344 | "execution_count": 10, 345 | "metadata": {}, 346 | "output_type": "execute_result" 347 | } 348 | ], 349 | "source": [ 350 | "# fill pre-allocated matrix with samples\n", 351 | "# each column is a time-series\n", 352 | "out = zeros(500, 10)\n", 353 | "simulate!(out, mc2, init=2)\n", 354 | "out" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 11, 360 | "metadata": { 361 | "slideshow": { 362 | "slide_type": "fragment" 363 | } 364 | }, 365 | "outputs": [ 366 | { 367 | "data": { 368 | "text/plain": [ 369 | "500×10 Array{Int64,2}:\n", 370 | " 2 2 2 2 2 2 2 2 2 2\n", 371 | " 2 1 1 2 2 1 2 1 2 2\n", 372 | " 2 2 2 2 2 2 1 1 2 2\n", 373 | " 1 2 2 2 2 2 1 1 2 2\n", 374 | " 1 2 2 2 2 2 1 1 2 2\n", 375 | " 2 2 2 2 2 2 1 1 2 2\n", 376 | " 2 1 2 2 2 2 1 1 2 2\n", 377 | " 1 1 2 2 2 2 1 1 2 2\n", 378 | " 1 1 2 1 2 1 1 1 1 2\n", 379 | " 1 1 2 1 1 1 1 2 1 1\n", 380 | " 1 1 2 2 1 1 1 2 2 1\n", 381 | " 1 1 2 1 1 1 1 2 2 1\n", 382 | " 1 1 1 1 1 1 1 1 2 1\n", 383 | " ⋮ ⋮ \n", 384 | " 1 1 2 1 1 2 1 2 1 2\n", 385 | " 1 1 2 1 2 2 1 2 2 2\n", 386 | " 1 1 2 1 2 1 2 2 2 2\n", 387 | " 2 1 2 1 1 1 2 2 2 1\n", 388 | " 2 2 1 2 1 1 2 1 2 1\n", 389 | " 2 2 1 2 1 1 1 1 2 1\n", 390 | " 1 2 1 2 1 1 1 1 2 1\n", 391 | " 2 2 1 2 1 1 1 1 2 1\n", 392 | " 2 1 1 2 1 2 2 1 2 1\n", 393 | " 2 1 1 2 1 2 2 1 2 2\n", 394 | " 2 1 1 1 1 2 2 1 2 2\n", 395 | " 2 1 1 1 1 2 1 1 2 2" 396 | ] 397 | }, 398 | "execution_count": 11, 399 | "metadata": {}, 400 | "output_type": "execute_result" 401 | } 402 | ], 403 | "source": [ 404 | "# same with indices\n", 405 | "out_inds = zeros(Int, 500, 10)\n", 406 | "simulate_indices!(out_inds, mc2, init=2)\n", 407 | "out_inds" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 12, 413 | "metadata": { 414 | "slideshow": { 415 | "slide_type": "fragment" 416 | } 417 | }, 418 | "outputs": [ 419 | { 420 | "data": { 421 | "text/html": [ 422 | "10-element Array{Method,1}:" 423 | ], 424 | "text/plain": [ 425 | "10-element Array{Method,1}:\n", 426 | " period(mc::QuantEcon.MarkovChain) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:224 \n", 427 | " communication_classes(mc::QuantEcon.MarkovChain) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:181 \n", 428 | " is_aperiodic(mc::QuantEcon.MarkovChain) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:209 \n", 429 | " is_irreducible(mc::QuantEcon.MarkovChain) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:195 \n", 430 | " n_states(mc::QuantEcon.MarkovChain) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:65 \n", 431 | " recurrent_classes(mc::QuantEcon.MarkovChain) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:166 \n", 432 | " simulate(mc::QuantEcon.MarkovChain, ts_length::Int64) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:364 \n", 433 | " simulate!(X::Union{AbstractArray{T,1} where T, AbstractArray{T,2} where T}, mc::QuantEcon.MarkovChain) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:389 \n", 434 | " simulate_indices(mc::QuantEcon.MarkovChain, ts_length::Int64) in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:419 \n", 435 | " simulate_indices!(X::Union{AbstractArray{T,1}, AbstractArray{T,2}}, mc::QuantEcon.MarkovChain) where T<:Integer in QuantEcon at /home/chase/.julia/v0.6/QuantEcon/src/markov/mc_tools.jl:442" 436 | ] 437 | }, 438 | "execution_count": 12, 439 | "metadata": {}, 440 | "output_type": "execute_result" 441 | } 442 | ], 443 | "source": [ 444 | "# other things we can do with MarkovChains\n", 445 | "methodswith(MarkovChain)" 446 | ] 447 | }, 448 | { 449 | "cell_type": "markdown", 450 | "metadata": { 451 | "slideshow": { 452 | "slide_type": "subslide" 453 | } 454 | }, 455 | "source": [ 456 | "#### Discretizing AR(1) process\n", 457 | "\n", 458 | "QuantEcon.jl provides two methods for discretizing an AR(1) process into an instance of MarkovChain:\n" 459 | ] 460 | }, 461 | { 462 | "cell_type": "code", 463 | "execution_count": 13, 464 | "metadata": { 465 | "slideshow": { 466 | "slide_type": "fragment" 467 | } 468 | }, 469 | "outputs": [ 470 | { 471 | "name": "stdout", 472 | "output_type": "stream", 473 | "text": [ 474 | "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", 475 | "\n" 476 | ] 477 | }, 478 | { 479 | "data": { 480 | "text/markdown": [ 481 | "Tauchen's (1996) method for approximating AR(1) process with finite markov chain\n", 482 | "\n", 483 | "The process follows\n", 484 | "\n", 485 | "```\n", 486 | "y_t = μ + ρ y_{t-1} + ε_t,\n", 487 | "```\n", 488 | "\n", 489 | "where ε_t ~ N (0, σ^2)\n", 490 | "\n", 491 | "##### Arguments\n", 492 | "\n", 493 | " * `N::Integer`: Number of points in markov process\n", 494 | " * `ρ::Real` : Persistence parameter in AR(1) process\n", 495 | " * `σ::Real` : Standard deviation of random component of AR(1) process\n", 496 | " * `μ::Real(0.0)` : Mean of AR(1) process\n", 497 | " * `n_std::Integer(3)` : The number of standard deviations to each side the process\n", 498 | "\n", 499 | "should span\n", 500 | "\n", 501 | "##### Returns\n", 502 | "\n", 503 | " * `mc::MarkovChain{Float64}` : Markov chain holding the state values and\n", 504 | "\n", 505 | "transition matrix\n" 506 | ], 507 | "text/plain": [ 508 | "Tauchen's (1996) method for approximating AR(1) process with finite markov chain\n", 509 | "\n", 510 | "The process follows\n", 511 | "\n", 512 | "```\n", 513 | "y_t = μ + ρ y_{t-1} + ε_t,\n", 514 | "```\n", 515 | "\n", 516 | "where ε_t ~ N (0, σ^2)\n", 517 | "\n", 518 | "##### Arguments\n", 519 | "\n", 520 | " * `N::Integer`: Number of points in markov process\n", 521 | " * `ρ::Real` : Persistence parameter in AR(1) process\n", 522 | " * `σ::Real` : Standard deviation of random component of AR(1) process\n", 523 | " * `μ::Real(0.0)` : Mean of AR(1) process\n", 524 | " * `n_std::Integer(3)` : The number of standard deviations to each side the process\n", 525 | "\n", 526 | "should span\n", 527 | "\n", 528 | "##### Returns\n", 529 | "\n", 530 | " * `mc::MarkovChain{Float64}` : Markov chain holding the state values and\n", 531 | "\n", 532 | "transition matrix\n" 533 | ] 534 | }, 535 | "execution_count": 13, 536 | "metadata": {}, 537 | "output_type": "execute_result" 538 | } 539 | ], 540 | "source": [ 541 | "?tauchen" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 14, 547 | "metadata": {}, 548 | "outputs": [ 549 | { 550 | "name": "stdout", 551 | "output_type": "stream", 552 | "text": [ 553 | "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", 554 | "\n" 555 | ] 556 | }, 557 | { 558 | "data": { 559 | "text/markdown": [ 560 | "Rouwenhorst's method to approximate AR(1) processes.\n", 561 | "\n", 562 | "The process follows\n", 563 | "\n", 564 | "```\n", 565 | "y_t = μ + ρ y_{t-1} + ε_t,\n", 566 | "```\n", 567 | "\n", 568 | "where ε_t ~ N (0, σ^2)\n", 569 | "\n", 570 | "##### Arguments\n", 571 | "\n", 572 | " * `N::Integer` : Number of points in markov process\n", 573 | " * `ρ::Real` : Persistence parameter in AR(1) process\n", 574 | " * `σ::Real` : Standard deviation of random component of AR(1) process\n", 575 | " * `μ::Real(0.0)` : Mean of AR(1) process\n", 576 | "\n", 577 | "##### Returns\n", 578 | "\n", 579 | " * `mc::MarkovChain{Float64}` : Markov chain holding the state values and\n", 580 | "\n", 581 | "transition matrix\n" 582 | ], 583 | "text/plain": [ 584 | "Rouwenhorst's method to approximate AR(1) processes.\n", 585 | "\n", 586 | "The process follows\n", 587 | "\n", 588 | "```\n", 589 | "y_t = μ + ρ y_{t-1} + ε_t,\n", 590 | "```\n", 591 | "\n", 592 | "where ε_t ~ N (0, σ^2)\n", 593 | "\n", 594 | "##### Arguments\n", 595 | "\n", 596 | " * `N::Integer` : Number of points in markov process\n", 597 | " * `ρ::Real` : Persistence parameter in AR(1) process\n", 598 | " * `σ::Real` : Standard deviation of random component of AR(1) process\n", 599 | " * `μ::Real(0.0)` : Mean of AR(1) process\n", 600 | "\n", 601 | "##### Returns\n", 602 | "\n", 603 | " * `mc::MarkovChain{Float64}` : Markov chain holding the state values and\n", 604 | "\n", 605 | "transition matrix\n" 606 | ] 607 | }, 608 | "execution_count": 14, 609 | "metadata": {}, 610 | "output_type": "execute_result" 611 | } 612 | ], 613 | "source": [ 614 | "?rouwenhorst" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": 15, 620 | "metadata": { 621 | "slideshow": { 622 | "slide_type": "fragment" 623 | } 624 | }, 625 | "outputs": [ 626 | { 627 | "name": "stdout", 628 | "output_type": "stream", 629 | "text": [ 630 | "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", 631 | "mc3.state_values = -3.6076892283052313:2.1350420507344947:15.60768922830522\n" 632 | ] 633 | }, 634 | { 635 | "data": { 636 | "text/plain": [ 637 | "20-element Array{Float64,1}:\n", 638 | " 2.79744\n", 639 | " 4.93248\n", 640 | " 7.06752\n", 641 | " 7.06752\n", 642 | " 7.06752\n", 643 | " 7.06752\n", 644 | " 7.06752\n", 645 | " 7.06752\n", 646 | " 4.93248\n", 647 | " 4.93248\n", 648 | " 4.93248\n", 649 | " 4.93248\n", 650 | " 4.93248\n", 651 | " 4.93248\n", 652 | " 4.93248\n", 653 | " 4.93248\n", 654 | " 4.93248\n", 655 | " 4.93248\n", 656 | " 4.93248\n", 657 | " 4.93248" 658 | ] 659 | }, 660 | "execution_count": 15, 661 | "metadata": {}, 662 | "output_type": "execute_result" 663 | } 664 | ], 665 | "source": [ 666 | "mc3 = rouwenhorst(10, 0.95, 1.0, 0.3)\n", 667 | "@show stationary_distributions(mc3)\n", 668 | "@show mc3.state_values\n", 669 | "simulate(mc3, 20)" 670 | ] 671 | }, 672 | { 673 | "cell_type": "markdown", 674 | "metadata": { 675 | "slideshow": { 676 | "slide_type": "subslide" 677 | } 678 | }, 679 | "source": [ 680 | "#### Quadrature routines\n", 681 | "\n", 682 | "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" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 16, 688 | "metadata": { 689 | "slideshow": { 690 | "slide_type": "fragment" 691 | } 692 | }, 693 | "outputs": [ 694 | { 695 | "data": { 696 | "text/plain": [ 697 | "12-element Array{Symbol,1}:\n", 698 | " :qnwbeta \n", 699 | " :qnwcheb \n", 700 | " :qnwequi \n", 701 | " :qnwgamma \n", 702 | " :qnwlege \n", 703 | " :qnwlogn \n", 704 | " :qnwmonomial1\n", 705 | " :qnwmonomial2\n", 706 | " :qnwnorm \n", 707 | " :qnwsimp \n", 708 | " :qnwtrap \n", 709 | " :qnwunif " 710 | ] 711 | }, 712 | "execution_count": 16, 713 | "metadata": {}, 714 | "output_type": "execute_result" 715 | } 716 | ], 717 | "source": [ 718 | "filter(x -> startswith(string(x), \"qnw\"), names(QuantEcon))" 719 | ] 720 | }, 721 | { 722 | "cell_type": "code", 723 | "execution_count": 17, 724 | "metadata": {}, 725 | "outputs": [ 726 | { 727 | "name": "stdout", 728 | "output_type": "stream", 729 | "text": [ 730 | "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", 731 | "\n" 732 | ] 733 | }, 734 | { 735 | "data": { 736 | "text/markdown": [ 737 | "Computes nodes and weights for multivariate normal distribution\n", 738 | "\n", 739 | "##### Arguments\n", 740 | "\n", 741 | " * `n::Union{Int, Vector{Int}}` : Number of desired nodes along each dimension\n", 742 | " * `mu::Union{Real, Vector{Real}}` : Mean along each dimension\n", 743 | " * `sig2::Union{Real, Vector{Real}, Matrix{Real}}(eye(length(n)))` : Covariance\n", 744 | "\n", 745 | "structure\n", 746 | "\n", 747 | "##### Returns\n", 748 | "\n", 749 | " * `nodes::Array{Float64}` : An array of quadrature nodes\n", 750 | " * `weights::Array{Float64}` : An array of corresponding quadrature weights\n", 751 | "\n", 752 | "##### Notes\n", 753 | "\n", 754 | "This function has many methods. I try to describe them here.\n", 755 | "\n", 756 | "`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", 757 | "\n", 758 | "`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", 759 | "\n", 760 | "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", 761 | "\n", 762 | "##### References\n", 763 | "\n", 764 | "Miranda, Mario J, and Paul L Fackler. Applied Computational Economics and Finance, MIT Press, 2002.\n" 765 | ], 766 | "text/plain": [ 767 | "Computes nodes and weights for multivariate normal distribution\n", 768 | "\n", 769 | "##### Arguments\n", 770 | "\n", 771 | " * `n::Union{Int, Vector{Int}}` : Number of desired nodes along each dimension\n", 772 | " * `mu::Union{Real, Vector{Real}}` : Mean along each dimension\n", 773 | " * `sig2::Union{Real, Vector{Real}, Matrix{Real}}(eye(length(n)))` : Covariance\n", 774 | "\n", 775 | "structure\n", 776 | "\n", 777 | "##### Returns\n", 778 | "\n", 779 | " * `nodes::Array{Float64}` : An array of quadrature nodes\n", 780 | " * `weights::Array{Float64}` : An array of corresponding quadrature weights\n", 781 | "\n", 782 | "##### Notes\n", 783 | "\n", 784 | "This function has many methods. I try to describe them here.\n", 785 | "\n", 786 | "`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", 787 | "\n", 788 | "`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", 789 | "\n", 790 | "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", 791 | "\n", 792 | "##### References\n", 793 | "\n", 794 | "Miranda, Mario J, and Paul L Fackler. Applied Computational Economics and Finance, MIT Press, 2002.\n" 795 | ] 796 | }, 797 | "execution_count": 17, 798 | "metadata": {}, 799 | "output_type": "execute_result" 800 | } 801 | ], 802 | "source": [ 803 | "?qnwnorm" 804 | ] 805 | }, 806 | { 807 | "cell_type": "code", 808 | "execution_count": 18, 809 | "metadata": {}, 810 | "outputs": [ 811 | { 812 | "name": "stdout", 813 | "output_type": "stream", 814 | "text": [ 815 | "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", 816 | "\n" 817 | ] 818 | }, 819 | { 820 | "data": { 821 | "text/markdown": [ 822 | "Computes nodes and weights for beta distribution\n", 823 | "\n", 824 | "##### Arguments\n", 825 | "\n", 826 | " * `n::Union{Int, Vector{Int}}` : Number of desired nodes along each dimension\n", 827 | " * `a::Union{Real, Vector{Real}}` : First parameter of the gamma distribution,\n", 828 | "\n", 829 | "along each dimension\n", 830 | "\n", 831 | " * `b::Union{Real, Vector{Real}}` : Second parameter of the gamma distribution,\n", 832 | "\n", 833 | "along each dimension\n", 834 | "\n", 835 | "##### Returns\n", 836 | "\n", 837 | " * `nodes::Array{Float64}` : An array of quadrature nodes\n", 838 | " * `weights::Array{Float64}` : An array of corresponding quadrature weights\n", 839 | "\n", 840 | "##### Notes\n", 841 | "\n", 842 | "If any of the parameters to this function are scalars while others are `Vector`s of length `n`, the the scalar parameter is repeated `n` times.\n", 843 | "\n", 844 | "##### References\n", 845 | "\n", 846 | "Miranda, Mario J, and Paul L Fackler. Applied Computational Economics and Finance, MIT Press, 2002.\n" 847 | ], 848 | "text/plain": [ 849 | "Computes nodes and weights for beta distribution\n", 850 | "\n", 851 | "##### Arguments\n", 852 | "\n", 853 | " * `n::Union{Int, Vector{Int}}` : Number of desired nodes along each dimension\n", 854 | " * `a::Union{Real, Vector{Real}}` : First parameter of the gamma distribution,\n", 855 | "\n", 856 | "along each dimension\n", 857 | "\n", 858 | " * `b::Union{Real, Vector{Real}}` : Second parameter of the gamma distribution,\n", 859 | "\n", 860 | "along each dimension\n", 861 | "\n", 862 | "##### Returns\n", 863 | "\n", 864 | " * `nodes::Array{Float64}` : An array of quadrature nodes\n", 865 | " * `weights::Array{Float64}` : An array of corresponding quadrature weights\n", 866 | "\n", 867 | "##### Notes\n", 868 | "\n", 869 | "If any of the parameters to this function are scalars while others are `Vector`s of length `n`, the the scalar parameter is repeated `n` times.\n", 870 | "\n", 871 | "##### References\n", 872 | "\n", 873 | "Miranda, Mario J, and Paul L Fackler. Applied Computational Economics and Finance, MIT Press, 2002.\n" 874 | ] 875 | }, 876 | "execution_count": 18, 877 | "metadata": {}, 878 | "output_type": "execute_result" 879 | } 880 | ], 881 | "source": [ 882 | "?qnwgamma" 883 | ] 884 | }, 885 | { 886 | "cell_type": "markdown", 887 | "metadata": { 888 | "slideshow": { 889 | "slide_type": "subslide" 890 | } 891 | }, 892 | "source": [ 893 | "#### Root finding and optimization\n", 894 | "\n", 895 | "QuantEcon.jl provides a handful of routines for solving or optimizing univariate functions.\n", 896 | "\n", 897 | "The solvers are: `brent` `brenth`, `bisect` and `ridder` " 898 | ] 899 | }, 900 | { 901 | "cell_type": "code", 902 | "execution_count": 19, 903 | "metadata": { 904 | "slideshow": { 905 | "slide_type": "fragment" 906 | } 907 | }, 908 | "outputs": [ 909 | { 910 | "name": "stdout", 911 | "output_type": "stream", 912 | "text": [ 913 | "(solver, solver(f2, 0.5, 2.0)) = (QuantEcon.brent, 1.0000000000001794)\n", 914 | "(solver, solver(f2, 0.5, 2.0)) = (QuantEcon.brenth, 0.9999999999999903)\n", 915 | "(solver, solver(f2, 0.5, 2.0)) = (QuantEcon.bisect, 1.0000000000002274)\n", 916 | "(solver, solver(f2, 0.5, 2.0)) = (QuantEcon.ridder, 1.0000000000005003)\n" 917 | ] 918 | } 919 | ], 920 | "source": [ 921 | "f2(x) = x^2 - 1\n", 922 | "for solver in [brent, brenth, bisect, ridder]\n", 923 | " @show solver, solver(f2, 0.5, 2.0)\n", 924 | "end" 925 | ] 926 | }, 927 | { 928 | "cell_type": "markdown", 929 | "metadata": {}, 930 | "source": [ 931 | "The optimization routine is `golden_method`" 932 | ] 933 | }, 934 | { 935 | "cell_type": "code", 936 | "execution_count": 20, 937 | "metadata": {}, 938 | "outputs": [ 939 | { 940 | "data": { 941 | "text/plain": [ 942 | "(7.071019315919172e-9, 1.0)" 943 | ] 944 | }, 945 | "execution_count": 20, 946 | "metadata": {}, 947 | "output_type": "execute_result" 948 | } 949 | ], 950 | "source": [ 951 | "# golden_method finds a max, let's find the min of x^2 - 1 between -1 and 1 (which is 0.0)\n", 952 | "xstar, f2star = golden_method(x -> -f2(x), -1, 1)" 953 | ] 954 | }, 955 | { 956 | "cell_type": "markdown", 957 | "metadata": { 958 | "slideshow": { 959 | "slide_type": "slide" 960 | } 961 | }, 962 | "source": [ 963 | "## DSGE.jl\n", 964 | "\n", 965 | "This package was written by the FRBNY team and implements the model they use for policy analysis. At this stage DSGE.jl can solve and do forecasting exercises for a few of the FRBNYs models.\n", 966 | "\n", 967 | "The library was written in a modular way so as to allow other users to specify their own models in the same format as those included with DSGE.jl and use the solution and forecasting routines implemented by the FRBNY.\n", 968 | "\n", 969 | "[Package](https://github.com/FRBNY-DSGE/DSGE.jl) and [Documentation](http://frbny-dsge.github.io/DSGE.jl/latest/)" 970 | ] 971 | }, 972 | { 973 | "cell_type": "markdown", 974 | "metadata": { 975 | "slideshow": { 976 | "slide_type": "slide" 977 | } 978 | }, 979 | "source": [ 980 | "## Dolo.jl\n", 981 | "\n", 982 | "Dolo is a tool to describe and solve economic models.\n", 983 | "\n", 984 | "It provides a simple classification scheme to describe\n", 985 | "many types of models, allows to write the models as simple\n", 986 | "text files and compiles these files into efficient Julia\n", 987 | "objects/functions/methods representing them.\n", 988 | "\n", 989 | "It also provides many reference solution algorithms\n", 990 | "to find the solution of these models under rational expectations.\n", 991 | "Dolo understand several types of nonlinear models with occasionnally\n", 992 | "binding constraints (with or without exogenous discrete shocks).\n", 993 | "\n", 994 | "Tomorrow evening we will present Dolo more formally in Parallel Sessions C\n", 995 | "\n", 996 | "[Package](https://github.com/EconForge/Dolo.jl) and documentation forthcoming (soon)" 997 | ] 998 | }, 999 | { 1000 | "cell_type": "code", 1001 | "execution_count": null, 1002 | "metadata": { 1003 | "collapsed": true 1004 | }, 1005 | "outputs": [], 1006 | "source": [] 1007 | } 1008 | ], 1009 | "metadata": { 1010 | "anaconda-cloud": {}, 1011 | "kernelspec": { 1012 | "display_name": "Julia 0.6.0", 1013 | "language": "julia", 1014 | "name": "julia-0.6" 1015 | }, 1016 | "language_info": { 1017 | "file_extension": ".jl", 1018 | "mimetype": "application/julia", 1019 | "name": "julia", 1020 | "version": "0.6.0" 1021 | }, 1022 | "livereveal": { 1023 | "scroll": true, 1024 | "start_slideshow_at": "selected", 1025 | "theme": "white", 1026 | "transition": "fade" 1027 | } 1028 | }, 1029 | "nbformat": 4, 1030 | "nbformat_minor": 2 1031 | } 1032 | -------------------------------------------------------------------------------- /julia_language/julia_language.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Introduction to Julia\n", 8 | "\n", 9 | "Abhi Gupta\n", 10 | "\n", 11 | "6/26/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) and [Vectors, Arrays, and Matrices](https://lectures.quantecon.org/jl/julia_arrays.html) lectures.\n", 16 | "\n", 17 | "### Outline\n", 18 | "\n", 19 | "1. Syntax Review\n", 20 | "2. Types and Multiple Dispatch\n", 21 | "3. Additional Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Syntax Review" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Most of the syntax covered here will be fairly familiar to users of MATLAB, but is worth covering in one place nonetheless." 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "### Hello World" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "println(\"Hello world!\")" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### Variable Assignment" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": { 67 | "collapsed": true 68 | }, 69 | "outputs": [], 70 | "source": [ 71 | "# Assign the value 10 to the variable x\n", 72 | "x = 10" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": { 79 | "collapsed": true 80 | }, 81 | "outputs": [], 82 | "source": [ 83 | "# Variable names can have Unicode characters\n", 84 | "# To get ϵ in the REPL, type \\epsilon\n", 85 | "ϵ = 1e-4" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "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", 93 | "\n", 94 | "Variable names are case-sensitive. By convention, they are in snake_case." 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "### Booleans" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "Equality comparisons:" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "0 == 1" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "collapsed": true 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "2 != 3" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": { 137 | "collapsed": true 138 | }, 139 | "outputs": [], 140 | "source": [ 141 | "3 <= 4" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Boolean operators:" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": { 155 | "collapsed": true 156 | }, 157 | "outputs": [], 158 | "source": [ 159 | "true && false" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": { 166 | "collapsed": true 167 | }, 168 | "outputs": [], 169 | "source": [ 170 | "true || false" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": { 177 | "collapsed": true 178 | }, 179 | "outputs": [], 180 | "source": [ 181 | "!true" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "### Strings" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "metadata": { 195 | "collapsed": true 196 | }, 197 | "outputs": [], 198 | "source": [ 199 | "# Strings are written using double quotes\n", 200 | "str = \"This is a string\"" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "metadata": { 207 | "collapsed": true 208 | }, 209 | "outputs": [], 210 | "source": [ 211 | "# Strings can also contain Unicode characters\n", 212 | "fancy_str = \"α is a string\"" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "metadata": { 219 | "collapsed": true 220 | }, 221 | "outputs": [], 222 | "source": [ 223 | "# String interpolation using $\n", 224 | "# The expression in parentheses is evaluated and the result is \n", 225 | "# inserted into the string\n", 226 | "\"2 + 2 = $(2+2)\"" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "### Functions" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": null, 239 | "metadata": { 240 | "collapsed": true 241 | }, 242 | "outputs": [], 243 | "source": [ 244 | "# Regular function definition\n", 245 | "function double(x)\n", 246 | " y = 2x # scalar multiplication does not need a *\n", 247 | " return y\n", 248 | "end" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": { 255 | "collapsed": true 256 | }, 257 | "outputs": [], 258 | "source": [ 259 | "# Inline function definition\n", 260 | "inline_double(x) = 2x" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": { 267 | "collapsed": true 268 | }, 269 | "outputs": [], 270 | "source": [ 271 | "# Functions can refer to variables that are in scope when the\n", 272 | "# function is defined\n", 273 | "a = 5\n", 274 | "add_a(x) = x + a\n", 275 | "add_a(1)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "metadata": { 282 | "collapsed": true 283 | }, 284 | "outputs": [], 285 | "source": [ 286 | "# Functions can return multiple arguments\n", 287 | "duple_of(x) = x, x + 1\n", 288 | "a, b = duple_of(3)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": { 295 | "collapsed": true 296 | }, 297 | "outputs": [], 298 | "source": [ 299 | "# Optional arguments - no more varargin!\n", 300 | "function foo(x, y = 0, override = 0)\n", 301 | " if override == 0\n", 302 | " return x + y\n", 303 | " else\n", 304 | " return override\n", 305 | " end\n", 306 | "end\n", 307 | "\n", 308 | "# Call with one argument\n", 309 | "foo(5)" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": null, 315 | "metadata": { 316 | "collapsed": true 317 | }, 318 | "outputs": [], 319 | "source": [ 320 | "# Call with two arguments\n", 321 | "foo(5, 3)" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": { 328 | "collapsed": true 329 | }, 330 | "outputs": [], 331 | "source": [ 332 | "# If we want to specify override, we must also specify y\n", 333 | "foo(5, 3, 100)" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": null, 339 | "metadata": { 340 | "collapsed": true 341 | }, 342 | "outputs": [], 343 | "source": [ 344 | "# Keyword arguments allow arguments to be identified by name\n", 345 | "# instead of only by position\n", 346 | "function join_strings(string1, string2; separator = \",\")\n", 347 | " return string1 * separator * string2\n", 348 | "end\n", 349 | "\n", 350 | "# Call without keyword argument\n", 351 | "join_strings(\"ciao\", \"mondo\")" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": { 358 | "collapsed": true 359 | }, 360 | "outputs": [], 361 | "source": [ 362 | "# Call with keyword argument\n", 363 | "join_strings(\"ciao\", \"mondo\"; separator = \" \")" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "### Arrays" 371 | ] 372 | }, 373 | { 374 | "cell_type": "markdown", 375 | "metadata": {}, 376 | "source": [ 377 | "Explicit array construction:" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": null, 383 | "metadata": { 384 | "collapsed": true 385 | }, 386 | "outputs": [], 387 | "source": [ 388 | "A = [1, 2]" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": null, 394 | "metadata": { 395 | "collapsed": true 396 | }, 397 | "outputs": [], 398 | "source": [ 399 | "B = [1 2 3; 4 5 6]" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "One-dimensional arrays `Array{Int64,1}` are also called (type-aliased) `Vector{Int64}`s. Two-dimensional arrays are called `Matrix{Int64}`s.\n", 407 | "\n", 408 | "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\")." 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "metadata": {}, 414 | "source": [ 415 | "Built-in array constructors:" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": null, 421 | "metadata": { 422 | "collapsed": true 423 | }, 424 | "outputs": [], 425 | "source": [ 426 | "zeros(2)" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": null, 432 | "metadata": { 433 | "collapsed": true 434 | }, 435 | "outputs": [], 436 | "source": [ 437 | "ones(2)" 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": null, 443 | "metadata": { 444 | "collapsed": true 445 | }, 446 | "outputs": [], 447 | "source": [ 448 | "eye(2)" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": null, 454 | "metadata": { 455 | "collapsed": true 456 | }, 457 | "outputs": [], 458 | "source": [ 459 | "fill(true, 2)" 460 | ] 461 | }, 462 | { 463 | "cell_type": "markdown", 464 | "metadata": {}, 465 | "source": [ 466 | "Matrix operations:" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": null, 472 | "metadata": { 473 | "collapsed": true 474 | }, 475 | "outputs": [], 476 | "source": [ 477 | "# Matrix transpose\n", 478 | "B'" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": null, 484 | "metadata": { 485 | "collapsed": true 486 | }, 487 | "outputs": [], 488 | "source": [ 489 | "# Matrix addition\n", 490 | "B + B" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": null, 496 | "metadata": { 497 | "collapsed": true 498 | }, 499 | "outputs": [], 500 | "source": [ 501 | "# Add a matrix to a vector using broadcasting\n", 502 | "B .+ A" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": { 509 | "collapsed": true 510 | }, 511 | "outputs": [], 512 | "source": [ 513 | "# Matrix inverse\n", 514 | "C = 4*eye(2)\n", 515 | "inv(C)" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": null, 521 | "metadata": { 522 | "collapsed": true 523 | }, 524 | "outputs": [], 525 | "source": [ 526 | "# Elementwise operations\n", 527 | "B .> 3" 528 | ] 529 | }, 530 | { 531 | "cell_type": "markdown", 532 | "metadata": {}, 533 | "source": [ 534 | "Access array elements using square brackets:" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": null, 540 | "metadata": { 541 | "collapsed": true 542 | }, 543 | "outputs": [], 544 | "source": [ 545 | "# First row of B\n", 546 | "B[1, :]" 547 | ] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "execution_count": null, 552 | "metadata": { 553 | "collapsed": true 554 | }, 555 | "outputs": [], 556 | "source": [ 557 | "# Element in row 2, column 3 of B\n", 558 | "B[2, 3]" 559 | ] 560 | }, 561 | { 562 | "cell_type": "markdown", 563 | "metadata": {}, 564 | "source": [ 565 | "### Control Flow" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": {}, 571 | "source": [ 572 | "If statements:" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": null, 578 | "metadata": { 579 | "collapsed": true 580 | }, 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 | "collapsed": true 605 | }, 606 | "outputs": [], 607 | "source": [ 608 | "i = 3\n", 609 | "while i > 0\n", 610 | " println(i)\n", 611 | " i = i - 1\n", 612 | "end" 613 | ] 614 | }, 615 | { 616 | "cell_type": "markdown", 617 | "metadata": {}, 618 | "source": [ 619 | "For loops:" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": null, 625 | "metadata": { 626 | "collapsed": true 627 | }, 628 | "outputs": [], 629 | "source": [ 630 | "# Iterate through ranges of numbers\n", 631 | "for i = 1:3\n", 632 | " println(i)\n", 633 | "end" 634 | ] 635 | }, 636 | { 637 | "cell_type": "code", 638 | "execution_count": null, 639 | "metadata": { 640 | "collapsed": true 641 | }, 642 | "outputs": [], 643 | "source": [ 644 | "# Iterate through arrays\n", 645 | "cities = [\"Boston\", \"New York\", \"Philadelphia\"]\n", 646 | "for city in cities\n", 647 | " println(city)\n", 648 | "end" 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": null, 654 | "metadata": { 655 | "collapsed": true 656 | }, 657 | "outputs": [], 658 | "source": [ 659 | "# Iterate through arrays of tuples using zip\n", 660 | "states = [\"MA\", \"NY\", \"PA\"]\n", 661 | "for (city, state) in zip(cities, states)\n", 662 | " println(\"$city, $state\")\n", 663 | "end" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": null, 669 | "metadata": { 670 | "collapsed": true 671 | }, 672 | "outputs": [], 673 | "source": [ 674 | "# Iterate through arrays and their indices using enumerate\n", 675 | "for (i, city) in enumerate(cities)\n", 676 | " println(\"City $i is $city\")\n", 677 | "end" 678 | ] 679 | }, 680 | { 681 | "attachments": {}, 682 | "cell_type": "markdown", 683 | "metadata": {}, 684 | "source": [ 685 | "### Exercises\n", 686 | "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", 687 | "\n", 688 | "2. Write a function that takes two 1-d arrays `x` and `y` and computes their inner product using `zip`.\n", 689 | "\n", 690 | "3. Write a function 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 operations will work on all 3 types)." 691 | ] 692 | }, 693 | { 694 | "cell_type": "markdown", 695 | "metadata": {}, 696 | "source": [ 697 | "## Types and Multiple Dispatch" 698 | ] 699 | }, 700 | { 701 | "cell_type": "markdown", 702 | "metadata": {}, 703 | "source": [ 704 | "A **data type** is a classification identifying the kind of data you have. An object’s type determines the possible values it can take on, which operations and functions can be applied to it, and how the computer stores it.\n", 705 | "\n", 706 | "Examples:\n", 707 | "\n", 708 | "- Numeric types: `Int64`, `Float64`\n", 709 | "- String types: `ASCIIString`, `UTF8String`\n", 710 | "- `Bool`\n", 711 | "- `Array`\n", 712 | "\n", 713 | "Names of types are written in UpperCamelCase.\n", 714 | "\n", 715 | "A **concrete instance** (also an object or a value) of a type `T` is a piece of data in memory that has type `T`.\n", 716 | "\n", 717 | "Variables are not data, but are simply names that point/refer to a specific piece of data. The underlying data that a variable refers to has a specific type." 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": null, 723 | "metadata": { 724 | "collapsed": true 725 | }, 726 | "outputs": [], 727 | "source": [ 728 | "# What is the type of 10?\n", 729 | "typeof(10)" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": null, 735 | "metadata": { 736 | "collapsed": true 737 | }, 738 | "outputs": [], 739 | "source": [ 740 | "# Is 10 an Int64?\n", 741 | "isa(10, Int64)" 742 | ] 743 | }, 744 | { 745 | "cell_type": "code", 746 | "execution_count": null, 747 | "metadata": { 748 | "collapsed": true 749 | }, 750 | "outputs": [], 751 | "source": [ 752 | "# What is the type of the elements of an array?\n", 753 | "X = [1.0, 2.0, 3.0]\n", 754 | "eltype(X)" 755 | ] 756 | }, 757 | { 758 | "cell_type": "markdown", 759 | "metadata": {}, 760 | "source": [ 761 | "### Composite Types" 762 | ] 763 | }, 764 | { 765 | "cell_type": "markdown", 766 | "metadata": {}, 767 | "source": [ 768 | "A **composite type** is a collection of named fields that can be treated as a single value. They bear a passing resemblance to MATLAB structs.\n", 769 | "\n", 770 | "All fields must be declared ahead of time. The double colon, `::`, constrains a field to contain values of a certain type. This is optional for any field." 771 | ] 772 | }, 773 | { 774 | "cell_type": "code", 775 | "execution_count": null, 776 | "metadata": { 777 | "collapsed": true 778 | }, 779 | "outputs": [], 780 | "source": [ 781 | "# Type definition\n", 782 | "type Parameter\n", 783 | " value::Float64\n", 784 | " transformation::Function # Function is a type!\n", 785 | " tex_label::String\n", 786 | " description::String\n", 787 | "end" 788 | ] 789 | }, 790 | { 791 | "cell_type": "markdown", 792 | "metadata": {}, 793 | "source": [ 794 | "When a type with $n$ fields is defined, a constructor (function that creates an instance of that type) that takes $n$ ordered arguments is automatically created. Additional constructors can be defined for convenience." 795 | ] 796 | }, 797 | { 798 | "cell_type": "code", 799 | "execution_count": null, 800 | "metadata": { 801 | "collapsed": true 802 | }, 803 | "outputs": [], 804 | "source": [ 805 | "# Creating an instance of the Parameter type using the default\n", 806 | "# constructor\n", 807 | "β = Parameter(0.9, identity, \"\\beta\", \"Discount rate\")" 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "execution_count": null, 813 | "metadata": { 814 | "collapsed": true 815 | }, 816 | "outputs": [], 817 | "source": [ 818 | "# Alternative constructors end with an appeal to the default\n", 819 | "# constructor\n", 820 | "function Parameter(value::Float64, tex_label::String)\n", 821 | " transformation = identity\n", 822 | " description = \"No description available\"\n", 823 | " return Parameter(value, transformation, tex_label, description)\n", 824 | "end\n", 825 | "\n", 826 | "α = Parameter(0.5, \"\\alpha\")" 827 | ] 828 | }, 829 | { 830 | "cell_type": "code", 831 | "execution_count": null, 832 | "metadata": { 833 | "collapsed": true 834 | }, 835 | "outputs": [], 836 | "source": [ 837 | "# Find the fields of an instance of a composite type\n", 838 | "fieldnames(α)" 839 | ] 840 | }, 841 | { 842 | "cell_type": "code", 843 | "execution_count": null, 844 | "metadata": { 845 | "collapsed": true 846 | }, 847 | "outputs": [], 848 | "source": [ 849 | "# Access a particular field using .\n", 850 | "α.value" 851 | ] 852 | }, 853 | { 854 | "cell_type": "code", 855 | "execution_count": null, 856 | "metadata": { 857 | "collapsed": true 858 | }, 859 | "outputs": [], 860 | "source": [ 861 | "# Fields are modifiable and can be assigned to, like \n", 862 | "# ordinary variables\n", 863 | "α.value = 0.75" 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": {}, 869 | "source": [ 870 | "### Subtyping" 871 | ] 872 | }, 873 | { 874 | "cell_type": "markdown", 875 | "metadata": {}, 876 | "source": [ 877 | "Types are hierarchically related to each other. All are subtypes of the `Any` type.\n", 878 | "\n", 879 | "There are two main kinds of types in Julia:\n", 880 | "\n", 881 | "1. Concrete types: familiar types that you can create instances of, like `Int64` or `Float64`.\n", 882 | "2. Abstract types: nodes in a type graph that serve to group similar kinds of objects. Abstract types cannot be instantiated and do not have explicitly declared fields. For example, `Integer` or `Number`." 883 | ] 884 | }, 885 | { 886 | "cell_type": "code", 887 | "execution_count": null, 888 | "metadata": { 889 | "collapsed": true 890 | }, 891 | "outputs": [], 892 | "source": [ 893 | "# Define an abstract type\n", 894 | "abstract Model" 895 | ] 896 | }, 897 | { 898 | "cell_type": "code", 899 | "execution_count": null, 900 | "metadata": { 901 | "collapsed": true 902 | }, 903 | "outputs": [], 904 | "source": [ 905 | "# Define concrete subtypes of that abstract type\n", 906 | "type VAR <: Model\n", 907 | " n_lags::Int64\n", 908 | " variables::Vector{Symbol}\n", 909 | " data::Matrix{Float64}\n", 910 | "end" 911 | ] 912 | }, 913 | { 914 | "cell_type": "code", 915 | "execution_count": null, 916 | "metadata": { 917 | "collapsed": true 918 | }, 919 | "outputs": [], 920 | "source": [ 921 | "# Check subtyping relation\n", 922 | "VAR <: Model" 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "execution_count": null, 928 | "metadata": { 929 | "collapsed": true 930 | }, 931 | "outputs": [], 932 | "source": [ 933 | "# Instances of the VAR type are also instances of the Model type\n", 934 | "model = VAR(1, [:gdp, :inflation]\n", 935 | "isa(model, Model)" 936 | ] 937 | }, 938 | { 939 | "cell_type": "code", 940 | "execution_count": null, 941 | "metadata": { 942 | "collapsed": true 943 | }, 944 | "outputs": [], 945 | "source": [ 946 | "# Why does this throw an error?\n", 947 | "3 <: Number" 948 | ] 949 | }, 950 | { 951 | "cell_type": "markdown", 952 | "metadata": {}, 953 | "source": [ 954 | "### Parameterized Types" 955 | ] 956 | }, 957 | { 958 | "cell_type": "markdown", 959 | "metadata": {}, 960 | "source": [ 961 | "**Parameterized types** are data types that are defined to handle values identically regardless of the type of those values.\n", 962 | "\n", 963 | "Arrays are a familiar example. An `Array{T,1}` is a one-dimensional array filled with objects of any type `T` (e.g. `Float64`, `String`)." 964 | ] 965 | }, 966 | { 967 | "cell_type": "code", 968 | "execution_count": null, 969 | "metadata": { 970 | "collapsed": true 971 | }, 972 | "outputs": [], 973 | "source": [ 974 | "# Defining a parametric point\n", 975 | "type Duple{T} # T is a parameter to the type Duple\n", 976 | " x::T\n", 977 | " y::T\n", 978 | "end" 979 | ] 980 | }, 981 | { 982 | "cell_type": "markdown", 983 | "metadata": {}, 984 | "source": [ 985 | "This single declaration defines an unlimited number of new types: `Duple{String}`, `Duple{Float64}`, etc. are all immediately usable." 986 | ] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": null, 991 | "metadata": { 992 | "collapsed": true 993 | }, 994 | "outputs": [], 995 | "source": [ 996 | "Duple(3, -15)" 997 | ] 998 | }, 999 | { 1000 | "cell_type": "code", 1001 | "execution_count": null, 1002 | "metadata": { 1003 | "collapsed": true 1004 | }, 1005 | "outputs": [], 1006 | "source": [ 1007 | "Duple(\"Broadway\", \"42nd St\")" 1008 | ] 1009 | }, 1010 | { 1011 | "cell_type": "code", 1012 | "execution_count": null, 1013 | "metadata": { 1014 | "collapsed": true 1015 | }, 1016 | "outputs": [], 1017 | "source": [ 1018 | "# What happens here?\n", 1019 | "Duple(1.5, 3)" 1020 | ] 1021 | }, 1022 | { 1023 | "cell_type": "markdown", 1024 | "metadata": {}, 1025 | "source": [ 1026 | "We can also restrict the type parameter `T`:" 1027 | ] 1028 | }, 1029 | { 1030 | "cell_type": "code", 1031 | "execution_count": null, 1032 | "metadata": { 1033 | "collapsed": true 1034 | }, 1035 | "outputs": [], 1036 | "source": [ 1037 | "# T can be any subtype of Number, but nothing else\n", 1038 | "type PlanarCoordinate{T<:Number}\n", 1039 | " x::T\n", 1040 | " y::T\n", 1041 | "end" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "code", 1046 | "execution_count": null, 1047 | "metadata": { 1048 | "collapsed": true 1049 | }, 1050 | "outputs": [], 1051 | "source": [ 1052 | "PlanarCoordinate(\"4th Ave\", \"14th St\")" 1053 | ] 1054 | }, 1055 | { 1056 | "cell_type": "markdown", 1057 | "metadata": {}, 1058 | "source": [ 1059 | "### Why Use Types?" 1060 | ] 1061 | }, 1062 | { 1063 | "cell_type": "markdown", 1064 | "metadata": {}, 1065 | "source": [ 1066 | "You can write all your code without thinking about types at all. If you do this, however, you’ll be missing out on some of the biggest benefits of using Julia.\n", 1067 | "\n", 1068 | "If you understand types, you can:\n", 1069 | "\n", 1070 | "- Write faster code\n", 1071 | "- Write expressive, clear, and well-structured programs (keep this in mind when we talk about functions)\n", 1072 | "- Reason more clearly about how your code works\n", 1073 | "\n", 1074 | "Even if you only use built-in functions and types, your code still takes advantage of Julia’s type system. That’s why it’s important to understand what types are and how to use them." 1075 | ] 1076 | }, 1077 | { 1078 | "cell_type": "code", 1079 | "execution_count": null, 1080 | "metadata": { 1081 | "collapsed": true 1082 | }, 1083 | "outputs": [], 1084 | "source": [ 1085 | "# Example: writing type-stable functions\n", 1086 | "function sumofsins_unstable(n::Float64) \n", 1087 | " sum = 0 \n", 1088 | " for i in 1:n \n", 1089 | " sum += sin(3.4) \n", 1090 | " end \n", 1091 | " return sum \n", 1092 | "end \n", 1093 | "\n", 1094 | "function sumofsins_stable(n::Float64) \n", 1095 | " sum = 0.0 \n", 1096 | " for i in 1:n \n", 1097 | " sum += sin(3.4) \n", 1098 | " end \n", 1099 | " return sum \n", 1100 | "end\n", 1101 | "\n", 1102 | "# Compile and run\n", 1103 | "sumofsins_unstable(1e5)\n", 1104 | "sumofsins_stable(1e5)" 1105 | ] 1106 | }, 1107 | { 1108 | "cell_type": "code", 1109 | "execution_count": null, 1110 | "metadata": { 1111 | "collapsed": true 1112 | }, 1113 | "outputs": [], 1114 | "source": [ 1115 | "@time sumofsins_unstable(1e5)" 1116 | ] 1117 | }, 1118 | { 1119 | "cell_type": "code", 1120 | "execution_count": null, 1121 | "metadata": { 1122 | "collapsed": true 1123 | }, 1124 | "outputs": [], 1125 | "source": [ 1126 | "@time sumofsins_stable(1e5)" 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "markdown", 1131 | "metadata": {}, 1132 | "source": [ 1133 | "In `sumofsins_stable`, the compiler is guaranteed that `sum` is of type `Float64` throughout; therefore, it saves time and memory. On the other hand, in `sumofsins_unstable`, the compiler must check the type of `sum` at each iteration of the loop. Let's look at the LLVM [intermediate representation](http://www.johnmyleswhite.com/notebook/2013/12/06/writing-type-stable-code-in-julia/)." 1134 | ] 1135 | }, 1136 | { 1137 | "cell_type": "markdown", 1138 | "metadata": {}, 1139 | "source": [ 1140 | "### Exercise\n", 1141 | "Write a function `solve_discrete_lyapunov` that solves the discrete Lyapunov equation $$S = ASA' + \\Sigma \\Sigma'$$ using the iterative procedure $$S_0 = \\Sigma \\Sigma'$$ $$S_{t+1} = A S_t A' + \\Sigma \\Sigma'$$ taking in as arguments the $n \\times n$ matrix $A$, the $n \\times k$ matrix $\\Sigma$, and a number of iterations. \n", 1142 | "You can assume that your $A$ and $\\Sigma$ matrices are of type `Matrix{Float64}` and the number of iterations is an `Integer`. Make sure to check your code for type stability!" 1143 | ] 1144 | }, 1145 | { 1146 | "cell_type": "markdown", 1147 | "metadata": {}, 1148 | "source": [ 1149 | "### Multiple Dispatch" 1150 | ] 1151 | }, 1152 | { 1153 | "cell_type": "markdown", 1154 | "metadata": {}, 1155 | "source": [ 1156 | "So far we have defined functions over argument lists of any type. Methods allow us to define functions “piecewise”. For any set of input arguments, we can define a **method**, a definition of one possible behavior for a function." 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "code", 1161 | "execution_count": null, 1162 | "metadata": { 1163 | "collapsed": true 1164 | }, 1165 | "outputs": [], 1166 | "source": [ 1167 | "# Define one method of the function print_type\n", 1168 | "function print_type(x::Number)\n", 1169 | " println(\"$x is a number\")\n", 1170 | "end" 1171 | ] 1172 | }, 1173 | { 1174 | "cell_type": "code", 1175 | "execution_count": null, 1176 | "metadata": { 1177 | "collapsed": true 1178 | }, 1179 | "outputs": [], 1180 | "source": [ 1181 | "# Define another method\n", 1182 | "function print_type(x::String)\n", 1183 | " println(\"$x is a string\")\n", 1184 | "end" 1185 | ] 1186 | }, 1187 | { 1188 | "cell_type": "code", 1189 | "execution_count": null, 1190 | "metadata": { 1191 | "collapsed": true 1192 | }, 1193 | "outputs": [], 1194 | "source": [ 1195 | "# Define yet another method\n", 1196 | "function print_type(x::Number, y::Number)\n", 1197 | " println(\"$x and $y are both numbers\")\n", 1198 | "end" 1199 | ] 1200 | }, 1201 | { 1202 | "cell_type": "code", 1203 | "execution_count": null, 1204 | "metadata": { 1205 | "collapsed": true, 1206 | "scrolled": true 1207 | }, 1208 | "outputs": [], 1209 | "source": [ 1210 | "# See all methods for a given function\n", 1211 | "methods(print_type)" 1212 | ] 1213 | }, 1214 | { 1215 | "cell_type": "markdown", 1216 | "metadata": {}, 1217 | "source": [ 1218 | "Julia uses **multiple dispatch** to decide which method of a function to execute when a function is applied. In particular, Julia compares the types of _all_ arguments to the signatures of the function’s methods in order to choose the applicable one, not just the first (hence \"multiple\")." 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "execution_count": null, 1224 | "metadata": { 1225 | "collapsed": true 1226 | }, 1227 | "outputs": [], 1228 | "source": [ 1229 | "print_type(5)" 1230 | ] 1231 | }, 1232 | { 1233 | "cell_type": "code", 1234 | "execution_count": null, 1235 | "metadata": { 1236 | "collapsed": true 1237 | }, 1238 | "outputs": [], 1239 | "source": [ 1240 | "print_type(\"foo\")" 1241 | ] 1242 | }, 1243 | { 1244 | "cell_type": "code", 1245 | "execution_count": null, 1246 | "metadata": { 1247 | "collapsed": true 1248 | }, 1249 | "outputs": [], 1250 | "source": [ 1251 | "# This throws an error because no method of print_type has been\n", 1252 | "# defined for this set of arguments\n", 1253 | "print_type([1, 2, 3])" 1254 | ] 1255 | }, 1256 | { 1257 | "cell_type": "markdown", 1258 | "metadata": {}, 1259 | "source": [ 1260 | "How is multiple dispatch useful for economic research? Recall that we defined the type `VAR` earlier, and made it a subtype of our abstract type `Model`. Let's define another subtype of `Model`:" 1261 | ] 1262 | }, 1263 | { 1264 | "cell_type": "code", 1265 | "execution_count": null, 1266 | "metadata": { 1267 | "collapsed": true 1268 | }, 1269 | "outputs": [], 1270 | "source": [ 1271 | "# Define a general linear model\n", 1272 | "type GLM <: Model\n", 1273 | " y_variables::Vector{Symbol}\n", 1274 | " x_variables::Vector{Symbol}\n", 1275 | " y_data::Matrix{Float64} # Nt * Ny\n", 1276 | " x_data::Matrix{Float64} # Nt * Nx\n", 1277 | "end" 1278 | ] 1279 | }, 1280 | { 1281 | "cell_type": "markdown", 1282 | "metadata": {}, 1283 | "source": [ 1284 | "Now we can use the same function name, `estimate`, to define different estimation behaviors for the different subtypes of `Model`:" 1285 | ] 1286 | }, 1287 | { 1288 | "cell_type": "code", 1289 | "execution_count": null, 1290 | "metadata": { 1291 | "collapsed": true 1292 | }, 1293 | "outputs": [], 1294 | "source": [ 1295 | "using Distributions\n", 1296 | "\n", 1297 | "function estimate(model::GLM)\n", 1298 | " # Estimate a general linear model using OLS\n", 1299 | "end\n", 1300 | "\n", 1301 | "function estimate(model::VAR)\n", 1302 | " # Estimate a VAR using maximum likelihood\n", 1303 | "end\n", 1304 | "\n", 1305 | "function estimate(model::VAR, prior::Distribution)\n", 1306 | " # Estimate a Bayesian VAR\n", 1307 | "end" 1308 | ] 1309 | }, 1310 | { 1311 | "cell_type": "code", 1312 | "execution_count": null, 1313 | "metadata": { 1314 | "collapsed": true 1315 | }, 1316 | "outputs": [], 1317 | "source": [ 1318 | "methods(estimate)" 1319 | ] 1320 | }, 1321 | { 1322 | "cell_type": "markdown", 1323 | "metadata": {}, 1324 | "source": [ 1325 | "### Exercise\n", 1326 | "\n", 1327 | "Implement the function `estimate(model::GLM)` using the given `GLM` type. That is, return a matrix of size $N_x \\times N_y$ of coefficients estimated using OLS. You may find the `pinv` and `inv`, or `qr` functions helpful.\n", 1328 | "\n", 1329 | "Test it on the following model:" 1330 | ] 1331 | }, 1332 | { 1333 | "cell_type": "code", 1334 | "execution_count": null, 1335 | "metadata": { 1336 | "collapsed": true 1337 | }, 1338 | "outputs": [], 1339 | "source": [ 1340 | "β = ones(2, 1) # Nx x Ny\n", 1341 | "x_data = rand(1000, 2) # Nt x Nx\n", 1342 | "y_data = x_data*β + randn(1000, 1) # Nt x Ny\n", 1343 | "model = GLM([:y1], [:x1, :x2], y_data, x_data)\n", 1344 | "# β_hat = estimate(model)" 1345 | ] 1346 | }, 1347 | { 1348 | "cell_type": "markdown", 1349 | "metadata": {}, 1350 | "source": [ 1351 | "### Writing Julian Code" 1352 | ] 1353 | }, 1354 | { 1355 | "cell_type": "markdown", 1356 | "metadata": {}, 1357 | "source": [ 1358 | "As we've seen, you can use Julia just like you use MATLAB and get faster code. However, to write faster and _better_ code, attempt to write in a “Julian” manner:\n", 1359 | "\n", 1360 | "- Define composite types as logically needed\n", 1361 | "- Write type-stable functions for best performance\n", 1362 | "- Take advantage of multiple dispatch to write code that looks like math\n", 1363 | "- Add methods to existing functions" 1364 | ] 1365 | }, 1366 | { 1367 | "cell_type": "markdown", 1368 | "metadata": {}, 1369 | "source": [ 1370 | "### Just-in-Time Compilation" 1371 | ] 1372 | }, 1373 | { 1374 | "cell_type": "markdown", 1375 | "metadata": {}, 1376 | "source": [ 1377 | "How is Julia so fast? Julia is just-in-time (JIT) compiled, which means (according to [this StackExchange answer](http://stackoverflow.com/questions/95635/what-does-a-just-in-time-jit-compiler-do), with emphasis mine):\n", 1378 | "\n", 1379 | "> A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster, typically the host CPU's native instruction set. _A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently._\n", 1380 | "\n", 1381 | "> This is in contrast to a traditional compiler that compiles all the code to machine language before the program is first run.\n", 1382 | "\n", 1383 | "In particular, Julia uses type information at runtime to optimize how your code is compiled. This is why writing type-stable code makes such a difference in speed!" 1384 | ] 1385 | }, 1386 | { 1387 | "cell_type": "markdown", 1388 | "metadata": {}, 1389 | "source": [ 1390 | "## Additional Exercises\n", 1391 | "\n", 1392 | "\n", 1393 | "1. Write a function `linapprox` that takes as arguments:\n", 1394 | " - A function `f` mapping some interval $[a, b]$ into $\\mathbb{R}$\n", 1395 | " - Two scalars `a` and `b` providing the limits of this interval\n", 1396 | " - An integer `n` determining the number of grid points\n", 1397 | " - A number `x` satisfying $a \\leq x \\leq b$\n", 1398 | "\n", 1399 | " 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" 1400 | ] 1401 | } 1402 | ], 1403 | "metadata": { 1404 | "celltoolbar": "Raw Cell Format", 1405 | "kernelspec": { 1406 | "display_name": "Julia 0.5.2", 1407 | "language": "julia", 1408 | "name": "julia-0.5" 1409 | }, 1410 | "language_info": { 1411 | "file_extension": ".jl", 1412 | "mimetype": "application/julia", 1413 | "name": "julia", 1414 | "version": "0.5.2" 1415 | } 1416 | }, 1417 | "nbformat": 4, 1418 | "nbformat_minor": 1 1419 | } 1420 | --------------------------------------------------------------------------------