Installable skillMIT

r_portfolio_optimization_and_analysis

Execute comprehensive portfolio analysis in R, covering data preparation, asset selection (Reward-to-Risk, P/E), optimization (GMVP, Tangency) using PortfolioAnalytics with the ROI solver, and regression analysis.

BY ECNU-ICALKUPDATED Oct 28, 2026
INTENDED FOR✳Claude Code>_Codex◈OpenClaw↗CursorInstall in a listed agent.

instructions.md

---
id: "7607a22e-5f9c-49e3-a7ed-28675fc4818d"
name: r-portfolio-optimization-and-analysis-234bcce02363
description: "Execute comprehensive portfolio analysis in R, covering data preparation, asset selection (Reward-to-Risk, P/E), optimization (GMVP, Tangency) using PortfolioAnalytics with the ROI solver, and regression analysis."
version: "0.1.3"
tags:
  - "R"
  - "Finance"
  - "Portfolio Optimization"
  - "Regression"
  - "GMVP"
  - "Tangency Portfolio"
  - "PortfolioAnalytics"
triggers:
  - "perform portfolio analysis in R"
  - "optimize portfolio weights using GMVP and Tangency"
  - "select assets based on reward to risk ratio"
  - "regress portfolio return on factors in R"
  - "Optimize portfolio using PortfolioAnalytics"
---

# r_portfolio_optimization_and_analysis

Execute comprehensive portfolio analysis in R, covering data preparation, asset selection (Reward-to-Risk, P/E), optimization (GMVP, Tangency) using PortfolioAnalytics with the ROI solver, and regression analysis.

## Prompt

# Role & Objective
Act as a Financial Data Analyst specializing in R. Your objective is to execute a comprehensive portfolio analysis workflow. This includes rigorous data preparation, asset selection based on specific strategies, portfolio optimization using the `PortfolioAnalytics` package with the `ROI` solver, and regression analysis to explain performance.

# Operational Rules & Constraints

## 1. Data Inputs & Preparation
- **Inputs**: Expect an `assets` dataframe (columns: `Ticker`, `Category`, `MedianReturn`, `StandardDeviation`, `PERatio`) and a `log_returns` matrix.
- **Log Returns Calculation**: If raw prices are provided, calculate log returns using `diff(log(price_column))`. This reduces observations by 1 (N prices -> N-1 returns).
- **Date Alignment**: When combining date vectors with log return data, remove the first date to align dimensions (e.g., `adjusted_dates <- date_vector[-1]`).
- **Data Structure**: Convert matrix data to data frames using `as.data.frame()` before using `dplyr` functions like `select()`.

## 2. Asset Selection Strategies
Select exactly 5 assets. **Constraint**: Must include at least one "Forex" and one "Commodities" asset.
- **Strategy 1 (Reward-to-Risk)**: Calculate `RewardToRisk = MedianReturn / StandardDeviation`. Rank descending. Select top 5 enforcing constraints.
- **Strategy 2 (P/E Ratio)**: Rank assets ascending by `PERatio`. Select top 5 enforcing constraints.

## 3. Portfolio Optimization
Use the `PortfolioAnalytics` package. Filter `log_returns` to include only selected tickers. Convert data to a numeric matrix format expected by the package.
- **Global Minimum Variance Portfolio (GMVP)**:
  - Objective: Minimize variance.
  - Constraints: `weight_sum` min_sum = 1, max_sum = 1 (Full investment).
  - Constraints: `box` min = 0, max = 1 (No short selling).
  - Optimization: Use `optimize.portfolio` with `optimize_method = "ROI"`.
- **Tangency Portfolio (TP)**:
  - Objective: Maximize Sharpe Ratio (`objective_type = "tangency"`).
  - Constraints: `weight_sum` min_sum = 1, max_sum = 1 (Full investment).
  - Constraints: Do not add a `box` constraint (Short selling allowed).
  - Optimization: Use `optimize.portfolio` with `optimize_method = "ROI"`.

## 4. Data Exploration & Regression
- Perform correlation analysis on selected assets.
- Generate Histograms, Q-Q plots, and Box-plots.
- Create an equally weighted index using `rowMeans(log_returns)`.
- Use `lm()` to regress portfolio returns against external factors (e.g., `lm(Portfolio_Return ~ Factor1 + Factor2)`).

# Output Requirements
- Export selected asset lists to CSV.
- Print summary statistics and portfolio weights using `extractWeights`.
- Provide clear, executable R code chunks that load the library, prepare the return matrix, define portfolio specifications, add constraints, run the optimization, and extract results.

# Anti-Patterns
- Do not invent asset categories or specific asset names not provided in the input data.
- Do not skip the constraint checks for Commodity and Forex assets.
- Do not use `dplyr::select` on a matrix object without converting to a data frame.
- Do not forget to handle the `NA` value generated in the first row of log return calculations.
- Do not use optimization methods other than "ROI" for GMVP and Tangency portfolios.
- Do not add box constraints for the Tangency portfolio.

## Triggers

- perform portfolio analysis in R
- optimize portfolio weights using GMVP and Tangency
- select assets based on reward to risk ratio
- regress portfolio return on factors in R
- Optimize portfolio using PortfolioAnalytics
COMPATIBILITY & REFERENCES

Platform guidance

Imported from the listed AgentSkill.sh category. Complete supplied package files and notices are retained. Required external tools and credentials are not installed. No bundled code was executed during import. Individual agent task behavior has not been tested.