Home > Blockchain >  Not returning the amount of sample requested
Not returning the amount of sample requested

Time:10-05

I tried using this code to run 10000 samples in num_months, but it's only limiting me to 9000 samples. Not sure why this is happening.

    def simulate_demand(num_months):
        simulated_months =[0]
        for months in range(num_months-1):
            simulated_months.append(rng.choice([1,0],p=[0.7,0.3]))

        simulated_months_distribution = [rng.normal(800,150)]

        for x in range(len(simulated_months)):
            if simulated_months[x] == 1:
                simulated_months_distribution.append(rng.normal(500,100))

            elif simulated_months[x] == 0:


                last_promotion = 0

                months_number = 1 

                while simulated_months[x-months_number] == 1:
                    last_promotion  = 1
                    months_number  = 1

                    if simulated_months[x-months_number] == 0:
                        t = last_promotion

                        simulated_months_distribution.append(rng.normal(800 (100*t),150))
        simulated_months_distribution_df = pd.DataFrame(simulated_months_distribution)
        simulated_months_distribution_df.plot(style='r--', figsize=(20,10))
        return simulated_months_distribution_df

CodePudding user response:

The problem is in the if statement in the while loop. It's only run if there is at least 1 simulated_month with 1 before a 0. This doesn't happen always, and thus you are missing some simulated distributions.

I believe what you want if to append the distribution even if that's not the case. The while loop is only for incrementing the t and subsequently, the mean of the distribution.

The solution is then to move the if out of the while loop. And the if is actually not necessary since it will always be true after the while loop.

def simulate_demand(num_months):
    simulated_months = [0]
    for months in range(num_months - 1):
        simulated_months.append(rng.choice([1, 0], p=[0.7, 0.3]))

    simulated_months_distribution = [rng.normal(800, 150)]

    for x in range(len(simulated_months)):
        if simulated_months[x] == 1:
            simulated_months_distribution.append(rng.normal(500, 100))

        elif simulated_months[x] == 0:

            last_promotion = 0

            months_number = 1

            while simulated_months[x - months_number] == 1:
                last_promotion  = 1
                months_number  = 1

            # the if is not necessary since it's always true after the while loop
            t = last_promotion

            # this should be run regardless whether the while loop runs so it should be outside
            simulated_months_distribution.append(rng.normal(800   (100 * t), 150))

    simulated_months_distribution_df = pd.DataFrame(simulated_months_distribution)
    simulated_months_distribution_df.plot(style='r--', figsize=(20, 10))
    return simulated_months_distribution_df
  • Related