的历史维度数字直方图密度=Tru

2024-04-26 21:38:19 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个数组A:

array([ 0.0019879 , -0.00172861, -0.00527226,  0.00639585, -0.00242005,
   -0.00717373,  0.00371651,  0.00164218,  0.00034572, -0.00864304,
   -0.00639585,  0.006828  ,  0.00354365,  0.00043215, -0.00440795,
    0.00544512,  0.00319793,  0.00164218,  0.00025929, -0.00155575,
    0.00129646,  0.00259291, -0.0039758 ,  0.00328436,  0.00207433,
    0.0011236 ,  0.00440795,  0.00164218, -0.00319793,  0.00233362,
    0.00025929,  0.00017286,  0.0008643 ,  0.00363008])

如果我跑:

^{pr2}$

据我所知:

array([  34.21952021,   34.21952021,   34.21952021,   34.21952021,
     34.21952021,  188.20736116,  102.65856063,   68.43904042,
     51.32928032])

手册上说:

"If True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability mass function."

我认为我对直方图和密度函数有很好的理解,但我真的不明白这些值代表什么,也不知道它们是如何计算的。在

我需要用R重新生成这些值,因为我要在两种语言之间移植一些代码。在


Tags: ofthetrueifthatisvaluenot
1条回答
网友
1楼 · 发布于 2024-04-26 21:38:19

在R中,可以使用hist()函数来绘制直方图。另外,hist是一个生成列表的S3函数。在

A <- c(0.0019879 , -0.00172861, -0.00527226,  0.00639585, -0.00242005,
        -0.00717373,  0.00371651,  0.00164218,  0.00034572, -0.00864304,
        -0.00639585,  0.006828  ,  0.00354365,  0.00043215, -0.00440795,
        0.00544512,  0.00319793,  0.00164218,  0.00025929, -0.00155575,
        0.00129646,  0.00259291, -0.0039758 ,  0.00328436,  0.00207433,
        0.0011236 ,  0.00440795,  0.00164218, -0.00319793,  0.00233362,
        0.00025929,  0.00017286,  0.0008643 ,  0.00363008)

这是R用向量A生成的默认直方图。在

^{pr2}$

这是一个柱状图,它是密度曲线的附加层。在

hist(A, freq = F)
lines(density(A), col = 'red')

enter image description here

让我们将列表hist(A)存储到p。在

p <- hist(A)

我们现在可以看到列表p的内容。在

str(p)
# List of 6
#  $ breaks  : num [1:10] -0.01 -0.008 -0.006 -0.004 -0.002 0 0.002 0.004 # 0.006 0.008
#  $ counts  : int [1:9] 1 2 2 3 2 12 8 2 2
#  $ density : num [1:9] 14.7 29.4 29.4 44.1 29.4 ...
#  $ mids    : num [1:9] -0.009 -0.007 -0.005 -0.003 -0.001 0.001 0.003 0.005 0.007
#  $ xname   : chr "A"
#  $ equidist: logi TRUE
#  - attr(*, "class")= chr "histogram"

density表示理论密度函数值。这可以超过1,但密度曲线下的面积应等于1。每个条的宽度很容易由柱状图中条的断点(breaks)之间的差异来确定。因此,如果我们将直方图的每个条的宽度乘以p$density,并将结果相加,则得到的和应该是1。在

sum(diff(p$breaks) * p$density)
# [1] 1

相关问题 更多 >