[R-SIG-Finance] Parma Package QP optimization Failing and Ignoring Leverage Constraint

alexios ghalanos alexios at 4dscape.com
Tue Oct 1 12:49:22 CEST 2013


Nothing (almost) prevents you from doing what the blog post has done in
parma:
##################################################
library(parma)
# get it from r-forge which includes a fix for the eq.mat
# constraint in the QP case (works for Rev. 7)
library(quadprog)
data(etfdata)
library(timeSeries)
R = returns(etfdata)
RB = cbind(R, -R)
V = cov(R)
VV <- rbind( cbind(V,-V), cbind(-V,V) )
VV <- kronecker( matrix(c(1,-1,-1,1), 2), V )
# Make PD by truncation...
e <- eigen(VV)
VV <- e$vectors %*% diag(pmax(1e-12,e$values)) %*% t(e$vectors)
A = matrix(c(rep(1,15),rep(-1,15)), ncol=30)
# budget constraint
b <- 0
# budget constraint in the parmaspec then becomes leverage constraint
spec<-parmaspec(S=VV,forecast=as.numeric(c(100*abs(colMeans(R)),
-100*abs(colMeans(R)))), LB=rep(0,30),UB=rep(0.05,30), eq.mat=A, eqB=b,
budget=0.2,risk='EV',riskType='minrisk',target=0.0030346)

# or don't include the 'budget' constraint
# spec<-parmaspec(S=VV,forecast=as.numeric(c(100*abs(colMeans(R)),
#-100*abs(colMeans(R)))), LB=rep(0,30),UB=rep(0.05,30),
# budget=0.2,risk='EV',riskType='minrisk',target=0.0030346)

mysol <- parmasolve(spec, type='QP')
w = weights(mysol)[1:15] - weights(mysol)[16:30]
# round(w, 5)
0.00000 -0.01722  0.00000  0.00000  0.02495  0.01118  0.00567  0.01648
-0.02124  0.00953  0.00955 -0.04315 -0.01838  0.00413  0.01851
# sum(abs(w))
0.2
# sum(w)
-4.722557e-14
################################################
The solution or not will of course depend on the consistency of the
constraints placed (so no solution likely means inconsistent constraints).

Finally, this particular method of imposing the leverage constraint is
already partially alluded to in the vignette at the bottom of page 18
and a related paper. I am still unsure how stable the approach is (cases
of non PD matrix) and how important the assumptions are.


-Alexios

