# [R-SIG-Finance] PerformanceAnalytics - Style Analysis

Thomas Etheber etheber at gmx.de
Wed Aug 4 20:57:43 CEST 2010

```Dear list,

finally I got the chance to have a look at your code. Basically I agree,
it implements what Eric has suggested.

I just changed to lines:

<<<
# perform constrained regression
lm.con = lm(y.new ~ 0 + x.new )

# solve for b1.hat
b1.hat = 1 - sum( coef(lm.con) )
<<<

I think it is clearer and more convenient. Indeed the coefficients are
restricted to sum to unity now, but is there a way to test the
restricted coefficients for significance, i.e. something like a
t-statistic or p-values for the single coefficients and not F-Stats for
the whole model? Can we perhaps use the t- and p-values from the lm.con
model?

Regards,
Thomas

Am 29.06.2010 16:55, schrieb Guy Yollin:
> Guys,
>
> Not sure about any package updates but based on Eric's description, I think the constrained regression looks something like this:
>
>
> conReg = function(R.fund,R.style)
> {
>    # make LHS and RHS matrices
>    y = as.matrix(R.fund)
>    x = as.matrix(R.style)
>
>    # subtract x1 from both sides for constrained regression
>    x1 = x[,1]
>    y.new = y - x1
>    x.new = x[,-1] - x1
>
>    # perform constrained regression
>    lm.con = lm(y.new ~ x.new - 1)
>
>    # solve for b1.hat
>    b1.hat = tail(cumsum(c(1,-1*coef(lm.con))),1)
>    con.coef = c(b1.hat,coef(lm.con))
>    names(con.coef) = colnames(x)
>
>    # calc fitted values
>    y.hat = x %*% con.coef
>
>    # compute R2
>    resid = y - y.hat
>    tss<- sum(y^2)
>
>    # return results
>    con.mod = list(coefficients = con.coef,fitted.values=y.hat,residuals=resid,r.squared=R2)
>    return(con.mod)
> }
>
> # compare constrained regression with quadratic programming
> data(edhec)
> data(managers)
> sf = style.fit(managers[97:132,2,drop=FALSE],edhec[85:120,], method="constrained", leverage=FALSE)
> cr = conReg(R.fund=managers[97:132,2,drop=FALSE],R.style=edhec[85:120,])
> par(mfrow=c(2,1))
> barplot(sf\$weights[,1],names.arg=rownames(sf\$weights),las=2,cex.names=0.5)
> barplot(cr\$coefficients,las=2,cex.names=0.5)
>
>
>
> Best,
>
> -- G
>
>
>
> -----Original Message-----
> From: r-sig-finance-bounces at stat.math.ethz.ch [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Thomas Etheber
> Sent: Tuesday, June 29, 2010 3:06 AM
> To: Eric Zivot
> Cc: r-sig-finance at stat.math.ethz.ch; brian at braverock.com
> Subject: Re: [R-SIG-Finance] PerformanceAnalytics - Style Analysis
>
> Dear Eric,
> dear Brian,
>
> just updated my R distribution and I am now running:
>
> R:  version 2.11.1 (2010-05-31)
> PerformanceAnalytics: version 1.0.2.1
>
> It seems that your bugfix for the constrained StyleAnalyiss is not yet
> included in the new PerformanceAnalytics. Maybe the timespan between
> your fix and the release of PerformanceAnalytics 1.0.2.1 in mid April
> was too short to include it.
>
> Eric, if you have fixed the code already, could you please send it to
> me? Maybe I can include the code in my actual project, so that I don't
> have to wait for the next release of the package.
>
> Regards,
> Thomas
>
>
>
> Am 23.03.2010 18:59, schrieb Eric Zivot:
>
>> You are right. The correct way to enforce that the regression coefficients
>> sum to unity is to impose this in the regression instead of just normalizing
>> the coefficients to sum to unity. It is not difficult to do this because it
>> is a linear restriction and the restriction can be substituted into the
>> linear regression to create another linear regression. I was supposed to add
>> this fix to the style.fit() function last year. I'll do it now and send the
>> fix off to Brian so he can put it in the PerformanceAnalytics package.
>> Briefly, consider the simple linear regression
>>
>> Y = x1*b1 + x2*b2 + e
>>
>> The restriction to impose is b1 + b2 = 1. Equivalently, b1 = 1 - b2.
>> Substituting into the regression gives
>>
>> Y = x1*(1 - b2) + x2*b2 + e = x1 + b2*(x2 - x1) + e =>   Y - x1 = b2*(x2 - x1)
>> + e
>>
>> Therefore, the linear regression to run to impose the restriction b1+b2 = 1
>> is
>>
>> (Y - x1) = b2*(x2 - x1) + e
>>
>> Once you have b2.hat, then just solve for b1 using the constraint: b1.hat =
>> 1 - b2.hat. (Note that it doesn't matter if you define the restriction in
>> terms of b1 = 1 - b2 or b2 = 1 - b1.)  Of course, to get the right R2 you
>> have to base it on Y as the dependent variable and not Y - x1. That is,
>> compute the R2 from the fitted values
>>
>> Y.hat = b1.hat*x1 + b2.hat*x2
>>
>> Where b1.hat and b2.hat are the restricted least squares coefficients.
>>
>> Eric Zivot
>> Professor and Gary Waterman Distinguished Scholar
>> Department of Economics
>> Box 353330                  email:  ezivot at u.washington.edu
>> University of Washington    phone:  206-543-6715
>> Seattle, WA 98195-3330
>> www:  http://faculty.washington.edu/ezivot
>>
>>
>>
>>
>> -----Original Message-----
>> From: r-sig-finance-bounces at stat.math.ethz.ch
>> [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Thomas Etheber
>> Sent: Tuesday, March 23, 2010 4:03 AM
>> To: r-sig-finance at stat.math.ethz.ch
>> Subject: [R-SIG-Finance] PerformanceAnalytics - Style Analysis
>>
>> Dear List,
>>
>> I had a look at the code of the PerformanceAnalytics package and I use
>> this package on a regular basis.
>>
>> The style.fit function for Style Analysis provides thrree supported
>> methods, which can be chosen via a parameter. I am talking of the
>> normalized method here. This method requires the regression coefficients
>> to sum to 1.
>> Indeed the code in Version 1.0.0 says something like this:
>>
>> [... Default implementation ...]
>> column.weights = as.data.frame(coef(column.lm))
>> [...]
>>    if (method == "normalized") {
>>                   column.weights = column.weights/sum(column.weights)
>>    }
>>
>> I suppose here we are just scaling the coefficients to sum to 1, in my
>> view the regression should a priori deal with this restriction (some
>> sort of constrained regression or the like).
>> At least if you look at Tabel 2 of Sharpe (1992) the coefficients do not
>> support the actual implementation and I think somebody might want to
>> have a look at this code fragment.
>> Unfortunately I do not know how to implement this kind of restricted
>> regression in R, but I believe there are already ways of doing this.
>> Perhaps somebody from the list can guide us to the right direction.
>>
>> Anyway great thx to the authors of this packkage!
>> Thomas
>>
>> PS: In Stata the right command seems to be cnsreg.
>>
>> _______________________________________________
>> R-SIG-Finance at stat.math.ethz.ch 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 at stat.math.ethz.ch 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.
>
>

```