简体   繁体   中英

multiplying 2 zoo series in R

The below is a small extract of the whole data, I have thousands of symbols over many years. . .both symbols and date range change from run to run

I have 2 zoo series "returns" and "decFac".

    > tail(returns)
                    AAPL         DISCA          IBM           JNJ            KO
2014-12-23 -0.0035479832  0.0137774854  0.004943048 -0.0233164191  0.0145336114
2014-12-24 -0.0047206092 -0.0054309123 -0.002592361  0.0029684238 -0.0006984054
2014-12-26  0.0175226064 -0.0005733945  0.003208447  0.0044836732  0.0004657399
2014-12-29 -0.0007020609            NA           NA  0.0025666222 -0.0023303779
2014-12-30 -0.0122776892            NA           NA  0.0002847851 -0.0023360686
2014-12-31 -0.0192020576 -0.0219631307  0.002433726 -0.0075263261 -0.0127090448
                     NKE           TXN
2014-12-23  0.0004169359 -0.0007298205
2014-12-24  0.0033288228  0.0014592993
2014-12-26  0.0055922518 -0.0020985205
2014-12-29            NA            NA
2014-12-30            NA            NA
2014-12-31 -0.0075636285 -0.0086595788

> tail(decFac)
2014-12-23 2014-12-24 2014-12-26 2014-12-29 2014-12-30 2014-12-31 
0.02576202 0.02655878 0.02738019 0.02822700 0.02910000 0.03000000 

Both of these have values (according to R-Studio) of "zoo series from 2012-01-04 tp 2014-12-31"

the data types of each are below:

> sapply(returns, typeof)
    AAPL    DISCA      IBM      JNJ       KO      NKE      TXN 
"double" "double" "double" "double" "double" "double" "double" 
> sapply(decFac, typeof)
[1] "double"

My objextive is to have each stock return, for each day, be multiplied bu the decFac for the same day

The desired results for the first 5 days of AAPL are below:

                 AAPL
12/23/2014  -0.000091403
12/24/2014  -0.000125374
12/26/2014   0.000479772
12/29/2014  -0.000019817
12/30/2014  -0.000357281
12/31/2014  -0.000576062

zoo and xts objects will be aligned by index before operations:

library(xts)

time = seq.Date(as.Date('2014-12-23'), as.Date('2014-12-31'), by = 'day')
time = time[c(1,2,4,7:9)]  

AAPL = c( -0.0035479832, -0.0047206092,  0.0175226064,
          -0.0007020609, -0.0122776892, -0.0192020576 )
DISCA = c( 0.0137774854, -0.0054309123 , -0.0005733945 , 
           NA, NA, -0.0219631307 )
IBM = c( 0.004943048,  -0.002592361,  0.003208447,  
         NA,  NA,  0.002433726  )
JNJ = c( -0.0233164191,  0.0029684238,  0.0044836732, 
          0.0025666222,  0.0002847851,  -0.0075263261  )
KO = c( 0.0145336114, -0.0006984054, 0.0004657399,
        -0.0023303779, -0.0023360686, -0.0127090448)
NKE = c( 0.0004169359,  0.0033288228,  0.0055922518, 
         NA,  NA,  -0.0075636285 )
TXN = c( -0.0007298205, 0.0014592993, -0.0020985205, 
         NA, NA, -0.0086595788 )
decFac_v = c( 0.02576202,  0.02655878,  0.02738019, 
              0.02822700,  0.02910000,  0.03000000  )

returns_zoo = zoo( cbind(AAPL, DISCA, IBM, JNJ, KO, NKE, TXN),  time)  
returns     = xts( cbind(AAPL, DISCA, IBM, JNJ, KO, NKE, TXN),  time)  
decFac_zoo  = drop( zoo( decFac_v, time ))
decFac      = drop( xts( decFac_v, time ))

Multiplying the zoo or xts objects together should work:

