def generate_dollar_volume_weights(close, volume):
"""
Generate dollar volume weights.
Parameters
----------
close : DataFrame
Close price for each ticker and date
volume : str
Volume for each ticker and date
Returns
-------
dollar_volume_weights : DataFrame
The dollar volume weights for each ticker and date
"""
assert close.index.equals(volume.index)
assert close.columns.equals(volume.columns)
#TODO: Implement function
dollar_volume_weights=np.cumsum(close * volume) / np.cumsum(volume)
return dollar_volume_weights
project_tests.test_generate_dollar_volume_weights(generate_dollar_volume_weights)#testing the function
所以我的结果如下
INPUT close:
XHNP FCUB ZYRP
2005-09-09 35.44110000 34.17990000 34.02230000
2005-09-10 92.11310000 91.05430000 90.95720000
2005-09-11 57.97080000 57.78140000 58.19820000
2005-09-12 34.17050000 92.45300000 58.51070000
INPUT volume:
XHNP FCUB ZYRP
2005-09-09 9836830.00000000 17807200.00000000 8829820.00000000
2005-09-10 82242700.00000000 68531500.00000000 48160100.00000000
2005-09-11 16234800.00000000 13052700.00000000 9512010.00000000
2005-09-12 10674200.00000000 56831300.00000000 9316010.00000000
OUTPUT dollar_volume_weights:
XHNP FCUB ZYRP
2005-09-09 35.44110000 34.17990000 34.02230000
2005-09-10 86.05884636 79.32405834 82.13590461
2005-09-11 81.84884187 76.49494177 78.71200871
2005-09-12 77.57172242 82.30022612 76.22980471
EXPECTED OUTPUT FOR dollar_volume_weights:
XHNP FCUB ZYRP
2005-09-09 0.27719777 0.48394253 0.23885970
2005-09-10 0.41632975 0.34293308 0.24073717
2005-09-11 0.41848548 0.33536102 0.24615350
2005-09-12 0.05917255 0.85239760 0.08842984
我是这里的初学者,我真的不明白我在这里缺少什么,我认为用于生成美元交易量权重的表达式应该类似于dollar_volume_weights=np.cumsum(close * volume) / np.cumsum(volume)
有人可以告诉我为什么我的结果与众不同吗?
答案 0 :(得分:1)
我认为您应该进行close * volume
,然后除以np.sum(x, axis=1)
的总和(volume * close
)并指定axis=1
。此外,您可以使用div()
函数进行此划分并指定axis=0
。
答案 1 :(得分:0)
您能解释一下您希望如何从输入中计算出dollar_volume_weights以获得预期结果吗?
为清楚代码的作用,以下内容显示了计算结果的逻辑,
2005-09-09 close * volume / volume ...
2005-09-10 (close1*volume1) + (close2*volume2) / (volume1+volume2) ...
答案 2 :(得分:0)
我在这里同意@joseph。我假设您正在谈论Udacity的Smart Beta项目。他们在先前牢房中的榜样令人误解,这让我很困惑。无论如何,我解决问题的方式如下:
def generate_dollar_volume_weights(close, volume):
"""
Generate dollar volume weights.
Parameters
----------
close : DataFrame
Close price for each ticker and date
volume : str
Volume for each ticker and date
Returns
-------
dollar_volume_weights : DataFrame
The dollar volume weights for each ticker and date
"""
assert close.index.equals(volume.index)
assert close.columns.equals(volume.columns)
#TODO: Implement function
print(close)
print(volume)
dollar_volume = close * volume
print(dollar_volume)
total_dollar_volume = dollar_volume.sum(axis=1)
print(total_dollar_volume)
dollar_volume_weights = dollar_volume.div(total_dollar_volume,axis=0)
print(dollar_volume_weights)
return dollar_volume_weights
project_tests.test_generate_dollar_volume_weights(generate_dollar_volume_weights)