Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting utility function #14

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
56 changes: 10 additions & 46 deletions examples/basic_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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))

1 change: 1 addition & 0 deletions src/BeforeIT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
44 changes: 44 additions & 0 deletions src/utils/plot_data_vector.jl
Original file line number Diff line number Diff line change
@@ -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

Loading