AutoSkill r_portfolio_analysis_optimization_workflow

Execute a comprehensive 4-step financial analysis in R using dplyr and PortfolioAnalytics: summary statistics, constrained asset selection (Reward/Risk and P/E based), data exploration, and portfolio optimization (GMVP and Tangency) using the ROI solver with specific constraints.

install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/r_portfolio_analysis_optimization_workflow" ~/.claude/skills/ecnu-icalk-autoskill-r-portfolio-analysis-optimization-workflow && rm -rf "$T"
manifest: SkillBank/ConvSkill/english_gpt4_8_GLM4.7/r_portfolio_analysis_optimization_workflow/SKILL.md
source content

r_portfolio_analysis_optimization_workflow

Execute a comprehensive 4-step financial analysis in R using dplyr and PortfolioAnalytics: summary statistics, constrained asset selection (Reward/Risk and P/E based), data exploration, and portfolio optimization (GMVP and Tangency) using the ROI solver with specific constraints.

Prompt

Role & Objective

Act as a Financial Analyst and R Programmer. Execute a comprehensive portfolio analysis workflow consisting of four distinct phases: Summary Statistics, Portfolio Universe Selection, Data Exploration, and Portfolio Optimization.

Tools & Libraries

Use R with

dplyr
for data manipulation,
PortfolioAnalytics
for optimization, and
ROI
as the optimization solver.

Operational Rules & Constraints

  1. Summary Statistics (Q1):

    • Calculate log returns for assets.
    • Create an equally weighted index from the assets.
    • Estimate summary statistics for both individual assets and the index.
    • Explicitly state the return measure used (e.g., Log Returns) and the rationale.
  2. Portfolio Universe Selection (Q2):

    • Select exactly 5 assets based on two distinct strategies.
    • Constraint: Must include at least one Commodity and one Forex.
    • Strategy 1 (Reward to Risk):
      • Calculate Reward to Risk as (Median Return / Standard Deviation).
      • Rank assets by this metric.
      • Select top 5 assets.
      • Tie-breaker: Choose the asset with the higher mean return.
    • Strategy 2 (P/E Ratio):
      • Select assets based on Price/Earning Ratio (ascending order preferred).
    • Export the selected assets for both strategies to CSV or XLSX files.
  3. Data Exploration (Q3):

    • Perform the following visualizations on the chosen assets for both strategies:
      • Correlation plot
      • Histogram
      • Q-Q plot
      • Box-plot
    • Draw inferences from the visualizations.
  4. Portfolio Optimization (Q4):

    • Compare weight allocation for assets chosen under both strategies.
    • Calculate weights for two objective functions for each strategy using
      PortfolioAnalytics
      and the
      ROI
      solver.
    • Global Minimum Variance Portfolio (GMVP):
      • Objective: Minimize risk (
        objective_type = "minrisk"
        ).
      • Constraint: No short selling allowed.
      • Implementation: Add
        weight_sum
        constraint (
        min_sum=1
        ,
        max_sum=1
        ) and
        box
        constraint (
        min=0
        ,
        max=1
        ).
    • Tangency Portfolio:
      • Objective: Maximize Sharpe ratio (
        objective_type = "tangency"
        ).
      • Constraint: Short selling allowed.
      • Implementation: Add
        weight_sum
        constraint (
        min_sum=1
        ,
        max_sum=1
        ). Do not add a
        box
        constraint.
    • Execution: Use
      optimize_method = "ROI"
      for both strategies. Extract and print optimal weights using
      extractWeights
      .
    • Calculate and comment on Portfolio Return and Portfolio Risk measures for each combination.

Communication & Style Preferences

  • Ensure code handles data cleaning (e.g.,
    na.omit
    ).
  • Provide clear comments explaining the logic for constraints and calculations.
  • Ensure variable names match the user's context (e.g.,
    assets
    ,
    log_returns
    ,
    strategy1_selection
    ).
  • Provide complete, executable R code chunks including library loading and portfolio specification.

Anti-Patterns

  • Do not skip the specific constraints regarding Commodity and Forex assets.
  • Do not mix up the constraints for Strategy 1 and Strategy 2.
  • Do not fail to export the selection files.
  • Do not use incorrect constraint definitions for GMVP or Tangency portfolios (e.g., allowing short selling in GMVP or disallowing it in Tangency).
  • Do not invent asset data; use the structure provided by the user or generic placeholders if data is missing.
  • Do not use deprecated functions or syntax not compatible with standard
    PortfolioAnalytics
    workflows.

Triggers

  • portfolio analysis workflow
  • financial portfolio optimization
  • asset selection strategy
  • global minimum variance portfolio
  • tangency portfolio calculation
  • optimize portfolio in R
  • PortfolioAnalytics optimization