# \[ANN\] WeightedOnlineStats.jl

**URL:** <https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317>\
**Category:** Package Announcements\
**Tags:** package, announcement, statistics\
**Created:** [December 5, 2018, 12:47pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317 "2018-12-05T12:47:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [December 5, 2018, 12:47pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/1 "2018-12-05T12:47:48Z")

</div>

Check out `WeightedOnlineStats.jl` ([GitHub - gdkrmr/WeightedOnlineStats.jl: Weighted version of OnlineStats.jl](https://github.com/gdkrmr/WeightedOnlineStats.jl)) if you like `OnlineStats.jl` but were missing proper statistical weighting.

---

<div class="post-metadata">

**Author:** ![ValdarT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valdart/32/24146_2.png) [@ValdarT](https://discourse.julialang.org/u/ValdarT)\
**Post date:** [December 5, 2018, 1:00pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/2 "2018-12-05T13:00:07Z")

</div>

This looks great, thank you for your work!  
Out of curiosity, why a new package instead of PRs to `OnlineStats.jl`?

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [December 5, 2018, 1:18pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/3 "2018-12-05T13:18:18Z")

</div>

`OnlineStats.jl` has a very different concept of weights, so we felt it was better to do this in a separate package.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [December 6, 2018, 11:41pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/4 "2018-12-06T23:41:30Z")

</div>

Why is this not using `StatsBase.Weights`?

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [December 7, 2018, 9:25am UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/5 "2018-12-07T09:25:32Z")

</div>

`StatsBase.Weights` are actual vectors that store all the weights, which is not what we want for something like `OnlineStats`. There is also no need to fix the type of weights when creating the object. If you have an idea how `StatsBase.Weights` can give more value to `WeightedOnlineStats`, I am all ears.

We could use multiple dispatch and do something like this

```julia
o = fit!(WeightedVariance(), x, w)
var(o, AnalyticWeights)

```

instead of

```julia
var(o, corrected = true, weight_type = :analytic)

```

but this is still very different from what `StatsBase` does:

```julia
var(x, aweights(w))

```

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [December 7, 2018, 3:37pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/6 "2018-12-07T15:37:34Z")

</div>

`StatsBase.Weights` are a light wrapper around an `AbstractVector` (no data is copied, just a reference and the sum). You could do something like `StatsBase.Weights(FillArrays.Ones(x))` which is basically a close to zero cost operation.

Easier to use the flexible proven API rather than have a different one. At least for statistical modeling, StatsBase weights abstraction comes in very handy. It should also lead to less code duplication as the functions are already implemented with the weighted generalization flavor.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [December 7, 2018, 4:37pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/7 "2018-12-07T16:37:54Z")

</div>

How would you suggest doing that?

```julia
o = WeightedMean()
fit!(o, x, aweights(w))

```

what if I do

```julia
fit!(o, x2, fweights(w2))

```

then on the same `o`?

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [December 7, 2018, 4:47pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/8 "2018-12-07T16:47:56Z")

</div>

Between,

```julia
o = WeightedMean()
fit!(o, x, aweights(w))

```

and

```julia
fit!(o, x2, fweights(w2))

```

Not sure about what `fit!` does in this case, but if needs an iterable, a function, and weights,

```julia
fit(x, ::Function, ::AbstractWeights = FrequencyWeights(Ones(x)))

```

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [December 7, 2018, 5:33pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/9 "2018-12-07T17:33:06Z")

</div>

> [@Nosferican](#):
>
> fit(x, ::Function, ::AbstractWeights = FrequencyWeights(Ones(x)))

I am very much agains the verbose syntax and the extra dependency. Also it does not make much sense to include this in the `fit!` function or the `WeightedOnlineStat` because there is no added value (maybe except for not keeping track of `sum(w.^2)`).

The best way I see this could be included would be by something like:

```julia
var(o::WeightedVar, ::Type{AnalyticWeights}) 

```

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [December 7, 2018, 5:46pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/10 "2018-12-07T17:46:56Z")

</div>

Verbose syntax?  
Compare,

```julia
fit(x, mean)

```

or

```julia
wts = aweights(w)
fit(x, mean, wts)

```

to

```julia
o = WeightedMean()
wts = aweights(w)
fit!(o, x, wts)

```

About the extra dependency, OnlineStats which is a dependency of WeightedOnlineStats already has StatsBase as a dependency so there is no extra dependency involved.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [December 7, 2018, 6:55pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/11 "2018-12-07T18:55:08Z")

</div>

This is not how `OnlineStats` works.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [December 7, 2018, 7:15pm UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/12 "2018-12-07T19:15:31Z")

</div>

Just giving suggestions, take what you think is good. Just do make the case clear since the dependency or verbosity arguments aren’t that compelling. There maybe be other valid reasons.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [January 8, 2019, 9:49am UTC](https://discourse.julialang.org/t/ann-weightedonlinestats-jl/18317/13 "2019-01-08T09:49:38Z")

</div>

FYI: [here](https://github.com/joshday/OnlineStats.jl/issues/129) is the Github issue discussing this.
