我有以下价格数据:
__getitem__
我使用IndexableEnumMeta
函数遍历所有列,并使用Quantmod软件包中的Level
函数计算每日收益。
IndexableEnum
这给我们:
import enum
from itertools import islice
import ast
import inspect
tree = ast.parse(inspect.getsource(enum))
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
if node.name == 'EnumMeta':
node.name = 'IndexableEnumMeta'
elif node.name == 'Enum':
node.name = 'IndexableEnum'
node.keywords[0].value.id = 'IndexableEnumMeta'
elif isinstance(node, ast.Name) and node.id == 'Enum':
node.id = 'IndexableEnum'
code = compile(tree, inspect.getfile(enum), 'exec')
scope = {}
exec(code, scope)
def IndexableEnumMeta__getitem__(cls, index):
if isinstance(index, slice):
return [cls._member_map_[i] for i in islice(cls._member_map_, index.start, index.stop, index.step)]
if isinstance(index, int):
return cls._member_map_[next(islice(cls._member_map_, index, index + 1))]
return cls._member_map_[index]
scope['IndexableEnumMeta'].__getitem__ = IndexableEnumMeta__getitem__
class Level(scope['IndexableEnum']):
DATA_CHECK = "data check"
DESIGN_CHECK = "design check"
ALERT = "alert"
# Level[1:3] returns [<Level.DESIGN_CHECK: 'design check'>, <Level.ALERT: 'alert'>]
# Level[1] returns Level.DESIGN_CHECK
最后,我将新创建的treas <- read.csv(file = 'treas.csv', header = TRUE, stringsAsFactors = FALSE)
2YR 3YR 5YR 7YR 10YR 30YR
0.41 0.85 1.65 2.18 2.6 3.43
0.41 0.85 1.65 2.2 2.61 3.45
0.4 0.82 1.63 2.17 2.59 3.44
0.41 0.86 1.66 2.19 2.6 3.44
0.43 0.88 1.69 2.22 2.62 3.45
0.45 0.93 1.71 2.24 2.64 3.47
0.44 0.91 1.7 2.23 2.65 3.47
0.42 0.88 1.66 2.17 2.58 3.41
0.45 0.93 1.7 2.21 2.6 3.41
0.49 0.95 1.71 2.21 2.61 3.4
0.51 0.99 1.77 2.27 2.66 3.44
0.48 0.95 1.71 2.21 2.61 3.43
0.48 0.94 1.71 2.22 2.64 3.47
0.5 0.94 1.71 2.22 2.63 3.44
0.48 0.96 1.72 2.23 2.63 3.45
0.49 0.95 1.7 2.19 2.59 3.41
0.48 0.92 1.68 2.17 2.57 3.38
0.46 0.9 1.64 2.14 2.53 3.35
0.45 0.88 1.64 2.14 2.54 3.36
0.47 0.88 1.62 2.13 2.53 3.34
0.47 0.9 1.66 2.17 2.58 3.4
0.49 0.95 1.71 2.22 2.64 3.46
0.52 0.98 1.74 2.25 2.65 3.47
0.52 1 1.74 2.24 2.63 3.44
0.51 0.99 1.7 2.19 2.58 3.38
0.51 0.97 1.68 2.17 2.57 3.37
0.46 0.93 1.66 2.15 2.55 3.38
0.48 0.92 1.65 2.13 2.53 3.34
0.48 0.95 1.68 2.17 2.55 3.36
数据帧传递给apply()
函数。
不幸的是,我们得到:
Delt()
apply(treas, 2, Delt)
给我们:
> treas_ret
X2YR X3YR X5YR X7YR X10YR X30YR
[1,] NA NA NA NA NA NA
[2,] 0.000000000 0.000000000 0.000000000 0.009174312 0.003846154 0.005830904
[3,] -0.024390244 -0.035294118 -0.012121212 -0.013636364 -0.007662835 -0.002898551
[4,] 0.025000000 0.048780488 0.018404908 0.009216590 0.003861004 0.000000000
[5,] 0.048780488 0.023255814 0.018072289 0.013698630 0.007692308 0.002906977
[6,] 0.046511628 0.056818182 0.011834320 0.009009009 0.007633588 0.005797101
[7,] -0.022222222 -0.021505376 -0.005847953 -0.004464286 0.003787879 0.000000000
[8,] -0.045454545 -0.032967033 -0.023529412 -0.026905830 -0.026415094 -0.017291066
[9,] 0.071428571 0.056818182 0.024096386 0.018433180 0.007751938 0.000000000
[10,] 0.088888889 0.021505376 0.005882353 0.000000000 0.003846154 -0.002932551
[11,] 0.040816327 0.042105263 0.035087719 0.027149321 0.019157088 0.011764706
[12,] -0.058823529 -0.040404040 -0.033898305 -0.026431718 -0.018796992 -0.002906977
[13,] 0.000000000 -0.010526316 0.000000000 0.004524887 0.011494253 0.011661808
[14,] 0.041666667 0.000000000 0.000000000 0.000000000 -0.003787879 -0.008645533
[15,] -0.040000000 0.021276596 0.005847953 0.004504505 0.000000000 0.002906977
[16,] 0.020833333 -0.010416667 -0.011627907 -0.017937220 -0.015209125 -0.011594203
[17,] -0.020408163 -0.031578947 -0.011764706 -0.009132420 -0.007722008 -0.008797654
[18,] -0.041666667 -0.021739130 -0.023809524 -0.013824885 -0.015564202 -0.008875740
[19,] -0.021739130 -0.022222222 0.000000000 0.000000000 0.003952569 0.002985075
[20,] 0.044444444 0.000000000 -0.012195122 -0.004672897 -0.003937008 -0.005952381
[21,] 0.000000000 0.022727273 0.024691358 0.018779343 0.019762846 0.017964072
[22,] 0.042553191 0.055555556 0.030120482 0.023041475 0.023255814 0.017647059
[23,] 0.061224490 0.031578947 0.017543860 0.013513514 0.003787879 0.002890173
[24,] 0.000000000 0.020408163 0.000000000 -0.004444444 -0.007547170 -0.008645533
[25,] -0.019230769 -0.010000000 -0.022988506 -0.022321429 -0.019011407 -0.017441860
[26,] 0.000000000 -0.020202020 -0.011764706 -0.009132420 -0.003875969 -0.002958580
[27,] -0.098039216 -0.041237113 -0.011904762 -0.009216590 -0.007782101 0.002967359
[28,] 0.043478261 -0.010752688 -0.006024096 -0.009302326 -0.007843137 -0.011834320
[29,] 0.000000000 0.032608696 0.018181818 0.018779343 0.007905138 0.005988024
我试图将treas_ret
传递给cov()
函数,但这似乎不起作用。
在传递给cov()函数之前,是否有办法确保每日收益是数值?
谢谢!
答案 0 :(得分:0)
treas_ret
已经是一个数字矩阵。这是由于在矩阵中存在NA
,因此在使用NA
时,所有值都为cov
。
您可以删除NA
,然后使用cov
library(quantmod)
treas_ret <- apply(treas, 2, function(x) na.omit(Delt(x)))
cov(treas_ret)
# X2YR X3YR X5YR X7YR X10YR X30YR
#X2YR 0.00189071490 0.0009671260 0.0004751261 0.0003592459 0.0002632325 0.00006466673
#X3YR 0.00096712596 0.0009824173 0.0004852795 0.0003843313 0.0002665753 0.00014473384
#X5YR 0.00047512612 0.0004852795 0.0003163515 0.0002553111 0.0001938068 0.00011919805
#X7YR 0.00035924594 0.0003843313 0.0002553111 0.0002219907 0.0001694694 0.00011048543
#X10YR 0.00026323252 0.0002665753 0.0001938068 0.0001694694 0.0001472908 0.00010040032
#X30YR 0.00006466673 0.0001447338 0.0001191980 0.0001104854 0.0001004003 0.00008497829
另一种选择是使用@ H1所建议的use = "complete.obs"
,它会得到相同的结果。
treas_ret <- apply(treas, 2, Delt)
cov(treas_ret, use = "complete.obs")