2

Tengo una dataframe de retornos anuales yearly_return y quiero hacer la matriz de covarianza. Entonces utilisé cov(yearly_return) pero me parece que no me da la respuesta esperada porque parece que la diagonal no está llena de 0 o 1. Supongo que la columna de las fechas esta la razón de este problema. Sin embargo cómo hacer la matriz de covarianza con R?

> yearly_return
                 .SXQR       .SXTR        .SXNR        .SXMR       .SXAR       .SX3R
2000-01-01 -0.04211651 -0.01692493 -0.134079012 -0.110542652 -0.13658011  0.27697419
2001-01-01 -0.06170831 -0.06020640 -0.270292443 -0.244983557  0.03086915 -0.01268035
2002-01-01 -0.13181676 -0.31916713 -0.370798530 -0.450515898 -0.28119776 -0.11107462
2003-01-01  0.12010929  0.23596541  0.195487179  0.091163773  0.18582579 -0.02995400
2004-01-01  0.09574009  0.07114804  0.093577436  0.079726746  0.02328106  0.06988229
2005-01-01  0.32241764  0.24503874  0.346011845  0.137005650  0.20891419  0.26745187
2006-01-01  0.19390175  0.25618254  0.255671803  0.095515590  0.25897784  0.17402985
2007-01-01  0.07151176 -0.15668573  0.099268407 -0.043097704  0.24907118  0.12253799
2008-01-01 -0.39909398 -0.43417808 -0.454896057 -0.374645801 -0.42310256 -0.28321255
2009-01-01  0.37555060  0.15490882  0.376812650  0.197982252  0.15445790  0.33306810
2010-01-01  0.29645850  0.26603074  0.331675002  0.157542404  0.43139233  0.21087586
2011-01-01  0.02689063 -0.13311326 -0.152939580 -0.082821116 -0.25170969  0.07262980
2012-01-01  0.23297708  0.33254856  0.236761181  0.200574474  0.34626234  0.20785336
2013-01-01  0.15758295  0.28226061  0.224889270  0.352383599  0.36813147  0.09852600
2014-01-01  0.12771485  0.20728740  0.007498316  0.106319669  0.07123982  0.14959034
2015-01-01  0.23221073  0.22357015  0.082448980  0.156892993  0.15695521  0.20384265
2016-01-01  0.07382496 -0.07403532  0.141806854 -0.027957980  0.02966412 -0.01935074
2017-01-01  0.11678507  0.15511923  0.162564962 -0.003737683  0.14260345  0.12560078
                  .SX6R       .SXFR        .SXOR       .SXDR       .SX4R
2000-01-01  0.104681337  0.15999703 -0.043590105  0.26370803  0.05147042
2001-01-01 -0.071406543 -0.15554701  0.019947448 -0.07791252 -0.13987785
2002-01-01 -0.230112301 -0.34337396 -0.289506263 -0.28954601 -0.21697798
2003-01-01  0.107872960  0.13056199  0.244542568  0.08783802  0.04559812
2004-01-01  0.281793979  0.23979186  0.242456556  0.02901212  0.15874338
2005-01-01  0.292232260  0.33659939  0.326988422  0.29211304  0.31711918
2006-01-01  0.393387562  0.47290317  0.378672907  0.04249343  0.23364378
2007-01-01  0.200444605 -0.11887693 -0.008781149 -0.09816185  0.26299600
2008-01-01 -0.355704066 -0.53960190 -0.449588567 -0.16008695 -0.36264681
2009-01-01  0.041725299  0.29868730  0.372961769  0.16775463  0.45728827
2010-01-01 -0.047881523  0.16608250  0.027759902  0.08104497  0.23118616
2011-01-01 -0.127988837 -0.19952875 -0.188775294  0.14888869 -0.08247720
2012-01-01  0.023502640  0.24994212  0.176444777  0.15020270  0.30207712
2013-01-01  0.111748340  0.36818622  0.221097462  0.22388398  0.15067803
2014-01-01  0.197105509  0.12921216  0.076237379  0.21724504  0.07132732
2015-01-01  0.006913529  0.21694561  0.190190108  0.17151173  0.07280142
2016-01-01 -0.027294030 -0.01439982  0.141503689 -0.06332644  0.09755300
2017-01-01  0.088810599  0.19514217  0.099277978  0.03878557  0.12007593
                  .SXRR        .SXER        .SXKR        .SX7R       .SX8R
