初级python和montecarlo
我一直很难遵循所有的蒙地卡罗教程,因为它们似乎开始让我遵循先进。我有几个月的python经验。到目前为止,我还找不到任何关于基础的东西。任何有关montecarlo和python基础知识的提示或链接都将非常有用 我想做一个简单的蒙特卡洛模拟。一个模拟包括从分布中选择事件1的随机结果,然后分配分数。然后是事件2的另一个随机结果,依此类推。所以一次模拟会给我3分。提前感谢您在这方面的帮助 我将尝试在一个更大的数据集上对一个sim卡进行大约10000次rum。我应该在numpy做这件事吗 概率分布初级python和montecarlo,python,montecarlo,Python,Montecarlo,我一直很难遵循所有的蒙地卡罗教程,因为它们似乎开始让我遵循先进。我有几个月的python经验。到目前为止,我还找不到任何关于基础的东西。任何有关montecarlo和python基础知识的提示或链接都将非常有用 我想做一个简单的蒙特卡洛模拟。一个模拟包括从分布中选择事件1的随机结果,然后分配分数。然后是事件2的另一个随机结果,依此类推。所以一次模拟会给我3分。提前感谢您在这方面的帮助 我将尝试在一个更大的数据集上对一个sim卡进行大约10000次rum。我应该在numpy做这件事吗 概率分布
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
得分
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
这就是你要找的吗
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)
python中的随机模块在处理随机模拟问题时特别有用。您可以使用该函数来模拟上述实验 通过“选择”功能,可以指定结果和相应的权重,以及要运行的模拟数。函数返回一个结果列表。我们可以使用一个对象将结果制成表格,并以字典的形式得到它
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
上面的代码示例演示了如何对一个事件运行10000次模拟。根据您的要求使用多组结果/权重。Hi!也许调查一下?数据帧对于更大的数据集非常有用,而且速度也很快。。。这是基于numyI的,我不明白你真正想要实现什么。是否要运行蒙特卡罗树搜索算法?还是仅在数组中随机选择一个值?