投资组合权重的所有可能组合

时间:2014-04-03 14:58:41

标签: python list combinations portfolio

我正在寻找提示和技巧,以便在Python中更好地编写以下代码(例如,删除多余的循环和复制,使用更多的拼接)

我已对此编码,以便为受限制的N种证券组合创建所有可能的权重组合:

权重来自可能性列表(在这种情况下为0,.1,.2,.3,.4,.5) 有效投资组合的权重总和必须= 1(完全投资)

这不是一种有效的方法,因为组合的数量很快变得无法管理。只是我试图掌握这种语言。

先谢谢你的帮助!

import copy as cp

def generateWeights (weights,possibleWeights,N):
    "Generate all possible combinations of weights"
    # cycle over number of desired coloumns
    for i in range(0,N):
        # copy weights to iterate over every element while i pop stuff out of
        # the original list
        weightsCopy = cp.deepcopy(weights)
        for w in weightsCopy:
            # make a copy to edit
            wtemp = cp.deepcopy(w)
            for p in possibleWeights:
                # append every possibility 
                wtemp.append(p)
                # I only want combinations with sum == 1 so I can start
                # avoiding those that are > 1
                if sum(wtemp) <= 1:
                    weights.append(cp.deepcopy(wtemp))
                # get the original wtemp back so I can work on it with my next p
                wtemp.pop()
            # finished developing the first line of the table. Pop it out and
            # move on.
            weights.pop(0)
    # once again copy weights to iterate over every element while I edit the 
    # original list
    weightsCopy = cp.deepcopy(weights)        
    for w in weightsCopy:
        # remove all possibilities whose sum < 1
        # all those > 1 were never added
        if sum(w) < 1:
            weights.remove(w)
    return weights

N=6 # Number of securities
possibleWeights = [0.0,0.1,0.2,0.3,0.4,0.5]
# weights is a coloumn because I want to access its elements and still get
# lists instead of floats.
weights = [[0.0],[0.1],[0.2],[0.3],[0.4],[0.5]]
weights = generateWeights(weights,possibleWeights,N)

2 个答案:

答案 0 :(得分:2)

您应该使用已经拥有算法的itertools模块来完成您想要的大部分工作。

from itertools import combinations

def valid_combinations(weights):
    '''generator of possible combinations of weights elements that add up to 1'''
    list_length = len(weights) # we will need this
    for lengths in range(list_length):
        for possible in combinations(weights, lengths): # all possible orderings of weights
            if sum(possible[:lengths]) == 1: # only generate valid ones
                yield possible[:lengths]


>>> original = [0, .1, .2, .3, .4, .5]
>>> print list(valid_combinations(original))
[(0.1, 0.4, 0.5), (0.2, 0.3, 0.5), (0, 0.1, 0.4, 0.5), (0, 0.2, 0.3, 0.5), (0.1, 0.2, 0.3, 0.4), (0, 0.1, 0.2, 0.3, 0.4)]

如果您只对权重的唯一组合感兴趣(顺序无关紧要),则需要使用combinations,如果确实重要,则应使用permutations以下方式:

from itertools import permutations

def valid_combinations(weights):
    '''generator of possible combinations of weights elements that add up to 1'''
    list_length = len(weights) # we will need this
    for possible in permutations(weights): # all possible orderings of weights
        for lengths in range(list_length): # get all prefix sublists
            if sum(possible[:lengths]) == 1: # only generate valid ones
                yield possible[:lengths]


>>> original = [0, .1, .2, .3, .4, .5]
>>> print list(valid_combinations(original))
>>> [(0, 0.1, 0.2, 0.3, 0.4), (0, 0.1, 0.2, 0.4, 0.3), (0, 0.1, 0.3, 0.2, 0.4), (0, 0.1, 0.3, 0.4, 0.2), (0, 0.1, 0.4, 0.2, 0.3), (0, 0.1, 0.4, 0.3, 0.2), (0, 0.1, 0.4, 0.5), (0, 0.1, 0.4, 0.5), (0, 0.1, 0.5, 0.4), (0, 0.1, 0.5, 0.4), (0, 0.2, 0.1, 0.3, 0.4), (0, 0.2 ...

答案 1 :(得分:1)

您可以使用itertools.combinations(),但是在达到数据集的长度之前,您必须增加组合的大小。

>>> input_list = [0,.1,.2,.3,.4,.5]
>>> from itertools import combinations
>>> valid_combinations = []
>>> for comb_length in range(1,len(input_list)+1):
    possible_combinations = combinations(input_list,comb_length)
    for comb in possible_combinations:
        if sum(comb) ==1:
            valid_combinations.append(comb)


>>>valid_combinations
[(0.1, 0.4, 0.5), (0.2, 0.3, 0.5), (0, 0.1, 0.4, 0.5), (0, 0.2, 0.3, 0.5), (0.1, 0.2, 0.3, 0.4), (0, 0.1, 0.2, 0.3, 0.4)]

阅读您的要求并进行更新,以获得==1组合,而不是<= 1

注意 - 如果输入的数据集非常大,则需要更好的算法,因为这是强力。