Skip to content

Consider refactoring components with strict methodological significance to pacta.portfolio.allocate #234

@jdhoffa

Description

@jdhoffa

PROBABLY DON'T DO THIS PRIOR TO SUCCESSFUL DELIVERY OF PACTA COP CH 2024

@cjyetman @AlexAxthelm if this issue should live in workflow.pacta (I sort of feel like it should? but leave it to you folks to decide)

Note: I am purposefully leaving out web_tool_script_3.R as that likely will end up corresponding to worfklow.pacta.report

Relates to RMI-PACTA/workflow.data.preparation#94

  • Refactor to pacta.portfolio.allocate
    # FIXME: this is necessary because pacta.portfolio.allocate::add_revenue_split()
    # was removed in #142, but later we realized that it had a sort of hidden
    # behavior where if there is no revenue data it maps the security_mapped_sector
    # column of the portfolio data to financial_sector, which is necessary later
    portfolio <-
    portfolio %>%
    mutate(
    has_revenue_data = FALSE,
    financial_sector = .data$security_mapped_sector
    )
  • Not sure if possible, but consider simplifying this. I'm thinking "one function, one dataset that is output by this script"
    port_raw_all_eq <- create_portfolio_subset(total_portfolio, "Equity")
    if (inherits(port_raw_all_eq, "data.frame") && nrow(port_raw_all_eq) > 0) {
    map_eq <- NA
    company_all_eq <- NA
    port_all_eq <- NA
    port_eq <- calculate_weights(port_raw_all_eq, "Equity")
    port_eq <- merge_abcd_from_db(
    portfolio = port_eq,
    portfolio_type= "Equity",
    db_dir = analysis_inputs_path,
    equity_market_list = equity_market_list,
    scenario_sources_list = scenario_sources_list,
    scenario_geographies_list = scenario_geographies_list,
    sector_list = sector_list,
    id_col = "id"
    )
    # Portfolio weight methodology
    port_pw_eq <- port_weight_allocation(port_eq)
    company_pw_eq <- aggregate_company(port_pw_eq)
    port_pw_eq <- aggregate_portfolio(company_pw_eq)
    # Ownership weight methodology
    port_own_eq <- ownership_allocation(port_eq)
    company_own_eq <- aggregate_company(port_own_eq)
    port_own_eq <- aggregate_portfolio(company_own_eq)
    # Create combined outputs
    company_all_eq <- bind_rows(company_pw_eq, company_own_eq)
    port_all_eq <- bind_rows(port_pw_eq, port_own_eq)
    if (has_map) {
    abcd_raw_eq <- get_abcd_raw("Equity")
    map_eq <- merge_in_geography(company_all_eq, abcd_raw_eq)
    rm(abcd_raw_eq)
    map_eq <- aggregate_map_data(map_eq)
    }
    # Technology Share Calculation
    port_all_eq <- calculate_technology_share(port_all_eq)
    company_all_eq <- calculate_technology_share(company_all_eq)
    # Scenario alignment calculations
    port_all_eq <- calculate_scenario_alignment(port_all_eq)
    company_all_eq <- calculate_scenario_alignment(company_all_eq)
    pf_file_results_path <- file.path(results_path, portfolio_name_ref_all)
    if (!dir.exists(pf_file_results_path)) {
    dir.create(pf_file_results_path)
    }
    if (data_check(company_all_eq)) {
    saveRDS(company_all_eq, file.path(pf_file_results_path, "Equity_results_company.rds"))
    }
    if (data_check(port_all_eq)) {
    saveRDS(port_all_eq, file.path(pf_file_results_path, "Equity_results_portfolio.rds"))
    }
    if (has_map) {
    if (data_check(map_eq)) {
    saveRDS(map_eq, file.path(pf_file_results_path, "Equity_results_map.rds"))
    }
    }
    rm(port_raw_all_eq)
    rm(port_eq)
    rm(port_pw_eq)
    rm(port_own_eq)
    rm(port_all_eq)
    rm(company_pw_eq)
    rm(company_own_eq)
    rm(company_all_eq)
    }
  • Same as above
    port_raw_all_cb <- create_portfolio_subset(total_portfolio, "Bonds")
    if (inherits(port_raw_all_cb, "data.frame") && nrow(port_raw_all_cb) > 0) {
    map_cb <- NA
    company_all_cb <- NA
    port_all_cb <- NA
    port_cb <- calculate_weights(port_raw_all_cb, "Bonds")
    port_cb <- merge_abcd_from_db(
    portfolio = port_cb,
    portfolio_type= "Bonds",
    db_dir = analysis_inputs_path,
    equity_market_list = equity_market_list,
    scenario_sources_list = scenario_sources_list,
    scenario_geographies_list = scenario_geographies_list,
    sector_list = sector_list,
    id_col = "credit_parent_ar_company_id"
    )
    # Portfolio weight methodology
    port_pw_cb <- port_weight_allocation(port_cb)
    company_pw_cb <- aggregate_company(port_pw_cb)
    port_pw_cb <- aggregate_portfolio(company_pw_cb)
    # Create combined outputs
    company_all_cb <- company_pw_cb
    port_all_cb <- port_pw_cb
    if (has_map) {
    if (data_check(company_all_cb)) {
    abcd_raw_cb <- get_abcd_raw("Bonds")
    map_cb <- merge_in_geography(company_all_cb, abcd_raw_cb)
    rm(abcd_raw_cb)
    map_cb <- aggregate_map_data(map_cb)
    }
    }
    # Technology Share Calculation
    if (nrow(port_all_cb) > 0) {
    port_all_cb <- calculate_technology_share(port_all_cb)
    }
    if (nrow(company_all_cb) > 0) {
    company_all_cb <- calculate_technology_share(company_all_cb)
    }
    # Scenario alignment calculations
    port_all_cb <- calculate_scenario_alignment(port_all_cb)
    company_all_cb <- calculate_scenario_alignment(company_all_cb)
    pf_file_results_path <- file.path(results_path, portfolio_name_ref_all)
    if (!dir.exists(pf_file_results_path)) {
    dir.create(pf_file_results_path)
    }
    if (data_check(company_all_cb)) {
    saveRDS(company_all_cb, file.path(pf_file_results_path, "Bonds_results_company.rds"))
    }
    if (data_check(port_all_cb)) {
    saveRDS(port_all_cb, file.path(pf_file_results_path, "Bonds_results_portfolio.rds"))
    }
    if (has_map) {
    if (data_check(map_cb)) {
    saveRDS(map_cb, file.path(pf_file_results_path, "Bonds_results_map.rds"))
    }
    }

AB#10862

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions