Probability And Statistics——CorrelationsCovariance

article/2025/10/29 15:28:55

Skew(偏度)

在概率论和统计学中,偏度衡量实数随机变量概率分布的不对称性。偏度的值可以为正,可以为负或者甚至是无法定义。在数量上,偏度为负(负偏态)就意味着在概率密度函数左侧的尾部比右侧的长,绝大多数的值(包括中位数在内)位于平均值的右侧。偏度为正(正偏态)就意味着在概率密度函数右侧的尾部比左侧的长,绝大多数的值(但不一定包括中位数)位于平均值的左侧。偏度为零就表示数值相对均匀地分布在平均值的两侧,但不一定意味着其为对称分布。

import matplotlib.pyplot as pltplt.hist(test_scores_negative)
plt.show()plt.hist(test_scores_positive)
plt.show()plt.hist(test_scores_normal)
plt.show()from scipy.stats import skew
negative_skew = skew(test_scores_negative)
positive_skew = skew(test_scores_positive)
no_skew = skew(test_scores_normal)
'''
-0.6093247474592194 
0.5376950498203763 
0.0223645171350847
'''

这里写图片描述

  • 图中描绘的是三个数据的直方图,表示了数据的分布,可以发现第一张图的数据大部分集中在平均值的右侧(负偏态),中间的图集中在平均值的左侧(正偏态),最后一张图集中在均值附近。

Kurtosis(峰度)

在统计学中,峰度(Kurtosis)衡量实数随机变量概率分布的峰态。峰度高就意味着方差增大是由低频度的大于或小于平均值的极端差值引起的。
峰度(Kurtosis)与偏态(Skewness)就是量测数据正态分布特性的两个指标。峰度衡量数据分布的平坦度(flatness)。尾部大的数据分布,其峰度值较大。正态分布的峰度值为3。偏态量度对称性。0说明是最完美的对称性,正态分布的偏态就是0。

  • 峰度其公式如下:

这里写图片描述

  • 偏态的计算公式如下:

这里写图片描述

kurt_platy = kurtosis(test_scores_platy)

Modality(情态)

Modality refers to the number of modes, or peaks, in a distribution. Real-world data often is unimodal (only has one mode).

import matplotlib.pyplot as plt# This plot has one mode, making it unimodal
plt.hist(test_scores_uni)
plt.show()# This plot has two peaks, and is bimodal
# This could happen if one group of students learned the material, and one learned something else, for example.
plt.hist(test_scores_bi)
plt.show()# More than one peak means that the plot is multimodal
# We can't easily measure the modality of a plot, like we can with kurtosis or skew.
# Often, the best way to detect multimodality is to observe the plot.
plt.hist(test_scores_multi)
plt.show()

这里写图片描述

Mean(均值)

import matplotlib.pyplot as pltplt.hist(test_scores_normal)
# The axvline function will plot a vertical line over an existing plot
plt.axvline(test_scores_normal.mean())
plt.show()plt.hist(test_scores_negative)
plt.axvline(test_scores_negative.mean())
plt.show()plt.hist(test_scores_positive)
plt.axvline(test_scores_positive.mean())
plt.show()

这里写图片描述

Median(中位数)

  • 同时显示中位数和均值

import numpy
import matplotlib.pyplot as plt# Plot the histogram
plt.hist(test_scores_negative)
# Compute the median
median = numpy.median(test_scores_negative)# Plot the median in blue (the color argument of "b" means blue)
plt.axvline(median, color="b")# Plot the mean in red
plt.axvline(test_scores_negative.mean(), color="r")# See how the median is further to the right than the mean?
# It's less sensitive to outliers, and isn't pulled to the left.
plt.show()
plt.hist(test_scores_positive)
plt.axvline(numpy.median(test_scores_positive), color="b")
plt.axvline(test_scores_positive.mean(), color="r")
plt.show()

这里写图片描述


  • 下面的统计分析是基于NBA的数据集,大概格式如下

player,pos,age,bref_team_id,g,gs,mp,fg,fga,fg.,x3p,x3pa,x3p.,x2p,x2pa,x2p.,efg.,ft,fta,ft.,orb,drb,trb,ast,stl,blk,tov,pf,pts,season,season_end
[Quincy,Acy,SF,23,TOT,63,0,847,66,141,0.468,4,15,0.266666666666667,62,126,0.492063492063492,0.482,35,53,0.66,72,144,216,28,23,26,30,122,171,2013-2014,2013]
[Steven,Adams,C,20,OKC,81,20,1197,93,185,0.503,0,0,NA,93,185,0.502702702702703,0.503,79,136,0.581,142,190,332,43,40,57,71,203,265,2013-2014,2013]

player – name of the player.
pts – the total number of points the player scored in the season.
ast – the total number of assists the player had in the season.
fg. – the player’s field goal percentage for the season.

