Compare commits

...

No commits in common. "main" and "gh-pages" have entirely different histories.

128 changed files with 7 additions and 485319 deletions

View File

@ -1,668 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1.随机事件与概率"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.1 基础概念 \n",
"随机试验:\n",
"试验是指为了察看某事的结果或某物的性能而从事的某种活动. 在概率论与数理统计中一个试验如果具有以下3个特点: \n",
" - (1) 可重复性: 在相同条件下可以重复进行:\n",
" - (2) 可观察性: 每次试验的可能结果不止一个,并且能事先明确试验的所有可能结果:\n",
" - (3) 不确定性: 一次试验之前,不能预知会出现哪一个结果。 就称这样的试验是一个随机试验,也简称为试验。\n",
"\n",
"样本点和样本空间: \n",
"每次试验的每一个结果称为基本事件,也称作样本点,记作 $w_{1}, w_{2}, \\cdots$ 全部样本点的集合称为样本空间,记作 $\\Omega,$ 则 $\\Omega=$ $\\left\\{w_{1}, w_{2}, \\cdots\\right\\}$ \n",
"例子: \n",
"投郑一颗均匀股子,观察出现的点数。这是一个随机试验。样本空间 $\\Omega=\\{1,2,3,4,5,6\\} .$ \n",
"随机事件: \n",
"基本事件是不可再分解的、最基本的事件,其他事件均可由它们复合而成,由基本事件复合而成的事件称为随机事件或简称事件。 常用大写字母 $A, B, \\quad C$ 等表示事件。比如 $A=\\{$ 出现的点数为偶数 $\\}=\\{2,4,6\\} .$ \n",
"\n",
"## 1.2 随机事件与概率 \n",
"随机事件在一次试验中是否发生虽然不能确定,但让人感兴趣的是随机事件在一次试验中发生的可能性有多大。概率就是用来描述随机事件发生的可能性大小的。比如抛硬币的试验,拖得次数越多,出现正面的次数与投郑次数之间的比例(也叫频率 )愈加趋 于0.5。\n",
"它的数学定义为: = : \n",
"在多次重复试验中若事件A发生的频率稳定在确定常数 $p$ 附近摆动,且随着试验次数的增加,这种摆动的幅度是很微小的。 则称确定常数p为事件A发生的概率记作 $P(A)=p .$ \n",
"思考题 \n",
"设一年有365天求下列事件 $A, B$ 的概率: \n",
"$A=\\{$n个人中没有2人同一天生日 $\\}$ \n",
"$B=\\{$ n个人中有2人同一天生日 $\\}$ \n",
"解 \n",
"显然事件 $A, \\quad B$ 是对立事件, $P(B)=1-P(A)$. 由于每个人的生日可以是365天的任意一天因此 $n$ 个人的生日有 $365^{n}$ 种可能结果而且每种结果是等可能的因而是古典概型事件A的发生必须是 $n$ 个不同的生日,因而 $A$ 的样本点数为从365中取 $n$ 个的排列数 $P_{365}^{n},$ 于是 \n",
"$$\n",
"\\begin{array}{c}\n",
"P(A)=\\frac{P_{365}^{n}}{365^{n}} \\\\\n",
"P(B)=1-P(A)=1-\\frac{P_{365}^{n}}{365^{n}}\n",
"\\end{array}\n",
"$$ \n",
"\n",
"## 1.3 条件概率\n",
"定义\n",
"设 $A, B$ 是两个事件,且 $P(A)>0,$ 则称 \n",
"$$\n",
"P(B \\mid A)=\\frac{P(A B)}{P(A)}\n",
"$$\n",
"为在事件A发生的条件下事件B的条件概率。 \n",
"例子 \n",
"某种元件用满6000h未坏的概率是3/4用满10000h未坏的概率是1 $/ 2$ ,现有一个此种元件,已经用过 $6000 h$ 未坏试求它能用到10000h的概率。 \n",
"解 \n",
"设A表示\\{用满10000h未坏\\}, $B$ 表示\\{用满 $6000 h$ 未坏\\}, 则 \n",
"$$\n",
"P(B)=3 / 4, P(A)=1 / 2\n",
"$$ \n",
"由于 $A \\subset B, A B=A,$ 因而 $P(A B)=1 / 2,$ 故 \n",
"$$\n",
"P(A \\mid B)=\\frac{P(A B)}{P(B)}=\\frac{P(A)}{P(B)}=\\frac{\\frac{1}{2}}{\\frac{3}{4}}=\\frac{2}{3}\n",
"$$ \n",
"\n",
"## 1.4 事件的独立性\n",
"定义\n",
"如果事件B发生的可能性不受事件A发生与否的影响即\n",
"$$\n",
"P(B \\mid A)=P(B)\n",
"$$\n",
"则称事件B对于事件A独立.显然若B对于A独立则 $A$ 对于 $B$ 也一定独立,称事件 $A$ 与事件 $B$ 相互独立.\n",
"例子.\n",
"口袋里装有5个黑球与3个白球从中有放回地取2次每次取一个设事件A表示第一次取到黑球事件B表示第二次取到黑球则\n",
"有\n",
"$$\n",
"P(A)=\\frac{5}{8}, P(B)=\\frac{5}{8}, P(A B)=\\frac{5}{8} \\times \\frac{5}{8}=\\frac{25}{64}\n",
"$$\n",
"因而\n",
"$$\n",
"P(B \\mid A)=\\frac{P(A B)}{P(A)}=\\frac{5}{8}\n",
"$$\n",
"因此, $P(B \\mid A)=P(B),$ 事实上还可以算出 $P(B \\mid \\bar{A})=P(B)$ 。这表明不论 $A$ 发是不发生,都对 $B$ 发生的概率没有影响。即B 与A独立. \n",
"\n",
"性质: \n",
"事件 $A$ 和事件 $B$ 相互独立的充分必要条件是 \n",
"$$\n",
"P(A B)=P(A) P(B)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2.全概率公式和贝叶斯公式"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.1 全概率公式\n",
"定义: \n",
"如果事件 $A_{1}, A_{2}, \\cdots, A_{n}$ 是一个完备事件组,并且都具有正概率,则有: \n",
"$$\n",
"\\begin{array}{c}\n",
"P(B)=P\\left(A_{1}\\right) P\\left(B \\mid A_{1}\\right)+P\\left(A_{2}\\right) P\\left(B \\mid A_{2}\\right)+\\cdots+P\\left(A_{n}\\right) P\\left(B \\mid A_{n}\\right) \\\\\n",
"=\\sum_{i=1}^{n} P\\left(A_{i}\\right) P\\left(B \\mid A_{i}\\right)\n",
"\\end{array}\n",
"$$ \n",
"对于任何事件 $B,$ 事件 $A \\bar{A}$ 构成最简单的完备事件组,根据全概率公式得 \n",
"$$\n",
"\\begin{aligned}\n",
"P(B) &=P(A B+\\bar{A} B)=P(A B)+P(\\bar{A} B) \\\\\n",
"&=P(A) P(B \\mid A)+P(\\bar{A}) P(B \\mid \\bar{A})\n",
"\\end{aligned}\n",
"$$ \n",
"\n",
"## 2.2 贝叶斯公式 \n",
"定义: \n",
"设 $A_{1}, A_{2}, \\cdots, A_{n}$ 是一完备事件组,则对任一事件 $B, P(B)>0,$ 有 \n",
"$$\n",
"P\\left(A_{i} \\mid B\\right)=\\frac{P\\left(A_{i} B\\right)}{P(B)}=\\frac{P\\left(A_{i}\\right) P\\left(B \\mid A_{i}\\right)}{\\sum_{i=1}^{n} P\\left(A_{i}\\right) P\\left(B \\mid A_{i}\\right)}\n",
"$$ \n",
"以上公式就叫贝叶斯公式,可由条件概率的定义及全概率公式证得。 \n",
"\n",
"例子: \n",
"市场上供应的某种商品只由甲、乙、丙3个厂生产甲厂占45%乙厂占35%丙厂占20%。如果各厂的次品率依次为 4%, 2%, 5% 。现从市场上购买1件这种商品发现是次品试判断它是由甲厂生产的概率。 \n",
"设事件 $A_{1}, A_{2}, A_{3},$ 分别表示“商品为甲、乙、丙厂生产的\",事件 $B$ 表示“商品为次品”,由题意得到概率 \n",
"$$\n",
"\\begin{array}{c}\n",
"P\\left(A_{1}\\right)=45 \\%, P\\left(A_{2}\\right)=35 \\%, P\\left(A_{3}\\right)=20 \\% \\\\\n",
"P\\left(B \\mid A_{1}\\right)=4 \\%, P\\left(B \\mid A_{2}\\right)=2 \\%, P\\left(B \\mid A_{3}\\right)=5 \\%\n",
"\\end{array}\n",
"$$ \n",
"根据贝叶斯公式,可得:\n",
"$$\n",
"\\begin{aligned}\n",
"P\\left(A_{1} \\mid B\\right) &=\\frac{P\\left(A_{1}\\right) P\\left(B \\mid A_{1}\\right)}{P\\left(A_{1}\\right) P\\left(B \\mid A_{1}\\right)+P\\left(A_{2}\\right) P\\left(B \\mid A_{2}\\right)+P\\left(A_{3}\\right) P\\left(B \\mid A_{3}\\right)} \\\\\n",
"&=\\frac{45 \\% \\times 4 \\%}{45 \\% \\times 4 \\%+35 \\% \\times 2 \\%+20 \\% \\times 5 \\%} \\approx 0.514\n",
"\\end{aligned}\n",
"$$\n",
"在“购买一件商品”这个试验中, $P\\left(A_{i}\\right)$ 是在试验以前就已经知道的概率,所以习惯地称为先验概率。试验结果出现了次品(即 $B$ 发 生),这时条件概率 $P\\left(A_{i} \\mid B\\right)$ 反映了在试验以后对 $B$ 发生的“来源”(即次品的来源)的各种可能性的大小,通常称为后验概率。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.随机变量"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.1 随机变量\n",
"把试验的结果与实数对应起来,随试验结果的不同而变化的量就是随机变量,包含离散型随机变量和连续型随机变量。\n",
"例子: \n",
"郑一枚匀称的硬币,观察正面、背面的出现情况。这一试验的样本空间为 $\\Omega=\\{H, T\\}$, 其中H表示\"正面朝上\", $T$ 表示“背面 朝上”。如果引入变量 $X$ ,对试验的两个结果进行数值化,将 $X$ 的值分别规定为1和0即\n",
"$$\n",
"X=\\left\\{\\begin{array}{ll}\n",
"1 & \\text { if } \\text { 出现 } H \\\\\n",
"0 & \\text { if } & \\text { 出现 } T\n",
"\\end{array}\\right.\n",
"$$\n",
"这里的 $X$ 就叫随机变量,因为它能取的值是离散的,我们就叫它离散型随机变量。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.2 概率分布\n",
"定义:设离散型随机变量 $X$ 的所有可能取值为 $x_{1}, x_{2}, \\cdots, x_{n},$ 称为 $X$ 的概率分布。 \n",
"离散型随机变量 $X$ 的分布律具有下列基本性质: \n",
"1. $p_{k} \\geq 0, k=1,2, \\cdots$ \n",
"2. $\\sum_{i=1}^{+\\infty} p_{k}=1$ \n",
"二项分布: \n",
"二项分布是一种离散型的概率分布。二项代表它有两种可能的结果成功或者不成功。每次试验必须相互独立重复n次并且每 次试验成功的概率是相同的,为 $p $; 失败的概率也相同,为 $1-p$。掷硬币就是一个典型的二项分布。当我们要计算抛硬币吻,恰巧有 $x$ 次正面朝上的概率,可以使用二项分布的公式: \n",
"$$\n",
"P\\{X=k\\}=C_{n}^{k} p^{k}(1-p)^{n-k}\n",
"$$\n",
"\n",
"![jupyter](./image/概率/1.png) \n",
"\n",
"泊松分布: \n",
"如果随机变量 $X$ 的概率分布为\n",
"$$\n",
"P\\{X=k\\}=\\frac{\\lambda^{k}}{k !} e^{-\\lambda}, k=0,1,2, \\cdots\n",
"$$\n",
"式中, $\\lambda>0$ 为常数,则称随机变量 $X$ 服从参数为 $\\lambda$ 的泊松(Possion)分布,记为 $X \\sim P(\\lambda)$. \n",
"\n",
"![jupyter](./image/概率/2.png)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.3 概率密度函数\n",
"定义:若存在非负函数 $f(x)$, 使一个连续型随机变量 $X$ 取值于任一区间 $(a, b]$ 的概率可以表示为\n",
"$$\n",
"P\\{a<X \\leq b\\}=\\int_{a}^{b} f(x) d_{x}\n",
"$$\n",
"\n",
"正态分布: \n",
"正态分布是概率论中最重要的连续型分布在19世纪前叶由德国数学家高斯Gauss加以推广故又常称为高斯分布。 正态分布的概率密度函数曲线呈钟形,概率密度函数为\n",
"$$\n",
"f(x)=\\frac{1}{\\sqrt{2 \\pi} \\sigma} e^{-\\frac{(x-\\mu)^{2}}{2 \\sigma^{2}}}\n",
"$$\n",
"具有两个参数 $\\mu$ 和 $\\sigma^{2}$ 。第一参数 $\\mu$ 是代表服从正态分布的随机变量的均值,第二个参数 $\\sigma^{2}$ 是此随机变量的方差。 \n",
"如果一个随机变量 服从均值为 $\\mu,$ 标准差为 $\\sigma$ 的正态分布,数学上记作\n",
"$$\n",
"X \\sim N\\left(\\mu, \\sigma^{2}\\right)\n",
"$$\n",
"我们通常所说的标准正态分布均值为0, 标准差为1的正态分布。 \n",
"\n",
"![jupyter](./image/概率/3.png) \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.4 随机变量的期望\n",
"对于一个随机变量,时常要考虑它的平均取什么,期望就是概率中的平均值,对随机变量中心位置的一种度量。\n",
"例子.\n",
"经过长期观察积累,某射手在每次射击中命中的坏数 $X$ 服从分布:\n",
"$$\n",
"\\begin{array}{|c|c|c|c|c|c|c|c|}\n",
"\\hline \\mathrm{X} & 0 & 5 & 6 & 7 & 8 & 9 & 10 \\\\\n",
"\\hline p_{i} & 0 & 0.05 & 0.05 & 0.1 & 0.1 & 0.2 & 0.5 \\\\\n",
"\\hline\n",
"\\end{array}\n",
"$$\n",
"求这个射手平均命中的坏数是多少? \n",
"解 \n",
"一种很自然的考虑是假定该射击手进行了100次射击那么约有5次命中5坏5次命中6坏10次命中7坏10次命中8坏20 次命中9坏50次命10没有脱革从而在一次射击中该射手平均命中的坏数为 \n",
"$\\frac{1}{100}(10 \\times 50+9 \\times 20+8 \\times 10+7 \\times 10+6 \\times 5+5 \\times 5+0 \\times 0)=8.85$ \n",
"所以,我们可以看到离散型的随机变量的期望值可以表示为: \n",
"$E(X)=\\sum_{i=1}^{+\\infty} x_{i} p_{k}$ \n",
"\n",
"期望的性质: \n",
"1. $E(c)=c$ \n",
"2. $E(X+c)=E(X)+c$ \n",
"3. $E(k X)=k E(X)$ \n",
"4. $E(k X+c)=k E(X)+c$ \n",
"5. $E(X+Y)=E(X)+E(Y)$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.5 随机变量的方差:\n",
"方差表示了随机变量的变异性,方差越大,随机变量的结果越不稳定。 \n",
"定义:随机变量,若\n",
"$$\n",
"E[X-E(X)]^{2}\n",
"$$\n",
"存在,则称其为 $X$ 的方差,记为 $D(X),$ 即\n",
"$$\n",
"D(X)=E[X-E(X)]^{2}\n",
"$$\n",
"而称 $\\sqrt{D(X)}$ 为 $X$ 的标准差或均方差。\n",
"由方差的定义和数学期望的性质,可以推出方差的计算公式:\n",
"$$\n",
"D(X)=E\\left(X^{2}\\right)-[E(X)]^{2}\n",
"$$\n",
"\n",
"方差的性质 \n",
"(1) $D(c)=0$ \n",
"(2) $D(X+c)=D(X)$ \n",
"(3) $D(c X)=c^{2} D(X)$ \n",
"\n",
"例子: \n",
"甲、乙两车间生产同一种产品设1000件产品中的次品数分别为随机变量 $X, Y,$ 已知他们的分布律如下: \n",
"$$\n",
"\\begin{array}{ccccc}\n",
"\\mathbf{X} & \\mathbf{0} & 1 & \\mathbf{2} & \\mathbf{3} \\\\\n",
"\\hline p_{i} & 0.2 & 0.1 & 0.5 & 0.2\n",
"\\end{array}\n",
"$$\n",
"与\n",
"$$\n",
"\\begin{array}{|c|c|c|c|c|}\n",
"\\hline \\mathrm{Y} & 0 & 1 & 2 & 3 \\\\\n",
"\\hline p_{i} & 0.1 & 0.3 & 0.4 & 0.2 \\\\\n",
"\\hline\n",
"\\end{array}\n",
"$$\n",
"试讨论甲乙两者车厢的质量。 \n",
"解 \n",
"先计算均值 \n",
"$$\n",
"\\begin{array}{l}\n",
"E(X)=0 \\times 0.2+1 \\times 0.1+2 \\times 0.5+3 \\times 0.2=1.7 \\\\\n",
"E(Y)=0 \\times 0.1+1 \\times 0.3+2 \\times 0.4+3 \\times 0.2=1.7\n",
"\\end{array}\n",
"$$\n",
"得到:甲、乙两车间次品数的均值相同。 再计算方差: \n",
"$D(X)=(0-1.7)^{2} \\times 0.2+(1-1.7)^{2} \\times 0.1+(2-1.7)^{2} \\times 0.1+(2-1.7)^{2} \\times 0.5+(3-1.7)^{2} \\times 0.2=1.01$\n",
"\n",
"$D(Y)=(0-1.7)^{2} \\times 0.1+(1-1.7)^{2} \\times 0.3+(2-1.7)^{2} \\times 0.4+(2-1.7)^{2} \\times 0.4+(3-1.7)^{2} \\times 0.2=0.81$\n",
"\n",
"得到 $D(X)>D(Y),$ 说明乙车间的产品质量较稳定。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 随机模拟1$\\pi$的估值\n",
"![jupyter](./image/概率/6.png)\n",
"![jupyter](./image/概率/7.png) "
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pi的估计值为 2.8\n",
"pi的估计值为 2.8\n",
"pi的估计值为 2.96\n",
"pi的估计值为 3.016\n",
"pi的估计值为 3.156\n",
"pi的估计值为 3.156\n",
"pi的估计值为 3.1208\n",
"pi的估计值为 3.13528\n",
"pi的估计值为 3.14612\n",
"pi的估计值为 3.137168\n",
"pi的估计值为 3.1410684\n"
]
}
],
"source": [
"# pi的估计问题\n",
"import numpy as np \n",
"def pi_estimate(n):\n",
" '''\n",
" n为投点的数量\n",
" '''\n",
" n_rand_X = np.random.uniform(-1.0,1.0,n)\n",
" n_rand_Y = np.random.uniform(-1.0,1.0,n)\n",
" ## 判断是否在圆内\n",
" distance = np.sqrt(n_rand_X**2 + n_rand_Y**2)\n",
" dis_n = float(len(distance[distance<=1.0]))\n",
" return 4 * (dis_n / n)\n",
"\n",
"for i in [10,50,100,500,1000,5000,10000,50000,100000,500000,10000000]:\n",
" print(\"pi的估计值为\",pi_estimate(i))\n"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;31mDocstring:\u001b[0m\n",
"uniform(low=0.0, high=1.0, size=None)\n",
"\n",
"Draw samples from a uniform distribution.\n",
"\n",
"Samples are uniformly distributed over the half-open interval\n",
"``[low, high)`` (includes low, but excludes high). In other words,\n",
"any value within the given interval is equally likely to be drawn\n",
"by `uniform`.\n",
"\n",
".. note::\n",
" New code should use the ``uniform`` method of a ``default_rng()``\n",
" instance instead; please see the :ref:`random-quick-start`.\n",
"\n",
"Parameters\n",
"----------\n",
"low : float or array_like of floats, optional\n",
" Lower boundary of the output interval. All values generated will be\n",
" greater than or equal to low. The default value is 0.\n",
"high : float or array_like of floats\n",
" Upper boundary of the output interval. All values generated will be\n",
" less than or equal to high. The default value is 1.0.\n",
"size : int or tuple of ints, optional\n",
" Output shape. If the given shape is, e.g., ``(m, n, k)``, then\n",
" ``m * n * k`` samples are drawn. If size is ``None`` (default),\n",
" a single value is returned if ``low`` and ``high`` are both scalars.\n",
" Otherwise, ``np.broadcast(low, high).size`` samples are drawn.\n",
"\n",
"Returns\n",
"-------\n",
"out : ndarray or scalar\n",
" Drawn samples from the parameterized uniform distribution.\n",
"\n",
"See Also\n",
"--------\n",
"randint : Discrete uniform distribution, yielding integers.\n",
"random_integers : Discrete uniform distribution over the closed\n",
" interval ``[low, high]``.\n",
"random_sample : Floats uniformly distributed over ``[0, 1)``.\n",
"random : Alias for `random_sample`.\n",
"rand : Convenience function that accepts dimensions as input, e.g.,\n",
" ``rand(2,2)`` would generate a 2-by-2 array of floats,\n",
" uniformly distributed over ``[0, 1)``.\n",
"Generator.uniform: which should be used for new code.\n",
"\n",
"Notes\n",
"-----\n",
"The probability density function of the uniform distribution is\n",
"\n",
".. math:: p(x) = \\frac{1}{b - a}\n",
"\n",
"anywhere within the interval ``[a, b)``, and zero elsewhere.\n",
"\n",
"When ``high`` == ``low``, values of ``low`` will be returned.\n",
"If ``high`` < ``low``, the results are officially undefined\n",
"and may eventually raise an error, i.e. do not rely on this\n",
"function to behave when passed arguments satisfying that\n",
"inequality condition. The ``high`` limit may be included in the\n",
"returned array of floats due to floating-point rounding in the\n",
"equation ``low + (high-low) * random_sample()``. For example:\n",
"\n",
">>> x = np.float32(5*0.99999999)\n",
">>> x\n",
"5.0\n",
"\n",
"\n",
"Examples\n",
"--------\n",
"Draw samples from the distribution:\n",
"\n",
">>> s = np.random.uniform(-1,0,1000)\n",
"\n",
"All values are within the given interval:\n",
"\n",
">>> np.all(s >= -1)\n",
"True\n",
">>> np.all(s < 0)\n",
"True\n",
"\n",
"Display the histogram of the samples, along with the\n",
"probability density function:\n",
"\n",
">>> import matplotlib.pyplot as plt\n",
">>> count, bins, ignored = plt.hist(s, 15, density=True)\n",
">>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')\n",
">>> plt.show()\n",
"\u001b[1;31mType:\u001b[0m builtin_function_or_method\n"
]
}
],
"source": [
"?np.random.uniform"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 随机模拟2:电子元件寿命问题\n",
"\n",
"![jupyter](./image/概率/4.png) \n",
"![jupyter](./image/概率/5.png) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9124"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 电子元件寿命问题\n",
"import numpy as np \n",
"\n",
"def ele_life(n,c,h,t,lamb):\n",
" \"\"\"\n",
" 参数n:模拟实验的次数\n",
" 参数c:每次试验中的c个元件\n",
" 参数t:每c个元件中规定的合格品数量\n",
" 参数h:小时数\n",
" \"\"\"\n",
" times = 0.0\n",
" for i in range(n):\n",
" c_rand = np.random.exponential(1/lamb,c)\n",
" c_rand_t = len(c_rand[c_rand>h])\n",
" if c_rand_t > t:\n",
" times = times + 1\n",
" return times / n \n",
"\n",
"ele_life(10000,1000,18,20,0.2)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;31mDocstring:\u001b[0m\n",
"exponential(scale=1.0, size=None)\n",
"\n",
"Draw samples from an exponential distribution.\n",
"\n",
"Its probability density function is\n",
"\n",
".. math:: f(x; \\frac{1}{\\beta}) = \\frac{1}{\\beta} \\exp(-\\frac{x}{\\beta}),\n",
"\n",
"for ``x > 0`` and 0 elsewhere. :math:`\\beta` is the scale parameter,\n",
"which is the inverse of the rate parameter :math:`\\lambda = 1/\\beta`.\n",
"The rate parameter is an alternative, widely used parameterization\n",
"of the exponential distribution [3]_.\n",
"\n",
"The exponential distribution is a continuous analogue of the\n",
"geometric distribution. It describes many common situations, such as\n",
"the size of raindrops measured over many rainstorms [1]_, or the time\n",
"between page requests to Wikipedia [2]_.\n",
"\n",
".. note::\n",
" New code should use the ``exponential`` method of a ``default_rng()``\n",
" instance instead; please see the :ref:`random-quick-start`.\n",
"\n",
"Parameters\n",
"----------\n",
"scale : float or array_like of floats\n",
" The scale parameter, :math:`\\beta = 1/\\lambda`. Must be\n",
" non-negative.\n",
"size : int or tuple of ints, optional\n",
" Output shape. If the given shape is, e.g., ``(m, n, k)``, then\n",
" ``m * n * k`` samples are drawn. If size is ``None`` (default),\n",
" a single value is returned if ``scale`` is a scalar. Otherwise,\n",
" ``np.array(scale).size`` samples are drawn.\n",
"\n",
"Returns\n",
"-------\n",
"out : ndarray or scalar\n",
" Drawn samples from the parameterized exponential distribution.\n",
"\n",
"See Also\n",
"--------\n",
"Generator.exponential: which should be used for new code.\n",
"\n",
"References\n",
"----------\n",
".. [1] Peyton Z. Peebles Jr., \"Probability, Random Variables and\n",
" Random Signal Principles\", 4th ed, 2001, p. 57.\n",
".. [2] Wikipedia, \"Poisson process\",\n",
" https://en.wikipedia.org/wiki/Poisson_process\n",
".. [3] Wikipedia, \"Exponential distribution\",\n",
" https://en.wikipedia.org/wiki/Exponential_distribution\n",
"\u001b[1;31mType:\u001b[0m builtin_function_or_method\n"
]
}
],
"source": [
"?np.random.exponential"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 随机模拟3三门问题\n",
"蒙提霍尔问题假如你参与一个有主持人的游戏你会看见三扇关闭了的门其中一扇的后面有一辆汽车另外2扇门后面各是一只山羊你看不见门后面的情况但主持人知道一切。你被主持人要求在三扇门中选择一扇但不能打开在你选定之后主持人开启了另一扇后面有山羊的门然后你可以坚持原来选定的门也可以改主意重新选择。问题是改与不改对选中汽车的概率有影响吗请使用模拟实验的方法回答该问题。"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"在100000次测试中,坚持原则第一次就选中的次数是33063,改变决定选择另一扇门中奖的次数是66937\n",
"概率分别是0.33063和0.66937改变决定选择另一扇门中奖几率是坚持选择的2.02倍\n"
]
}
],
"source": [
"# 三门问题\n",
"import numpy.random as random\n",
"def MontyHallProblem(n_test):\n",
" #测试次数\n",
" \n",
" winning_door = random.randint(0,3,n_test)\n",
" first_get = 0\n",
" change_get = 0\n",
" for winning_doors in winning_door:\n",
" act_door = random.randint(0,3)\n",
" if winning_doors == act_door:\n",
" first_get += 1\n",
" else :\n",
" change_get += 1\n",
" first_pro = first_get / n_test\n",
" change_pro = change_get / n_test\n",
" compar1 = round(change_get / first_get,2)\n",
" print (\"在%d次测试中,坚持原则第一次就选中的次数是%d,改变决定选择另一扇门中奖的次数是%d\"% (n_test,first_get,change_get))\n",
" print (\"概率分别是{0}和{1},改变决定选择另一扇门中奖几率是坚持选择的{2}倍\".format(first_pro,change_pro,compar1))\n",
"MontyHallProblem(100000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@ -1,40 +0,0 @@
# MCMC方法之Metropolis-Hastings算法
import numpy as np
import random
import matplotlib.pyplot as plt
## 设置参数
mu = 0.5
sigma = 0.1
skip = 700 ## 设置收敛步长
num = 100000 ##采样点数
def Gussian(x,mu,sigma):
return 1/(np.sqrt(2*np.pi)*sigma)*np.exp(-np.square((x-mu))/(2*np.square(sigma)))
def M_H(num):
x_0 = 0
samples = []
j = 1
while(len(samples) <= num):
while True:
x_1 = random.random() # 转移函数(转移矩阵)
q_i = Gussian(x_0,mu,sigma)
q_j = Gussian(x_1,mu,sigma)
alpha = min(1,q_i/q_j)
u = random.random()
if u <= alpha:
x_0 = x_1
if j >= skip:
samples.append(x_1)
j = j + 1
break
return samples
norm_samples = M_H(num)
X = np.array(norm_samples)
px = Gussian(X,mu,sigma)
plt.scatter(X,px)
plt.show()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 145 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 218 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 607 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 902 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 486 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 597 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 141 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 201 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 147 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 121 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 249 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

View File

@ -1,668 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1.随机事件与概率"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.1 基础概念 \n",
"随机试验:\n",
"试验是指为了察看某事的结果或某物的性能而从事的某种活动. 在概率论与数理统计中一个试验如果具有以下3个特点: \n",
" - (1) 可重复性: 在相同条件下可以重复进行:\n",
" - (2) 可观察性: 每次试验的可能结果不止一个,并且能事先明确试验的所有可能结果:\n",
" - (3) 不确定性: 一次试验之前,不能预知会出现哪一个结果。 就称这样的试验是一个随机试验,也简称为试验。\n",
"\n",
"样本点和样本空间: \n",
"每次试验的每一个结果称为基本事件,也称作样本点,记作 $w_{1}, w_{2}, \\cdots$ 全部样本点的集合称为样本空间,记作 $\\Omega,$ 则 $\\Omega=$ $\\left\\{w_{1}, w_{2}, \\cdots\\right\\}$ \n",
"例子: \n",
"投郑一颗均匀股子,观察出现的点数。这是一个随机试验。样本空间 $\\Omega=\\{1,2,3,4,5,6\\} .$ \n",
"随机事件: \n",
"基本事件是不可再分解的、最基本的事件,其他事件均可由它们复合而成,由基本事件复合而成的事件称为随机事件或简称事件。 常用大写字母 $A, B, \\quad C$ 等表示事件。比如 $A=\\{$ 出现的点数为偶数 $\\}=\\{2,4,6\\} .$ \n",
"\n",
"## 1.2 随机事件与概率 \n",
"随机事件在一次试验中是否发生虽然不能确定,但让人感兴趣的是随机事件在一次试验中发生的可能性有多大。概率就是用来描述随机事件发生的可能性大小的。比如抛硬币的试验,拖得次数越多,出现正面的次数与投郑次数之间的比例(也叫频率 )愈加趋 于0.5。\n",
"它的数学定义为: = : \n",
"在多次重复试验中若事件A发生的频率稳定在确定常数 $p$ 附近摆动,且随着试验次数的增加,这种摆动的幅度是很微小的。 则称确定常数p为事件A发生的概率记作 $P(A)=p .$ \n",
"思考题 \n",
"设一年有365天求下列事件 $A, B$ 的概率: \n",
"$A=\\{$n个人中没有2人同一天生日 $\\}$ \n",
"$B=\\{$ n个人中有2人同一天生日 $\\}$ \n",
"解 \n",
"显然事件 $A, \\quad B$ 是对立事件, $P(B)=1-P(A)$. 由于每个人的生日可以是365天的任意一天因此 $n$ 个人的生日有 $365^{n}$ 种可能结果而且每种结果是等可能的因而是古典概型事件A的发生必须是 $n$ 个不同的生日,因而 $A$ 的样本点数为从365中取 $n$ 个的排列数 $P_{365}^{n},$ 于是 \n",
"$$\n",
"\\begin{array}{c}\n",
"P(A)=\\frac{P_{365}^{n}}{365^{n}} \\\\\n",
"P(B)=1-P(A)=1-\\frac{P_{365}^{n}}{365^{n}}\n",
"\\end{array}\n",
"$$ \n",
"\n",
"## 1.3 条件概率\n",
"定义\n",
"设 $A, B$ 是两个事件,且 $P(A)>0,$ 则称 \n",
"$$\n",
"P(B \\mid A)=\\frac{P(A B)}{P(A)}\n",
"$$\n",
"为在事件A发生的条件下事件B的条件概率。 \n",
"例子 \n",
"某种元件用满6000h未坏的概率是3/4用满10000h未坏的概率是1 $/ 2$ ,现有一个此种元件,已经用过 $6000 h$ 未坏试求它能用到10000h的概率。 \n",
"解 \n",
"设A表示\\{用满10000h未坏\\}, $B$ 表示\\{用满 $6000 h$ 未坏\\}, 则 \n",
"$$\n",
"P(B)=3 / 4, P(A)=1 / 2\n",
"$$ \n",
"由于 $A \\subset B, A B=A,$ 因而 $P(A B)=1 / 2,$ 故 \n",
"$$\n",
"P(A \\mid B)=\\frac{P(A B)}{P(B)}=\\frac{P(A)}{P(B)}=\\frac{\\frac{1}{2}}{\\frac{3}{4}}=\\frac{2}{3}\n",
"$$ \n",
"\n",
"## 1.4 事件的独立性\n",
"定义\n",
"如果事件B发生的可能性不受事件A发生与否的影响即\n",
"$$\n",
"P(B \\mid A)=P(B)\n",
"$$\n",
"则称事件B对于事件A独立.显然若B对于A独立则 $A$ 对于 $B$ 也一定独立,称事件 $A$ 与事件 $B$ 相互独立.\n",
"例子.\n",
"口袋里装有5个黑球与3个白球从中有放回地取2次每次取一个设事件A表示第一次取到黑球事件B表示第二次取到黑球则\n",
"有\n",
"$$\n",
"P(A)=\\frac{5}{8}, P(B)=\\frac{5}{8}, P(A B)=\\frac{5}{8} \\times \\frac{5}{8}=\\frac{25}{64}\n",
"$$\n",
"因而\n",
"$$\n",
"P(B \\mid A)=\\frac{P(A B)}{P(A)}=\\frac{5}{8}\n",
"$$\n",
"因此, $P(B \\mid A)=P(B),$ 事实上还可以算出 $P(B \\mid \\bar{A})=P(B)$ 。这表明不论 $A$ 发是不发生,都对 $B$ 发生的概率没有影响。即B 与A独立. \n",
"\n",
"性质: \n",
"事件 $A$ 和事件 $B$ 相互独立的充分必要条件是 \n",
"$$\n",
"P(A B)=P(A) P(B)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2.全概率公式和贝叶斯公式"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.1 全概率公式\n",
"定义: \n",
"如果事件 $A_{1}, A_{2}, \\cdots, A_{n}$ 是一个完备事件组,并且都具有正概率,则有: \n",
"$$\n",
"\\begin{array}{c}\n",
"P(B)=P\\left(A_{1}\\right) P\\left(B \\mid A_{1}\\right)+P\\left(A_{2}\\right) P\\left(B \\mid A_{2}\\right)+\\cdots+P\\left(A_{n}\\right) P\\left(B \\mid A_{n}\\right) \\\\\n",
"=\\sum_{i=1}^{n} P\\left(A_{i}\\right) P\\left(B \\mid A_{i}\\right)\n",
"\\end{array}\n",
"$$ \n",
"对于任何事件 $B,$ 事件 $A \\bar{A}$ 构成最简单的完备事件组,根据全概率公式得 \n",
"$$\n",
"\\begin{aligned}\n",
"P(B) &=P(A B+\\bar{A} B)=P(A B)+P(\\bar{A} B) \\\\\n",
"&=P(A) P(B \\mid A)+P(\\bar{A}) P(B \\mid \\bar{A})\n",
"\\end{aligned}\n",
"$$ \n",
"\n",
"## 2.2 贝叶斯公式 \n",
"定义: \n",
"设 $A_{1}, A_{2}, \\cdots, A_{n}$ 是一完备事件组,则对任一事件 $B, P(B)>0,$ 有 \n",
"$$\n",
"P\\left(A_{i} \\mid B\\right)=\\frac{P\\left(A_{i} B\\right)}{P(B)}=\\frac{P\\left(A_{i}\\right) P\\left(B \\mid A_{i}\\right)}{\\sum_{i=1}^{n} P\\left(A_{i}\\right) P\\left(B \\mid A_{i}\\right)}\n",
"$$ \n",
"以上公式就叫贝叶斯公式,可由条件概率的定义及全概率公式证得。 \n",
"\n",
"例子: \n",
"市场上供应的某种商品只由甲、乙、丙3个厂生产甲厂占45%乙厂占35%丙厂占20%。如果各厂的次品率依次为 4%, 2%, 5% 。现从市场上购买1件这种商品发现是次品试判断它是由甲厂生产的概率。 \n",
"设事件 $A_{1}, A_{2}, A_{3},$ 分别表示“商品为甲、乙、丙厂生产的\",事件 $B$ 表示“商品为次品”,由题意得到概率 \n",
"$$\n",
"\\begin{array}{c}\n",
"P\\left(A_{1}\\right)=45 \\%, P\\left(A_{2}\\right)=35 \\%, P\\left(A_{3}\\right)=20 \\% \\\\\n",
"P\\left(B \\mid A_{1}\\right)=4 \\%, P\\left(B \\mid A_{2}\\right)=2 \\%, P\\left(B \\mid A_{3}\\right)=5 \\%\n",
"\\end{array}\n",
"$$ \n",
"根据贝叶斯公式,可得:\n",
"$$\n",
"\\begin{aligned}\n",
"P\\left(A_{1} \\mid B\\right) &=\\frac{P\\left(A_{1}\\right) P\\left(B \\mid A_{1}\\right)}{P\\left(A_{1}\\right) P\\left(B \\mid A_{1}\\right)+P\\left(A_{2}\\right) P\\left(B \\mid A_{2}\\right)+P\\left(A_{3}\\right) P\\left(B \\mid A_{3}\\right)} \\\\\n",
"&=\\frac{45 \\% \\times 4 \\%}{45 \\% \\times 4 \\%+35 \\% \\times 2 \\%+20 \\% \\times 5 \\%} \\approx 0.514\n",
"\\end{aligned}\n",
"$$\n",
"在“购买一件商品”这个试验中, $P\\left(A_{i}\\right)$ 是在试验以前就已经知道的概率,所以习惯地称为先验概率。试验结果出现了次品(即 $B$ 发 生),这时条件概率 $P\\left(A_{i} \\mid B\\right)$ 反映了在试验以后对 $B$ 发生的“来源”(即次品的来源)的各种可能性的大小,通常称为后验概率。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.随机变量"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.1 随机变量\n",
"把试验的结果与实数对应起来,随试验结果的不同而变化的量就是随机变量,包含离散型随机变量和连续型随机变量。\n",
"例子: \n",
"郑一枚匀称的硬币,观察正面、背面的出现情况。这一试验的样本空间为 $\\Omega=\\{H, T\\}$, 其中H表示\"正面朝上\", $T$ 表示“背面 朝上”。如果引入变量 $X$ ,对试验的两个结果进行数值化,将 $X$ 的值分别规定为1和0即\n",
"$$\n",
"X=\\left\\{\\begin{array}{ll}\n",
"1 & \\text { if } \\text { 出现 } H \\\\\n",
"0 & \\text { if } & \\text { 出现 } T\n",
"\\end{array}\\right.\n",
"$$\n",
"这里的 $X$ 就叫随机变量,因为它能取的值是离散的,我们就叫它离散型随机变量。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.2 概率分布\n",
"定义:设离散型随机变量 $X$ 的所有可能取值为 $x_{1}, x_{2}, \\cdots, x_{n},$ 称为 $X$ 的概率分布。 \n",
"离散型随机变量 $X$ 的分布律具有下列基本性质: \n",
"1. $p_{k} \\geq 0, k=1,2, \\cdots$ \n",
"2. $\\sum_{i=1}^{+\\infty} p_{k}=1$ \n",
"二项分布: \n",
"二项分布是一种离散型的概率分布。二项代表它有两种可能的结果成功或者不成功。每次试验必须相互独立重复n次并且每 次试验成功的概率是相同的,为 $p $; 失败的概率也相同,为 $1-p$。掷硬币就是一个典型的二项分布。当我们要计算抛硬币吻,恰巧有 $x$ 次正面朝上的概率,可以使用二项分布的公式: \n",
"$$\n",
"P\\{X=k\\}=C_{n}^{k} p^{k}(1-p)^{n-k}\n",
"$$\n",
"\n",
"![jupyter](./image/概率/1.png) \n",
"\n",
"泊松分布: \n",
"如果随机变量 $X$ 的概率分布为\n",
"$$\n",
"P\\{X=k\\}=\\frac{\\lambda^{k}}{k !} e^{-\\lambda}, k=0,1,2, \\cdots\n",
"$$\n",
"式中, $\\lambda>0$ 为常数,则称随机变量 $X$ 服从参数为 $\\lambda$ 的泊松(Possion)分布,记为 $X \\sim P(\\lambda)$. \n",
"\n",
"![jupyter](./image/概率/2.png)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.3 概率密度函数\n",
"定义:若存在非负函数 $f(x)$, 使一个连续型随机变量 $X$ 取值于任一区间 $(a, b]$ 的概率可以表示为\n",
"$$\n",
"P\\{a<X \\leq b\\}=\\int_{a}^{b} f(x) d_{x}\n",
"$$\n",
"\n",
"正态分布: \n",
"正态分布是概率论中最重要的连续型分布在19世纪前叶由德国数学家高斯Gauss加以推广故又常称为高斯分布。 正态分布的概率密度函数曲线呈钟形,概率密度函数为\n",
"$$\n",
"f(x)=\\frac{1}{\\sqrt{2 \\pi} \\sigma} e^{-\\frac{(x-\\mu)^{2}}{2 \\sigma^{2}}}\n",
"$$\n",
"具有两个参数 $\\mu$ 和 $\\sigma^{2}$ 。第一参数 $\\mu$ 是代表服从正态分布的随机变量的均值,第二个参数 $\\sigma^{2}$ 是此随机变量的方差。 \n",
"如果一个随机变量 服从均值为 $\\mu,$ 标准差为 $\\sigma$ 的正态分布,数学上记作\n",
"$$\n",
"X \\sim N\\left(\\mu, \\sigma^{2}\\right)\n",
"$$\n",
"我们通常所说的标准正态分布均值为0, 标准差为1的正态分布。 \n",
"\n",
"![jupyter](./image/概率/3.png) \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.4 随机变量的期望\n",
"对于一个随机变量,时常要考虑它的平均取什么,期望就是概率中的平均值,对随机变量中心位置的一种度量。\n",
"例子.\n",
"经过长期观察积累,某射手在每次射击中命中的坏数 $X$ 服从分布:\n",
"$$\n",
"\\begin{array}{|c|c|c|c|c|c|c|c|}\n",
"\\hline \\mathrm{X} & 0 & 5 & 6 & 7 & 8 & 9 & 10 \\\\\n",
"\\hline p_{i} & 0 & 0.05 & 0.05 & 0.1 & 0.1 & 0.2 & 0.5 \\\\\n",
"\\hline\n",
"\\end{array}\n",
"$$\n",
"求这个射手平均命中的坏数是多少? \n",
"解 \n",
"一种很自然的考虑是假定该射击手进行了100次射击那么约有5次命中5坏5次命中6坏10次命中7坏10次命中8坏20 次命中9坏50次命10没有脱革从而在一次射击中该射手平均命中的坏数为 \n",
"$\\frac{1}{100}(10 \\times 50+9 \\times 20+8 \\times 10+7 \\times 10+6 \\times 5+5 \\times 5+0 \\times 0)=8.85$ \n",
"所以,我们可以看到离散型的随机变量的期望值可以表示为: \n",
"$E(X)=\\sum_{i=1}^{+\\infty} x_{i} p_{k}$ \n",
"\n",
"期望的性质: \n",
"1. $E(c)=c$ \n",
"2. $E(X+c)=E(X)+c$ \n",
"3. $E(k X)=k E(X)$ \n",
"4. $E(k X+c)=k E(X)+c$ \n",
"5. $E(X+Y)=E(X)+E(Y)$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.5 随机变量的方差:\n",
"方差表示了随机变量的变异性,方差越大,随机变量的结果越不稳定。 \n",
"定义:随机变量,若\n",
"$$\n",
"E[X-E(X)]^{2}\n",
"$$\n",
"存在,则称其为 $X$ 的方差,记为 $D(X),$ 即\n",
"$$\n",
"D(X)=E[X-E(X)]^{2}\n",
"$$\n",
"而称 $\\sqrt{D(X)}$ 为 $X$ 的标准差或均方差。\n",
"由方差的定义和数学期望的性质,可以推出方差的计算公式:\n",
"$$\n",
"D(X)=E\\left(X^{2}\\right)-[E(X)]^{2}\n",
"$$\n",
"\n",
"方差的性质 \n",
"(1) $D(c)=0$ \n",
"(2) $D(X+c)=D(X)$ \n",
"(3) $D(c X)=c^{2} D(X)$ \n",
"\n",
"例子: \n",
"甲、乙两车间生产同一种产品设1000件产品中的次品数分别为随机变量 $X, Y,$ 已知他们的分布律如下: \n",
"$$\n",
"\\begin{array}{ccccc}\n",
"\\mathbf{X} & \\mathbf{0} & 1 & \\mathbf{2} & \\mathbf{3} \\\\\n",
"\\hline p_{i} & 0.2 & 0.1 & 0.5 & 0.2\n",
"\\end{array}\n",
"$$\n",
"与\n",
"$$\n",
"\\begin{array}{|c|c|c|c|c|}\n",
"\\hline \\mathrm{Y} & 0 & 1 & 2 & 3 \\\\\n",
"\\hline p_{i} & 0.1 & 0.3 & 0.4 & 0.2 \\\\\n",
"\\hline\n",
"\\end{array}\n",
"$$\n",
"试讨论甲乙两者车厢的质量。 \n",
"解 \n",
"先计算均值 \n",
"$$\n",
"\\begin{array}{l}\n",
"E(X)=0 \\times 0.2+1 \\times 0.1+2 \\times 0.5+3 \\times 0.2=1.7 \\\\\n",
"E(Y)=0 \\times 0.1+1 \\times 0.3+2 \\times 0.4+3 \\times 0.2=1.7\n",
"\\end{array}\n",
"$$\n",
"得到:甲、乙两车间次品数的均值相同。 再计算方差: \n",
"$D(X)=(0-1.7)^{2} \\times 0.2+(1-1.7)^{2} \\times 0.1+(2-1.7)^{2} \\times 0.1+(2-1.7)^{2} \\times 0.5+(3-1.7)^{2} \\times 0.2=1.01$\n",
"\n",
"$D(Y)=(0-1.7)^{2} \\times 0.1+(1-1.7)^{2} \\times 0.3+(2-1.7)^{2} \\times 0.4+(2-1.7)^{2} \\times 0.4+(3-1.7)^{2} \\times 0.2=0.81$\n",
"\n",
"得到 $D(X)>D(Y),$ 说明乙车间的产品质量较稳定。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 随机模拟1$\\pi$的估值\n",
"![jupyter](./image/概率/6.png)\n",
"![jupyter](./image/概率/7.png) "
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pi的估计值为 2.8\n",
"pi的估计值为 2.8\n",
"pi的估计值为 2.96\n",
"pi的估计值为 3.016\n",
"pi的估计值为 3.156\n",
"pi的估计值为 3.156\n",
"pi的估计值为 3.1208\n",
"pi的估计值为 3.13528\n",
"pi的估计值为 3.14612\n",
"pi的估计值为 3.137168\n",
"pi的估计值为 3.1410684\n"
]
}
],
"source": [
"# pi的估计问题\n",
"import numpy as np \n",
"def pi_estimate(n):\n",
" '''\n",
" n为投点的数量\n",
" '''\n",
" n_rand_X = np.random.uniform(-1.0,1.0,n)\n",
" n_rand_Y = np.random.uniform(-1.0,1.0,n)\n",
" ## 判断是否在圆内\n",
" distance = np.sqrt(n_rand_X**2 + n_rand_Y**2)\n",
" dis_n = float(len(distance[distance<=1.0]))\n",
" return 4 * (dis_n / n)\n",
"\n",
"for i in [10,50,100,500,1000,5000,10000,50000,100000,500000,10000000]:\n",
" print(\"pi的估计值为\",pi_estimate(i))\n"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;31mDocstring:\u001b[0m\n",
"uniform(low=0.0, high=1.0, size=None)\n",
"\n",
"Draw samples from a uniform distribution.\n",
"\n",
"Samples are uniformly distributed over the half-open interval\n",
"``[low, high)`` (includes low, but excludes high). In other words,\n",
"any value within the given interval is equally likely to be drawn\n",
"by `uniform`.\n",
"\n",
".. note::\n",
" New code should use the ``uniform`` method of a ``default_rng()``\n",
" instance instead; please see the :ref:`random-quick-start`.\n",
"\n",
"Parameters\n",
"----------\n",
"low : float or array_like of floats, optional\n",
" Lower boundary of the output interval. All values generated will be\n",
" greater than or equal to low. The default value is 0.\n",
"high : float or array_like of floats\n",
" Upper boundary of the output interval. All values generated will be\n",
" less than or equal to high. The default value is 1.0.\n",
"size : int or tuple of ints, optional\n",
" Output shape. If the given shape is, e.g., ``(m, n, k)``, then\n",
" ``m * n * k`` samples are drawn. If size is ``None`` (default),\n",
" a single value is returned if ``low`` and ``high`` are both scalars.\n",
" Otherwise, ``np.broadcast(low, high).size`` samples are drawn.\n",
"\n",
"Returns\n",
"-------\n",
"out : ndarray or scalar\n",
" Drawn samples from the parameterized uniform distribution.\n",
"\n",
"See Also\n",
"--------\n",
"randint : Discrete uniform distribution, yielding integers.\n",
"random_integers : Discrete uniform distribution over the closed\n",
" interval ``[low, high]``.\n",
"random_sample : Floats uniformly distributed over ``[0, 1)``.\n",
"random : Alias for `random_sample`.\n",
"rand : Convenience function that accepts dimensions as input, e.g.,\n",
" ``rand(2,2)`` would generate a 2-by-2 array of floats,\n",
" uniformly distributed over ``[0, 1)``.\n",
"Generator.uniform: which should be used for new code.\n",
"\n",
"Notes\n",
"-----\n",
"The probability density function of the uniform distribution is\n",
"\n",
".. math:: p(x) = \\frac{1}{b - a}\n",
"\n",
"anywhere within the interval ``[a, b)``, and zero elsewhere.\n",
"\n",
"When ``high`` == ``low``, values of ``low`` will be returned.\n",
"If ``high`` < ``low``, the results are officially undefined\n",
"and may eventually raise an error, i.e. do not rely on this\n",
"function to behave when passed arguments satisfying that\n",
"inequality condition. The ``high`` limit may be included in the\n",
"returned array of floats due to floating-point rounding in the\n",
"equation ``low + (high-low) * random_sample()``. For example:\n",
"\n",
">>> x = np.float32(5*0.99999999)\n",
">>> x\n",
"5.0\n",
"\n",
"\n",
"Examples\n",
"--------\n",
"Draw samples from the distribution:\n",
"\n",
">>> s = np.random.uniform(-1,0,1000)\n",
"\n",
"All values are within the given interval:\n",
"\n",
">>> np.all(s >= -1)\n",
"True\n",
">>> np.all(s < 0)\n",
"True\n",
"\n",
"Display the histogram of the samples, along with the\n",
"probability density function:\n",
"\n",
">>> import matplotlib.pyplot as plt\n",
">>> count, bins, ignored = plt.hist(s, 15, density=True)\n",
">>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')\n",
">>> plt.show()\n",
"\u001b[1;31mType:\u001b[0m builtin_function_or_method\n"
]
}
],
"source": [
"?np.random.uniform"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 随机模拟2:电子元件寿命问题\n",
"\n",
"![jupyter](./image/概率/4.png) \n",
"![jupyter](./image/概率/5.png) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9124"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 电子元件寿命问题\n",
"import numpy as np \n",
"\n",
"def ele_life(n,c,h,t,lamb):\n",
" \"\"\"\n",
" 参数n:模拟实验的次数\n",
" 参数c:每次试验中的c个元件\n",
" 参数t:每c个元件中规定的合格品数量\n",
" 参数h:小时数\n",
" \"\"\"\n",
" times = 0.0\n",
" for i in range(n):\n",
" c_rand = np.random.exponential(1/lamb,c)\n",
" c_rand_t = len(c_rand[c_rand>h])\n",
" if c_rand_t > t:\n",
" times = times + 1\n",
" return times / n \n",
"\n",
"ele_life(10000,1000,18,20,0.2)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;31mDocstring:\u001b[0m\n",
"exponential(scale=1.0, size=None)\n",
"\n",
"Draw samples from an exponential distribution.\n",
"\n",
"Its probability density function is\n",
"\n",
".. math:: f(x; \\frac{1}{\\beta}) = \\frac{1}{\\beta} \\exp(-\\frac{x}{\\beta}),\n",
"\n",
"for ``x > 0`` and 0 elsewhere. :math:`\\beta` is the scale parameter,\n",
"which is the inverse of the rate parameter :math:`\\lambda = 1/\\beta`.\n",
"The rate parameter is an alternative, widely used parameterization\n",
"of the exponential distribution [3]_.\n",
"\n",
"The exponential distribution is a continuous analogue of the\n",
"geometric distribution. It describes many common situations, such as\n",
"the size of raindrops measured over many rainstorms [1]_, or the time\n",
"between page requests to Wikipedia [2]_.\n",
"\n",
".. note::\n",
" New code should use the ``exponential`` method of a ``default_rng()``\n",
" instance instead; please see the :ref:`random-quick-start`.\n",
"\n",
"Parameters\n",
"----------\n",
"scale : float or array_like of floats\n",
" The scale parameter, :math:`\\beta = 1/\\lambda`. Must be\n",
" non-negative.\n",
"size : int or tuple of ints, optional\n",
" Output shape. If the given shape is, e.g., ``(m, n, k)``, then\n",
" ``m * n * k`` samples are drawn. If size is ``None`` (default),\n",
" a single value is returned if ``scale`` is a scalar. Otherwise,\n",
" ``np.array(scale).size`` samples are drawn.\n",
"\n",
"Returns\n",
"-------\n",
"out : ndarray or scalar\n",
" Drawn samples from the parameterized exponential distribution.\n",
"\n",
"See Also\n",
"--------\n",
"Generator.exponential: which should be used for new code.\n",
"\n",
"References\n",
"----------\n",
".. [1] Peyton Z. Peebles Jr., \"Probability, Random Variables and\n",
" Random Signal Principles\", 4th ed, 2001, p. 57.\n",
".. [2] Wikipedia, \"Poisson process\",\n",
" https://en.wikipedia.org/wiki/Poisson_process\n",
".. [3] Wikipedia, \"Exponential distribution\",\n",
" https://en.wikipedia.org/wiki/Exponential_distribution\n",
"\u001b[1;31mType:\u001b[0m builtin_function_or_method\n"
]
}
],
"source": [
"?np.random.exponential"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 随机模拟3三门问题\n",
"蒙提霍尔问题假如你参与一个有主持人的游戏你会看见三扇关闭了的门其中一扇的后面有一辆汽车另外2扇门后面各是一只山羊你看不见门后面的情况但主持人知道一切。你被主持人要求在三扇门中选择一扇但不能打开在你选定之后主持人开启了另一扇后面有山羊的门然后你可以坚持原来选定的门也可以改主意重新选择。问题是改与不改对选中汽车的概率有影响吗请使用模拟实验的方法回答该问题。"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"在100000次测试中,坚持原则第一次就选中的次数是33063,改变决定选择另一扇门中奖的次数是66937\n",
"概率分别是0.33063和0.66937改变决定选择另一扇门中奖几率是坚持选择的2.02倍\n"
]
}
],
"source": [
"# 三门问题\n",
"import numpy.random as random\n",
"def MontyHallProblem(n_test):\n",
" #测试次数\n",
" \n",
" winning_door = random.randint(0,3,n_test)\n",
" first_get = 0\n",
" change_get = 0\n",
" for winning_doors in winning_door:\n",
" act_door = random.randint(0,3)\n",
" if winning_doors == act_door:\n",
" first_get += 1\n",
" else :\n",
" change_get += 1\n",
" first_pro = first_get / n_test\n",
" change_pro = change_get / n_test\n",
" compar1 = round(change_get / first_get,2)\n",
" print (\"在%d次测试中,坚持原则第一次就选中的次数是%d,改变决定选择另一扇门中奖的次数是%d\"% (n_test,first_get,change_get))\n",
" print (\"概率分别是{0}和{1},改变决定选择另一扇门中奖几率是坚持选择的{2}倍\".format(first_pro,change_pro,compar1))\n",
"MontyHallProblem(100000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 117 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 205 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 294 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 137 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 107 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 72 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 516 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 316 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 168 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 174 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 616 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 474 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 400 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 360 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

Some files were not shown because too many files have changed in this diff Show More