0

I have code which produces a minority game model in Python. I am running my code for parameters with single values,e.g. N = 101 and it works fine, but I want to be able to run this for multiple values for the same parameter,e.g. N = 101,201,301. I can't just set a list since my code has a lot with numpy arrays, so need to match all my arrays etc. I have now stored my code into a function as such:

def mg_theory(N,time, S, M):
    # index to how we pick out a strategy
    index = 1 - 2 * np.random.rand(N, M, S)

    # allocating strategy randomly to all players for past m weeks
    # each strategy is different for all players, each number corresponds to a different strategy
    # some may have same strategy
    outcomes = np.random.rand(N, S)

    # guess randomly how many people are going for first m weeks
    history = np.random.randint(0, N, size=2 * M)
    history = np.reshape(history, (history.shape[0], 1))
....
etc 
(rest of code)

As you can see I have 4 parameters, 2 of which (N and M) I would like to have multiple values. Anyone has a clue?

simka999
  • 13
  • 3

1 Answers1

0

If I understand you correctly, one option would be to use a list comprehension as described here.

For your example you could run your function using:

[mg_theory(N, time="something", S="something", M) for N in [1, 2, 3] for M in [4, 5, 6]]

This would store your outputs as a list.

I know in your question you said your data are in numpy arrays, to make the above example work you would need to change the 2 hard-coded lists to be your actual data (I don't know what your array structure is so cannot include it in my answer).

Dharman
  • 21,838
  • 18
  • 57
  • 107
  • Yes Matt thanks for the help, that works wonderfully and my code works for all values now. In addition I wanted to ask how I could store these as a list for all values, my code loops with function "tqdm" about 200 times and it overwrites the previous list, any ideas on how we can get the values out? – simka999 Mar 04 '21 at 22:01
  • Just above the for loop you can put an empty list `my_list = []` and then at the end of the loop you add the output to the empty list, either `my_list.append(some_data)` or `my_list.extend(some_data)`. The differences between the 2 methods are explained [here](https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend). – Matt Arnold Mar 05 '21 at 10:36