mean-variance-optimization

Compute tangency portfolio weights maximizing the Sharpe ratio from expected returns, risks, and correlations.

Updated Sep 7, 2025
One-click install
npx skills add https://github.com/kerryback/genai4finance --skill mean-variance-optimization
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mean-variance-optimization
Source: https://github.com/kerryback/genai4finance/tree/main/.claude/skills/mean-variance-optimization
Command: npx skills add https://github.com/kerryback/genai4finance --skill mean-variance-optimization

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires numpy, and includes scripts (resource) components.

What problem does it solve?

This Skill eliminates the complex and time-consuming manual calculations involved in mean-variance portfolio optimization. It helps users effortlessly determine optimal asset allocations, understand risk-return tradeoffs, and identify the tangency portfolio, saving significant effort and reducing the potential for error in financial analysis.

Core Features & Use Cases

  • Optimal Portfolio Weights: Automatically calculates the precise allocation for each asset to maximize the Sharpe ratio, allowing for both long and short positions.
  • Sharpe Ratio Maximization: Identifies the tangency portfolio—the most efficient risky portfolio that offers the highest expected return per unit of risk.
  • Detailed Financial Explanation: Provides comprehensive interpretations of the optimization results, including the rationale behind asset allocations, diversification benefits, and the economic intuition of modern portfolio theory.
  • Use Case: An investment analyst needs to quickly determine the optimal asset allocation for a client's multi-asset portfolio, considering various expected returns, risks, and correlations. This Skill interactively gathers the necessary data, performs the complex calculations, and presents a clear, actionable portfolio strategy with detailed explanations.

Quick Start

Run the interactive optimization script and follow the prompts:

python .claude/skills/mean-variance-optimization/scripts/optimize_portfolio.py

Frequently Asked Questions about mean-variance-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I calculate optimal portfolio weights to maximize the Sharpe ratio?

Mean-variance optimization computes tangency portfolio weights by taking expected returns, standard deviations, correlations, and risk-free rate as inputs, then solves for asset allocations that maximize risk-adjusted returns. The Skill constructs and inverts the covariance matrix, normalizes weights to sum to one, and outputs portfolio statistics and Sharpe ratio.

What is modern portfolio theory and how does it improve asset allocation?

Modern portfolio theory identifies efficient portfolios that maximize expected return for a given risk level by analyzing correlations between assets. This Skill applies mean-variance optimization to find the tangency portfolio—the most efficient risky portfolio offering the highest return per unit of risk taken.

Can I use this for both long and short positions in portfolio optimization?

Yes, this Skill supports both long and short positions when computing optimal weights. It allows negative allocations for short positions while maximizing the Sharpe ratio across your multi-asset portfolio.

What input data do I need to run portfolio optimization?

You need expected returns and standard deviations for each asset, the correlation matrix showing how assets move together, and the risk-free rate. The Skill gathers this data interactively, constructs the covariance matrix, and computes optimal weights.

How does the Skill validate and handle the covariance matrix?

The Skill constructs the covariance matrix from standard deviations and correlations, validates it for mathematical soundness, computes its inverse for optimization calculations, and rounds final asset weights to four decimals for precision.

What outputs should I expect from mean-variance portfolio optimization?

The Skill outputs optimal asset weights for each position, portfolio expected return, portfolio standard deviation, Sharpe ratio, and detailed explanations of allocation rationale and diversification benefits.