Search code examples
pythonlistsimulationmontecarlo

Creating a N sets of combinations of variables from the given range of each variable


This may be a very vague question -- I apologize in advance.

y is a function of a,b,c,d,e. a can go from 1 to 130; b from 0.5 to 1; c from 3 to 10; d from 0 to 1; and e is 1-d.

Is there a way in python I can create N (say, 10,000) sets of combinations of a,b,c,d and e from the given range?


Solution

  • itertools is a great library for creating iterators like that. In your case, it is product that we need. However, you cannot set the number of point you want. You are going to have to derive that mathematically and translate it into steps (the 1s at the end of the range() function).

    a = [i for i in range(1, 131, 1)]
    b = [i/10 for i in range(5, 11,1)]
    c = [i for i in range(3, 11, 1)]
    d = [i/10 for i in range(0, 11, 1)]
    
    from itertools import product
    
    combs = product(a, b, c, d)
    for i, x in enumerate(combs, 1):
        x = list(x)
        x.append(1-x[-1])
        print(i, x)  # example print: 52076 [99, 0.8, 9, 0.1, 0.9]
    

    The example above produces: 130 * 6 * 8 * 11 = 68640 combinations

    These are more than you requested but you get the point. You can also decide to have a variable more ro less finely discretised.

    I am also assuming a & c are integer variables..