This macro shows in detail the use of the quadratic programming package quadp . 
Running this macro :
or
Let's first review what we exactly mean by "quadratic programming" :
We want to minimize the following objective function :
\( c^T x + ( 1/2 ) x^T Q x \) wrt. the vector \( x \)
\( c \) is a vector and \( Q \) a symmetric positive definite matrix
You might wonder what is so special about this objective which is quadratic in the unknowns, that can not be done by Minuit/Fumili . Well, we have in addition the following boundary conditions on \( x \):
\[ A x = b \\ clo \le C x \le cup \\ xlo \le x \le xup \]
where A and C are arbitrary matrices and the rest are vectors
Not all these constraints have to be defined . Our example will only use \( xlo \), \( A \) and \( b \) Still, this could be handled by a general non-linear minimizer like Minuit by introducing so-called "slack" variables . However, quadp is tailored to objective functions not more complex than being quadratic . This allows usage of solving techniques which are even stable for problems involving for instance 500 variables, 100 inequality conditions and 50 equality conditions .
Enough said about quadratic programming, let's return to our example . Suppose, after a long day of doing physics, you have a look at your investments and realize that an early retirement is not possible, given the returns of your stocks . So what now ? ROOT to the rescue ...
In 1990 Harry Markowitz was awarded the Nobel prize for economics: " his work provided new tools for weighing the risks and rewards of different investments and for valuing corporate stocks and bonds" . In plain English, he developed the tools to balance greed and fear, we want the maximum return with the minimum amount of risk. Our stock portfolio should be at the "Efficient Frontier". To quantify better the risk we are willing to take, we define a utility function \( U(x) \). It describes as a function of our total assets \( x \), our "satisfaction" . A common choice is \( 1-exp(-k*x) \) (the reason for the exponent will be clear later) . The parameter \( k \) is the risk-aversion factor . For small values of \( k \) the satisfaction is small for small values of \( x \); by increasing \( x \) the satisfaction can still be increased significantly . For large values of \( k \), \( U(x) \) increases rapidly to 1, there is no increase in satisfaction for additional dollars earned .
In summary :
Suppose we have for nrStocks the historical daily returns \( r = closing_price(n) - closing_price(n-1) \). Define a vector \( x \) of length of \( nrStocks \), which contains the fraction of our money invested in each stock . We can calculate the average daily return \( z \) of our portfolio and its variance using the portfolio covariance Covar :
\( z = r^T x \) and \( var = x^T Covar x \)
Assuming that the daily returns have a Normal distribution, \( N(x) \), so will \( z \) with mean \( r^T x \) and variance \( x^T Covar x \)
The expected value of the utility function is :
\[ E(u(x)) = Int (1-exp(-k*x) N(x) dx \\ = 1-exp(-k (r^T x - 0.5 k x^T Covar x) ) \\ \]
Its value is maximised by maximising \( r^T x -0.5 k x^T Covar x \) under the condition \( sum (x_i) = 1 \), meaning we want all our money invested and \( x_i \ge 0 \), we can not "short" a stock
For 10 stocks we got the historical daily data for Sep-2000 to Jun-2004:
We calculate the optimal portfolio for 2.0 and 10.0 .
Food for thought :
Definition in file portfolio.C.