searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

蒙特卡洛模拟

2024-11-08 09:21:13
0
0

import numpy as np

m = 10000
a = 0
b = 0
y1 = np.random.normal(loc=14,scale=2,size=m)
y2 = np.random.normal(loc=23,scale=3,size=m)
y3 = np.random.normal(loc=22,scale=4,size=m)
y =y1 + y2 + y3
a,b = np.mean(y),np.var(y)
print(a,b )

# (59.08396232409535, 29.120136564111085)
# 理论均值为59 = 14 + 23 + 22 方差为29 = 2*2 + 3*3 + 4*4

import math

import random


def play(change):
prize = random.randint(0, 2)
guess = random.randint(0, 2)
if guess == prize:
if change:
return False
else:
return True
else:
if change:
return True
else:
return False


def winRate(change, N):
win = 0
for i in range(N):
if play(change):
win += 1
print("中奖率为{}".format(win / N))


N = 1000000
print("每次换门的中奖概率:")
winRate(True, N)
print("每次都不换门的中奖概率:")
winRate(False, N)

# 理论换门2/3 不换门1/3
# 每次换门的中奖概率:
# 中奖率为0.665769
# 每次都不换门的中奖概率:
# 中奖率为0.33292


import time
import random

for i in range(10):
print(time.strftime("%Y-%m-%d %X", time.localtime()))
dou = {1994: {'褐色': 30, '黄色': 20, '红色': 20, '绿色': 10, '橙色': 10, '黄褐': 30},
1996: {'蓝色': 24, '绿色': 20, '橙色': 16, '黄色': 14, '红色': 13, '褐色': 13}}

num = 10000
list_1994 = ['褐色'] * 30 * num + ['黄色'] * 20 * num + ['红色'] * 20 * num + ['绿色'] * 10 * num + ['橙色'] * 10 * num + ['黄褐'] * 10 * num
list_1996 = ['蓝色'] * 24 * num + ['绿色'] * 20 * num + ['橙色'] * 16 * num + ['黄色'] * 14 * num + ['红色'] * 13 * num + ['褐色'] * 13 * num
random.shuffle(list_1994)
random.shuffle(list_1996)

count_all = 0
count_key = 0
for key in range(100 * num):
if list_1994[key] == '黄色' and list_1996[key] == '绿色':
count_all += 1
count_key += 1
if list_1994[key] == '绿色' and list_1996[key] == '黄色':
count_all += 1
print(count_key / count_all, 20 / 27)
print(time.strftime("%Y-%m-%d %X", time.localtime()))

# ...
# 0.7407064573459715 0.7407407407407407
# 20/27是理论答案
0条评论
作者已关闭评论
Top123
33文章数
3粉丝数
Top123
33 文章 | 3 粉丝
Top123
33文章数
3粉丝数
Top123
33 文章 | 3 粉丝
原创

蒙特卡洛模拟

2024-11-08 09:21:13
0
0

import numpy as np

m = 10000
a = 0
b = 0
y1 = np.random.normal(loc=14,scale=2,size=m)
y2 = np.random.normal(loc=23,scale=3,size=m)
y3 = np.random.normal(loc=22,scale=4,size=m)
y =y1 + y2 + y3
a,b = np.mean(y),np.var(y)
print(a,b )

# (59.08396232409535, 29.120136564111085)
# 理论均值为59 = 14 + 23 + 22 方差为29 = 2*2 + 3*3 + 4*4

import math

import random


def play(change):
prize = random.randint(0, 2)
guess = random.randint(0, 2)
if guess == prize:
if change:
return False
else:
return True
else:
if change:
return True
else:
return False


def winRate(change, N):
win = 0
for i in range(N):
if play(change):
win += 1
print("中奖率为{}".format(win / N))


N = 1000000
print("每次换门的中奖概率:")
winRate(True, N)
print("每次都不换门的中奖概率:")
winRate(False, N)

# 理论换门2/3 不换门1/3
# 每次换门的中奖概率:
# 中奖率为0.665769
# 每次都不换门的中奖概率:
# 中奖率为0.33292


import time
import random

for i in range(10):
print(time.strftime("%Y-%m-%d %X", time.localtime()))
dou = {1994: {'褐色': 30, '黄色': 20, '红色': 20, '绿色': 10, '橙色': 10, '黄褐': 30},
1996: {'蓝色': 24, '绿色': 20, '橙色': 16, '黄色': 14, '红色': 13, '褐色': 13}}

num = 10000
list_1994 = ['褐色'] * 30 * num + ['黄色'] * 20 * num + ['红色'] * 20 * num + ['绿色'] * 10 * num + ['橙色'] * 10 * num + ['黄褐'] * 10 * num
list_1996 = ['蓝色'] * 24 * num + ['绿色'] * 20 * num + ['橙色'] * 16 * num + ['黄色'] * 14 * num + ['红色'] * 13 * num + ['褐色'] * 13 * num
random.shuffle(list_1994)
random.shuffle(list_1996)

count_all = 0
count_key = 0
for key in range(100 * num):
if list_1994[key] == '黄色' and list_1996[key] == '绿色':
count_all += 1
count_key += 1
if list_1994[key] == '绿色' and list_1996[key] == '黄色':
count_all += 1
print(count_key / count_all, 20 / 27)
print(time.strftime("%Y-%m-%d %X", time.localtime()))

# ...
# 0.7407064573459715 0.7407407407407407
# 20/27是理论答案
文章来自个人专栏
云原生最佳实践
33 文章 | 1 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0