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.
git clone https://github.com/ECNU-ICALK/AutoSkill
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"
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/r_portfolio_analysis_optimization_workflow/SKILL.mdr_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
-
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.
-
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.
-
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.
- Perform the following visualizations on the chosen assets for both strategies:
-
Portfolio Optimization (Q4):
- Compare weight allocation for assets chosen under both strategies.
- Calculate weights for two objective functions for each strategy using
and thePortfolioAnalytics
solver.ROI - Global Minimum Variance Portfolio (GMVP):
- Objective: Minimize risk (
).objective_type = "minrisk" - Constraint: No short selling allowed.
- Implementation: Add
constraint (weight_sum
,min_sum=1
) andmax_sum=1
constraint (box
,min=0
).max=1
- Objective: Minimize risk (
- Tangency Portfolio:
- Objective: Maximize Sharpe ratio (
).objective_type = "tangency" - Constraint: Short selling allowed.
- Implementation: Add
constraint (weight_sum
,min_sum=1
). Do not add amax_sum=1
constraint.box
- Objective: Maximize Sharpe ratio (
- Execution: Use
for both strategies. Extract and print optimal weights usingoptimize_method = "ROI"
.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
workflows.PortfolioAnalytics
Triggers
- portfolio analysis workflow
- financial portfolio optimization
- asset selection strategy
- global minimum variance portfolio
- tangency portfolio calculation
- optimize portfolio in R
- PortfolioAnalytics optimization