The issue seems to be in the calculation of the co-skewness and co-kurtosis.

In particular, when calling ES directly, the user-supplied mu gets
used to calculate M3 and M4. When called through PortfolioAnalytics,
M3 and M4 are calculated (without using mu) before calling ES.

A pure PerformanceAnalytics example:

###### < BEGIN EXAMPLE > ######
## Marco's problem
library(PerformanceAnalytics)
data(indexes)

R <- indexes[,1:4]

w <- rep(1/4, 4)
mu <- rep(0.01, 4)

M2 <- cov(R)
M3 <- M3.MM(R)
M4 <- M4.MM(R)

ES(R, portfolio_method="single", weights=w,
   sigma=M2, m3=M3, m4=M4, mu=mu, invert=FALSE)

ES(R, portfolio_method="component", weights=w, mu=mu)$MES

## Adding mu to the calculation of M3, M4 gives consistent answers
M2_mu <- cov(R)
M3_mu <- M3.MM(R, mu=mu)
M4_mu <- M4.MM(R, mu=mu)

ES(R, portfolio_method="single", weights=w,
   sigma=M2, m3=M3_mu, m4=M4_mu, mu=mu, invert=FALSE)

ES(R, portfolio_method="component", weights=w, mu=mu)$MES

##### < END EXAMPLE > ######

It looks like PortfolioAnalytics::set.portfolio.moments does not
attempt to pass a user supplied mu to PerformanceAnalytics::M3.MM and
PerformanceAnalytics::M4.MM, even if its given as an argument to
constrained_objective():

>From 
>https://github.com/cran/PortfolioAnalytics/blob/master/R/moment.functions.R#L327

