Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

julia - How can i calculate the difference between two histograms?

I have two histograms (both with the same bin size) and i want to compare each bin. I already graphed them in the same histogram so i have a visual comparison of both groups. Now i want to calculate the difference between them for each bin. For ex, if in the first bin the group 1 has a frecuency of 2 and the group 2 of 5, i want to get the difference between them = 3.

question from:https://stackoverflow.com/questions/65866191/how-can-i-calculate-the-difference-between-two-histograms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Histogram has field weights and you can compare them:

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

which you can visually plot with, e.g., 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

Or maybe you meant statistical testing:

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

Finally, the Chi-square test is actually comparing the histograms:

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

...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...