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

Julia 如何计算两个直方图之间的差异?,julia,histogram,Julia,Histogram,我有两个直方图(都有相同的箱子大小),我想比较每个箱子。 我已经在同一个柱状图中绘制了它们,所以我对这两组进行了视觉比较。现在我想计算每个箱子的差值。 例如,如果在第一个箱子中,组1的频率为2,组2的频率为5,我想得到它们之间的差值=3。直方图有字段权重,您可以比较它们: julia> using StatsBase, Random; Random.seed!(0); julia> x1, x2 = rand(100), rand(100); julia> h1 = fit

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

直方图
有字段权重,您可以比较它们:

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)

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

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

...

您好,我使用“h1=直方图(x,nbins=料仓,fillalpha=0.3)和h2=直方图(y,nbins=料仓,fillalpha=0.3)”来创建直方图,当我尝试使用“权重”时,它会说“类型图没有字段权重”。我需要像你一样创建直方图吗?使用fit?是的,正如Przemyslaw的详细回答一样,您应该使用StatsBase.jl的
fit(直方图、数据、箱子)