Skip to content

Commit c807275

Browse files
committed
Merge branch 'main' into combine_plots
2 parents cd0bc5a + 895d027 commit c807275

23 files changed

+540
-384
lines changed

.github/workflows/Downgrade.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
name: Downgrade
22
on:
3-
workflow_dispatch: # allows manual triggering
4-
pull_request:
3+
pull_request_target:
54
branches:
65
- master
76
paths-ignore:
87
- 'docs/**'
8+
workflow_dispatch: # allows manual triggering
99
push:
1010
branches:
1111
- master
@@ -17,14 +17,20 @@ jobs:
1717
strategy:
1818
matrix:
1919
version: ['lts']
20+
test_set: ["basic"]
21+
os: [ubuntu-latest]
22+
arch: [x64]
2023
steps:
2124
- uses: actions/checkout@v4
2225
- uses: julia-actions/setup-julia@v2
2326
with:
2427
version: ${{ matrix.version }}
25-
- uses: julia-actions/julia-downgrade-compat@v1
28+
arch: ${{ matrix.arch }}
29+
- uses: julia-actions/julia-downgrade-compat@v2
2630
# if: ${{ matrix.version == 'lts' }}
2731
with:
2832
skip: Pkg,TOML
2933
- uses: julia-actions/julia-buildpkg@v1
3034
- uses: julia-actions/julia-runtest@v1
35+
env:
36+
TEST_SET: ${{ matrix.test_set }}

Project.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MacroModelling"
22
uuid = "687ffad2-3618-405e-ac50-e0f7b9c75e44"
33
authors = ["Thore Kockerols <mail@thorekockerols.com>"]
4-
version = "0.1.40"
4+
version = "0.1.41"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
@@ -33,8 +33,6 @@ REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
3333
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
3434
RecursiveFactorization = "f2c3362d-daeb-58d1-803e-2bc74f2840b4"
3535
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
36-
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
37-
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
3836
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
3937
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
4038
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
@@ -49,6 +47,10 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
4947
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
5048
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
5149

50+
[extensions]
51+
StatsPlotsExt = "StatsPlots"
52+
TuringExt = "Turing"
53+
5254
[compat]
5355
ADTypes = "1"
5456
Accessors = "0.1"
@@ -59,12 +61,12 @@ CSV = "0.10"
5961
ChainRulesCore = "1"
6062
Combinatorics = "1"
6163
DataFrames = "1"
62-
DataStructures = "0.18"
64+
DataStructures = "0.18, 0.19"
6365
DifferentiationInterface = "0.6,0.7"
6466
DispatchDoctor = "0.4"
6567
DocStringExtensions = "0.8, 0.9"
6668
DynamicPPL = "0.23 - 0.36"
67-
DynarePreprocessor_jll = "6"
69+
DynarePreprocessor_jll = "6, 7"
6870
FiniteDifferences = "0.12"
6971
ForwardDiff = "0.10, 1"
7072
JET = "0.7, 0.8, 0.9"
@@ -89,8 +91,6 @@ REPL = "1"
8991
Random = "1"
9092
RecursiveFactorization = "0.2"
9193
Reexport = "1"
92-
Requires = "1"
93-
Revise = "3.8.0"
9494
RuntimeGeneratedFunctions = "0.5"
9595
SparseArrays = "1"
9696
SpecialFunctions = "2"

docs/src/tutorials/estimation.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ data = data(observables,:)
9999
Next we define the parameter priors using the Turing package. The `@model` macro of the Turing package allows us to define the prior distributions over the parameters and combine it with the (Kalman filter) loglikelihood of the model and parameters given the data with the help of the `get_loglikelihood` function. We define the prior distributions in an array and pass it on to the `arraydist` function inside the `@model` macro from the Turing package. It is also possible to define the prior distributions inside the macro but especially for reverse mode auto differentiation the `arraydist` function is substantially faster. When defining the prior distributions we can rely n the distribution implemented in the Distributions package. Note that the `μσ` parameter allows us to hand over the moments (`μ` and `σ`) of the distribution as parameters in case of the non-normal distributions (Gamma, Beta, InverseGamma), and we can also define upper and lower bounds truncating the distribution as third and fourth arguments to the distribution functions. Last but not least, we define the loglikelihood and add it to the posterior loglikelihood with the help of the `@addlogprob!` macro.
100100