Calculating Standard Deviation

  • q其实std()函数就可以计算标准差
# The nba stats are loaded into the nba_stats variable.
def calc_column_deviation(column):mean = column.mean()variance = 0for p in column:difference = p - meansquare_difference = difference ** 2variance += square_differencevariance = variance / len(column)return variance ** (1/2)mp_dev = calc_column_deviation(nba_stats["mp"])
ast_dev = calc_column_deviation(nba_stats["ast"])

Normal Distribution

  • norm.pdf可以将一组数据生成正态分布数据,给予每个数据相应的概率使得满足给定的均值方差。
import numpy as np
import matplotlib.pyplot as plt
# The norm module has a pdf function (pdf stands for probability density function)
from scipy.stats import norm# The arange function generates a numpy vector
# The vector below will start at -1, and go up to, but not including 1
# It will proceed in "steps" of .01.  So the first element will be -1, the second -.99, the third -.98, all the way up to .99.
points = np.arange(-1, 1, 0.01)# The norm.pdf function will take points vector and turn it into a probability vector
# Each element in the vector will correspond to the normal distribution (earlier elements and later element smaller, peak in the center)
# The distribution will be centered on 0, and will have a standard devation of .3
probabilities = norm.pdf(points, 0, .3)# Plot the points values on the x axis and the corresponding probabilities on the y axis
# See the bell curve?
plt.plot(points, probabilities)
plt.show()
points = np.arange(-10, 10, 0.1)
probabilities = norm.pdf(points, 0, 2)
plt.plot(points, probabilities)
plt.show()

这里写图片描述

Covariance(协方差)

这里写图片描述

# The nba_stats variable has been loaded.
def covariance(x, y):x_mean = sum(x) / len(x)y_mean = sum(y) / len(y)x_diffs = [i - x_mean for i in x]y_diffs = [i - y_mean for i in y]codeviates = [x_diffs[i] * y_diffs[i] for i in range(len(x))]return sum(codeviates) / len(codeviates)cov_stl_pf = covariance(nba_stats["stl"], nba_stats["pf"])
cov_fta_pts = covariance(nba_stats["fta"], nba_stats["pts"])

Correlations

  • 相关性最常见的度量方法是Pearson’s r,也叫r-value.
from scipy.stats.stats import pearsonrr, p_value = pearsonr(nba_stats["fga"], nba_stats["pts"])
# As we can see, this is a very high positive r value -- close to 1
print(r)
r_fta_pts, p_value = pearsonr(nba_stats["fta"], nba_stats["pts"])
r_stl_pf, p_value = pearsonr(nba_stats["stl"], nba_stats["pf"])
'''
0.369861731248
'''
  • 相关性的计算公式如下:

    这里写图片描述

from numpy import cov
# The nba_stats variable has been loaded in.
r_fta_blk = cov(nba_stats["fta"], nba_stats["blk"])[0,1] / ((nba_stats["fta"].var() * nba_stats["blk"].var())** (1/2))
r_ast_stl = cov(nba_stats["ast"], nba_stats["stl"])[0,1] / ((nba_stats["ast"].var() * nba_stats["stl"].var())** (1/2))

http://chatgpt.dhexx.cn/article/6IwlMLpq.shtml

相关文章

structural covariance network

structural covariance network 结构协方差网络 结构协方差网络是一个较老的概念,只是近年受到了一定的重视。 大佬 Aaron Alexander-Bloch 在2013年通过一篇综述描述了这种结构协方差网络的应用意义及前景。 既往一般是在bold信号和fiber tracking建立连接&#xf…

_variant_t、CComVariant与COleVariant、CDBVariant

目前计算机语言多种多样,如C、Java、Basic、Pascal等,此外还有JavaScript、VBScript、ActionScript等脚本语言,它们各自维护自己的数据类型,当使用C这样强类型的语言来读取数据库或者与其他语言之间来交换数据时,它很有…

Partial correlation coefficient

利用PYTHON计算偏相关系数(Partial correlation coefficient) 在统计学中,我们经常使用皮尔逊相关系数来衡量两个变量之间的线性关系。然而,有时我们感兴趣的是理解两个变量之间的关系,同时控制第三个变量。 例如,假设…

Multilevel Cooperative Coevolution for Large Scale Optimization

0、论文背景 本文在CCEA_G的基础上,提出了MLCC框架。在MLCC中,基于不同组大小的随机分组策略构造了一组问题分解器。演化过程分为若干个循环,在每个周期开始时,MLCC使用自适应机制根据其历史性能选择分解器。由于不同的组大小捕获…

mean value coordinates(均值重心坐标)定义及证明

