# [R] Calculating Portfolio Standard deviation

Joshua Wiley jwiley.psych at gmail.com
Mon Jan 10 10:05:10 CET 2011

```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_vettori at 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)^2+(P)^2+(P)^2+(P[4)^2 +
>                         2*P*P*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?
>
>
> Amelia Vettori
>
>
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help