139 const Int_t nrStocks = 10;
140 static const Char_t *stocks[] =
141 {
"GE",
"SUNW",
"QCOM",
"BRCM",
"TYC",
"IBM",
"AMAT",
"C",
"PFE",
"HD"};
154 fDate = fVol = fOpen = fHigh = fLow = fClose = fCloseAdj = 0;
156 virtual ~TStockDaily() {}
171 TStockDaily *
data = 0;
179 for (
Int_t i = 0; i < nrEntries; i++) {
182 if (data->fDate >= sDay && data->fDate <= eDay)
183 closeAdj[i] = data->fCloseAdj/100.;
187 for (
Int_t i = 1; i < nrEntries; i++)
189 (*r)[i-1] = closeAdj[i]/closeAdj[i-1];
214 const Int_t nrVar = nrStocks;
215 const Int_t nrEqual = 1;
216 const Int_t nrInEqual = 0;
258 TQpDataDens *prob = (
TQpDataDens *)qp->
MakeData(c,Q,xlo,ixlo,xup,ixup,
A,
b,
C,clo,iclo,cup,icup);
270 const Int_t status = s->
Solve(prob,vars,resid);
274 delete qp;
delete prob;
delete vars;
delete resid;
delete s;
276 cout <<
"Could not solve this problem." <<endl;
287 const Int_t sDay = 20000809;
288 const Int_t eDay = 20040602;
290 const char *fname =
"stock.root";
297 printf(
"accessing %s file from http://root.cern.ch/files\n",fname);
303 for (
Int_t i = 0; i < nrStocks; i++) {
304 const TString symbol = stocks[i];
305 data[i] = StockReturn(f,symbol,sDay,eDay);
311 for (
Int_t i = 0; i < nrStocks; i++)
312 r[i] = data[i].GetSum()/nrData;
315 for (
Int_t i = 0; i < nrStocks; i++) {
316 for (
Int_t j = 0; j <= i; j++) {
318 for (
Int_t k = 0; k < nrData; k++) {
319 sum += (data[i][k] - r[i]) * (data[j][k] - r[j]);
321 Covar(i,j) = Covar(j,i) = sum/nrData;
325 const TVectorD weight1 = OptimalInvest(2.0,r,Covar);
326 const TVectorD weight2 = OptimalInvest(10.,r,Covar);
328 cout <<
"stock daily daily w1 w2" <<endl;
329 cout <<
"symb return sdv " <<endl;
330 for (
Int_t i = 0; i < nrStocks; i++)
331 printf(
"%s\t: %.3f %.3f %.3f %.3f\n",stocks[i],r[i],
TMath::Sqrt(Covar[i][i]),weight1[i],weight2[i]);
342 TF1 *
f1 =
new TF1(
"f1",RiskProfile,0,2.5,1);
350 TF1 *f2 =
new TF1(
"f2",RiskProfile,0,2.5,1);
356 legend1->
AddEntry(f1,
"1-exp(-2.0*x)",
"l");
357 legend1->
AddEntry(f2,
"1-exp(-10.*x)",
"l");
363 TH1F *
h1 =
new TH1F(
"h1",
"Portfolio Distribution",nrStocks,0,0);
364 TH1F *h2 =
new TH1F(
"h2",
"Portfolio Distribution",nrStocks,0,0);
372 for (
Int_t i = 0; i < nrStocks; i++) {
373 h1->
Fill(stocks[i],weight1[i]);
374 h2->
Fill(stocks[i],weight2[i]);
377 h1->
Draw(
"BAR2 HIST");
378 h2->
Draw(
"BAR2SAME HIST");
381 legend2->
AddEntry(h1,
"high risk",
"f");
382 legend2->
AddEntry(h2,
"low risk",
"f");
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
virtual void SetBarOffset(Float_t offset=0.25)
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
static long int sum(long int i)
virtual void SetMaximum(Double_t maximum=-1111)
This class displays a legend box (TPaveText) containing several legend entries.
static const TString & GetTutorialDir()
Get the tutorials directory in the installation. Static utility function.
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual void SetMinimum(Double_t minimum=-1111)
1-D histogram with a float per channel (see TH1 documentation)}
Array of floats (32 bits per element).
virtual TQpResidual * MakeResiduals(const TQpDataBase *data)
Setup the residuals.
virtual void SetYTitle(const char *title)
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
virtual void SetBarWidth(Float_t width=0.5)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
#define ClassDef(name, id)
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
TVectorT< Double_t > TVectorD
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
virtual void SetLineColor(Color_t lcolor)
Set the line color.
virtual Int_t Solve(TQpDataBase *prob, TQpVar *iterate, TQpResidual *resid)
Solve the quadratic programming problem as formulated through prob, store the final solution in itera...
R__EXTERN TSystem * gSystem
virtual void Draw(Option_t *option="")
Draw this histogram with options.
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
char * Form(const char *fmt,...)
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
static constexpr double s
virtual Long64_t GetEntries() const
virtual void SetXTitle(const char *title)
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
A TTree object has a header with a name and a title.
virtual TH1 * GetHistogram() const
Return a pointer to the histogram used to visualise the function.
virtual void SetParameter(Int_t param, Double_t value)
Double_t Sqrt(Double_t x)
virtual TQpDataBase * MakeData(Double_t *c, Double_t *Q, Double_t *xlo, Bool_t *ixlo, Double_t *xup, Bool_t *ixup, Double_t *A, Double_t *bA, Double_t *C, Double_t *clo, Bool_t *iclo, Double_t *cup, Bool_t *icup)
Setup the data.
A TTree is a list of TBranches.
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
virtual TQpVar * MakeVariables(const TQpDataBase *data)
Setup the variables.