139const Int_t nrStocks = 10;
140static 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";
292 if (!
gSystem->AccessPathName(fname)) {
297 printf(
"accessing %s file from http://root.cern/files\n",fname);
304 for (
Int_t i = 0;
i < nrStocks;
i++) {
306 data[
i] = StockReturn(
f,symbol,sDay,eDay);
312 for (
Int_t i = 0;
i < nrStocks;
i++)
313 r[
i] =
data[
i].GetSum()/nrData;
316 for (
Int_t i = 0;
i < nrStocks;
i++) {
317 for (
Int_t j = 0; j <=
i; j++) {
319 for (
Int_t k = 0; k < nrData; k++) {
322 Covar(
i,j) = Covar(j,
i) =
sum/nrData;
326 const TVectorD weight1 = OptimalInvest(2.0,
r,Covar);
327 const TVectorD weight2 = OptimalInvest(10.,
r,Covar);
329 cout <<
"stock daily daily w1 w2" <<endl;
330 cout <<
"symb return sdv " <<endl;
331 for (
Int_t i = 0;
i < nrStocks;
i++)
332 printf(
"%s\t: %.3f %.3f %.3f %.3f\n",stocks[
i],
r[
i],
TMath::Sqrt(Covar[
i][
i]),weight1[
i],weight2[
i]);
343 TF1 *
f1 =
new TF1(
"f1",RiskProfile,0,2.5,1);
344 f1->SetParameter(0,2.0);
345 f1->SetLineColor(49);
347 f1->GetHistogram()->SetXTitle(
"dollar");
348 f1->GetHistogram()->SetYTitle(
"utility");
349 f1->GetHistogram()->SetMinimum(0.0);
350 f1->GetHistogram()->SetMaximum(1.0);
351 TF1 *f2 =
new TF1(
"f2",RiskProfile,0,2.5,1);
358 legend1->
AddEntry(f2,
"1-exp(-10.*x)",
"l");
364 TH1F *
h1 =
new TH1F(
"h1",
"Portfolio Distribution",nrStocks,0,0);
365 TH1F *
h2 =
new TH1F(
"h2",
"Portfolio Distribution",nrStocks,0,0);
367 h1->SetFillColor(49);
368 h2->SetFillColor(50);
369 h1->SetBarWidth(0.45);
370 h1->SetBarOffset(0.1);
371 h2->SetBarWidth(0.4);
372 h2->SetBarOffset(0.55);
373 for (
Int_t i = 0;
i < nrStocks;
i++) {
374 h1->Fill(stocks[
i],weight1[
i]);
375 h2->Fill(stocks[
i],weight2[
i]);
378 h1->Draw(
"BAR2 HIST");
379 h2->Draw(
"BAR2SAME HIST");
383 legend2->
AddEntry(h2,
"low risk",
"f");
#define ClassDef(name, id)
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
TMatrixTSym< Double_t > TMatrixDSym
TMatrixT< Double_t > TMatrixD
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
R__EXTERN TSystem * gSystem
TVectorT< Double_t > TVectorD
Array of floats (32 bits per element).
virtual void SetLineColor(Color_t lcolor)
Set the line color.
A TTree is a list of TBranches.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
void Draw(Option_t *option="") override
Draw this function with its current attributes.
virtual void SetParameter(Int_t param, Double_t value)
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
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.
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)
This class displays a legend box (TPaveText) containing several legend entries.
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Data for the dense QP formulation.
dense matrix problem formulation
TQpVar * MakeVariables(const TQpDataBase *data) override
Setup the variables.
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.
TQpResidual * MakeResiduals(const TQpDataBase *data) override
Setup the residuals.
The Residuals class calculates and stores the quantities that appear on the right-hand side of the li...
Class containing the variables for the general QP formulation.
static const TString & GetTutorialDir()
Get the tutorials directory in the installation. Static utility function.
A TTree represents a columnar dataset.
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
virtual Long64_t GetEntries() const
constexpr Double_t C()
Velocity of light in .
Double_t Exp(Double_t x)
Returns the base-e exponential function of x, which is e raised to the power x.
Double_t Sqrt(Double_t x)
Returns the square root of x.
static uint64_t sum(uint64_t i)