returns * decFac
#                  AAPL         DISCA           IBM           JNJ            KO           NKE           TXN
# 2014-12-23 -9.140321e-05  3.549359e-04  1.273429e-04 -6.006781e-04  3.744152e-04  1.074111e-05 -1.880165e-05
# 2014-12-24 -1.253736e-04 -1.442384e-04 -6.884995e-05  7.883771e-05 -1.854880e-05  8.840947e-05  3.875721e-05
# 2014-12-26  4.797723e-04 -1.569965e-05  8.784789e-05  1.227638e-04  1.275205e-05  1.531169e-04 -5.745789e-05
# 2014-12-29 -1.981707e-05            NA            NA  7.244804e-05 -6.577958e-05            NA            NA
# 2014-12-30 -3.572808e-04            NA            NA  8.287246e-06 -6.797960e-05            NA            NA
# 2014-12-31 -5.760617e-04 -6.588939e-04  7.301178e-05 -2.257898e-04 -3.812713e-04 -2.269089e-04 -2.597874e-04
returns_zoo * decFac_zoo
#                  AAPL         DISCA           IBM           JNJ            KO           NKE           TXN
# 2014-12-23 -9.140321e-05  3.549359e-04  1.273429e-04 -6.006781e-04  3.744152e-04  1.074111e-05 -1.880165e-05
# 2014-12-24 -1.253736e-04 -1.442384e-04 -6.884995e-05  7.883771e-05 -1.854880e-05  8.840947e-05  3.875721e-05
# 2014-12-26  4.797723e-04 -1.569965e-05  8.784789e-05  1.227638e-04  1.275205e-05  1.531169e-04 -5.745789e-05
# 2014-12-29 -1.981707e-05            NA            NA  7.244804e-05 -6.577958e-05            NA            NA
# 2014-12-30 -3.572808e-04            NA            NA  8.287246e-06 -6.797960e-05            NA            NA
# 2014-12-31 -5.760617e-04 -6.588939e-04  7.301178e-05 -2.257898e-04 -3.812713e-04 -2.269089e-04 -2.597874e-04

Consider what would happen if you were to subset the returns or the decFac objects:

# subsetting
x = zoo( cbind(AAPL, DISCA, IBM, JNJ, KO, NKE, TXN),  time)  
y = drop( zoo( decFac_v, time))

x * y
#                  AAPL         DISCA           IBM           JNJ            KO           NKE           TXN
# 2014-12-23 -9.140321e-05  3.549359e-04  1.273429e-04 -6.006781e-04  3.744152e-04  1.074111e-05 -1.880165e-05
# 2014-12-24 -1.253736e-04 -1.442384e-04 -6.884995e-05  7.883771e-05 -1.854880e-05  8.840947e-05  3.875721e-05
# 2014-12-26  4.797723e-04 -1.569965e-05  8.784789e-05  1.227638e-04  1.275205e-05  1.531169e-04 -5.745789e-05
# 2014-12-29 -1.981707e-05            NA            NA  7.244804e-05 -6.577958e-05            NA            NA
# 2014-12-30 -3.572808e-04            NA            NA  8.287246e-06 -6.797960e-05            NA            NA
# 2014-12-31 -5.760617e-04 -6.588939e-04  7.301178e-05 -2.257898e-04 -3.812713e-04 -2.269089e-04 -2.597874e-04

x * y[-3] # does not return values corresponding to the third date index
#                  AAPL         DISCA           IBM           JNJ            KO           NKE           TXN
# 2014-12-23 -9.140321e-05  0.0003549359  1.273429e-04 -6.006781e-04  3.744152e-04  1.074111e-05 -1.880165e-05
# 2014-12-24 -1.253736e-04 -0.0001442384 -6.884995e-05  7.883771e-05 -1.854880e-05  8.840947e-05  3.875721e-05
# 2014-12-29 -1.981707e-05            NA            NA  7.244804e-05 -6.577958e-05            NA            NA
# 2014-12-30 -3.572808e-04            NA            NA  8.287246e-06 -6.797960e-05            NA            NA
# 2014-12-31 -5.760617e-04 -0.0006588939  7.301178e-05 -2.257898e-04 -3.812713e-04 -2.269089e-04 -2.597874e-04

x[-3] * y # does not return values corresponding to the third date index
#                  AAPL         DISCA           IBM           JNJ            KO           NKE           TXN
# 2014-12-23 -9.140321e-05  0.0003549359  1.273429e-04 -6.006781e-04  3.744152e-04  1.074111e-05 -1.880165e-05
# 2014-12-24 -1.253736e-04 -0.0001442384 -6.884995e-05  7.883771e-05 -1.854880e-05  8.840947e-05  3.875721e-05
# 2014-12-29 -1.981707e-05            NA            NA  7.244804e-05 -6.577958e-05            NA            NA
# 2014-12-30 -3.572808e-04            NA            NA  8.287246e-06 -6.797960e-05            NA            NA
# 2014-12-31 -5.760617e-04 -0.0006588939  7.301178e-05 -2.257898e-04 -3.812713e-04 -2.269089e-04 -2.597874e-04

