You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why do functions of trait Statistics take ownership on the dataset and not immutable reference ? #345
This is a question I have been wondering for a while: why all the functions in the statistics::Statistics trait take self and not &self ?
I often have the use case where I need to compute several statistics on the same dataset, like for example:
use statrs::statistics::Statistics;fnmain(){let dataset = vec![0.,1.,2.,3.,4.,5.];let mean = dataset.mean();let variance = dataset.variance();}
but I cannot do so because mean(self) function takes ownership on the dataset. The only solution I see is to clone the dataset, which in my use case is usually not wanted.
Naïvely, if I had to implement a mean() function, I would only need an immutable reference on the dataset to do so, so I am wondering if there is a specific reason for this particular design choice, and could it change in the future ?
We could certainly change this so that only the rank/order related statistics have the ownership signature. We could also use an iterator trait bound, are you able to make the change?
the online module provides an API circumventing this problem and also is more numerically stable. I planned on migrating the whole statistics API since there's some conflated aspects of the random variate statistics that don't play nicely. There's also work for comparative statistics like ks and mwu tests being worked in #406 to drop need for clone.
Activity
YeungOnion commentedon Jul 12, 2025
We could certainly change this so that only the rank/order related statistics have the ownership signature. We could also use an iterator trait bound, are you able to make the change?
HuStmpHrrr commentedon Sep 11, 2025
the same question. it doesn't make sense to take the ownership of the data structure at all.
YeungOnion commentedon Aug 18, 2026
the online module provides an API circumventing this problem and also is more numerically stable. I planned on migrating the whole statistics API since there's some conflated aspects of the random variate statistics that don't play nicely. There's also work for comparative statistics like ks and mwu tests being worked in #406 to drop need for clone.