打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
AI系列二:数学基础-概率与统计-概率

概率

概率亦称“或然率”。它反映随机事件出现的可能性大小的量度。随机事件是指在相同条件下,可能出现也可能不出现的事件。例如,从一批有正品和次品的商品中,随意抽取一件,“抽得的是正品”就是一个随机事件。设对某一随机现象进行了n次试验与观察,其中A事件出现了m次,即其出现的频率为m/n。经过大量反复试验,常有m/n越来越接近于某个确定的常数。该常数即为事件A出现的概率,常用P (A) 表示。

基本定义:

  • 试验:一次无法预计结果的行动,比如扔硬币。
  • 样本空间:一次试验所有可能结果集,比如扔硬币,样本空间有两个结果。
  • 样本点:一个可能结果。
  • 事件:一次试验的具体结果,比如扔一次硬币得到正面。
  • 概率:取值0到1之间,表示一个具体事件的可能性。0表示不可能,1表示必然发生。

举例:掷两个骰子,希望掷出7。

36个样本点组成的样本空间。

6个样本点得到7,所以掷出7的概率是6 / 36 = 0.167 (16.7%)。

P(A) = 0.167

倾斜(Bias): 有时样本空间中的样本点没有相同的概率,我们称这种情况是倾斜,使得一个结果比另一个结果可能性更大,例如:

A代表天晴,B代表多云,C代表下雨。

P(A) = 0.6; P(B) = 0.3; P(C) = 0.1

P(A′) = 1 − P(A) = 1 − 0.6 = 0.4,A′代表任何一个非晴天

条件概率

事件有以下几种类型:

  • 独立事件
  • 依赖事件
  • 互斥事件

独立事件

例如投硬币,样本空间有两个可能结果:正面和背面。得到正面的概率和得到背面的概率都是1/2,每次投硬币都是一个独立事件,意味着每次的结果都不会依赖上一次。

%matplotlib inlineimport random# Create a list with 2 element (for heads and tails)heads_tails = [0,0] # loop through 10000 trialstrials = 10000trial = 0while trial < trials: trial = trial + 1 # Get a random 0 or 1 toss = random.randint(0,1) # Increment the list element corresponding to the toss result heads_tails[toss] = heads_tails[toss] + 1 print (heads_tails) # Show a pie chart of the resultsfrom matplotlib import pyplot as pltplt.figure(figsize=(5,5))plt.pie(heads_tails, labels=['heads', 'tails'])plt.legend()plt.show()

[4994, 5006]

如果连续投硬币三次,三次都是正面的概率又是多少呢?

这样的情况就是独立事件组合

P(A) = 1/2 * 1/2 * 1/2 = 0.125

依赖事件

我们来考虑抽扑克牌场景,样本空间如下:

A代表抽取黑色牌,B代表抽取红色牌。

抽一张牌:

假定不放回去,事件A和B的概率变成:

考虑这样一个场景:在给定事件A为抽一张黑色牌的前提下,抽一张红色牌的概率是?这就是所谓的条件概率。

互斥事件

二项分布

二项分布就是重复n次独立的伯努利试验。在每次试验中只有两种可能的结果,而且两种结果发生与否互相对立,并且相互独立,与其它各次试验结果无关,事件发生与否的概率在每一次独立试验中都保持不变,则这一系列试验总称为n重伯努利实验,当试验次数为1时,二项分布服从0-1分布。

  • 二项概率公式:

对于二项变量,我们使用该公式来计算概率质量函数。

  • 概率质量函数 (Probability Mass Function,PMF)

是离散随机变量在各特定取值上的概率。概率质量函数和概率密度函数不同之处在于:概率密度函数是对连续随机变量定义的。

举例:机场安检,进行5次试验,每次试验都会有一个乘客过安检,过安检时会有两种状态:被搜身或不搜身,每个乘客被搜身的概率是25%,5位乘客中有3位被搜身的概率是多少?除了通过公式计算外,我们通过scipy.stats.binom.pmf来看下整个分布。

%matplotlib inlinefrom scipy.stats import binomfrom matplotlib import pyplot as pltimport numpy as np n = 5p = 0.25x = np.array(range(0, n+1)) prob = np.array([binom.pmf(k, n, p) for k in x]) # Set up the graphplt.xlabel('x')plt.ylabel('Probability')plt.bar(x, prob)plt.show()

从图中可以看出,该分布是右倾斜的,原因很简单,n取值太小,让我们增加n到100来看看。

%matplotlib inlinefrom scipy.stats import binomfrom matplotlib import pyplot as pltimport numpy as np n = 100p = 0.25x = np.array(range(0, n+1)) prob = np.array([binom.pmf(k, n, p) for k in x]) # Set up the graphplt.xlabel('x')plt.ylabel('Probability')plt.bar(x, prob)plt.show()

  • 期望值(Expected Value)

μ = np

以上例子的期望值是:μ = 100 × 0.25 = 25,意味着100位乘客有25位乘客被搜身。

  • 方差和标准差

方差:

标准差:

from scipy.stats import binom n = 100p = 0.25 print(binom.mean(n,p))print(binom.var(n,p))print(binom.std(n,p))

25.0

18.75

4.330127018922194

假设检验

学生们结束了今年的学业,被要求为其中一门课数据科学打分,-5(糟糕)到5(很好),该课程是通过网上在线形式教授的,受众学生成千上万,这里获取50个随机样本数据。

