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

Sectoral Certainty Equivalent Calculations Undefined (Appear as 0.0 or -0.0) #68

Merged
merged 9 commits into from
Oct 9, 2024

Conversation

lrennels
Copy link
Collaborator

@lrennels lrennels commented Sep 19, 2024

Handles #67

The norm_cpc_values_ce in the payload had elements for each s in sectors, which were initialized to undef, never filled in after that, and then accessed when computing certainty equivalent SCCs. That meant sectoral certainty equivalent SCCs often appeared as 0.0 or -0.0 because of dividing by a very large or very small Float (the initialization number).

Practically we used the full NamedTuple key for lookup ease, but there is no difference between sectors for norm_cpc_values_ce as we just use the total sector for each one to look up the consumption per capita. The only unique characteristics are actually (1) region (2) ew and (3) ew_norm_region.

I think we should probably allocate only

norm_cpc_values_ce = certainty_equivalent ? Dict((region=r, sector=s, ew=dr.ew, ew_norm_region=dr.ew_norm_region) => Vector{Float64}(undef, n) for dr in discount_rates, r in regions, s in [:total]) : nothing

and use only those NamedTuple keys for saving the data needed for equity weighting.

To do:

  • Main fixes to scc.jl listed above
  • update the How-to-Guide scc.jl file to match
  • double check the documentation for (1) equity weighting and (2) general comments in scc.jl and related
  • decide on skipmissing -- throw a warning for visibility
  • add some tests

@lrennels lrennels changed the title Fix Bug in Sectoral Certainty Equivalent Calculations Sectoral Certainty Equivalent Calculations Undefined (Appear as 0.0 or -0.0) Sep 19, 2024
@lrennels lrennels marked this pull request as ready for review September 20, 2024 00:56
md_values = save_md ? Dict((region=r, sector=s) => Array{Float64}(undef, n, length(_damages_years)) for r in regions, s in sectors) : nothing
cpc_values = save_cpc ? Dict((region=r, sector=s) => Array{Float64}(undef, n, length(_damages_years)) for r in [:globe], s in [:total]) : nothing # just global and total for now

norm_cpc_values_ce = certainty_equivalent ? Dict((region=r, sector=s, ew=dr.ew, ew_norm_region=dr.ew_norm_region) => Vector{Float64}(undef, n) for dr in discount_rates, r in regions, s in [:total]) : nothing
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove dr_label, prtp, and eta from the NamedTuples used as keys for the norm_cpc_values_ce Dictionary element.

@@ -518,7 +534,7 @@ function post_trial_func(mcs::SimulationInstance, trialnum::Int, ntimesteps::Int
intermediate_ce_scc = sum(df_ce .* total_mds[year_index:last_year_index])
intermediate_ce_scc_values[(region=:globe, sector=:total, dr_label=dr.label, prtp=dr.prtp, eta=dr.eta, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = intermediate_ce_scc

norm_cpc_values_ce[(region=:globe, sector=:total, dr_label=dr.label, prtp=dr.prtp, eta=dr.eta, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = cpc[year_index]
norm_cpc_values_ce[(region=:globe, sector=:total, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = cpc[year_index]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove fields from the NamedTuple used as the key for norm_cpc_values_ce Dictionary, we don't use these anymore as they create redundancy

@@ -676,7 +704,7 @@ function post_trial_func(mcs::SimulationInstance, trialnum::Int, ntimesteps::Int

if options.certainty_equivalent
intermediate_ce_scc_values[(region=:globe, sector=:total, dr_label=dr.label, prtp=dr.prtp, eta=dr.eta, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = total_utils
norm_cpc_values_ce[(region=:globe, sector=:total, dr_label=dr.label, prtp=dr.prtp, eta=dr.eta, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = base[pc_gdp_component_name, :pc_gdp][year_index,normalization_region_index]
norm_cpc_values_ce[(region=:globe, sector=:total, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = base[pc_gdp_component_name, :pc_gdp][year_index,normalization_region_index]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove fields from the NamedTuple used as the key for norm_cpc_values_ce Dictionary, we don't use these anymore as they create redundancy

src/scc.jl Outdated
@@ -795,7 +830,7 @@ function post_trial_func(mcs::SimulationInstance, trialnum::Int, ntimesteps::Int

if options.certainty_equivalent
intermediate_ce_scc_values[(region=:globe, sector=:total, dr_label=dr.label, prtp=dr.prtp, eta=dr.eta, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = total_utils
norm_cpc_values_ce[(region=:globe, sector=:total, dr_label=dr.label, prtp=dr.prtp, eta=dr.eta, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = base[pc_gdp_component_name, :pc_gdp][year_index,normalization_region_index]
norm_cpc_values_ce[(region=:globe, sector=:total, dr_label=dr.label, prtp=dr.prtp, eta=dr.eta, ew=dr.ew, ew_norm_region=dr.ew_norm_region)][trialnum] = base[net_cpc_component_name, :net_cpc][year_index,normalization_region_index]
Copy link
Collaborator Author

@lrennels lrennels Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove fields from the NamedTuple used as the key for norm_cpc_values_ce Dictionary, we don't use these anymore as they create redundancy AND bug fix to indexing with net_cpc instead of gdp

cpc_in_year_of_emission = norm_cpc_values_ce[k]

# new key using all the same fields except making sector total
k_sector_total = (region=k.region, sector=:total, ew=k.ew, ew_norm_region=k.ew_norm_region)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create the slimmed-down key we will use for norm_cpc_values_ce Dictionary

@lrennels
Copy link
Collaborator Author

Ok @davidanthoff ready for review, I added in some testing and also fixes to comments and the README, but the only actual functional changes happen in scc.jl in the comments I made above. The rest is either documentation or testing code.

@lrennels lrennels merged commit 97085fb into main Oct 9, 2024
15 checks passed
@lrennels lrennels deleted the ce-fix branch October 9, 2024 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant