File size: 9,573 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""
Mean-variance portfolio optimization (Markowitz, 1952).

Solves for optimal portfolio weights given expected returns and a
covariance matrix.  Includes:
  - Maximum Sharpe ratio portfolio
  - Minimum variance portfolio
  - Efficient frontier construction
  - Target return/volatility portfolios

Uses scipy.optimize for constrained optimization with bounds.
"""

from __future__ import annotations

import logging
from dataclasses import dataclass
from typing import Optional

import numpy as np
import pandas as pd
from scipy.optimize import minimize

from quantlib_pro.utils.validation import ValidationError, require_positive

log = logging.getLogger(__name__)


@dataclass
class PortfolioResult:
    """Results from a portfolio optimization."""
    weights: np.ndarray
    expected_return: float
    volatility: float
    sharpe_ratio: float
    tickers: list[str]
    method: str
    
    def to_dict(self) -> dict:
        """Return weights as {ticker: weight} dict."""
        return dict(zip(self.tickers, self.weights))
    
    def summary(self) -> str:
        return (
            f"{self.method} Portfolio:\n"
            f"  Expected Return: {self.expected_return:.2%}\n"
            f"  Volatility: {self.volatility:.2%}\n"
            f"  Sharpe Ratio: {self.sharpe_ratio:.4f}\n"
            f"  Weights: {self.to_dict()}"
        )


def _portfolio_return(weights: np.ndarray, expected_returns: np.ndarray) -> float:
    """Calculate portfolio expected return."""
    return np.dot(weights, expected_returns)


def _portfolio_volatility(weights: np.ndarray, cov_matrix: np.ndarray) -> float:
    """Calculate portfolio volatility (standard deviation)."""
    return np.sqrt(np.dot(weights.T, np.dot(cov_matrix, weights)))


def _negative_sharpe(
    weights: np.ndarray,
    expected_returns: np.ndarray,
    cov_matrix: np.ndarray,
    risk_free_rate: float,
) -> float:
    """Negative Sharpe ratio (for minimization)."""
    ret = _portfolio_return(weights, expected_returns)
    vol = _portfolio_volatility(weights, cov_matrix)
    if vol == 0:
        return 1e10  # Penalize zero-volatility portfolios
    return -(ret - risk_free_rate) / vol


def max_sharpe_portfolio(
    expected_returns: pd.Series | np.ndarray,
    cov_matrix: pd.DataFrame | np.ndarray,
    risk_free_rate: float = 0.02,
    allow_short: bool = False,
) -> PortfolioResult:
    """
    Compute maximum Sharpe ratio portfolio.

    Parameters
    ----------
    expected_returns : array-like
        Expected annual returns for each asset
    cov_matrix : array-like
        Covariance matrix (annualized)
    risk_free_rate : float
        Annual risk-free rate (e.g., 0.02 for 2%)
    allow_short : bool
        If True, allow negative weights (short selling)

    Returns
    -------
    PortfolioResult
        Optimal weights and metrics
    """
    if isinstance(expected_returns, pd.Series):
        tickers = expected_returns.index.tolist()
        exp_ret = expected_returns.values
    else:
        exp_ret = np.array(expected_returns)
        tickers = [f"Asset{i+1}" for i in range(len(exp_ret))]
    
    if isinstance(cov_matrix, pd.DataFrame):
        cov = cov_matrix.values
    else:
        cov = np.array(cov_matrix)
    
    n_assets = len(exp_ret)
    require_positive(n_assets, "n_assets")
    
    # Initial guess: equal weights
    x0 = np.ones(n_assets) / n_assets
    
    # Constraints: weights sum to 1
    constraints = {"type": "eq", "fun": lambda w: np.sum(w) - 1.0}
    
    # Bounds: [0, 1] for long-only, or [-1, 1] for short-allowed
    if allow_short:
        bounds = [(-1.0, 1.0)] * n_assets
    else:
        bounds = [(0.0, 1.0)] * n_assets
    
    result = minimize(
        _negative_sharpe,
        x0,
        args=(exp_ret, cov, risk_free_rate),
        method="SLSQP",
        bounds=bounds,
        constraints=constraints,
    )
    
    if not result.success:
        raise ValidationError(f"Optimization failed: {result.message}")
    
    weights = result.x
    ret = _portfolio_return(weights, exp_ret)
    vol = _portfolio_volatility(weights, cov)
    sharpe = (ret - risk_free_rate) / vol if vol > 0 else 0.0
    
    return PortfolioResult(
        weights=weights,
        expected_return=ret,
        volatility=vol,
        sharpe_ratio=sharpe,
        tickers=tickers,
        method="Max Sharpe",
    )


def min_volatility_portfolio(
    expected_returns: pd.Series | np.ndarray,
    cov_matrix: pd.DataFrame | np.ndarray,
    allow_short: bool = False,
) -> PortfolioResult:
    """
    Compute minimum volatility portfolio.

    Parameters
    ----------
    expected_returns : array-like
        Expected returns (needed for PortfolioResult, not optimization)
    cov_matrix : array-like
        Covariance matrix
    allow_short : bool
        Allow short selling

    Returns
    -------
    PortfolioResult
        Minimum variance portfolio
    """
    if isinstance(expected_returns, pd.Series):
        tickers = expected_returns.index.tolist()
        exp_ret = expected_returns.values
    else:
        exp_ret = np.array(expected_returns)
        tickers = [f"Asset{i+1}" for i in range(len(exp_ret))]
    
    if isinstance(cov_matrix, pd.DataFrame):
        cov = cov_matrix.values
    else:
        cov = np.array(cov_matrix)
    
    n_assets = len(exp_ret)
    x0 = np.ones(n_assets) / n_assets
    
    constraints = {"type": "eq", "fun": lambda w: np.sum(w) - 1.0}
    bounds = [(-1.0, 1.0)] * n_assets if allow_short else [(0.0, 1.0)] * n_assets
    
    result = minimize(
        lambda w: _portfolio_volatility(w, cov),
        x0,
        method="SLSQP",
        bounds=bounds,
        constraints=constraints,
    )
    
    if not result.success:
        raise ValidationError(f"Optimization failed: {result.message}")
    
    weights = result.x
    ret = _portfolio_return(weights, exp_ret)
    vol = _portfolio_volatility(weights, cov)
    sharpe = ret / vol if vol > 0 else 0.0
    
    return PortfolioResult(
        weights=weights,
        expected_return=ret,
        volatility=vol,
        sharpe_ratio=sharpe,
        tickers=tickers,
        method="Min Volatility",
    )


def target_return_portfolio(
    expected_returns: pd.Series | np.ndarray,
    cov_matrix: pd.DataFrame | np.ndarray,
    target_return: float,
    allow_short: bool = False,
) -> PortfolioResult:
    """
    Find minimum volatility portfolio with a target expected return.

    Parameters
    ----------
    target_return : float
        Desired portfolio return (e.g., 0.10 for 10%)

    Returns
    -------
    PortfolioResult
        Portfolio achieving target return with minimum variance
    """
    if isinstance(expected_returns, pd.Series):
        tickers = expected_returns.index.tolist()
        exp_ret = expected_returns.values
    else:
        exp_ret = np.array(expected_returns)
        tickers = [f"Asset{i+1}" for i in range(len(exp_ret))]
    
    if isinstance(cov_matrix, pd.DataFrame):
        cov = cov_matrix.values
    else:
        cov = np.array(cov_matrix)
    
    n_assets = len(exp_ret)
    x0 = np.ones(n_assets) / n_assets
    
    constraints = [
        {"type": "eq", "fun": lambda w: np.sum(w) - 1.0},
        {"type": "eq", "fun": lambda w: _portfolio_return(w, exp_ret) - target_return},
    ]
    bounds = [(-1.0, 1.0)] * n_assets if allow_short else [(0.0, 1.0)] * n_assets
    
    result = minimize(
        lambda w: _portfolio_volatility(w, cov),
        x0,
        method="SLSQP",
        bounds=bounds,
        constraints=constraints,
    )
    
    if not result.success:
        raise ValidationError(
            f"Cannot achieve target return {target_return:.2%}. "
            "Try allowing short selling or adjusting the target."
        )
    
    weights = result.x
    ret = _portfolio_return(weights, exp_ret)
    vol = _portfolio_volatility(weights, cov)
    sharpe = ret / vol if vol > 0 else 0.0
    
    return PortfolioResult(
        weights=weights,
        expected_return=ret,
        volatility=vol,
        sharpe_ratio=sharpe,
        tickers=tickers,
        method=f"Target Return ({target_return:.2%})",
    )


def efficient_frontier(
    expected_returns: pd.Series | np.ndarray,
    cov_matrix: pd.DataFrame | np.ndarray,
    n_points: int = 50,
    allow_short: bool = False,
) -> list[PortfolioResult]:
    """
    Construct the efficient frontier.

    Parameters
    ----------
    n_points : int
        Number of portfolios to compute along the frontier

    Returns
    -------
    list[PortfolioResult]
        Portfolios from minimum variance to maximum return
    """
    if isinstance(expected_returns, pd.Series):
        exp_ret = expected_returns.values
    else:
        exp_ret = np.array(expected_returns)
    
    # Find min and max achievable returns
    min_var = min_volatility_portfolio(expected_returns, cov_matrix, allow_short)
    
    # Max return portfolio: all weight on highest-return asset (if long-only)
    if allow_short:
        max_ret = exp_ret.max() * 2  # Arbitrary upper bound for short-allowed
    else:
        max_ret = exp_ret.max()
    
    target_returns = np.linspace(min_var.expected_return, max_ret, n_points)
    frontier = []
    
    for target in target_returns:
        try:
            portfolio = target_return_portfolio(
                expected_returns, cov_matrix, target, allow_short
            )
            frontier.append(portfolio)
        except ValidationError:
            # Skip infeasible target returns
            continue
    
    return frontier