Skip to content

Commit

Permalink
adding different ptf
Browse files Browse the repository at this point in the history
  • Loading branch information
danielecampus committed Jan 9, 2025
1 parent bb604f3 commit a79b855
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 13 deletions.
Binary file added input/data_prices.parquet
Binary file not shown.
Binary file added input/data_returns.parquet
Binary file not shown.
Binary file added input/data_ticker_df.parquet
Binary file not shown.
Binary file added input/data_timeseries.xlsx
Binary file not shown.
Binary file modified input/multiassets.xlsx
Binary file not shown.
13 changes: 10 additions & 3 deletions multiasset_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ input_path <- "C:/Users/danie/OneDrive/GitHub/myportfolio/input/"
output_path <- "C:/Users/danie/OneDrive/GitHub/myportfolio/output/"

# open inputs
multiasset_ticker_df <- read_parquet(paste0(input_path, "multiasset_ticker_df.parquet"))
multiasset_sheet_prices <- read_parquet(paste0(input_path, "multiasset_sheet_prices.parquet"))
multiasset_sheet_returns <- read_parquet(paste0(input_path, "multiasset_sheet_returns.parquet"))
# anna data
anna_assets <- c("MSCI World", "US Quality", "World Low Quality", "EU Gov bonds 7-10y", "EU Inflation-Linked
", "EU Inflation-Linked", "EU Overnight", "US Short Treasury", "ETC Gold")
anna_ticker_df <- read_parquet(paste0(input_path, "data_ticker_df.parquet")) %>%
filter(Index == anna_assets)
anna_sheet_prices <- read_parquet(paste0(input_path, "data_returns.parquet"))
anna_sheet_returns <- read_parquet(paste0(input_path, "data_prices.parquet"))

# ANNA


# analysis
ret_pure <- multiasset_sheet_returns %>% select(-Dates) %>% as.data.frame()
Expand Down
8 changes: 4 additions & 4 deletions multiasset_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ticker_df <- read_xlsx(paste0(input_path,"multiassets.xlsx")
, col_types = "text")
ticker_assets <- ticker_df$Ticker
index_assets <- ticker_df$Index
write_parquet(ticker_df, paste0(input_path, "multiasset_ticker_df.parquet"))
write_parquet(ticker_df, paste0(input_path, "data_ticker_df.parquet"))

# download prices from Yahoo Finance
tsPrices <- map(ticker_assets, get_prices)
Expand All @@ -23,11 +23,11 @@ sheet_prices <- final_prices(price_matrix)

# compute returns
sheet_returns <- get_returns(sheet_prices, tsPrices)
write_parquet(sheet_returns, paste0(input_path, "multiasset_sheet_returns.parquet"))
write_parquet(sheet_returns, paste0(input_path, "data_returns.parquet"))

# arrange prices otherwise I couldn't compute returns
sheet_prices <- sheet_prices %>% arrange(desc(Dates))
write_parquet(sheet_prices, paste0(input_path, "multiasset_sheet_prices.parquet"))
write_parquet(sheet_prices, paste0(input_path, "data_prices.parquet"))

# save in output path
wb <- createWorkbook()
Expand All @@ -38,4 +38,4 @@ writeData(wb, "Prices", sheet_prices)
addWorksheet(wb, "Returns")
writeData(wb, "Returns", sheet_returns)

saveWorkbook(wb, paste0(input_path, "multiassets_ret.xlsx"), overwrite = T)
saveWorkbook(wb, paste0(input_path, "data_timeseries.xlsx"), overwrite = T)
17 changes: 11 additions & 6 deletions multiasset_montecarlo_simulation.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
setwd("C:/Users/danie/OneDrive/GitHub/myportfolio")

source("library.R")
source("functions.R")

input_path <- "C:/Users/danie/OneDrive/GitHub/myportfolio/input/"
output_path <- "C:/Users/danie/OneDrive/GitHub/myportfolio/output/"

Expand All @@ -13,12 +18,12 @@ corr_matrix <- cor(ret_pure)
avg_returns <- ret_pure %>% summarise(across(everything(), mean))

# define quotes of each asset
multiasset_quotes <- c(0.15,0.15,0.10, 0.22,0.06,0.15, 0.05, 0.12)
sum(multiasset_quotes) # = 1
anna_quotes <- c(0.30,0.15,0.10, 0.15,0.05,0.1, 0.05, 0.1)
sum(anna_quotes) # = 1

# monte carlo inputs
n_sim <- 10000
n_period <- 24
n_period <- 12
initial_value <- 50000
avg_returns_vector <- avg_returns %>% as.numeric() %>% as.vector()

Expand All @@ -32,7 +37,7 @@ simulated_returns <- MASS::mvrnorm(

simulated_returns <- array(simulated_returns, dim = c(n_sim, n_period, length(avg_returns_vector)))

portfolio_returns <-apply(simulated_returns, c(1,2), function(x) sum(x*multiasset_quotes))
portfolio_returns <-apply(simulated_returns, c(1,2), function(x) sum(x*anna_quotes))

# portfolio value
portfolio_values <- matrix(NA, nrow = n_sim, ncol = n_period)
Expand Down Expand Up @@ -72,7 +77,7 @@ cat("ES:", round(ES_percent, 2), "%\n")

# weighted returns
ret_pure <- multiasset_sheet_returns %>% arrange(Dates) %>% select(-Dates) %>% as.matrix()
historical_returns <- ret_pure %*% multiasset_quotes
historical_returns <- ret_pure %*% anna_quotes

historical_data <- data.frame(Dates = multiasset_sheet_returns$Dates) %>%
arrange(Dates) %>%
Expand Down Expand Up @@ -110,7 +115,7 @@ write_parquet(multiasset_combined_data, paste0(output_path, "multiasset_mc_forec
exp_ret <- max(multiasset_forecast_summary$Mean)/last(historical_data$Cumulative_Value)-1

# plot
png(paste0(output_path, "gfx/multiasset_forecast.png"))
png(paste0(output_path, "gfx/anna_12m_forecast.png"))
ggplot(multiasset_combined_data, aes(x = Dates)) +
geom_line(aes(y = Mean), color = "blue", size = 1) +
geom_ribbon(data = multiasset_forecast_summary, aes(ymin = P5, ymax = P95), fill = "blue", alpha = 0.2) +
Expand Down
Binary file added output/gfx/anna_12m_forecast.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified output/multiasset_mc_forecast.parquet
Binary file not shown.
Binary file added xlsx/asset_allocation_anna.xlsx
Binary file not shown.
Binary file added xlsx/asset_allocation_chiara.xlsx
Binary file not shown.

0 comments on commit a79b855

Please sign in to comment.