import numpy as npimport matplotlib.pyplot as plt%matplotlib inline np.random.seed(123)lo = np.random.randint(-5, -1, 6)mid = np.random.randint(0, 3, 38)hi = np.random.randint(4, 6, 6)sample = np.append(lo,np.append(mid, hi))print('Min:' + str(sample.min()))print('Max:' + str(sample.max()))print('Mean:' + str(sample.mean())) plt.hist(sample)plt.show()

Min:-5

Max:5

Mean:0.84

该样本的均值0.84意味着学生们对该课的态度还是正面的,也就是喜欢该课,但这个结论只是基于随机样本,那如果是整个数据呢?我们来定义两个假设:

  1. H0假设:分数的总体均值小于等于0,我们的样本均值大于0是由于样本选择的偶然性。
  2. H1假设:分数的总体均值大于0,我们的样本也均值大于0,说明样本正确的探测到了这个趋势。

H0 : μ ≤ 0

H1 : μ > 0

如果H0假设正确,50个样本分布会是一个正态分布,均值是0。

import numpy as npimport matplotlib.pyplot as plt%matplotlib inline pop = np.random.normal(0, 1.15, 100000)plt.hist(pop, bins=100)plt.axvline(pop.mean(), color='yellow', linestyle='dashed', linewidth=2)plt.show()

T检验,亦称student t检验(Student's t test),主要用于样本含量较小(例如n < 30),总体标准差σ未知的正态分布。 T检验是用t分布理论来推论差异发生的概率,从而比较两个平均数的差异是否显著。单样本t检验是检验一个样本平均数与一个已知的总体平均数的差异是否显著。当总体分布是正态分布,如总体标准差未知且样本容量小于30,那么样本平均数与总体平均数的离差统计量呈t分布。

单样本t检验公式:

x̄是样本均值,μ是总体均值,s是标准差,n是样本均值。

from scipy import statsimport numpy as npimport matplotlib.pyplot as plt%matplotlib inline # T-Testt,p = stats.ttest_1samp(sample, 0)# ttest_1samp is 2-tailed, so half the resulting p-value to get a 1-tailed p-valuep1 = '%f' % (p/2)print ('t-statistic:' + str(t))print('p-value:' + str(p1)) # calculate a 90% confidence interval. 10% of the probability is outside this, 5% in each tailci = stats.norm.interval(0.90, 0, 1.15)plt.hist(pop, bins=100)# show the hypothesized population meanplt.axvline(pop.mean(), color='yellow', linestyle='dashed', linewidth=2)# show the right-tail confidence interval threshold - 5% of propbability is under the curve to the right of this.plt.axvline(ci[1], color='red', linestyle='dashed', linewidth=2)# show the t-statistic - the p-value is the area under the curve to the right of thisplt.axvline(pop.mean() + t*pop.std(), color='magenta', linestyle='dashed', linewidth=2)plt.show()

t-statistic:2.773584905660377

p-value:0.003911

黄线代表H0假设的总体均值,红线右边和曲线下方围成的区域表达了显著性水平0.05(5%),紫线右边和曲线下方围成的区域表达了p-value。

p-value:统计学根据显著性检验方法所得到的P 值,一般以P < 0.05 为显著, P <0.01 为非常显著,其含义是样本间的差异由抽样误差所致的概率小于0.05 或0.01。

那结论是什么呢?

p-value小于显著性水平0.05,说明样本间的差异由抽样误差所致的概率小于0.05,很低,意味着我们可以合理的拒绝H0,也就是说总均值大于0,该课程的评价分数是积极的、正面的。

双尾检验

重新定义之前的假设

  1. H0假设:分数的总体均值等于0,我们的样本均值大于或小于0是由于样本选择的偶然性。
  2. H1假设:分数的总体均值不等于0,我们的样本也均值大于0。

H0 : μ = 0

H1 : μ != 0

from scipy import stats

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

# T-Test

t,p = stats.ttest_1samp(sample, 0)

print ('t-statistic:' + str(t))

# ttest_1samp is 2-tailed

print('p-value:' + '%f' % p)

# calculate a 95% confidence interval. 50% of the probability is outside this, 2.5% in each tail

ci = stats.norm.interval(0.95, 0, 1.15)

plt.hist(pop, bins=100)

# show the hypothesized population mean

plt.axvline(pop.mean(), color='yellow', linestyle='dashed', linewidth=2)

# show the confidence interval thresholds - 5% of propbability is under the curve outside these.

plt.axvline(ci[0], color='red', linestyle='dashed', linewidth=2)

plt.axvline(ci[1], color='red', linestyle='dashed', linewidth=2)

# show the t-statistic thresholds - the p-value is the area under the curve outside these

plt.axvline(pop.mean() - t*pop.std(), color='magenta', linestyle='dashed', linewidth=2)

plt.axvline(pop.mean() + t*pop.std(), color='magenta', linestyle='dashed', linewidth=2)

plt.show()

t-statistic:2.773584905660377

p-value:0.007822

双尾p-value小于0.05,所以我们拒绝H0假设。

两样本检验

这里不再累述。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python数据清洗--异常值识别与处理01
用matplotlib在同一个画布显示20个 双y轴折线图
概率论07 联合分布
实例讲解统计学基础知识(4):参数估计
机器学习|回归算法中的贝叶斯
4
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服