Portfolio logo

Portfolio

Community
gauss314
portfolio

Construcción y optimización cuantitativa de portafolios: Markowitz (scipy.optimize + Monte Carlo), Black-Litterman (prior CAPM, views absolutas/relativas, posterior bayesiano), HRP/HERC/NCO (clustering jerárquico, risk parity, NCO con restricciones). Todo flat numpy + scipy, sin Riskfolio-Lib ni PyPortfolioOpt.

Overview

Publishergauss314
Repositoryskills
Skill nameportfolio
Stars
250
Forks
35
Bundled files
21
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 21 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by gauss314 on GitHub. Read the source before you install it.

Installation

Install the Portfolio AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/gauss314/skills/tree/main/skills/portfolio
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/gauss314/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/skills/portfolio .claude/skills/portfolio
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Portfolio in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Portfolio on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Portfolio is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Portfolio — Optimización Cuantitativa de Portafolios

Este skill implementa 3 enfoques de optimización de portafolios desde el material del curso (notebook Clase_08_teoria_2025_portafolio.ipynb y PDF Portafolios 2025 Ucema.pdf):

  1. Markowitz / Media-Varianza — Optimización convexa vía scipy.optimize
    • simulación Monte Carlo + frontera eficiente + CML.
  2. Black-Litterman — Combinación bayesiana de retornos de equilibrio de mercado (CAPM inverso) con views del inversor, incluyendo matriz de incertidumbre Ω (método Idzorek).
  3. HRP / HERC / NCO — Construcción jerárquica de portafolios mediante clustering (single/complete/average/ward), risk parity y NCO con restricciones.

Todos los scripts usan solo numpy, pandas y scipy. Sin dependencias pesadas. Este skill es autónomo: funciona sin skills/backtesting.

Para ratios de performance post-optimización (Sharpe, Sortino, VaR, drawdowns, etc.) consultar el skill hermana: skills/backtesting.

Part of the Gauss314 Skills Repository.


File Map

skills/portfolio/
├── SKILL.md                           ← Este archivo
├── references/
│   ├── PORTFOLIO_THEORY.md            ← MPT, Markowitz, frontera eficiente (ES)
│   ├── BLACK_LITTERMAN.md             ← BL: prior, views, posterior, omega (ES)
│   ├── HIERARCHICAL.md                ← HRP, HERC, NCO, clustering (ES)
│   └── RISK_MEASURES.md               ← VaR, CVaR, MAD, MSV, DR, MDD (ES)
├── assets/
│   ├── sample_prices.csv              ← Precios multi-activo para ejemplos
│   ├── sample_returns.csv             ← Retornos multi-activo
│   ├── sample_mcaps.json              ← Market caps para Black-Litterman
│   └── defaults.json                  ← Parámetros default
├── scripts/
│   ├── __init__.py
│   ├── portfolio.py                   ← Core: Markowitz, Sharpe, Monte Carlo, frontera
│   ├── black_litterman.py             ← BL: prior, posterior, omega, views
│   ├── hierarchical.py                ← HRP/HERC/NCO: clustering, risk parity, constraints
│   ├── risk_measures.py               ← VaR, CVaR, MAD, MSV, MDD, DR
│   ├── covariance.py                  ← Covarianza: hist, ledoit-wolf, oas, ewma
│   └── cli.py                         ← CLI unificada (12 modos)
└── tests/
    ├── __init__.py
    └── test_portfolio.py              ← Tests + validación contra notebook

Qué hace cada script

ScriptRolFunciones clave
portfolio.pyCore de optimización Markowitzmax_sharpe_optim, min_variance_optim, random_portfolios, efficient_frontier, cml_portfolio, asset_stats
black_litterman.pyBlack-Litterman completomarket_implied_risk_aversion, market_implied_prior_returns, bl_posterior_returns, omega_idzorek
hierarchical.pyHRP / HERC / NCOhrp_portfolio, herc_portfolio, nco_portfolio, nco_with_constraints, hrp_constraints
risk_measures.pyMedidas de riesgovar_historic, cvar, max_drawdown, cdar, diversification_ratio, risk_contribution
covariance.pyEstimación de covarianzacov_hist, cov_ledoit_wolf, cov_oas, cov_ewma

