# Unsure about counts and proportions

**URL:** <https://discourse.julialang.org/t/unsure-about-counts-and-proportions/67333>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 30, 2021, 8:09am UTC](https://discourse.julialang.org/t/unsure-about-counts-and-proportions/67333 "2021-08-30T08:09:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naba7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naba7/32/10203_2.png) [@Naba7](https://discourse.julialang.org/u/Naba7)\
**Post date:** [August 30, 2021, 8:09am UTC](https://discourse.julialang.org/t/unsure-about-counts-and-proportions/67333/1 "2021-08-30T08:09:45Z")

</div>

`isequal(proportions([1, 2, 2, 3, 4, 5, 5, 5, 7, 7], s1), counts([1, 2, 2, 3, 4, 5, 5, 5, 7, 7], s1)/length([1, 2, 2, 3, 4, 5, 5, 5, 7, 7]))`  
when `s1 = span([1, 2, 2, 3, 4, 5, 5, 5, 7, 7])`  
gives false while

`proportions([1, 2, 2, 3, 4, 5, 5, 5, 7, 7], s1)` gives equal vector as of `counts([1, 2, 2, 3, 4, 5, 5, 5, 7, 7], s1)/length([1, 2, 2, 3, 4, 5, 5, 5, 7, 7]))` and both are of same types.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 30, 2021, 8:44am UTC](https://discourse.julialang.org/t/unsure-about-counts-and-proportions/67333/2 "2021-08-30T08:44:38Z")

</div>

Seems to be a problem of floating point precision:

```julia
julia> using StatsBase

julia> isapprox(proportions([1, 2, 2, 3, 4, 5, 5, 5, 7, 7], s1), counts([1, 2, 2, 3, 4, 5, 5, 5, 7, 7], s1)/length([1, 2, 2, 3, 4, 5, 5, 5, 7, 7]))
true

```

The documentations says:

```julia
help?> proportions
...
Equivalent to counts(x, levels) / length(x)
...

```

which seems to be wrong:

```julia
julia> @less proportions(x,s)

...
proportions(x::IntegerArray, levels::IntUnitRange) = counts(x, levels) .* inv(length(x))
...

```

Therefore:

```julia
julia> using StatsBase

julia> x=[1, 2, 2, 3, 4, 5, 5, 5, 7, 7]

julia> s=span(x)

julia> proportions(x,s)

julia> counts(x, s) .* inv(length(x))

julia> isequal(proportions(x,s),counts(x, s) .* inv(length(x)))
true

```

Opening an issue (or better a PR) regarding the documentation would be appropriate, I guess:  
[https://juliastats.org/StatsBase.jl/stable/counts/#StatsBase.proportions](https://juliastats.org/StatsBase.jl/stable/counts/#StatsBase.proportions)

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 30, 2021, 9:01am UTC](https://discourse.julialang.org/t/unsure-about-counts-and-proportions/67333/3 "2021-08-30T09:01:05Z")

</div>

Some more example to show the underlying floating point precision issue here:

```julia
julia> 3/10
0.3

julia> 3*0.1
0.30000000000000004

```
