From 824831d7928648808926fab2ac455a2338e8b8f8 Mon Sep 17 00:00:00 2001 From: Shunsuke Hori Date: Sat, 24 Feb 2018 17:30:03 -0800 Subject: [PATCH 1/4] add exercise05 with julia without for/if --- data/US_Data.xlsx | Bin 0 -> 19995 bytes exercise05_optional-jl.ipynb | 206 +++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 data/US_Data.xlsx create mode 100644 exercise05_optional-jl.ipynb diff --git a/data/US_Data.xlsx b/data/US_Data.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..0ccbbcfe186785fe854cca7fc9a170d0a654a6d0 GIT binary patch literal 19995 zcmeIaWmp}}wk`_6-QC^Y-66QUySsaEcemgW9D=)B2<{LdxV!tE$=YjwYiE7uJolV? z|Lr}0OsVSeR@dlvNcXH!3eq5;s6gOAkU&5{L_kE*;@r-_KtR!8KtL!!kRP-~?Co4k z?OgO#JRMA(b?H59Z3qiMKTzfYeE=N)Kd=AB8W>O+wd-R<>{LGz5baWn2oOMpmkFfOwhxXry&{R>S6(+n*q^#Swy_4~8u{tt=0* z=7$CvbT|?70}#p>G$1zDy&%IOMsQ;GP+|NXScPrVB5idnb#X%ttT+UlS4fd|ryui8 zXGw7+m~f*X!JZW0ywfVQ{Ms3Fxj81~f^@KDqbaSE>v+~E9=6SrFXub!;urKmyA&kO zb6pTOT;nf2eNmD2q8*D|;XIDFXnJI^8Jjkq30i8!ba}r*WwX{rQL--wiBuNeUkiv z&=^xlinG>Dfs3eu9Rwob-|6@AZFP+|=4gcIZi}@t3K^A$q{+Q1H0{aJ6@r@FDOKFD za&rL5ZT@!tE<-}flhUm-j=G}dOTP5z28sCWrDz@EB)vK|6iP8}C^}z8fYy+#=DN{C zHSoN!@@Z9QZ8JySQPO0V|6*#%A)G)sm(0mb2HJ?Tk@<49&xj4t?IX6TvIUoAt=7kmR^$ifNr_>yyjy>*2G+~59M1s`sQ!wPi_yR+f-mcU zu=VdoB2`vm_yo|11PBliHV`DRhYiC&?8M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|neu z46w$0(X?-l8wue8vYk6jRx^ljfH?bitm}H-G)MyUu>ndo5dSp>8#*}rPYC{{vwys@ zlVt5ze{Delaz_BWhSR#t4I5Bd@KNig)(AM!#ayk#IKPSXV`ak0Vq8rGd-jGQr&A=@!1{U|WG+?dCk}W}XR}?K$3o`b3Euw_XmYV-t(ZV!DvV8&y-d>JCrq>4fb4M~(;xVRa)9!n-!Ng; z(Vr-PKF<0JWs>VR%D+9_`V-;5-Z}jx4+K;q00i`J_fda}|C!(Z1t1{&`vm^TZvQsh z{S)-h{N*pu2GRdFpue-3KSBR%c=`(x2#8wZUncq=!Ae0I3}A1+_BlZSLI&jS*OI?} F{XcHBQ>_31 literal 0 HcmV?d00001 diff --git a/exercise05_optional-jl.ipynb b/exercise05_optional-jl.ipynb new file mode 100644 index 0000000..dcb168f --- /dev/null +++ b/exercise05_optional-jl.ipynb @@ -0,0 +1,206 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercise 5 (without using for or if)\n", + "- Required packages:\n", + " - `Plots.jl` (for plotting)\n", + " - `ExcelReaders.jl` (to read excel file)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING: Method definition midpoints(Base.Range{T} where " + ] + } + ], + "source": [ + "using Plots\n", + "pyplot()\n", + "using ExcelReaders" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Function for HP filter\n", + "I am using sparse matrix and a function `spdiagm` to create matrix `H`" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "T) in module Base at deprecated.jl:56 overwritten in module StatsBase at C:\\Users\\holly\\.julia\\v0.6\\StatsBase\\src\\hist.jl:535.\n", + "WARNING: Method definition midpoints(AbstractArray{T, 1} where T) in module Base at deprecated.jl:56 overwritten" + ] + }, + { + "data": { + "text/plain": [ + "hp_filter (generic function with 1 method)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + " in module StatsBase at C:\\Users\\holly\\.julia\\v0.6\\StatsBase\\src\\hist.jl:533.\n" + ] + } + ], + "source": [ + "function hp_filter(y::AbstractVector{T}, lambda::Real) where T <: Real\n", + " N = length(y)\n", + " H = spdiagm(-2 => fill(lambda, N-2),\n", + " -1 => vcat(-2lambda, fill(-4lambda, N-3), -2lambda),\n", + " 0 => vcat(1+lambda, 1+5lambda, fill(1+6lambda, N-4),\n", + " 1+5lambda, 1+lambda),\n", + " 1 => vcat(-2lambda, fill(-4lambda, N-3), -2lambda),\n", + " 2 => fill(lambda, N-2))\n", + " y_trend = H\\y\n", + " y_cyclical = y - y_trend\n", + " return y_trend, y_cyclical\n", + "end\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Apply HP filter" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 5.239342 seconds (3.03 M allocations: 162.836 MiB, 1.42% gc time)\n", + " 0.007106 seconds (623 allocations: 564.594 KiB)\n" + ] + }, + { + "data": { + "text/plain": [ + "([1775.78, 1797.81, 1819.83, 1841.86, 1863.89, 1885.93, 1907.97, 1930.02, 1952.08, 1974.14 … 12616.2, 12700.1, 12783.9, 12867.8, 12951.6, 13035.5, 13119.4, 13203.3, 13287.1, 13371.0], [47.6206, 59.0947, 47.0683, 27.9404, -20.0906, -48.8265, -50.269, -89.719, -37.4774, -1.24372 … 66.9964, 48.6446, 131.985, 94.7209, 14.2537, 25.1842, -19.4868, 0.740762, 33.9676, 20.1943])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = readxlsheet(\"data/US_Data.xlsx\", \"Data\")\n", + "y = collect(data[4:end, 2]) # removing header\n", + "T = length(y)\n", + "@time (ytr1600, yc1600) = hp_filter(Float64.(y), 1600)\n", + "@time (ytr100000, yc100000) = hp_filter(Float64.(y), 100000)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plot trend component" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "plot([log.(ytr1600), log.(ytr100000), log.(y)], lw = 2,\n", + " lab = [\"lambda = 1600\" \"lambda = 1e5\" \"raw value\"],\n", + " title = \"log of real GDP\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plot cyclical component" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p = plot([yc1600, yc100000], lw = 2,\n", + " lab = [\"lambda = 1600\" \"lambda = 1e5\"],\n", + " title = \"cyclical component of real GDP\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.6.2", + "language": "julia", + "name": "julia-0.6" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.6.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 487917b42ff2a65333288068a3b5832fdffd985b Mon Sep 17 00:00:00 2001 From: vikjam Date: Thu, 1 Mar 2018 20:44:34 -0800 Subject: [PATCH 2/4] Exercise 05 adding loop and format --- exercise05-jl.ipynb | 224 +++++++++++++++++++++++++++++++++++ exercise05_optional-jl.ipynb | 206 -------------------------------- 2 files changed, 224 insertions(+), 206 deletions(-) create mode 100644 exercise05-jl.ipynb delete mode 100644 exercise05_optional-jl.ipynb diff --git a/exercise05-jl.ipynb b/exercise05-jl.ipynb new file mode 100644 index 0000000..6feaf0a --- /dev/null +++ b/exercise05-jl.ipynb @@ -0,0 +1,224 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercise 5\n", + "- Required packages:\n", + " - `Plots.jl` (for plotting)\n", + " - `ExcelReaders.jl` (to read excel file)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "using Plots\n", + "pyplot()\n", + "using ExcelReaders" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Functions for HP filter\n", + "- First, we'll use the loop method.\n", + "- Second, we'll use a sparse matrix and via the function `spdiagm` to create matrix `H`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Loop version" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "function hp_loop(y::AbstractVector{T}, λ::Real) where T <: Real\n", + " N = length(y)\n", + " H = zeros(N, N);\n", + " for j = 1:N\n", + " for i = 1:N\n", + " if j == 1\n", + " H[1, 1] = 1λ;\n", + " H[2, 1] = -2λ;\n", + " H[3, 1] = λ;\n", + " elseif j == 2\n", + " H[1, 2] = -2λ;\n", + " H[2, 2] = 1 + 5λ;\n", + " H[3, 2] = -4λ;\n", + " H[4, 2] = λ;\n", + " elseif j == N - 1\n", + " H[N - 3, N - 1] = λ;\n", + " H[N - 2, N - 1] = -4λ;\n", + " H[N - 1, N - 1] = 1 + 5λ;\n", + " H[N, N - 1] = -2λ;\n", + " elseif j == N\n", + " H[N - 2, N] = λ;\n", + " H[N - 1, N] = -2λ;\n", + " H[N, N] = 1λ;\n", + " else\n", + " H[j - 2, j] = λ;\n", + " H[j - 1, j] = -4λ;\n", + " H[j, j] = 1 + 6λ;\n", + " H[j + 1, j] = -4λ;\n", + " H[j + 2, j] = λ;\n", + " end\n", + " end\n", + " end\n", + "\n", + " y_trend = H \\ y;\n", + " y_cyclical = y - y_trend;\n", + " return y_trend, y_cyclical\n", + "\n", + "end;" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Vectorized version" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "function hp_filter(y::AbstractVector{T}, λ::Real) where T <: Real\n", + " N = length(y)\n", + " H = spdiagm(-2 => fill(λ, N-2),\n", + " -1 => vcat(-2λ, fill(-4λ, N - 3), -2λ),\n", + " 0 => vcat(1 + λ, 1 + 5λ, fill(1 + 6λ, N-4),\n", + " 1 + 5λ, 1 + λ),\n", + " 1 => vcat(-2λ, fill(-4λ, N - 3), -2λ),\n", + " 2 => fill(λ, N-2))\n", + " y_trend = H \\ y\n", + " y_cyclical = y - y_trend\n", + " return y_trend, y_cyclical\n", + "end;" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Apply HP filter" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 5.828467 seconds (1.31 M allocations: 69.065 MiB, 8.22% gc time)\n", + " 0.080008 seconds (9.66 k allocations: 1.367 MiB)\n", + " 4.830055 seconds (2.13 M allocations: 116.765 MiB, 3.18% gc time)\n", + " 1.442218 seconds (268.92 k allocations: 14.670 MiB)\n" + ] + } + ], + "source": [ + "data = readxlsheet(\"data/US_Data.xlsx\", \"Data\");\n", + "y = collect(data[4:end, 2]); # removing header\n", + "T = length(y);\n", + "\n", + "# Looped version\n", + "@time (ytr1600, yc1600) = hp_loop(Float64.(y), 1600);\n", + "@time (ytr1e5, yc1e5) = hp_loop(Float64.(y), 1e5);\n", + "\n", + "# Sparse matrix version\n", + "@time (ytr1600, yc1600) = hp_filter(Float64.(y), 1600);\n", + "@time (ytr1e5, yc1e5) = hp_filter(Float64.(y), 1e5);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plot trend component" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "plot([log.(ytr1600), log.(ytr1e5), log.(y)], lw = 2,\n", + " lab = [\"λ = 1600\" \"λ = 10⁵\" \"Raw Data\"],\n", + " title = \"log of real GDP\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plot cyclical component" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p = plot([yc1600, yc1e5], lw = 2,\n", + " lab = [\"λ = 1600\" \"λ = 10⁵\" \"Raw Data\"],\n", + " title = \"log of real GDP\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.6.0", + "language": "julia", + "name": "julia-0.6" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.6.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/exercise05_optional-jl.ipynb b/exercise05_optional-jl.ipynb deleted file mode 100644 index dcb168f..0000000 --- a/exercise05_optional-jl.ipynb +++ /dev/null @@ -1,206 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Exercise 5 (without using for or if)\n", - "- Required packages:\n", - " - `Plots.jl` (for plotting)\n", - " - `ExcelReaders.jl` (to read excel file)" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "WARNING: Method definition midpoints(Base.Range{T} where " - ] - } - ], - "source": [ - "using Plots\n", - "pyplot()\n", - "using ExcelReaders" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Function for HP filter\n", - "I am using sparse matrix and a function `spdiagm` to create matrix `H`" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "T) in module Base at deprecated.jl:56 overwritten in module StatsBase at C:\\Users\\holly\\.julia\\v0.6\\StatsBase\\src\\hist.jl:535.\n", - "WARNING: Method definition midpoints(AbstractArray{T, 1} where T) in module Base at deprecated.jl:56 overwritten" - ] - }, - { - "data": { - "text/plain": [ - "hp_filter (generic function with 1 method)" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - " in module StatsBase at C:\\Users\\holly\\.julia\\v0.6\\StatsBase\\src\\hist.jl:533.\n" - ] - } - ], - "source": [ - "function hp_filter(y::AbstractVector{T}, lambda::Real) where T <: Real\n", - " N = length(y)\n", - " H = spdiagm(-2 => fill(lambda, N-2),\n", - " -1 => vcat(-2lambda, fill(-4lambda, N-3), -2lambda),\n", - " 0 => vcat(1+lambda, 1+5lambda, fill(1+6lambda, N-4),\n", - " 1+5lambda, 1+lambda),\n", - " 1 => vcat(-2lambda, fill(-4lambda, N-3), -2lambda),\n", - " 2 => fill(lambda, N-2))\n", - " y_trend = H\\y\n", - " y_cyclical = y - y_trend\n", - " return y_trend, y_cyclical\n", - "end\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Apply HP filter" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 5.239342 seconds (3.03 M allocations: 162.836 MiB, 1.42% gc time)\n", - " 0.007106 seconds (623 allocations: 564.594 KiB)\n" - ] - }, - { - "data": { - "text/plain": [ - "([1775.78, 1797.81, 1819.83, 1841.86, 1863.89, 1885.93, 1907.97, 1930.02, 1952.08, 1974.14 … 12616.2, 12700.1, 12783.9, 12867.8, 12951.6, 13035.5, 13119.4, 13203.3, 13287.1, 13371.0], [47.6206, 59.0947, 47.0683, 27.9404, -20.0906, -48.8265, -50.269, -89.719, -37.4774, -1.24372 … 66.9964, 48.6446, 131.985, 94.7209, 14.2537, 25.1842, -19.4868, 0.740762, 33.9676, 20.1943])" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "data = readxlsheet(\"data/US_Data.xlsx\", \"Data\")\n", - "y = collect(data[4:end, 2]) # removing header\n", - "T = length(y)\n", - "@time (ytr1600, yc1600) = hp_filter(Float64.(y), 1600)\n", - "@time (ytr100000, yc100000) = hp_filter(Float64.(y), 100000)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Plot trend component" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "plot([log.(ytr1600), log.(ytr100000), log.(y)], lw = 2,\n", - " lab = [\"lambda = 1600\" \"lambda = 1e5\" \"raw value\"],\n", - " title = \"log of real GDP\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Plot cyclical component" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "p = plot([yc1600, yc100000], lw = 2,\n", - " lab = [\"lambda = 1600\" \"lambda = 1e5\"],\n", - " title = \"cyclical component of real GDP\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Julia 0.6.2", - "language": "julia", - "name": "julia-0.6" - }, - "language_info": { - "file_extension": ".jl", - "mimetype": "application/julia", - "name": "julia", - "version": "0.6.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From ffe973e1a75d1aa8c06e746a7e2dfb241ddc67ae Mon Sep 17 00:00:00 2001 From: Shunsuke Hori Date: Fri, 2 Mar 2018 08:51:48 -0800 Subject: [PATCH 3/4] fix my previous code, simplify and extend functions --- exercise05-jl.ipynb | 232 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 193 insertions(+), 39 deletions(-) diff --git a/exercise05-jl.ipynb b/exercise05-jl.ipynb index 6feaf0a..638bf16 100644 --- a/exercise05-jl.ipynb +++ b/exercise05-jl.ipynb @@ -30,6 +30,20 @@ "- Second, we'll use a sparse matrix and via the function `spdiagm` to create matrix `H`." ] }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "abstract type MatrixType end\n", + "struct Dense <: MatrixType end\n", + "struct Sparse <: MatrixType end\n", + "abstract type AssignMethod end\n", + "struct Loop <: AssignMethod end\n", + "struct Vectorized <: AssignMethod end" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -39,17 +53,36 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "assign! (generic function with 1 method)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "function hp_loop(y::AbstractVector{T}, λ::Real) where T <: Real\n", - " N = length(y)\n", - " H = zeros(N, N);\n", + "function create_matrix(λ, N::Integer, ::Dense, ::Loop)\n", + " H = zeros(N, N)\n", + " assign!(H, λ, N)\n", + " return H\n", + "end\n", + "function create_matrix(λ, N::Integer, ::Sparse, ::Loop)\n", + " H = spzeros(N, N)\n", + " assign!(H, λ, N)\n", + " return H\n", + "end\n", + "function assign!(H::AbstractMatrix, λ, N) \n", " for j = 1:N\n", " for i = 1:N\n", " if j == 1\n", - " H[1, 1] = 1λ;\n", + " H[1, 1] = 1+λ;\n", " H[2, 1] = -2λ;\n", " H[3, 1] = λ;\n", " elseif j == 2\n", @@ -65,7 +98,7 @@ " elseif j == N\n", " H[N - 2, N] = λ;\n", " H[N - 1, N] = -2λ;\n", - " H[N, N] = 1λ;\n", + " H[N, N] = 1+λ;\n", " else\n", " H[j - 2, j] = λ;\n", " H[j - 1, j] = -4λ;\n", @@ -75,12 +108,7 @@ " end\n", " end\n", " end\n", - "\n", - " y_trend = H \\ y;\n", - " y_cyclical = y - y_trend;\n", - " return y_trend, y_cyclical\n", - "\n", - "end;" + "end" ] }, { @@ -92,18 +120,50 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "create_matrix (generic function with 4 methods)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function create_matrix(λ::Real, N::Integer, ::Sparse, ::Vectorized) \n", + " return spdiagm(-2 => fill(λ, N-2),\n", + " -1 => vcat(-2λ, fill(-4λ, N - 3), -2λ),\n", + " 0 => vcat(1 + λ, 1 + 5λ, fill(1 + 6λ, N-4),\n", + " 1 + 5λ, 1 + λ),\n", + " 1 => vcat(-2λ, fill(-4λ, N - 3), -2λ),\n", + " 2 => fill(λ, N-2))\n", + "end\n", + "function create_matrix(λ::Real, N::Integer, ::Dense, ::Vectorized)\n", + " H = zeros(N, N)\n", + " H += diagm(fill(λ, N-2), -2)\n", + " H += diagm(vcat(-2λ, fill(-4λ, N - 3), -2λ), -1)\n", + " H += diagm(vcat(1 + λ, 1 + 5λ, fill(1 + 6λ, N-4),\n", + " 1 + 5λ, 1 + λ), 0)\n", + " H += diagm(vcat(-2λ, fill(-4λ, N - 3), -2λ), 1)\n", + " H += diagm(fill(λ, N-2), 2)\n", + " return H\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "function hp_filter(y::AbstractVector{T}, λ::Real) where T <: Real\n", + "function hp_filter(y::AbstractVector{T}, λ::Real, mt::MatrixType, am::AssignMethod) where T <: Real\n", " N = length(y)\n", - " H = spdiagm(-2 => fill(λ, N-2),\n", - " -1 => vcat(-2λ, fill(-4λ, N - 3), -2λ),\n", - " 0 => vcat(1 + λ, 1 + 5λ, fill(1 + 6λ, N-4),\n", - " 1 + 5λ, 1 + λ),\n", - " 1 => vcat(-2λ, fill(-4λ, N - 3), -2λ),\n", - " 2 => fill(λ, N-2))\n", + " H = create_matrix(T(λ), N, mt, am)\n", " y_trend = H \\ y\n", " y_cyclical = y - y_trend\n", " return y_trend, y_cyclical\n", @@ -117,19 +177,34 @@ "## Apply HP filter" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "NOTE: First run for each include compilation time, so don't take it seriously." + ] + }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - " 5.828467 seconds (1.31 M allocations: 69.065 MiB, 8.22% gc time)\n", - " 0.080008 seconds (9.66 k allocations: 1.367 MiB)\n", - " 4.830055 seconds (2.13 M allocations: 116.765 MiB, 3.18% gc time)\n", - " 1.442218 seconds (268.92 k allocations: 14.670 MiB)\n" + "Looped dense\n", + " 2.542403 seconds (1.33 M allocations: 70.537 MiB, 1.11% gc time)\n", + " 0.023778 seconds (998 allocations: 954.137 KiB)\n", + "Looped sparse\n", + " 1.268429 seconds (250.85 k allocations: 13.654 MiB)\n", + " 0.017410 seconds (1.21 k allocations: 255.492 KiB)\n", + "Vectorized dense\n", + " 0.652097 seconds (284.03 k allocations: 20.536 MiB, 1.96% gc time)\n", + " 0.018391 seconds (1.24 k allocations: 5.349 MiB, 50.37% gc time)\n", + "Vectorized sparse\n", + " 1.745333 seconds (1.13 M allocations: 62.273 MiB, 1.25% gc time)\n", + " 0.006968 seconds (1.39 k allocations: 284.764 KiB)\n" ] } ], @@ -138,13 +213,72 @@ "y = collect(data[4:end, 2]); # removing header\n", "T = length(y);\n", "\n", - "# Looped version\n", - "@time (ytr1600, yc1600) = hp_loop(Float64.(y), 1600);\n", - "@time (ytr1e5, yc1e5) = hp_loop(Float64.(y), 1e5);\n", + "# Looped dense version\n", + "println(\"Looped dense\")\n", + "@time (ytr1600, yc1600) = hp_filter(Float64.(y), 1600, Dense(), Loop());\n", + "@time (ytr1e5, yc1e5) = hp_filter(Float64.(y), 1e5, Dense(), Loop());\n", + "\n", + "# Looped sparse version\n", + "println(\"Looped sparse\")\n", + "@time (ytr1600, yc1600) = hp_filter(Float64.(y), 1600, Sparse(), Loop());\n", + "@time (ytr1e5, yc1e5) = hp_filter(Float64.(y), 1e5, Sparse(), Loop());\n", + "\n", + "# Dense matrix version\n", + "println(\"Vectorized dense\")\n", + "@time (ytr1600, yc1600) = hp_filter(Float64.(y), 1600, Dense(), Vectorized());\n", + "@time (ytr1e5, yc1e5) = hp_filter(Float64.(y), 1e5, Dense(), Vectorized());\n", "\n", "# Sparse matrix version\n", - "@time (ytr1600, yc1600) = hp_filter(Float64.(y), 1600);\n", - "@time (ytr1e5, yc1e5) = hp_filter(Float64.(y), 1e5);" + "println(\"Vectorized sparse\")\n", + "@time (ytr1600, yc1600) = hp_filter(Float64.(y), 1600, Sparse(), Vectorized());\n", + "@time (ytr1e5, yc1e5) = hp_filter(Float64.(y), 1e5, Sparse(), Vectorized());" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## With large data" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Looped dense\n", + " 2.370069 seconds (23 allocations: 381.585 MiB, 7.99% gc time)\n", + "Looped sparse\n", + " 1.976020 seconds (104 allocations: 4.094 MiB)\n", + "Vectorized dense\n", + " 3.920070 seconds (310 allocations: 2.236 GiB, 18.76% gc time)\n", + "Vectorized sparse\n", + " 0.005138 seconds (383 allocations: 4.475 MiB)\n" + ] + } + ], + "source": [ + "N_test = 5_000\n", + "y_test = randn(N_test)\n", + "# Looped dense version\n", + "println(\"Looped dense\")\n", + "@time (ytrtest_del, yctest_del) = hp_filter(y_test, 1600, Dense(), Loop());\n", + "\n", + "# Looped sparse version\n", + "println(\"Looped sparse\")\n", + "@time (ytrtest_spl, yctest_spl) = hp_filter(y_test, 1600, Sparse(), Loop());\n", + "\n", + "# Dense matrix version\n", + "println(\"Vectorized dense\")\n", + "@time (ytrtest_dev, yctest_dev) = hp_filter(y_test, 1600, Dense(), Vectorized());\n", + "\n", + "# # Sparse matrix version\n", + "println(\"Vectorized sparse\")\n", + "@time (ytrtest_spv, yctest_spv) = hp_filter(y_test, 1600, Sparse(), Vectorized());" ] }, { @@ -156,16 +290,16 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "" + "" ] }, - "execution_count": 5, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -185,16 +319,16 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "" + "" ] }, - "execution_count": 6, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -207,8 +341,9 @@ } ], "metadata": { + "hide_input": false, "kernelspec": { - "display_name": "Julia 0.6.0", + "display_name": "Julia 0.6.2", "language": "julia", "name": "julia-0.6" }, @@ -216,7 +351,26 @@ "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", - "version": "0.6.1" + "version": "0.6.2" + }, + "toc": { + "colors": { + "hover_highlight": "#DAA520", + "running_highlight": "#FF0000", + "selected_highlight": "#FFD700" + }, + "moveMenuLeft": true, + "nav_menu": { + "height": "161px", + "width": "253px" + }, + "navigate_menu": true, + "number_sections": true, + "sideBar": true, + "threshold": 4, + "toc_cell": false, + "toc_section_display": "block", + "toc_window_display": false } }, "nbformat": 4, From 98fef6ea04fc177a166e761514b4b59caac65b64 Mon Sep 17 00:00:00 2001 From: vikjam Date: Sat, 3 Mar 2018 20:23:28 -0800 Subject: [PATCH 4/4] Added a bit more documentation --- exercise05-jl.ipynb | 122 ++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 77 deletions(-) diff --git a/exercise05-jl.ipynb b/exercise05-jl.ipynb index 638bf16..944e2cc 100644 --- a/exercise05-jl.ipynb +++ b/exercise05-jl.ipynb @@ -25,9 +25,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Functions for HP filter\n", - "- First, we'll use the loop method.\n", - "- Second, we'll use a sparse matrix and via the function `spdiagm` to create matrix `H`." + "## Functions for Hodrick-Prescott (HP) filter\n", + "This exercise allows us to showcase how different methods of computing the HP filter affects runtime and memory use.\n", + "\n", + "- First, we'll use the loop method with a dense matrix to create matrix $H$.\n", + "- Second, we'll do a loop again, but with a sparse matrix.\n", + "- Third, we'll vectorize the calculation with a dense matrix and via the function `spdiagm` to create matrix $H$.\n", + "- Finally, we'll vectorize the calculation with a sparse matrix and via the function `spdiagm` to create matrix $H$." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Declare the matrices" ] }, { @@ -55,29 +66,20 @@ "cell_type": "code", "execution_count": 3, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "assign! (generic function with 1 method)" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "function create_matrix(λ, N::Integer, ::Dense, ::Loop)\n", " H = zeros(N, N)\n", " assign!(H, λ, N)\n", " return H\n", - "end\n", + "end;\n", + "\n", "function create_matrix(λ, N::Integer, ::Sparse, ::Loop)\n", " H = spzeros(N, N)\n", " assign!(H, λ, N)\n", " return H\n", - "end\n", + "end;\n", + "\n", "function assign!(H::AbstractMatrix, λ, N) \n", " for j = 1:N\n", " for i = 1:N\n", @@ -108,7 +110,7 @@ " end\n", " end\n", " end\n", - "end" + "end;" ] }, { @@ -122,18 +124,7 @@ "cell_type": "code", "execution_count": 4, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "create_matrix (generic function with 4 methods)" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "function create_matrix(λ::Real, N::Integer, ::Sparse, ::Vectorized) \n", " return spdiagm(-2 => fill(λ, N-2),\n", @@ -142,7 +133,8 @@ " 1 + 5λ, 1 + λ),\n", " 1 => vcat(-2λ, fill(-4λ, N - 3), -2λ),\n", " 2 => fill(λ, N-2))\n", - "end\n", + "end;\n", + "\n", "function create_matrix(λ::Real, N::Integer, ::Dense, ::Vectorized)\n", " H = zeros(N, N)\n", " H += diagm(fill(λ, N-2), -2)\n", @@ -152,7 +144,7 @@ " H += diagm(vcat(-2λ, fill(-4λ, N - 3), -2λ), 1)\n", " H += diagm(fill(λ, N-2), 2)\n", " return H\n", - "end" + "end;" ] }, { @@ -181,7 +173,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "NOTE: First run for each include compilation time, so don't take it seriously." + "*Note*: First run for each include compilation time, so don't take it seriously. Run a second time to get a better sense of the differences." ] }, { @@ -194,17 +186,17 @@ "output_type": "stream", "text": [ "Looped dense\n", - " 2.542403 seconds (1.33 M allocations: 70.537 MiB, 1.11% gc time)\n", - " 0.023778 seconds (998 allocations: 954.137 KiB)\n", + " 5.267661 seconds (1.31 M allocations: 69.188 MiB, 6.57% gc time)\n", + " 0.048105 seconds (998 allocations: 954.137 KiB)\n", "Looped sparse\n", - " 1.268429 seconds (250.85 k allocations: 13.654 MiB)\n", - " 0.017410 seconds (1.21 k allocations: 255.492 KiB)\n", + " 1.623298 seconds (250.93 k allocations: 13.624 MiB, 2.09% gc time)\n", + " 0.025615 seconds (1.21 k allocations: 255.492 KiB)\n", "Vectorized dense\n", - " 0.652097 seconds (284.03 k allocations: 20.536 MiB, 1.96% gc time)\n", - " 0.018391 seconds (1.24 k allocations: 5.349 MiB, 50.37% gc time)\n", + " 1.128149 seconds (288.31 k allocations: 20.795 MiB, 3.79% gc time)\n", + " 0.024424 seconds (1.24 k allocations: 5.349 MiB)\n", "Vectorized sparse\n", - " 1.745333 seconds (1.13 M allocations: 62.273 MiB, 1.25% gc time)\n", - " 0.006968 seconds (1.39 k allocations: 284.764 KiB)\n" + " 3.065138 seconds (1.49 M allocations: 81.303 MiB, 3.46% gc time)\n", + " 0.014130 seconds (1.39 k allocations: 284.764 KiB)\n" ] } ], @@ -243,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -251,19 +243,16 @@ "output_type": "stream", "text": [ "Looped dense\n", - " 2.370069 seconds (23 allocations: 381.585 MiB, 7.99% gc time)\n", + " 8.708596 seconds (27 allocations: 381.585 MiB, 5.26% gc time)\n", "Looped sparse\n", - " 1.976020 seconds (104 allocations: 4.094 MiB)\n", - "Vectorized dense\n", - " 3.920070 seconds (310 allocations: 2.236 GiB, 18.76% gc time)\n", - "Vectorized sparse\n", - " 0.005138 seconds (383 allocations: 4.475 MiB)\n" + " " ] } ], "source": [ "N_test = 5_000\n", "y_test = randn(N_test)\n", + "\n", "# Looped dense version\n", "println(\"Looped dense\")\n", "@time (ytrtest_del, yctest_del) = hp_filter(y_test, 1600, Dense(), Loop());\n", @@ -285,25 +274,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Plot trend component" + "## Plot trend component\n", + "Now back to the actual assignment." ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "plot([log.(ytr1600), log.(ytr1e5), log.(y)], lw = 2,\n", " lab = [\"λ = 1600\" \"λ = 10⁵\" \"Raw Data\"],\n", @@ -319,20 +298,9 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "p = plot([yc1600, yc1e5], lw = 2,\n", " lab = [\"λ = 1600\" \"λ = 10⁵\" \"Raw Data\"],\n", @@ -343,7 +311,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "Julia 0.6.2", + "display_name": "Julia 0.6.0", "language": "julia", "name": "julia-0.6" }, @@ -351,7 +319,7 @@ "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", - "version": "0.6.2" + "version": "0.6.1" }, "toc": { "colors": {