On 01/10/2013 07:23, Preston Li wrote:
> Thanks, sure you can use NLP.  
> 
> However, in case you are interested a leverage constraint can be handled
> in QP by rewriting the vector to solve for as the sum of a positive and
> negative part. Please
> see http://zoonek.free.fr/blosxom/R/2012-06-01_Optimization.html
> There is an example under "Long-short constraints".
> 
> 
> 
> On Mon, Sep 30, 2013 at 3:24 PM, alexios ghalanos <alexios at 4dscape.com
> <mailto:alexios at 4dscape.com>> wrote:
> 
>     1. This works just fine: (LB and UB should be a vector)
>     myspec<-parmaspec(S=cov(R),forecast=100*abs(colMeans(R)),LB=rep(-0.05,15),UB=rep(0.05,15),budget=0,risk='EV',riskType='minrisk',target=0.0030346)
>     mysol <- parmasolve(myspec,type='QP')
>     +---------------------------------+
>     |        PARMA Portfolio          |
>     +---------------------------------+
>     No.Assets               : 15
>     Problem                 : QP
>     Risk Measure            : EV
>     Objective               : minrisk
>     Risk                    : 3e-07
>     Reward                  : 0.0030346
> 
>         Optimal_Weights
>     EWG          0.0293
>     EEM          0.0206
>     EWL          0.0174
>     TLT          0.0120
>     EZA          0.0107
>     EWA          0.0107
>     EPP          0.0106
>     EWC          0.0094
>     IWO          0.0061
>     IWN          0.0051
>     IWF         -0.0078
>     EWJ         -0.0214
>     EWU         -0.0221
>     IWD         -0.0309
>     EWQ         -0.0500
> 
> 
>     2. You've set leverage with a quadratic solver. How do you suppose the
>     absolute value function can be accommodated in this problem?
>     This works:
>     myspec <-
>     parmaspec(scenario=as.matrix(R),forecast=100*abs(colMeans(R)),LB=rep(-0.05,15),UB=rep(0.05,15),leverage=0.1,risk='EV',riskType='minrisk',target=0.0030346)
>     mysol <- parmasolve(myspec, type='NLP', w0=rep(0.01,15))
>     sum(abs(weights(mysol)))
> 
>     i.e. use a scenario matrix instead and solver as NLP (and make sure to
>     provide a set of starting weights 'w0').
> 
> 
>     I suppose I should add more checks/warnings.
> 
> 
>     Regards,
> 
>     Alexios
> 
> 
> 
>     On 30/09/2013 22:52, Preston Li wrote:
>     > *Can someone please tell me why the following is not giving me a
>     solution?*
>     >
>     >
>     > if (!is.loaded('etfdata')) data(etfdata)
>     >
>     > R = timeSeries::returns(etfdata)
>     >
>     >> myspec <-
>     >
>     parmaspec(S=cov(R),forecast=100*abs(colMeans(R)),LB=-0.05,UB=0.05,budget=0,risk='EV',riskType='minrisk',target=0.0030346)
>     >> mysol <- parmasolve(myspec,type='QP')
>     >> mysol
>     >
>     > +---------------------------------+
>     > |        PARMA Portfolio          |
>     > +---------------------------------+
>     > No.Assets               : 15
>     > Problem                 : QP
>     > Risk Measure            : EV
>     > Objective               : minrisk
>     > Risk                    : NA
>     > Reward                  : NA
>     >
>     > Error in Math.data.frame(list(Optimal_Weights = logical(0)), 4) :
>     >   non-numeric variable in data frame: Optimal_Weights
>     >
>     > *Whereas if I do the following I get a solution (and why is the
>     "leverage"
>     > constraint ignored?):*
>     >
>     >> myspec <-
>     >
>     parmaspec(S=cov(R),forecast=100*abs(colMeans(R)),LB=-0.05,UB=0.05,budget=0,risk='EV',riskType='optimal',leverage=0.1)
>     >> mysol <- parmasolve(myspec,type='QP')
>     >> mysol
>     >
>     > +---------------------------------+
>     > |        PARMA Portfolio          |
>     > +---------------------------------+
>     > No.Assets               : 15
>     > Problem                 : QP
>     > Risk Measure            : EV
>     > Objective               : optimal
>     > Risk                    : 3e-07
>     > Reward                  : 0.0030346
>     >
>     >     Optimal_Weights
>     > EWG          0.0293
>     > EEM          0.0206
>     > EWL          0.0174
>     > TLT          0.0120
>     > EZA          0.0107
>     > EWA          0.0107
>     > EPP          0.0106
>     > EWC          0.0094
>     > IWO          0.0061
>     > IWN          0.0051
>     > IWF         -0.0078
>     > EWJ         -0.0214
>     > EWU         -0.0221
>     > IWD         -0.0309
>     > EWQ         -0.0500
>     >> sum(abs(mysol at solution$weight)/2)
>     > [1] 0.1321498
>     >
>     >       [[alternative HTML version deleted]]
>     >
>     > _______________________________________________
>     > R-SIG-Finance at r-project.org <mailto:R-SIG-Finance at r-project.org>
>     mailing list
>     > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>     > -- Subscriber-posting only. If you want to post, subscribe first.
>     > -- Also note that this is not the r-help list where general R
>     questions should go.
>     >
>     >
> 
>



More information about the R-SIG-Finance mailing list