Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
portfolio.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_quadp
3/// \notebook
4/// This macro shows in detail the use of the quadratic programming package quadp .
5/// Running this macro :
6///
7/// ~~~{.cpp}
8/// .x portfolio.C+
9/// ~~~
10///
11/// or
12///
13/// ~~~{.cpp}
14/// gSystem->Load("libQuadp");
15/// .L portFolio.C+; portfolio()
16/// ~~~
17///
18/// Let's first review what we exactly mean by "quadratic programming" :
19///
20/// We want to minimize the following objective function :
21///
22/// \f$ c^T x + ( 1/2 ) x^T Q x \f$ wrt. the vector \f$ x \f$
23///
24/// \f$ c \f$ is a vector and \f$ Q \f$ a symmetric positive definite matrix
25///
26/// You might wonder what is so special about this objective which is quadratic in
27/// the unknowns, that can not be done by Minuit/Fumili . Well, we have in addition
28/// the following boundary conditions on \f$ x \f$:
29///
30/// \f[
31/// A x = b \\
32/// clo \le C x \le cup \\
33/// xlo \le x \le xup
34/// \f] where A and C are arbitrary matrices and the rest are vectors
35///
36/// Not all these constraints have to be defined . Our example will only use \f$ xlo \f$,
37/// \f$ A \f$ and \f$ b \f$
38/// Still, this could be handled by a general non-linear minimizer like Minuit by introducing
39/// so-called "slack" variables . However, quadp is tailored to objective functions not more
40/// complex than being quadratic . This allows usage of solving techniques which are even
41/// stable for problems involving for instance 500 variables, 100 inequality conditions
42/// and 50 equality conditions .
43///
44/// Enough said about quadratic programming, let's return to our example .
45/// Suppose, after a long day of doing physics, you have a look at your investments and
46/// realize that an early retirement is not possible, given the returns of your stocks .
47/// So what now ? ROOT to the rescue ...
48///
49/// In 1990 Harry Markowitz was awarded the Nobel prize for economics: " his work provided new tools
50/// for weighing the risks and rewards of different investments and for valuing corporate stocks and bonds" .
51/// In plain English, he developed the tools to balance greed and fear, we want the maximum return
52/// with the minimum amount of risk. Our stock portfolio should be at the
53/// ["Efficient Frontier"](see http://www.riskglossary.com/articles/efficient_frontier.htm).
54/// To quantify better the risk we are willing to take, we define a utility function \f$ U(x) \f$. It describes
55/// as a function of our total assets \f$ x \f$, our "satisfaction" . A common choice is \f$ 1-exp(-k*x) \f$ (the reason for
56/// the exponent will be clear later) . The parameter \f$ k \f$ is the risk-aversion factor . For small values of \f$ k \f$
57/// the satisfaction is small for small values of \f$ x \f$; by increasing \f$ x \f$ the satisfaction can still be increased
58/// significantly . For large values of \f$ k \f$, \f$ U(x) \f$ increases rapidly to 1, there is no increase in satisfaction
59/// for additional dollars earned .
60///
61/// In summary :
62/// - small \f$ k \f$ ==> risk-loving investor
63/// - large \f$ k \f$ ==> risk-averse investor
64///
65/// Suppose we have for nrStocks the historical daily returns \f$ r = closing_price(n) - closing_price(n-1) \f$.
66/// Define a vector \f$ x \f$ of length of \f$ nrStocks \f$, which contains the fraction of our money invested in
67/// each stock . We can calculate the average daily return \f$ z \f$ of our portfolio and its variance using
68/// the portfolio covariance Covar :
69///
70/// \f$ z = r^T x \f$ and \f$ var = x^T Covar x \f$
71///
72/// Assuming that the daily returns have a Normal distribution, \f$ N(x) \f$, so will \f$ z \f$ with mean \f$ r^T x \f$
73/// and variance \f$ x^T Covar x \f$
74///
75/// The expected value of the utility function is :
76///
77/// \f[
78/// E(u(x)) = Int (1-exp(-k*x) N(x) dx \\
79/// = 1-exp(-k (r^T x - 0.5 k x^T Covar x) ) \\
80/// \f]
81///
82/// Its value is maximised by maximising \f$ r^T x -0.5 k x^T Covar x \f$
83/// under the condition \f$ sum (x_i) = 1 \f$, meaning we want all our money invested and
84/// \f$ x_i \ge 0 \f$, we can not "short" a stock
85///
86/// For 10 stocks we got the historical daily data for Sep-2000 to Jun-2004:
87///
88/// - GE : General Electric Co
89/// - SUNW : Sun Microsystems Inc
90/// - QCOM : Qualcomm Inc
91/// - BRCM : Broadcom Corp
92/// - TYC : Tyco International Ltd
93/// - IBM : International Business Machines Corp
94/// - AMAT : Applied Materials Inc
95/// - C : Citigroup Inc
96/// - PFE : Pfizer Inc
97/// - HD : Home Depot Inc
98///
99/// We calculate the optimal portfolio for 2.0 and 10.0 .
100///
101/// Food for thought :
102/// - We assumed that the stock returns have a Normal distribution . Check this assumption by
103/// histogramming the stock returns !
104/// - We used for the expected return in the objective function, the flat average over a time
105/// period . Investment firms will put significant resources in improving the return prediction .
106/// - If you want to trade significant number of shares, several other considerations have
107/// to be taken into account :
108/// + If you are going to buy, you will drive the price up (so-called "slippage") .
109/// This can be taken into account by adding terms to the objective
110/// (Google for "slippage optimization")
111/// + FTC regulations might have to be added to the inequality constraints
112/// - Investment firms do not want to be exposed to the "market" as defined by a broad
113/// index like the S&P and "hedge" this exposure away . A perfect hedge this can be added
114/// as an equality constrain, otherwise add an inequality constrain .
115///
116/// \macro_image
117/// \macro_output
118/// \macro_code
119///
120/// \author Eddy Offermann
121
122#include "TCanvas.h"
123#include "TFile.h"
124#include "TMath.h"
125#include "TTree.h"
126#include "TArrayF.h"
127#include "TH1.h"
128#include "TF1.h"
129#include "TLegend.h"
130#include "TSystem.h"
131
132#include "TMatrixD.h"
133#include "TMatrixDSym.h"
134#include "TVectorD.h"
135#include "TQpProbDens.h"
136#include "TGondzioSolver.h"
137
138#include <iostream>
139
140const Int_t nrStocks = 10;
141static const Char_t *stocks[] =
142 {"GE","SUNW","QCOM","BRCM","TYC","IBM","AMAT","C","PFE","HD"};
143
144class TStockDaily {
145public:
146 Int_t fDate;
147 Int_t fOpen; // 100*open_price
148 Int_t fHigh; // 100*high_price
149 Int_t fLow; // 100*low_price
150 Int_t fClose; // 100*close_price
151 Int_t fVol;
152 Int_t fCloseAdj; // 100*close_price adjusted for splits and dividend
153
154 TStockDaily() {
155 fDate = fVol = fOpen = fHigh = fLow = fClose = fCloseAdj = 0;
156 }
157 virtual ~TStockDaily() {}
158
160};
161
162//---------------------------------------------------------------------------
164 Double_t riskFactor = par[0];
165 return 1-TMath::Exp(-riskFactor*x[0]);
166}
167
168//---------------------------------------------------------------------------
170{
171 TTree *tDaily = (TTree*)f->Get(name);
172 TStockDaily *data = 0;
173 tDaily->SetBranchAddress("daily",&data);
174 TBranch *b_closeAdj = tDaily->GetBranch("fCloseAdj");
175 TBranch *b_date = tDaily->GetBranch("fDate");
176
177 //read only the "adjusted close" branch for all entries
178 const Int_t nrEntries = (Int_t)tDaily->GetEntries();
180 for (Int_t i = 0; i < nrEntries; i++) {
181 b_date->GetEntry(i);
182 b_closeAdj->GetEntry(i);
183 if (data->fDate >= sDay && data->fDate <= eDay)
184 closeAdj[i] = data->fCloseAdj/100.;
185 }
186
187 TArrayF *r = new TArrayF(nrEntries-1);
188 for (Int_t i = 1; i < nrEntries; i++)
189// (*r)[i-1] = closeAdj[i]-closeAdj[i-1];
190 (*r)[i-1] = closeAdj[i]/closeAdj[i-1];
191
192 return *r;
193}
194
195#ifndef __ROOTCLING__
196//---------------------------------------------------------------------------
198{
199// what the quadratic programming package will do:
200//
201// minimize c^T x + ( 1/2 ) x^T Q x
202// subject to A x = b
203// clo <= C x <= cup
204// xlo <= x <= xup
205// what we want :
206//
207// maximize c^T x - k ( 1/2 ) x^T Q x
208// subject to sum_x x_i = 1
209// 0 <= x_i
210
211 // We have nrStocks weights to determine,
212 // 1 equality- and 0 inequality- equations (the simple square boundary
213 // condition (xlo <= x <= xup) does not count)
214
215 const Int_t nrVar = nrStocks;
216 const Int_t nrEqual = 1;
217 const Int_t nrInEqual = 0;
218
219 // flip the sign of the objective function because we want to maximize
220 TVectorD c = -1.*r;
221 TMatrixDSym Q = riskFactor*Covar;
222
223 // equality equation
224 TMatrixD A(nrEqual,nrVar); A = 1;
225 TVectorD b(nrEqual); b = 1;
226
227 // inequality equation
228 //
229 // - although not applicable in the current situation since nrInEqual = 0, one
230 // has to specify not only clo and cup but also an index vector iclo and icup,
231 // whose values are either 0 or 1 . If iclo[j] = 1, the lower boundary condition
232 // is active on x[j], etc. ...
233
239
240 // simple square boundary condition : 0 <= x_i, so only xlo is relevant .
241 // Like for clo and cup above, we have to define an index vector ixlo and ixup .
242 // Since each variable has the lower boundary, we can set the whole vector
243 // ixlo = 1
244
245 TVectorD xlo (nrVar); xlo = 0;
246 TVectorD xup (nrVar); xup = 0;
247 TVectorD ixlo(nrVar); ixlo = 1;
248 TVectorD ixup(nrVar); ixup = 0;
249
250 // setup the quadratic programming problem . Since a small number of variables are
251 // involved and "Q" has everywhere entries, we chose the dense version "TQpProbDens" .
252 // In case of a sparse formulation, simply replace all "Dens" by "Sparse" below and
253 // use TMatrixDSparse instead of TMatrixDSym and TMatrixD
254
256
257 // stuff all the matrices/vectors defined above in the proper places
258
259 TQpDataDens *prob = (TQpDataDens *)qp->MakeData(c,Q,xlo,ixlo,xup,ixup,A,b,C,clo,iclo,cup,icup);
260
261 // setup the nrStock variables, vars->fX will contain the final solution
262
263 TQpVar *vars = qp->MakeVariables(prob);
264 TQpResidual *resid = qp->MakeResiduals(prob);
265
266 // Now we have to choose the method of solving, either TGondzioSolver or TMehrotraSolver
267 // The Gondzio method is more sophisticated and therefore numerically more involved
268 // If one want the Mehrotra method, simply replace "Gondzio" by "Mehrotra" .
269
271 const Int_t status = s->Solve(prob,vars,resid);
272
273 const TVectorD weight = vars->fX;
274
275 delete qp; delete prob; delete vars; delete resid; delete s;
276 if (status != 0) {
277 cout << "Could not solve this problem." <<endl;
278 return TVectorD(nrStocks);
279 }
280
281 return weight;
282}
283#endif
284
285 //---------------------------------------------------------------------------
286void portfolio()
287{
288 const Int_t sDay = 20000809;
289 const Int_t eDay = 20040602;
290
291 const char *fname = "stock.root";
292 TFile *f = 0;
295 } else if (!gSystem->AccessPathName(Form("%s/quadp/%s", TROOT::GetTutorialDir().Data(), fname))) {
296 f = TFile::Open(Form("%s/quadp/%s", TROOT::GetTutorialDir().Data(), fname));
297 }
298
299 if (!f) return;
300
302 for (Int_t i = 0; i < nrStocks; i++) {
303 const TString symbol = stocks[i];
305 }
306
307 const Int_t nrData = data[0].GetSize();
308
310 for (Int_t i = 0; i < nrStocks; i++)
311 r[i] = data[i].GetSum()/nrData;
312
313 TMatrixDSym Covar(nrStocks);
314 for (Int_t i = 0; i < nrStocks; i++) {
315 for (Int_t j = 0; j <= i; j++) {
316 Double_t sum = 0.;
317 for (Int_t k = 0; k < nrData; k++) {
318 sum += (data[i][k] - r[i]) * (data[j][k] - r[j]);
319 }
320 Covar(i,j) = Covar(j,i) = sum/nrData;
321 }
322 }
323
324 const TVectorD weight1 = OptimalInvest(2.0,r,Covar);
325 const TVectorD weight2 = OptimalInvest(10.,r,Covar);
326
327 cout << "stock daily daily w1 w2" <<endl;
328 cout << "symb return sdv " <<endl;
329 for (Int_t i = 0; i < nrStocks; i++)
330 printf("%s\t: %.3f %.3f %.3f %.3f\n",stocks[i],r[i],TMath::Sqrt(Covar[i][i]),weight1[i],weight2[i]);
331
332 TCanvas *c1 = new TCanvas("c1","Portfolio Optimizations",10,10,800,900);
333 c1->Divide(1,2);
334
335 // utility function / risk profile
336
337 c1->cd(1);
338 gPad->SetGridx();
339 gPad->SetGridy();
340
341 TF1 *f1 = new TF1("f1",RiskProfile,0,2.5,1);
342 f1->SetParameter(0,2.0);
343 f1->SetLineColor(49);
344 f1->Draw("AC");
345 f1->GetHistogram()->SetXTitle("dollar");
346 f1->GetHistogram()->SetYTitle("utility");
347 f1->GetHistogram()->SetMinimum(0.0);
348 f1->GetHistogram()->SetMaximum(1.0);
349 TF1 *f2 = new TF1("f2",RiskProfile,0,2.5,1);
350 f2->SetParameter(0,10.);
351 f2->SetLineColor(50);
352 f2->Draw("CSAME");
353
354 TLegend *legend1 = new TLegend(0.50,0.65,0.70,0.82);
355 legend1->AddEntry(f1,"1-exp(-2.0*x)","l");
356 legend1->AddEntry(f2,"1-exp(-10.*x)","l");
357 legend1->Draw();
358
359 // vertical bar chart of portfolio distribution
360
361 c1->cd(2);
362 TH1F *h1 = new TH1F("h1","Portfolio Distribution",nrStocks,0,0);
363 TH1F *h2 = new TH1F("h2","Portfolio Distribution",nrStocks,0,0);
364 h1->SetStats(0);
365 h1->SetFillColor(49);
366 h2->SetFillColor(50);
367 h1->SetBarWidth(0.45);
368 h1->SetBarOffset(0.1);
369 h2->SetBarWidth(0.4);
370 h2->SetBarOffset(0.55);
371 for (Int_t i = 0; i < nrStocks; i++) {
372 h1->Fill(stocks[i],weight1[i]);
373 h2->Fill(stocks[i],weight2[i]);
374 }
375
376 h1->Draw("BAR2 HIST");
377 h2->Draw("BAR2SAME HIST");
378
379 TLegend *legend2 = new TLegend(0.50,0.65,0.70,0.82);
380 legend2->AddEntry(h1,"high risk","f");
381 legend2->AddEntry(h2,"low risk","f");
382 legend2->Draw();
383}
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:52
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
#define ClassDef(name, id)
Definition Rtypes.h:343
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
char name[80]
Definition TGX11.cxx:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2571
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
TVectorT< Double_t > TVectorD
Definition TVectorDfwd.h:23
#define gPad
Array of floats (32 bits per element).
Definition TArrayF.h:27
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
A TTree is a list of TBranches.
Definition TBranch.h:93
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:182
virtual TH1 * GetHistogram() const
Return a pointer to the histogram used to visualise the function Note that this histogram is managed ...
Definition TF1.cxx:1635
void Draw(Option_t *option="") override
Draw this function with its current attributes.
Definition TF1.cxx:1341
virtual void SetParameter(Int_t param, Double_t value)
Definition TF1.h:608
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3801
Derived class of TQpSolverBase implementing Gondzio-correction version of Mehrotra's original predict...
Int_t Solve(TQpDataBase *prob, TQpVar *iterate, TQpResidual *resid) override
Solve the quadratic programming problem as formulated through prob, store the final solution in itera...
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
virtual void SetBarOffset(Float_t offset=0.25)
Set the bar offset as fraction of the bin width for drawing mode "B".
Definition TH1.h:612
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3489
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3193
virtual void SetBarWidth(Float_t width=0.5)
Set the width of bars as fraction of the bin width for drawing mode "B".
Definition TH1.h:613
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9223
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
Data for the dense QP formulation.
Definition TQpDataDens.h:63
dense matrix problem formulation
Definition TQpProbDens.h:61
The Residuals class calculates and stores the quantities that appear on the right-hand side of the li...
Definition TQpResidual.h:62
Class containing the variables for the general QP formulation.
Definition TQpVar.h:60
TVectorD fX
Definition TQpVar.h:91
static const TString & GetTutorialDir()
Get the tutorials directory in the installation. Static utility function.
Definition TROOT.cxx:3459
Basic string class.
Definition TString.h:137
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1311
A TTree represents a columnar dataset.
Definition TTree.h:89
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5
TF1 * f1
Definition legend1.C:11
constexpr Double_t C()
Velocity of light in .
Definition TMath.h:117
Double_t Exp(Double_t x)
Returns the base-e exponential function of x, which is e raised to the power x.
Definition TMath.h:722
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:675
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2335