<> Markowitz mean variance model

* Markowitz mean - The variance model is a multi-objective optimization problem , The effective frontier is the solution of multi-objective optimization problem pareto solution ( Certain risk , Maximum benefit ; Certain income , Minimum risk )
* Markowitz model measures return by expected rate of return , Risk is measured by the variance of return rate
* Income and risk calculation function portstats
* Function syntax :PortRisk
* PortReturn=portstats(ExpReturn,ExpCovariance,PortWts)
* input :ExpReturn,ExpCovariance,PortWts: Expected return on assets , Covariance matrix of assets , Asset weight
* output :PortRisk,PortReturn: Portfolio risk ( standard deviation ), Expected return of portfolio ( expect )
* for example : Calculate the risk and return of the stock portfolio of three enterprises
* The code is as follows : import numpy as np def portstats(ExpReturn, ExpCovariance, PortWts):
PortRisk= 0 for i in range(len(PortWts)): for j in range(len(PortWts)): PortRisk
+= PortWts[i] * PortWts[j] * ExpCovariance[i * 3 + j] PortReturn = np.sum(
ExpReturn* np.array(PortWts)) return np.sqrt(PortRisk), PortReturn #
Expected rate of return of each security in the portfolio ExpReturn = [0.000540, 0.000275, 0.000236] # Covariance matrix of securities in portfolio
ExpCovariance= 0.0001 * np.array([5.27, 2.80, 1.74, 2.80, 4.26, 1.67, 1.74, 1.67
, 2.90]) ExpCovariance = ExpCovariance.tolist() # Initial weight of each security in the portfolio ( Initial investment amount )/ Initial total amount
PortWts= 1.0 / 3 * np.array([1, 1, 1]) PortWts = PortWts.tolist() #
call portstats function [PortRisk, PortReturn] = portstats(ExpReturn, ExpCovariance,
PortWts) print(PortRisk, PortReturn) # Calculation results
* give the result as follows :
risk ( standard deviation ) PortRisk= 0.016617
Portfolio yield PortReturn= 3.5033e-004

* Next, the effective frontier is calculated
* Effective frontier calculation function PortRisk,
PortReturn,PortWts=frontcon(ExpReturn,ExpCovariance,NumPorts,PortReturn,AssetBounds,
Groups,GroupBounds,varargin)
* ExpReturn,ExpCovariance Expected return on assets , Covariance matrix of assets
* NumPorts, Decimal of the output point on the effective leading edge , Default to 10
* AssetBounds Upper and lower limits of each asset weight
* Groups,GroupBounds,varargin Asset grouping , Constraints per asset group
* The code is as follows : import numpy as np import matplotlib.pyplot as plt # mapping import scipy.
optimizeas sco def frontcon(ExpReturn, ExpCovariance, NumPorts): noa = len(
ExpReturn) def statistics(weights): weights = np.array(weights) z = np.dot(
ExpCovariance, weights) x = np.dot(weights, z) port_returns = (np.sum(ExpReturn
* weights.T)) port_variance = np.sqrt(x) num1 = port_returns / port_variance
return np.array([port_returns, port_variance, num1]) # Define a function pair Minimize variance def
min_variance(weights): return statistics(weights)[1] bnds = tuple((0, 1) for x
in range(noa)) # At different target yield levels (target_returns) Cycle time , One of the minimized constraints will change . target_returns =
np.linspace(min(ExpReturn), max(ExpReturn), NumPorts) target_variance = []
PortWts= [] for tar in target_returns: # Two constraints are used in optimization ,1. Given target rate of return ,2. The sum of portfolio weights is 1. cons
= ({'type': 'eq', 'fun': lambda x: statistics(x)[0] - tar}, {'type': 'eq', 'fun'
: lambda x: np.sum(x) - 1}) res = sco.minimize(min_variance, noa * [1. / noa, ],
method='SLSQP', bounds=bnds, constraints=cons) target_variance.append(res['fun'
]) PortWts.append(res["x"]) target_variance = np.array(target_variance) return [
target_variance, target_returns, PortWts] ExpReturn = np.array([0.00540, 0.00275
, 0.00236]) ExpCovariance = 0.0001 * np.array([[5.27, 2.80, 1.74], [2.80, 4.26,
1.67], [1.74, 1.67, 2.90]]) NumPorts = 10 [target_variance, target_returns,
PortWts] = frontcon(ExpReturn, ExpCovariance, NumPorts) plt.plot(target_variance
, target_returns) plt.title("Mean-Variance-Efficient Frontier") plt.xlabel(
"Risk(Standard Deviation)") plt.ylabel("Expected Return") plt.show()
* The drawing is as follows :

* That's all for today's sharing , Learn more , Share frequently .

Technology