-
Notifications
You must be signed in to change notification settings - Fork 17
Add grouped covariance input to get_statistics and optimize determine_efficient_order #181
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
Changes from 9 commits
03ccde1
effc713
6f3e155
ae7e893
db09540
6ad2404
2ff0804
e949bfd
18d77b5
8bc6672
093b24d
a13fd2d
291bf89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3207,7 +3207,7 @@ If occasionally binding constraints are present in the model, they are not taken | |||||||||||||||||||||||||||||||||||||||||
| - `mean` [Default: `Symbol[]`, Type: `Union{Symbol_input,String_input}`]: variables for which to show the mean of selected variables (the mean for the linearised solution is the NSSS). Inputs can be a variable name passed on as either a `Symbol` or `String` (e.g. `:y` or \"y\"), or `Tuple`, `Matrix` or `Vector` of `String` or `Symbol`. Any variables not part of the model will trigger a warning. `:all_excluding_auxiliary_and_obc` contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). `:all_excluding_obc` contains all shocks less those related to auxiliary variables. `:all` will contain all variables. | ||||||||||||||||||||||||||||||||||||||||||
| - `standard_deviation` [Default: `Symbol[]`, Type: `Union{Symbol_input,String_input}`]: variables for which to show the standard deviation of selected variables. Inputs can be a variable name passed on as either a `Symbol` or `String` (e.g. `:y` or \"y\"), or `Tuple`, `Matrix` or `Vector` of `String` or `Symbol`. Any variables not part of the model will trigger a warning. `:all_excluding_auxiliary_and_obc` contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). `:all_excluding_obc` contains all shocks less those related to auxiliary variables. `:all` will contain all variables. | ||||||||||||||||||||||||||||||||||||||||||
| - `variance` [Default: `Symbol[]`, Type: `Union{Symbol_input,String_input}`]: variables for which to show the variance of selected variables. Inputs can be a variable name passed on as either a `Symbol` or `String` (e.g. `:y` or \"y\"), or `Tuple`, `Matrix` or `Vector` of `String` or `Symbol`. Any variables not part of the model will trigger a warning. `:all_excluding_auxiliary_and_obc` contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). `:all_excluding_obc` contains all shocks less those related to auxiliary variables. `:all` will contain all variables. | ||||||||||||||||||||||||||||||||||||||||||
| - `covariance` [Default: `Symbol[]`, Type: `Union{Symbol_input,String_input}`]: variables for which to show the covariance of selected variables. Inputs can be a variable name passed on as either a `Symbol` or `String` (e.g. `:y` or \"y\"), or `Tuple`, `Matrix` or `Vector` of `String` or `Symbol`. Any variables not part of the model will trigger a warning. `:all_excluding_auxiliary_and_obc` contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). `:all_excluding_obc` contains all shocks less those related to auxiliary variables. `:all` will contain all variables. | ||||||||||||||||||||||||||||||||||||||||||
| - `covariance` [Default: `Symbol[]`, Type: `Union{Symbol_input,String_input}`]: variables for which to show the covariance of selected variables. Inputs can be a variable name passed on as either a `Symbol` or `String` (e.g. `:y` or \"y\"), or `Tuple`, `Matrix` or `Vector` of `String` or `Symbol`. For grouped covariance computation, pass a `Vector` of `Vector`s (e.g. `[[:y, :c], [:k, :i]]`) to compute covariances only within each group, returning a single covariance matrix where cross-group covariances are set to zero. This allows more granular control over which covariances to compute. Any variables not part of the model will trigger a warning. `:all_excluding_auxiliary_and_obc` contains all variables less those related to auxiliary variables and related to occasionally binding constraints (obc). `:all_excluding_obc` contains all variables less those related to occasionally binding constraints. `:all` will contain all variables. | ||||||||||||||||||||||||||||||||||||||||||
| - `autocorrelation` [Default: `Symbol[]`, Type: `Union{Symbol_input,String_input}`]: variables for which to show the autocorrelation of selected variables. Inputs can be a variable name passed on as either a `Symbol` or `String` (e.g. `:y` or \"y\"), or `Tuple`, `Matrix` or `Vector` of `String` or `Symbol`. Any variables not part of the model will trigger a warning. `:all_excluding_auxiliary_and_obc` contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). `:all_excluding_obc` contains all shocks less those related to auxiliary variables. `:all` will contain all variables. | ||||||||||||||||||||||||||||||||||||||||||
| - `autocorrelation_periods` [Default: `1:5`, Type = `UnitRange{Int}`]: periods for which to return the autocorrelation of selected variables | ||||||||||||||||||||||||||||||||||||||||||
| - $ALGORITHM® | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -3243,6 +3243,12 @@ get_statistics(RBC, RBC.parameter_values, parameters = RBC.parameters, standard_ | |||||||||||||||||||||||||||||||||||||||||
| # output | ||||||||||||||||||||||||||||||||||||||||||
| Dict{Symbol, AbstractArray{Float64}} with 1 entry: | ||||||||||||||||||||||||||||||||||||||||||
| :standard_deviation => [0.0266642, 0.264677, 0.0739325, 0.0102062] | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # For grouped covariance (computing covariances only within specified groups): | ||||||||||||||||||||||||||||||||||||||||||
| get_statistics(RBC, RBC.parameter_values, covariance = [[:c, :k], [:q]]) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| # output | ||||||||||||||||||||||||||||||||||||||||||
| Dict{Symbol, AbstractArray{Float64}} with 1 entry: | ||||||||||||||||||||||||||||||||||||||||||
| :covariance => [...3x3 matrix with c-k covariances filled, q variance filled, and cross-group elements set to zero...] | ||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||
| function get_statistics(𝓂, | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -3283,6 +3289,10 @@ function get_statistics(𝓂, | |||||||||||||||||||||||||||||||||||||||||
| var_var_idx = @ignore_derivatives parse_variables_input_to_index(variance, 𝓂.timings) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| covar_var_idx = @ignore_derivatives parse_variables_input_to_index(covariance, 𝓂.timings) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Check if covariance is grouped and parse groups | ||||||||||||||||||||||||||||||||||||||||||
| is_grouped_covar = @ignore_derivatives is_grouped_covariance_input(covariance) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| covar_groups = @ignore_derivatives is_grouped_covar ? parse_covariance_groups(covariance, 𝓂.timings) : nothing | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| autocorr_var_idx = @ignore_derivatives parse_variables_input_to_index(autocorrelation, 𝓂.timings) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -3405,8 +3415,38 @@ function get_statistics(𝓂, | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # droptol!(covar_dcmp_sp,eps(Float64)) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # push!(ret,covar_dcmp_sp[covar_var_idx,covar_var_idx]) | ||||||||||||||||||||||||||||||||||||||||||
| ret[:covariance] = solved ? covar_dcmp_sp[covar_var_idx,covar_var_idx] : fill(Inf * sum(abs2,parameter_values),isnothing(covar_var_idx) ? 0 : length(covar_var_idx), isnothing(covar_var_idx) ? 0 : length(covar_var_idx)) | ||||||||||||||||||||||||||||||||||||||||||
| if is_grouped_covar | ||||||||||||||||||||||||||||||||||||||||||
| # Extract only the specified covariance groups (block diagonal structure) | ||||||||||||||||||||||||||||||||||||||||||
| # Return a single matrix with zeros for non-computed covariances | ||||||||||||||||||||||||||||||||||||||||||
| if solved | ||||||||||||||||||||||||||||||||||||||||||
| # Initialize matrix with zeros | ||||||||||||||||||||||||||||||||||||||||||
| covar_result = zeros(T, length(covar_var_idx), length(covar_var_idx)) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Fill in only the specified groups | ||||||||||||||||||||||||||||||||||||||||||
| for group in covar_groups | ||||||||||||||||||||||||||||||||||||||||||
| for (i_idx, i) in enumerate(group) | ||||||||||||||||||||||||||||||||||||||||||
| for (j_idx, j) in enumerate(group) | ||||||||||||||||||||||||||||||||||||||||||
| # Find position in covar_var_idx | ||||||||||||||||||||||||||||||||||||||||||
| i_pos = findfirst(==(i), covar_var_idx) | ||||||||||||||||||||||||||||||||||||||||||
| j_pos = findfirst(==(j), covar_var_idx) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3424
to
+3430
|
||||||||||||||||||||||||||||||||||||||||||
| # Fill in only the specified groups | |
| for group in covar_groups | |
| for (i_idx, i) in enumerate(group) | |
| for (j_idx, j) in enumerate(group) | |
| # Find position in covar_var_idx | |
| i_pos = findfirst(==(i), covar_var_idx) | |
| j_pos = findfirst(==(j), covar_var_idx) | |
| # Precompute lookup dictionary for covar_var_idx | |
| covar_var_idx_lookup = Dict{eltype(covar_var_idx), Int}() | |
| for (idx, val) in enumerate(covar_var_idx) | |
| covar_var_idx_lookup[val] = idx | |
| end | |
| # Fill in only the specified groups | |
| for group in covar_groups | |
| for (i_idx, i) in enumerate(group) | |
| for (j_idx, j) in enumerate(group) | |
| # Find position in covar_var_idx using lookup | |
| i_pos = get(covar_var_idx_lookup, i, nothing) | |
| j_pos = get(covar_var_idx_lookup, j, nothing) |
Copilot
AI
Nov 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the model is not solved, the unsolved case fills the entire matrix with Inf values, but the comment suggests it should have 'zeros elsewhere'. This is inconsistent with the solved case which has zeros for cross-group covariances. The unsolved matrix should match the structure of the solved case with zeros for non-diagonal elements or cross-group elements.
| covar_result = fill(Inf * sum(abs2,parameter_values), length(covar_var_idx), length(covar_var_idx)) | |
| covar_result = zeros(T, length(covar_var_idx), length(covar_var_idx)) | |
| for i in 1:length(covar_var_idx) | |
| covar_result[i, i] = Inf * sum(abs2, parameter_values) | |
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot also allow for vector of tuples or tuples of tuples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added support for
Vector{Tuple{Symbol,Vararg{Symbol}}}andTuple{Tuple{Symbol,Vararg{Symbol}},Vararg{Tuple{Symbol,Vararg{Symbol}}}}(and their String equivalents). UpdatedSymbol_inputandString_inputtype definitions, and modifiedis_grouped_covariance_input,parse_covariance_groups, andparse_variables_input_to_indexto handle these formats. Commit: a13fd2d