It is necessary to solve the problem with the help of Python: There is a set of financial instruments (even 10 pieces). There is the amount of the investment portfolio.
It is necessary to find ALL possible distribution of shares from the total amount of the investment portfolio attributable to each instrument (including 0%). The total amount should be = 100%. Step set (albeit 2%). Those. percent values 0,2,4,6 ..100%. Example: [14, 6, 10, 6, 10, 0, 16, 24, 2, 12].
Option vlob:
step = 2.0 num_of_tickers = 10 steps = math.floor(100.0 / step) + 1 share_lst = [idx * step for idx in range(0, steps)] comb_all = itertools.product(share_lst, repeat=num_of_tickers) comb_res = list(filter(lambda x: sum(x) == 100.0, comb_all)) Considers very long.
I tried tricks that reduce the set by the condition <= 100%:
comb_all = itertools.product(share_lst, repeat=5) comb_all_lst = list(filter(lambda x: sum(x) <= 100.0, comb_all)) for lst in comb_all_lst: for lst_1 in comb_all_lst: if (sum(lst) + sum(lst_1)) == 100.0: comb_res.append(lst + lst_1) Already more real, but still long.
How to achieve faster work speed?
Example for step = 25.0 and the number of tools num_of_tickers = 4:
Possible percentage values:
[0.0, 25.0, 50.0, 75.0, 100.0] The output should be a table of distribution of the form:
0, 0, 0, 100 0, 0, 25, 75 0, 25, 0, 75 25, 0, 0, 75 25, 0, 25, 50 ... 100, 0, 0, 0
num_of_tickers,MAX_SHARE_SUM- MaxU