101101
```@repl tutorial_2
102-
import Zygote
103102
import DynamicPPL
104103
import Turing
105104
import Turing: NUTS, sample, logpdf

docs/src/unfinished_docs/todo.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
- [ ] write tests/docs/technical details for nonlinear obc, forecasting, (non-linear) solution algorithms, SS solver, obc solver, and other algorithms
66
- [ ] replace RF with LinearSolve codes (RF has too many dependencies)
7+
- [ ] add FRB US model
8+
- [ ] check again return value when NSSS not found, maybe NaN is better here
9+
- [ ] error when parsing expression of the form: XYZ[0] = 0
710
- [ ] add caches to lyapunov krylov solvers
811
- [ ] allow not to define all parameters in @parameters and enter them later in subsequent calls. so you can do things like loading them from a file and putting them in. internally he would need to delay the solution until all parameters are defined
912
- [ ] eliminiate last elements of factorisation calls not using linearsolvers.jl, check whether they can be done with linearsolvers in case of a matrix as RHS (otherwise consider mumps for sparse matrix RHS)

src/plotting.jl renamed to ext/StatsPlotsExt.jl

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1+
module StatsPlotsExt
2+
3+
using MacroModelling
4+
import MacroModelling: ParameterType, ℳ, Symbol_input, String_input, Tolerances, merge_calculation_options, MODEL®, DATA®, PARAMETERS®, ALGORITHM®, FILTER®, VARIABLES®, SMOOTH®, SHOW_PLOTS®, SAVE_PLOTS®, SAVE_PLOTS_FORMATH®, SAVE_PLOTS_PATH®, PLOTS_PER_PAGE®, MAX_ELEMENTS_PER_LEGENDS_ROW®, EXTRA_LEGEND_SPACE®, PLOT_ATTRIBUTES®, QME®, SYLVESTER®, LYAPUNOV®, TOLERANCES®, VERBOSE®, DATA_IN_LEVELS®, PERIODS®, SHOCKS®, SHOCK_SIZE®, NEGATIVE_SHOCK®, GENERALISED_IRF®, INITIAL_STATE®, IGNORE_OBC®, CONDITIONS®, SHOCK_CONDITIONS®, LEVELS®, parse_shocks_input_to_index, parse_variables_input_to_index, replace_indices, filter_data_with_model, get_relevant_steady_states, replace_indices_in_symbol, parse_algorithm_to_state_update, girf, decompose_name, obc_objective_optim_fun, obc_constraint_optim_fun
5+
import DocStringExtensions: FIELDS, SIGNATURES, TYPEDEF, TYPEDSIGNATURES, TYPEDFIELDS
16
import LaTeXStrings
27

38
const irf_active_plot_container = Dict[]
49
const model_estimates_active_plot_container = Dict[]
510

11+
import StatsPlots
12+
import SparseArrays: SparseMatrixCSC
13+
import NLopt
14+
using DispatchDoctor
15+
16+
import MacroModelling: plot_irfs, plot_irf, plot_IRF, plot_simulations, plot_simulation, plot_solution, plot_girf, plot_conditional_forecast, plot_conditional_variance_decomposition, plot_forecast_error_variance_decomposition, plot_fevd, plot_model_estimates, plot_shock_decomposition, plotlyjs_backend, gr_backend
17+
18+
const default_plot_attributes = Dict(:size=>(700,500),
19+
:plot_titlefont => 10,
20+
:titlefont => 10,
21+
:guidefont => 8,
22+
:legendfontsize => 8,
23+
:tickfontsize => 8,
24+
:framestyle => :semi)
25+
26+
627
@stable default_mode = "disable" begin
728
"""
829
gr_backend()
@@ -11,7 +32,7 @@ Renaming and reexport of StatsPlots function `gr()` to define GR.jl as backend.
1132
# Returns
1233
- `StatsPlots.GRBackend`: backend instance.
1334
"""
14-
gr_backend = StatsPlots.gr
35+
gr_backend(args...; kwargs...) = StatsPlots.gr(args...; kwargs...)
1536

1637

1738

@@ -22,7 +43,7 @@ Renaming and reexport of StatsPlots function `plotlyjs()` to define PlotlyJS.jl
2243
# Returns
2344
- `StatsPlots.PlotlyJSBackend`: backend instance.
2445
"""
25-
plotlyjs_backend = StatsPlots.plotlyjs
46+
plotlyjs_backend(args...; kwargs...) = StatsPlots.plotlyjs(args...; kwargs...)
2647

2748

2849

@@ -1587,13 +1608,13 @@ end
15871608
"""
15881609
See [`plot_irf`](@ref)
15891610
"""
1590-
plot_IRF = plot_irf
1611+
plot_IRF(args...; kwargs...) = plot_irf(args...; kwargs...)
15911612

15921613

15931614
"""
15941615
See [`plot_irf`](@ref)
15951616
"""
1596-
plot_irfs = plot_irf
1617+
plot_irfs(args...; kwargs...) = plot_irf(args...; kwargs...)
15971618

15981619

15991620
"""
@@ -1820,12 +1841,12 @@ end
18201841
"""
18211842
See [`plot_conditional_variance_decomposition`](@ref)
18221843
"""
1823-
plot_fevd = plot_conditional_variance_decomposition
1844+
plot_fevd(args...; kwargs...) = plot_conditional_variance_decomposition(args...; kwargs...)
18241845

18251846
"""
18261847
See [`plot_conditional_variance_decomposition`](@ref)
18271848
"""
1828-
plot_forecast_error_variance_decomposition = plot_conditional_variance_decomposition
1849+
plot_forecast_error_variance_decomposition(args...; kwargs...) = plot_conditional_variance_decomposition(args...; kwargs...)
18291850

18301851

18311852

@@ -2559,4 +2580,6 @@ function plot_conditional_forecast(𝓂::ℳ,
25592580

25602581
end
25612582

2562-
end # dispatch_doctor
2583+
end # dispatch_doctor
2584+
2585+
end # module

ext/TuringExt.jl

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
module TuringExt
2+
3+
# We only import `truncated` now, as we will be defining our own wrappers
4+
# with the same names as the distributions.
5+
import Turing: truncated
6+
# We explicitly refer to the Turing distributions to avoid method overwriting.
7+
import Turing
8+
import DocStringExtensions: SIGNATURES
9+
using DispatchDoctor
10+
import MacroModelling: Normal, Beta, Cauchy, Gamma, InverseGamma
11+
12+
@stable default_mode = "disable" begin
13+
14+
#==========================================================================================
15+
Beta Distribution
16+
==========================================================================================#
17+
18+
"""
19+
$(SIGNATURES)
20+
Constructs a `Beta` distribution, optionally parameterized by its mean and standard deviation.
21+
22+
# Arguments
23+
- `μ` [Type: `Real`]: The first parameter (α) of the distribution, or the mean when `μσ=true`.
24+
- `σ` [Type: `Real`]: The second parameter (β) of the distribution, or the standard deviation when `μσ=true`.
25+
26+
# Keyword Arguments
27+
- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the `α` and `β` parameters.
28+
"""
29+
function Beta::Real, σ::Real; μσ::Bool=false)
30+
if μσ
31+
# Calculate alpha and beta from mean (μ) and standard deviation (σ)
32+
ν = μ * (1 - μ) / σ^2 - 1
33+
α = μ * ν
34+
β = (1 - μ) * ν
35+
return Turing.Beta(α, β)
36+
end
37+
# By default, treat μ and σ as the distribution parameters α and β
38+
return Turing.Beta(μ, σ)
39+
end
40+
41+
"""
42+
$(SIGNATURES)
43+
Constructs a truncated `Beta` distribution, optionally parameterized by its mean and standard deviation.
44+
45+
# Arguments
46+
- `μ` [Type: `Real`]: The first parameter (α) of the distribution, or the mean when `μσ=true`.
47+
- `σ` [Type: `Real`]: The second parameter (β) of the distribution, or the standard deviation when `μσ=true`.
48+
- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution.
49+
- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution.
50+
51+
# Keyword Arguments
52+
- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the `α` and `β` parameters.
53+
"""
54+
function Beta::Real, σ::Real, lower_bound::Real, upper_bound::Real; μσ::Bool=false)
55+
# Create the base distribution, then truncate it
56+
dist = Beta(μ, σ; μσ=μσ)
57+
return truncated(dist, lower_bound, upper_bound)
58+
end
59+
60+
61+
#==========================================================================================
62+
InverseGamma Distribution
63+
==========================================================================================#
64+
65+
"""
66+
$(SIGNATURES)
67+
Constructs an `InverseGamma` distribution, optionally parameterized by its mean and standard deviation.
68+
69+
# Arguments
70+
- `μ` [Type: `Real`]: The shape parameter (α) of the distribution, or the mean when `μσ=true`.
71+
- `σ` [Type: `Real`]: The scale parameter (β) of the distribution, or the standard deviation when `μσ=true`.
72+
73+
# Keyword Arguments
74+
- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the shape `α` and scale `β` parameters.
75+
"""
76+
function InverseGamma::Real, σ::Real; μσ::Bool=false)
77+
if μσ
78+
# Calculate shape (α) and scale (β) from mean (μ) and standard deviation (σ)
79+
α =/ σ)^2 + 2
80+
β = μ * ((μ / σ)^2 + 1)
81+
return Turing.InverseGamma(α, β)
82+
end
83+
# By default, treat μ and σ as the distribution parameters α and β
84+
return Turing.InverseGamma(μ, σ)
85+
end
86+
87+
"""
88+
$(SIGNATURES)
89+
Constructs a truncated `InverseGamma` distribution, optionally parameterized by its mean and standard deviation.
90+
91+
# Arguments
92+
- `μ` [Type: `Real`]: The shape parameter (α) of the distribution, or the mean when `μσ=true`.
93+
- `σ` [Type: `Real`]: The scale parameter (β) of the distribution, or the standard deviation when `μσ=true`.
94+
- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution.
95+
- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution.
96+
97+
# Keyword Arguments
98+
- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the shape `α` and scale `β` parameters.
99+
"""
100+
function InverseGamma::Real, σ::Real, lower_bound::Real, upper_bound::Real; μσ::Bool=false)
101+
# Create the base distribution, then truncate it
102+
dist = InverseGamma(μ, σ; μσ=μσ)
103+
return truncated(dist, lower_bound, upper_bound)
104+
end
105+
106+
107+
#==========================================================================================
108+
Gamma Distribution
109+
==========================================================================================#
110+
111+
"""
112+
$(SIGNATURES)
113+
Constructs a `Gamma` distribution, optionally parameterized by its mean and standard deviation.
114+
115+
# Arguments
116+
- `μ` [Type: `Real`]: The shape parameter (α) of the distribution, or the mean when `μσ=true`.
117+
- `σ` [Type: `Real`]: The rate parameter (θ) of the distribution, or the standard deviation when `μσ=true`.
118+
119+
# Keyword Arguments
120+
- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the shape `α` and scale `θ` parameters.
121+
"""
122+
function Gamma::Real, σ::Real; μσ::Bool=false)
123+
if μσ
124+
# Calculate shape (α) and scale (θ) from mean (μ) and standard deviation (σ)
125+
θ = σ^2 / μ
126+
α = μ / θ
127+
return Turing.Gamma(α, θ)
128+
end
129+
# By default, treat μ and σ as the distribution parameters α and θ
130+
return Turing.Gamma(μ, σ)
131+
end
132+
133+
"""
134+
$(SIGNATURES)
135+
Constructs a truncated `Gamma` distribution, optionally parameterized by its mean and standard deviation.
136+
137+
# Arguments
138+
- `μ` [Type: `Real`]: The shape parameter (α) of the distribution, or the mean when `μσ=true`.
139+
- `σ` [Type: `Real`]: The rate parameter (θ) of the distribution, or the standard deviation when `μσ=true`.
140+
- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution.
141+
- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution.
142+
143+
# Keyword Arguments
144+
- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the shape `α` and scale `θ` parameters.
145+
"""
146+
function Gamma::Real, σ::Real, lower_bound::Real, upper_bound::Real; μσ::Bool=false)
147+
# Create the base distribution, then truncate it
148+
dist = Gamma(μ, σ; μσ=μσ)
149+
return truncated(dist, lower_bound, upper_bound)
150+
end
151+
152+
153+
#==========================================================================================
154+
Simple Truncation Wrappers
155+
==========================================================================================#
156+
157+
"""
158+
$(SIGNATURES)
159+
Convenience wrapper for the truncated `Normal` distribution.
160+
161+
# Arguments
162+
- `μ` [Type: `Real`]: The mean of the distribution.
163+
- `σ` [Type: `Real`]: The standard deviation of the distribution.
164+
- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution.
165+
- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution.
166+
"""
167+
function Normal::Real, σ::Real, lower_bound::Real, upper_bound::Real)
168+
truncated(Turing.Normal(μ, σ), lower_bound, upper_bound)
169+
end
170+
171+
function Normal::Real, σ::Real)
172+
Turing.Normal(μ, σ)
173+
end
174+
175+
"""
176+
$(SIGNATURES)
177+
Convenience wrapper for the truncated `Cauchy` distribution.
178+
179+
# Arguments
180+
- `μ` [Type: `Real`]: The location parameter.
181+
- `σ` [Type: `Real`]: The scale parameter.
182+
- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution.
183+
- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution.
184+
"""
185+
function Cauchy::Real, σ::Real, lower_bound::Real, upper_bound::Real)
186+
truncated(Turing.Cauchy(μ, σ), lower_bound, upper_bound)
187+
end
188+
189+
function Cauchy::Real, σ::Real)
190+
Turing.Cauchy(μ, σ)
191+
end
192+
193+
194+
end # @stable
195+
196+
end # module

0 commit comments

Comments
 (0)