|
28 | 28 | from pymc.distributions.dist_math import check_parameters
|
29 | 29 | from pymc.distributions.distribution import Continuous
|
30 | 30 | from pymc.distributions.shape_utils import rv_size_is_none
|
| 31 | +from pymc.logprob.utils import CheckParameterValue |
31 | 32 | from pymc.pytensorf import floatX
|
32 | 33 | from pytensor.tensor.random.op import RandomVariable
|
33 | 34 | from pytensor.tensor.variable import TensorVariable
|
@@ -280,3 +281,73 @@ def __new__(cls, name, nu, **kwargs):
|
280 | 281 | @classmethod
|
281 | 282 | def dist(cls, nu, **kwargs):
|
282 | 283 | return CustomDist.dist(nu, dist=cls.chi_dist, class_name="Chi", **kwargs)
|
| 284 | + |
| 285 | + |
| 286 | +class Maxwell: |
| 287 | + R""" |
| 288 | + The Maxwell-Boltzmann distribution |
| 289 | +
|
| 290 | + The pdf of this distribution is |
| 291 | +
|
| 292 | + .. math:: |
| 293 | +
|
| 294 | + f(x \mid a) = {\displaystyle {\sqrt {\frac {2}{\pi }}}\,{\frac {x^{2}}{a^{3}}}\,\exp \left({\frac {-x^{2}}{2a^{2}}}\right)} |
| 295 | +
|
| 296 | + Read more about it on `Wikipedia <https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution>`_ |
| 297 | +
|
| 298 | + .. plot:: |
| 299 | + :context: close-figs |
| 300 | +
|
| 301 | + import matplotlib.pyplot as plt |
| 302 | + import numpy as np |
| 303 | + import scipy.stats as st |
| 304 | + import arviz as az |
| 305 | + plt.style.use('arviz-darkgrid') |
| 306 | + x = np.linspace(0, 20, 200) |
| 307 | + for a in [1, 2, 5]: |
| 308 | + pdf = st.maxwell.pdf(x, scale=a) |
| 309 | + plt.plot(x, pdf, label=r'$a$ = {}'.format(a)) |
| 310 | + plt.xlabel('x', fontsize=12) |
| 311 | + plt.ylabel('f(x)', fontsize=12) |
| 312 | + plt.legend(loc=1) |
| 313 | + plt.show() |
| 314 | +
|
| 315 | + ======== ========================================================================= |
| 316 | + Support :math:`x \in (0, \infty)` |
| 317 | + Mean :math:`2a \sqrt{\frac{2}{\pi}}` |
| 318 | + Variance :math:`\frac{a^2(3 \pi - 8)}{\pi}` |
| 319 | + ======== ========================================================================= |
| 320 | +
|
| 321 | + Parameters |
| 322 | + ---------- |
| 323 | + a : tensor_like of float |
| 324 | + Scale parameter (a > 0). |
| 325 | +
|
| 326 | + """ |
| 327 | + |
| 328 | + @staticmethod |
| 329 | + def maxwell_dist(a: TensorVariable, size: TensorVariable) -> TensorVariable: |
| 330 | + if rv_size_is_none(size): |
| 331 | + size = a.shape |
| 332 | + |
| 333 | + a = CheckParameterValue("a > 0")(a, pt.all(pt.gt(a, 0))) |
| 334 | + |
| 335 | + return Chi.dist(nu=3, size=size) * a |
| 336 | + |
| 337 | + def __new__(cls, name, a, **kwargs): |
| 338 | + return CustomDist( |
| 339 | + name, |
| 340 | + a, |
| 341 | + dist=cls.maxwell_dist, |
| 342 | + class_name="Maxwell", |
| 343 | + **kwargs, |
| 344 | + ) |
| 345 | + |
| 346 | + @classmethod |
| 347 | + def dist(cls, a, **kwargs): |
| 348 | + return CustomDist.dist( |
| 349 | + a, |
| 350 | + dist=cls.maxwell_dist, |
| 351 | + class_name="Maxwell", |
| 352 | + **kwargs, |
| 353 | + ) |
0 commit comments