The book, The Leverage Space Trading Model by RALPH VINCE
Mostly these are only organizing data, listing statistics and producing data tables. Please focusing on computing HPR for multiple financial instruments and summing them together.
This case study is long, I better say more on few things. 1.Put each instrument data into 5 bins, compute bin average. Combination of instrument bins gives us joint scenarios and a joint probability distribution. 2.HPR is just 1 plus return. Joint HPR or Net HPR is just 1 plus sum all instruments return. 3.GHPR is considered as mean of HPRs, but it is geometrical. Probability weighting is done by exponential operation, and then instead of summation we use production.
A=c(47,9,78,136,-38,-68,70,91,-108,-30,-15,2,22)
B=c(448,300,-200,321,-735,-73,26,48,122,-75,-207,30,269)
C=c(381,799,547,283,57,317,140,-325,429,121,-393,623,242)
c(min(A),max(A),diff(range(A)))
## [1] -108 136 244
c(min(B),max(B),diff(range(B)))
## [1] -735 448 1183
c(min(C),max(C),diff(range(C)))
## [1] -393 799 1192
c(diff(range(A))/3,diff(range(B))/3,diff(range(C))/3)
## [1] 81.33333 394.33333 397.33333
binning<-function(seq,num_bins){
mid_bins=num_bins-2
breaks=0:mid_bins
breaks=round((max(seq)-min(seq))/mid_bins*(0:mid_bins)+min(seq),2)
group_tags <- cut(seq,
breaks=breaks,
include.lowest=FALSE,
dig.lab=5,
right=FALSE)
min_tag=paste0("< ",min(seq))
max_tag=paste0(max(seq)," >")
levels(group_tags)<-c(levels(group_tags),min_tag,max_tag)
group_tags[which(seq==max(seq))] <- max_tag
group_tags[which(seq==min(seq))] <- min_tag
res=data.frame(group_tags,seq)
return(res)
}
d1=binning(A,5)
d2=binning(B,5)
d3=binning(C,5)
d1
## group_tags seq
## 1 [-26.67,54.67) 47
## 2 [-26.67,54.67) 9
## 3 [54.67,136) 78
## 4 136 > 136
## 5 [-108,-26.67) -38
## 6 [-108,-26.67) -68
## 7 [54.67,136) 70
## 8 [54.67,136) 91
## 9 < -108 -108
## 10 [-108,-26.67) -30
## 11 [-26.67,54.67) -15
## 12 [-26.67,54.67) 2
## 13 [-26.67,54.67) 22
d2
## group_tags seq
## 1 448 > 448
## 2 [53.67,448) 300
## 3 [-340.67,53.67) -200
## 4 [53.67,448) 321
## 5 < -735 -735
## 6 [-340.67,53.67) -73
## 7 [-340.67,53.67) 26
## 8 [-340.67,53.67) 48
## 9 [53.67,448) 122
## 10 [-340.67,53.67) -75
## 11 [-340.67,53.67) -207
## 12 [-340.67,53.67) 30
## 13 [53.67,448) 269
d3
## group_tags seq
## 1 [4.33,401.67) 381
## 2 799 > 799
## 3 [401.67,799) 547
## 4 [4.33,401.67) 283
## 5 [4.33,401.67) 57
## 6 [4.33,401.67) 317
## 7 [4.33,401.67) 140
## 8 [-393,4.33) -325
## 9 [401.67,799) 429
## 10 [4.33,401.67) 121
## 11 < -393 -393
## 12 [401.67,799) 623
## 13 [4.33,401.67) 242
aggregate(d1$seq, list(d1$group_tags), FUN=mean)$x
## [1] -45.33333 13.00000 79.66667 -108.00000 136.00000
aggregate(d2$seq, list(d2$group_tags), FUN=mean)$x
## [1] -64.42857 253.00000 -735.00000 448.00000
aggregate(d3$seq, list(d3$group_tags), FUN=mean)$x
## [1] -325.0000 220.1429 533.0000 -393.0000 799.0000
aggregate(d1$seq, list(d1$group_tags), FUN=print)
## [1] -38 -68 -30
## [1] 47 9 -15 2 22
## [1] 78 70 91
## [1] -108
## [1] 136
## Group.1 x
## 1 [-108,-26.67) -38, -68, -30
## 2 [-26.67,54.67) 47, 9, -15, 2, 22
## 3 [54.67,136) 78, 70, 91
## 4 < -108 -108
## 5 136 > 136
aggregate(d2$seq, list(d2$group_tags), FUN=print)
## [1] -200 -73 26 48 -75 -207 30
## [1] 300 321 122 269
## [1] -735
## [1] 448
## Group.1 x
## 1 [-340.67,53.67) -200, -73, 26, 48, -75, -207, 30
## 2 [53.67,448) 300, 321, 122, 269
## 3 < -735 -735
## 4 448 > 448
aggregate(d3$seq, list(d3$group_tags), FUN=print)
## [1] -325
## [1] 381 283 57 317 140 121 242
## [1] 547 429 623
## [1] -393
## [1] 799
## Group.1 x
## 1 [-393,4.33) -325
## 2 [4.33,401.67) 381, 283, 57, 317, 140, 121, 242
## 3 [401.67,799) 547, 429, 623
## 4 < -393 -393
## 5 799 > 799
aggregate(d1$seq, list(d1$group_tags), FUN=length)$x/13
## [1] 0.23076923 0.38461538 0.23076923 0.07692308 0.07692308
aggregate(d2$seq, list(d2$group_tags), FUN=length)$x/13
## [1] 0.53846154 0.30769231 0.07692308 0.07692308
aggregate(d3$seq, list(d3$group_tags), FUN=length)$x/13
## [1] 0.07692308 0.53846154 0.23076923 0.07692308 0.07692308
commasplit<-function(x){
points=as.numeric(unlist(strsplit(x, split=",")))
mean(points)
}
mid_point<-function(x){
inn=x
inn=(gsub("^\\inn*[(]", "-", gsub("[$[<>)]", "", inn)))
sapply(inn,commasplit)
}
treat_missing<-function(x){
c2=aggregate(x$seq, list(x$group_tags), FUN=mean,drop=FALSE)
c2[which(is.na(c2[,2])),2]=mid_point(c2[which(is.na(c2[,2])),1])
c3=aggregate(x$seq, list(x$group_tags), FUN=length,drop=FALSE)
c3[which(is.na(c3[,2])),2] =0
c3[,2]=c3[,2]/13
merge(c3,c2,by="Group.1")
}
dist_marketa=treat_missing(d1)
dist_marketb=treat_missing(d2)
dist_marketc=treat_missing(d3)
dist_marketa
## Group.1 x.x x.y
## 1 [-108,-26.67) 0.23076923 -45.33333
## 2 [-26.67,54.67) 0.38461538 13.00000
## 3 [54.67,136) 0.23076923 79.66667
## 4 < -108 0.07692308 -108.00000
## 5 136 > 0.07692308 136.00000
dist_marketb
## Group.1 x.x x.y
## 1 [-340.67,53.67) 0.53846154 -64.42857
## 2 [-735,-340.67) 0.00000000 -537.83500
## 3 [53.67,448) 0.30769231 253.00000
## 4 < -735 0.07692308 -735.00000
## 5 448 > 0.07692308 448.00000
dist_marketc
## Group.1 x.x x.y
## 1 [-393,4.33) 0.07692308 -325.0000
## 2 [4.33,401.67) 0.53846154 220.1429
## 3 [401.67,799) 0.23076923 533.0000
## 4 < -393 0.07692308 -393.0000
## 5 799 > 0.07692308 799.0000
dist_marketa[,2]%*%dist_marketa[,3]
## [,1]
## [1,] 15.07692
dist_marketb[,2]%*%dist_marketb[,3]
## [,1]
## [1,] 21.07692
dist_marketc[,2]%*%dist_marketc[,3]
## [,1]
## [1,] 247.7692
dist_marketa[4,3]=-150
dist_marketb[4,3]=-1000
dist_marketc[4,3]=-500
dist_marketa
## Group.1 x.x x.y
## 1 [-108,-26.67) 0.23076923 -45.33333
## 2 [-26.67,54.67) 0.38461538 13.00000
## 3 [54.67,136) 0.23076923 79.66667
## 4 < -108 0.07692308 -150.00000
## 5 136 > 0.07692308 136.00000
dist_marketb
## Group.1 x.x x.y
## 1 [-340.67,53.67) 0.53846154 -64.42857
## 2 [-735,-340.67) 0.00000000 -537.83500
## 3 [53.67,448) 0.30769231 253.00000
## 4 < -735 0.07692308 -1000.00000
## 5 448 > 0.07692308 448.00000
dist_marketc
## Group.1 x.x x.y
## 1 [-393,4.33) 0.07692308 -325.0000
## 2 [4.33,401.67) 0.53846154 220.1429
## 3 [401.67,799) 0.23076923 533.0000
## 4 < -393 0.07692308 -500.0000
## 5 799 > 0.07692308 799.0000
dist_marketa[,2]%*%dist_marketa[,3]
## [,1]
## [1,] 11.84615
dist_marketb[,2]%*%dist_marketb[,3]
## [,1]
## [1,] 0.6923077
dist_marketc[,2]%*%dist_marketc[,3]
## [,1]
## [1,] 239.5385
d1.a=d1
d1.a$seq[which(d1$seq==min(A))]=-150
d2.a=d2
d2.a$seq[which(d2$seq==min(B))]=-1000
d3.a=d3
d3.a$seq[which(d3$seq==min(C))]=-500
d1.mean=aggregate(d1.a$seq, list(d1$group_tags), FUN=mean)
d2.mean=aggregate(d2.a$seq, list(d2$group_tags), FUN=mean)
d3.mean=aggregate(d3.a$seq, list(d3$group_tags), FUN=mean)
d1.a$seq=d1.mean[match(d1.a$group_tags,d1.mean$Group.1),2]
d2.a$seq=d2.mean[match(d2.a$group_tags,d2.mean$Group.1),2]
d3.a$seq=d3.mean[match(d3.a$group_tags,d3.mean$Group.1),2]
occurs=cbind(d1.a$seq,d2.a$seq,d3.a$seq)
jstable=aggregate(occurs, list(occurs[,1],occurs[,2],occurs[,3]), FUN=length)
jsp=jstable[,4]/13
jstable=cbind(round(jstable,2),jsp)
#joint scenario table
jstable=jstable[,c(1,2,3,7)]
jstable
## Group.1 Group.2 Group.3 jsp
## 1 13.00 -64.43 -500.00 0.07692308
## 2 79.67 -64.43 -325.00 0.07692308
## 3 -45.33 -1000.00 220.14 0.07692308
## 4 -45.33 -64.43 220.14 0.15384615
## 5 79.67 -64.43 220.14 0.07692308
## 6 13.00 253.00 220.14 0.07692308
## 7 136.00 253.00 220.14 0.07692308
## 8 13.00 448.00 220.14 0.07692308
## 9 13.00 -64.43 533.00 0.07692308
## 10 79.67 -64.43 533.00 0.07692308
## 11 -150.00 253.00 533.00 0.07692308
## 12 13.00 253.00 799.00 0.07692308
f.A=.1
f.B=.4
f.C=.25
BLA=min(jstable[,1])
BLB=min(jstable[,2])
BLC=min(jstable[,3])
PLA=jstable[,1]
PLB=jstable[,2]
PLC=jstable[,3]
returnA=f.A*PLA/abs(BLA)
returnB=f.B*PLB/abs(BLB)
returnC=f.C*PLC/abs(BLC)
table85=cbind(jstable,returnA,returnB,returnC)
table85
## Group.1 Group.2 Group.3 jsp returnA returnB returnC
## 1 13.00 -64.43 -500.00 0.07692308 0.008666667 -0.025772 -0.25000
## 2 79.67 -64.43 -325.00 0.07692308 0.053113333 -0.025772 -0.16250
## 3 -45.33 -1000.00 220.14 0.07692308 -0.030220000 -0.400000 0.11007
## 4 -45.33 -64.43 220.14 0.15384615 -0.030220000 -0.025772 0.11007
## 5 79.67 -64.43 220.14 0.07692308 0.053113333 -0.025772 0.11007
## 6 13.00 253.00 220.14 0.07692308 0.008666667 0.101200 0.11007
## 7 136.00 253.00 220.14 0.07692308 0.090666667 0.101200 0.11007
## 8 13.00 448.00 220.14 0.07692308 0.008666667 0.179200 0.11007
## 9 13.00 -64.43 533.00 0.07692308 0.008666667 -0.025772 0.26650
## 10 79.67 -64.43 533.00 0.07692308 0.053113333 -0.025772 0.26650
## 11 -150.00 253.00 533.00 0.07692308 -0.100000000 0.101200 0.26650
## 12 13.00 253.00 799.00 0.07692308 0.008666667 0.101200 0.39950
Net_HPR=1+returnA+returnB+returnC
Net_HPR_p=Net_HPR^table85$jsp
GHPR=prod(Net_HPR_p)
GHPR
## [1] 1.100491
f.A=.307
f.B=0
f.C=.693
returnA=f.A*PLA/abs(BLA)
returnB=f.B*PLB/abs(BLB)
returnC=f.C*PLC/abs(BLC)
Net_HPR=1+returnA+returnB+returnC
Net_HPR_p=Net_HPR^table85$jsp
GHPR=prod(Net_HPR_p)
GHPR
## [1] 1.248538
#f=|biggestlost|/$f
150/.307
## [1] 488.5993
500/.693
## [1] 721.5007
108/.304
## [1] 355.2632
393/.696
## [1] 564.6552