RPubs will retire in June 2027. Your existing documents will stay accessible through December 31, 2031
and Connect Cloud is the recommended home for new publishing. Read the blog post
gravatar

lts1125

Lina Elsehelly

Recently Published

HTML
port_2_max_ret <- portfolio.spec(assets = colnames(returns_date_droped_dividened2)) # Add objectives - here we minimize risk (VAR) port_2_max_ret <- add.objective(portfolio = port_2_max_ret, type = "risk", name = "CVaR")#added #port_2_max_ret <- add.objective(portfolio=port_2_max_ret, type = "risk_adjusted_return", name = "SharpeRatio", risk_free_rate = risk_free_rate) # Add constraints - fully invested portfolio with no short sales port_2_max_ret <- add.constraint(portfolio = port_2_max_ret, type = "long_only") port_2_max_ret <- add.constraint(portfolio = port_2_max_ret, type = "full_investment") # Optimize the portfolio to minimize risk optimal_portfolio_2 <- optimize.portfolio(R = returns_date_droped_dividened2, portfolio = port_2_max_ret, optimize_method = "ROI") optimal_weights <- extractWeights(optimal_portfolio_2) asset_names <- names(optimal_weights) weights_df <- data.frame(Asset = asset_names, Weight = optimal_weights) plot_ly(data = weights_df, x = ~Asset, y = ~Weight, type = 'bar') %>% layout(title = "Interactive Portfolio Weights", xaxis = list(title = "Asset"), yaxis = list(title = "Weight"))
HTML
initial_portfolio <- portfolio.spec(assets = colnames(returns_date_droped_dividened2)) # Add objectives - here we minimize risk (VAR) initial_portfolio <- add.objective(portfolio = initial_portfolio, type = "risk", name = "VaR") initial_portfolio <- add.objective(portfolio = initial_portfolio, type = "return", name = "mean") initial_portfolio <- add.objective(portfolio = initial_portfolio, type = "risk-adjusted", name = "SortinoRatio") # Add constraints - fully invested portfolio with no short sales initial_portfolio <- add.constraint(portfolio = initial_portfolio, type = "long_only") initial_portfolio <- add.constraint(portfolio = initial_portfolio, type = "full_investment") initial_portfolio <- add.constraint(portfolio = initial_portfolio, type = "box", min = 0, max = 1) # Optimize the portfolio to minimize risk optimal_portfolio <- optimize.portfolio(R = returns_date_droped_dividened2, portfolio = initial_portfolio, optimize_method = "DEoptim") # View the optimal weights optimal_weights <- extractWeights(optimal_portfolio) asset_names <- names(optimal_weights) weights_df <- data.frame(Asset = asset_names, Weight = optimal_weights) plot_ly(data = weights_df, x = ~Asset, y = ~Weight, type = 'bar') %>% layout(title = "Interactive Portfolio Weights", xaxis = list(title = "Asset"), yaxis = list(title = "Weight"))
HTML
port_2_max_ret <- portfolio.spec(assets = colnames(returns_date_droped_dividened2)) # Add objectives - here we minimize risk (VAR) port_2_max_ret <- add.objective(portfolio = port_2_max_ret, type = "risk", name = "StdDev")#added #port_2_max_ret <- add.objective(portfolio = port_2_max_ret, type = "return", name = "mean") # Add constraints - fully invested portfolio with no short sales port_2_max_ret <- add.constraint(portfolio = port_2_max_ret, type = "long_only") port_2_max_ret <- add.constraint(portfolio = port_2_max_ret, type = "full_investment") port_2_max_ret <- add.constraint(portfolio = port_2_max_ret, type = "box", min = 0, max = 1) # Optimize the portfolio to minimize risk optimal_portfolio_2 <- optimize.portfolio(R = returns_date_droped_dividened2, portfolio = port_2_max_ret, optimize_method = "ROI") optimal_weights <- extractWeights(optimal_portfolio_2) asset_names <- names(optimal_weights) weights_df <- data.frame(Asset = asset_names, Weight = optimal_weights) plot_ly(data = weights_df, x = ~Asset, y = ~Weight, type = 'bar') %>% layout(title = "Interactive Portfolio Weights", xaxis = list(title = "Asset"), yaxis = list(title = "Weight"))
HTML
port_2_max_ret <- portfolio.spec(assets = colnames(returns_date_droped_dividened2)) # Add objectives - here we minimize risk (VAR) port_2_max_ret <- add.objective(portfolio = port_2_max_ret, type = "risk", name = "StdDev")#added port_2_max_ret <- add.objective(portfolio = port_2_max_ret, type = "return", name = "mean") # Add constraints - fully invested portfolio with no short sales port_2_max_ret <- add.constraint(portfolio = port_2_max_ret, type = "long_only") port_2_max_ret <- add.constraint(portfolio = port_2_max_ret, type = "full_investment") port_2_max_ret <- add.constraint(portfolio = port_2_max_ret, type = "box", min = 0, max = 1) # Optimize the portfolio to minimize risk optimal_portfolio_2 <- optimize.portfolio(R = returns_date_droped_dividened2, portfolio = port_2_max_ret, optimize_method = "DEoptim") # expected_return_optimal_portfolio_2 <- sum(optimal_portfolio_2$weights * colMeans(returns_date_droped_dividened2)) # expected_risk_optimal_portfolio_2 <- sqrt(t(weights) %*% cov(returns_df) %*% weights) # # annualized_return_optimal_portfolio_2 <- Return.annualized(expected_return_optimal_portfolio_2, scale = 252) # # # Step 2: Annualize the risk # trading_days <- 252 # Number of trading days in a year # annualized_risk_tan <- tanRisk * sqrt(trading_days) optimal_weights <- extractWeights(optimal_portfolio_2) asset_names <- names(optimal_weights) weights_df <- data.frame(Asset = asset_names, Weight = optimal_weights) plot_ly(data = weights_df, x = ~Asset, y = ~Weight, type = 'bar') %>% layout(title = "Interactive Portfolio Weights", xaxis = list(title = "Asset"), yaxis = list(title = "Weight"))
HTML
# Calculate mean returns mean_returns <- colMeans(returns_date_droped_dividened2) # Function to calculate VaR (assuming normal distribution) VaR <- function(x, alpha = 0.05) { mean(x) - qnorm(1 - alpha) * sd(x) } # Calculate VaR for each stock VaR_values <- apply(returns_date_droped_dividened2, 2, VaR) # Create a dataframe for plotting plot_data <- data.frame(Stock = names(mean_returns), Mean_Return = mean_returns, VaR = VaR_values) # Create the ggplot p <- ggplot(plot_data, aes(x = Mean_Return, y = VaR, text = paste("Stock:", Stock, "<br>Return:", round(Mean_Return, 4), "<br>VaR:", round(VaR, 4)))) + geom_point(color = 'blue', size = 3) + labs(title = "Mean Return vs VaR for Each Stock", x = "Mean Return", y = "Value at Risk (VaR)") + theme_minimal() # Convert ggplot to plotly for interactivity interactive_plot <- ggplotly(p, tooltip = "text") # Display the interactive plot interactive_plot
HTML
initial_portfolio <- portfolio.spec(assets = colnames(returns_date_droped_dividened2)) # Add objectives - here we minimize risk (VAR) initial_portfolio <- add.objective(portfolio = initial_portfolio, type = "risk", name = "VaR") initial_portfolio <- add.objective(portfolio = initial_portfolio, type = "risk-adjusted", name = "SortinoRatio") # Add constraints - fully invested portfolio with no short sales initial_portfolio <- add.constraint(portfolio = initial_portfolio, type = "long_only") initial_portfolio <- add.constraint(portfolio = initial_portfolio, type = "full_investment") initial_portfolio <- add.constraint(portfolio = initial_portfolio, type = "box", min = 0, max = 1) # Optimize the portfolio to minimize risk optimal_portfolio <- optimize.portfolio(R = returns_date_droped_dividened2, portfolio = initial_portfolio, optimize_method = "DEoptim") # View the optimal weights optimal_weights <- extractWeights(optimal_portfolio) asset_names <- names(optimal_weights) weights_df <- data.frame(Asset = asset_names, Weight = optimal_weights) plot_ly(data = weights_df, x = ~Asset, y = ~Weight, type = 'bar') %>% layout(title = "Interactive Portfolio Weights", xaxis = list(title = "Asset"), yaxis = list(title = "Weight"))
HTML
portfolio <- add.constraint(portfolio, type = "full_investment") portfolio <- add.constraint(portfolio, type = "box", min = 0, max = 1) # Add objective to maximize the Sharpe Ratio portfolio <- add.objective(portfolio, type = "risk", name = "StdDev") portfolio <- add.objective(portfolio, type = "return", name = "mean", target = 0.35/109) portfolio <- add.objective(portfolio, type = "risk_adjusted_return", name = "SharpeRatio", risk_free_rate = risk_free_rate) opt_portfolio_deoptim <- optimize.portfolio(R = returns_xts, portfolio = portfolio, optimize_method = "DEoptim", trace = TRUE)
HTML
portfolio <- add.constraint(portfolio, type = "full_investment") portfolio <- add.constraint(portfolio, type = "box", min = 0, max = 1) # Add objective to maximize the Sharpe Ratio portfolio <- add.objective(portfolio, type = "risk", name = "StdDev") portfolio <- add.objective(portfolio, type = "return", name = "mean", target = risk_free_rate) portfolio <- add.objective(portfolio, type = "risk_adjusted_return", name = "SharpeRatio", risk_free_rate = risk_free_rate) opt_portfolio_deoptim <- optimize.portfolio(R = returns_xts, portfolio = portfolio, optimize_method = "DEoptim", trace = TRUE)
HTML
portfolio <- add.constraint(portfolio, type = "full_investment") portfolio <- add.constraint(portfolio, type = "box", min = 0, max = 1) # Add objective to maximize the Sharpe Ratio portfolio <- add.objective(portfolio, type = "risk", name = "StdDev") portfolio <- add.objective(portfolio, type = "return", name = "mean", target = 0.35/109) portfolio <- add.objective(portfolio, type = "risk_adjusted_return", name = "SharpeRatio", risk_free_rate = risk_free_rate) opt_portfolio_deoptim <- optimize.portfolio(R = returns_xts, portfolio = portfolio, optimize_method = "DEoptim", trace = TRUE)
HTML
7:10
HTML
lina
HTML
235% return
HTML
latest
HTML
50% geom mean
HTML
using roi
HTML
optimal wight # Add constraints portfolio <- add.constraint(portfolio, type = "full_investment") portfolio <- add.constraint(portfolio, type = "box", min = 0, max = 1) # Add objective to maximize the Sharpe Ratio portfolio <- add.objective(portfolio, type = "risk", name = "StdDev") portfolio <- add.objective(portfolio, type = "return", name = "mean", target = 0.35/109) portfolio <- add.objective(portfolio, type = "risk_adjusted_return", name = "SharpeRatio", risk_free_rate = risk_free_rate) opt_portfolio_deoptim <- optimize.portfolio(R = returns_xts, portfolio = portfolio, optimize_method = "DEoptim", trace = TRUE) #
HTML
lina
HTML
lina 4.9% return port
HTML
max return, min risk
HTML
portfolio, type = "return", name = "mean"
HTML
123
HTML
according expected shortfall
HTML
Low Risk Portfolio last 6 month
HTML
interactive stocks 2
HTML
interactive chart 1