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

feat: build (internal) package infrastructure, and export these two functions #19

Open
jdhoffa opened this issue Nov 20, 2024 · 5 comments · May be fixed by #53
Open

feat: build (internal) package infrastructure, and export these two functions #19

jdhoffa opened this issue Nov 20, 2024 · 5 comments · May be fixed by #53
Labels
enhancement New feature or request

Comments

@jdhoffa
Copy link
Member

jdhoffa commented Nov 20, 2024

prep_techmix_sector <-
function(equity_results_portfolio,
bonds_results_portfolio,
indices_equity_results_portfolio,
indices_bonds_results_portfolio,
peers_equity_results_portfolio,
peers_bonds_results_portfolio,
investor_name,
portfolio_name,
start_year,
year_span,
peer_group,
green_techs,
all_tech_levels
) {
portfolio <-
list(`Listed Equity` = equity_results_portfolio,
`Corporate Bonds` = bonds_results_portfolio) %>%
bind_rows(.id = "asset_class") %>%
filter(.data$investor_name == .env$investor_name,
.data$portfolio_name == .env$portfolio_name) %>%
filter(!is.na(.data$ald_sector))
asset_classes <-
portfolio %>%
pull("asset_class") %>%
unique()
equity_sectors <-
portfolio %>%
filter(.data$asset_class == "Listed Equity") %>%
filter(.data$allocation == "portfolio_weight") %>%
pull("ald_sector") %>%
unique()
bonds_sectors <-
portfolio %>%
filter(.data$asset_class == "Corporate Bonds") %>%
pull("ald_sector") %>%
unique()
indices <-
list(`Listed Equity` = indices_equity_results_portfolio,
`Corporate Bonds` = indices_bonds_results_portfolio) %>%
bind_rows(.id = "asset_class") %>%
filter(.data$asset_class %in% .env$asset_classes) %>%
filter(.data$asset_class == "Listed Equity" & .data$ald_sector %in% .env$equity_sectors |
.data$asset_class == "Corporate Bonds" & .data$ald_sector %in% .env$bonds_sectors)
peers <-
list(`Listed Equity` = peers_equity_results_portfolio,
`Corporate Bonds` = peers_bonds_results_portfolio) %>%
bind_rows(.id = "asset_class") %>%
filter(.data$asset_class %in% .env$asset_classes) %>%
filter(.data$asset_class == "Listed Equity" & .data$ald_sector %in% .env$equity_sectors |
.data$asset_class == "Corporate Bonds" & .data$ald_sector %in% .env$bonds_sectors) %>%
filter(.data$investor_name == .env$peer_group)
techexposure_data <-
bind_rows(portfolio, peers, indices) %>%
filter(.data$allocation == "portfolio_weight") %>%
filter(.data$scenario_geography == "Global") %>%
filter(.data$year %in% c(.env$start_year, .env$start_year + .env$year_span))
if (nrow(techexposure_data) > 0) {
techexposure_data <-
techexposure_data %>%
mutate(green = .data$technology %in% .env$green_techs) %>%
group_by(.data$asset_class, .data$equity_market, .data$portfolio_name,
.data$ald_sector, .data$scenario, .data$year) %>%
mutate(plan_alloc_wt_sec_prod = sum(.data$plan_alloc_wt_tech_prod),
scen_alloc_wt_sec_prod = sum(.data$scen_alloc_wt_tech_prod)) %>%
mutate(production_plan = if_else(.data$plan_alloc_wt_tech_prod > 0, .data$plan_alloc_wt_tech_prod / .data$plan_alloc_wt_sec_prod, 0),
scenario_plan = if_else(.data$scen_alloc_wt_tech_prod > 0, .data$scen_alloc_wt_tech_prod / .data$scen_alloc_wt_sec_prod, 0)) %>%
group_by(.data$asset_class, .data$equity_market, .data$portfolio_name,
.data$ald_sector, .data$scenario, .data$year, .data$green) %>%
mutate(green_sum_prod = sum(.data$production_plan),
green_sum_scenario = sum(.data$scenario_plan)) %>%
ungroup() %>%
select("asset_class", "investor_name", "portfolio_name", "scenario_source",
"scenario", "allocation", "equity_market", "year", "ald_sector",
"technology", "production_plan", "scenario_plan", "green",
"green_sum_prod", "green_sum_scenario") %>%
pivot_longer(
cols = -c("asset_class", "investor_name", "portfolio_name", "scenario_source",
"scenario", "allocation", "equity_market", "year", "ald_sector",
"technology", "green","green_sum_prod", "green_sum_scenario"),
names_to = "val_type", values_to = "value") %>%
mutate(
green_sum = if_else(.data$val_type == "production_plan", .data$green_sum_prod, .data$green_sum_scenario)
) %>%
select(-c("green_sum_prod", "green_sum_scenario")) %>%
ungroup() %>%
mutate(this_portfolio = .data$portfolio_name == .env$portfolio_name,
val_type = if_else(.data$this_portfolio == TRUE, paste0(.data$val_type, "_portfolio"), paste0(.data$val_type, "_benchmark"))) %>%
mutate(equity_market = case_when(
.data$equity_market == "GlobalMarket" ~ "Global Market",
.data$equity_market == "DevelopedMarket" ~ "Developed Market",
.data$equity_market == "EmergingMarket" ~ "Emerging Market",
TRUE ~ .data$equity_market)
) %>%
# no need for showing scenario mix for the benchmark
filter(
.data$val_type != "scenario_plan_benchmark"
) %>%
mutate(val_type = case_when(
.data$val_type == "production_plan_portfolio" ~ "Portfolio",
.data$val_type == "scenario_plan_portfolio" ~ "Scenario",
.data$val_type == "production_plan_benchmark" ~ "Benchmark",
TRUE ~ .data$val_type)
) %>%
arrange(
.data$asset_class,
factor(.data$equity_market, levels = c("Global Market", "Developed Market", "Emerging Market")),
desc(.data$this_portfolio),
factor(.data$val_type, levels = c("Portfolio", "Scenario", "Benchmark")),
.data$portfolio_name,
factor(.data$technology, levels = .env$all_tech_levels)
) %>%
select("asset_class", "equity_market", "portfolio_name", "scenario", "scenario_source",
"this_portfolio", "val_type", "ald_sector", "technology", "value",
"green", "green_sum", "year") %>%
filter(
!(.data$year == .env$start_year & .data$val_type == "Scenario")
)
}
techexposure_data
}
prep_exposure_stats <- function(audit_file, investor_name, portfolio_name, pacta_sectors) {
pacta_asset_classes <- c("Bonds", "Equity")
audit_table <- pacta.portfolio.report:::prep_audit_table(
audit_file,
investor_name = investor_name,
portfolio_name = portfolio_name,
currency_exchange_value = currency_exchange_value
)
exposure_stats <- audit_file %>%
filter(
.data$investor_name == .env$investor_name &
.data$portfolio_name == .env$portfolio_name) %>%
filter(.data$asset_type %in% pacta_asset_classes) %>%
filter(.data$valid_input == TRUE) %>%
mutate(across(c("bics_sector", "financial_sector"), as.character)) %>%
mutate(
sector =
if_else(!.data$financial_sector %in% .env$pacta_sectors,
"Other",
.data$financial_sector
)
) %>%
summarise(
value = sum(.data$value_usd, na.rm = TRUE) / .env$currency_exchange_value,
.by = c("asset_type", "sector")
) %>%
mutate(
perc_asset_val_sector = .data$value / sum(.data$value, na.rm = TRUE),
.by = c("asset_type")
) %>%
inner_join(audit_table, by = join_by(asset_type == asset_type_analysis)) %>%
select("asset_type", "percentage_value_invested", "sector", "perc_asset_val_sector")
asset_classes_in_portfolio <- intersect(pacta_asset_classes, unique(exposure_stats$asset_type))
all_stats_with_zero_sector_exposure <- expand.grid(
asset_type = asset_classes_in_portfolio,
sector = pacta_sectors,
val_sector = 0
) %>% inner_join(
distinct(select(exposure_stats, c("asset_type", "percentage_value_invested"))),
by = join_by(asset_type)
)
exposure_stats_all <- all_stats_with_zero_sector_exposure %>%
left_join(exposure_stats, by = join_by(asset_type, sector, percentage_value_invested)) %>%
mutate(
perc_asset_val_sector = if_else(
is.na(.data$perc_asset_val_sector),
.data$val_sector,
.data$perc_asset_val_sector
)
) %>%
mutate(
asset_type = case_when(
.data$asset_type == "Bonds" ~ "Corporate Bonds",
.data$asset_type == "Equity" ~ "Listed Equity"
)
) %>%
select("asset_type", "percentage_value_invested", "sector", "perc_asset_val_sector")
exposure_stats_all
}

