File size: 5,233 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
"""
Portfolio optimizer - convenience wrapper module.

This module re-exports the optimization functionality for cleaner imports.
"""

from quantlib_pro.portfolio.optimization import (
    PortfolioResult,
    max_sharpe_portfolio,
    min_volatility_portfolio,
    target_return_portfolio,
    efficient_frontier,
)

__all__ = [
    "PortfolioOptimizer",
    "PortfolioResult",
    "max_sharpe_portfolio",
    "min_volatility_portfolio",
    "target_return_portfolio",
    "efficient_frontier",
]


class PortfolioOptimizer:
    """
    Portfolio optimization class using Modern Portfolio Theory.
    
    Provides methods for:
    - Maximum Sharpe ratio optimization
    - Minimum variance optimization
    - Target return/volatility portfolios
    - Efficient frontier construction
    
    Parameters
    ----------
    expected_returns : np.ndarray
        Expected returns for each asset
    cov_matrix : np.ndarray
        Covariance matrix of asset returns
    risk_free_rate : float, optional
        Risk-free rate for Sharpe ratio calculation (default: 0.02)
    tickers : list[str], optional
        Asset ticker symbols
    """
    
    def __init__(
        self,
        expected_returns,
        cov_matrix,
        risk_free_rate=0.02,
        tickers=None
    ):
        import numpy as np
        import pandas as pd
        
        # Store as pandas Series to preserve tickers
        if isinstance(expected_returns, pd.Series):
            self.expected_returns = expected_returns
            self.tickers = expected_returns.index.tolist()
        else:
            exp_ret_array = np.asarray(expected_returns)
            if tickers is None:
                tickers = [f"Asset_{i+1}" for i in range(len(exp_ret_array))]
            self.tickers = list(tickers)
            self.expected_returns = pd.Series(exp_ret_array, index=self.tickers)
        
        # Store as pandas DataFrame to preserve structure
        if isinstance(cov_matrix, pd.DataFrame):
            self.cov_matrix = cov_matrix
        else:
            cov_array = np.asarray(cov_matrix)
            self.cov_matrix = pd.DataFrame(cov_array, index=self.tickers, columns=self.tickers)
        
        self.risk_free_rate = risk_free_rate
        self.n_assets = len(self.expected_returns)
    
    def max_sharpe(self, allow_short=False):
        """Optimize for maximum Sharpe ratio."""
        return max_sharpe_portfolio(
            self.expected_returns,
            self.cov_matrix,
            risk_free_rate=self.risk_free_rate,
            allow_short=allow_short
        )
    
    def min_variance(self, allow_short=False):
        """Optimize for minimum variance."""
        return min_volatility_portfolio(
            self.expected_returns,
            self.cov_matrix,
            allow_short=allow_short
        )
    
    def target_return(self, target_return, allow_short=False):
        """
        Optimize for minimum variance at a target return.
        
        Parameters
        ----------
        target_return : float
            Target expected return (e.g., 0.10 for 10%)
        allow_short : bool
            Allow short selling
        """
        return target_return_portfolio(
            self.expected_returns,
            self.cov_matrix,
            target_return=target_return,
            allow_short=allow_short
        )
    
    def target_volatility(self, target_vol, allow_short=False):
        """
        Optimize for maximum return at a target volatility.
        
        NOTE: This feature is not directly implemented in the underlying
        optimization module. Using target_return as workaround.
        
        Parameters
        ----------
        target_vol : float
            Target volatility (e.g., 0.15 for 15%)
        allow_short : bool
            Allow short selling
        """
        # Estimate a return target based on current portfolio metrics
        # This is a workaround since target_volatility_portfolio doesn't exist
        min_var = self.min_variance(allow_short=allow_short)
        
        if target_vol < min_var.volatility:
            raise ValueError(
                f"Target volatility {target_vol:.2%} is below minimum "
                f"achievable volatility {min_var.volatility:.2%}"
            )
        
        # Use binary search to find portfolio with approximately target volatility
        frontier = self.efficient_frontier(n_points=100, allow_short=allow_short)
        
        # Find portfolio closest to target volatility
        closest = min(frontier, key=lambda p: abs(p.volatility - target_vol))
        return closest
    
    def efficient_frontier(self, n_points=50, allow_short=False):
        """
        Compute the efficient frontier.
        
        Parameters
        ----------
        n_points : int
            Number of points on the frontier
        allow_short : bool
            Allow short selling
        
        Returns
        -------
        list[PortfolioResult]
            Portfolios along the efficient frontier
        """
        return efficient_frontier(
            self.expected_returns,
            self.cov_matrix,
            n_points=n_points,
            allow_short=allow_short
        )