-2

I have been having trouble following all the montecarlo tutorials as they are seem to start to advanced for me to follow. I have a few months of python experience. So far I have been unable to find any on the basics. Any tips or links to the basics of montecarlo and python would be great.

Im looking to make a simple montecarlo simulation. One simulation would involve selecting a random outcome for event 1 from the distribution and then allocating a score. Then another random outcome for event 2 and so on. So 1 run of the sim would give me 3 scores. Thanks in advance for any help with this

I will be trying to rum a sim 10000 times approx on a bigger dataset. Should I be trying to do this in numpy?

Probability distribution

         outcome 1  outcome 2  outcome 3  outcome 4  outcome 5
event 1        0.1        0.2        0.5        0.6          1
event 2        0.1        0.3        0.4        0.7          1
event 3        0.1        0.5        0.6        0.7          1

Scores

         outcome 1  outcome 2  outcome 3  outcome 4  outcome 5
score 1        100        400        200        600        100
score 2        200        600        300        700        500
score 3        400        100        500        300        200
Drive
  • 1
  • 1
  • Hi! Maybe look into https://pandas.pydata.org/? DataFrames are really good for bigger datasets and are also fast... It is based on numy – Aljaž Medič Aug 30 '20 at 19:26
  • I don't understand what you're really trying to achieve. Do you want to run a Monte Carlo Tree Search algorithm ? Or only to select one value randomly inside an array ? – mlisthenewcool Aug 30 '20 at 19:39

2 Answers2

0

Is that what you are looking for ?

import numpy as np


if __name__ == "__main__":
    n_events = 3
    n_scores = 3
    n_outcomes = 5

    events = np.random.random((n_events, n_outcomes))
    scores = np.random.random((n_scores, n_outcomes))

    print("events", events.shape, events, "\n")
    print("scores", scores.shape, scores, "\n")

    run_scores = np.zeros(n_events)
    for run_idx in range(n_events):
        selected_idx = np.random.choice(n_outcomes, 1)
        run_scores[run_idx] = scores[run_idx][selected_idx]

    print("run_scores", run_scores.shape, run_scores)
mlisthenewcool
  • 539
  • 3
  • 14
0

The random module in python is particularly useful when dealing with random simulation problems. You can use the random.choices() function to simulate the above experiment.

The choices() function lets you specify the outcomes and the corresponding weights, along with the number of simulations to run. The function returns a list of the results. We can use a collections.Counter object to tabulate the results and get it in dictionary form.

from random import choices
from collections import Counter

"""
         outcome 1  outcome 2  outcome 3  outcome 4  outcome 5
event 1        0.1        0.2        0.5        0.6          1
"""
# outcomes and weights for event 1 as per the probability distribution you provided
outcomes = [1, 2, 3, 4, 5]
cumulative_weights = [0.1, 0.2, 0.5, 0.6, 1]
num_simulations = 10000

scores_list = choices(population=outcomes, cum_weights=cumulative_weights, k=num_simulations)

# Use a Counter to tabulate our results. This will give us a dict-like object
scores = Counter(scores_list)

for outcome in outcomes:
    print("Outcome {}, Score: {}".format(outcome, scores[outcome]))

# Output ->>
# Outcome 1, Score: 1022
# Outcome 2, Score: 1009
# Outcome 3, Score: 2983
# Outcome 4, Score: 1045
# Outcome 5, Score: 3941

The code sample above demonstrates how to run 10,000 simulations of one event. Use multiple sets of outcomes/weights as per your requirement.

Aaron Alphonso
  • 336
  • 3
  • 6