@jdhoffa jdhoffa added the enhancement New feature or request label Nov 20, 2024
@AlexAxthelm
Copy link
Contributor

@jdhoffa: the functions are prep_techmix_sector() and prep_exposure_stats()?

is there a reverse dependency I need to track?

@jdhoffa
Copy link
Member Author

jdhoffa commented Nov 22, 2024

I suppose there are two main things to be aware of. Number 1 is in-scope for this current issue. Number 2 is something to be aware of, but probably a can we can kick down the road for now.

  1. In main.R there are 5 functions that we should package and export:
  • prep_techmix_sector
  • prep_exposure_stats
  • prep_company_bubble
  • prep_key_bars_company
  • prep_key_bars_portfolio
  1. We are utilizing un-exported function from various PACTA packages, e.g. pacta.portfolio.report

audit_file %>%
pacta.portfolio.report:::prep_exposure_pie(
asset_type = "Equity",
investor_name = investor_name,
portfolio_name = portfolio_name,
pacta_sectors = pacta_sectors,
currency_exchange_value = currency_exchange_value
) %>%
pacta.portfolio.report:::translate_df_contents("data_value_pie_equity", dictionary) %>%
jsonlite::write_json(path = file.path(output_dir, "data_value_pie_equity.json"))

@jdhoffa
Copy link
Member Author

jdhoffa commented Nov 22, 2024

is there a reverse dependency I need to track?

No reverse dependency in terms of function names/ arguments etc.! However of course the final datasets output are fixed, and have an expected format so that must be preserved.

@cjyetman
Copy link
Member

cjyetman commented Dec 4, 2024

I think this is resolved by #30

@jdhoffa jdhoffa closed this as completed Dec 4, 2024
@AlexAxthelm
Copy link
Contributor

Not resolved, as no functions were exported in that PR.

@AlexAxthelm AlexAxthelm reopened this Dec 9, 2024
@AlexAxthelm AlexAxthelm linked a pull request Dec 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants