我需要帮助打印出一份声明,该声明提供夏普比率最高的 A 和 B 各自分配的权重;这是从数据框派生的。到目前为止我做了什么:输入efficient_df = pd.DataFrame({'Weights_A':weighted_A, 'Weights_B':weighted_B, 'Portfolio Return': port_ret, 'Portfolio Std Dev': port_std, 'Sharpe Ratio': sharpe_ratio })print (efficient_df.head())print('\n')sharpe_highest = efficient_df[efficient_df['Sharpe Ratio'] == efficient_df['Sharpe Ratio'].max()]print('Optimal portfolio details: ')print(sharpe_highest)输出 Weights_A Weights_B Portfolio Return Portfolio Std Dev Sharpe Ratio0 0.00 1.00 0.001933 0.017561 0.1100811 0.01 0.99 0.001928 0.017386 0.1109162 0.02 0.98 0.001924 0.017212 0.1117603 0.03 0.97 0.001919 0.017040 0.1126124 0.04 0.96 0.001914 0.016869 0.113472Optimal portfolio details : Weights_A Weights_B Portfolio Return Portfolio Std Dev Sharpe Ratio51 0.51 0.49 0.001692 0.01148 0.147348我需要的是这样的声明:The best allocation is 0.51 of A and 0.49 of B.但是,下面的代码没有按预期工作。print('The best allocation is ', weighted_A == efficient_df['Sharpe Ratio'].max(), 'of A and ', weighted_B == efficient_df['Sharpe Ratio'].max(), ' of B.')结果The best allocation is False of A and False of B.
2 回答
斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
您只是打印布尔值而不是实际值。
(假设只有一行具有最大“Srape Ratio”)
sharpe_highest = efficient_df[efficient_df['Sharpe Ratio'] == efficient_df['Sharpe Ratio'].max()]
然后简单地打印:
print('The best allocation is ', float(sharpe_highest.weighted_A), 'of A and ', float(sharpe_highest.weighted_B), ' of B.')
jeck猫
TA贡献1909条经验 获得超7个赞
你得到布尔值(False 和 True)而不是你想要的浮点数,因为你使用“==”,它必然返回一个布尔值。除非我误解了,否则你不能使用“==”来获得你想要的输出。
print('The best allocation is ', weighted_GS == eff_portfolio_df['Sharpe Ratio'].max(), 'of GS and ', weighted_FB == eff_portfolio_df['Sharpe Ratio'].max(), ' of FB.')
添加回答
举报
0/150
提交
取消