欢迎关注更多精彩 关注我,学习常用算法与数据结构,一题多解,降维打击。 在图形学中对于物体的描述往往是离散,但是在具体展示过程中我们又希望是连续。线性插值是解决离散与连续的常用手段。 三角形中的插值点击前往凸四边形中的…

numpy中的convolve的理解

写在前面 浏览更多内容,可访问:http://www.growai.cn 欢迎您关注作者知乎:ML与DL成长之路 推荐关注公众号:AI成长社,ML与DL的成长圣地。 函数 numpy.convolve(a, v, mode‘full’),这是numpy函数中的卷…

Clustering Coefficient

Define Clustering Coefficient:聚类系数 Clustering Coefficient measures the degree to which nodes in a network tend to cluster or form triangles. ——聚类系数衡量网络中节点倾向于聚类或形成三角形的程度 Triadic Closure 三元闭包 The tendency of…

covariate(covariate是控制变量吗)

如何用STATA对连续性变量进行meta回归分析 在stata中有个metareg命令,好像可以对连续变量进行回归分析。 附件中是一篇pdf文档,主要介绍stata中关于meta分析的命令。跟大家分享一下。 里面在提到metareg命令时,列举了以下三个列子&#xff1a…

协方差矩阵简介(Covariance Matrix)

协方差矩阵定义 首先我们要明白,协方差实际是在概率论和统计学中用于衡量两个变量的总体误差,当然方差是协方差的一种特殊情况,即当两个变量是相同情况。它表示的是两个变量的总体的误差,这与只表示一个变量误差的方差不同。如果两个变量的变…

covariance matrix

协方差的定义 对于一般的分布,直接代入E(X)之类的就可以计算出来了,但真给你一个具体数值的分布,要计算协方差矩阵,根据这个公式来计算,还真不容易反应过来。这里用一个例子说明协方差矩阵是怎么计算出来的吧。 记住&…

经典排序算法——堆排序

对于一个int数组,请编写一个堆排序算法,对数组元素排序。 给定一个int数组A及数组的大小n,请返回排序后的数组。 测试样例: [1,2,3,5,2,3],6 [1,2,2,3,3,5] class HeapSort { public:int* heapSort(int* A, int n) {BuildMaxHeap(…

堆排序算法原理及c++实现

文章目录 准备知识MAX-HEAPIFY过程建堆堆排序算法总结 准备知识 堆的结构可以分为最大堆和最小堆,是一个完全二叉树,而堆排序是根据堆的这种数据结构设计的一种排序。 所谓完全二叉树即叶节点只能出现在最下层和次下层,并且最下面一层的结点…

堆排序算法设计与分析

堆排序(HeapSort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种。堆分为大根堆和小根堆,是完全二叉树。大根堆要求父结点的值大于或等于子结点的值,小根堆相反。根据大根堆的性质,我们可以知道最大值一…

堆排序算法实现

堆排序:结构逻辑上是完全二叉树,但是可以使用顺序存储来实现 一些二叉树的区别: 二叉树:度数最大为2并且每个子树也是二叉树 满二叉树:每层节点都是满的,没有空缺,也就是,叶子节点只能出现在最后一层 完全二叉树:限制条件比满二叉树弱化,只需要前k-1层是满二叉树结构,最后…

数据结构之堆排序算法详解+C语言实现

堆   堆是具有以下性质的完全二叉树:每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆;或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆。 堆排序   堆排序是利用堆这种数据结构而设计的一种排序算法&…

堆排序算法原理及实现

堆排序是排序中一种比较重要的算法,和快速排序一样,其复杂度也是O(nlogn);同时也是一种原地排序算法:在任何时候,数组中只有常数个元素存储在输入数组以外。堆这种数据结构是处理海量数据比较常见的结构,海…

堆排序算法Java

基本原理 1):将带排序的序列构造成一个大顶堆,根据大顶堆的性质,当前堆的根节点(堆顶)就是序列中最大的元素 2):将堆顶元素和最后一个元素交换,然后将剩下的节点重新构造成一个大顶堆; 3):重复步骤2 小知识…

堆排序算法详细分析

一、堆相关概念 1.堆 堆是完全二叉树,即除最后一层外,其它层都是满的,且最后一层从左到右依次都有元素。如下图所示。 堆是用数组来实现的,图中下标就为数组的下标,其对应数组[5, 1, 7, 2, 8, 6, 3, 9, 4]&#xf…

数据结构——堆排序(算法)

基本介绍 1)、堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最好、最坏、平均时间复杂度均为O(nlogn),它也是不稳定排序。2)、堆是具有以下性质的完全二叉树:每个节点的值都…

C++:堆排序算法详解

图解排序算法(三)之堆排序 预备知识 堆排序 堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复杂度均为O(nlogn),它也是不稳定排序。首先简单了解下堆结构。 堆 堆是具有…