Dear Amelia,

If you have the actual data you should be able to use the variance covariance 
matrix to simplify this

Vdat <- cov(prices_df)

sum(diag(Vdat)) + 2*Vdat[upper.tri(Vdat)]

By using covariances instead of correlations you do not need to multiply by he 
standard deviations and by using variances there's no need to square. The only 
trick would be adding your weights back in.  See ?diag and ?upper.tri and ?vcov 
for relevant documentation.

Cheers,

Josh

On Jan 10, 2011, at 0:26, Amelia Vettori <amelia_vett...@yahoo.co.nz> wrote:

> Dear R helpers
> 
> I have following data
> 
> stocks <- c("ABC", "DEF", "GHI", "JKL")
> 
> prices_df <- data.frame(ABC = c(17,24,15,22,16,22,17,22,15,19), 
>                                          DEF = 
> c(22,28,20,20,28,26,29,18,24,21), 
>                                           GHI = 
> c(32,27,32,36,37,37,34,23,25,32),
> 
>                                           JKL = 
> c(47,60,60,43,62,38,44,53,61,41))
> 
> sd_prices <- c(3.3483,3.9497,4.9721,9.3387)   # standard deviations say(sd1, 
> sd2, sd3, sd4)
>                        
> weights <- c(0.10, 0.25, 0.20, 0.45)      # say (w1, w2, w3, w4)
> 
> I need to calculate the standard deviation of the portfolio. The formula is
> 
> stdev_portfolio = sqrt((w1*sd1)^2+(w2*sd2)^2+(w3*sd3)^2+(w4*sd4)^2 + 
>                         2*w1*w2*sd1*sd2*correlation(ABC, DEF)+
>   
>                      2*w1*w3*sd1*sd3*correlation(ABC, GHI)+
>                         2*w1*w4*sd1*sd4*correlation(ABC, JKL)+
>                         2*w2*w3*sd2*sd3*correlation(DEF, GHI)+
>                         2*w2*w4*sd2*sd4*correlation(DEF, JKL)+
>                         2*w3*w4*sd3*sd4*correlation(GHI, JKL))
>                   
>      
>                         
> OR if we define 
> 
> P = sd_prices*weights
> 
> I need to calculate 
> 
> stdev_portfolio = sqrt((P1)^2+(P2)^2+(P3)^2+(P4)^2 + 
>                         2*P1*P2*correlation(ABC, DEF)+
>                         2*P1*P3*correlation(ABC, GHI)+
>                         2*P1*P4*correlation(ABC, JKL)+
>                         2*P2*P3*correlation(DEF,
> GHI)+
>                         2*P2*P4*correlation(DEF, JKL)+
>                         2*P3*P4*correlation(GHI, JKL))
>                         
> In reality I will be dealing with not 4, but many stocks and hence I can't 
> generalize this as
> 
> stdev_portfolio = sqrt((P[1])^2+(P[2])^2+(P[3])^2+(P[4)^2 + 
>                         2*P[1]*P[2]*correlation(ABC, DEF)+
>                         2*P1*P3*correlation(ABC,
> GHI)+
>                         2*P1*P4*correlation(ABC, JKL)+
>                         2*P2*P3*correlation(DEF, GHI)+
>                         2*P2*P4*correlation(DEF, JKL)+
>                         2*P3*P4*correlation(GHI, JKL))                        
>                         
> Kindly advise as to how do I
> calculate the portfolio standard deviation?
> 
> Thanking in advance
> 
> Amelia Vettori
> 
> 
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to