def gen_global_investment(budget):
    investment, budget = gen_investment(budget)
    print("Accessing all companies")
    while True:
        i = 0
        for c in d.companies:
            if budget >= d.stock_cost[c]:
                curr_vol = \
                    e.volatility(list(zip(
                        [d.stock_cost[c_] for c_ in d.companies],
                        investment.values())))
                print("Budget | Volatility:\t {} | {}"
                      .format(budget, curr_vol))
                if isnan(curr_vol):
                    curr_vol = float('inf')
                investment[c] += 1
                budget -= d.stock_cost[c]
                new_vol = \
                    e.volatility(list(zip(
                        [d.stock_cost[c_] for c_ in d.companies],
                        investment.values())))
                if new_vol >= curr_vol:
                    investment[c] -= 1
                    budget += d.stock_cost[c]
            else:
                i += 1
                if i > 5:
                    break
        if i > 5:
            break
    return (e.volatility(
        list(zip(
            [d.stock_cost[c] for c in d.companies], investment.values()))),
            ' '.join([str(investment[company]) for company in d.companies]))
def gen_investment(budget):
    _investment = dict(zip(d.companies, [0] * len(d.companies)))
    investment = OrderedDict(sorted(_investment.items(), key=lambda x: x[0]))
    for _, co1, co2 in tqdm(d.magic_pairs):
        if budget >= d.stock_cost[co1] + d.stock_cost[co2]:
            curr_vol = \
                e.volatility(list(zip(
                    [d.stock_cost[c] for c in d.companies],
                    investment.values())))
            if isnan(curr_vol):
                curr_vol = float('inf')
            investment[co1] += 1
            investment[co2] += 1
            budget -= d.stock_cost[co1] + d.stock_cost[co2]
            new_vol = \
                e.volatility(list(zip(
                    [d.stock_cost[c] for c in d.companies],
                    investment.values())))
            if new_vol >= curr_vol:
                investment[co1] -= 1
                investment[co2] -= 1
                budget += d.stock_cost[co1] + d.stock_cost[co2]
        else:
            if budget >= d.stock_cost[co1]:
                curr_vol = \
                    e.volatility(list(zip(
                        [d.stock_cost[c] for c in d.companies],
                        investment.values())))
                investment[co1] += 1
                budget -= d.stock_cost[co1]
                new_vol = \
                    e.volatility(list(zip(
                        [d.stock_cost[c] for c in d.companies],
                        investment.values())))
                if new_vol >= curr_vol:
                    investment[co1] -= 1
                    budget += d.stock_cost[co1]
            elif budget >= d.stock_cost[co2]:
                curr_vol = \
                    e.volatility(list(zip(
                        [d.stock_cost[c] for c in d.companies],
                        investment.values())))
                investment[co2] += 1
                budget -= d.stock_cost[co2]
                new_vol = \
                    e.volatility(list(zip(
                        [d.stock_cost[c] for c in d.companies],
                        investment.values())))
                if new_vol >= curr_vol:
                    investment[co2] -= 1
                    budget += d.stock_cost[co2]
    # Return the current investment and money left up to now
    return investment, budget