Spaces:
Sleeping
Sleeping
File size: 6,414 Bytes
72fc0ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | """
Portfolio Manager - Portfolio analysis and optimization tools.
This module provides portfolio construction, optimization, and analysis
functionality for the QuantLib Pro SDK.
"""
from typing import Dict, List, Any, Optional, Union
import pandas as pd
import numpy as np
import logging
logger = logging.getLogger(__name__)
class PortfolioManager:
"""
Portfolio management and analysis tools.
Provides functionality for:
- Portfolio construction and optimization
- Performance attribution and analysis
- Rebalancing and risk budgeting
- Backtesting and scenario analysis
"""
def __init__(self, config=None):
"""
Initialize Portfolio Manager.
Args:
config: SDK configuration object
"""
self.config = config
logger.info("Portfolio Manager initialized")
def create_portfolio(self, assets: List[str], weights: Optional[List[float]] = None) -> Dict[str, Any]:
"""
Create a portfolio from assets and weights.
Args:
assets: List of asset symbols
weights: Optional list of weights (equal weights if None)
Returns:
Dict containing portfolio information
"""
if weights is None:
weights = [1.0 / len(assets)] * len(assets)
if len(weights) != len(assets):
raise ValueError("Number of weights must match number of assets")
if abs(sum(weights) - 1.0) > 1e-6:
logger.warning("Weights do not sum to 1.0, normalizing.")
weights = [w / sum(weights) for w in weights]
portfolio = {
"assets": assets,
"weights": weights,
"creation_date": pd.Timestamp.now(),
"portfolio_id": f"portfolio_{hash(str(assets + weights)) % 10000}"
}
logger.info(f"Created portfolio with {len(assets)} assets")
return portfolio
def calculate_returns(self, portfolio_data: pd.DataFrame) -> pd.Series:
"""
Calculate portfolio returns from price data.
Args:
portfolio_data: DataFrame with price data
Returns:
Series of portfolio returns
"""
if portfolio_data.empty:
raise ValueError("Portfolio data cannot be empty")
returns = portfolio_data.pct_change().dropna()
logger.info(f"Calculated returns for {len(returns)} periods")
return returns
def calculate_portfolio_metrics(self, returns: pd.Series) -> Dict[str, float]:
"""
Calculate key portfolio performance metrics.
Args:
returns: Series of portfolio returns
Returns:
Dict containing performance metrics
"""
if len(returns) < 2:
raise ValueError("Need at least 2 return observations")
metrics = {
"total_return": (1 + returns).prod() - 1,
"annualized_return": (1 + returns.mean()) ** 252 - 1,
"volatility": returns.std() * np.sqrt(252),
"sharpe_ratio": (returns.mean() * 252) / (returns.std() * np.sqrt(252)),
"max_drawdown": self._calculate_max_drawdown(returns),
"skewness": returns.skew(),
"kurtosis": returns.kurtosis(),
"var_95": returns.quantile(0.05),
"observation_count": len(returns)
}
logger.info("Calculated portfolio metrics")
return metrics
def optimize_portfolio(self, expected_returns: pd.Series,
covariance_matrix: pd.DataFrame,
risk_aversion: float = 1.0) -> Dict[str, Any]:
"""
Optimize portfolio using mean-variance optimization.
Args:
expected_returns: Expected returns for each asset
covariance_matrix: Asset covariance matrix
risk_aversion: Risk aversion parameter
Returns:
Dict containing optimal weights and metrics
"""
try:
# Simple mean-variance optimization
inv_cov = np.linalg.inv(covariance_matrix.values)
ones = np.ones((len(expected_returns), 1))
# Calculate optimal weights
w_opt = (inv_cov @ expected_returns.values.reshape(-1, 1)) / risk_aversion
w_opt = w_opt / np.sum(w_opt) # Normalize
# Calculate expected portfolio return and risk
expected_port_return = float(expected_returns.T @ w_opt.flatten())
expected_port_risk = float(np.sqrt(w_opt.T @ covariance_matrix.values @ w_opt))
result = {
"optimal_weights": dict(zip(expected_returns.index, w_opt.flatten())),
"expected_return": expected_port_return,
"expected_risk": expected_port_risk,
"sharpe_ratio": expected_port_return / expected_port_risk if expected_port_risk > 0 else 0,
"optimization_method": "mean_variance"
}
logger.info("Portfolio optimization completed successfully")
return result
except Exception as e:
logger.error(f"Portfolio optimization failed: {e}")
return {"error": str(e)}
def _calculate_max_drawdown(self, returns: pd.Series) -> float:
"""
Calculate maximum drawdown from returns series.
Args:
returns: Series of returns
Returns:
Maximum drawdown as a float
"""
cumulative = (1 + returns).cumprod()
running_max = cumulative.expanding().max()
drawdown = (cumulative - running_max) / running_max
return float(drawdown.min())
def health_check(self) -> Dict[str, Any]:
"""
Perform health check of portfolio manager.
Returns:
Dict containing health status
"""
return {
"status": "healthy",
"module": "portfolio",
"capabilities": [
"portfolio_creation",
"return_calculation",
"performance_metrics",
"mean_variance_optimization"
]
} |