switch(method,
    sample = {
        if(is.null(momentargs$mu)) momentargs$mu = matrix(
as.vector(apply(tmpR, 2, 'mean')), ncol=1);
        if(is.null(momentargs$sigma)) momentargs$sigma = cov(tmpR)
        if(is.null(momentargs$m3)) momentargs$m3 =
PerformanceAnalytics::M3.MM(tmpR)
        if(is.null(momentargs$m4)) momentargs$m4 =
PerformanceAnalytics::M4.MM(tmpR)
},

[Code for the current development version looks to behave similarly]


Hope this helps,
Michael


On Wed, Oct 12, 2016 at 5:21 AM, Brian G. Peterson <br...@braverock.com> wrote:
> I attach what I think is a syntactically correct version of the email, which
> looks like it was pasted from HTML.
>
> We'll try to take a look.
>
> Regards,
>
> Brian
>
> On 10/12/2016 04:34 AM, Marco Mastrangeli wrote:
>>
>> Hi Michael,
>>
>> thanks for your reply, I apologize for the not full clarity of my
>> question.
>> In the following, I try to report a full example.
>>
>> #Library
>> *library(PerformanceAnalytics)*
>> *library(PortfolioAnalytics)*
>>
>> #Returns data present in "PortfolioAnalytics"
>> *data(indexes)*
>> *indexes <- indexes[,1:4]*
>>
>> #New Portfolio Object
>> *Wcons <- portfolio.spec(assets=colnames(indexes))*
>>
>> #Add box constraints
>> *Wcons <- add.constraint(portfolio=Wcons, type='box', min=0, max=1)*
>>
>> #Add the full investment constraint
>> *Wcons <- add.constraint(portfolio=Wcons, type="full_investment")*
>>
>> #Add Objective specification: VaR with default parameter for vector "mu"
>> (EXAMPLE 1)
>> *VaRObjSpec <- add.objective(portfolio=Wcons, type="risk", name="VaR",
>> arguments=list(p=0.95), enabled=TRUE)*
>>
>> #The value of the objective function is:
>> *constrained_objective(w=rep(1/4,4), R=indexes, portfolio=VaRObjSpec)
>> #* VaR
>> *0.0499467*
>>
>> #This is the VaR of the equal-weight portfolio as computed by the function
>> VaR in the PerformanceAnalytics package.
>> *VaRout <- VaR(indexes, weights=rep(1/4,4), p=0.95,
>> portfolio_method="component")*
>> *VaRout$MVaR       # *[1,]* 0.0499467*
>>
>> Now, I repet the VaR example with a user-defined vector for the parameter
>> "mu".
>>
>> #User-defined vector "mu"
>> *myMu = rep(0.01, 4)*
>>
>> #Add Objective specification: VaR with user-defined parameter for vector
>> "mu"
>> *myVaRObjSpec <- add.objective(portfolio=Wcons, type="risk", name="VaR",
>> arguments=list(p=0.95, mu=myMu), enabled=TRUE)*
>>
>> #The value of the objective function is:
>> *constrained_objective(w=rep(1/4,4), R=indexes, portfolio=myVaRObjSpec)
>>   #* VaR *0.04638622*
>>
>> #This is the VaR of the equal-weight portfolio as computed by the function
>> VaR in the PerformanceAnalytics package with *mu=myMu.*
>> *myVaRout <- VaR(indexes, weights=rep(1/4,4),
>> p=0.95, mu=myMu, portfolio_method="component")*
>> *myVaRout$MVaR       # *[1,]* 0.04638622*
>>
>> So, using the default and user-defined parameter for "mu" there is
>> corrispondence between constrained_objective and the function VaR of
>> PerformanceAnalytics package.
>> I repet the example but now adding CVaR as risk objective in Wcons
>> portfolio.
>>
>> #Add Objective specification: CVaR with default parameter for vector "mu"
>> (EXAMPLE 2)
>> *CVaRObjSpec <- add.objective(portfolio=Wcons, type="risk", name="CVaR",
>> arguments=list(p=0.95), enabled=TRUE)*
>>
>> #The value of the objective function is:
>> *constrained_objective(w=rep(1/4,4), R=indexes, portfolio=CVaRObjSpec)
>> #* ES
>> *0.1253199*
>>
>> #This is the CVaR of the equal-weight portfolio as computed by the
>> function
>> ES in the PerformanceAnalytics package.
>> *CVaRout <- ES(indexes, weights=rep(1/4,4), p=0.95,
>> portfolio_method="component")*
>> *CVaRout$MES       # *[1,]* 0.1253199*
>>
>> Now, I repet the CVaR example with a user-defined vector for the parameter
>> "mu".
>>
>> #User-defined vector "mu"
>> *myMu = rep(0.01, 4)*
>>
>> #Add Objective specification: CVaR with user-defined parameter for vector
>> "mu"
>> *myCVaRObjSpec <- add.objective(portfolio=Wcons, type="risk", name="CVaR",
>> arguments=list(p=0.95, mu=myMu), enabled=TRUE)*
>>
>> #The value of the objective function is:
>> *constrained_objective(w=rep(1/4,4), R=indexes, portfolio=myCVaRObjSpec)
>>   #* ES *0.1217594*
>>
>> #This should be the CVaR of the equal-weight portfolio as computed by the
>> function ES in the PerformanceAnalytics package with *mu=myMu.*
>> *myCVaRout <- ES(indexes, weights=rep(1/4,4),
>> p=0.95, mu=myMu, portfolio_method="component")*
>> *myCVaRout$MES       # *[1,]* 0.1235878*
>>
>> In this case, using the user-defined parameter for "mu" there is no
>> corrispondence between the value of constrained_objective (0.1217594*)
>> *and
>>
>> the result of function ES of PerformanceAnalytics package (0.1235878). Why
>> there is no match in this case?
>> This not the case for the matrix parameter (for portfolio) "sigma": if I
>> use a user-defined sigma matrix, there is always corrispondence (without,
>> obviosly, costraints that augment the penalty augmented objective
>> function) between the value of constrained_objective (with VaR/CVaR risk
>> objective) and the result of function VaR/ES of PerformanceAnalytics
>> package.
>>
>> I hope my example is clear enough to illustrate the question.
>> Thanks a lot for your attention.
>> Marco
>>
>>
>> On Wed, Oct 12, 2016 at 1:55 AM, Michael Weylandt <
>> michael.weyla...@gmail.com> wrote:
>>
>>> Hi Marco,
>>>
>>> Can you put together a minimal reproducible example [1,2] so that it's
>>> easier for others to answer your question?
>>>
>>> For this problem, I'd recommend using the edhec data distributed with
>>> PerformanceAnalytics.
>>>
>>> Thanks,
>>> Michael
>>>
>>> [1] http://stackoverflow.com/questions/5963269/how-to-make-
>>> a-great-r-reproducible-example
>>> [2] http://adv-r.had.co.nz/Reproducibility.html
>>>
>>> On Tue, Oct 11, 2016 at 11:46 AM, Marco Mastrangeli
>>> <marco.mastrang...@gmail.com> wrote:
>>>>
>>>> I have a question about the use of the "mu" parameter in the functions
>>>> StdDev, VaR e CVaR.
>>>> As reference data we can use data in the paper "Vignette: Portfolio
>>>> Optimization with CVaR budgets in PortfolioAnalytics".
>>>> If we use the default parameters for "mu" and "sigma", there is a
>>>> match between
>>>>
>>>>> constrained_objective( w = rep(1/4,4) , R = indexes, portfolio =
>>>>> ObjSpec)
>>>>
>>>>                 [,1]
>>>> ES 0.1253199
>>>>
>>>> and
>>>>
>>>>> out<-ES(indexes, weights = rep(1/4,4),p=0.95,
>>>
>>> portfolio_method="component")
>>>>>
>>>>> out$MES
>>>>
>>>>                  [,1]
>>>> [1,] 0.1253199
>>>>
>>>> as explained by the authors.
>>>> If I insert a user-defined sigma matrix for the "sigma" parameter, the
>>>> match is still there between this two exspressions. If I insert a
>>>> user-defined vector for the "mu" parameter (for example "mu=rep(0.01,
>>>
>>> 4)",
>>>>
>>>> the result of the two exspressions is the same only for portafolio with
>>>> risk objective function StdDev and VaR, not for CVaR.
>>>>
>>>> VaR case:
>>>>>
>>>>> ObjSpec = add.objective(portfolio=Wcons, type="risk", name="VaR",
>>>>
>>>> arguments=list(p=0.95, mu=rep(0.01,4)), enabled=TRUE)
>>>>>
>>>>> constrained_objective(w=rep(1/4,4), R=indexes, portfolio=ObjSpec)
>>>>
>>>>                    [,1]
>>>> VaR 0.04638622
>>>>
>>>>> out<-VaR(indexes, weights=rep(1/4,4), p=0.95, mu=rep(0.01,4),
>>>>
>>>> portfolio_method="component")
>>>>>
>>>>> out
>>>>
>>>> $MVaR
>>>>                   [,1]
>>>> [1,] 0.04638622
>>>>
>>>>
>>>> CVaR case:
>>>>>
>>>>> ObjSpec = add.objective(portfolio=Wcons, type="risk", name="CVaR",
>>>>
>>>> arguments=list(p=0.95, mu=rep(0.01,4)), enabled=TRUE)
>>>>>
>>>>> constrained_objective(w=rep(1/4,4), R=indexes, portfolio=ObjSpec)
>>>>
>>>>                  [,1]
>>>> ES 0.1217594
>>>>
>>>>> out<-ES(indexes, weights=rep(1/4,4), p=0.95, mu=rep(0.01,4),
>>>>
>>>> portfolio_method="component")
>>>>>
>>>>> out
>>>>
>>>> $MES
>>>>                  [,1]
>>>> [1,] 0.1235878
>>>>
>>>> I can't find the explanation for this thing. Thanks a lot for your
>>>> attention.
>>>>
>>>> Marco
>>>>
>>>>          [[alternative HTML version deleted]]
>>>>
>>>> _______________________________________________
>>>> R-SIG-Finance@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.
>>>
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-SIG-Finance@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.
>>
>
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
>
> _______________________________________________
> R-SIG-Finance@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.

_______________________________________________
R-SIG-Finance@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.

Reply via email to