用Python做数学实验
2011-11-27 21:27
211 查看
Determine if you win or loose a hazard game.
Somebody suggests the following game. You pay 1 unit of money and are
allowed to throw four dice. If the sum of the eyes on the dice is less than 9,
you win 10 units of money, otherwise you loose your investment. Should you
play this game?
先用已经知道的数学方法进行求解,假设赢一把的概率是P,求出P即可算出收益的期望。假设4次摇出的骰子正面分别为:x,y,z,w
则胜利的条件是:x+y+z+w<9,其中1<=x,y,z,w<=6
x+y+z+w = 9 时,有C_{8}^{3}种组合;
x+y+z+w = 8 时,有C_{7}^{3}种组合;
x+y+z+w = 7 时,有C_{6}^{3}种组合;
…
x+y+z+w = 4 时,有1种组合;
所以,P = (C_{8}^{3}+C_{7}^{3}+C_{6}^{3}+C_{5}^{3}+C_{4}^{3}+C_{3}^{3})/64 = 7/72
所以期望为: 7/72*9+65/72*(-1) = –1/36
用Python进行Monte Carlo 仿真,
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Python实验的结果则是:-0.47
虽然都是输,但是结果有所出入
Somebody suggests the following game. You pay 1 unit of money and are
allowed to throw four dice. If the sum of the eyes on the dice is less than 9,
you win 10 units of money, otherwise you loose your investment. Should you
play this game?
先用已经知道的数学方法进行求解,假设赢一把的概率是P,求出P即可算出收益的期望。假设4次摇出的骰子正面分别为:x,y,z,w
则胜利的条件是:x+y+z+w<9,其中1<=x,y,z,w<=6
x+y+z+w = 9 时,有C_{8}^{3}种组合;
x+y+z+w = 8 时,有C_{7}^{3}种组合;
x+y+z+w = 7 时,有C_{6}^{3}种组合;
…
x+y+z+w = 4 时,有1种组合;
所以,P = (C_{8}^{3}+C_{7}^{3}+C_{6}^{3}+C_{5}^{3}+C_{4}^{3}+C_{3}^{3})/64 = 7/72
所以期望为: 7/72*9+65/72*(-1) = –1/36
用Python进行Monte Carlo 仿真,
import random n = 10000 m = 10000 money = 0 while(n): b = [] for i in range(1,5): b.append(random.randint(1,6)) if(sum(b)<9): money+=9 else: money-=1 n-=1 money = float(money) / m print money
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Python实验的结果则是:-0.47
虽然都是输,但是结果有所出入
相关文章推荐
- 解析数论导轮中的数学实验(python)
- 实验与作业(Python)-04 数据类型、数学函数与字符串
- python爬虫实验
- python冒泡排序与常用数学计算
- 27 个机器学习、数学、Python 速查表
- 数学之路-python计算实战(10)-机器视觉-透视投影
- 【转】VS 2005中实现对Python 2.5.2的模块扩展实验
- 数学老师从没这么教过,乘法竖式中进位可以是多位(附Python实现与测试源码)
- 数学模型 机器学习 系统聚类(system clustering) Python实现
- 数学之路-分布式计算-hadoop、python与R(1)
- python简单的数学运算.py
- 关于Python+Opencv实现人脸检测的实验笔记(调用图片文件篇)
- python 实验5 字符串练习(一)
- 【scikit-learn】用Python进行机器学习实验
- Python实验:百度搜索关键字自动打开相关URL
- Python基础05 for,if 语句 【例1:数学计算】
- python 实验环境
- Python的math模块中的常用数学函数整理
- Python的math模块中的常用数学函数整理
- Python在数学建模中的简单应用