diff --git a/Project.toml b/Project.toml index 3df3d24..9488478 100644 --- a/Project.toml +++ b/Project.toml @@ -12,8 +12,10 @@ JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MAT = "23992714-dd62-5051-b70f-ba57cb901cac" MutableNamedTuples = "af6c499f-54b4-48cc-bbd2-094bba7533c7" +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" +StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] diff --git a/examples/basic_example.jl b/examples/basic_example.jl index e5a494e..62fce24 100644 --- a/examples/basic_example.jl +++ b/examples/basic_example.jl @@ -3,7 +3,7 @@ # We start by importing the BeforeIT library and other useful libraries. import BeforeIT as Bit -using FileIO, Plots, StatsPlots +using FileIO, Plots # We then initialise the model loading some precomputed set of parameters and by specifying a number of epochs. @@ -43,18 +43,12 @@ end # We can then plot any time series stored in the data tracker, for example -p1 = plot(data.real_gdp, title = "gdp", titlefont = 10) -p2 = plot(data.real_household_consumption, title = "household cons.", titlefont = 10) -p3 = plot(data.real_government_consumption, title = "gov. cons.", titlefont = 10) -p4 = plot(data.real_capitalformation, title = "capital form.", titlefont = 10) -p5 = plot(data.real_exports, title = "exports", titlefont = 10) -p6 = plot(data.real_imports, title = "imports", titlefont = 10) -p7 = plot(data.wages, title = "wages", titlefont = 10) -p8 = plot(data.euribor, title = "euribor", titlefont = 10) -p9 = plot(data.nominal_gdp ./ data.real_gdp, title = "gdp deflator", titlefont = 10) +plot(data.real_gdp, title = "gdp", titlefont = 10) +# Or we can plot multiple time series at once using the function `plot_data` -plot(p1, p2, p3, p4, p5, p6, p7, p8, p9, layout = (3, 3), legend = false) +ps = Bit.plot_data(data, quantities = [:real_gdp, :real_household_consumption, :real_government_consumption, :real_capitalformation, :real_exports, :real_imports, :wages, :euribor, :gdp_deflator]) +plot(ps..., layout = (3, 3)) # To run multiple monte-carlo repetitions in parallel we can use @@ -69,38 +63,8 @@ Threads.nthreads() # To activate Julia with a specific number of threads, say 8, you can use the command # `julia -t 8` in the terminal. -# We can then plot the results of the monte-carlo repetitions. -# Since we are saving the initial data point, we effectively have T+1 data points in our time series. - -Te = T + 1 - -p1 = errorline(1:Te, data_vector.real_gdp, errorstyle = :ribbon, title = "gdp", titlefont = 10) -p2 = errorline( - 1:Te, - data_vector.real_household_consumption, - errorstyle = :ribbon, - title = "household cons.", - titlefont = 10, -) -p3 = - errorline(1:Te, data_vector.real_government_consumption, errorstyle = :ribbon, title = "gov. cons.", titlefont = 10) -p4 = errorline(1:Te, data_vector.real_capitalformation, errorstyle = :ribbon, title = "capital form.", titlefont = 10) -p5 = errorline(1:Te, data_vector.real_exports, errorstyle = :ribbon, title = "exports", titlefont = 10) -p6 = errorline(1:Te, data_vector.real_imports, errorstyle = :ribbon, title = "imports", titlefont = 10) -p7 = errorline(1:Te, data_vector.wages, errorstyle = :ribbon, title = "wages", titlefont = 10) -p8 = errorline(1:Te, data_vector.euribor, errorstyle = :ribbon, title = "euribor", titlefont = 10) -p9 = errorline( - 1:Te, - data_vector.nominal_gdp ./ data.real_gdp, - errorstyle = :ribbon, - title = "gdp deflator", - titlefont = 10, -) -plot(p1, p2, p3, p4, p5, p6, p7, p8, p9, layout = (3, 3), legend = false) - -plot(p1, p4, p5, p3, p8, p9, layout = (3, 2), legend = false, size = (400, 600), dpi = 300, left_margin = 3Plots.mm) - -plot(p1, p4, p5, p3, p8, p9, layout = (2, 3), legend = false, size = (600, 400), dpi = 300)#, left_margin = 3Plots.mm) - - -savefig("output.png") +# We can then plot the results of the monte-carlo repetitions using the function `plot_data_vector` + +ps = Bit.plot_data_vector(data_vector) +plot(ps..., layout = (3, 3)) + diff --git a/src/BeforeIT.jl b/src/BeforeIT.jl index 572d3a3..3b38f83 100644 --- a/src/BeforeIT.jl +++ b/src/BeforeIT.jl @@ -46,6 +46,7 @@ include("utils/epsilon.jl") include("utils/positive.jl") include("utils/toannual.jl") include("utils/get_predictions_from_sims.jl") +include("utils/plot_data_vector.jl") # calibration include("utils/calibration.jl") diff --git a/src/utils/plot_data_vector.jl b/src/utils/plot_data_vector.jl new file mode 100644 index 0000000..b737bc4 --- /dev/null +++ b/src/utils/plot_data_vector.jl @@ -0,0 +1,44 @@ +using Plots, StatsPlots + +const default_quantities = [:real_gdp, :real_household_consumption, :real_government_consumption, :real_capitalformation, :real_exports, :real_imports, :wages, :euribor, :gdp_deflator] + +# define a table that maps the quantities to concise names +const quantity_titles = Dict( + :real_gdp => "gdp", + :real_household_consumption => "household cons.", + :real_government_consumption => "gov. cons.", + :real_capitalformation => "capital form.", + :real_exports => "exports", + :real_imports => "imports", + :gdp_deflator => "gdp deflator", +) + +function plot_data_vector(data_vector::Vector{Data}; titlefont = 9, quantities = default_quantities) + Te = length(data_vector[1].wages) + ps = [] + + for q in quantities + # define title via the table only if the entry exists + title = haskey(quantity_titles, q) ? quantity_titles[q] : string(q) + if q == :gdp_deflator + push!(ps, errorline(1:Te, data_vector.nominal_gdp ./ data_vector.real_gdp, errorstyle = :ribbon, title = title, titlefont = titlefont, legend = false)) + else + push!(ps, errorline(1:Te, getproperty(data_vector, q), errorstyle = :ribbon, title = title, titlefont = titlefont, legend = false)) + end + end + return ps +end + +function plot_data(data::Data; titlefont = 9, quantities = default_quantities) + ps = [] + for q in quantities + title = haskey(quantity_titles, q) ? quantity_titles[q] : string(q) + if q == :gdp_deflator + push!(ps, plot(data.nominal_gdp ./ data.real_gdp, title = title, titlefont = titlefont, legend = false)) + else + push!(ps, plot(getproperty(data, q), title = title, titlefont = titlefont, legend = false)) + end + end + return ps +end +