[SciPy-Dev] Rationale behind *_gen and *_frozen in _multivariate.py

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Jul 27 07:36:46 EDT 2016


On Wed, Jul 27, 2016 at 7:09 AM, Lukas Drude <mail at lukas-drude.de> wrote:
> Hello Scipy,
>
> I would like to implement additional distributions (at least locally for
> now).
>
> To do so, I looked at scipy/stats/_multivariate.py and would like to
> understand the rationale behind the *_gen and *_frozen classes.
>
> - Are the frozen-classes used to avoid parameter checks during run time?
> - Why is i.e. in line 1360 dirichlet = dirichlet_gen() [1]? It seems like an
> object is created during the import although it appears to the user as if
> scipy.stats.dirichlet was a module and scipy.stats.dirichlet.pdf() was a
> function of just that module?
>
> I do not want to change the scipy code. I would just like to know, what the
> benefits are.
>
> With best regards
> Lukas
>
> [1]
> https://github.com/scipy/scipy/blob/ffaebc9e684e5bd23bbd3d5234c27a71369990b7/scipy/stats/_multivariate.py#L1360


some history in the following, Evgeni knows better the recent changes

The original implementation of the distributions was mostly
"functional". Classes are used as namespace and to make implementation
easier, but users only used a single global instance of the
distribution classes.

Because it is only a single global instance it cannot keep state, i.e.
store intermediate results and parameters as attributes. This was a
headache and source of bugs when state spilled over in the global
instance from one use to the next.

The better design would have been to have users use the classes to
create new instances for each use.
`frozen` distributions was the way to create a new instance that
stores the parameters of the distribution.

The use of `frozen` distributions has been improved and expanded
mostly by Evgeni. It is also used more extensively in the multivariate
distributions which have only been added in the last few years. The
main advantage of frozen distributions is that by having a new
instance each time, it is possible to store intermediate results to
improve performance.

Josef




>
>
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev at scipy.org
> https://mail.scipy.org/mailman/listinfo/scipy-dev
>



More information about the SciPy-Dev mailing list