2000-01-01 -0.225365403  0.043774723 -0.361583644  0.128316144 -0.17028728
2001-01-01 -0.009904841 -0.009732258 -0.283345384 -0.060287204 -0.39441865
2002-01-01 -0.291441728 -0.150425114 -0.365582268 -0.236771176 -0.55863189
2003-01-01  0.088919239  0.022473035  0.151242142  0.210549933  0.26432695
2004-01-01  0.125110885  0.138461912  0.121153901  0.121524454 -0.02500651
2005-01-01  0.106820548  0.343225228  0.003708784  0.243734200  0.22048081
2006-01-01  0.273049830  0.063976954  0.216590444  0.220488172  0.03986477
2007-01-01 -0.007858586  0.093594463  0.172031349 -0.154626065 -0.01157508
2008-01-01 -0.412050836 -0.374351734 -0.327785050 -0.626580865 -0.47660446
2009-01-01  0.323031727  0.243966979  0.152915965  0.471351371  0.20212243
2010-01-01  0.120842042  0.013476388  0.078833242 -0.115211802  0.16435588
2011-01-01 -0.049966570  0.039481850 -0.009725331 -0.310751184 -0.12290388
2012-01-01  0.117352173 -0.013181300 -0.061099921  0.258543547  0.22139256
2013-01-01  0.208803171  0.056021733  0.351386970  0.187817561  0.24531130
2014-01-01 -0.011008923 -0.114184021  0.127413678  0.008276349  0.09512400
2015-01-01  0.106651835 -0.037800910  0.129595315 -0.008791811  0.15368136
2016-01-01 -0.018977205  0.306591774 -0.099367626 -0.002784941  0.08031547
2017-01-01 -0.013230032  0.010090399 -0.003590741  0.111173975  0.19937089
                 .SXIR       .SXPR
2000-01-01  0.21292564 -0.18061961
2001-01-01 -0.28494845  0.18713470
2002-01-01 -0.49789995 -0.17531545
2003-01-01  0.07890223  0.19142430
2004-01-01  0.09043759  0.12806068
2005-01-01  0.32582934  0.52502544
2006-01-01  0.19226424  0.38868295
2007-01-01 -0.10726150  0.28007157
2008-01-01 -0.43832446 -0.63296576
2009-01-01  0.15149087  0.88588641
2010-01-01  0.03285816  0.25505589
2011-01-01 -0.11482104 -0.28741280
2012-01-01  0.36427523  0.06243915
2013-01-01  0.30237368 -0.14556775
2014-01-01  0.14390280 -0.02503591
2015-01-01  0.18403687 -0.31307481
2016-01-01  0.01939247  0.71674991
2017-01-01  0.10445053  0.22769644

Aquí pueden ver la matriz de covarianza:

> head(covMat)
           .SXQR      .SXTR      .SXNR      .SXMR      .SXAR      .SX3R      .SX6R
.SXQR 0.03345763 0.03498086 0.04185753 0.03245136 0.03497776 0.02324828 0.02081399
.SXTR 0.03498086 0.04974472 0.04619830 0.04159817 0.04420657 0.02401824 0.02689768
.SXNR 0.04185753 0.04619830 0.06097311 0.04537355 0.05011720 0.02686316 0.03057380
.SXMR 0.03245136 0.04159817 0.04537355 0.04287405 0.04017447 0.02191462 0.02405812
.SXAR 0.03497776 0.04420657 0.05011720 0.04017447 0.05465295 0.02184147 0.02625680
.SX3R 0.02324828 0.02401824 0.02686316 0.02191462 0.02184147 0.02318865 0.01733667
           .SXFR      .SXOR      .SXDR      .SX4R      .SXRR      .SXER      .SXKR
.SXQR 0.04086303 0.03468033 0.01823918 0.03342966 0.03033003 0.02051229 0.02694061
.SXTR 0.05423479 0.04174388 0.02550628 0.03293408 0.03421856 0.01525291 0.03196615
.SXNR 0.05599539 0.04790487 0.02203189 0.04631396 0.04020604 0.02833009 0.03950701
.SXMR 0.04781758 0.03778244 0.02453928 0.03373207 0.03334523 0.01743750 0.03682066
.SXAR 0.04785298 0.03958789 0.01603731 0.03772380 0.03561357 0.01769105 0.03574271
.SX3R 0.03177379 0.02265676 0.01815483 0.02582638 0.01863272 0.01464734 0.01394396
           .SX7R      .SX8R      .SXIR      .SXPR
.SXQR 0.03635913 0.03991513 0.03508348 0.04456718
.SXTR 0.04453817 0.04959444 0.04831080 0.03223030
.SXNR 0.04871591 0.05847244 0.04905516 0.06505796
.SXMR 0.03789661 0.04934120 0.04555107 0.03031465
.SXAR 0.03882093 0.04838590 0.04046836 0.04557004
.SX3R 0.02804067 0.02522826 0.02949463 0.02648925

Por ejemplo SXQR varía consigo mismo por 0.04086303

Actualización

Para reproducir el error aqui esta lo que retorna dput(head(yearly_return))

