对于每个项目,计算数组中的其他 2 个项目

For each item, calculate with 2 other items in an array

对于一个学校项目,我正在尝试计算在给定历史日期由三只股票组成的表现最好的投资组合(夏普比率)。

我已经知道如何收集信息并计算平均值 returns 和方差 python。但是,我不知道如何遍历数据并比较 所有可能投资组合的结果。

例如:

result 1 = stockA + stock B + stock C
result 2 = stockB + Stock C + Stock D
result 3 = stockC + Stock D + Stock E

等等等等

更复杂的是,股票将被加权。我知道在每种组合中对 3 只股票进行所有可能的加权将需要几天的时间来计算,所以我打算做一些更精简的事情:

# checks to see which stock will perform better with higher weight:

trialstockA = (stockA * .75) + (stockB * .125) + (stockC * .125)
trialstockB = (stockA * .125) + (stockB * .75) + (stockC * .125)
trialstockC = (stockA * .125) + (stockB * .125) + (stockC * .75)
 
# if trialstockA should be weighed higher:

if trialstockA > trialstockB and trialstockC:

trialstockA2 = (stockA * .95) + (stockB * .025) + (stockC * .025)
trialstockA3 = (stockA * .85) + (stockB * .075) + (stockC * .075)
trialstockA4 = (stockA * .80) + (stockB * .10) + (stockC * .10)
trialstockA5 = (stockA * .70) + (stockB * .15) + (stockC * .15)
trialstockA6 = (stockA * .65) + (stockB * .175) + (stockC * .175)
trialstockA7 = (stockA * .60) + (stockB * .20) + (stockC * .20)
trialstockA8 = (stockA * .55) + (stockB * .225) + (stockC * .225)
trialstockA9 = (stockA * .50) + (stockB * .25) + (stockC * .25)

不过这部分不太重要。我只是不确定如何遍历每个可能的投资组合。我可以将数据导出到一个 CSV 文件,在该文件中它将显示为 [NAME (string) ,MEAN RETURN (INT) ,VARIANCE (INT), RISK (INT)],例如 [AAPL, .427, .1957, .442],或者我可以将它们作为变量留在 scrapy 项目中。不过我觉得 CSV 方法可能更简单。

我也想知道将股票分成三组是否有帮助?

希望这是有道理的。不知道还有什么要说的...

提前感谢您的帮助!

更新

对于每种股票组合和每种权重组合,假设股票组合名为 stockA、stockB 和 StockC,目标是进行以下计算。为了节省时间,我准备将每只股票的数据导出到一个csv中,其中股票的均值、方差、标准差(风险)和returns(大约100个数字)的列表存储在每行股票的不同列。

首先需要找到两只股票的每种组合之间的相关性:

stockAB_corr = numpy.corrcoef(stockA_returns, stockB_returns)[0, 1]
stockAC_corr = numpy.corrcoef(stockA_returns, stockC_returns)[0, 1]
stockBC_corr = numpy.corrcoef(stockB_returns, stockC_returns)[0, 1]

那么,我们可以运行下面的函数:

portfolio_return =  (stockA[avgret] * stockA[weight]) + (stockB)
portfolio_variance = ((stockA[weight]^2) * stockA[variance]) + ((stockB[weight]^2) * stockB[variance]) + ((stockC[weight]^2) * stockC[variance]) + (2*stockA[weight]*stockB[weight]*stockAB_corr*stockA[risk]*stockB[risk]) + (2*stockA[weight]*stockC[weight]*stockAC_corr*stockA[risk]*stockC[risk]) + (2*stockB[weight]*stockC[weight]*stockBC_corr*stockB[risk]*stockC[risk]) 
portfolio_risk = portfolio_variance ** 0.5

Sharpe = (porfolio_return - 0.03)/portfolio_variance

夏普比率是最终结果。对于每只股票,它们各自的方差和平均值已经计算出来。

根据@Aaron 的评论,您可以使用 itertools module, specifically combinations and permutations

您可以使用 combinations 从所有可能的股票列表中创建一个股票集合,使用 permutationsset 的唯一权重排序,使用 [=18] 组合它们=],并且 yield 它们来自生成器函数。

import itertools

stocks = ['stock{}'.format(x) for x in xrange(10)]
weights_list = [(0.95, 0.025, 0.025),
               (0.90, 0.05, 0.05),
               (0.85, 0.075, 0.075),
               (0.80, 0.1, 0.1),
               (0.75, 0.125, 0.125),
               (0.70, 0.15, 0.15),
               (0.65, 0.175, 0.175),
               (0.60, 0.20, 0.20),
               (0.55, 0.225, 0.225),
               (0.50, 0.25, 0.25)]


def portfolios(stocks, weights_list):
    for stock_triplet in itertools.combinations(stocks, 3):
        for weights in weights_list:
            unique_weight_orders = set(itertools.permutations(weights))
            for weight_order in unique_weight_orders:
                yield zip(stock_triplet, weight_order)


for port in portfolios(stocks, weights_list):
    print port

输出将如下所示:

>>> ...
[('stock0', 0.95), ('stock1', 0.025), ('stock2', 0.025)]
[('stock0', 0.025), ('stock1', 0.025), ('stock2', 0.95)]
[('stock0', 0.025), ('stock1', 0.95), ('stock2', 0.025)]
[('stock0', 0.075), ('stock1', 0.075), ('stock2', 0.85)]
[('stock0', 0.075), ('stock1', 0.85), ('stock2', 0.075)]
[('stock0', 0.85), ('stock1', 0.075), ('stock2', 0.075)]
[('stock0', 0.1), ('stock1', 0.1), ('stock2', 0.8)]
...