# Announcing plstat - statistics using prolog

**URL:** https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960
**Category:** General
**Created:** [May 15, 2021, 2:47pm UTC](https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960 "2021-05-15T14:47:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![damiazz94](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@damiazz94](https://swi-prolog.discourse.group/u/damiazz94)
#### Post date: [May 15, 2021, 2:47pm UTC](https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960/1 "2021-05-15T14:47:27Z")

</div>

Hi,  
I’ve gathered some of the predicates related to statistics (mean, variance, correlation, skew etc) plus some utility I usually use in a package called plstat, maybe it can be useful to someone. They are all written in prolog. You can have a look at this link: [GitHub - damianoazzolini/plstat: Statistics using prolog](https://github.com/damianoazzolini/plstat)

If you have suggestions about anything (fixing code style, more predicates to add, ecc) or spot some bugs, please let me kow!

---

<div class="post-metadata">

### Author: ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)
#### Post date: [May 15, 2021, 6:01pm UTC](https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960/2 "2021-05-15T18:01:46Z")

</div>

Looks very good, thanks for sharing it. In particular it is very useful that you provide examples in the documentation for each of the predicates.

I would suggest you use `%! somepred(A,B) ...` in the documentation instead of `/* ... */`, so that the documentation is nicely produced on a webpage by [pldoc](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/pldoc.html%27)) (just like the docs for swi-prolog).

---

<div class="post-metadata">

### Author: ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)
#### Post date: [May 17, 2021, 9:52pm UTC](https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960/3 "2021-05-17T21:52:06Z")

</div>

> [@damiazz94](#):
>
> I’ve gathered some of the predicates related to statistics

I’m no statistician but it strikes me as it might be helpful to have a functional form of these predicates for use in arithmetic expressions. They’re certainly in the right format, i.e., return value is the last argument.

For example:

```prolog
?- S is sum([1,24,2,3,-1]).
S = 29.

?- std_dev([1,2,4,6,7,8,9]) > 2.
true.

?- N=10, prod(seq(1,N,1))=:=factorial(N).
N = 10.

```

I’ve been looking at this issue for arithmetic operations on arrays, e.g., for solving the matrix form of a linear system of equations. But maybe it’s not so applicable to the statistics domain.

If you think it’s worth exploring, I’ve published a pack at [https://github.com/ridgeworks/arithmetic\_types](https://github.com/ridgeworks/arithmetic_types) which may help.

---

<div class="post-metadata">

### Author: ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)
#### Post date: [May 24, 2021, 1:00pm UTC](https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960/5 "2021-05-24T13:00:33Z")

</div>

> [@anon95304481](#):
>
> I guess one would then rather do:
> 
> ```prolog
> aggregate_all((count,sum(X)), ... data ..., (N,S)),
> Mean is S/N.
> 
> ```

I am not sure that this does what you think it does. My understanding is that with the current implementation, this will also materialize the list (using findall?). The source is here:

> <https://github.com/SWI-Prolog/swipl-devel/blob/4e808fb378bc64b36d63dbc8ac87688f401219b3/library/aggregate.pl>

---

<div class="post-metadata">

### Author: ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)
#### Post date: [May 24, 2021, 1:22pm UTC](https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960/7 "2021-05-24T13:22:34Z")

</div>

Well you are now not showing the same thing that you showed above. This will not materialize a list:

```prolog
aggregate_all(sum(X), ..., Sum),
aggregate_all(count, ..., N),
Mean is Sum / N

```

but it will obviously iterate twice.

I was commenting on this formulation, again:

> [@anon95304481](#):
>
> I guess one would then rather do:
> 
> ```prolog
> aggregate_all((count,sum(X)), ... data ..., (N,S)),
> Mean is S/N.
> 
> ```

I thought that this will go [here](https://github.com/SWI-Prolog/swipl-devel/blob/4e808fb378bc64b36d63dbc8ac87688f401219b3/library/aggregate.pl#L253-L256), and there I do see a findall.

---

<div class="post-metadata">

### Author: ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)
#### Post date: [May 24, 2021, 1:33pm UTC](https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960/9 "2021-05-24T13:33:26Z")

</div>

You wrote this after I wrote that 😉 either way, yes, I agree, it would be very nice to have something that:

- goes over the solutions only once, and
- has an obvious interface for plugging in your own aggregator.

It will look exactly like a fold, but for solutions instead of a list.

---

<div class="post-metadata">

### Author: ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)
#### Post date: [May 24, 2021, 1:46pm UTC](https://swi-prolog.discourse.group/t/announcing-plstat-statistics-using-prolog/3960/11 "2021-05-24T13:46:36Z")

</div>

Yes, I have tried out such things myself, and was also happy to see that it is indeed faster. I actually started reading about how to _really_ calculate the mean, and went a bit too deep. I think I gave up somewhere around this point: ~~[Kahan summation algorithm - Wikipedia](https://en.wikipedia.org/wiki/Kahan_summation_algorithm)~~ (EDIT no, it was something else that calculated the running mean as a side-effect of trying to find the standard deviation. I cannot find it any more, I decided to stop wasting time on it back then…) (One of the very few things they managed to teach me at university is a healthy fear of floating point math)

Long story short, if I actually needed to do statistics I would probably fall back to R. This is however completely orthogonal to the question of an aggregate\_all that can be used as a foldl.
