如何计算两个直方图之间的差异?

时间:2021-01-24 01:01:40

标签: julia histogram

我有两个直方图(都具有相同的 bin 大小),我想比较每个 bin。 我已经在同一个直方图中绘制了它们,所以我对两组进行了视觉比较。现在我想为每个 bin 计算它们之间的差异。 例如,如果在第一个 bin 中,第 1 组的频率为 2,第 2 组的频率为 5,我想得到它们之间的差值 = 3。

1 个答案:

答案 0 :(得分:1)

Histogram 有字段 weights,您可以比较它们:

julia> using StatsBase, Random; Random.seed!(0);

julia> x1, x2 = rand(100), rand(100);

julia> h1 = fit(Histogram, x1, 0:0.1:1);

julia> h2 = fit(Histogram, x2, 0:0.1:1);

julia> h1.weights .- h2.weights
10-element Vector{Int64}:
  4
  2
 -7
  1
 -2
 -3
  0
  2
 -1
  4

你可以用它直观地绘制,例如,Plots.jl:

julia> using Plots

julia> p1 = plot(h1, α=0.5, lab="x1") ; plot!(p1, h2, α=0.5, lab="x2")

julia> p2 = bar(0:0.1:1, h2.weights - h1.weights, lab="diff")

julia> plot(p1, p2)

enter image description here

或者你的意思是统计测试:

julia> using HypothesisTests

julia> ApproximateTwoSampleKSTest(x1,x2)
Approximate two sample Kolmogorov-Smirnov test
----------------------------------------------
Population details:
    parameter of interest:   Supremum of CDF differences
    value under h_0:         0.0
    point estimate:          0.1

Test summary:
    outcome with 95% confidence: fail to reject h_0
    two-sided p-value:           0.6994

Details:
    number of observations:   [100,100]
    KS-statistic:              0.7071067811865475

最后,卡方检验实际上是在比较直方图:

julia> ChisqTest( hcat(h1.weights, h2.weights))
Pearson's Chi-square Test
-------------------------
Population details:
    parameter of interest:   Multinomial Probabilities

...

Test summary:
    outcome with 95% confidence: fail to reject h_0
    one-sided p-value:           0.7731

...
相关问题