Quick Start

Markowitz (scipy.optimize)

bash
# Max Sharpe con 3 activos
py scripts/cli.py markowitz --assets assets/sample_returns.csv

# Con tasa libre de riesgo personalizada
py scripts/cli.py markowitz --assets assets/sample_returns.csv --rf 0.05

# Estadísticas individuales
py scripts/cli.py stats --assets assets/sample_returns.csv

Monte Carlo

bash
# Simular 10.000 carteras aleatorias
py scripts/cli.py montecarlo --assets assets/sample_returns.csv

# Guardar frontera a CSV
py scripts/cli.py montecarlo --assets assets/sample_returns.csv --save frontier.csv

Frontera Eficiente

bash
py scripts/cli.py frontier --assets assets/sample_returns.csv --n 50

CML — Leverage y Deleverage

El portafolio tangente (máximo Sharpe) se combina con el activo libre de riesgo para obtener cualquier punto sobre la Capital Market Line (CML), manteniendo el mismo Sharpe ratio.

bash
# Portafolio tangente puro (w=1)
py scripts/cli.py cml --assets assets/sample_returns.csv --weight 1.0

# Deleverage: 60% en tangencia, 40% en Rf (menos riesgo, mismo Sharpe)
py scripts/cli.py cml --assets assets/sample_returns.csv --weight 0.6

# Leverage: pide prestado 50% a Rf, invierte 150% en tangencia (más riesgo, mismo Sharpe)
py scripts/cli.py cml --assets assets/sample_returns.csv --weight 1.5

Black-Litterman

bash
# Prior: retornos implícitos de mercado (CAPM inverso)
py scripts/cli.py bl-prior --assets assets/sample_returns.csv --market-prices assets/sample_prices.csv --mcaps assets/sample_mcaps.json

# BL completo con views + optimización
py scripts/cli.py bl --assets assets/sample_returns.csv --market-prices assets/sample_prices.csv --mcaps assets/sample_mcaps.json --views '{"BMA": 0.25, "LOMA": 0.4, "MELI": -0.1}' --confidences "0.3,0.5,0.8" --optimize

HRP / HERC / NCO

bash
# Hierarchical Risk Parity
py scripts/cli.py hrp --assets assets/sample_returns.csv

# Nested Clustered Optimization
py scripts/cli.py nco --assets assets/sample_returns.csv --clusters 3

# NCO con restricciones
py scripts/cli.py nco-con --assets assets/sample_returns.csv --constraints assets/sample_constraints.csv --classes assets/sample_classes.csv

Riesgo

bash
# Todas las medidas de riesgo
py scripts/cli.py risk --prices assets/sample_prices.csv

# Medida específica
py scripts/cli.py risk --prices assets/sample_prices.csv --measure var

Usar como Librería

python
from scripts.portfolio import *
from scripts.black_litterman import *
from scripts.hierarchical import *

import numpy as np

# --- Markowitz ---
rets = pd.read_csv('assets/sample_returns.csv', index_col=0)
result = max_sharpe_optim(rets, rf=0.045)
print(result['weights'], result['sharpe'])  # pesos óptimos, Sharpe

# --- CML: leverage/deleverage ---
# 60% en tangencia, 40% en Rf (deleverage)
cml = cml_portfolio(rets, rf=0.045, weight_tangency=0.6)
print(cml['ret'], cml['vol'], cml['sharpe'])  # mismo Sharpe que el tangente

