Skip to content

Commit

Permalink
Merge pull request #17 from RMI-PACTA/implement-prep-stats
Browse files Browse the repository at this point in the history
Add data prep function for exposure statistics
  • Loading branch information
MonikaFu authored Nov 13, 2024
2 parents 6828ae1 + 1ac0f09 commit e2f02a4
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions main.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,72 @@ if (nrow(techexposure_data) > 0) {
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
}


# input and output directories -------------------------------------------------

Expand Down Expand Up @@ -434,3 +500,13 @@ pacta.portfolio.report:::prep_emissions_trajectory(
) %>%
pacta.portfolio.report:::translate_df_contents("data_emissions", dictionary) %>%
jsonlite::write_json(path = file.path(output_dir, "data_emissions.json"))

# data_exposure_stats.json

prep_exposure_stats(
audit_file = audit_file,
investor_name = investor_name,
portfolio_name = portfolio_name,
pacta_sectors = pacta_sectors
) %>%
jsonlite::write_json(path = file.path(output_dir, "data_exposure_stats.json"))

0 comments on commit e2f02a4

Please sign in to comment.