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++) {
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)) {
301 for (
Int_t i = 0; i < nrStocks; i++) {
302 const TString symbol = stocks[i];
306 const Int_t nrData = data[0].GetSize();
309 for (
Int_t i = 0; i < nrStocks; i++)
310 r[i] = data[i].GetSum()/nrData;
313 for (
Int_t i = 0; i < nrStocks; i++) {
314 for (
Int_t j = 0; j <= i; j++) {
316 for (
Int_t k = 0; k < nrData; k++) {
317 sum += (data[i][k] -
r[i]) * (data[j][k] -
r[j]);
319 Covar(i,j) = Covar(j,i) =
sum/nrData;
323 const TVectorD weight1 = OptimalInvest(2.0,
r,Covar);
324 const TVectorD weight2 = OptimalInvest(10.,
r,Covar);
326 cout <<
"stock daily daily w1 w2" <<endl;
327 cout <<
"symb return sdv " <<endl;
328 for (
Int_t i = 0; i < nrStocks; i++)
329 printf(
"%s\t: %.3f %.3f %.3f %.3f\n",stocks[i],
r[i],
TMath::Sqrt(Covar[i][i]),weight1[i],weight2[i]);
340 TF1 *
f1 =
new TF1(
"f1",RiskProfile,0,2.5,1);
341 f1->SetParameter(0,2.0);
342 f1->SetLineColor(49);
344 f1->GetHistogram()->SetXTitle(
"dollar");
345 f1->GetHistogram()->SetYTitle(
"utility");
346 f1->GetHistogram()->SetMinimum(0.0);
347 f1->GetHistogram()->SetMaximum(1.0);
348 TF1 *f2 =
new TF1(
"f2",RiskProfile,0,2.5,1);
355 legend1->
AddEntry(f2,
"1-exp(-10.*x)",
"l");
361 TH1F *
h1 =
new TH1F(
"h1",
"Portfolio Distribution",nrStocks,0,0);
362 TH1F *h2 =
new TH1F(
"h2",
"Portfolio Distribution",nrStocks,0,0);
364 h1->SetFillColor(49);
366 h1->SetBarWidth(0.45);
367 h1->SetBarOffset(0.1);
370 for (
Int_t i = 0; i < nrStocks; i++) {
371 h1->Fill(stocks[i],weight1[i]);
372 h2->
Fill(stocks[i],weight2[i]);
375 h1->Draw(
"BAR2 HIST");
376 h2->
Draw(
"BAR2SAME HIST");
380 legend2->
AddEntry(h2,
"low risk",
"f");
int Int_t
Signed integer 4 bytes (int).
char Char_t
Character 1 byte (char).
double Double_t
Double 8 bytes.
#define ClassDef(name, id)
TMatrixTSym< Double_t > TMatrixDSym
TMatrixT< Double_t > TMatrixD
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
TVectorT< Double_t > TVectorD
Array of floats (32 bits per element).
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
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
Default Draw method for all objects.
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)
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
void Draw(Option_t *option="") override
Draw this histogram with options.
virtual void SetBarWidth(Float_t width=0.5)
Set the width of bars as fraction of the bin width for drawing mode "B".
virtual void SetBarOffset(Float_t offset=0.25)
Set the bar offset as fraction of the bin width for drawing mode "B".
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
void Draw(Option_t *option="") override
Draw this box 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 Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr, TClass *realClass, EDataType datatype, bool isptr, bool suppressMissingBranchError)
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
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)