# API design and consistency for stdlib stats(\_distribution) modules

**URL:** <https://fortran-lang.discourse.group/t/api-design-and-consistency-for-stdlib-stats-distribution-modules/9614>\
**Category:** stdlib\
**Created:** [April 22, 2025, 2:15pm UTC](https://fortran-lang.discourse.group/t/api-design-and-consistency-for-stdlib-stats-distribution-modules/9614 "2025-04-22T14:15:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxm](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/fxm/32/5726_2.png) [@fxm](https://fortran-lang.discourse.group/u/fxm)\
**Post date:** [April 22, 2025, 2:15pm UTC](https://fortran-lang.discourse.group/t/api-design-and-consistency-for-stdlib-stats-distribution-modules/9614/1 "2025-04-22T14:15:10Z")

</div>

Following Monday’s discussion about [stdlib release and API stability](https://fortran-lang.discourse.group/t/fortran-monthly-call-april-2025/9562/21), I wanted to bring up a point about API design and consistency for the stats modules (with specifics from the distributions modules). I can also create an issue on github, but decided to post here, since it’s potentially a broader discussion not strictly related to one issue.

There’s some inconsistency in passed arguments for normal and exponential distributions. Consider the pdfs:

`result =` [pdf\_normal](https://stdlib.fortran-lang.org/interface/pdf_normal.html) `(x, loc, scale)`

`result =` [pdf\_exp](https://stdlib.fortran-lang.org/interface/pdf_exp.html) `(x, lambda)`

While the normal distribution procedure uses `loc` and `scale` (reminiscent of scipy) as opposed to mu and sigma, the exponential distribution procdedure uses `lambda` rather than scale (, i.e. `beta = 1/lambda`) and is missing `loc` (though I appreciate that’s not needed as often as for normal distributions).

For comparison, scipy pdf arguments are as follows:  
`scipy.stats.norm.pdf(x, loc, scale)`  
`scipy.stats.expon.pdf(x, loc, scale)`

This brings up a broader question of API design. If the goal is to make it feel familiar to scipy users ([as touched on in this discussion](https://fortran-lang.discourse.group/t/poll-naming-a-function-in-stdlib/1900)), sticking to location and scale makes sense. In this case, I’d suggest changing lambda to scale in the passed arguments. On the other hand, I found that while the scale and location convention creates consistency in arguments for different distributions, they also create some confusion (some not connecting normal distribution scale to standard deviation, for example, or taking scale and location parameters as statistically identical between distributions). It’s the reason I decided to stick with the more mathematical arguments for some of my older code [I’m currently reworking into a stats lib](https://github.com/sebastian-mutz/fsml/), while adopting procedure naming conventions more reminiscent of scipy.  
Perhaps it’s worth implementing several options, allowing users to (for example) pass either scale/beta or lambda? If you’re a scipy user with little background in statistics, scale will be intuitive and lambda potentiall confusing; if you come into it from the other side, it may be the other way around. However, there’s always the risk of things getting too bloated then. Anyway, this may be worth discussing.

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [April 23, 2025, 9:47am UTC](https://fortran-lang.discourse.group/t/api-design-and-consistency-for-stdlib-stats-distribution-modules/9614/2 "2025-04-23T09:47:37Z")

</div>

very happy to see that there is growing interest in `stdlib` and that the discussion is reaching the point where the library is considered mature, and ready for production.

My personal take is that discussion at the API level should not hinder progress: one PR with some code is worth a thousand threads!

Where code is shown in action, is where reviewers experience the actual interface and refine and improve on it. My experience so far has been very positive. I believe it makes sense to have an interface for the exponential distribution with the same API as the normal one.

If you look at the history, `stdlib` development has been kept afloat by a tiny handful of developers led by @jeremie.vandenplas’s heroic effort. Many initial discussions on the API were at some point left behind, so if you are knowledgeable in statistics, I’m sure further contribtions in the area will be very much welcome.

---

<div class="post-metadata">

**Author:** ![fxm](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/fxm/32/5726_2.png) [@fxm](https://fortran-lang.discourse.group/u/fxm)\
**Post date:** [May 8, 2025, 10:40am UTC](https://fortran-lang.discourse.group/t/api-design-and-consistency-for-stdlib-stats-distribution-modules/9614/3 "2025-05-08T10:40:43Z")

</div>

Ok. PR with suggested modifications to the procedures, API (and associated examples and tests) is opened. 🙂

---

<div class="post-metadata">

**Author:** ![fxm](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/fxm/32/5726_2.png) [@fxm](https://fortran-lang.discourse.group/u/fxm)\
**Post date:** [May 10, 2025, 5:52pm UTC](https://fortran-lang.discourse.group/t/api-design-and-consistency-for-stdlib-stats-distribution-modules/9614/4 "2025-05-10T17:52:56Z")

</div>

Just a heads-up:

Since an API change proposed in this PR ([modified stats exponential distribution procedures to use `loc` and `scale`](https://github.com/fortran-lang/stdlib/pull/991)) might impact some users, I’m linking it here to alert possible users, following @hkvzjal’s suggestion.

To summarise what I’m proposing with the PR (copied from GH):

I suggest introducing more consistency in the stats distribution procedures API. I have made some modifications to `stdlib_stats_distribution_exponential` (and associated examples and tests) to:

1. use `scale` instead of `lambda`, and
2. allow passing an additional location parameter `loc`.

With the suggested modifications in the PR, rvs, pdf and cdf procedures can now be called as such:  
`result = rvs_exp(x, loc, scale)`,  
`result = pdf_exp(x, loc, scale)`, and  
`result = cdf_exp(x, loc, scale)`.
