Restarting a loop after a specific number of iterations?

Viewed 45

I'm new to coding but is being asked to create a simulation of a 10-year experiment 1000 times. I have it at a number lower than 1000 to speed up the testing process. This is a partial copy of my code, I can get the other parameters of the task to work but instead of stopping at and restarting for every 10-years, it seems to accumulate the results of the previous years'.

For example, the code is supposed to compound money earned the year following a 'Success,' while I can get it to compound, my code seems to compound into year 11 and 12 instead of stopping at 10 and essentially restarting at year 1.

I tried .count() to keep track of how many elements I'm iterating through and also tried the while xyz function but I can't seem to get either to work.

for sim in range(5):
    for yr in range(10):
        experiment = "Success" if np.random.random() <= 0.1 else "Failure"
        expense = 25000
        margin = 0
        results1.append(experiment)
        expenses1.append(expense)
        margins1.append(margin)
        iter = 0
        if iter < 10:
            for i in range(len(results1)):
                if i + 1 < len(results1) and i - 1 >= 0:
                    if results1[i] == 'Success':
                        expenses1[i + 1] = 0
                        margins1[i + 1] = 10000
                    if results1[i - 1] == 'Success':
                        expenses1[i] = 0
                        if margins1[i] != 10000:
                            margins1[i] = 10000
                    if expenses1[i - 1] == 0:
                        expenses1[i] = 0
                        expenses1[i + 1] = 0
                    if margins1[i] >= 10000:
                        margins1[i + 1] = margins1[i] * 1.2
                    iter += 1
        else:
            continue
            iter = 0
all_data1 = zip(results1, expenses1, margins1)
df1 = pd.DataFrame(all_data1, columns=["Results", "R&D", "Margins"])
1 Answers

Let's look at the following simplified code snippet of your implementation.

import numpy as np

results1 = []

for sim in range(10):
    for yr in range(10):
        experiment = "Success" if np.random.random() <= 0.1 else "Failure"
        results1.append(experiment)

What is the problem here? Well, what I assume you want is that for every year we add a result to a list for that simulation. However, what is the difference between what you currently have, and this following code?

import numpy as np
    
results1 = []
    
for yr in range(10 * 10):
    experiment = "Success" if np.random.random() <= 0.1 else "Failure"
    results1.append(experiment)

Well, unless you left something out of your included code, it looks like not much! You want the simulation to reset after n years (in this example 10), but you don't change anything in your code! Here is an example of how this reset could be represented:

import numpy as np

results1 = []

for sim in range(10):
    sim_results1 = []
    for yr in range(10):
        experiment = "Success" if np.random.random() <= 0.1 else "Failure"
        sim_results1.append(experiment)
    
    results1.append(sim_results1)

Now, your results1 list will contain m lists (where m is the number of simulations) with each sublist showing whether the experiment was a success or failure over n years. In short: if you just add each experimental result to a big list that is the same across all simulation runs, it's not a surprise that it looks like you are simulating into year 11, 12, etc. What is actually happening is year 11 is really year 1 of simulation number 2, but you do not separate the simulations currently.

Related