x[,-3] * y # does not return values corresponding to the 3rd symbol column
#                  AAPL         DISCA           JNJ            KO           NKE           TXN
# 2014-12-23 -9.140321e-05  3.549359e-04 -6.006781e-04  3.744152e-04  1.074111e-05 -1.880165e-05
# 2014-12-24 -1.253736e-04 -1.442384e-04  7.883771e-05 -1.854880e-05  8.840947e-05  3.875721e-05
# 2014-12-26  4.797723e-04 -1.569965e-05  1.227638e-04  1.275205e-05  1.531169e-04 -5.745789e-05
# 2014-12-29 -1.981707e-05            NA  7.244804e-05 -6.577958e-05            NA            NA
# 2014-12-30 -3.572808e-04            NA  8.287246e-06 -6.797960e-05            NA            NA
# 2014-12-31 -5.760617e-04 -6.588939e-04 -2.257898e-04 -3.812713e-04 -2.269089e-04 -2.597874e-04

Consider what would happen if you were to expand the date range:

# expanding time dimension
expanded_time = seq.Date(as.Date('2012-01-04'), 
                         as.Date('2014-12-22'), 
                         by = 'day')

value = rep_len(1, length(expanded_time))
old_returns = xts( cbind(AAPL = value, 
                         DISCA = value, 
                         IBM = value, 
                         JNJ = value, 
                         KO = value, 
                         NKE = value, 
                         TXN = value), 
                   expanded_time)

returns_expanded_time = xts( rbind(old_returns, returns), c(expanded_time, time) )
returns_expanded_time * decFac  
# returns only values where the date index of each object matches:
#                  AAPL         DISCA           IBM           JNJ            KO           NKE           TXN
# 2014-12-23 -9.140321e-05  3.549359e-04  1.273429e-04 -6.006781e-04  3.744152e-04  1.074111e-05 -1.880165e-05
# 2014-12-24 -1.253736e-04 -1.442384e-04 -6.884995e-05  7.883771e-05 -1.854880e-05  8.840947e-05  3.875721e-05
# 2014-12-26  4.797723e-04 -1.569965e-05  8.784789e-05  1.227638e-04  1.275205e-05  1.531169e-04 -5.745789e-05
# 2014-12-29 -1.981707e-05            NA            NA  7.244804e-05 -6.577958e-05            NA            NA
# 2014-12-30 -3.572808e-04            NA            NA  8.287246e-06 -6.797960e-05            NA            NA
# 2014-12-31 -5.760617e-04 -6.588939e-04  7.301178e-05 -2.257898e-04 -3.812713e-04 -2.269089e-04 -2.597874e-04

Consider what would happen if you were to append additional columns:

new_column1 = rep_len(1, length(c(expanded_time, time)))
new_column2 = new_column1

returns_expanded_cols = xts( 
  cbind( rbind(old_returns, returns), 
         nc1 = new_column1, 
         nc2 =new_column2),
  c(expanded_time, time) )

returns_expanded_cols * decFac
# returns only values where the date index of each object matches,
# including the two new columns, `nc1` and `nc2`
#                  AAPL         DISCA           IBM           JNJ            KO           NKE           TXN        nc1        nc2
# 2014-12-23 -9.140321e-05  3.549359e-04  1.273429e-04 -6.006781e-04  3.744152e-04  1.074111e-05 -1.880165e-05 0.02576202 0.02576202
# 2014-12-24 -1.253736e-04 -1.442384e-04 -6.884995e-05  7.883771e-05 -1.854880e-05  8.840947e-05  3.875721e-05 0.02655878 0.02655878
# 2014-12-26  4.797723e-04 -1.569965e-05  8.784789e-05  1.227638e-04  1.275205e-05  1.531169e-04 -5.745789e-05 0.02738019 0.02738019
# 2014-12-29 -1.981707e-05            NA            NA  7.244804e-05 -6.577958e-05            NA            NA 0.02822700 0.02822700
# 2014-12-30 -3.572808e-04            NA            NA  8.287246e-06 -6.797960e-05            NA            NA 0.02910000 0.02910000
# 2014-12-31 -5.760617e-04 -6.588939e-04  7.301178e-05 -2.257898e-04 -3.812713e-04 -2.269089e-04 -2.597874e-04 0.03000000 0.03000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM