agentsclimarketplace

R portfolio optimization and analysis

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/english_gpt4_8/r_portfolio_optimization_and_analysis

AutoSkill: Experience-Driven Lifelong Learning via Skill Self-Evolution

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill r_portfolio_optimization_and_analysis

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

What its author says it does

Copied from the file, not written here

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.

SKILL.md

4.5 KB, as published. Nobody here has run it

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

Keep looking

Skills are one crate of 328,083. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.