> dput(head(yearly_return))
structure(c(-0.0421165123033923, -0.0617083091226032, -0.131816755569501, 
0.120109285789344, 0.0957400900067813, 0.322417643774428, -0.0169249318983612, 
-0.0602063954833634, -0.319167127697028, 0.235965409696626, 0.0711480448517274, 
0.245038735010082, -0.134079011941763, -0.2702924428144, -0.370798529903107, 
0.19548717948718, 0.0935774360717017, 0.346011845242674, -0.110542651522903, 
-0.244983557489711, -0.450515898083807, 0.0911637733076145, 0.0797267463656433, 
0.137005649717514, -0.136580114822547, 0.0308691499522444, -0.28119775606494, 
0.18582578531509, 0.0232810615199035, 0.208914188351921, 0.276974190672987, 
-0.0126803533973178, -0.111074619022596, -0.029954000507081, 
0.069882291782704, 0.267451871749836, 0.1046813369317, -0.071406542832064, 
-0.230112300861844, 0.107872959801132, 0.281793979111202, 0.292232260407681, 
0.159997028378981, -0.155547010190348, -0.34337395766517, 0.130561994148269, 
0.239791860002651, 0.336599392938921, -0.0435901050109238, 0.0199474482809667, 
-0.289506262743956, 0.244542567969835, 0.242456556082149, 0.326988421847471, 
0.263708030479369, -0.0779125192766018, -0.289546012748289, 0.0878380192808617, 
0.0290121189864121, 0.292113042450041, 0.051470416208502, -0.13987784564131, 
-0.216977981038514, 0.0455981235340108, 0.158743380734218, 0.317119184193754, 
-0.225365403148089, -0.00990484092618038, -0.29144172753255, 
0.0889192394781297, 0.125110884778395, 0.106820548420687, 0.0437747229932033, 
-0.0097322584687356, -0.150425114453891, 0.0224730348607323, 
0.138461912046818, 0.343225227534931, -0.361583644073633, -0.283345383672315, 
-0.365582268299222, 0.151242142064746, 0.121153901298999, 0.00370878423004628, 
0.128316144184622, -0.0602872043484647, -0.236771176027587, 0.210549932691828, 
0.121524453588088, 0.243734200072228, -0.170287283323371, -0.394418648502133, 
-0.558631889568851, 0.264326947209905, -0.0250065121125294, 0.220480807544162, 
0.212925636847071, -0.284948446239345, -0.497899952214482, 0.0789022298456261, 
0.0904375871687588, 0.325829336926796, -0.180619608268678, 0.187134698239872, 
-0.175315448448335, 0.191424297822835, 0.12806067767732, 0.525025437278938
), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(946684800, 
978307200, 1009843200, 1041379200, 1072915200, 1104537600), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
18L), .Dimnames = list(NULL, c(".SXQR", ".SXTR", ".SXNR", ".SXMR", 
".SXAR", ".SX3R", ".SX6R", ".SXFR", ".SXOR", ".SXDR", ".SX4R", 
".SXRR", ".SXER", ".SXKR", ".SX7R", ".SX8R", ".SXIR", ".SXPR"
)))
2
  • ¿Podrías incluir un dput(head(yearly_return)) para que se pueda reproducir el ejemplo? También creo que es la columna de fechas la que genera el problema, pero no hay forma de verificarlo sin reproducir el ejemplo.
    – mpaladino
    Commented el 29 mar. 2019 a las 14:17
  • @mpaladino Lo actualicé, pero no sé cómo verificar el tipo de la columna de fecha. Commented el 29 mar. 2019 a las 14:34

1 respuesta 1

4

El problema no está en las fechas, que son metadatos y no intervienen en el cálculo de la covarianza. De hecho el resultado que estás obteniendo es el que "correcto" para el cálculo de una matriz de covarianza: la diagonal te arroja la varianza de cada columna, entendiendo a la varianza como un caso especial de covarianza de una variable con sigo misma.

Para obtener 1 en la diagonal deberías usar cor(), que calcula una matriz de correlaciones (por defecto el coeficiente de determinación r de Pearson).

La covarianza de x y x es igual a la varianza de x

cov(yearly_return$.SXQR, yearly_return$.SXQR)

      .SXQR
.SXQR 0.02704784

var(yearly_return$.SXQR)
      .SXQR
.SXQR 0.02704784

La matriz de correlación tiene 1 en la diagonal

cor(yearly_return[,1:4])  #primeras 4 columnas

.SXQR     .SXTR     .SXNR     .SXMR
.SXQR 1.0000000 0.8808104 0.9654083 0.8785866
.SXTR 0.8808104 1.0000000 0.9416833 0.9599468
.SXNR 0.9654083 0.9416833 1.0000000 0.9551311
.SXMR 0.8785866 0.9599468 0.9551311 1.0000000

En https://es.wikipedia.org/wiki/Covarianza#Propiedades hay una escueta formulación de esa propiedad.

De todos modos se entiende facilmente mirando las ecuaciones de cada una.

La varianza se define como la sumatoria de los desvíos a la media (x-media_de_x) al cuadrado entre la n. La covarianza como la sumatoria del producto de los desvíos a la media de x y y entre la n. Si es la covarianza de x y x entonces ese producto es lo mismo que el cuadrado en la ecuación de la varianza. Por eso dan el mismo resultado: son matemáticamente equivalentes.

Sería útil mathJAX en SO.

1
  • Sí efectivamente @mpaladino all((var(yearly_return)==cov(yearly_return, y=yearly_return, use="all.obs"))==TRUE) #Se imprime: TRUE Commented el 29 mar. 2019 a las 14:57

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.