# Leverage: 150% en tangencia (pide prestado 50% a Rf)
cml2 = cml_portfolio(rets, rf=0.045, weight_tangency=1.5)
print(cml2['ret'], cml2['vol'], cml2['sharpe'])  # mismo Sharpe

# --- Monte Carlo ---
port_df = random_portfolios(rets, n_portfolios=10000, rf=0.045)
best = port_df.loc[port_df['sharpe'].idxmax()]
print(best['weights'])  # mejor combinación Monte Carlo

# --- Black-Litterman ---
import json
with open('assets/sample_mcaps.json') as f:
    mcaps = json.load(f)
spy = pd.read_csv('assets/sample_prices.csv')['SPY'].pct_change().dropna()
bl_result = bl_pipeline(rets, spy.values, mcaps,
                        view_dict={'BMA': 0.25, 'LOMA': 0.4},
                        view_confidences=[0.3, 0.5], rf=0.045)
print(bl_result['posterior'])  # retornos a posteriori

# --- HRP ---
hrp_result = hrp_portfolio(rets, linkage_method='ward')
print(hrp_result['weights'])  # pesos HRP

Dependencias

LibreríaRequeridaUso
numpy✅Cómputo vectorizado, álgebra lineal
pandas✅CSV I/O, DataFrames
scipy✅optimize (Markowitz), cluster.hierarchy (HRP/NCO), stats

No requiere Riskfolio-Lib, PyPortfolioOpt, sklearn, cvxpy ni arch.

Para visualización (dendrogramas, frontera eficiente) se puede usar matplotlib opcionalmente. Ejemplos de plots están en el notebook de referencia.


Referencias Teóricas

  • Markowitz (1952): "Portfolio Selection", Journal of Finance.
  • Black & Litterman (1992): "Global Portfolio Optimization", Financial Analysts Journal.
  • Idzorek (2005): "A Step-by-Step Guide to the Black-Litterman Model".
  • Lopez de Prado (2016): "Building Diversified Portfolios that Outperform Out of Sample" (HRP).
  • De Prado (2019): "Nested Clustered Optimization", SSRN 3469961.
  • Pfitzinger & Katzke (2019): "NCO with Constraints", SSRN 4409173.
  • Meucci (2006): "Beyond Black-Litterman: Views on Non-Normal Markets", SSRN 1213325.
  • Avramov (2004): "Bayesian Variable Selection in Portfolio Analysis", SSRN 3326617.

Para profundizar en ratios de performance (30+ métricas: Sharpe, Sortino, VaR, cVaR, Kelly, Rachev, Profit Factor, etc.) y backtesting de estrategias: skills/backtesting.


Notebook de referencia

El contenido teórico y ejemplos numéricos de este skill están basados en:

  • temp/Clase_08_teoria_2025_portafolio.ipynb — Implementaciones en Python de Markowitz, Monte Carlo, NCO (Riskfolio-Lib), Black-Litterman (PyPortfolioOpt).
  • temp/Portafolios 2025 Ucema.pdf — Marco teórico: MPT, CAPM, Fama-French, clustering, NCO, Black-Litterman.

Las implementaciones flat numpy en scripts/ replican los resultados de esos notebooks sin depender de las librerías mencionadas.

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Portfolio AI skill do?

Construcción y optimización cuantitativa de portafolios: Markowitz (scipy.optimize + Monte Carlo), Black-Litterman (prior CAPM, views absolutas/relativas, posterior bayesiano), HRP/HERC/NCO (clustering jerárquico, risk parity, NCO con restricciones). Todo flat numpy + scipy, sin Riskfolio-Lib ni PyPortfolioOpt.

Why use Portfolio on TypingMind?

Because you install it once and use it with any model. Portfolio is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Portfolio in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/gauss314/skills/tree/main/skills/portfolio. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Portfolio?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Portfolio?

As many as you like. As long as a model supports skills, you can use Portfolio with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Portfolio AI skill free?

Yes. It is published on GitHub by gauss314 under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